Files
FitOS/api/test/unit/services-trainer.test.ts
2026-09-04 19:06:19 +02:00

151 lines
4.1 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { computeFlags } from '../../src/services/trainer.js';
const NOW = new Date('2026-08-25T12:00:00Z');
describe('computeFlags', () => {
it('returns no flags without an active plan', () => {
expect(
computeFlags({
hasActivePlan: false,
killstreak: 0,
resolvedDays: 5,
lastSessionAt: null,
plannedSetCount: 10,
donePlannedSetCount: 0,
now: NOW,
}),
).toEqual([]);
});
it('streak_reset: killstreak 0 with resolved days', () => {
const flags = computeFlags({
hasActivePlan: true,
killstreak: 0,
resolvedDays: 3,
lastSessionAt: new Date(NOW.getTime() - 24 * 60 * 60 * 1000),
plannedSetCount: 10,
donePlannedSetCount: 10,
now: NOW,
});
expect(flags).toContain('streak_reset');
});
it('no streak_reset when killstreak is positive', () => {
const flags = computeFlags({
hasActivePlan: true,
killstreak: 4,
resolvedDays: 3,
lastSessionAt: new Date(NOW.getTime() - 24 * 60 * 60 * 1000),
plannedSetCount: 10,
donePlannedSetCount: 10,
now: NOW,
});
expect(flags).not.toContain('streak_reset');
});
it('no streak_reset when the plan has no resolved days yet', () => {
const flags = computeFlags({
hasActivePlan: true,
killstreak: 0,
resolvedDays: 0,
lastSessionAt: new Date(NOW.getTime() - 24 * 60 * 60 * 1000),
plannedSetCount: 10,
donePlannedSetCount: 10,
now: NOW,
});
expect(flags).not.toContain('streak_reset');
});
it('no_session_7d when the last session is older than 7 days', () => {
const flags = computeFlags({
hasActivePlan: true,
killstreak: 1,
resolvedDays: 1,
lastSessionAt: new Date(NOW.getTime() - 8 * 24 * 60 * 60 * 1000),
plannedSetCount: 10,
donePlannedSetCount: 10,
now: NOW,
});
expect(flags).toContain('no_session_7d');
});
it('no_session_7d when there is no session at all', () => {
const flags = computeFlags({
hasActivePlan: true,
killstreak: 0,
resolvedDays: 0,
lastSessionAt: null,
plannedSetCount: 10,
donePlannedSetCount: 0,
now: NOW,
});
expect(flags).toContain('no_session_7d');
});
it('no no_session_7d when the last session is within 7 days', () => {
const flags = computeFlags({
hasActivePlan: true,
killstreak: 1,
resolvedDays: 1,
lastSessionAt: new Date(NOW.getTime() - 6 * 24 * 60 * 60 * 1000),
plannedSetCount: 10,
donePlannedSetCount: 10,
now: NOW,
});
expect(flags).not.toContain('no_session_7d');
});
it('high_deficit when more than 30% of planned sets are missing', () => {
const flags = computeFlags({
hasActivePlan: true,
killstreak: 1,
resolvedDays: 1,
lastSessionAt: new Date(NOW.getTime() - 24 * 60 * 60 * 1000),
plannedSetCount: 10,
donePlannedSetCount: 6,
now: NOW,
});
expect(flags).toContain('high_deficit');
});
it('no high_deficit when all planned sets are done', () => {
const flags = computeFlags({
hasActivePlan: true,
killstreak: 1,
resolvedDays: 1,
lastSessionAt: new Date(NOW.getTime() - 24 * 60 * 60 * 1000),
plannedSetCount: 10,
donePlannedSetCount: 10,
now: NOW,
});
expect(flags).not.toContain('high_deficit');
});
it('boundary: exactly 30% deficit is not high_deficit', () => {
const flags = computeFlags({
hasActivePlan: true,
killstreak: 1,
resolvedDays: 1,
lastSessionAt: new Date(NOW.getTime() - 24 * 60 * 60 * 1000),
plannedSetCount: 10,
donePlannedSetCount: 7,
now: NOW,
});
expect(flags).not.toContain('high_deficit');
});
it('no high_deficit when there are no planned sets', () => {
const flags = computeFlags({
hasActivePlan: true,
killstreak: 1,
resolvedDays: 1,
lastSessionAt: new Date(NOW.getTime() - 24 * 60 * 60 * 1000),
plannedSetCount: 0,
donePlannedSetCount: 0,
now: NOW,
});
expect(flags).not.toContain('high_deficit');
});
});