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

afterEach(cleanup);

describe('admin/ocr/[personId] page', () => {
	it('renders the person name from personNames lookup', async () => {
		render(AdminOcrPersonPage, {
			props: {
				data: {
					personId: 'p-1',
					history: {
						runs: [],
						personNames: { 'p-1': 'Anna Schmidt' }
					}
				}
			}
		});

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

	it('falls back to "Unknown" when the personNames lookup misses', async () => {
		render(AdminOcrPersonPage, {
			props: {
				data: {
					personId: 'p-1',
					history: {
						runs: [],
						personNames: {}
					}
				}
			}
		});

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

	it('renders the back link to /admin/ocr', async () => {
		render(AdminOcrPersonPage, {
			props: {
				data: {
					personId: 'p-1',
					history: { runs: [], personNames: { 'p-1': 'Anna Schmidt' } }
				}
			}
		});

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

	it('handles missing personNames object gracefully', async () => {
		render(AdminOcrPersonPage, {
			props: {
				data: {
					personId: 'p-1',
					history: { runs: undefined, personNames: undefined }
				}
			}
		});

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