Repository navigation
Expand file tree
/
Copy pathqa-document-pdf.mts
More file actions
81 lines (80 loc) · 6.41 KB
/
Copy pathqa-document-pdf.mts
File metadata and controls
81 lines (80 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Uses an isolated fixture and mocked asset/API routes; never opens a user document.
import assert from "node:assert/strict";
import { mkdir, writeFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { resolve } from "node:path";
import sharp from "sharp";
import { createServer } from "vite";
import { chromium } from "playwright-core";
import { getDocument } from "pdfjs-dist/legacy/build/pdf.mjs";
import { renderDocumentPdf } from "../apps/server/src/services/document-pdf.js";
const root = fileURLToPath(new URL("../", import.meta.url));
const output = resolve(root, process.env.DOCA_QA_OUTPUT ?? "artifacts/verification/document-pdf");
await mkdir(output, { recursive: true });
const html = `${output}/view.html`;
await writeFile(html, `<!doctype html><meta charset="utf-8"><div id="root"></div><script type="module">import R from "/@react-refresh";R.injectIntoGlobalHook(window);window.$RefreshReg$=()=>{};window.$RefreshSig$=()=>t=>t;window.__vite_plugin_react_preamble_installed__=true;</script><script type="module" src="/@fs/${root}tests/fixtures/document-pdf-view.tsx"></script>`);
const server = await createServer({ configFile: `${root}apps/web/vite.config.ts`, server: { host: "127.0.0.1", port: 0, open: false } });
await server.listen();
let browser: Awaited<ReturnType<typeof chromium.launch>> | undefined;
try {
browser = await chromium.launch({ headless: true, ...(process.env.DOCA_QA_BROWSER ? { executablePath: process.env.DOCA_QA_BROWSER } : {}) });
const page = await browser.newPage({ viewport: { width: 1100, height: 1300 } });
const errors: string[] = [];
page.on("pageerror", error => errors.push(error.message));
const png = await sharp({ create: { width: 200, height: 120, channels: 4, background: "#d4e5ff" } }).png().toBuffer();
await page.route("**/api/v1/assets/**", route => route.fulfill({ contentType: "image/png", body: png }));
await page.route("**/api/v1/resources/pdf-qa/pdf", async route => {
const { html, width } = route.request().postDataJSON();
await writeFile(`${output}/projection-${width}-${Date.now()}.html`, html);
assert(!html.includes("katex-mathml"), "Auxiliary math text duplicated in PDF");
assert(!/<button\b|contenteditable=/.test(html), "Editing controls present in PDF");
await route.fulfill({ contentType: "application/pdf", body: await renderDocumentPdf(html, width) });
});
const address = server.httpServer!.address();
assert(address && typeof address !== "string");
await page.goto(`http://127.0.0.1:${address.port}/@fs/${html}`);
await page.waitForFunction(() => !!(window as any).pdfQA && !!document.querySelector(".sk-page"));
assert((await page.locator("#markdown .markdown-body").evaluate(element => getComputedStyle(element).fontFamily)).includes("sans-serif"), "Preview lost the scoped editor typography");
const rich = await page.evaluate(() => (window as any).pdfQA.exportRich());
assert(rich.unchanged, "PDF export changed the source Y.Doc");
const markdown = await page.evaluate(() => (window as any).pdfQA.exportMarkdown());
assert.equal(await page.locator("#markdown .katex .vlist > span").evaluate(element => !!(element as HTMLElement).style.top), true, "Markdown preview discarded math positioning");
const diagram = await page.evaluate(async () => {
const view = await (window as any).pdfQA.captureMarkdown();
const copy = new DOMParser().parseFromString(view.html, "text/html");
const image = Array.from(copy.images).find(image => image.alt.includes("Start") && image.alt.includes("Finish"));
return image?.src.split(",")[1];
});
assert(diagram, "HTML diagram was not preserved in the export projection");
const reference = await page.locator("#markdown .mermaid-diagram svg").screenshot({ path: `${output}/diagram-online.png` });
const expected = await sharp(reference).flatten({ background: "white" }).removeAlpha().raw().toBuffer({ resolveWithObject: true });
const projected = Buffer.from(diagram, "base64");
await writeFile(`${output}/diagram-projection.png`, projected);
const actual = await sharp(projected).flatten({ background: "white" }).resize(expected.info.width, expected.info.height).removeAlpha().raw().toBuffer();
let difference = 0;
for (let i = 0; i < actual.length; i++) difference += Math.abs(actual[i]! - expected.data[i]!);
const diagramPixelError = difference / actual.length;
assert(diagramPixelError < 10, `Diagram pixels differ from the online view: ${diagramPixelError}`);
await page.locator("#rich .sk-page").screenshot({ path: `${output}/rich-online.png` });
await page.locator("#markdown .markdown-body").screenshot({ path: `${output}/markdown-online.png` });
for (const [name, data, required] of [["rich", rich.bytes, ["Editable text", "Readable attachment.pdf", "Host context retained", "Row 34 / 2", "Last row remains visible"]], ["markdown", markdown, ["Markdown 中文验收", "Last Markdown paragraph"]]] as const) {
const bytes = new Uint8Array(data);
await writeFile(`${output}/${name}.pdf`, bytes);
const pdf = await getDocument({ data: bytes.slice() }).promise;
try {
let text = "";
for (let n = 1; n <= pdf.numPages; n++) text += (await (await pdf.getPage(n)).getTextContent()).items.map(item => "str" in item ? item.str : "").join(" ");
for (const expected of required) assert(text.includes(expected), `Missing ${expected} in ${name} PDF`);
if (name === "rich") assert(pdf.numPages > 1, "Pagination fixture did not span pages");
console.log(`${name}: ${pdf.numPages} pages; text, images and controls verified`);
} finally { await pdf.destroy(); }
const imported = await page.evaluate(bytes => (window as any).pdfQA.importPdf(bytes), Array.from(bytes));
// HTML diagram labels are preserved as a PNG; body text stays selectable.
assert(imported.resources.length >= (name === "markdown" ? 2 : 1), `${name} image/diagram missing after import`);
await writeFile(`${output}/${name}-import.json`, JSON.stringify(imported.initialValue, null, 2));
}
assert.equal(await page.locator('[aria-hidden="true"].document-editor-shell, [aria-hidden="true"].doca-markdown').count(), 0, "Projection leaked after export");
assert.deepEqual(errors, []);
await writeFile(`${output}/result.json`, JSON.stringify({ passed: true, diagramPixelError, sourceUnchanged: rich.unchanged, userDocumentsUsed: false }, null, 2) + "\n");
console.log(`PDF browser QA passed; visual artifacts: ${output}`);
} finally { await browser?.close(); await server.close(); }