import { describe, it, expect } from 'vitest';
import { createPdfRenderer } from './usePdfRenderer.svelte';

// Note: init() and loadDocument() require pdfjsLib (browser module).
// These tests cover pure state logic only — bounds clamping and zoom limits.

describe('createPdfRenderer', () => {
	it('starts at page 1 with scale 1.5 and no error', () => {
		const r = createPdfRenderer();
		expect(r.currentPage).toBe(1);
		expect(r.scale).toBe(1.5);
		expect(r.totalPages).toBe(0);
		expect(r.loading).toBe(false);
		expect(r.error).toBeNull();
		expect(r.isLoaded).toBe(false);
		expect(r.pdfjsReady).toBe(false);
	});

	it('prevPage does not go below page 1', () => {
		const r = createPdfRenderer();
		r.prevPage();
		expect(r.currentPage).toBe(1);
	});

	it('nextPage does not exceed totalPages', () => {
		const r = createPdfRenderer();
		// totalPages = 0, so 1 < 0 is false → stays at 1
		r.nextPage();
		expect(r.currentPage).toBe(1);
	});

	it('goToPage does not navigate when n > totalPages', () => {
		const r = createPdfRenderer();
		r.goToPage(5);
		expect(r.currentPage).toBe(1);
	});

	it('goToPage does not navigate when n < 1', () => {
		const r = createPdfRenderer();
		r.goToPage(0);
		expect(r.currentPage).toBe(1);
	});

	it('zoomIn increases scale by 0.25', () => {
		const r = createPdfRenderer();
		r.zoomIn();
		expect(r.scale).toBeCloseTo(1.75);
	});

	it('zoomOut decreases scale by 0.25', () => {
		const r = createPdfRenderer();
		r.zoomOut();
		expect(r.scale).toBeCloseTo(1.25);
	});

	it('zoomOut does not go below 0.5', () => {
		const r = createPdfRenderer();
		for (let i = 0; i < 20; i++) r.zoomOut();
		expect(r.scale).toBeCloseTo(0.5);
	});

	it('loadDocument is a no-op when pdfjsLib not initialized', async () => {
		const r = createPdfRenderer();
		await r.loadDocument('/some/path');
		// No-op because pdfjsLib is null (init not called)
		expect(r.error).toBeNull();
		expect(r.loading).toBe(false);
	});
});
