import { describe, it, expect, vi, afterEach } from 'vitest';
import { render, screen, cleanup, waitFor } from '@testing-library/svelte';

const getAnalysisMetrics = vi.fn();
const getAnalysisMetricsSeries = vi.fn();
vi.mock('$lib/api/admin', () => ({
    getAnalysisMetrics: (...a: unknown[]) => getAnalysisMetrics(...a),
    getAnalysisMetricsSeries: (...a: unknown[]) => getAnalysisMetricsSeries(...a)
}));

import AnalysisMetricsPanel from './AnalysisMetricsPanel.svelte';

afterEach(() => {
    cleanup();
    getAnalysisMetrics.mockReset();
    getAnalysisMetricsSeries.mockReset();
});

describe('AnalysisMetricsPanel (OCR)', () => {
    it('renders the aggregate tiles but not the by-model table', async () => {
        getAnalysisMetrics.mockResolvedValue({
            n: 10,
            ok: 9,
            failed: 1,
            avg_ms: 1200,
            // Even when the API returns a by-model breakdown, OCR has a single
            // engine so the panel must not surface the table.
            by_model: [{ model: 'ocrs', avg_ms: 1200, n: 10 }]
        });
        getAnalysisMetricsSeries.mockResolvedValue({ buckets: [] });

        render(AnalysisMetricsPanel);

        await waitFor(() =>
            expect(screen.getByTestId('analysis-metrics-n').textContent).toContain('10')
        );
        expect(screen.queryByText(/By model/i)).toBeNull();
        expect(screen.queryByRole('table')).toBeNull();
    });
});
