Files
wekan/tests/pdfjsTextRuntime.test.cjs
T
Lauri Ojansivu 189adaee52 Update imapflow, mailparser, pdfjs-dist, isomorphic-git and pretty-ms, with runtime tests.
imapflow 2.1.2 -> 2.8.0 and mailparser 3.9.31 -> 3.12.2 (email to card):
bug fixes, and mailparser no longer turns a decoded display name into an
address. pdfjs-dist 6.2.108 -> 6.4.299 (attachment text search),
isomorphic-git 1.42.4 -> 1.44.3 (GitHub wiki import), pretty-ms 9 -> 10
(upload time estimate; its breaking changes are in colonNotation, which
WeKan does not use, and the output it shows is unchanged).

Each now has a test that RUNS it the way WeKan calls it, so the next
update that moves an API fails in seconds:
- imapflowMailparserRuntime: the poller's connect, mailbox lock, unseen
  search, fetchOne, simpleParser and \Seen flag against
  server/lib/tests/fakeImapServer.js; a wrong password is refused, and an
  encoded address in a display name stays a name.
- pdfjsTextRuntime: documentGif.js's legacy build, worker, fonts, CMaps
  and getTextContent on a PDF with known text; a non-PDF is refused.
- isomorphicGitRuntime: githubWiki.js's log, recursive readTree and
  readBlob on a two-commit repository; a missing object is an error.

npm audit reports nothing. The e2e run against a build with these:
rules-blocks (147 languages), attachments, both email specs, and the
security specs pass, except two rules-blocks cases that fail the same way
on a build of the commit before this one.

Thanks to developers of dependencies and xet7 !
2026-10-22 06:34:36 +03:00

84 lines
3.7 KiB
JavaScript

'use strict';
// The installed pdfjs-dist, called the way server/lib/documentGif.js's
// pdfSearchText() calls it - the legacy build, the real worker file, the
// package's own standard fonts and CMaps, getDocument/getPage/getTextContent
// and destroy() on the LOADING TASK - on a PDF with known text. A pdfjs-dist
// update that moves any of those fails here instead of in attachment search.
// pdfjs-dist went 6.2.108 -> 6.4.299.
// Run: node tests/pdfjsTextRuntime.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { pathToFileURL } = require('url');
const ROOT = path.join(__dirname, '..');
const read = rel => fs.readFileSync(path.join(ROOT, rel), 'utf8');
// A one-page PDF with the text "WeKan attachment search", with a correct xref.
function tinyPdf(text) {
const objects = [
'<< /Type /Catalog /Pages 2 0 R >>',
'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
'<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 144] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>',
null,
'<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>',
];
const stream = `BT /F1 18 Tf 20 70 Td (${text}) Tj ET`;
objects[3] = `<< /Length ${stream.length} >>\nstream\n${stream}\nendstream`;
let out = '%PDF-1.4\n';
const offsets = [];
objects.forEach((body, i) => { offsets.push(out.length); out += `${i + 1} 0 obj\n${body}\nendobj\n`; });
const xref = out.length;
out += `xref\n0 ${objects.length + 1}\n0000000000 65535 f \n`;
for (const o of offsets) out += `${String(o).padStart(10, '0')} 00000 n \n`;
out += `trailer\n<< /Size ${objects.length + 1} /Root 1 0 R >>\nstartxref\n${xref}\n%%EOF\n`;
return Buffer.from(out, 'latin1');
}
(async () => {
const src = read('server/lib/documentGif.js');
// The calls this test makes are the calls documentGif.js makes.
for (const shape of [
"import('pdfjs-dist/legacy/build/pdf.mjs')",
"runtimeResolve('pdfjs-dist/legacy/build/pdf.worker.mjs')",
"pdfjsAssetPath('standard_fonts')", "pdfjsAssetPath('cmaps')",
'await textPage.getTextContent()', 'await loadingTask.destroy()',
]) assert.ok(src.includes(shape), `documentGif.js no longer uses ${shape}; update this test with it`);
const pkg = path.dirname(require.resolve('pdfjs-dist/package.json'));
const pdfjs = await import(pathToFileURL(path.join(pkg, 'legacy/build/pdf.mjs')).href);
pdfjs.GlobalWorkerOptions.workerSrc = pathToFileURL(path.join(pkg, 'legacy/build/pdf.worker.mjs')).href;
for (const dir of ['standard_fonts', 'cmaps']) assert.ok(fs.statSync(path.join(pkg, dir)).isDirectory(), `${dir}/ ships with pdfjs-dist`);
const loadingTask = pdfjs.getDocument({
data: new Uint8Array(tinyPdf('WeKan attachment search')),
isEvalSupported: false,
standardFontDataUrl: path.join(pkg, 'standard_fonts') + path.sep,
cMapUrl: path.join(pkg, 'cmaps') + path.sep,
cMapPacked: true,
verbosity: 0,
});
const doc = await loadingTask.promise;
try {
assert.strictEqual(doc.numPages, 1);
const page = await doc.getPage(1);
const content = await page.getTextContent();
assert.match(content.items.map(item => item.str || '').join(' '), /WeKan attachment search/);
} finally {
await loadingTask.destroy();
}
console.log(' ok - pdfjs-dist extracts the text of a PDF the way documentGif.js asks');
// Negative: a file that is not a PDF is refused, not read as empty text.
const bad = pdfjs.getDocument({ data: new Uint8Array(Buffer.from('not a pdf at all')), verbosity: 0 });
await assert.rejects(bad.promise);
await bad.destroy();
console.log(' ok - a file that is not a PDF is refused (negative)');
console.log('\n2 passed');
})().catch((error) => {
console.error(error);
process.exit(1);
});