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

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

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

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

    it('listTags GETs /v1/tags with no query when no opts', async () => {
        fetchSpy.mockResolvedValueOnce(ok([]));
        await listTags();
        const url = fetchSpy.mock.calls[0][0] as string;
        expect(url).toMatch(/\/v1\/tags$/);
    });

    it('listTags encodes search + limit', async () => {
        fetchSpy.mockResolvedValueOnce(
            ok([{ id: 't1', name: 'Mystery', created_at: '2026-01-01T00:00:00Z' }])
        );
        const tags = await listTags({ search: 'myst', limit: 5 });
        expect(tags[0].name).toBe('Mystery');
        const url = fetchSpy.mock.calls[0][0] as string;
        expect(url).toContain('search=myst');
        expect(url).toContain('limit=5');
    });
});
