test: setup Vitest workspace and Playwright with first nutrition tests

Vitest:
- Workspace at root (vitest.workspace.ts) running per-package configs
- packages/shared/vitest.config.ts with v8 coverage and 70% thresholds
- Root scripts: test / test:watch / test:coverage

Nutrition module in @ketopath/shared:
- calculateBmr (Mifflin-St Jeor) + calculateTdee with activity multipliers
- ACTIVITY_MULTIPLIERS constants matching CLAUDE.md domain knowledge
- 14 unit tests covering Michele/Laura PRD personas, all sex variants,
  every activity level, and input validation (100% coverage on src/nutrition)

Playwright in apps/web:
- chromium-only project, webServer auto-boot of pnpm dev
- e2e/home.spec.ts smoke test asserts Italian copy and disclaimer

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
lucianoandClaude Opus 4.7 committed 2026-04-29 12:32:55 +02:00
1 parent ff004cdf8d
commit a3109d2a6c
16 files changed
+194 -6

No files matched your search

+3 -1
View File
@@ -1 +1,3 @@
export {};
export * from './nutrition/bmr.js';
export * from './nutrition/tdee.js';
export * from './nutrition/types.js';
+35
View File
@@ -0,0 +1,35 @@
import { describe, expect, it } from 'vitest';
import { calculateBmr } from './bmr.js';
describe('calculateBmr (Mifflin-St Jeor)', () => {
it('matches the male formula for the "Michele" PRD persona', () => {
// 49 anni, uomo, 76 kg, 170 cm
// 10·76 + 6.25·170 − 5·49 + 5 = 760 + 1062.5 − 245 + 5 = 1582.5
const bmr = calculateBmr({ weightKg: 76, heightCm: 170, ageYears: 49, gender: 'MALE' });
expect(bmr).toBeCloseTo(1582.5, 2);
});
it('matches the female formula for the "Laura" PRD persona', () => {
// 46 anni, donna, 70 kg, 165 cm
// 10·70 + 6.25·165 − 5·46 − 161 = 700 + 1031.25 − 230 − 161 = 1340.25
const bmr = calculateBmr({ weightKg: 70, heightCm: 165, ageYears: 46, gender: 'FEMALE' });
expect(bmr).toBeCloseTo(1340.25, 2);
});
it('uses the average sex-constant for OTHER', () => {
const male = calculateBmr({ weightKg: 70, heightCm: 170, ageYears: 30, gender: 'MALE' });
const female = calculateBmr({ weightKg: 70, heightCm: 170, ageYears: 30, gender: 'FEMALE' });
const other = calculateBmr({ weightKg: 70, heightCm: 170, ageYears: 30, gender: 'OTHER' });
expect(other).toBeCloseTo((male + female) / 2, 5);
});
it.each([
{ weightKg: 0, heightCm: 170, ageYears: 30 },
{ weightKg: 70, heightCm: 0, ageYears: 30 },
{ weightKg: 70, heightCm: 170, ageYears: 0 },
{ weightKg: -1, heightCm: 170, ageYears: 30 },
])('rejects non-positive inputs %j', (input) => {
expect(() => calculateBmr({ ...input, gender: 'MALE' })).toThrow(RangeError);
});
});
+27
View File
@@ -0,0 +1,27 @@
import type { Gender } from './types.js';
export interface BmrInput {
weightKg: number;
heightCm: number;
ageYears: number;
gender: Gender;
}
// Mifflin-St Jeor equation. For Gender 'OTHER' we use the average of the
// male and female sex-specific constants (-78), as a neutral approximation.
export function calculateBmr({ weightKg, heightCm, ageYears, gender }: BmrInput): number {
if (weightKg <= 0 || heightCm <= 0 || ageYears <= 0) {
throw new RangeError('weightKg, heightCm and ageYears must be positive');
}
const base = 10 * weightKg + 6.25 * heightCm - 5 * ageYears;
switch (gender) {
case 'MALE':
return base + 5;
case 'FEMALE':
return base - 161;
case 'OTHER':
return base - 78;
}
}
@@ -0,0 +1,30 @@
import { describe, expect, it } from 'vitest';
import { calculateBmr } from './bmr.js';
import { calculateTdee } from './tdee.js';
describe('calculateTdee', () => {
it('multiplies BMR by sedentary factor 1.2 for "Michele"', () => {
const bmr = calculateBmr({ weightKg: 76, heightCm: 170, ageYears: 49, gender: 'MALE' });
expect(calculateTdee(bmr, 'SEDENTARY')).toBeCloseTo(1899, 1);
});
it('multiplies BMR by moderate factor 1.55 for "Laura"', () => {
const bmr = calculateBmr({ weightKg: 70, heightCm: 165, ageYears: 46, gender: 'FEMALE' });
expect(calculateTdee(bmr, 'MODERATE')).toBeCloseTo(2077.39, 2);
});
it.each([
['SEDENTARY', 1.2],
['LIGHT', 1.375],
['MODERATE', 1.55],
['INTENSE', 1.725],
] as const)('applies the %s multiplier (%f)', (level, factor) => {
expect(calculateTdee(2000, level)).toBeCloseTo(2000 * factor, 5);
});
it('rejects non-positive BMR', () => {
expect(() => calculateTdee(0, 'SEDENTARY')).toThrow(RangeError);
expect(() => calculateTdee(-1, 'SEDENTARY')).toThrow(RangeError);
});
});
+8
View File
@@ -0,0 +1,8 @@
import { ACTIVITY_MULTIPLIERS, type ActivityLevel } from './types.js';
export function calculateTdee(bmr: number, activityLevel: ActivityLevel): number {
if (bmr <= 0) {
throw new RangeError('bmr must be positive');
}
return bmr * ACTIVITY_MULTIPLIERS[activityLevel];
}
+10
View File
@@ -0,0 +1,10 @@
export type Gender = 'MALE' | 'FEMALE' | 'OTHER';
export type ActivityLevel = 'SEDENTARY' | 'LIGHT' | 'MODERATE' | 'INTENSE';
export const ACTIVITY_MULTIPLIERS: Record<ActivityLevel, number> = {
SEDENTARY: 1.2,
LIGHT: 1.375,
MODERATE: 1.55,
INTENSE: 1.725,
};