// The PDF Viewer feature: the page and zoom limits, the document identification of the catalog, and the document // mount of a feature session. The model tests come from the PDF Viewer plugin (tests/model.test.ts). import { test } from "node:test"; import assert from "node:assert/strict"; import { featureEntry, featureOfOwner } from "../../../src/lib/features/catalog.ts"; import { identifyPdf } from "../../../src/lib/features/pdfDocument.ts"; import { PAGE_GAP, PAGE_MARGIN, fitZoom, mostVisiblePage, nextRotation, outputScale, pageBoxes, pageNumber, pagesInRange, zoomFromText, zoomLevel } from "../../../src/lib/features/pdf/model.ts"; import { FeatureSession } from "../../../src/lib/features/session.ts"; import type { FeatureContext, FeatureInstance } from "../../../src/lib/features/types.ts"; import type { PluginDocumentContext } from "../../../src/lib/plugins/sdk.ts"; test("PDF identification handles uppercase extensions and distinct paths", () => { assert.deepEqual(identifyPdf("notes\\Report.PDF"), { id: "notes/Report.PDF" }); assert.notDeepEqual(identifyPdf("a/report.pdf"), identifyPdf("b/report.pdf")); for (const path of ["report.pdf.md", "pdf", "report.png"]) assert.equal(identifyPdf(path), null); }); test("page and zoom inputs stay within supported bounds", () => { assert.equal(pageNumber(NaN, 20), 1); assert.equal(pageNumber(-3, 20), 1); assert.equal(pageNumber(99, 20), 20); assert.equal(pageNumber(2.8, 20), 2); assert.equal(zoomLevel(Infinity), 1); assert.equal(zoomLevel(0), .25); assert.equal(zoomLevel(10), 4); assert.deepEqual([0, 90, 180, 270].map(nextRotation), [90, 180, 270, 0]); }); test("a typed zoom is a percent within the limits", () => { assert.equal(zoomFromText("137"), 1.37); assert.equal(zoomFromText(" 150 % "), 1.5); assert.equal(zoomFromText("112,5"), 1.125); assert.equal(zoomFromText("9000"), 4); assert.equal(zoomFromText("1"), .25); for (const text of ["", " ", "text", "-50", "0", "1e999"]) assert.equal(zoomFromText(text), null); }); test("the pages are one column, and the scroll position gives the current page", () => { const sizes = [{ width: 600, height: 800 }, { width: 800, height: 600 }, { width: 600, height: 800 }]; const boxes = pageBoxes(sizes, .5, 0); assert.deepEqual(boxes, [ { top: PAGE_MARGIN, width: 300, height: 400 }, { top: PAGE_MARGIN + 400 + PAGE_GAP, width: 400, height: 300 }, { top: PAGE_MARGIN + 700 + 2 * PAGE_GAP, width: 300, height: 400 }, ]); assert.deepEqual(pageBoxes(sizes, 1, 90).map(box => [box.width, box.height]), [[800, 600], [600, 800], [800, 600]]); assert.equal(fitZoom(sizes, 0, 400), .5); assert.equal(fitZoom(sizes, 90, 400), .5); assert.equal(fitZoom([{ width: 600, height: 900 }], 90, 450), .5); assert.equal(fitZoom([], 0, 400), 1); assert.deepEqual(pagesInRange(boxes, 0, 100), [1]); assert.deepEqual(pagesInRange(boxes, 400, 800), [1, 2, 3]); assert.deepEqual(pagesInRange(boxes, 5000, 6000), []); assert.equal(mostVisiblePage(boxes, 0, 300), 1); assert.equal(mostVisiblePage(boxes, 300, 600), 2); assert.equal(mostVisiblePage(boxes, 900, 1200), 3); assert.equal(mostVisiblePage(boxes, 0, 2000), 1, "The first page wins when several pages show in full"); assert.equal(mostVisiblePage([], 0, 100), 1); }); test("large PDF pages respect canvas dimension and memory limits", () => { for (const [width, height] of [[612, 792], [20000, 500], [20000, 20000]]) { const scale = outputScale(width, height, 3); assert.ok(width * scale <= 8192); assert.ok(height * scale <= 8192); assert.ok(width * height * scale * scale <= 16_000_001); } }); test("the catalog gives PDF Viewer the old package ID and one binary document type", () => { const pdf = featureEntry("pdf"); assert.equal(pdf.owner, "runestone.pdf-viewer"); assert.equal(featureOfOwner("runestone.pdf-viewer")?.id, "pdf"); assert.equal(pdf.defaultOn, true); assert.equal(pdf.followsPackage, true); assert.deepEqual(pdf.views, []); assert.deepEqual(pdf.commands, []); assert.deepEqual(pdf.documentTypes.map(type => [type.id, type.icon, type.content, [...type.extensions], type.title()]), [["pdf", "file", "binary", ["pdf"], "PDF document"]]); assert.deepEqual(featureEntry("git").documentTypes, []); }); function fakeContext(): FeatureContext { return { openView: () => {}, openFile: async () => {}, onWorkspaceChange: () => () => {}, setViewTitle: () => {}, closeView: () => {}, replaceView: () => {} }; } test("a feature session identifies PDF files without loading the feature", () => { let loads = 0; const session = new FeatureSession(featureEntry("pdf"), fakeContext(), async () => { loads++; throw new Error("not loaded"); }); const type = session.documentTypes.get("pdf"); assert.ok(type); assert.equal(type.title, "PDF document"); assert.equal(type.content, "binary"); assert.deepEqual(type.extensions, ["pdf"]); assert.deepEqual(type.identify("docs/Guide.PDF", ""), { id: "docs/Guide.PDF" }); assert.equal(type.identify("docs/guide.md", ""), null); assert.equal(loads, 0, "Identification does not load the feature."); assert.equal(session.started, false); }); test("the first document mount starts one instance, each tab gets the document, and stop disposes it", async () => { const events: string[] = []; let starts = 0; const instance: FeatureInstance = { mount: () => { throw new Error("no views"); }, mountDocument: (type, element, document) => { events.push(`mount ${type} ${document.path}`); element.textContent = document.id; return () => { events.push(`unmount ${document.path}`); }; }, dispose: () => { events.push("dispose"); }, }; const session = new FeatureSession(featureEntry("pdf"), fakeContext(), async () => ({ start: () => { starts++; return instance; } })); const element = () => ({ textContent: "", replaceChildren(this: { textContent: string }) { this.textContent = ""; } }) as unknown as HTMLElement; const first = element(), second = element(); const a: PluginDocumentContext = { id: "a.pdf", path: "a.pdf" }; const b: PluginDocumentContext = { id: "b/B.PDF", path: "b/B.PDF" }; const closeA = await session.mount("pdf", first, a); const closeB = await session.mount("pdf", second, b); assert.equal(starts, 1, "Two tabs share one instance."); assert.equal(first.textContent, "a.pdf"); await closeA(); assert.equal(first.textContent, "", "The host empties the element after the dispose."); await assert.rejects(session.mount("missing", element(), a), /This view is unavailable/); await assert.rejects(session.mount("pdf", element()), /This view is unavailable/, "Without a document, the ID names a view."); await closeB(); await session.stop(); assert.deepEqual(events, ["mount pdf a.pdf", "mount pdf b/B.PDF", "unmount a.pdf", "unmount b/B.PDF", "dispose"]); await assert.rejects(session.mount("pdf", element(), a), /This feature is off/); }); test("a feature without a document mount refuses a document", async () => { const instance: FeatureInstance = { mount: () => () => {}, dispose() {} }; const session = new FeatureSession(featureEntry("pdf"), fakeContext(), async () => ({ start: () => instance })); await assert.rejects(session.mount("pdf", { replaceChildren() {} } as unknown as HTMLElement, { id: "a.pdf", path: "a.pdf" }), /This view is unavailable/); await session.stop(); });