import { describe, it, expect, beforeAll } from 'vitest'; import { buildApp, createUser, createExercise, loginViaApi, registerViaApi, seedPlan, PASSWORD } from '../helpers.js'; describe('calendar routes', () => { let token: string; beforeAll(async () => { token = (await registerViaApi()).token; }); it('rejects an invalid month format with 400', async () => { const app = buildApp(); for (const month of ['2026-13', '202608', 'abc', '2026-8']) { const res = await app .get('/api/v1/calendar') .set('Authorization', `Bearer ${token}`) .query({ month }); expect(res.status).toBe(400); } }); it('returns only the current user active-plan days of a month', async () => { const app = buildApp(); const { user } = await createUser(); const exercise = await createExercise(); await seedPlan({ userId: user.id, status: 'ACTIVE', weeks: [ { index: 0, days: [ { dayOfWeek: 1, date: new Date(Date.UTC(2026, 7, 24)), exercises: [{ exerciseId: exercise.id, order: 0, sets: [{ index: 0, reps: 10 }] }], }, { dayOfWeek: 3, date: new Date(Date.UTC(2026, 8, 7)), exercises: [] }, ], }, ], }); const myToken = (await loginViaApi(user.email, PASSWORD)).token; const res = await app .get('/api/v1/calendar?month=2026-08') .set('Authorization', `Bearer ${myToken}`); expect(res.status).toBe(200); expect(res.body.month).toBe('2026-08'); expect(res.body.days).toHaveLength(1); const day = res.body.days[0]; expect(day.date).toBe('2026-08-24T00:00:00.000Z'); expect(day.status).toBe('PLANNED'); expect(day.exercises).toHaveLength(1); expect(day.exercises[0].name).toBeTruthy(); }); it('returns an empty list for a month without days', async () => { const app = buildApp(); const res = await app .get('/api/v1/calendar?month=2025-01') .set('Authorization', `Bearer ${token}`); expect(res.status).toBe(200); expect(res.body.month).toBe('2025-01'); expect(res.body.days).toEqual([]); }); it('defaults to the current month', async () => { const app = buildApp(); const res = await app.get('/api/v1/calendar').set('Authorization', `Bearer ${token}`); expect(res.status).toBe(200); const now = new Date(); const expected = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`; expect(res.body.month).toBe(expected); }); it('rejects /calendar without a token with 401', async () => { const app = buildApp(); const res = await app.get('/api/v1/calendar'); expect(res.status).toBe(401); }); });