import { describe, it, expect, vi, beforeEach, afterEach, type MockInstance } from 'vitest';
import { listGenres } from './genres';

function ok(body: unknown): Response {
    return new Response(JSON.stringify(body), {
        status: 200,
        headers: { 'content-type': 'application/json' }
    });
}

describe('genres api client', () => {
    let fetchSpy: MockInstance<typeof globalThis.fetch>;

    beforeEach(() => {
        fetchSpy = vi.spyOn(globalThis, 'fetch');
    });
    afterEach(() => {
        vi.restoreAllMocks();
    });

    it('listGenres GETs /v1/genres and returns a flat array', async () => {
        fetchSpy.mockResolvedValueOnce(
            ok([
                { id: 'g1', name: 'Action' },
                { id: 'g2', name: 'Comedy' }
            ])
        );
        const genres = await listGenres();
        expect(genres.map((g) => g.name)).toEqual(['Action', 'Comedy']);
        const url = fetchSpy.mock.calls[0][0] as string;
        expect(url).toMatch(/\/v1\/genres$/);
    });
});
