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

afterEach(cleanup);

describe('admin/ocr/global page', () => {
	it('renders the heading and back link to /admin/ocr', async () => {
		render(OcrGlobalPage, {
			props: { data: { history: { runs: [], personNames: {} } } }
		});

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

	it('passes the runs array through to TrainingHistory', async () => {
		render(OcrGlobalPage, {
			props: {
				data: { history: { runs: [], personNames: { 'p-1': 'Anna Schmidt' } } }
			}
		});

		await expect.element(page.getByRole('heading', { name: /globaler verlauf/i })).toBeVisible();
	});

	it('handles a missing history.runs by defaulting to an empty list', async () => {
		render(OcrGlobalPage, {
			props: { data: { history: { runs: undefined, personNames: undefined } } }
		});

		await expect.element(page.getByRole('heading', { name: /globaler verlauf/i })).toBeVisible();
	});
});
