From d11ac7804204bfb1e4da6c37efd08653ca8402d6 Mon Sep 17 00:00:00 2001 From: tisachi Date: Tue, 27 Oct 2026 20:30:08 +0100 Subject: [PATCH] =?UTF-8?q?App:=20filtr=20cwiczen=20w=20planie=20po=20sprz?= =?UTF-8?q?ecie=20silowni=20na=20OR=20=E2=80=94=20cwiczenie=20z=20wieloma?= =?UTF-8?q?=20urzadzeniami=20(warianty=20typu=20guma=20lub=20TRX)=20widocz?= =?UTF-8?q?ne,=20gdy=20silownia=20ma=20choc=20jedno=20z=20nich;=20pusta=20?= =?UTF-8?q?lista=20i=20BODYWEIGHT=20bez=20zmian,=20nowy=20test=20equipment?= =?UTF-8?q?.test.ts=20(app=20unit=20259=20przechodzi)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/lib/equipment.ts | 12 ++++++---- app/test/equipment.test.ts | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 app/test/equipment.test.ts 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); + }); +});