mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-23 09:26:42 +02:00
✨ feat(nutriphi): prepare for production release with tests and improved UX
- Remove broken header links to non-existent settings/profile pages
- Replace header links with settings page link
- Remove TODO comments for credit system in analysis controller
- Add comprehensive error handling with German messages in meals store
- Add loading states, retry buttons, and error displays in UI components
- Create new settings page with daily goals editor
- Add 99 tests across backend, web, and shared packages:
- Backend: MealService, GoalsService, StatsService, FavoritesService,
RecommendationsService, nutrition.utils (Jest)
- Web: API client tests with mocks (Vitest)
- Shared: utility function tests (Vitest)
- Set up test infrastructure (Jest for NestJS, Vitest for SvelteKit)
This commit is contained in:
parent
ee630158c5
commit
3ff8d3833b
28 changed files with 2470 additions and 119 deletions
14
apps/nutriphi/apps/backend/jest.config.js
Normal file
14
apps/nutriphi/apps/backend/jest.config.js
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
module.exports = {
|
||||
moduleFileExtensions: ['js', 'json', 'ts'],
|
||||
rootDir: 'src',
|
||||
testRegex: '.*\\.spec\\.ts$',
|
||||
transform: {
|
||||
'^.+\\.(t|j)s$': 'ts-jest',
|
||||
},
|
||||
collectCoverageFrom: ['**/*.(t|j)s', '!**/*.module.ts', '!**/main.ts', '!**/db/**'],
|
||||
coverageDirectory: '../coverage',
|
||||
testEnvironment: 'node',
|
||||
moduleNameMapper: {
|
||||
'^@nutriphi/shared$': '<rootDir>/../../packages/shared/src',
|
||||
},
|
||||
};
|
||||
|
|
@ -12,6 +12,10 @@
|
|||
"start:prod": "node dist/main",
|
||||
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
||||
"type-check": "tsc --noEmit",
|
||||
"test": "jest",
|
||||
"test:watch": "jest --watch",
|
||||
"test:cov": "jest --coverage",
|
||||
"test:e2e": "jest --config ./test/jest-e2e.json",
|
||||
"migration:generate": "drizzle-kit generate",
|
||||
"migration:run": "tsx src/db/migrate.ts",
|
||||
"db:push": "drizzle-kit push",
|
||||
|
|
@ -38,15 +42,19 @@
|
|||
"devDependencies": {
|
||||
"@nestjs/cli": "^10.4.9",
|
||||
"@nestjs/schematics": "^10.2.3",
|
||||
"@nestjs/testing": "^10.4.15",
|
||||
"@types/express": "^5.0.0",
|
||||
"@types/jest": "^29.5.14",
|
||||
"@types/node": "^22.10.2",
|
||||
"@typescript-eslint/eslint-plugin": "^8.18.1",
|
||||
"@typescript-eslint/parser": "^8.18.1",
|
||||
"eslint": "^9.17.0",
|
||||
"eslint-config-prettier": "^9.1.0",
|
||||
"eslint-plugin-prettier": "^5.2.1",
|
||||
"jest": "^29.7.0",
|
||||
"prettier": "^3.4.2",
|
||||
"source-map-support": "^0.5.21",
|
||||
"ts-jest": "^29.2.5",
|
||||
"ts-loader": "^9.5.1",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsconfig-paths": "^4.2.0",
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ export class AnalysisController {
|
|||
|
||||
@Post('photo')
|
||||
async analyzePhoto(@CurrentUser() _user: CurrentUserData, @Body() dto: AnalyzePhotoDto) {
|
||||
// TODO: Deduct credits from user account
|
||||
try {
|
||||
return await this.analysisService.analyzePhoto(dto.imageBase64, dto.mimeType);
|
||||
} catch (error) {
|
||||
|
|
@ -36,7 +35,6 @@ export class AnalysisController {
|
|||
|
||||
@Post('text')
|
||||
async analyzeText(@CurrentUser() _user: CurrentUserData, @Body() dto: AnalyzeTextDto) {
|
||||
// TODO: Deduct credits from user account
|
||||
try {
|
||||
return await this.analysisService.analyzeText(dto.description);
|
||||
} catch (error) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,168 @@
|
|||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { FavoritesService } from './favorites.service';
|
||||
import { DATABASE_CONNECTION } from '../db/database.module';
|
||||
|
||||
describe('FavoritesService', () => {
|
||||
let service: FavoritesService;
|
||||
let mockDb: any;
|
||||
|
||||
const mockFavorite = {
|
||||
id: 'fav-1',
|
||||
userId: 'user-1',
|
||||
name: 'Lieblings-Spaghetti',
|
||||
nutrition: { calories: 650, protein: 25, carbohydrates: 80, fat: 20 },
|
||||
usageCount: 5,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
mockDb = {
|
||||
select: jest.fn().mockReturnThis(),
|
||||
from: jest.fn().mockReturnThis(),
|
||||
where: jest.fn().mockReturnThis(),
|
||||
orderBy: jest.fn().mockResolvedValue([mockFavorite]),
|
||||
limit: jest.fn().mockResolvedValue([mockFavorite]),
|
||||
insert: jest.fn().mockReturnThis(),
|
||||
values: jest.fn().mockReturnThis(),
|
||||
returning: jest.fn().mockResolvedValue([mockFavorite]),
|
||||
update: jest.fn().mockReturnThis(),
|
||||
set: jest.fn().mockReturnThis(),
|
||||
delete: jest.fn().mockReturnThis(),
|
||||
};
|
||||
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
FavoritesService,
|
||||
{
|
||||
provide: DATABASE_CONNECTION,
|
||||
useValue: mockDb,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<FavoritesService>(FavoritesService);
|
||||
});
|
||||
|
||||
describe('findAll', () => {
|
||||
it('should return all favorites ordered by usage count', async () => {
|
||||
const favorites = [
|
||||
{ ...mockFavorite, id: 'fav-1', usageCount: 10 },
|
||||
{ ...mockFavorite, id: 'fav-2', usageCount: 5 },
|
||||
];
|
||||
mockDb.orderBy.mockResolvedValueOnce(favorites);
|
||||
|
||||
const result = await service.findAll('user-1');
|
||||
|
||||
expect(mockDb.select).toHaveBeenCalled();
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result[0].usageCount).toBeGreaterThan(result[1].usageCount);
|
||||
});
|
||||
|
||||
it('should return empty array when no favorites', async () => {
|
||||
mockDb.orderBy.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await service.findAll('user-1');
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('should create a favorite with usageCount 0', async () => {
|
||||
const newFavorite = { ...mockFavorite, usageCount: 0 };
|
||||
mockDb.returning.mockResolvedValueOnce([newFavorite]);
|
||||
|
||||
const result = await service.create('user-1', {
|
||||
name: 'Lieblings-Spaghetti',
|
||||
description: 'Leckere Spaghetti mit Bolognese',
|
||||
mealType: 'lunch',
|
||||
nutrition: { calories: 650, protein: 25, carbohydrates: 80, fat: 20 },
|
||||
});
|
||||
|
||||
expect(mockDb.insert).toHaveBeenCalled();
|
||||
expect(mockDb.values).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ usageCount: 0, userId: 'user-1' })
|
||||
);
|
||||
expect(result.usageCount).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('incrementUsage', () => {
|
||||
it('should increment usage count', async () => {
|
||||
const updatedFavorite = { ...mockFavorite, usageCount: 6 };
|
||||
mockDb.returning.mockResolvedValueOnce([updatedFavorite]);
|
||||
|
||||
const result = await service.incrementUsage('user-1', 'fav-1');
|
||||
|
||||
expect(mockDb.update).toHaveBeenCalled();
|
||||
expect(result?.usageCount).toBe(6);
|
||||
});
|
||||
|
||||
it('should return null if favorite not found', async () => {
|
||||
mockDb.limit.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await service.incrementUsage('user-1', 'fav-999');
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should not increment for wrong user', async () => {
|
||||
mockDb.limit.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await service.incrementUsage('user-2', 'fav-1');
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete', () => {
|
||||
it('should delete favorite and return deleted record', async () => {
|
||||
mockDb.returning.mockResolvedValueOnce([mockFavorite]);
|
||||
|
||||
const result = await service.delete('user-1', 'fav-1');
|
||||
|
||||
expect(mockDb.delete).toHaveBeenCalled();
|
||||
expect(result).toEqual(mockFavorite);
|
||||
});
|
||||
|
||||
it('should return undefined if not found', async () => {
|
||||
mockDb.returning.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await service.delete('user-1', 'fav-999');
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('update', () => {
|
||||
it('should update favorite fields', async () => {
|
||||
const updatedFavorite = { ...mockFavorite, name: 'Neue Spaghetti' };
|
||||
mockDb.returning.mockResolvedValueOnce([updatedFavorite]);
|
||||
|
||||
const result = await service.update('user-1', 'fav-1', { name: 'Neue Spaghetti' });
|
||||
|
||||
expect(mockDb.update).toHaveBeenCalled();
|
||||
expect(result?.name).toBe('Neue Spaghetti');
|
||||
});
|
||||
|
||||
it('should set updatedAt timestamp', async () => {
|
||||
const before = new Date();
|
||||
mockDb.returning.mockResolvedValueOnce([{ ...mockFavorite, updatedAt: new Date() }]);
|
||||
|
||||
await service.update('user-1', 'fav-1', { name: 'Updated' });
|
||||
|
||||
expect(mockDb.set).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ updatedAt: expect.any(Date) })
|
||||
);
|
||||
});
|
||||
|
||||
it('should return undefined if not found', async () => {
|
||||
mockDb.returning.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await service.update('user-1', 'fav-999', { name: 'Updated' });
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
112
apps/nutriphi/apps/backend/src/goals/goals.service.spec.ts
Normal file
112
apps/nutriphi/apps/backend/src/goals/goals.service.spec.ts
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { GoalsService } from './goals.service';
|
||||
import { DATABASE_CONNECTION } from '../db/database.module';
|
||||
|
||||
describe('GoalsService', () => {
|
||||
let service: GoalsService;
|
||||
let mockDb: any;
|
||||
|
||||
const mockGoals = {
|
||||
id: 'goal-1',
|
||||
userId: 'user-1',
|
||||
dailyCalories: 2000,
|
||||
dailyProtein: 50,
|
||||
dailyCarbs: 275,
|
||||
dailyFat: 78,
|
||||
dailyFiber: 28,
|
||||
createdAt: new Date('2024-01-01'),
|
||||
updatedAt: new Date('2024-01-01'),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
mockDb = {
|
||||
select: jest.fn().mockReturnThis(),
|
||||
from: jest.fn().mockReturnThis(),
|
||||
where: jest.fn().mockReturnThis(),
|
||||
limit: jest.fn().mockResolvedValue([mockGoals]),
|
||||
insert: jest.fn().mockReturnThis(),
|
||||
values: jest.fn().mockReturnThis(),
|
||||
returning: jest.fn().mockResolvedValue([mockGoals]),
|
||||
update: jest.fn().mockReturnThis(),
|
||||
set: jest.fn().mockReturnThis(),
|
||||
delete: jest.fn().mockReturnThis(),
|
||||
};
|
||||
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
GoalsService,
|
||||
{
|
||||
provide: DATABASE_CONNECTION,
|
||||
useValue: mockDb,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<GoalsService>(GoalsService);
|
||||
});
|
||||
|
||||
describe('getGoals', () => {
|
||||
it('should return goals for a user', async () => {
|
||||
const result = await service.getGoals('user-1');
|
||||
|
||||
expect(mockDb.select).toHaveBeenCalled();
|
||||
expect(result).toEqual(mockGoals);
|
||||
});
|
||||
|
||||
it('should return null if no goals found', async () => {
|
||||
mockDb.limit.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await service.getGoals('user-2');
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('createOrUpdate', () => {
|
||||
const newGoalsData = {
|
||||
dailyCalories: 2500,
|
||||
dailyProtein: 100,
|
||||
dailyCarbs: 300,
|
||||
dailyFat: 80,
|
||||
dailyFiber: 30,
|
||||
};
|
||||
|
||||
it('should create new goals if none exist', async () => {
|
||||
mockDb.limit.mockResolvedValueOnce([]); // getGoals returns null
|
||||
|
||||
const result = await service.createOrUpdate('user-1', newGoalsData);
|
||||
|
||||
expect(mockDb.insert).toHaveBeenCalled();
|
||||
expect(result).toEqual(mockGoals);
|
||||
});
|
||||
|
||||
it('should update existing goals', async () => {
|
||||
const updatedGoals = { ...mockGoals, ...newGoalsData };
|
||||
mockDb.returning.mockResolvedValueOnce([updatedGoals]);
|
||||
|
||||
const result = await service.createOrUpdate('user-1', newGoalsData);
|
||||
|
||||
expect(mockDb.update).toHaveBeenCalled();
|
||||
expect(result).toEqual(updatedGoals);
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete', () => {
|
||||
it('should delete goals and return deleted record', async () => {
|
||||
mockDb.returning.mockResolvedValueOnce([mockGoals]);
|
||||
|
||||
const result = await service.delete('user-1');
|
||||
|
||||
expect(mockDb.delete).toHaveBeenCalled();
|
||||
expect(result).toEqual(mockGoals);
|
||||
});
|
||||
|
||||
it('should return undefined if no goals to delete', async () => {
|
||||
mockDb.returning.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await service.delete('user-2');
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
221
apps/nutriphi/apps/backend/src/meal/meal.service.spec.ts
Normal file
221
apps/nutriphi/apps/backend/src/meal/meal.service.spec.ts
Normal file
|
|
@ -0,0 +1,221 @@
|
|||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { MealService } from './meal.service';
|
||||
import { DATABASE_CONNECTION } from '../db/database.module';
|
||||
|
||||
describe('MealService', () => {
|
||||
let service: MealService;
|
||||
let mockDb: any;
|
||||
|
||||
const mockMeal = {
|
||||
id: 'meal-1',
|
||||
userId: 'user-1',
|
||||
date: new Date('2024-01-15T12:00:00Z'),
|
||||
mealType: 'lunch',
|
||||
inputType: 'photo',
|
||||
description: 'Spaghetti Bolognese',
|
||||
confidence: 0.95,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
const mockNutrition = {
|
||||
id: 'nutrition-1',
|
||||
mealId: 'meal-1',
|
||||
calories: 650,
|
||||
protein: 25,
|
||||
carbohydrates: 80,
|
||||
fat: 20,
|
||||
fiber: 5,
|
||||
sugar: 8,
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
mockDb = {
|
||||
select: jest.fn().mockReturnThis(),
|
||||
from: jest.fn().mockReturnThis(),
|
||||
leftJoin: jest.fn().mockReturnThis(),
|
||||
where: jest.fn().mockReturnThis(),
|
||||
orderBy: jest.fn().mockReturnThis(),
|
||||
limit: jest.fn().mockResolvedValue([{ meals: mockMeal, meal_nutrition: mockNutrition }]),
|
||||
insert: jest.fn().mockReturnThis(),
|
||||
values: jest.fn().mockReturnThis(),
|
||||
returning: jest.fn(),
|
||||
update: jest.fn().mockReturnThis(),
|
||||
set: jest.fn().mockReturnThis(),
|
||||
delete: jest.fn().mockReturnThis(),
|
||||
};
|
||||
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
MealService,
|
||||
{
|
||||
provide: DATABASE_CONNECTION,
|
||||
useValue: mockDb,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<MealService>(MealService);
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('should create a meal with nutrition data', async () => {
|
||||
mockDb.returning
|
||||
.mockResolvedValueOnce([mockMeal]) // First call for meal insert
|
||||
.mockResolvedValueOnce([mockNutrition]); // Second call for nutrition insert
|
||||
|
||||
const mealData = {
|
||||
date: new Date(),
|
||||
mealType: 'lunch',
|
||||
inputType: 'photo',
|
||||
description: 'Spaghetti Bolognese',
|
||||
confidence: 0.95,
|
||||
};
|
||||
|
||||
const nutritionData = {
|
||||
calories: 650,
|
||||
protein: 25,
|
||||
carbohydrates: 80,
|
||||
fat: 20,
|
||||
};
|
||||
|
||||
const result = await service.create('user-1', mealData as any, nutritionData as any);
|
||||
|
||||
expect(mockDb.insert).toHaveBeenCalledTimes(2);
|
||||
expect(result).toEqual({ ...mockMeal, nutrition: mockNutrition });
|
||||
});
|
||||
});
|
||||
|
||||
describe('findByDate', () => {
|
||||
it('should return meals for a specific date', async () => {
|
||||
mockDb.orderBy.mockResolvedValueOnce([{ meals: mockMeal, meal_nutrition: mockNutrition }]);
|
||||
|
||||
const result = await service.findByDate('user-1', new Date('2024-01-15'));
|
||||
|
||||
expect(mockDb.select).toHaveBeenCalled();
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]).toEqual({ ...mockMeal, nutrition: mockNutrition });
|
||||
});
|
||||
|
||||
it('should return empty array when no meals found', async () => {
|
||||
mockDb.orderBy.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await service.findByDate('user-1', new Date('2024-01-16'));
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should handle meals without nutrition data', async () => {
|
||||
mockDb.orderBy.mockResolvedValueOnce([{ meals: mockMeal, meal_nutrition: null }]);
|
||||
|
||||
const result = await service.findByDate('user-1', new Date('2024-01-15'));
|
||||
|
||||
expect(result[0].nutrition).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('findByDateRange', () => {
|
||||
it('should return meals within date range', async () => {
|
||||
const meal1 = { ...mockMeal, id: 'meal-1', date: new Date('2024-01-15') };
|
||||
const meal2 = { ...mockMeal, id: 'meal-2', date: new Date('2024-01-16') };
|
||||
|
||||
mockDb.orderBy.mockResolvedValueOnce([
|
||||
{ meals: meal2, meal_nutrition: mockNutrition },
|
||||
{ meals: meal1, meal_nutrition: mockNutrition },
|
||||
]);
|
||||
|
||||
const result = await service.findByDateRange(
|
||||
'user-1',
|
||||
new Date('2024-01-15'),
|
||||
new Date('2024-01-17')
|
||||
);
|
||||
|
||||
expect(result).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findOne', () => {
|
||||
it('should return a single meal with nutrition', async () => {
|
||||
const result = await service.findOne('user-1', 'meal-1');
|
||||
|
||||
expect(result).toEqual({ ...mockMeal, nutrition: mockNutrition });
|
||||
});
|
||||
|
||||
it('should return null if meal not found', async () => {
|
||||
mockDb.limit.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await service.findOne('user-1', 'meal-999');
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null if meal belongs to different user', async () => {
|
||||
mockDb.limit.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await service.findOne('user-2', 'meal-1');
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete', () => {
|
||||
it('should delete meal and return deleted record', async () => {
|
||||
mockDb.returning.mockResolvedValueOnce([mockMeal]);
|
||||
|
||||
const result = await service.delete('user-1', 'meal-1');
|
||||
|
||||
expect(mockDb.delete).toHaveBeenCalled();
|
||||
expect(result).toEqual(mockMeal);
|
||||
});
|
||||
|
||||
it('should return undefined if meal not found', async () => {
|
||||
mockDb.returning.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await service.delete('user-1', 'meal-999');
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should not delete meal of different user', async () => {
|
||||
mockDb.returning.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await service.delete('user-2', 'meal-1');
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('update', () => {
|
||||
it('should update meal data', async () => {
|
||||
const updatedMeal = { ...mockMeal, description: 'Updated description' };
|
||||
mockDb.returning.mockResolvedValueOnce([updatedMeal]);
|
||||
mockDb.limit.mockResolvedValueOnce([{ meals: updatedMeal, meal_nutrition: mockNutrition }]);
|
||||
|
||||
const result = await service.update('user-1', 'meal-1', {
|
||||
description: 'Updated description',
|
||||
});
|
||||
|
||||
expect(mockDb.update).toHaveBeenCalled();
|
||||
expect(result?.description).toBe('Updated description');
|
||||
});
|
||||
|
||||
it('should update meal and nutrition data', async () => {
|
||||
const updatedMeal = { ...mockMeal, description: 'Updated' };
|
||||
const updatedNutrition = { ...mockNutrition, calories: 700 };
|
||||
mockDb.returning.mockResolvedValueOnce([updatedMeal]);
|
||||
mockDb.limit.mockResolvedValueOnce([
|
||||
{ meals: updatedMeal, meal_nutrition: updatedNutrition },
|
||||
]);
|
||||
|
||||
const result = await service.update(
|
||||
'user-1',
|
||||
'meal-1',
|
||||
{ description: 'Updated' },
|
||||
{ calories: 700 }
|
||||
);
|
||||
|
||||
expect(mockDb.update).toHaveBeenCalledTimes(2);
|
||||
expect(result?.nutrition?.calories).toBe(700);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,225 @@
|
|||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { RecommendationsService } from './recommendations.service';
|
||||
import { DATABASE_CONNECTION } from '../db/database.module';
|
||||
|
||||
describe('RecommendationsService', () => {
|
||||
let service: RecommendationsService;
|
||||
let mockDb: any;
|
||||
|
||||
const mockRecommendation = {
|
||||
id: 'rec-1',
|
||||
userId: 'user-1',
|
||||
date: new Date(),
|
||||
type: 'hint',
|
||||
priority: 'medium',
|
||||
message: 'Deine Proteinaufnahme ist heute niedrig.',
|
||||
nutrient: 'protein',
|
||||
actionable: 'Füge mehr proteinreiche Lebensmittel hinzu',
|
||||
dismissed: false,
|
||||
createdAt: new Date(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
mockDb = {
|
||||
select: jest.fn().mockReturnThis(),
|
||||
from: jest.fn().mockReturnThis(),
|
||||
where: jest.fn().mockReturnThis(),
|
||||
orderBy: jest.fn().mockReturnThis(),
|
||||
limit: jest.fn().mockResolvedValue([mockRecommendation]),
|
||||
insert: jest.fn().mockReturnThis(),
|
||||
values: jest.fn().mockReturnThis(),
|
||||
returning: jest.fn().mockResolvedValue([mockRecommendation]),
|
||||
update: jest.fn().mockReturnThis(),
|
||||
set: jest.fn().mockReturnThis(),
|
||||
};
|
||||
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
RecommendationsService,
|
||||
{
|
||||
provide: DATABASE_CONNECTION,
|
||||
useValue: mockDb,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<RecommendationsService>(RecommendationsService);
|
||||
});
|
||||
|
||||
describe('findByDate', () => {
|
||||
it('should return non-dismissed recommendations', async () => {
|
||||
const recommendations = [mockRecommendation, { ...mockRecommendation, id: 'rec-2' }];
|
||||
mockDb.limit.mockResolvedValueOnce(recommendations);
|
||||
|
||||
const result = await service.findByDate('user-1', new Date());
|
||||
|
||||
expect(mockDb.select).toHaveBeenCalled();
|
||||
expect(result).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should limit to 10 results', async () => {
|
||||
await service.findByDate('user-1', new Date());
|
||||
|
||||
expect(mockDb.limit).toHaveBeenCalledWith(10);
|
||||
});
|
||||
|
||||
it('should return empty array when no recommendations', async () => {
|
||||
mockDb.limit.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await service.findByDate('user-1', new Date());
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('should create a recommendation with dismissed=false', async () => {
|
||||
const newRec = {
|
||||
date: new Date(),
|
||||
type: 'hint' as const,
|
||||
priority: 'high' as const,
|
||||
message: 'Test message',
|
||||
nutrient: 'sugar',
|
||||
actionable: 'Do something',
|
||||
};
|
||||
|
||||
await service.create('user-1', newRec);
|
||||
|
||||
expect(mockDb.insert).toHaveBeenCalled();
|
||||
expect(mockDb.values).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ dismissed: false, userId: 'user-1' })
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('dismiss', () => {
|
||||
it('should mark recommendation as dismissed', async () => {
|
||||
const dismissedRec = { ...mockRecommendation, dismissed: true };
|
||||
mockDb.returning.mockResolvedValueOnce([dismissedRec]);
|
||||
|
||||
const result = await service.dismiss('user-1', 'rec-1');
|
||||
|
||||
expect(mockDb.update).toHaveBeenCalled();
|
||||
expect(mockDb.set).toHaveBeenCalledWith({ dismissed: true });
|
||||
expect(result?.dismissed).toBe(true);
|
||||
});
|
||||
|
||||
it('should return undefined if not found', async () => {
|
||||
mockDb.returning.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await service.dismiss('user-1', 'rec-999');
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('generateHints', () => {
|
||||
it('should generate low protein hint', async () => {
|
||||
const nutritionSummary = { protein: 20, fiber: 25, sugar: 30 };
|
||||
|
||||
const hints = await service.generateHints('user-1', nutritionSummary);
|
||||
|
||||
expect(hints).toHaveLength(1);
|
||||
expect(hints[0].nutrient).toBe('protein');
|
||||
expect(hints[0].priority).toBe('medium');
|
||||
expect(hints[0].message).toContain('Proteinaufnahme');
|
||||
});
|
||||
|
||||
it('should generate low fiber hint', async () => {
|
||||
const nutritionSummary = { protein: 50, fiber: 5, sugar: 30 };
|
||||
|
||||
const hints = await service.generateHints('user-1', nutritionSummary);
|
||||
|
||||
expect(hints).toHaveLength(1);
|
||||
expect(hints[0].nutrient).toBe('fiber');
|
||||
expect(hints[0].priority).toBe('low');
|
||||
expect(hints[0].message).toContain('Ballaststoffe');
|
||||
});
|
||||
|
||||
it('should generate high sugar hint', async () => {
|
||||
const nutritionSummary = { protein: 50, fiber: 25, sugar: 60 };
|
||||
|
||||
const hints = await service.generateHints('user-1', nutritionSummary);
|
||||
|
||||
expect(hints).toHaveLength(1);
|
||||
expect(hints[0].nutrient).toBe('sugar');
|
||||
expect(hints[0].priority).toBe('high');
|
||||
expect(hints[0].message).toContain('Zuckeraufnahme');
|
||||
});
|
||||
|
||||
it('should generate multiple hints when applicable', async () => {
|
||||
const nutritionSummary = { protein: 15, fiber: 5, sugar: 70 };
|
||||
|
||||
const hints = await service.generateHints('user-1', nutritionSummary);
|
||||
|
||||
expect(hints).toHaveLength(3);
|
||||
expect(hints.map((h) => h.nutrient)).toContain('protein');
|
||||
expect(hints.map((h) => h.nutrient)).toContain('fiber');
|
||||
expect(hints.map((h) => h.nutrient)).toContain('sugar');
|
||||
});
|
||||
|
||||
it('should not generate hints when nutrition is good', async () => {
|
||||
const nutritionSummary = { protein: 50, fiber: 30, sugar: 25 };
|
||||
|
||||
const hints = await service.generateHints('user-1', nutritionSummary);
|
||||
|
||||
expect(hints).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should save hints to database', async () => {
|
||||
const nutritionSummary = { protein: 10, fiber: 25, sugar: 30 };
|
||||
|
||||
await service.generateHints('user-1', nutritionSummary);
|
||||
|
||||
expect(mockDb.insert).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should handle missing nutrition values', async () => {
|
||||
const nutritionSummary = {};
|
||||
|
||||
const hints = await service.generateHints('user-1', nutritionSummary);
|
||||
|
||||
expect(hints).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should include German actionable suggestions', async () => {
|
||||
const nutritionSummary = { protein: 10 };
|
||||
|
||||
const hints = await service.generateHints('user-1', nutritionSummary);
|
||||
|
||||
expect(hints[0].actionable).toContain('Hühnchen');
|
||||
});
|
||||
|
||||
it('should check thresholds correctly - protein boundary', async () => {
|
||||
// Exactly at threshold (25) should NOT trigger
|
||||
const atThreshold = { protein: 25 };
|
||||
const hints1 = await service.generateHints('user-1', atThreshold);
|
||||
expect(hints1).toHaveLength(0);
|
||||
|
||||
// Below threshold should trigger
|
||||
const belowThreshold = { protein: 24 };
|
||||
const hints2 = await service.generateHints('user-1', belowThreshold);
|
||||
expect(hints2).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should check thresholds correctly - fiber boundary', async () => {
|
||||
const atThreshold = { fiber: 10 };
|
||||
const hints1 = await service.generateHints('user-1', atThreshold);
|
||||
expect(hints1).toHaveLength(0);
|
||||
|
||||
const belowThreshold = { fiber: 9 };
|
||||
const hints2 = await service.generateHints('user-1', belowThreshold);
|
||||
expect(hints2).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should check thresholds correctly - sugar boundary', async () => {
|
||||
const atThreshold = { sugar: 50 };
|
||||
const hints1 = await service.generateHints('user-1', atThreshold);
|
||||
expect(hints1).toHaveLength(0);
|
||||
|
||||
const aboveThreshold = { sugar: 51 };
|
||||
const hints2 = await service.generateHints('user-1', aboveThreshold);
|
||||
expect(hints2).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
239
apps/nutriphi/apps/backend/src/stats/stats.service.spec.ts
Normal file
239
apps/nutriphi/apps/backend/src/stats/stats.service.spec.ts
Normal file
|
|
@ -0,0 +1,239 @@
|
|||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { StatsService } from './stats.service';
|
||||
import { MealService } from '../meal/meal.service';
|
||||
import { GoalsService } from '../goals/goals.service';
|
||||
|
||||
describe('StatsService', () => {
|
||||
let service: StatsService;
|
||||
let mockMealService: jest.Mocked<MealService>;
|
||||
let mockGoalsService: jest.Mocked<GoalsService>;
|
||||
|
||||
const mockGoals = {
|
||||
id: 'goal-1',
|
||||
userId: 'user-1',
|
||||
dailyCalories: 2000,
|
||||
dailyProtein: 50,
|
||||
dailyCarbs: 275,
|
||||
dailyFat: 78,
|
||||
dailyFiber: 28,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
const createMeal = (
|
||||
date: Date,
|
||||
calories: number,
|
||||
protein: number,
|
||||
carbs: number,
|
||||
fat: number
|
||||
) => ({
|
||||
id: `meal-${Date.now()}-${Math.random()}`,
|
||||
userId: 'user-1',
|
||||
date,
|
||||
mealType: 'lunch',
|
||||
inputType: 'text',
|
||||
description: 'Test meal',
|
||||
confidence: 0.9,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
nutrition: { calories, protein, carbohydrates: carbs, fat, fiber: 5, sugar: 10 },
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
mockMealService = {
|
||||
findByDate: jest.fn(),
|
||||
findByDateRange: jest.fn(),
|
||||
} as any;
|
||||
|
||||
mockGoalsService = {
|
||||
getGoals: jest.fn(),
|
||||
} as any;
|
||||
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
StatsService,
|
||||
{ provide: MealService, useValue: mockMealService },
|
||||
{ provide: GoalsService, useValue: mockGoalsService },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<StatsService>(StatsService);
|
||||
});
|
||||
|
||||
describe('getDailySummary', () => {
|
||||
it('should return daily summary with meals and progress', async () => {
|
||||
const meals = [
|
||||
createMeal(new Date(), 500, 20, 60, 15),
|
||||
createMeal(new Date(), 700, 30, 80, 25),
|
||||
];
|
||||
mockMealService.findByDate.mockResolvedValue(meals as any);
|
||||
mockGoalsService.getGoals.mockResolvedValue(mockGoals);
|
||||
|
||||
const result = await service.getDailySummary('user-1', new Date());
|
||||
|
||||
expect(result.meals).toHaveLength(2);
|
||||
expect(result.totalNutrition.calories).toBe(1200);
|
||||
expect(result.totalNutrition.protein).toBe(50);
|
||||
expect(result.progress.calories.percentage).toBe(60); // 1200/2000 = 60%
|
||||
expect(result.progress.protein!.percentage).toBe(100); // 50/50 = 100%
|
||||
});
|
||||
|
||||
it('should use default values when no goals set', async () => {
|
||||
const meals = [createMeal(new Date(), 1000, 25, 140, 40)];
|
||||
mockMealService.findByDate.mockResolvedValue(meals as any);
|
||||
mockGoalsService.getGoals.mockResolvedValue(null as any);
|
||||
|
||||
const result = await service.getDailySummary('user-1', new Date());
|
||||
|
||||
expect(result.goals).toBeUndefined();
|
||||
expect(result.progress.calories.target).toBe(2000); // default
|
||||
});
|
||||
|
||||
it('should handle days with no meals', async () => {
|
||||
mockMealService.findByDate.mockResolvedValue([]);
|
||||
mockGoalsService.getGoals.mockResolvedValue(mockGoals);
|
||||
|
||||
const result = await service.getDailySummary('user-1', new Date());
|
||||
|
||||
expect(result.meals).toHaveLength(0);
|
||||
expect(result.totalNutrition.calories).toBe(0);
|
||||
expect(result.progress.calories.percentage).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getWeeklyStats', () => {
|
||||
it('should return weekly stats with daily breakdown', async () => {
|
||||
const today = new Date();
|
||||
const meals = [
|
||||
createMeal(today, 1800, 45, 220, 70),
|
||||
createMeal(new Date(today.getTime() - 86400000), 2000, 50, 250, 75),
|
||||
];
|
||||
mockMealService.findByDateRange.mockResolvedValue(meals as any);
|
||||
mockGoalsService.getGoals.mockResolvedValue(mockGoals);
|
||||
|
||||
const result = await service.getWeeklyStats('user-1', today);
|
||||
|
||||
expect(result.days).toHaveLength(7);
|
||||
expect(result.averages).toBeDefined();
|
||||
expect(result.trends).toBeDefined();
|
||||
});
|
||||
|
||||
it('should calculate averages correctly', async () => {
|
||||
const today = new Date('2024-01-15');
|
||||
const day1 = new Date('2024-01-09'); // 6 days ago
|
||||
const day2 = new Date('2024-01-10'); // 5 days ago
|
||||
|
||||
const meals = [createMeal(day1, 2000, 50, 250, 80), createMeal(day2, 1800, 45, 220, 70)];
|
||||
mockMealService.findByDateRange.mockResolvedValue(meals as any);
|
||||
mockGoalsService.getGoals.mockResolvedValue(mockGoals);
|
||||
|
||||
const result = await service.getWeeklyStats('user-1', today);
|
||||
|
||||
// Average should be based on days with data (2 days)
|
||||
expect(result.averages.calories).toBe(1900); // (2000+1800)/2
|
||||
expect(result.averages.protein).toBe(48); // (50+45)/2 rounded
|
||||
});
|
||||
|
||||
it('should detect upward trend', async () => {
|
||||
const today = new Date('2024-01-15');
|
||||
const meals = [
|
||||
// First half (days 0-2) - low calories
|
||||
createMeal(new Date('2024-01-09'), 1000, 25, 120, 30),
|
||||
createMeal(new Date('2024-01-10'), 1100, 28, 130, 35),
|
||||
createMeal(new Date('2024-01-11'), 1050, 26, 125, 32),
|
||||
// Second half (days 4-6) - high calories (>10% increase)
|
||||
createMeal(new Date('2024-01-13'), 1500, 40, 180, 50),
|
||||
createMeal(new Date('2024-01-14'), 1600, 42, 190, 52),
|
||||
createMeal(new Date('2024-01-15'), 1550, 41, 185, 51),
|
||||
];
|
||||
mockMealService.findByDateRange.mockResolvedValue(meals as any);
|
||||
mockGoalsService.getGoals.mockResolvedValue(mockGoals);
|
||||
|
||||
const result = await service.getWeeklyStats('user-1', today);
|
||||
|
||||
expect(result.trends.caloriesTrend).toBe('up');
|
||||
});
|
||||
|
||||
it('should detect downward trend', async () => {
|
||||
const today = new Date('2024-01-15');
|
||||
const meals = [
|
||||
// First half - high calories
|
||||
createMeal(new Date('2024-01-09'), 2000, 50, 250, 80),
|
||||
createMeal(new Date('2024-01-10'), 2100, 52, 260, 82),
|
||||
createMeal(new Date('2024-01-11'), 1950, 49, 240, 78),
|
||||
// Second half - low calories (>10% decrease)
|
||||
createMeal(new Date('2024-01-13'), 1500, 38, 180, 48),
|
||||
createMeal(new Date('2024-01-14'), 1400, 35, 170, 45),
|
||||
createMeal(new Date('2024-01-15'), 1450, 36, 175, 46),
|
||||
];
|
||||
mockMealService.findByDateRange.mockResolvedValue(meals as any);
|
||||
mockGoalsService.getGoals.mockResolvedValue(mockGoals);
|
||||
|
||||
const result = await service.getWeeklyStats('user-1', today);
|
||||
|
||||
expect(result.trends.caloriesTrend).toBe('down');
|
||||
});
|
||||
|
||||
it('should detect stable trend', async () => {
|
||||
const today = new Date('2024-01-15');
|
||||
const meals = [
|
||||
createMeal(new Date('2024-01-09'), 2000, 50, 250, 80),
|
||||
createMeal(new Date('2024-01-10'), 2050, 51, 255, 81),
|
||||
createMeal(new Date('2024-01-11'), 1980, 49, 245, 79),
|
||||
createMeal(new Date('2024-01-13'), 2020, 50, 252, 80),
|
||||
createMeal(new Date('2024-01-14'), 2000, 50, 250, 80),
|
||||
createMeal(new Date('2024-01-15'), 2010, 50, 251, 80),
|
||||
];
|
||||
mockMealService.findByDateRange.mockResolvedValue(meals as any);
|
||||
mockGoalsService.getGoals.mockResolvedValue(mockGoals);
|
||||
|
||||
const result = await service.getWeeklyStats('user-1', today);
|
||||
|
||||
expect(result.trends.caloriesTrend).toBe('stable');
|
||||
});
|
||||
|
||||
it('should mark goalsMet correctly when within 10% of target', async () => {
|
||||
// goalsMet = dayCalories >= goals.dailyCalories * 0.9 && dayCalories <= goals.dailyCalories * 1.1
|
||||
// With dailyCalories = 2000, range is 1800-2200
|
||||
// 1900 is within this range, so goalsMet should be true
|
||||
const today = new Date();
|
||||
today.setHours(23, 59, 59, 0);
|
||||
|
||||
// Create a meal that is definitely within the week range
|
||||
const startDate = new Date(today);
|
||||
startDate.setDate(startDate.getDate() - 6);
|
||||
startDate.setHours(0, 0, 0, 0);
|
||||
|
||||
// Use the third day of the week for the meal
|
||||
const mealDate = new Date(startDate);
|
||||
mealDate.setDate(mealDate.getDate() + 2);
|
||||
mealDate.setHours(12, 0, 0, 0);
|
||||
|
||||
const meals = [createMeal(mealDate, 1900, 50, 250, 80)];
|
||||
mockMealService.findByDateRange.mockResolvedValue(meals as any);
|
||||
mockGoalsService.getGoals.mockResolvedValue(mockGoals);
|
||||
|
||||
const result = await service.getWeeklyStats('user-1', today);
|
||||
|
||||
// The day with 1900 calories should have goalsMet = true
|
||||
const dayWithMeals = result.days.find((d) => d.totalCalories > 0);
|
||||
expect(dayWithMeals).toBeDefined();
|
||||
expect(dayWithMeals!.totalCalories).toBe(1900);
|
||||
expect(dayWithMeals!.goalsMet).toBe(true);
|
||||
});
|
||||
|
||||
it('should handle empty week', async () => {
|
||||
mockMealService.findByDateRange.mockResolvedValue([]);
|
||||
mockGoalsService.getGoals.mockResolvedValue(mockGoals);
|
||||
|
||||
const result = await service.getWeeklyStats('user-1', new Date());
|
||||
|
||||
expect(result.days).toHaveLength(7);
|
||||
expect(result.averages.calories).toBe(0);
|
||||
result.days.forEach((day) => {
|
||||
expect(day.mealCount).toBe(0);
|
||||
expect(day.goalsMet).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
177
apps/nutriphi/apps/backend/src/utils/nutrition.utils.spec.ts
Normal file
177
apps/nutriphi/apps/backend/src/utils/nutrition.utils.spec.ts
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
import { calculateProgress, sumNutrition } from './nutrition.utils';
|
||||
import { DEFAULT_DAILY_VALUES } from '../types/nutrition.types';
|
||||
|
||||
describe('nutrition.utils', () => {
|
||||
describe('calculateProgress', () => {
|
||||
it('should calculate progress with default values when no goals provided', () => {
|
||||
const nutrition = {
|
||||
calories: 1000,
|
||||
protein: 25,
|
||||
carbohydrates: 138,
|
||||
fat: 39,
|
||||
};
|
||||
|
||||
const progress = calculateProgress(nutrition);
|
||||
|
||||
expect(progress.calories.current).toBe(1000);
|
||||
expect(progress.calories.target).toBe(DEFAULT_DAILY_VALUES.calories);
|
||||
expect(progress.calories.percentage).toBe(50);
|
||||
|
||||
expect(progress.protein!.current).toBe(25);
|
||||
expect(progress.protein!.target).toBe(DEFAULT_DAILY_VALUES.protein);
|
||||
expect(progress.protein!.percentage).toBe(50);
|
||||
});
|
||||
|
||||
it('should use custom goals when provided', () => {
|
||||
const nutrition = { calories: 1500, protein: 75, carbohydrates: 150, fat: 50 };
|
||||
const goals = {
|
||||
id: '1',
|
||||
userId: 'user1',
|
||||
dailyCalories: 3000,
|
||||
dailyProtein: 150,
|
||||
dailyCarbs: 300,
|
||||
dailyFat: 100,
|
||||
dailyFiber: 30,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
const progress = calculateProgress(nutrition, goals);
|
||||
|
||||
expect(progress.calories.target).toBe(3000);
|
||||
expect(progress.calories.percentage).toBe(50);
|
||||
expect(progress.protein!.target).toBe(150);
|
||||
expect(progress.protein!.percentage).toBe(50);
|
||||
});
|
||||
|
||||
it('should cap percentage at 100%', () => {
|
||||
const nutrition = { calories: 3000, protein: 200, carbohydrates: 500, fat: 200 };
|
||||
|
||||
const progress = calculateProgress(nutrition);
|
||||
|
||||
expect(progress.calories.percentage).toBe(100);
|
||||
expect(progress.protein!.percentage).toBe(100);
|
||||
});
|
||||
|
||||
it('should handle zero values', () => {
|
||||
const nutrition = { calories: 0, protein: 0, carbohydrates: 0, fat: 0 };
|
||||
|
||||
const progress = calculateProgress(nutrition);
|
||||
|
||||
expect(progress.calories.current).toBe(0);
|
||||
expect(progress.calories.percentage).toBe(0);
|
||||
});
|
||||
|
||||
it('should handle missing nutrition values', () => {
|
||||
const progress = calculateProgress({});
|
||||
|
||||
expect(progress.calories.current).toBe(0);
|
||||
expect(progress.protein!.current).toBe(0);
|
||||
expect(progress.carbs!.current).toBe(0);
|
||||
expect(progress.fat!.current).toBe(0);
|
||||
});
|
||||
|
||||
it('should round percentages', () => {
|
||||
const nutrition = { calories: 333, protein: 17, carbohydrates: 91, fat: 26 };
|
||||
|
||||
const progress = calculateProgress(nutrition);
|
||||
|
||||
expect(progress.calories.percentage).toBe(17); // 333/2000 = 16.65 -> 17
|
||||
expect(progress.protein!.percentage).toBe(34); // 17/50 = 34
|
||||
});
|
||||
});
|
||||
|
||||
describe('sumNutrition', () => {
|
||||
it('should sum nutrition from multiple meals', () => {
|
||||
const meals = [
|
||||
{
|
||||
nutrition: {
|
||||
calories: 500,
|
||||
protein: 20,
|
||||
carbohydrates: 60,
|
||||
fat: 15,
|
||||
fiber: 5,
|
||||
sugar: 10,
|
||||
},
|
||||
},
|
||||
{
|
||||
nutrition: { calories: 300, protein: 15, carbohydrates: 40, fat: 10, fiber: 3, sugar: 5 },
|
||||
},
|
||||
{
|
||||
nutrition: { calories: 200, protein: 10, carbohydrates: 25, fat: 8, fiber: 2, sugar: 3 },
|
||||
},
|
||||
];
|
||||
|
||||
const sum = sumNutrition(meals);
|
||||
|
||||
expect(sum.calories).toBe(1000);
|
||||
expect(sum.protein).toBe(45);
|
||||
expect(sum.carbohydrates).toBe(125);
|
||||
expect(sum.fat).toBe(33);
|
||||
expect(sum.fiber).toBe(10);
|
||||
expect(sum.sugar).toBe(18);
|
||||
});
|
||||
|
||||
it('should handle empty meals array', () => {
|
||||
const sum = sumNutrition([]);
|
||||
|
||||
expect(sum.calories).toBe(0);
|
||||
expect(sum.protein).toBe(0);
|
||||
expect(sum.carbohydrates).toBe(0);
|
||||
expect(sum.fat).toBe(0);
|
||||
});
|
||||
|
||||
it('should handle meals with null nutrition', () => {
|
||||
const meals = [
|
||||
{ nutrition: { calories: 500, protein: 20, carbohydrates: 60, fat: 15 } },
|
||||
{ nutrition: null },
|
||||
{ nutrition: { calories: 300, protein: 15, carbohydrates: 40, fat: 10 } },
|
||||
];
|
||||
|
||||
const sum = sumNutrition(meals);
|
||||
|
||||
expect(sum.calories).toBe(800);
|
||||
expect(sum.protein).toBe(35);
|
||||
});
|
||||
|
||||
it('should handle meals with undefined nutrition', () => {
|
||||
const meals = [
|
||||
{ nutrition: { calories: 500, protein: 20, carbohydrates: 60, fat: 15 } },
|
||||
{},
|
||||
{ nutrition: { calories: 300, protein: 15, carbohydrates: 40, fat: 10 } },
|
||||
];
|
||||
|
||||
const sum = sumNutrition(meals);
|
||||
|
||||
expect(sum.calories).toBe(800);
|
||||
});
|
||||
|
||||
it('should handle partial nutrition data', () => {
|
||||
const meals = [
|
||||
{ nutrition: { calories: 500 } },
|
||||
{ nutrition: { protein: 20 } },
|
||||
{ nutrition: { fat: 10, fiber: 5 } },
|
||||
];
|
||||
|
||||
const sum = sumNutrition(meals);
|
||||
|
||||
expect(sum.calories).toBe(500);
|
||||
expect(sum.protein).toBe(20);
|
||||
expect(sum.fat).toBe(10);
|
||||
expect(sum.fiber).toBe(5);
|
||||
expect(sum.carbohydrates).toBe(0);
|
||||
});
|
||||
|
||||
it('should ignore non-numeric values', () => {
|
||||
const meals = [
|
||||
{ nutrition: { calories: 500, protein: 'invalid' as any } },
|
||||
{ nutrition: { calories: 300, protein: 15 } },
|
||||
];
|
||||
|
||||
const sum = sumNutrition(meals);
|
||||
|
||||
expect(sum.calories).toBe(800);
|
||||
expect(sum.protein).toBe(15);
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue