RuneStone 1.0.0
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
// 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 { nextRotation, outputScale, pageNumber, 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("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();
|
||||
});
|
||||
Reference in New Issue
Block a user