FileBytes gathers a file chunk by chunk in linear memory (capacity reserved from the announced size, refused above the PDF cap); SheetHandle.import_pdf_bytes and restore_copy_bytes consume it, so the worker streams file.stream() and the file lives once, never as a JavaScript ArrayBuffer nor as a JSON-wrapped string. restore_copy: strict UTF-8, one BOM, the OD-3 budget, draft.restore's kinds. Node: same envelopes as import_pdf on the 51 O8 cases; rescue-copy parity with draft.restore.
244 lines
8.7 KiB
TypeScript
244 lines
8.7 KiB
TypeScript
/**
|
|
* R4.2d: the PDF import through the shipped module (`SheetHandle.import_pdf`), in Node,
|
|
* against the frozen R3 outcomes: O8 (`crates/core/tests/oracles/r4/tolerance.json`, the
|
|
* full R3 import path on the bundled draft sheet, 51 valid and hostile PDFs), the O2 PDFs
|
|
* of the R3 build, the O3 envelopes of Step 6 and the Step 5 fixtures. The cargo suite
|
|
* `crates/core/tests/pdf_import.rs` pins the same outcomes natively; this one proves the
|
|
* wasm build and the glue (bytes copied once, the envelope string) give the same answers,
|
|
* and that none of the hostile files traps the instance.
|
|
*/
|
|
import { createHash } from 'node:crypto'
|
|
import { readdirSync, readFileSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
import {
|
|
CoreRuntime,
|
|
type GlueModule,
|
|
type GlueSheetHandle,
|
|
} from '../src/index'
|
|
import { nodeCoreSource } from '../src/node'
|
|
|
|
const ROOT = fileURLToPath(new URL('../../../', import.meta.url))
|
|
const SEED = join(ROOT, 'supabase/seed/anatomical-sheets/equine/draft')
|
|
const FIXTURES = join(ROOT, 'apps/app/tests/fixtures')
|
|
const read = (path: string) => readFileSync(path, 'utf8')
|
|
const sha256 = (data: Uint8Array | string) =>
|
|
createHash('sha256').update(data).digest('hex')
|
|
|
|
/** The bundled draft exactly as `apps/app/vite/sheet-bundle.ts` embeds it. */
|
|
function draftBundle(): string {
|
|
const manifest = JSON.parse(read(join(SEED, 'assets.json'))) as Array<{
|
|
file: string
|
|
sha256: string
|
|
svg_asset_id: string
|
|
}>
|
|
const assets: Record<string, string> = {}
|
|
const assetHashes: Record<string, string> = {}
|
|
for (const asset of manifest) {
|
|
assets[asset.svg_asset_id] = read(join(SEED, 'assets', asset.file))
|
|
assetHashes[asset.svg_asset_id] = asset.sha256
|
|
}
|
|
return JSON.stringify({
|
|
status: 'draft',
|
|
ref: JSON.parse(read(join(SEED, 'sheet-ref.json'))),
|
|
document: JSON.parse(read(join(SEED, 'document.json'))),
|
|
migrationMap: JSON.parse(read(join(SEED, 'report-migration-map.json'))),
|
|
assets,
|
|
assetHashes,
|
|
})
|
|
}
|
|
|
|
interface Envelope {
|
|
ok: boolean
|
|
kind?: string
|
|
found?: number
|
|
value?: {
|
|
read: {
|
|
origin: string
|
|
migrated: boolean
|
|
unresolvedZones: number
|
|
unresolvedFindings: number
|
|
snapshotRejected?: string
|
|
archive?: unknown
|
|
}
|
|
rebase: {
|
|
report: { conclusion: string }
|
|
rebased: boolean
|
|
unresolvedZones: number
|
|
unresolvedFindings: number
|
|
}
|
|
}
|
|
}
|
|
|
|
/** The O8 row of one envelope, in the oracle's shape. */
|
|
function o8Row(envelope: Envelope): unknown {
|
|
if (!envelope.ok) {
|
|
return envelope.found === undefined
|
|
? { ok: false, kind: envelope.kind }
|
|
: { ok: false, kind: envelope.kind, foundVersion: envelope.found }
|
|
}
|
|
const { read, rebase } = envelope.value!
|
|
return {
|
|
ok: true,
|
|
reportSha256: sha256(JSON.stringify(rebase.report)),
|
|
archiveSha256: read.archive ? sha256(JSON.stringify(read.archive)) : null,
|
|
conclusion: rebase.report.conclusion,
|
|
read: {
|
|
origin: read.origin,
|
|
migrated: read.migrated,
|
|
unresolvedZones: read.unresolvedZones,
|
|
unresolvedFindings: read.unresolvedFindings,
|
|
snapshotRejected: read.snapshotRejected ?? null,
|
|
},
|
|
rebase: {
|
|
rebased: rebase.rebased,
|
|
unresolvedZones: rebase.unresolvedZones,
|
|
unresolvedFindings: rebase.unresolvedFindings,
|
|
},
|
|
}
|
|
}
|
|
|
|
/**
|
|
* O8 cases whose Rust outcome differs on purpose (plan §A.3): OD-4, a lone surrogate escape
|
|
* in a value a duplicate key overwrote; OD-3, a token-dense document over `MAX_JSON_VALUES`
|
|
* (skipped like one nested too deep, so the kind follows the stamp).
|
|
*/
|
|
const DECLARED: Record<string, unknown> = {
|
|
'surrogate-content-overwritten': { ok: false, kind: 'lone_surrogate' },
|
|
'surrogate-version-overwritten': { ok: false, kind: 'lone_surrogate' },
|
|
'two-files-decoy-first': { ok: false, kind: 'lone_surrogate' },
|
|
'values-1m': { ok: false, kind: 'attachment_stripped' },
|
|
}
|
|
|
|
function pdfsUnder(dir: string): string[] {
|
|
return readdirSync(dir, { recursive: true, encoding: 'utf8' })
|
|
.filter((name) => name.endsWith('.pdf'))
|
|
.map((name) => join(dir, name))
|
|
}
|
|
|
|
describe('R4 PDF import through the shipped module (Node)', () => {
|
|
let runtime: CoreRuntime
|
|
let sheet: GlueSheetHandle
|
|
let glue: GlueModule
|
|
beforeAll(async () => {
|
|
const source = nodeCoreSource('pkg')
|
|
runtime = new CoreRuntime({
|
|
compile: source.compile,
|
|
importGlue: async (n) => {
|
|
glue = await source.importGlue(n)
|
|
return glue
|
|
},
|
|
})
|
|
expect(await runtime.ready()).toBe(true)
|
|
sheet = new glue.SheetHandle(draftBundle())
|
|
})
|
|
|
|
it('gives the R3 outcome on every O8 case, outside the declared differences', () => {
|
|
const oracle = JSON.parse(
|
|
read(join(ROOT, 'crates/core/tests/oracles/r4/tolerance.json')),
|
|
) as {
|
|
cases: Array<{
|
|
id: string
|
|
pdf: string
|
|
pdfSha256: string
|
|
outcome: unknown
|
|
}>
|
|
}
|
|
expect(oracle.cases).toHaveLength(51)
|
|
let declared = 0
|
|
for (const c of oracle.cases) {
|
|
const pdf = new Uint8Array(readFileSync(join(ROOT, c.pdf)))
|
|
expect(sha256(pdf), c.id).toBe(c.pdfSha256)
|
|
const envelope = JSON.parse(sheet.import_pdf(pdf)) as Envelope
|
|
const expected = DECLARED[c.id] ?? c.outcome
|
|
if (c.id in DECLARED) declared += 1
|
|
expect(o8Row(envelope), c.id).toEqual(expected)
|
|
}
|
|
expect(declared).toBe(4)
|
|
expect(runtime.status).toBe('ready')
|
|
})
|
|
|
|
it('imports every reference PDF of the R3 build, Step 6 and Step 5', () => {
|
|
const files = [
|
|
join(FIXTURES, 'report-v2.full.pdf'),
|
|
join(FIXTURES, 'report-v2.minimal.pdf'),
|
|
join(FIXTURES, 'report-v3.bare.pdf'),
|
|
...pdfsUnder(join(FIXTURES, 'pdf/r3')),
|
|
...pdfsUnder(join(FIXTURES, 'pdf/step6')),
|
|
]
|
|
expect(files.length).toBeGreaterThanOrEqual(20)
|
|
for (const file of files) {
|
|
const envelope = JSON.parse(
|
|
sheet.import_pdf(new Uint8Array(readFileSync(file))),
|
|
) as Envelope
|
|
expect(envelope.ok, file).toBe(true)
|
|
expect(typeof envelope.value?.rebase.report.conclusion, file).toBe(
|
|
'string',
|
|
)
|
|
}
|
|
})
|
|
|
|
it('gives the same envelope when the file is streamed into the module (FileBytes)', () => {
|
|
const oracle = JSON.parse(
|
|
read(join(ROOT, 'crates/core/tests/oracles/r4/tolerance.json')),
|
|
) as { cases: Array<{ id: string; pdf: string }> }
|
|
for (const c of oracle.cases) {
|
|
const pdf = new Uint8Array(readFileSync(join(ROOT, c.pdf)))
|
|
const bytes = new glue.FileBytes(pdf.byteLength)
|
|
for (let at = 0; at < pdf.byteLength; at += 4096) {
|
|
expect(bytes.push(pdf.subarray(at, at + 4096)), c.id).toBe(true)
|
|
}
|
|
expect(bytes.len()).toBe(pdf.byteLength)
|
|
expect(sheet.import_pdf_bytes(bytes), c.id).toBe(sheet.import_pdf(pdf))
|
|
}
|
|
const over = new glue.FileBytes(3)
|
|
expect(over.push(new Uint8Array([1, 2, 3, 4]))).toBe(false)
|
|
expect(over.len()).toBe(0)
|
|
over.free()
|
|
const huge = new glue.FileBytes(60 * 1024 * 1024)
|
|
expect(huge.push(new Uint8Array(1))).toBe(false)
|
|
huge.free()
|
|
})
|
|
|
|
it('reads a rescue copy from streamed bytes as draft.restore reads its text', () => {
|
|
const copy = read(join(FIXTURES, 'report-v3.demo.json'))
|
|
const restoreText = (text: string) =>
|
|
sheet.call(
|
|
'draft.restore',
|
|
JSON.stringify({ draft: text, archive: null }),
|
|
)
|
|
const restoreBytes = (data: Uint8Array) => {
|
|
const bytes = new glue.FileBytes(data.byteLength)
|
|
bytes.push(data)
|
|
return sheet.restore_copy_bytes(bytes)
|
|
}
|
|
const encode = (text: string) => new TextEncoder().encode(text)
|
|
expect(restoreBytes(encode(copy))).toBe(restoreText(copy))
|
|
expect(JSON.parse(restoreBytes(encode(copy))).ok).toBe(true)
|
|
// One BOM dropped, as TextDecoder did.
|
|
expect(restoreBytes(encode(`\uFEFF${copy}`))).toBe(restoreText(copy))
|
|
const invalid = JSON.parse(restoreText(`${copy} trailing`))
|
|
expect(restoreBytes(encode(`${copy} trailing`))).toBe(
|
|
restoreText(`${copy} trailing`),
|
|
)
|
|
expect(invalid.kind).toBe('invalid')
|
|
// Not UTF-8: refused as invalid, the kind the application maps to error_invalid_copy.
|
|
const notUtf8 = JSON.parse(restoreBytes(new Uint8Array([0x7b, 0xff, 0x7d])))
|
|
expect(notUtf8).toMatchObject({ ok: false, kind: 'invalid' })
|
|
// A lone surrogate escape is refused, never rewritten.
|
|
const lone = JSON.parse(
|
|
restoreBytes(
|
|
encode(copy.replace('"conclusion": "', '"conclusion": "\\ud800')),
|
|
),
|
|
)
|
|
expect(lone).toMatchObject({ ok: false, kind: 'lone_surrogate' })
|
|
})
|
|
|
|
it('answers invalid_pdf on bytes that are not a PDF, without a trap', () => {
|
|
const envelope = JSON.parse(sheet.import_pdf(new Uint8Array([1, 2, 3])))
|
|
expect(envelope).toEqual({ ok: false, kind: 'invalid_pdf', detail: '' })
|
|
expect(runtime.status).toBe('ready')
|
|
})
|
|
})
|