import { describe, it, expect, vi, beforeEach } from 'vitest';
import { getCoverageRollup, type CoverageRollup } from './admin';

vi.mock('./client', () => ({
  api: { get: vi.fn(), post: vi.fn() }
}));

import { api } from './client';

describe('admin coverage API', () => {
  beforeEach(() => vi.clearAllMocks());

  it('getCoverageRollup GETs the correct path', async () => {
    const sample: CoverageRollup = {
      total: 18026,
      with_art: 12400,
      pending: 5400,
      settled: 226,
      pending_no_mbid: 320
    };
    (api.get as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce(sample);
    const got = await getCoverageRollup();
    expect(api.get).toHaveBeenCalledWith('/api/admin/library/coverage');
    expect(got).toEqual(sample);
  });

  it('returns zeros on an empty library', async () => {
    const empty: CoverageRollup = {
      total: 0,
      with_art: 0,
      pending: 0,
      settled: 0,
      pending_no_mbid: 0
    };
    (api.get as unknown as ReturnType<typeof vi.fn>).mockResolvedValueOnce(empty);
    const got = await getCoverageRollup();
    expect(got.total).toBe(0);
    expect(got.with_art + got.pending + got.settled).toBe(got.total);
  });
});
