diff --git a/app/src/lib/equipment.ts b/app/src/lib/equipment.ts index 937c8a3..13b0a95 100644 --- a/app/src/lib/equipment.ts +++ b/app/src/lib/equipment.ts @@ -132,14 +132,18 @@ export function formatGymEquipment( } /** - * Czy ćwiczenie można wykonać w danej siłowni: każde wymagane urządzenie - * musi być w wyposażeniu siłowni. BODYWEIGHT (masa ciała) = bez sprzętu, - * zawsze dostępne. Pusta lista urządzeń = ćwiczenie zawsze dostępne. + * Czy ćwiczenie można wykonać w danej siłowni: wystarcza, że siłownia ma + * choćby jedno z podłączonych urządzeń (wieloznaczność katalogu = warianty + * typu „wyciąg lub brama"). BODYWEIGHT (masa ciała) = bez sprzętu, zawsze + * dostępne. Pusta lista urządzeń = ćwiczenie zawsze dostępne. */ export function exerciseAvailableAtGym( exercise: { equipment?: Equipment[] }, gym: { equipment?: GymEquipment[] }, ): boolean { const available = new Set((gym.equipment ?? []).map((ge) => ge.equipmentId)); - return (exercise.equipment ?? []).every((eq) => eq.kind === 'BODYWEIGHT' || available.has(eq.id)); + const required = exercise.equipment ?? []; + return ( + required.length === 0 || required.some((eq) => eq.kind === 'BODYWEIGHT' || available.has(eq.id)) + ); } diff --git a/app/test/equipment.test.ts b/app/test/equipment.test.ts new file mode 100644 index 0000000..1bcd348 --- /dev/null +++ b/app/test/equipment.test.ts @@ -0,0 +1,46 @@ +import { describe, it, expect } from 'vitest'; +import { exerciseAvailableAtGym } from '../src/lib/equipment'; +import type { Equipment, EquipmentKind, GymEquipment } from '../src/api/types'; + +const eq = (id: string, kind: EquipmentKind = 'OTHER'): Equipment => ({ + id, + name: id, + kind, + spec: null, + description: null, +}); + +const ge = (equipmentId: string): GymEquipment => ({ + id: `ge-${equipmentId}`, + gymId: 'g1', + equipmentId, + quantity: 1, + spec: null, + equipment: eq(equipmentId), +}); + +describe('exerciseAvailableAtGym', () => { + it('is available when the exercise has no linked equipment', () => { + expect(exerciseAvailableAtGym({ equipment: [] }, { equipment: [] })).toBe(true); + expect(exerciseAvailableAtGym({}, { equipment: [ge('trx')] })).toBe(true); + }); + + it('is available when the gym has at least one of the linked devices (variants)', () => { + const facePull = { equipment: [eq('brama', 'CABLE'), eq('guma'), eq('trx')] }; + expect(exerciseAvailableAtGym(facePull, { equipment: [ge('guma')] })).toBe(true); + expect(exerciseAvailableAtGym(facePull, { equipment: [ge('trx')] })).toBe(true); + }); + + it('is unavailable when the gym has none of the linked devices', () => { + const facePull = { equipment: [eq('brama', 'CABLE'), eq('guma'), eq('trx')] }; + expect(exerciseAvailableAtGym(facePull, { equipment: [ge('lawka'), ge('sztanga')] })).toBe( + false, + ); + expect(exerciseAvailableAtGym(facePull, { equipment: [] })).toBe(false); + }); + + it('BODYWEIGHT device makes the exercise available regardless of gym equipment', () => { + const dip = { equipment: [eq('pole', 'BODYWEIGHT'), eq('nieistnieje')] }; + expect(exerciseAvailableAtGym(dip, { equipment: [] })).toBe(true); + }); +});