File size: 3,026 Bytes
dac2ca2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48eb29c
dac2ca2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48eb29c
dac2ca2
 
 
48eb29c
dac2ca2
 
48eb29c
dac2ca2
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
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import vm from 'node:vm';
import { createReaderPdfTextRuntime } from '../../../static/reader/pdf/reader-pdf-text-runtime.mjs';

const sandbox = { self: {} };
vm.runInNewContext(readFileSync(new URL('../../../static/reader/core/reader-security.js', import.meta.url), 'utf8'), sandbox);
globalThis.VoiceOfMLReaderSecurity = sandbox.self.VoiceOfMLReaderSecurity;
globalThis.location = new URL('https://example.org/');
const hash = 'a'.repeat(64), root = `objects/aa/${hash}`;
const entry = { p: 1, o: `${root}/ocr/page-000001.json.gz`, os: hash, ob: 1 };
const manifest = { version: 1, kind: 'pdf-ocr', complete: true, source_sha256: hash,
  profile: 'layout-v1', page_count: 1, pages: [entry],
  book_text: { path: `${root}/ocr/book-text.json.gz`, sha256: hash, bytes: 1 } };
let input, fetches = 0;
const ctx = { ocrManifestUrl: '/manifest', documentState: {pageCount: 1}, pdfOcrManifest: null,
  pdfOcrManifestPromise: null, pdfOcrPagePromises: new Map(), pdfOcrSource: () => ({assetUrl: () => '/page'}),
  readerAbortController: new AbortController(),
  assertReaderActive() {}, cachedPdfOcrPage: () => null, cachePdfOcrPage() {},
  fetchReaderUrl: async () => { fetches++; return new Response('x'); } };
const runtime = createReaderPdfTextRuntime(ctx);
Object.assign(ctx, runtime);
ctx.cachedPdfOcrPage = () => null;
ctx.pdfOcrSource = () => ({assetUrl: () => '/page'});
ctx.readPdfOcrJson = async (...args) => args[0] === '/manifest' ? input : runtime.readPdfOcrJson(...args);
for (input of [null, {}, { ...manifest, pages: [null] }]) {
  ctx.pdfOcrManifest = null;
  await assert.rejects(runtime.loadPdfOcrManifest(), /PDF_OCR_MANIFEST_INVALID/);
}
input = { ...manifest, page_count: 2 };
await assert.rejects(runtime.loadPdfOcrManifest(), /PDF_OCR_MANIFEST_INVALID/);
input = { ...manifest, page_count: 1 };
ctx.documentState.pageCount = 2;
await assert.rejects(runtime.loadPdfOcrManifest(), /PDF_OCR_PAGE_COUNT_MISMATCH/);
ctx.documentState.pageCount = 1;
for (const patch of [{os: undefined}, {ob: undefined}, {os: null}, {ob: null}, {os: ''}, {ob: 0}, {os: 'z'.repeat(64)}, {ob: -1}]) {
  input = { ...manifest, pages: [{ ...entry, ...patch }] };
  ctx.pdfOcrManifest = null; ctx.pdfOcrManifestPromise = null;
  // Old manifests remain usable for book search even without page integrity metadata.
  assert.equal((await runtime.loadPdfOcrManifest()).book_text.path, manifest.book_text.path);
  await assert.rejects(runtime.loadPdfOcrPage(1, "high", true), /PDF_OCR_MANIFEST_INVALID/);
  assert.equal(fetches, 0);
}
input = manifest; ctx.pdfOcrManifest = null; ctx.pdfOcrManifestPromise = null;
await assert.rejects(runtime.loadPdfOcrPage(1, "high", true), /PDF_OCR_HASH_MISMATCH/);
assert.equal(fetches, 1);
input = { ...manifest, pages: [{ ...entry, ob: 2 }] }; ctx.pdfOcrManifest = null; ctx.pdfOcrManifestPromise = null;
await assert.rejects(runtime.loadPdfOcrPage(1, "high", true), /PDF_OCR_SIZE_MISMATCH/);
assert.equal(fetches, 2);