import { describe, it, expect } from 'vitest' import { server, http, HttpResponse } from './test-setup' import { getMeal, markMealConsumed, getUpcomingMeals } from '@/api/sdk' import { setHouseholdSlugProvider } from '@/api/client' describe('meals api (typed client)', () => { it('gets a meal by id', async () => { server.use( http.get('*/api/v1/households/:householdSlug/meals/:id', ({ params }) => { return HttpResponse.json({ id: params.id, suggestedDate: '2025-01-01T00:00:00Z' }) }) ) setHouseholdSlugProvider(() => 'the-smiths') const meal = await getMeal(10) expect(meal.id).toBeDefined() }) it('marks a meal consumed', async () => { server.use( http.post('*/api/v1/households/:householdSlug/meals/:id/consumed', () => { return HttpResponse.json({ id: 10, consumedDate: '2025-01-02T00:00:00Z' }) }) ) setHouseholdSlugProvider(() => 'the-smiths') const meal = await markMealConsumed(10) expect(meal.consumedDate).toBeInstanceOf(Date) }) it('lists upcoming meals', async () => { server.use( http.get('*/api/v1/households/:householdSlug/meals/upcoming', ({ request }) => { const url = new URL(request.url) if (!url.searchParams.get('from') || !url.searchParams.get('to')) { return new HttpResponse(null, { status: 400 }) } return HttpResponse.json([ { id: 1, suggestedDate: '2025-01-01T00:00:00Z' }, { id: 2, suggestedDate: '2025-01-02T00:00:00Z' }, ]) }) ) setHouseholdSlugProvider(() => 'the-smiths') const list = await getUpcomingMeals(new Date('2025-01-01T00:00:00Z'), new Date('2025-01-03T00:00:00Z')) expect(Array.isArray(list)).toBe(true) expect(list[0].suggestedDate).toBeInstanceOf(Date) }) })