locate_attachment (shared 16 MiB budget, JS-path steps in the TypeScript order, OD-3 cap hook off until measured), pdf::import, facade import_pdf with the PdfImportKind kinds (ts-rs exported). cargo: O7 144 inputs (locate, read, archive rows) and O8 51 cases equal to the R3 path, 3 OD-4 differences declared (lone surrogate in an overwritten duplicate value). Node through the shipped module: the same O8 outcomes, every reference PDF. Module 421.7 KB gzip (W1 budget 600 KB).
183 lines
6.1 KiB
TypeScript
183 lines
6.1 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). */
|
|
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' },
|
|
}
|
|
|
|
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(3)
|
|
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('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')
|
|
})
|
|
})
|