import { beforeEach, describe, expect, it, vi } from 'vitest';
import { getGraphicsElements } from './pickers';

function jsonResponse(body: unknown, status = 200): Response {
  return new Response(JSON.stringify(body), { headers: { 'Content-Type': 'application/json' }, status });
}

describe('getGraphicsElements', () => {
  beforeEach(() => {
    window.localStorage.clear();
    vi.restoreAllMocks();
  });

  it('GETs /api/v1/graphics-elements without refresh by default', async () => {
    const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse([]));

    await getGraphicsElements();

    expect(fetchSpy.mock.calls[0][0]).toBe('/api/v1/graphics-elements');
  });

  it('passes refresh=true to re-sync on-disk definitions first', async () => {
    const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse([]));

    await getGraphicsElements(true);

    expect(fetchSpy.mock.calls[0][0]).toBe('/api/v1/graphics-elements?refresh=true');
  });
});
