// @vitest-environment happy-dom // The routine builder, driven the way a person drives it: muscles, cardio, effort, build, add. // // The Coach itself is mocked — the real one is a network job — so what is asserted is everything // around it: the questions that reach the model, that a routine only one muscle wide is refused, // and that what comes back lands in the plan as a NEW routine without touching the week. import React, { act } from 'react' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { createRoot } from 'react-dom/client' vi.mock('./lib/coach-api.js', () => ({ requestPlan: vi.fn(async () => ({ job: { id: 'j1' } })), awaitCoachJob: vi.fn(), jobErrorText: (cls, detail) => `ERROR(${cls})${detail ? ': ' + detail : ''}`, })) import { useStore } from './store/useStore.js' import { useUI } from './store/useUI.js' import { aiRoutineSheet } from './sheets.jsx' import { requestPlan, awaitCoachJob } from './lib/coach-api.js' import { EXDB } from './lib/exercises.js' const mounted = [] const BACK = EXDB.find(e => e.bp === 'back' && e.n.includes('barbell bent over row')) || EXDB.find(e => e.bp === 'back') const BICEPS = EXDB.find(e => e.tg === 'biceps') const RUN = EXDB.find(e => e.bp === 'cardio') function renderSheet() { aiRoutineSheet() const sheet = useUI.getState().sheets.at(-1) const host = document.createElement('div') document.body.appendChild(host) const root = createRoot(host) mounted.push(root) act(() => root.render(sheet.render(() => useUI.getState().closeSheet(sheet.id)))) return host } const button = (host, label) => [...host.querySelectorAll('button')].find(b => b.textContent.trim() === label) const click = (host, label) => act(() => button(host, label).click()) /** The plan the mocked Coach hands back: one day, one routine. */ const bundle = { coach_contract: 1, name: 'Plan', summary: 's', basedOn: 'a', customEx: [], week: { 0: 'r1' }, routines: [{ id: 'r1', name: 'Espalda y bíceps', emoji: '💪', ex: [ { id: RUN.id, sets: 3, min: 8 }, { id: BACK.id, sets: 3, reps: 10, mode: 'reps' }, { id: BICEPS.id, sets: 3, reps: 10, mode: 'reps' }, ], }], } describe('build a routine with AI', () => { beforeEach(() => { globalThis.IS_REACT_ACT_ENVIRONMENT = true useUI.setState({ sheets: [], toastMsg: '' }) useStore.setState(s => ({ S: { ...s.S, unit: 'lb', lang: 'es', routines: [], week: {}, coach: { profile: { equipment: ['barbell'] } } } })) document.body.innerHTML = '' vi.clearAllMocks() }) afterEach(() => { act(() => { mounted.splice(0).forEach(root => root.unmount()) }) }) it('asks which muscles first, and will not go on with none', () => { const host = renderSheet() expect(host.textContent).toContain('Which muscles?') expect(button(host, 'Continue').disabled).toBe(true) click(host, 'back') expect(button(host, 'Continue').disabled).toBe(false) }) it('puts the muscles, the cardio and the effort into the questions it asks the model', async () => { const host = renderSheet() click(host, 'back') click(host, 'biceps') click(host, 'Continue') expect(host.textContent).toContain('Start with cardio?') click(host, 'Yes, 8 minutes') click(host, 'Continue') expect(host.textContent).toContain('How hard should it be?') click(host, 'Medium') await act(async () => { button(host, 'Build the routine').click() }) expect(requestPlan).toHaveBeenCalledTimes(1) const intake = requestPlan.mock.calls[0][0] // The muscles travel as catalogue selectors — a target muscle for the arms, because biceps // and triceps share one body part — plus cardio, which has to be in the library and not only // in the sentence or the model can only improvise a warm-up. expect(intake.groups).toEqual([{ bp: 'back' }, { tg: 'biceps' }, { bp: 'cardio' }]) expect(intake.catalogueOnly).toBe(true) expect(intake.daysPerWeek).toBe(1) expect(intake.notes).toContain('back') expect(intake.notes).toContain('mode "cardio"') // The profile's equipment rides along, so it only proposes what this person owns. expect(intake.equipment).toEqual(['barbell']) }) it('adds what came back as a new routine, and leaves the week alone', async () => { awaitCoachJob.mockResolvedValue({ id: 'p1', kind: 'create', bundle }) const host = renderSheet() click(host, 'back') click(host, 'Continue') click(host, 'No') click(host, 'Continue') click(host, 'Easy') await act(async () => { button(host, 'Build the routine').click() }) // The preview: the routine's own name, and its exercises with their prescription. expect(host.textContent).toContain('Espalda y bíceps') click(host, 'Add to my plan') const S = useStore.getState().S expect(S.routines).toHaveLength(1) expect(S.routines[0].name).toBe('Espalda y bíceps') expect(S.routines[0].ex.map(e => e.id)).toEqual([RUN.id, BACK.id, BICEPS.id]) // Cardio is one bout whatever the model sent, and the mode it left out is read from the // exercise — the two normalisations the app owns rather than trusting the answer for. expect(S.routines[0].ex[0]).toMatchObject({ sets: 1, mode: 'cardio', min: 8 }) expect(S.routines[0].ex[1].mode).toBe('reps') // A new routine, unscheduled: the week is the person's, and the builder was never asked // about days. expect(S.week).toEqual({}) expect(useUI.getState().sheets).toHaveLength(0) }) it('shows the failure in the app’s own words and lets the person try again', async () => { awaitCoachJob.mockRejectedValue(Object.assign(new Error(''), { code: 'auth' })) const host = renderSheet() click(host, 'back') click(host, 'Continue') click(host, 'Continue') await act(async () => { button(host, 'Build the routine').click() }) expect(host.textContent).toContain('ERROR(auth)') click(host, 'Try again') expect(host.textContent).toContain('Which muscles?') expect(useStore.getState().S.routines).toHaveLength(0) }) it('refuses an answer with no routine in it rather than adding an empty one', async () => { awaitCoachJob.mockResolvedValue({ id: 'p1', kind: 'create', bundle: { routines: [], week: {} } }) const host = renderSheet() click(host, 'back') click(host, 'Continue') click(host, 'Continue') await act(async () => { button(host, 'Build the routine').click() }) expect(host.textContent).toContain('ERROR(unusable)') expect(useStore.getState().S.routines).toHaveLength(0) }) })