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

102 lines
3.2 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import fc from 'fast-check';
import { isSessionEffective, type EffectiveSession } from '../../src/services/killstreak.js';
type Exercise = EffectiveSession['exercises'][number];
const planSetIdArb = fc.stringOf(fc.char(), { minLength: 1, maxLength: 8 });
const exerciseArb: fc.Arbitrary<Exercise> = fc.record({
hidden: fc.boolean(),
removed: fc.boolean(),
planExercise: fc.oneof(
fc.constant(null),
fc.array(planSetIdArb).map((ids) => ({ sets: ids.map((id) => ({ id })) })),
),
sets: fc.array(
fc.record({
planSetId: fc.oneof(fc.constant(null), planSetIdArb),
reps: fc.oneof(fc.constant(null), fc.integer({ min: 1, max: 100 })),
}),
),
});
const sessionArb: fc.Arbitrary<EffectiveSession> = fc.array(exerciseArb).map((exercises) => ({
exercises,
}));
describe('isSessionEffective (property-based)', () => {
it('hidden/removed exercises never affect the result', () => {
fc.assert(
fc.property(sessionArb, (session) => {
const visible = session.exercises.filter((e) => !e.hidden && !e.removed);
return isSessionEffective(session) === isSessionEffective({ exercises: visible });
}),
{ numRuns: 200 },
);
});
it('a session where every visible exercise is fully done is effective', () => {
const fullyDoneExerciseArb = fc.oneof(
// planned exercise with all plan sets completed
fc.array(planSetIdArb, { minLength: 1 }).map((ids) => ({
hidden: false,
removed: false,
planExercise: { sets: ids.map((id) => ({ id })) },
sets: ids.map((id) => ({ planSetId: id, reps: 10 })),
})),
// added exercise with at least one set that has reps
fc.array(
fc.record({
planSetId: fc.constant(null as string | null),
reps: fc.integer({ min: 1, max: 100 }),
}),
{ minLength: 1 },
).map((sets) => ({ hidden: false, removed: false, planExercise: null, sets })),
// hidden/removed exercise with arbitrary (even incomplete) data
exerciseArb.map((e) => ({ ...e, hidden: true })),
);
fc.assert(
fc.property(fc.array(fullyDoneExerciseArb), (exercises) =>
isSessionEffective({ exercises }),
),
{ numRuns: 200 },
);
});
it('a visible planned exercise with any missing set makes the session ineffective', () => {
fc.assert(
fc.property(
fc.array(planSetIdArb, { minLength: 1 }),
fc.array(exerciseArb),
(missingIds, others) => {
const incomplete: Exercise = {
hidden: false,
removed: false,
planExercise: { sets: missingIds.map((id) => ({ id })) },
sets: [],
};
return !isSessionEffective({ exercises: [...others, incomplete] });
},
),
{ numRuns: 200 },
);
});
it('a visible added exercise without sets makes the session ineffective', () => {
fc.assert(
fc.property(fc.array(exerciseArb), (others) => {
const emptyAdded: Exercise = {
hidden: false,
removed: false,
planExercise: null,
sets: [],
};
return !isSessionEffective({ exercises: [...others, emptyAdded] });
}),
{ numRuns: 200 },
);
});
});