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

// pdfjs-dist is a rendering dependency — we mock it so unit tests don't need
// a real browser PDF engine. The interesting behaviour under test here is the
// component's own UI logic (controls, page counter), not pdfjs internals.
vi.mock('pdfjs-dist', () => {
	function TextLayerMock() {}
	TextLayerMock.prototype.render = () => Promise.resolve();
	TextLayerMock.prototype.cancel = () => {};

	return {
		GlobalWorkerOptions: { workerSrc: '' },
		getDocument: vi.fn().mockReturnValue({
			promise: Promise.resolve({
				numPages: 2,
				getPage: vi.fn().mockResolvedValue({
					getViewport: vi.fn().mockReturnValue({ width: 595, height: 842 }),
					render: vi.fn().mockReturnValue({ promise: Promise.resolve() }),
					streamTextContent: vi.fn().mockReturnValue(new ReadableStream())
				})
			})
		}),
		TextLayer: TextLayerMock
	};
});

vi.mock('pdfjs-dist/build/pdf.worker.min.mjs?url', () => ({ default: '' }));

import PdfViewer from './PdfViewer.svelte';

afterEach(cleanup);

describe('PdfViewer', () => {
	it('shows previous and next page navigation buttons', async () => {
		render(PdfViewer, { url: '/api/documents/test-id/file' });
		await expect.element(page.getByRole('button', { name: /zurück/i })).toBeInTheDocument();
		await expect.element(page.getByRole('button', { name: /weiter/i })).toBeInTheDocument();
	});

	it('shows zoom controls', async () => {
		render(PdfViewer, { url: '/api/documents/test-id/file' });
		await expect.element(page.getByRole('button', { name: /vergrößern/i })).toBeInTheDocument();
		await expect.element(page.getByRole('button', { name: /verkleinern/i })).toBeInTheDocument();
	});

	it('displays the page counter once the PDF has loaded', async () => {
		render(PdfViewer, { url: '/api/documents/test-id/file' });
		// Mock resolves synchronously, so "1 / 2" should appear quickly
		await expect.element(page.getByText(/1\s*\/\s*2/)).toBeInTheDocument();
	});
});
