Backend (Go): - Project structure with chi router, pgxpool, goose migrations - JWT auth (access/refresh tokens) with Firebase token verification - NoopTokenVerifier for local dev without Firebase credentials - PostgreSQL user repository with atomic profile updates (transactions) - Mifflin-St Jeor calorie calculation based on profile data - REST API: POST /auth/login, /auth/refresh, /auth/logout, GET/PUT /profile, GET /health - Middleware: auth, CORS (localhost wildcard), logging, recovery, request_id - Unit tests (51 passing) and integration tests (testcontainers) - Docker Compose setup with postgres healthcheck and graceful shutdown Flutter client: - Riverpod state management with GoRouter navigation - Firebase Auth (email/password + Google sign-in with web popup support) - Platform-aware API URLs (web/Android/iOS) - Dio HTTP client with JWT auth interceptor and concurrent refresh handling - Secure token storage - Screens: Login, Register, Home (tabs: Menu, Recipes, Products, Profile) - Unit tests (17 passing) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
160 lines
4.3 KiB
Dart
160 lines
4.3 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:food_ai/shared/models/user.dart';
|
|
|
|
void main() {
|
|
group('User.fromJson', () {
|
|
test('deserializes full object', () {
|
|
final json = {
|
|
'id': 'user-1',
|
|
'email': 'test@example.com',
|
|
'name': 'Test User',
|
|
'avatar_url': 'https://avatar.url/pic.jpg',
|
|
'height_cm': 180,
|
|
'weight_kg': 75.5,
|
|
'age': 28,
|
|
'gender': 'male',
|
|
'activity': 'moderate',
|
|
'goal': 'maintain',
|
|
'daily_calories': 2500,
|
|
'plan': 'free',
|
|
'preferences': {'cuisines': ['russian']},
|
|
};
|
|
|
|
final user = User.fromJson(json);
|
|
|
|
expect(user.id, 'user-1');
|
|
expect(user.email, 'test@example.com');
|
|
expect(user.name, 'Test User');
|
|
expect(user.avatarUrl, 'https://avatar.url/pic.jpg');
|
|
expect(user.heightCm, 180);
|
|
expect(user.weightKg, 75.5);
|
|
expect(user.age, 28);
|
|
expect(user.gender, 'male');
|
|
expect(user.activity, 'moderate');
|
|
expect(user.goal, 'maintain');
|
|
expect(user.dailyCalories, 2500);
|
|
expect(user.plan, 'free');
|
|
expect(user.preferences['cuisines'], ['russian']);
|
|
});
|
|
|
|
test('deserializes minimal object (required fields only)', () {
|
|
final json = {
|
|
'id': 'user-2',
|
|
'email': 'min@example.com',
|
|
'name': 'Min User',
|
|
'plan': 'paid',
|
|
};
|
|
|
|
final user = User.fromJson(json);
|
|
|
|
expect(user.id, 'user-2');
|
|
expect(user.email, 'min@example.com');
|
|
expect(user.name, 'Min User');
|
|
expect(user.plan, 'paid');
|
|
expect(user.avatarUrl, isNull);
|
|
expect(user.heightCm, isNull);
|
|
expect(user.weightKg, isNull);
|
|
expect(user.age, isNull);
|
|
expect(user.gender, isNull);
|
|
expect(user.dailyCalories, isNull);
|
|
expect(user.preferences, isEmpty);
|
|
});
|
|
|
|
test('handles snake_case to camelCase mapping', () {
|
|
final json = {
|
|
'id': 'user-3',
|
|
'email': 'test@example.com',
|
|
'name': 'Test',
|
|
'plan': 'free',
|
|
'avatar_url': 'https://pic.url',
|
|
'height_cm': 170,
|
|
'weight_kg': 65.0,
|
|
'daily_calories': 2000,
|
|
};
|
|
|
|
final user = User.fromJson(json);
|
|
|
|
expect(user.avatarUrl, 'https://pic.url');
|
|
expect(user.heightCm, 170);
|
|
expect(user.weightKg, 65.0);
|
|
expect(user.dailyCalories, 2000);
|
|
});
|
|
|
|
test('roundtrip fromJson -> toJson -> fromJson', () {
|
|
final original = User(
|
|
id: 'user-4',
|
|
email: 'roundtrip@example.com',
|
|
name: 'Roundtrip',
|
|
avatarUrl: 'https://avatar.url',
|
|
heightCm: 175,
|
|
weightKg: 70.0,
|
|
age: 30,
|
|
gender: 'female',
|
|
activity: 'high',
|
|
goal: 'lose',
|
|
dailyCalories: 1800,
|
|
plan: 'paid',
|
|
preferences: {'cuisines': ['asian']},
|
|
);
|
|
|
|
final json = original.toJson();
|
|
final restored = User.fromJson(json);
|
|
|
|
expect(restored.id, original.id);
|
|
expect(restored.email, original.email);
|
|
expect(restored.name, original.name);
|
|
expect(restored.avatarUrl, original.avatarUrl);
|
|
expect(restored.heightCm, original.heightCm);
|
|
expect(restored.weightKg, original.weightKg);
|
|
expect(restored.age, original.age);
|
|
expect(restored.gender, original.gender);
|
|
expect(restored.plan, original.plan);
|
|
expect(restored.dailyCalories, original.dailyCalories);
|
|
});
|
|
|
|
test('ignores unknown fields', () {
|
|
final json = {
|
|
'id': 'user-5',
|
|
'email': 'test@example.com',
|
|
'name': 'Test',
|
|
'plan': 'free',
|
|
'unknown_field': 42,
|
|
'another_unknown': 'hello',
|
|
};
|
|
|
|
final user = User.fromJson(json);
|
|
|
|
expect(user.id, 'user-5');
|
|
expect(user.email, 'test@example.com');
|
|
});
|
|
});
|
|
|
|
group('User.hasCompletedOnboarding', () {
|
|
test('returns false when body params are missing', () {
|
|
const user = User(
|
|
id: '1',
|
|
email: 'test@test.com',
|
|
name: 'Test',
|
|
plan: 'free',
|
|
);
|
|
|
|
expect(user.hasCompletedOnboarding, isFalse);
|
|
});
|
|
|
|
test('returns true when all body params are present', () {
|
|
const user = User(
|
|
id: '1',
|
|
email: 'test@test.com',
|
|
name: 'Test',
|
|
plan: 'free',
|
|
heightCm: 180,
|
|
weightKg: 75.0,
|
|
age: 28,
|
|
gender: 'male',
|
|
);
|
|
|
|
expect(user.hasCompletedOnboarding, isTrue);
|
|
});
|
|
});
|
|
}
|