import { describe, it, expect, afterEach } from 'vitest';
import { cleanup, render } from 'vitest-browser-svelte';
import { page } from 'vitest/browser';
import AdminOcrPage from './+page.svelte';

afterEach(cleanup);

const baseTrainingInfo = (overrides: Record<string, unknown> = {}) => ({
	ocrServiceAvailable: true,
	availableBlocks: 100,
	totalOcrBlocks: 200,
	availableDocuments: 50,
	availableSegBlocks: 30,
	senderModels: [],
	personNames: {},
	runs: [],
	...overrides
});

describe('admin/ocr index page', () => {
	it('renders the OCR heading', async () => {
		render(AdminOcrPage, { props: { data: { trainingInfo: baseTrainingInfo() } } });

		await expect.element(page.getByRole('heading', { level: 1, name: /^ocr$/i })).toBeVisible();
	});

	it('renders the sender-models heading', async () => {
		render(AdminOcrPage, { props: { data: { trainingInfo: baseTrainingInfo() } } });

		await expect.element(page.getByRole('heading', { name: /sender-modelle/i })).toBeVisible();
	});

	it('renders the global-history link to /admin/ocr/global', async () => {
		render(AdminOcrPage, { props: { data: { trainingInfo: baseTrainingInfo() } } });

		await expect
			.element(page.getByRole('link', { name: /globaler verlauf/i }))
			.toHaveAttribute('href', '/admin/ocr/global');
	});

	it('handles missing trainingInfo fields with defaults', async () => {
		render(AdminOcrPage, {
			props: {
				data: {
					trainingInfo: { ocrServiceAvailable: false }
				}
			}
		});

		await expect.element(page.getByRole('heading', { level: 1, name: /^ocr$/i })).toBeVisible();
	});
});
