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 = 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 = fc.array(exerciseArb).map((exercises) => ({ exercises, })); describe('isSessionEffective (property-based)', () => { it('removed exercises never affect the result (hiding does)', () => { fc.assert( fc.property(sessionArb, (session) => { const visible = session.exercises.filter((e) => !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 })), // removed exercise with arbitrary (even incomplete) data — liczone nie jest exerciseArb.map((e) => ({ ...e, removed: 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('an unrecorded extra (bonus) set never changes the result', () => { fc.assert( fc.property(sessionArb, (session) => { // dokleja niezapisaną serię nadprogramową do pierwszego widocznego ćwiczenia const withPendingExtra: EffectiveSession = { exercises: session.exercises.map((e, i) => i === 0 ? { ...e, sets: [...e.sets, { planSetId: null, reps: null }] } : e, ), }; return isSessionEffective(session) === isSessionEffective(withPendingExtra); }), { 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 }, ); }); });