refactor(pdf): R4.8 the TypeScript PDF engine leaves: packages/pdf, pdf-lib, the TS CLI and import path, the oracle generators deleted; test read-back on an own reader of the writer's files (pdfFile.ts), view goldens from the session's SVGs, import helper on the module's reader, N-1 scanner exported by core-wasm/n-1; app no longer depends on @faunosteo/domain; r4.census suite
This commit is contained in:
1 parent
77e11a53e4
commit
0dc1ba27c7
96 files changed
+743
-15769
No files matched your search
Vendored
+6
-2
@@ -41,8 +41,12 @@ tests/unit/pdf.goldens.test.ts` from `apps/app`.
|
||||
|
||||
What the TypeScript PDF engine and reader produced right before the Rust port,
|
||||
frozen on branch `feature/rust-r4-pdf-core` (`docs/development/RUST.md` §10.1
|
||||
lists every oracle with its commit). Generators run behind `MAKE_R4_ORACLES=1`;
|
||||
without the flag the same tests assert equality with the committed files.
|
||||
lists every oracle with its commit). The generators (the `r4.*-oracles` vitest
|
||||
suites of `packages/pdf` and `apps/app`) ran behind `MAKE_R4_ORACLES=1` and were
|
||||
deleted with the TypeScript engine at R4.8 (2026-10-19): the files below are
|
||||
frozen for good and checked by the cargo suites (`crates/core/tests/pdf_*.rs`);
|
||||
only `r4/reports` (O13, `r4.synthetic-reports.test.ts`) and the R4 digests
|
||||
(`goldens/r4/pdf.*.sha256`, cargo) keep a generator.
|
||||
|
||||
| Path | Content |
|
||||
| --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { resolve } from 'node:path'
|
||||
import type { Report } from '@faunosteo/core-wasm/types'
|
||||
import { importAttachment } from '@/pdf/import'
|
||||
import { importAttachment } from './pdfImport'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { FIXTURES_DIR } from './goldens'
|
||||
import {
|
||||
|
||||
@@ -1,36 +1,26 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { dirname, resolve } from 'node:path'
|
||||
import { expect, vi } from 'vitest'
|
||||
import {
|
||||
PDFArray,
|
||||
PDFDict,
|
||||
PDFDocument,
|
||||
PDFHexString,
|
||||
PDFName,
|
||||
PDFNumber,
|
||||
PDFPage,
|
||||
PDFString,
|
||||
} from 'pdf-lib'
|
||||
import type {
|
||||
Branding,
|
||||
Observation,
|
||||
Report,
|
||||
SheetSnapshot,
|
||||
} from '@faunosteo/core-wasm/types'
|
||||
import { composeHighlightedViewSvg } from '@faunosteo/pdf'
|
||||
|
||||
/** The engine's own (Zod-inferred) types of the same JSON contracts. */
|
||||
type EngineView = Parameters<typeof composeHighlightedViewSvg>
|
||||
import { PdfFile, nameOf, numbersOf, subDict, textOf } from './pdfFile'
|
||||
import { nologosBranding } from './sheet'
|
||||
|
||||
/**
|
||||
* Step 6 Phase 0 freeze: what the PDF pipeline drew before the engine,
|
||||
* recorded as JSON goldens (docs/development/STEP6_PDF_ARCHIVE_ANALYSIS.md
|
||||
* §17) that the app export path must still reproduce. The recorder spies on
|
||||
* pdf-lib, the link reader parses the saved bytes, the view snapshot parses
|
||||
* the engine's highlighted SVG with the browser DOM. Numbers are rounded to
|
||||
* 2 decimals; nothing ordered by drawing order is sorted.
|
||||
* §17) that the app export path must still reproduce. Since R4.8 the text
|
||||
* and image facts are read back from the file's content streams
|
||||
* (`pdfRead.ts`), the links from its annotations (`pdfFile.ts`), and the
|
||||
* view snapshot parses the SVG the core's session asked the rasteriser for,
|
||||
* with the browser DOM. Numbers are rounded to 2 decimals; nothing ordered
|
||||
* by drawing order is sorted.
|
||||
*/
|
||||
|
||||
/** 1×1 PNG (nologos: the legacy stub) and 2×1 PNG (logos: deterministic partner logo width). */
|
||||
@@ -53,6 +43,13 @@ export const GOLDEN_DIR = resolve(FIXTURES_DIR, 'goldens/step6')
|
||||
|
||||
/** `MAKE_GOLDEN=1` writes the goldens instead of asserting them. */
|
||||
export const MAKE_GOLDEN = process.env.MAKE_GOLDEN === '1'
|
||||
/** The R4 digests of the writer's own bytes (`goldens/r4/pdf.*.sha256`), frozen by the cargo suite. */
|
||||
export const R4_GOLDEN_DIR = resolve(FIXTURES_DIR, 'goldens/r4')
|
||||
/** The export time of every R4 run (the engine suites' `FIXED_EXPORTED_AT` at the freeze). */
|
||||
export const FIXED_EXPORTED_AT = new Date('2026-10-11T12:00:00.000Z')
|
||||
|
||||
export const sha256 = (data: Uint8Array | string): string =>
|
||||
createHash('sha256').update(data).digest('hex')
|
||||
|
||||
export type LogoMode = 'nologos' | 'logos'
|
||||
|
||||
@@ -106,82 +103,19 @@ export function round2(value: unknown): number | null {
|
||||
return typeof value === 'number' ? Math.round(value * 100) / 100 : null
|
||||
}
|
||||
|
||||
/** Pages are appended in order and never moved: the index at draw time is final. */
|
||||
function pageIndex(page: PDFPage): number {
|
||||
return page.doc.getPages().indexOf(page)
|
||||
}
|
||||
|
||||
export interface DrawRecording {
|
||||
texts: TextEntry[]
|
||||
images: ImageEntry[]
|
||||
stop(): void
|
||||
}
|
||||
|
||||
/** Records every drawText / drawImage while letting pdf-lib draw for real. */
|
||||
export function recordDrawing(): DrawRecording {
|
||||
const texts: TextEntry[] = []
|
||||
const images: ImageEntry[] = []
|
||||
const drawText = PDFPage.prototype.drawText
|
||||
const drawImage = PDFPage.prototype.drawImage
|
||||
const textSpy = vi
|
||||
.spyOn(PDFPage.prototype, 'drawText')
|
||||
.mockImplementation(function (this: PDFPage, text, options) {
|
||||
texts.push({
|
||||
page: pageIndex(this),
|
||||
text,
|
||||
x: round2(options?.x),
|
||||
y: round2(options?.y),
|
||||
size: round2(options?.size),
|
||||
font: options?.font?.name ?? null,
|
||||
})
|
||||
return drawText.call(this, text, options)
|
||||
})
|
||||
const imageSpy = vi
|
||||
.spyOn(PDFPage.prototype, 'drawImage')
|
||||
.mockImplementation(function (this: PDFPage, image, options) {
|
||||
images.push({
|
||||
page: pageIndex(this),
|
||||
x: round2(options?.x),
|
||||
y: round2(options?.y),
|
||||
width: round2(options?.width),
|
||||
height: round2(options?.height),
|
||||
})
|
||||
return drawImage.call(this, image, options)
|
||||
})
|
||||
return {
|
||||
texts,
|
||||
images,
|
||||
stop: () => {
|
||||
textSpy.mockRestore()
|
||||
imageSpy.mockRestore()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function linkOf(dict: PDFDict, page: number): LinkEntry | null {
|
||||
if (dict.get(PDFName.of('Subtype')) !== PDFName.of('Link')) return null
|
||||
const rect = dict
|
||||
.lookup(PDFName.of('Rect'), PDFArray)
|
||||
.asArray()
|
||||
.map((n) => round2((n as PDFNumber).asNumber()) ?? 0)
|
||||
const uri = dict.lookup(PDFName.of('A'), PDFDict).lookup(PDFName.of('URI'))
|
||||
const text =
|
||||
uri instanceof PDFString || uri instanceof PDFHexString
|
||||
? uri.decodeText()
|
||||
: String(uri)
|
||||
return { page, rect, uri: text }
|
||||
}
|
||||
|
||||
/** Every `/Annot /Link` of the saved PDF, page by page, in annotation order. */
|
||||
export async function readLinks(bytes: Uint8Array): Promise<LinkEntry[]> {
|
||||
const doc = await PDFDocument.load(bytes)
|
||||
const file = PdfFile.parse(bytes)
|
||||
const out: LinkEntry[] = []
|
||||
doc.getPages().forEach((page, index) => {
|
||||
const annots = page.node.Annots()
|
||||
if (!annots) return
|
||||
for (let i = 0; i < annots.size(); i += 1) {
|
||||
const link = linkOf(annots.lookup(i, PDFDict), index)
|
||||
if (link) out.push(link)
|
||||
file.pages().forEach((page, index) => {
|
||||
for (const annot of file.annots(page)) {
|
||||
if (nameOf(annot.dict, 'Subtype') !== 'Link') continue
|
||||
const action = subDict(annot.dict, 'A') ?? ''
|
||||
out.push({
|
||||
page: index,
|
||||
rect: numbersOf(annot.dict, 'Rect').map((n) => round2(n) ?? 0),
|
||||
uri: textOf(action, 'URI') ?? '',
|
||||
})
|
||||
}
|
||||
})
|
||||
return out
|
||||
@@ -239,22 +173,18 @@ function countDataAttributes(root: Element): number {
|
||||
)
|
||||
}
|
||||
|
||||
/** The facts of one composed view, from the SVG the core's session asked the rasteriser for. */
|
||||
export function snapshotView(
|
||||
sheet: SheetSnapshot,
|
||||
observation: Observation,
|
||||
observationId: string,
|
||||
viewId: string,
|
||||
svg: string,
|
||||
): ViewSnapshot {
|
||||
const svg = composeHighlightedViewSvg(
|
||||
sheet as EngineView[0],
|
||||
viewId,
|
||||
observation as EngineView[2],
|
||||
)
|
||||
const root = new DOMParser().parseFromString(
|
||||
svg,
|
||||
'image/svg+xml',
|
||||
).documentElement
|
||||
return {
|
||||
observationId: observation.id,
|
||||
observationId,
|
||||
viewId,
|
||||
viewBox: root.getAttribute('viewBox'),
|
||||
halo: root.querySelector('filter#osteo-halo') !== null,
|
||||
@@ -279,14 +209,32 @@ export function viewIdsOf(
|
||||
.sort((a, b) => Number(b === main) - Number(a === main))
|
||||
}
|
||||
|
||||
/** The (observation, view) pairs an export rasterises, in the session's request order. */
|
||||
export function viewPairsOf(
|
||||
sheet: SheetSnapshot,
|
||||
report: Report,
|
||||
): { observationId: string; viewId: string }[] {
|
||||
return report.observations.flatMap((observation) =>
|
||||
viewIdsOf(sheet, observation).map((viewId) => ({
|
||||
observationId: observation.id,
|
||||
viewId,
|
||||
})),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The view snapshots of an export: `svgs` are the view SVGs the session asked the
|
||||
* rasteriser for (every logo raster excluded), in request order, one per pair.
|
||||
*/
|
||||
export function snapshotViews(
|
||||
sheet: SheetSnapshot,
|
||||
report: Report,
|
||||
svgs: readonly string[],
|
||||
): ViewSnapshot[] {
|
||||
return report.observations.flatMap((observation) =>
|
||||
viewIdsOf(sheet, observation).map((viewId) =>
|
||||
snapshotView(sheet, observation, viewId),
|
||||
),
|
||||
const pairs = viewPairsOf(sheet, report)
|
||||
expect(svgs).toHaveLength(pairs.length)
|
||||
return pairs.map((pair, i) =>
|
||||
snapshotView(pair.observationId, pair.viewId, svgs[i]!),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,285 @@
|
||||
import { inflateSync } from 'node:zlib'
|
||||
|
||||
/**
|
||||
* A reader of the files the core's writer produces (R4.8: pdf-lib left the application
|
||||
* and its tests; `crates/core/src/pdf/write/{objects,serialize}.rs` is the format). Test
|
||||
* tooling only, on trusted bytes the tests just wrote: every object is `N 0 obj`, one
|
||||
* dictionary on one line (`<< /Key value … >>`, nested dictionaries inline, strings in hex),
|
||||
* an optional `stream` of the direct `/Length` bytes, then `endobj`; the page tree is one
|
||||
* `/Pages` node whose `/Kids` lists the pages in order. Nothing here parses a foreign PDF:
|
||||
* the application reads attachments with the core's scanner.
|
||||
*/
|
||||
|
||||
export interface PdfObject {
|
||||
number: number
|
||||
/** The dictionary text, `<<` to `>>` inclusive. */
|
||||
dict: string
|
||||
/** The raw stream bytes (not inflated), when the object is a stream. */
|
||||
stream: Uint8Array | null
|
||||
}
|
||||
|
||||
const latin1 = new TextDecoder('latin1')
|
||||
|
||||
/** The end of the dictionary that opens at `start` (`<<`): strings and nesting skipped. */
|
||||
function dictEnd(text: string, start: number): number {
|
||||
let depth = 0
|
||||
let at = start
|
||||
while (at < text.length) {
|
||||
const two = text.slice(at, at + 2)
|
||||
if (two === '<<') {
|
||||
depth += 1
|
||||
at += 2
|
||||
} else if (two === '>>') {
|
||||
depth -= 1
|
||||
at += 2
|
||||
if (depth === 0) return at
|
||||
} else if (text[at] === '<') {
|
||||
at = text.indexOf('>', at) + 1
|
||||
} else if (text[at] === '(') {
|
||||
at = text.indexOf(')', at) + 1
|
||||
} else {
|
||||
at += 1
|
||||
}
|
||||
}
|
||||
throw new Error('unterminated dictionary')
|
||||
}
|
||||
|
||||
function lengthOf(dict: string): number {
|
||||
const match = /\/Length (\d+)/.exec(dict)
|
||||
if (!match) throw new Error('stream without /Length')
|
||||
return Number(match[1])
|
||||
}
|
||||
|
||||
/** The objects of the file, by number. */
|
||||
export function parsePdf(bytes: Uint8Array): Map<number, PdfObject> {
|
||||
const text = latin1.decode(bytes)
|
||||
const objects = new Map<number, PdfObject>()
|
||||
const header = /(\d+) 0 obj\n/g
|
||||
let match: RegExpExecArray | null
|
||||
while ((match = header.exec(text)) !== null) {
|
||||
const number = Number(match[1])
|
||||
const start = header.lastIndex
|
||||
if (text.slice(start, start + 2) !== '<<') continue
|
||||
const end = dictEnd(text, start)
|
||||
const dict = text.slice(start, end)
|
||||
let stream: Uint8Array | null = null
|
||||
const marker = '\nstream\n'
|
||||
if (text.slice(end, end + marker.length) === marker) {
|
||||
const from = end + marker.length
|
||||
stream = bytes.slice(from, from + lengthOf(dict))
|
||||
header.lastIndex = from + stream.length
|
||||
}
|
||||
objects.set(number, { number, dict, stream })
|
||||
}
|
||||
return objects
|
||||
}
|
||||
|
||||
/** The decoded stream of an object: inflated when `/Filter /FlateDecode`. */
|
||||
export function streamOf(object: PdfObject): Uint8Array {
|
||||
if (!object.stream) throw new Error(`object ${object.number} is not a stream`)
|
||||
return object.dict.includes('/Filter /FlateDecode')
|
||||
? new Uint8Array(inflateSync(object.stream))
|
||||
: object.stream
|
||||
}
|
||||
|
||||
/** The text of a nested dictionary value (`/Key << … >>`). */
|
||||
export function subDict(dict: string, key: string): string | null {
|
||||
const at = dict.indexOf(`/${key} <<`)
|
||||
if (at < 0) return null
|
||||
const start = at + key.length + 2
|
||||
return dict.slice(start, dictEnd(dict, start))
|
||||
}
|
||||
|
||||
/** `/Key N 0 R` → N. */
|
||||
export function refOf(dict: string, key: string): number | null {
|
||||
const match = new RegExp(`/${key} (\\d+) 0 R`).exec(dict)
|
||||
return match ? Number(match[1]) : null
|
||||
}
|
||||
|
||||
/** `/Key [N 0 R M 0 R …]` → [N, M, …]. */
|
||||
export function refsOf(dict: string, key: string): number[] {
|
||||
const match = new RegExp(`/${key} \\[([^\\]]*)\\]`).exec(dict)
|
||||
if (!match) return []
|
||||
return [...match[1]!.matchAll(/(\d+) 0 R/g)].map((m) => Number(m[1]))
|
||||
}
|
||||
|
||||
/** `/Key /Name` → Name. */
|
||||
export function nameOf(dict: string, key: string): string | null {
|
||||
const match = new RegExp(`/${key} /([^\\s/>]+)`).exec(dict)
|
||||
return match ? match[1]! : null
|
||||
}
|
||||
|
||||
export function numberOf(dict: string, key: string): number | null {
|
||||
const match = new RegExp(`/${key} (-?[\\d.]+)`).exec(dict)
|
||||
return match ? Number(match[1]) : null
|
||||
}
|
||||
|
||||
/** `/Key [n n n n]` → the numbers. */
|
||||
export function numbersOf(dict: string, key: string): number[] {
|
||||
const match = new RegExp(`/${key} \\[([^\\]]*)\\]`).exec(dict)
|
||||
return match ? match[1]!.trim().split(/\s+/).map(Number) : []
|
||||
}
|
||||
|
||||
/** A hex string `<…>`: UTF-16BE behind a BOM, else Latin-1 bytes. */
|
||||
export function decodeHexString(token: string): string {
|
||||
const hex = token.slice(1, -1)
|
||||
const bytes = new Uint8Array(hex.length / 2)
|
||||
for (let i = 0; i < bytes.length; i += 1) {
|
||||
bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16)
|
||||
}
|
||||
if (bytes[0] === 0xfe && bytes[1] === 0xff) {
|
||||
return new TextDecoder('utf-16be').decode(bytes.subarray(2))
|
||||
}
|
||||
return latin1.decode(bytes)
|
||||
}
|
||||
|
||||
/** `/Key <hex>` or `/Key (literal)` → the decoded text. */
|
||||
export function textOf(dict: string, key: string): string | null {
|
||||
const match = new RegExp(`/${key} (<[0-9A-Fa-f]*>|\\([^)]*\\))`).exec(dict)
|
||||
if (!match) return null
|
||||
const token = match[1]!
|
||||
return token.startsWith('<') ? decodeHexString(token) : token.slice(1, -1)
|
||||
}
|
||||
|
||||
/** `/F1 5 0 R /F2 6 0 R` → the name → object number pairs of a resource dictionary. */
|
||||
export function entriesOf(dict: string): Map<string, number> {
|
||||
return new Map(
|
||||
[...dict.matchAll(/\/([^\s/<>]+) (\d+) 0 R/g)].map((m) => [
|
||||
m[1]!,
|
||||
Number(m[2]),
|
||||
]),
|
||||
)
|
||||
}
|
||||
|
||||
/** The file as a whole: its objects, catalog, pages in order, Info. */
|
||||
export class PdfFile {
|
||||
private constructor(
|
||||
private readonly objects: Map<number, PdfObject>,
|
||||
private readonly trailer: string,
|
||||
) {}
|
||||
|
||||
static parse(bytes: Uint8Array): PdfFile {
|
||||
const text = latin1.decode(bytes)
|
||||
const at = text.lastIndexOf('trailer\n')
|
||||
if (at < 0) throw new Error('no trailer')
|
||||
return new PdfFile(parsePdf(bytes), text.slice(at))
|
||||
}
|
||||
|
||||
object(number: number): PdfObject {
|
||||
const object = this.objects.get(number)
|
||||
if (!object) throw new Error(`object ${number} missing`)
|
||||
return object
|
||||
}
|
||||
|
||||
/** Every object, in number order. */
|
||||
all(): PdfObject[] {
|
||||
return [...this.objects.values()].sort((a, b) => a.number - b.number)
|
||||
}
|
||||
|
||||
catalog(): PdfObject {
|
||||
return this.object(refOf(this.trailer, 'Root')!)
|
||||
}
|
||||
|
||||
info(): PdfObject {
|
||||
return this.object(refOf(this.trailer, 'Info')!)
|
||||
}
|
||||
|
||||
/** The pages in `/Kids` order. */
|
||||
pages(): PdfObject[] {
|
||||
const pages = this.object(refOf(this.catalog().dict, 'Pages')!)
|
||||
return refsOf(pages.dict, 'Kids').map((n) => this.object(n))
|
||||
}
|
||||
|
||||
/** The content stream text (Latin-1) of a page. */
|
||||
content(page: PdfObject): string {
|
||||
return latin1.decode(streamOf(this.object(refOf(page.dict, 'Contents')!)))
|
||||
}
|
||||
|
||||
/** `/F1` → `Helvetica`, through the page's font resources. */
|
||||
fontNames(page: PdfObject): Map<string, string> {
|
||||
const fonts = subDict(subDict(page.dict, 'Resources') ?? '', 'Font') ?? ''
|
||||
const names = new Map<string, string>()
|
||||
for (const [name, number] of entriesOf(fonts)) {
|
||||
names.set(name, nameOf(this.object(number).dict, 'BaseFont') ?? '')
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
/** The image XObjects of a page, by resource name. */
|
||||
xobjects(page: PdfObject): Map<string, PdfObject> {
|
||||
const dict = subDict(subDict(page.dict, 'Resources') ?? '', 'XObject') ?? ''
|
||||
return new Map(
|
||||
[...entriesOf(dict)].map(([name, number]) => [name, this.object(number)]),
|
||||
)
|
||||
}
|
||||
|
||||
/** The annotation objects of a page, in `/Annots` order. */
|
||||
annots(page: PdfObject): PdfObject[] {
|
||||
return refsOf(page.dict, 'Annots').map((n) => this.object(n))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A minimal PDF carrying one embedded file in clear (one empty page, the stream stored
|
||||
* raw, a Filespec named `/F (name)` with `/EF`, the names tree, a classic xref): what the
|
||||
* rejection cases of the import suites used to build with pdf-lib. Test input only.
|
||||
*/
|
||||
export function buildPdfWithAttachment(
|
||||
name: string,
|
||||
data: Uint8Array,
|
||||
): Uint8Array {
|
||||
const latin1Bytes = (text: string) =>
|
||||
Uint8Array.from(text, (c) => c.charCodeAt(0))
|
||||
const bodies: Uint8Array[] = [
|
||||
latin1Bytes(
|
||||
`<< /Type /Catalog /Pages 2 0 R /Names << /EmbeddedFiles << /Names [(${name}) 5 0 R] >> >> >>`,
|
||||
),
|
||||
latin1Bytes('<< /Type /Pages /Count 1 /Kids [3 0 R] >>'),
|
||||
latin1Bytes(
|
||||
'<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595.28 841.89] >>',
|
||||
),
|
||||
new Uint8Array([
|
||||
...latin1Bytes(
|
||||
`<< /Type /EmbeddedFile /Subtype /application#2Fjson /Length ${data.length} >>\nstream\n`,
|
||||
),
|
||||
...data,
|
||||
...latin1Bytes('\nendstream'),
|
||||
]),
|
||||
latin1Bytes(
|
||||
`<< /Type /Filespec /F (${name}) /UF (${name}) /EF << /F 4 0 R >> /AFRelationship /Data >>`,
|
||||
),
|
||||
]
|
||||
const parts: Uint8Array[] = [latin1Bytes('%PDF-1.7\n%\xe2\xe3\xcf\xd3\n')]
|
||||
const offsets: number[] = []
|
||||
let total = parts[0]!.length
|
||||
bodies.forEach((body, i) => {
|
||||
offsets.push(total)
|
||||
const object = new Uint8Array([
|
||||
...latin1Bytes(`${i + 1} 0 obj\n`),
|
||||
...body,
|
||||
...latin1Bytes('\nendobj\n'),
|
||||
])
|
||||
parts.push(object)
|
||||
total += object.length
|
||||
})
|
||||
const xref = [
|
||||
'xref',
|
||||
`0 ${bodies.length + 1}`,
|
||||
'0000000000 65535 f ',
|
||||
...offsets.map((o) => `${String(o).padStart(10, '0')} 00000 n `),
|
||||
'trailer',
|
||||
`<< /Size ${bodies.length + 1} /Root 1 0 R >>`,
|
||||
'startxref',
|
||||
String(total),
|
||||
'%%EOF',
|
||||
'',
|
||||
].join('\n')
|
||||
parts.push(latin1Bytes(xref))
|
||||
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0))
|
||||
let at = 0
|
||||
for (const part of parts) {
|
||||
out.set(part, at)
|
||||
at += part.length
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import type { ArchiveImport } from '@faunosteo/core-wasm/types'
|
||||
import { coreRuntime } from '@/core/runtime'
|
||||
import { ImportError, pdfImportOf } from '@/core/worker/outcome'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { bundledSheet } from './sheet'
|
||||
|
||||
export { ImportError }
|
||||
|
||||
/**
|
||||
* The application's PDF import, in-process for the tests (R4.8, the TypeScript scanner
|
||||
* gone): the module's reader (`SheetHandle.import_pdf`, R4.2) on a handle of the bundled
|
||||
* sheet, its envelope mapped as HomeRoute maps the worker's settlement (`pdfImportOf`). The
|
||||
* page never runs the reader on its main instance (OD-1: the import worker does, on a
|
||||
* handle of its own); these helpers give the tests the same reading, same kinds, without a
|
||||
* worker thread. The route suites cover the worker itself.
|
||||
*/
|
||||
export async function importReportFromPdf(
|
||||
bytes: Uint8Array,
|
||||
): Promise<ArchiveImport> {
|
||||
const bundle = JSON.stringify(await bundledSheet())
|
||||
const runtime = coreRuntime()
|
||||
if (!(await runtime.ready())) throw new ImportError('core_unavailable')
|
||||
const out = runtime.invoke<string>((glue) => {
|
||||
const handle = new glue.SheetHandle(bundle)
|
||||
try {
|
||||
return { ok: true, value: handle.import_pdf(bytes) }
|
||||
} finally {
|
||||
handle.free()
|
||||
}
|
||||
})
|
||||
if (!out.ok) throw new ImportError('core_unavailable', null, out.kind)
|
||||
return pdfImportOf({
|
||||
kind: 'result',
|
||||
out: out.value,
|
||||
diag: { ms: 0, linearBytes: null, chunks: 0 },
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* The core's layered reading of an attachment already parsed (`archive.import` on the
|
||||
* loaded sheet): the v3 report as is, a v2 migrated through the sheet's map, the envelope
|
||||
* kept or its snapshot dropped with a reason, then the rebase onto that sheet. The kinds
|
||||
* are the worker's.
|
||||
*/
|
||||
export function importAttachment(
|
||||
attachment: unknown,
|
||||
index: SheetIndex,
|
||||
): ArchiveImport {
|
||||
const outcome = index.ops.importArchive(attachment)
|
||||
if (outcome.ok) return outcome.value
|
||||
switch (outcome.kind) {
|
||||
case 'incompatible_version':
|
||||
throw new ImportError('incompatible_version', outcome.found ?? null)
|
||||
case 'no_attachment':
|
||||
case 'lone_surrogate':
|
||||
throw new ImportError(outcome.kind)
|
||||
default:
|
||||
throw new ImportError('core_unavailable', null, outcome.kind)
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,13 @@
|
||||
import {
|
||||
decodePDFRawStream,
|
||||
PDFArray,
|
||||
PDFDict,
|
||||
PDFDocument,
|
||||
PDFName,
|
||||
PDFRawStream,
|
||||
type PDFPage,
|
||||
} from 'pdf-lib'
|
||||
import { PdfFile, type PdfObject } from './pdfFile'
|
||||
import { round2, type ImageEntry, type TextEntry } from './goldens'
|
||||
|
||||
/**
|
||||
* The drawing facts of a saved PDF, read back from its content streams (R4.6: the core
|
||||
* writes the file, so nothing is recorded at draw time any more; pdf-lib still reads the
|
||||
* file in the tests until R4.8). The streams are the operator sequences pdf-lib emitted
|
||||
* and the core reproduces: one operator per line, `q BT rg Tf TL Tm (Tj T*)+ ET Q` per
|
||||
* text call, `q cm cm cm cm Do Q` per image. Positions are rounded like the recorder did.
|
||||
* writes the file, so nothing is recorded at draw time any more; R4.8: the file is read by
|
||||
* `pdfFile.ts`, pdf-lib having left the tests). The streams are the operator sequences
|
||||
* pdf-lib emitted and the core reproduces: one operator per line, `q BT rg Tf TL Tm (Tj
|
||||
* T*)+ ET Q` per text call, `q cm cm cm cm Do Q` per image. Positions are rounded like the
|
||||
* recorder did.
|
||||
*/
|
||||
|
||||
/** WinAnsi is Windows-1252 for every glyph the engine prints. */
|
||||
@@ -29,43 +22,13 @@ function hexBytes(token: string): Uint8Array {
|
||||
return out
|
||||
}
|
||||
|
||||
function contentOf(page: PDFPage): string {
|
||||
const contents = page.node.Contents()
|
||||
const streams: PDFRawStream[] = []
|
||||
if (contents instanceof PDFRawStream) streams.push(contents)
|
||||
else if (contents instanceof PDFArray) {
|
||||
for (let i = 0; i < contents.size(); i += 1) {
|
||||
streams.push(contents.lookup(i, PDFRawStream))
|
||||
}
|
||||
}
|
||||
return streams
|
||||
.map((stream) =>
|
||||
new TextDecoder('latin1').decode(decodePDFRawStream(stream).decode()),
|
||||
)
|
||||
.join('\n')
|
||||
}
|
||||
|
||||
/** `/F1` → `Helvetica`, through the page's font resources. */
|
||||
function fontNames(page: PDFPage): Map<string, string> {
|
||||
const names = new Map<string, string>()
|
||||
const resources = page.node.lookup(PDFName.of('Resources'), PDFDict)
|
||||
const fonts = resources.lookupMaybe(PDFName.of('Font'), PDFDict)
|
||||
if (!fonts) return names
|
||||
for (const [key, ref] of fonts.entries()) {
|
||||
const font = fonts.context.lookup(ref, PDFDict)
|
||||
const base = font.lookup(PDFName.of('BaseFont'))
|
||||
names.set(key.decodeText(), String(base).replace(/^\//, ''))
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
interface PageFacts {
|
||||
texts: TextEntry[]
|
||||
images: ImageEntry[]
|
||||
}
|
||||
|
||||
function readPage(page: PDFPage, index: number): PageFacts {
|
||||
const fonts = fontNames(page)
|
||||
function readPage(file: PdfFile, page: PdfObject, index: number): PageFacts {
|
||||
const fonts = file.fontNames(page)
|
||||
const texts: TextEntry[] = []
|
||||
const images: ImageEntry[] = []
|
||||
let font: string | null = null
|
||||
@@ -74,7 +37,7 @@ function readPage(page: PDFPage, index: number): PageFacts {
|
||||
let y: number | null = null
|
||||
let lines: string[] = []
|
||||
let matrices: number[][] = []
|
||||
for (const line of contentOf(page).split('\n')) {
|
||||
for (const line of file.content(page).split('\n')) {
|
||||
const tokens = line.trim().split(' ')
|
||||
const op = tokens[tokens.length - 1]
|
||||
const operands = tokens.slice(0, -1)
|
||||
@@ -123,11 +86,11 @@ function readPage(page: PDFPage, index: number): PageFacts {
|
||||
|
||||
/** Every text and image call of the file, page by page, in stream order. */
|
||||
export async function readDrawing(bytes: Uint8Array): Promise<PageFacts> {
|
||||
const doc = await PDFDocument.load(bytes)
|
||||
const file = PdfFile.parse(bytes)
|
||||
const texts: TextEntry[] = []
|
||||
const images: ImageEntry[] = []
|
||||
doc.getPages().forEach((page, index) => {
|
||||
const facts = readPage(page, index)
|
||||
file.pages().forEach((page, index) => {
|
||||
const facts = readPage(file, page, index)
|
||||
texts.push(...facts.texts)
|
||||
images.push(...facts.images)
|
||||
})
|
||||
|
||||
@@ -1,389 +0,0 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
||||
import { dirname, resolve } from 'node:path'
|
||||
import { expect, vi } from 'vitest'
|
||||
import {
|
||||
PDFArray,
|
||||
PDFDict,
|
||||
PDFDocument,
|
||||
PDFFont,
|
||||
PDFHexString,
|
||||
PDFImage,
|
||||
PDFName,
|
||||
PDFNumber,
|
||||
PDFPage,
|
||||
PDFString,
|
||||
} from 'pdf-lib'
|
||||
import {
|
||||
FIXTURES_DIR,
|
||||
round2,
|
||||
type ImageEntry,
|
||||
type LinkEntry,
|
||||
type TextEntry,
|
||||
} from './goldens'
|
||||
|
||||
/**
|
||||
* R4.1 freeze, writer oracles (plan 2026-10-17 §B.5.1 O1 to O3): what the
|
||||
* TypeScript writer draws, at full precision, for the Rust writer to
|
||||
* reproduce. The recorder is the Step 6 one made complete: every drawText,
|
||||
* drawImage, drawLine and drawRectangle call with every option passed, the
|
||||
* images by embed order and PNG digest, the links read back from the saved
|
||||
* bytes. `MAKE_R4_ORACLES=1` writes the files; otherwise they are asserted.
|
||||
*/
|
||||
|
||||
export const MAKE_R4_ORACLES = process.env.MAKE_R4_ORACLES === '1'
|
||||
export const R4_GOLDEN_DIR = resolve(FIXTURES_DIR, 'goldens/r4')
|
||||
export const R3_PDF_DIR = resolve(FIXTURES_DIR, 'pdf/r3')
|
||||
export const STEP6_PDF_DIR = resolve(FIXTURES_DIR, 'pdf/step6')
|
||||
|
||||
/** The engine suites' export time (packages/pdf/tests/helpers/goldens.ts). */
|
||||
export const FIXED_EXPORTED_AT = new Date('2026-10-11T12:00:00.000Z')
|
||||
|
||||
export type Json =
|
||||
| null
|
||||
| boolean
|
||||
| number
|
||||
| string
|
||||
| Json[]
|
||||
| { [key: string]: Json }
|
||||
|
||||
/** A type alias, not an interface: it must fit the `Json` index signature. */
|
||||
export type ImageRef = {
|
||||
embedIndex: number
|
||||
pngSha256: string
|
||||
}
|
||||
|
||||
export type DrawOpName = 'drawText' | 'drawImage' | 'drawLine' | 'drawRectangle'
|
||||
|
||||
export interface DrawOp {
|
||||
/** Global call order across pages: footers are drawn after every page. */
|
||||
seq: number
|
||||
op: DrawOpName
|
||||
text?: string
|
||||
image?: ImageRef
|
||||
options: Json
|
||||
}
|
||||
|
||||
export interface FullLink {
|
||||
page: number
|
||||
rect: number[]
|
||||
uri: string
|
||||
}
|
||||
|
||||
export interface DrawLog {
|
||||
pages: { index: number; ops: DrawOp[] }[]
|
||||
/** Every embedPng call in order, drawn or not: each one is an XObject. */
|
||||
embeds: ImageRef[]
|
||||
links: FullLink[]
|
||||
}
|
||||
|
||||
export const sha256 = (data: Uint8Array | string): string =>
|
||||
createHash('sha256').update(data).digest('hex')
|
||||
|
||||
interface RecorderState {
|
||||
ops: { page: number; op: DrawOp }[]
|
||||
embeds: ImageRef[]
|
||||
images: Map<PDFImage, ImageRef>
|
||||
documents: Set<PDFDocument>
|
||||
}
|
||||
|
||||
/** Non-finite numbers and -0 have no JSON form: tagged so none is lost. */
|
||||
function numberOf(value: number): Json {
|
||||
if (Number.isFinite(value) && !Object.is(value, -0)) return value
|
||||
return { $number: Object.is(value, -0) ? '-0' : String(value) }
|
||||
}
|
||||
|
||||
function imageRefOf(state: RecorderState, image: PDFImage): ImageRef {
|
||||
const ref = state.images.get(image)
|
||||
if (!ref) throw new Error('drawImage of an image embedPng did not return')
|
||||
return ref
|
||||
}
|
||||
|
||||
function objectOf(
|
||||
value: object,
|
||||
state: RecorderState,
|
||||
path: Set<object>,
|
||||
): Json {
|
||||
if (path.has(value)) throw new Error('cyclic draw argument')
|
||||
path.add(value)
|
||||
const out: Json = Array.isArray(value)
|
||||
? value.map((item) => serialise(item, state, path))
|
||||
: Object.fromEntries(
|
||||
Object.entries(value).map(([k, v]) => [k, serialise(v, state, path)]),
|
||||
)
|
||||
path.delete(value)
|
||||
return out
|
||||
}
|
||||
|
||||
/** Generic: whatever a call passes is kept, keys in insertion order. */
|
||||
function serialise(
|
||||
value: unknown,
|
||||
state: RecorderState,
|
||||
path = new Set<object>(),
|
||||
): Json {
|
||||
if (value === undefined) return { $undefined: true }
|
||||
if (value === null || typeof value === 'string') return value
|
||||
if (typeof value === 'boolean') return value
|
||||
if (typeof value === 'number') return numberOf(value)
|
||||
if (value instanceof PDFFont) return value.name
|
||||
if (value instanceof PDFImage) return imageRefOf(state, value)
|
||||
if (typeof value === 'object') return objectOf(value, state, path)
|
||||
throw new Error(`unexpected draw argument of type ${typeof value}`)
|
||||
}
|
||||
|
||||
/** Pages are appended in order and never moved: the index at draw time is final. */
|
||||
function pageIndex(page: PDFPage): number {
|
||||
return page.doc.getPages().indexOf(page)
|
||||
}
|
||||
|
||||
function record(
|
||||
state: RecorderState,
|
||||
page: PDFPage,
|
||||
entry: Omit<DrawOp, 'seq'>,
|
||||
): void {
|
||||
state.documents.add(page.doc)
|
||||
state.ops.push({
|
||||
page: pageIndex(page),
|
||||
op: { seq: state.ops.length, ...entry },
|
||||
})
|
||||
}
|
||||
|
||||
function pngBytes(png: string | Uint8Array | ArrayBuffer): Uint8Array {
|
||||
if (png instanceof Uint8Array) return png
|
||||
if (png instanceof ArrayBuffer) return new Uint8Array(png)
|
||||
throw new Error('embedPng received a string: the engine passes bytes')
|
||||
}
|
||||
|
||||
function spyEmbedPng(state: RecorderState) {
|
||||
const original = PDFDocument.prototype.embedPng
|
||||
return vi
|
||||
.spyOn(PDFDocument.prototype, 'embedPng')
|
||||
.mockImplementation(async function (this: PDFDocument, png) {
|
||||
state.documents.add(this)
|
||||
const ref = {
|
||||
embedIndex: state.embeds.length,
|
||||
pngSha256: sha256(pngBytes(png)),
|
||||
}
|
||||
state.embeds.push(ref)
|
||||
const image = await original.call(this, png)
|
||||
state.images.set(image, ref)
|
||||
return image
|
||||
})
|
||||
}
|
||||
|
||||
function spyDrawText(state: RecorderState) {
|
||||
const original = PDFPage.prototype.drawText
|
||||
return vi.spyOn(PDFPage.prototype, 'drawText').mockImplementation(function (
|
||||
this: PDFPage,
|
||||
text,
|
||||
options,
|
||||
) {
|
||||
const opts = serialise(options, state)
|
||||
record(state, this, { op: 'drawText', text, options: opts })
|
||||
return original.call(this, text, options)
|
||||
})
|
||||
}
|
||||
|
||||
function spyDrawImage(state: RecorderState) {
|
||||
const original = PDFPage.prototype.drawImage
|
||||
return vi.spyOn(PDFPage.prototype, 'drawImage').mockImplementation(function (
|
||||
this: PDFPage,
|
||||
image,
|
||||
options,
|
||||
) {
|
||||
const ref = imageRefOf(state, image)
|
||||
const opts = serialise(options, state)
|
||||
record(state, this, { op: 'drawImage', image: ref, options: opts })
|
||||
return original.call(this, image, options)
|
||||
})
|
||||
}
|
||||
|
||||
function spyDrawLine(state: RecorderState) {
|
||||
const original = PDFPage.prototype.drawLine
|
||||
return vi.spyOn(PDFPage.prototype, 'drawLine').mockImplementation(function (
|
||||
this: PDFPage,
|
||||
options,
|
||||
) {
|
||||
record(state, this, { op: 'drawLine', options: serialise(options, state) })
|
||||
return original.call(this, options)
|
||||
})
|
||||
}
|
||||
|
||||
function spyDrawRectangle(state: RecorderState) {
|
||||
const original = PDFPage.prototype.drawRectangle
|
||||
return vi
|
||||
.spyOn(PDFPage.prototype, 'drawRectangle')
|
||||
.mockImplementation(function (this: PDFPage, options) {
|
||||
const opts = serialise(options, state)
|
||||
record(state, this, { op: 'drawRectangle', options: opts })
|
||||
return original.call(this, options)
|
||||
})
|
||||
}
|
||||
|
||||
/** Drawing and embedding entry points the engine must not use. */
|
||||
const UNEXPECTED_PAGE = [
|
||||
'drawPage',
|
||||
'drawSvgPath',
|
||||
'drawSquare',
|
||||
'drawEllipse',
|
||||
'drawCircle',
|
||||
] as const
|
||||
const UNEXPECTED_DOC = ['embedJpg', 'embedPdf', 'embedPage'] as const
|
||||
|
||||
function guardSpies() {
|
||||
return [
|
||||
...UNEXPECTED_PAGE.map((name) => ({
|
||||
name,
|
||||
spy: vi.spyOn(PDFPage.prototype, name),
|
||||
})),
|
||||
...UNEXPECTED_DOC.map((name) => ({
|
||||
name,
|
||||
spy: vi.spyOn(PDFDocument.prototype, name),
|
||||
})),
|
||||
]
|
||||
}
|
||||
|
||||
export interface DrawRecorder {
|
||||
state: RecorderState
|
||||
/** Names of the guarded methods that were called. */
|
||||
unexpectedCalls(): string[]
|
||||
stop(): void
|
||||
}
|
||||
|
||||
export function recordFullDrawing(): DrawRecorder {
|
||||
const state: RecorderState = {
|
||||
ops: [],
|
||||
embeds: [],
|
||||
images: new Map(),
|
||||
documents: new Set(),
|
||||
}
|
||||
const spies = [
|
||||
spyEmbedPng(state),
|
||||
spyDrawText(state),
|
||||
spyDrawImage(state),
|
||||
spyDrawLine(state),
|
||||
spyDrawRectangle(state),
|
||||
]
|
||||
const guards = guardSpies()
|
||||
return {
|
||||
state,
|
||||
unexpectedCalls: () =>
|
||||
guards.filter((g) => g.spy.mock.calls.length > 0).map((g) => g.name),
|
||||
stop: () =>
|
||||
[...spies, ...guards.map((g) => g.spy)].forEach((s) => s.mockRestore()),
|
||||
}
|
||||
}
|
||||
|
||||
function linkOf(dict: PDFDict, page: number): FullLink | null {
|
||||
if (dict.get(PDFName.of('Subtype')) !== PDFName.of('Link')) return null
|
||||
const rect = dict
|
||||
.lookup(PDFName.of('Rect'), PDFArray)
|
||||
.asArray()
|
||||
.map((n) => (n as PDFNumber).asNumber())
|
||||
const uri = dict.lookup(PDFName.of('A'), PDFDict).lookup(PDFName.of('URI'))
|
||||
const text =
|
||||
uri instanceof PDFString || uri instanceof PDFHexString
|
||||
? uri.decodeText()
|
||||
: String(uri)
|
||||
return { page, rect, uri: text }
|
||||
}
|
||||
|
||||
export interface PdfFacts {
|
||||
pageCount: number
|
||||
links: FullLink[]
|
||||
}
|
||||
|
||||
/** Page count and every `/Annot /Link` of the saved bytes, in annotation order. */
|
||||
export async function readPdfFacts(bytes: Uint8Array): Promise<PdfFacts> {
|
||||
const doc = await PDFDocument.load(bytes)
|
||||
const links: FullLink[] = []
|
||||
doc.getPages().forEach((page, index) => {
|
||||
const annots = page.node.Annots()
|
||||
for (let i = 0; i < (annots?.size() ?? 0); i += 1) {
|
||||
const link = linkOf(annots!.lookup(i, PDFDict), index)
|
||||
if (link) links.push(link)
|
||||
}
|
||||
})
|
||||
return { pageCount: doc.getPageCount(), links }
|
||||
}
|
||||
|
||||
export function drawLogOf(recorder: DrawRecorder, facts: PdfFacts): DrawLog {
|
||||
const { ops, embeds } = recorder.state
|
||||
if (ops.some((o) => o.page < 0 || o.page >= facts.pageCount)) {
|
||||
throw new Error('a draw call landed on a page the saved PDF lacks')
|
||||
}
|
||||
const pages = Array.from({ length: facts.pageCount }, (_, index) => ({
|
||||
index,
|
||||
ops: ops.filter((o) => o.page === index).map((o) => o.op),
|
||||
}))
|
||||
return { pages, embeds, links: facts.links }
|
||||
}
|
||||
|
||||
function inCallOrder(log: DrawLog, name: DrawOpName) {
|
||||
return log.pages
|
||||
.flatMap((p) => p.ops.map((op) => ({ page: p.index, op })))
|
||||
.filter((entry) => entry.op.op === name)
|
||||
.sort((a, b) => a.op.seq - b.op.seq)
|
||||
}
|
||||
|
||||
const optionsOf = (op: DrawOp) => op.options as Record<string, Json>
|
||||
|
||||
/** The Step 6 text golden's projection: 2 decimals, the same fields. */
|
||||
export function step6Texts(log: DrawLog): TextEntry[] {
|
||||
return inCallOrder(log, 'drawText').map(({ page, op }) => {
|
||||
const o = optionsOf(op)
|
||||
return {
|
||||
page,
|
||||
text: op.text ?? '',
|
||||
x: round2(o.x),
|
||||
y: round2(o.y),
|
||||
size: round2(o.size),
|
||||
font: typeof o.font === 'string' ? o.font : null,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function step6Images(log: DrawLog): ImageEntry[] {
|
||||
return inCallOrder(log, 'drawImage').map(({ page, op }) => {
|
||||
const o = optionsOf(op)
|
||||
return {
|
||||
page,
|
||||
x: round2(o.x),
|
||||
y: round2(o.y),
|
||||
width: round2(o.width),
|
||||
height: round2(o.height),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function step6Links(log: DrawLog): LinkEntry[] {
|
||||
return log.links.map(({ page, rect, uri }) => ({
|
||||
page,
|
||||
rect: rect.map((n) => round2(n) ?? 0),
|
||||
uri,
|
||||
}))
|
||||
}
|
||||
|
||||
/** Writes under MAKE_R4_ORACLES; otherwise deep equality, then the exact text. */
|
||||
export function checkJsonOracle(file: string, value: unknown): void {
|
||||
const text = `${JSON.stringify(value, null, 2)}\n`
|
||||
if (MAKE_R4_ORACLES) {
|
||||
mkdirSync(dirname(file), { recursive: true })
|
||||
writeFileSync(file, text)
|
||||
return
|
||||
}
|
||||
const committed = readFileSync(file, 'utf8')
|
||||
expect(JSON.parse(text), file).toEqual(JSON.parse(committed))
|
||||
expect(text, file).toBe(committed)
|
||||
}
|
||||
|
||||
/** Writes under MAKE_R4_ORACLES; otherwise byte equality (by digest). */
|
||||
export function checkBytesOracle(file: string, bytes: Uint8Array): void {
|
||||
if (MAKE_R4_ORACLES) {
|
||||
mkdirSync(dirname(file), { recursive: true })
|
||||
writeFileSync(file, bytes)
|
||||
return
|
||||
}
|
||||
const committed = new Uint8Array(readFileSync(file))
|
||||
expect(sha256(bytes), file).toBe(sha256(committed))
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import type {
|
||||
ReportArchiveEnvelope,
|
||||
SheetSnapshot,
|
||||
} from '@faunosteo/core-wasm/types'
|
||||
import { defaultPdfLabels } from '@faunosteo/pdf'
|
||||
import { defaultPdfLabels } from '@/pdf/labels'
|
||||
import type { SheetIndexData } from '@faunosteo/core-wasm/types'
|
||||
import type { ReportMigrationMap } from '@faunosteo/core-wasm/n-1'
|
||||
import { coreRuntime } from '@/core/runtime'
|
||||
|
||||
@@ -9,14 +9,16 @@
|
||||
* writes the files: never once the switch has landed without an owner
|
||||
* decision. Since R2 the golden reports are read and the snapshot built by
|
||||
* the Rust core: the attachment differs from the frozen one by key order at
|
||||
* most (the deep-equality below ignores it).
|
||||
* most (the deep-equality below ignores it). Since R4.8 the attachment is
|
||||
* located by the frozen O11 scanner (`@faunosteo/core-wasm/n-1`), the R3
|
||||
* build's reader, as a rollback would locate it.
|
||||
*/
|
||||
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
||||
import { resolve } from 'node:path'
|
||||
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
|
||||
import { readAttachmentBytes } from '@faunosteo/pdf'
|
||||
import {
|
||||
readArchiveDocument,
|
||||
readAttachmentBytes,
|
||||
ReportArchiveEnvelopeSchema,
|
||||
} from '@faunosteo/core-wasm/n-1'
|
||||
import { exportReportToPdf } from '@/pdf/export'
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
} from 'vitest'
|
||||
import { render, screen, cleanup } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import type { Observation } from '@faunosteo/domain'
|
||||
import type { Observation } from '@faunosteo/core-wasm/types'
|
||||
import ObservationCard from '@/components/observation/ObservationCard'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { fr } from '@/i18n/fr'
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
import { describe, it, expect, vi, beforeAll } from 'vitest'
|
||||
import type { Finding, Report } from '@faunosteo/core-wasm/types'
|
||||
import { exportReportToPdf } from '@/pdf/export'
|
||||
import { importAttachment, importReportFromPdf } from '@/pdf/import'
|
||||
import { importAttachment, importReportFromPdf } from '../helpers/pdfImport'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { findingNodeAt, loadTestSheet } from '../helpers/sheet'
|
||||
import {
|
||||
@@ -57,7 +57,7 @@ describe('the 612 legacy leaves in the application', () => {
|
||||
it('keeps every finding, resolved or not, through an archive PDF export and import', async () => {
|
||||
const { pdf } = await exportReportToPdf(report, sheet, { locale: 'fr' })
|
||||
const bytes = new Uint8Array(await pdf.arrayBuffer())
|
||||
const { read: back } = await importReportFromPdf(bytes, sheet)
|
||||
const { read: back } = await importReportFromPdf(bytes)
|
||||
expect(back.migrated).toBe(false)
|
||||
expect(back.origin).toBe('envelope_v1')
|
||||
expect(back.archive).not.toBeNull()
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
* stay readable by the build before R2, whose Zod schemas and readers are
|
||||
* frozen in `@faunosteo/core-wasm/n-1`. A rollback to that build then finds
|
||||
* every session the R2 build left behind (key order may differ: D-R11).
|
||||
* Since R4.8 the attachment is located by the frozen O11 scanner of that
|
||||
* same package (the R3 build's byte-scan reader), never by the core.
|
||||
*/
|
||||
import {
|
||||
afterAll,
|
||||
@@ -18,13 +20,13 @@ import { readFileSync } from 'node:fs'
|
||||
import { resolve } from 'node:path'
|
||||
import {
|
||||
readArchiveDocument,
|
||||
readAttachmentBytes,
|
||||
readReportDocument,
|
||||
ReportArchiveEnvelopeSchema,
|
||||
ReportSchema,
|
||||
} from '@faunosteo/core-wasm/n-1'
|
||||
import { readAttachmentBytes } from '@faunosteo/pdf'
|
||||
import { exportReportToPdf } from '@/pdf/export'
|
||||
import { importAttachment } from '@/pdf/import'
|
||||
import { importAttachment } from '../helpers/pdfImport'
|
||||
import { useReportStore } from '@/state/reportStore'
|
||||
import { ARCHIVE_KEY, DRAFT_KEY } from '@/state/persistence'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
|
||||
@@ -1,230 +0,0 @@
|
||||
/**
|
||||
* T2 and user scenario 3 (STEP6 §7): the archive the app exports is
|
||||
* self-sufficient. Once exported, the catalog may change its labels and
|
||||
* geometry and the deployment may ship other logos: a re-export from the
|
||||
* envelope alone reproduces the first export, not the current resources.
|
||||
*/
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
expect,
|
||||
vi,
|
||||
beforeAll,
|
||||
afterAll,
|
||||
afterEach,
|
||||
} from 'vitest'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { resolve } from 'node:path'
|
||||
import type {
|
||||
Branding,
|
||||
LocalizedText,
|
||||
Report,
|
||||
} from '@faunosteo/core-wasm/types'
|
||||
import { exportFromEnvelope } from '@faunosteo/pdf'
|
||||
import { exportReportToPdf } from '@/pdf/export'
|
||||
import { importAttachment, importReportFromPdf } from '@/pdf/import'
|
||||
import { browserRasterizer } from '@/pdf/browserRasterizer'
|
||||
import { loadBranding } from '@/pdf/branding'
|
||||
import type { BundledSheetData } from '@/sheets/provider'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import {
|
||||
bundledSheet,
|
||||
loadTestSheet,
|
||||
sheetFromBundle,
|
||||
sheetSnapshot,
|
||||
} from '../helpers/sheet'
|
||||
import {
|
||||
FIXTURES_DIR,
|
||||
ONE_PX_PNG,
|
||||
readLinks,
|
||||
snapshotViews,
|
||||
type ImageEntry,
|
||||
type LinkEntry,
|
||||
type TextEntry,
|
||||
} from '../helpers/goldens'
|
||||
import { bytesOf, readDrawing } from '../helpers/pdfRead'
|
||||
|
||||
// Same fixed raster for every view and logo: images then depend on the
|
||||
// branding and the layout only.
|
||||
vi.mock('@/pdf/browserRasterizer', async () => {
|
||||
const { TWO_BY_ONE_PNG } = await import('../helpers/goldens')
|
||||
return { browserRasterizer: vi.fn(async () => TWO_BY_ONE_PNG) }
|
||||
})
|
||||
// The bundled branding, unless a scenario swaps the deployment's logos.
|
||||
const branding = vi.hoisted(() => ({ current: null as Branding | null }))
|
||||
vi.mock('@/pdf/branding', async (importOriginal) => {
|
||||
const real = await importOriginal<typeof import('@/pdf/branding')>()
|
||||
return {
|
||||
loadBranding: vi.fn(async () => branding.current ?? real.loadBranding()),
|
||||
}
|
||||
})
|
||||
|
||||
let sheet: SheetIndex
|
||||
let demo: Report
|
||||
|
||||
beforeAll(async () => {
|
||||
sheet = await loadTestSheet()
|
||||
demo = importAttachment(
|
||||
JSON.parse(
|
||||
readFileSync(resolve(FIXTURES_DIR, 'report-v3.demo.json'), 'utf8'),
|
||||
),
|
||||
sheet,
|
||||
).read.report
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => undefined)
|
||||
})
|
||||
afterEach(() => {
|
||||
branding.current = null
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
afterAll(() => vi.restoreAllMocks())
|
||||
|
||||
interface Facts {
|
||||
texts: TextEntry[]
|
||||
images: ImageEntry[]
|
||||
links: LinkEntry[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs an export and reads its facts back from the file (R4.6: the app export is the
|
||||
* core's, the re-export the engine's; both files are read the same way).
|
||||
*/
|
||||
async function recorded<T extends { pdf: Uint8Array | Blob }>(
|
||||
run: () => Promise<T>,
|
||||
): Promise<Omit<T, 'pdf'> & { pdf: Uint8Array } & Facts> {
|
||||
const result = await run()
|
||||
const pdf =
|
||||
result.pdf instanceof Uint8Array ? result.pdf : await bytesOf(result.pdf)
|
||||
const { texts, images } = await readDrawing(pdf)
|
||||
return { ...result, pdf, texts, images, links: await readLinks(pdf) }
|
||||
}
|
||||
|
||||
const facts = ({ texts, images, links }: Facts): Facts => ({
|
||||
texts,
|
||||
images,
|
||||
links,
|
||||
})
|
||||
|
||||
/** The app export path, bundled logos, under the recorder. */
|
||||
function appExport(index: SheetIndex): Promise<Facts & { pdf: Uint8Array }> {
|
||||
return recorded(() => exportReportToPdf(demo, index, { locale: 'fr' }))
|
||||
}
|
||||
|
||||
/** The envelope the app reads back from its own export, as the engine takes it. */
|
||||
type EngineEnvelope = Parameters<typeof exportFromEnvelope>[0]
|
||||
|
||||
async function archiveOf(pdf: Uint8Array): Promise<EngineEnvelope> {
|
||||
const { read: outcome } = await importReportFromPdf(pdf, sheet)
|
||||
expect(outcome.origin).toBe('envelope_v1')
|
||||
return outcome.archive! as EngineEnvelope
|
||||
}
|
||||
|
||||
const REEXPORT = {
|
||||
locale: 'fr' as const,
|
||||
rasterizer: browserRasterizer,
|
||||
exportedAt: new Date('2027-01-01T00:00:00.000Z'),
|
||||
}
|
||||
|
||||
/** A category or node of the bundled payload (authoring form: leaves have no children). */
|
||||
interface PayloadNode {
|
||||
name: LocalizedText
|
||||
children?: PayloadNode[]
|
||||
}
|
||||
|
||||
interface PayloadDocument {
|
||||
annotations: {
|
||||
id: string
|
||||
name: LocalizedText | null
|
||||
geometry: { type: string; cx?: number } | null
|
||||
}[]
|
||||
finding_zones: { name: LocalizedText; categories: PayloadNode[] }[]
|
||||
}
|
||||
|
||||
function renameNodes(nodes: PayloadNode[]): void {
|
||||
for (const node of nodes) {
|
||||
node.name = {
|
||||
fr: `${node.name.fr} (révisé)`,
|
||||
en: `${node.name.en} (revised)`,
|
||||
}
|
||||
renameNodes(node.children ?? [])
|
||||
}
|
||||
}
|
||||
|
||||
/** The catalog after a revision: every label changed, the hip marker moved. */
|
||||
function revisedSheet(source: BundledSheetData): BundledSheetData {
|
||||
const next = structuredClone(source)
|
||||
const document = next.document as PayloadDocument
|
||||
for (const a of document.annotations) {
|
||||
if (a.name) {
|
||||
a.name = { fr: `${a.name.fr} (révisé)`, en: `${a.name.en} (revised)` }
|
||||
}
|
||||
if (a.id === 'hip' && a.geometry?.type === 'circle') a.geometry.cx! += 100
|
||||
}
|
||||
for (const zone of document.finding_zones) {
|
||||
zone.name = {
|
||||
fr: `${zone.name.fr} (révisé)`,
|
||||
en: `${zone.name.en} (revised)`,
|
||||
}
|
||||
renameNodes(zone.categories)
|
||||
}
|
||||
return next
|
||||
}
|
||||
|
||||
describe('the archive survives its resources', () => {
|
||||
it('T2 catalog changed: the envelope re-exports the archived sheet, not the revised one', async () => {
|
||||
const first = await appExport(sheet)
|
||||
const archive = await archiveOf(first.pdf)
|
||||
|
||||
// The revision is visible through the app path (labels and geometry)...
|
||||
const revised = sheetFromBundle(revisedSheet(await bundledSheet()))
|
||||
const onRevised = await appExport(revised)
|
||||
expect(onRevised.texts).not.toEqual(first.texts)
|
||||
const revisedViews = snapshotViews(sheetSnapshot(revised, demo), demo)
|
||||
const archivedViews = snapshotViews(
|
||||
archive.sheetSnapshot as Parameters<typeof snapshotViews>[0],
|
||||
demo,
|
||||
)
|
||||
expect(revisedViews).not.toEqual(archivedViews)
|
||||
|
||||
// ...and absent from a re-export of the envelope alone.
|
||||
const again = await recorded(() => exportFromEnvelope(archive, REEXPORT))
|
||||
expect(again.warnings).toEqual([])
|
||||
expect(facts(again)).toEqual(facts(first))
|
||||
expect(
|
||||
snapshotViews(again.envelope.sheetSnapshot, again.envelope.report),
|
||||
).toEqual(archivedViews)
|
||||
}, 60_000)
|
||||
|
||||
it('scenario 3 branding changed: the envelope re-exports the archived branding and never fetches', async () => {
|
||||
const first = await appExport(sheet)
|
||||
const archive = await archiveOf(first.pdf)
|
||||
|
||||
// The deployment now ships other logos: a 1×1 PNG everywhere, another partner SVG.
|
||||
const onePx = {
|
||||
kind: 'png' as const,
|
||||
base64: Buffer.from(ONE_PX_PNG).toString('base64'),
|
||||
}
|
||||
const otherSvg =
|
||||
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><rect width="10" height="10"/></svg>'
|
||||
branding.current = {
|
||||
...archive.branding,
|
||||
headerLogo: onePx,
|
||||
footerLogo: onePx,
|
||||
poweredBy: { ...archive.branding.poweredBy!, logo: onePx },
|
||||
partner: {
|
||||
...archive.branding.partner!,
|
||||
logo: { kind: 'svg', svg: otherSvg },
|
||||
},
|
||||
}
|
||||
const current = await loadBranding()
|
||||
expect(current.headerLogo).not.toBeNull()
|
||||
expect(current).not.toEqual(archive.branding)
|
||||
const fetchSpy = vi.fn()
|
||||
vi.stubGlobal('fetch', fetchSpy)
|
||||
|
||||
const again = await recorded(() => exportFromEnvelope(archive, REEXPORT))
|
||||
expect(fetchSpy).not.toHaveBeenCalled()
|
||||
expect(again.warnings).toEqual([])
|
||||
expect(facts(again)).toEqual(facts(first))
|
||||
expect(again.envelope.branding).toEqual(archive.branding)
|
||||
}, 60_000)
|
||||
})
|
||||
@@ -4,22 +4,24 @@
|
||||
* with the bundled sheet snapshotted, the bundled branding (mocked to every
|
||||
* slot empty for the `nologos` mode, the real module for `logos`) and the
|
||||
* browser rasterizer (mocked to fixed PNGs, as the Phase 0 stub was). Text,
|
||||
* images and links goldens are read back from the file's content streams and
|
||||
* asserted as frozen; `MAKE_GOLDEN=1` rewrites them, never without a user
|
||||
* decision. At the fixed export time the file's bytes are the R4 digests the
|
||||
* cargo suite froze (`goldens/r4/pdf.*.sha256`): the app path writes what the
|
||||
* native core writes. Never regenerate `report-v3.bare.pdf`: it is the Step 5
|
||||
* bare export, an import fixture only.
|
||||
* images and links goldens are read back from the file (`tests/helpers/pdfFile.ts`,
|
||||
* R4.8: no pdf-lib) and asserted as frozen; `MAKE_GOLDEN=1` rewrites them,
|
||||
* never without a user decision. The highlighted-view goldens are read from
|
||||
* the SVGs the core's session asks the rasteriser for. At the fixed export
|
||||
* time the file's bytes are the R4 digests the cargo suite froze
|
||||
* (`goldens/r4/pdf.*.sha256`): the app path writes what the native core
|
||||
* writes. Never regenerate `report-v3.bare.pdf`: it is the Step 5 bare
|
||||
* export, an import fixture only.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest'
|
||||
import { PDFDocument } from 'pdf-lib'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { resolve } from 'node:path'
|
||||
import type { Report } from '@faunosteo/core-wasm/types'
|
||||
import { exportReportToPdf } from '@/pdf/export'
|
||||
import { importReportFromPdf } from '@/pdf/import'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { importReportFromPdf } from '../helpers/pdfImport'
|
||||
import { PdfFile, numberOf } from '../helpers/pdfFile'
|
||||
import { loadTestSheet, sheetSnapshot } from '../helpers/sheet'
|
||||
import {
|
||||
buildGoldenReports,
|
||||
@@ -28,10 +30,13 @@ import {
|
||||
} from '../helpers/goldenReports'
|
||||
import {
|
||||
checkGolden,
|
||||
FIXED_EXPORTED_AT,
|
||||
FIXTURES_DIR,
|
||||
ONE_PX_PNG,
|
||||
R4_GOLDEN_DIR,
|
||||
readGolden,
|
||||
readLinks,
|
||||
sha256,
|
||||
snapshotViews,
|
||||
TWO_BY_ONE_PNG,
|
||||
type ImageEntry,
|
||||
@@ -41,11 +46,18 @@ import {
|
||||
type ViewSnapshot,
|
||||
} from '../helpers/goldens'
|
||||
import { bytesOf, readDrawing } from '../helpers/pdfRead'
|
||||
import { FIXED_EXPORTED_AT, R4_GOLDEN_DIR, sha256 } from '../helpers/r4-draw'
|
||||
|
||||
const raster = vi.hoisted(() => ({ png: new Uint8Array() }))
|
||||
// The fixed PNG for every raster; the SVGs the session asks for are kept, in request
|
||||
// order (the view goldens are read from them).
|
||||
const raster = vi.hoisted(() => ({
|
||||
png: new Uint8Array(),
|
||||
svgs: [] as string[],
|
||||
}))
|
||||
vi.mock('@/pdf/browserRasterizer', () => ({
|
||||
browserRasterizer: vi.fn(async () => raster.png),
|
||||
browserRasterizer: vi.fn(async (svg: string) => {
|
||||
raster.svgs.push(svg)
|
||||
return raster.png
|
||||
}),
|
||||
}))
|
||||
const logoMode = vi.hoisted(() => ({ current: 'nologos' as LogoMode }))
|
||||
vi.mock('@/pdf/branding', async () => {
|
||||
@@ -86,6 +98,7 @@ async function exportWith(
|
||||
locale: Locale,
|
||||
): Promise<Exported> {
|
||||
raster.png = mode === 'logos' ? TWO_BY_ONE_PNG : ONE_PX_PNG
|
||||
raster.svgs = []
|
||||
logoMode.current = mode
|
||||
const { pdf, warnings } = await exportReportToPdf(report, sheet, { locale })
|
||||
expect(warnings).toEqual([])
|
||||
@@ -115,13 +128,25 @@ function r4Digest(name: string, locale: Locale, mode: LogoMode): string {
|
||||
const pagesOf = (entries: readonly { page: number }[]) =>
|
||||
entries.map((e) => e.page)
|
||||
|
||||
/** The size of every image XObject the file's first page draws. */
|
||||
function imageSizesOf(bytes: Uint8Array): [number, number][] {
|
||||
const file = PdfFile.parse(bytes)
|
||||
return [...file.xobjects(file.pages()[0]!).values()].map((image) => [
|
||||
numberOf(image.dict, 'Width')!,
|
||||
numberOf(image.dict, 'Height')!,
|
||||
])
|
||||
}
|
||||
|
||||
describe('golden inputs', () => {
|
||||
it('the 2×1 PNG decodes as 2×1 and the 1×1 as 1×1', async () => {
|
||||
const doc = await PDFDocument.create()
|
||||
const two = await doc.embedPng(TWO_BY_ONE_PNG)
|
||||
expect([two.width, two.height]).toEqual([2, 1])
|
||||
const one = await doc.embedPng(ONE_PX_PNG)
|
||||
expect([one.width, one.height]).toEqual([1, 1])
|
||||
// logos: the view and partner rasters are the 2×1 PNG, the bundled PNG logos their own size.
|
||||
const two = await exportWith(reports.demo, 'logos', 'fr')
|
||||
expect(imageSizesOf(two.bytes)).toContainEqual([2, 1])
|
||||
// nologos: every image of the page is a view raster, the 1×1 PNG.
|
||||
const one = await exportWith(reports.demo, 'nologos', 'fr')
|
||||
const sizes = imageSizesOf(one.bytes)
|
||||
expect(sizes.length).toBeGreaterThan(0)
|
||||
expect(new Set(sizes.map(String))).toEqual(new Set(['1,1']))
|
||||
})
|
||||
|
||||
it('the demo report covers the sheet features the goldens must freeze', () => {
|
||||
@@ -170,9 +195,7 @@ describe.each(REPORTS)('goldens of %s', (name) => {
|
||||
inPageOrder(`images.${id}.json`),
|
||||
)
|
||||
checkGolden(`links.${id}.json`, out.links)
|
||||
expect(sha256(out.bytes), id).toBe(
|
||||
r4Digest(name, locale, mode),
|
||||
)
|
||||
expect(sha256(out.bytes), id).toBe(r4Digest(name, locale, mode))
|
||||
}
|
||||
expect(pagesOf(en.images)).toEqual(pagesOf(fr.images))
|
||||
expect(pagesOf(en.links)).toEqual(pagesOf(fr.links))
|
||||
@@ -183,9 +206,11 @@ describe.each(REPORTS)('goldens of %s', (name) => {
|
||||
// display="none") and the layer <g> carries no data-* any more: `titles`
|
||||
// shrinks to the highlighted markers plus the asset's own, and
|
||||
// `dataAttributes` drops from 2 to 0. Everything else must match.
|
||||
it('highlighted views: same facts but titles and data attributes', () => {
|
||||
it('highlighted views: same facts but titles and data attributes', async () => {
|
||||
const snapshot = sheetSnapshot(sheet, reports[name])
|
||||
const actual = snapshotViews(snapshot, reports[name])
|
||||
// Every slot empty: the session asks for the view rasters only, in request order.
|
||||
await exportWith(reports[name], 'nologos', 'fr')
|
||||
const actual = snapshotViews(snapshot, reports[name], raster.svgs)
|
||||
const expected = readGolden<ViewSnapshot[]>(`views.${name}.json`)
|
||||
const comparable = (v: ViewSnapshot) => ({
|
||||
observationId: v.observationId,
|
||||
@@ -207,7 +232,7 @@ describe.each(REPORTS)('goldens of %s', (name) => {
|
||||
describe('report-v3.bare.pdf', () => {
|
||||
it('is the demo report exported bare by Step 5 and imports with no archive', async () => {
|
||||
const bytes = new Uint8Array(readFileSync(BARE_PDF))
|
||||
const { read: outcome } = await importReportFromPdf(bytes, sheet)
|
||||
const { read: outcome } = await importReportFromPdf(bytes)
|
||||
expect(outcome.origin).toBe('bare_v3')
|
||||
expect(outcome.migrated).toBe(false)
|
||||
expect(outcome.archive).toBeNull()
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
/**
|
||||
* Replaces pdf.attachment.test.ts and pdf.attachment.inflate.test.ts on the
|
||||
* engine read API: the typed ImportError kinds the home route maps. The
|
||||
* attachment roundtrip moved to pdf.roundtrip.test.ts ("attaches the
|
||||
* envelope and roundtrips") and the engine's tests/read.test.ts (T4); the
|
||||
* inflate bomb cap lives in the engine's tests/security.test.ts.
|
||||
*/
|
||||
import { describe, it, expect, beforeAll } from 'vitest'
|
||||
import { PDFDocument } from 'pdf-lib'
|
||||
import {
|
||||
importAttachment,
|
||||
importReportFromPdf,
|
||||
ImportError,
|
||||
MAX_PDF_BYTES,
|
||||
} from '@/pdf/import'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { loadTestSheet, minimalReport } from '../helpers/sheet'
|
||||
|
||||
let sheet: SheetIndex
|
||||
beforeAll(async () => {
|
||||
sheet = await loadTestSheet()
|
||||
})
|
||||
|
||||
/** Saved like the export (no object streams, BLK-009) so the keyword stamp is in clear. */
|
||||
async function pdfWithoutAttachment(keywords?: string[]): Promise<Uint8Array> {
|
||||
const doc = await PDFDocument.create()
|
||||
doc.addPage()
|
||||
if (keywords) doc.setKeywords(keywords)
|
||||
return doc.save({ useObjectStreams: false })
|
||||
}
|
||||
|
||||
async function rejectsWith(bytes: Uint8Array, kind: string): Promise<void> {
|
||||
const promise = importReportFromPdf(bytes, sheet)
|
||||
await expect(promise).rejects.toBeInstanceOf(ImportError)
|
||||
await expect(promise).rejects.toMatchObject({ kind })
|
||||
}
|
||||
|
||||
describe('importReportFromPdf error kinds', () => {
|
||||
it('no_attachment for a PDF without the attachment', async () => {
|
||||
await rejectsWith(await pdfWithoutAttachment(), 'no_attachment')
|
||||
})
|
||||
|
||||
it('attachment_stripped for a stamped archive whose attachment was removed', async () => {
|
||||
await rejectsWith(
|
||||
await pdfWithoutAttachment(['faunosteo-archive/1']),
|
||||
'attachment_stripped',
|
||||
)
|
||||
})
|
||||
|
||||
it('invalid_pdf for an oversized file, before parsing it', async () => {
|
||||
await rejectsWith(new Uint8Array(MAX_PDF_BYTES + 1), 'invalid_pdf')
|
||||
})
|
||||
})
|
||||
|
||||
describe('importAttachment on the core (L1)', () => {
|
||||
it('refuses an attachment with a lone surrogate as lone_surrogate, never rewritten', () => {
|
||||
for (const text of ['a\ud800b', '\udfff']) {
|
||||
const attachment = minimalReport(sheet, { conclusion: text })
|
||||
expect(() => importAttachment(attachment, sheet)).toThrow(
|
||||
expect.objectContaining({ kind: 'lone_surrogate' }),
|
||||
)
|
||||
expect(attachment.conclusion).toBe(text)
|
||||
}
|
||||
})
|
||||
|
||||
it('imports normal text with pairs and literal backslashes unchanged', () => {
|
||||
const text = 'é 😀 \\ud800 \ufffd'
|
||||
const imported = importAttachment(
|
||||
minimalReport(sheet, { conclusion: text }),
|
||||
sheet,
|
||||
)
|
||||
expect(imported.rebase.report.conclusion).toBe(text)
|
||||
})
|
||||
})
|
||||
@@ -6,36 +6,45 @@
|
||||
* archive PDFs, migrated without silent loss.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest'
|
||||
import { AFRelationship, PDFDocument } from 'pdf-lib'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { dirname, resolve } from 'node:path'
|
||||
import type { Observation, Report } from '@faunosteo/core-wasm/types'
|
||||
import { composeHighlightedViewSvg } from '@faunosteo/pdf'
|
||||
import { exportReportToPdf } from '@/pdf/export'
|
||||
import { browserRasterizer } from '@/pdf/browserRasterizer'
|
||||
import { defaultPdfLabels } from '@/pdf/labels'
|
||||
import { importReportFromPdf, ImportError } from '@/pdf/import'
|
||||
import { formatDate } from '@/lib/dates'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { loadTestSheet, minimalReport, sheetSnapshot } from '../helpers/sheet'
|
||||
import { importReportFromPdf, ImportError } from '../helpers/pdfImport'
|
||||
import {
|
||||
PdfFile,
|
||||
buildPdfWithAttachment,
|
||||
nameOf,
|
||||
textOf,
|
||||
} from '../helpers/pdfFile'
|
||||
import { loadTestSheet, minimalReport } from '../helpers/sheet'
|
||||
import { bytesOf, readDrawing } from '../helpers/pdfRead'
|
||||
|
||||
// The factory runs when the engine first loads the adapter: fixed 1×1 PNG.
|
||||
// The fixed 1×1 PNG for every raster; the view SVGs the session asks for are kept.
|
||||
const raster = vi.hoisted(() => ({ svgs: [] as string[] }))
|
||||
vi.mock('@/pdf/browserRasterizer', async () => {
|
||||
const { ONE_PX_PNG } = await import('../helpers/goldens')
|
||||
return { browserRasterizer: vi.fn(async () => ONE_PX_PNG) }
|
||||
return {
|
||||
browserRasterizer: vi.fn(async (svg: string) => {
|
||||
raster.svgs.push(svg)
|
||||
return ONE_PX_PNG
|
||||
}),
|
||||
}
|
||||
})
|
||||
|
||||
/** The engine's own (Zod-inferred) types of the same JSON contracts. */
|
||||
type EngineView = Parameters<typeof composeHighlightedViewSvg>
|
||||
|
||||
/** The layered reading the application does on import (the core's read part). */
|
||||
/** The layered reading the application does on import (the core's reader). */
|
||||
async function readBack(bytes: Uint8Array) {
|
||||
return (await importReportFromPdf(bytes, sheet)).read
|
||||
return (await importReportFromPdf(bytes)).read
|
||||
}
|
||||
|
||||
const pageCount = (bytes: Uint8Array) => PdfFile.parse(bytes).pages().length
|
||||
|
||||
/** The static PDF strings belong to the app's label set. */
|
||||
const { fr, en } = defaultPdfLabels
|
||||
|
||||
@@ -44,6 +53,7 @@ const { fr, en } = defaultPdfLabels
|
||||
* texts of its content streams, in draw order.
|
||||
*/
|
||||
async function exportPdf(report: Report, locale: Locale): Promise<Uint8Array> {
|
||||
raster.svgs = []
|
||||
const bytes = await bytesOf(
|
||||
(await exportReportToPdf(report, sheet, { locale })).pdf,
|
||||
)
|
||||
@@ -118,8 +128,7 @@ describe('export on a full v3 report (FR)', () => {
|
||||
conclusion: 'ok',
|
||||
})
|
||||
const bytes = await exportPdf(report, 'fr')
|
||||
const doc = await PDFDocument.load(bytes)
|
||||
expect(doc.getPageCount()).toBe(2)
|
||||
expect(pageCount(bytes)).toBe(2)
|
||||
const zonesIdx = drawn.indexOf(fr['pdf.section.zones']!)
|
||||
expect(drawn.slice(zonesIdx + 1, zonesIdx + 6)).toEqual([
|
||||
'Charnière C7-T1 (cervico-thoracique)',
|
||||
@@ -156,19 +165,21 @@ describe('export on a full v3 report (FR)', () => {
|
||||
}
|
||||
})
|
||||
|
||||
// The engine draws only the observation's markers (the browser pipeline
|
||||
// The core composes only the observation's markers (the browser pipeline
|
||||
// hid the others with display="none"); the view facts are frozen in
|
||||
// pdf.goldens.test.ts "highlighted views".
|
||||
it('highlights the observation zones of a view and omits the other markers', () => {
|
||||
// pdf.goldens.test.ts "highlighted views". The SVGs are the ones the
|
||||
// session asked the rasteriser for: the bundled partner logo (the one SVG
|
||||
// logo) first, then the main view, then the head.
|
||||
it('highlights the observation zones of a view and omits the other markers', async () => {
|
||||
const report = minimalReport(sheet, { observations: [fullObservation()] })
|
||||
const snapshot = sheetSnapshot(sheet, report) as EngineView[0]
|
||||
const svg = composeHighlightedViewSvg(snapshot, 'main', fullObservation())
|
||||
await exportPdf(report, 'fr')
|
||||
expect(raster.svgs).toHaveLength(3)
|
||||
const [svg, head] = raster.svgs.slice(1)
|
||||
expect(svg).toContain('viewBox="0 0 2073.4 1629.4"')
|
||||
expect(svg).toContain('fill="#b85a3f"')
|
||||
expect(svg).toContain('url(#osteo-halo)')
|
||||
expect(svg).not.toContain('display="none"')
|
||||
expect(svg).not.toContain('data-anno-id')
|
||||
const head = composeHighlightedViewSvg(snapshot, 'head', fullObservation())
|
||||
expect(head).toContain('viewBox="60 100 500 380"')
|
||||
expect(head).not.toContain('osteo-halo')
|
||||
})
|
||||
@@ -242,7 +253,7 @@ describe('PDF content on a two-observation report', () => {
|
||||
it('draws free findings, comments, the conclusion page and the header on every page', async () => {
|
||||
const report = fullReport()
|
||||
const bytes = await exportPdf(report, 'fr')
|
||||
const pages = (await PDFDocument.load(bytes)).getPageCount()
|
||||
const pages = pageCount(bytes)
|
||||
expect(drawn).toContain(fr['pdf.section.free_findings'])
|
||||
expect(joined()).toContain('Sensibilité au garrot')
|
||||
expect(joined()).toContain('Raideur au garrot')
|
||||
@@ -260,11 +271,11 @@ describe('PDF content on a two-observation report', () => {
|
||||
|
||||
it('sets the PDF metadata and the archive keyword', async () => {
|
||||
const bytes = await exportPdf(fullReport(), 'fr')
|
||||
const doc = await PDFDocument.load(bytes)
|
||||
expect(doc.getTitle()).toBe(`${fr['pdf.title.suffix.bilan']}Mistral`)
|
||||
expect(doc.getAuthor()).toBe('Dr. Dupont')
|
||||
expect(doc.getSubject()).toBe(fr['pdf.subject'])
|
||||
expect(doc.getKeywords()).toContain('faunosteo-archive/1')
|
||||
const info = PdfFile.parse(bytes).info().dict
|
||||
expect(textOf(info, 'Title')).toBe(`${fr['pdf.title.suffix.bilan']}Mistral`)
|
||||
expect(textOf(info, 'Author')).toBe('Dr. Dupont')
|
||||
expect(textOf(info, 'Subject')).toBe(fr['pdf.subject'])
|
||||
expect(textOf(info, 'Keywords')).toContain('faunosteo-archive/1')
|
||||
})
|
||||
|
||||
it('embeds the four bundled logos without any network request', async () => {
|
||||
@@ -289,20 +300,17 @@ describe('PDF content on a two-observation report', () => {
|
||||
|
||||
it('attaches osteo-report.json as a Flate EmbeddedFile, AFRelationship Data, "Faunosteo report archive v1"', async () => {
|
||||
const bytes = await exportPdf(fullReport(), 'fr')
|
||||
const doc = await PDFDocument.load(bytes)
|
||||
const objects = [...doc.context.enumerateIndirectObjects()].map(([, o]) =>
|
||||
String(o),
|
||||
)
|
||||
const filespec = objects.find((o) => o.includes('/Type /Filespec'))!
|
||||
expect(filespec).toContain('osteo-report.json')
|
||||
expect(filespec).toContain('/AFRelationship /Data')
|
||||
const utf16 = (text: string) =>
|
||||
[...text]
|
||||
.map((c) => c.charCodeAt(0).toString(16).toUpperCase().padStart(4, '0'))
|
||||
.join('')
|
||||
expect(filespec).toContain(utf16('Faunosteo report archive v1'))
|
||||
const embedded = objects.find((o) => o.includes('/Type /EmbeddedFile'))!
|
||||
expect(embedded).toContain('/Filter /FlateDecode')
|
||||
const objects = PdfFile.parse(bytes).all()
|
||||
const filespec = objects.find((o) => nameOf(o.dict, 'Type') === 'Filespec')!
|
||||
expect(textOf(filespec.dict, 'F')).toBe('osteo-report.json')
|
||||
expect(textOf(filespec.dict, 'UF')).toBe('osteo-report.json')
|
||||
expect(nameOf(filespec.dict, 'AFRelationship')).toBe('Data')
|
||||
expect(textOf(filespec.dict, 'Desc')).toBe('Faunosteo report archive v1')
|
||||
const embedded = objects.find(
|
||||
(o) => nameOf(o.dict, 'Type') === 'EmbeddedFile',
|
||||
)!
|
||||
expect(nameOf(embedded.dict, 'Filter')).toBe('FlateDecode')
|
||||
expect(embedded.stream).not.toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -324,7 +332,11 @@ describe('a raster the browser cannot draw', () => {
|
||||
locale: 'fr',
|
||||
})
|
||||
expect(warnings).toEqual([
|
||||
{ kind: 'view_raster_failed', observationId: 'obs-full', viewId: 'head' },
|
||||
{
|
||||
kind: 'view_raster_failed',
|
||||
observationId: 'obs-full',
|
||||
viewId: 'head',
|
||||
},
|
||||
])
|
||||
const bytes = await bytesOf(pdf)
|
||||
const { images, texts } = await readDrawing(bytes)
|
||||
@@ -349,19 +361,15 @@ describe('a raster the browser cannot draw', () => {
|
||||
})
|
||||
|
||||
describe('attachment rejection', () => {
|
||||
async function pdfWith(name: string, payload: unknown): Promise<Uint8Array> {
|
||||
const doc = await PDFDocument.create()
|
||||
doc.addPage()
|
||||
await doc.attach(new TextEncoder().encode(JSON.stringify(payload)), name, {
|
||||
mimeType: 'application/json',
|
||||
afRelationship: AFRelationship.Data,
|
||||
})
|
||||
return doc.save()
|
||||
}
|
||||
const pdfWith = (name: string, payload: unknown): Uint8Array =>
|
||||
buildPdfWithAttachment(
|
||||
name,
|
||||
new TextEncoder().encode(JSON.stringify(payload)),
|
||||
)
|
||||
|
||||
it('reports no_attachment for a foreign JSON attachment (no version)', async () => {
|
||||
const bytes = await pdfWith('other.json', { hello: 'world' })
|
||||
await expect(importReportFromPdf(bytes, sheet)).rejects.toMatchObject({
|
||||
const bytes = pdfWith('other.json', { hello: 'world' })
|
||||
await expect(importReportFromPdf(bytes)).rejects.toMatchObject({
|
||||
kind: 'no_attachment',
|
||||
})
|
||||
})
|
||||
@@ -369,11 +377,18 @@ describe('attachment rejection', () => {
|
||||
it('reports no_attachment for a v3 report failing validation', async () => {
|
||||
const broken: Record<string, unknown> = { ...minimalReport(sheet) }
|
||||
delete broken.sheet
|
||||
const bytes = await pdfWith('osteo-report.json', broken)
|
||||
await expect(importReportFromPdf(bytes, sheet)).rejects.toMatchObject({
|
||||
const bytes = pdfWith('osteo-report.json', broken)
|
||||
await expect(importReportFromPdf(bytes)).rejects.toMatchObject({
|
||||
kind: 'no_attachment',
|
||||
})
|
||||
})
|
||||
|
||||
it('reads the same attachment through the core when the report is sound', async () => {
|
||||
const report = minimalReport(sheet)
|
||||
const out = await readBack(pdfWith('osteo-report.json', report))
|
||||
expect(out.origin).toBe('bare_v3')
|
||||
expect(out.report).toEqual(report)
|
||||
})
|
||||
})
|
||||
|
||||
describe('import of the frozen v2 archive PDFs', () => {
|
||||
@@ -451,14 +466,12 @@ describe('import of the frozen v2 archive PDFs', () => {
|
||||
},
|
||||
],
|
||||
}
|
||||
const doc = await PDFDocument.create()
|
||||
doc.addPage()
|
||||
await doc.attach(
|
||||
new TextEncoder().encode(JSON.stringify(v2)),
|
||||
'osteo-report.json',
|
||||
{ mimeType: 'application/json' },
|
||||
const out = await readBack(
|
||||
buildPdfWithAttachment(
|
||||
'osteo-report.json',
|
||||
new TextEncoder().encode(JSON.stringify(v2)),
|
||||
),
|
||||
)
|
||||
const out = await readBack(await doc.save())
|
||||
const [obs] = out.report.observations
|
||||
expect(obs!.zones).toEqual([
|
||||
expect.objectContaining({
|
||||
@@ -494,17 +507,15 @@ describe('import of the frozen v2 archive PDFs', () => {
|
||||
})
|
||||
|
||||
it('refuses unknown versions and unrelated PDFs with typed errors', async () => {
|
||||
const doc = await PDFDocument.create()
|
||||
doc.addPage()
|
||||
await doc.attach(
|
||||
new TextEncoder().encode(JSON.stringify({ version: 7 })),
|
||||
const v7 = buildPdfWithAttachment(
|
||||
'osteo-report.json',
|
||||
{ mimeType: 'application/json' },
|
||||
new TextEncoder().encode(JSON.stringify({ version: 7 })),
|
||||
)
|
||||
await expect(
|
||||
importReportFromPdf(await doc.save(), sheet),
|
||||
).rejects.toMatchObject({ kind: 'incompatible_version', foundVersion: 7 })
|
||||
await expect(importReportFromPdf(v7)).rejects.toMatchObject({
|
||||
kind: 'incompatible_version',
|
||||
foundVersion: 7,
|
||||
})
|
||||
const fake = new Uint8Array([37, 80, 68, 70, 45, 49, 46, 52, 10])
|
||||
await expect(importReportFromPdf(fake, sheet)).rejects.toThrow(ImportError)
|
||||
await expect(importReportFromPdf(fake)).rejects.toThrow(ImportError)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
* R4.8 census (plan §B.7 R4.8, RUST.md §10.10): the PDF path runs on the core and nothing of
|
||||
* the TypeScript engine is left where the application or its glue could reach it. Grep facts
|
||||
* over the repository, so that a reintroduced import, a dependency added back or a script
|
||||
* repointed fails here before anything else.
|
||||
*/
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
|
||||
import { dirname, join, resolve } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '../../../..')
|
||||
const read = (path: string) => readFileSync(join(ROOT, path), 'utf8')
|
||||
|
||||
/** Every `.ts` / `.tsx` file under `dir`, relative to the repository root. */
|
||||
function sources(dir: string): string[] {
|
||||
const out: string[] = []
|
||||
const walk = (at: string) => {
|
||||
for (const entry of readdirSync(join(ROOT, at))) {
|
||||
const path = `${at}/${entry}`
|
||||
if (statSync(join(ROOT, path)).isDirectory()) walk(path)
|
||||
else if (/\.tsx?$/.test(entry)) out.push(path)
|
||||
}
|
||||
}
|
||||
walk(dir)
|
||||
return out
|
||||
}
|
||||
|
||||
const importsOf = (text: string): string[] =>
|
||||
[...text.matchAll(/from\s+'([^']+)'/g)].map((m) => m[1]!)
|
||||
|
||||
describe('the TypeScript PDF engine is gone', () => {
|
||||
it('packages/pdf and the TS CLI no longer exist', () => {
|
||||
// The folder itself may linger with a git-ignored node_modules of a stale install.
|
||||
expect(existsSync(join(ROOT, 'packages/pdf/package.json'))).toBe(false)
|
||||
expect(existsSync(join(ROOT, 'packages/pdf/src'))).toBe(false)
|
||||
expect(existsSync(join(ROOT, 'apps/app/scripts'))).toBe(false)
|
||||
expect(existsSync(join(ROOT, 'apps/app/src/pdf/import.ts'))).toBe(false)
|
||||
})
|
||||
|
||||
it('no application or glue source imports the engine, pdf-lib, the TS domain or xmldom', () => {
|
||||
const banned = [
|
||||
'@faunosteo/pdf',
|
||||
'pdf-lib',
|
||||
'@faunosteo/domain',
|
||||
'@xmldom/xmldom',
|
||||
]
|
||||
for (const file of [
|
||||
...sources('apps/app/src'),
|
||||
...sources('packages/core-wasm/src'),
|
||||
]) {
|
||||
const found = importsOf(read(file)).filter((spec) =>
|
||||
banned.some((b) => spec === b || spec.startsWith(`${b}/`)),
|
||||
)
|
||||
expect(found, file).toEqual([])
|
||||
}
|
||||
})
|
||||
|
||||
it('the application and the glue package depend on none of them', () => {
|
||||
for (const path of [
|
||||
'apps/app/package.json',
|
||||
'packages/core-wasm/package.json',
|
||||
]) {
|
||||
const pkg = JSON.parse(read(path)) as Record<
|
||||
string,
|
||||
Record<string, string>
|
||||
>
|
||||
const names = Object.keys({ ...pkg.dependencies, ...pkg.devDependencies })
|
||||
for (const banned of [
|
||||
'@faunosteo/pdf',
|
||||
'pdf-lib',
|
||||
'@faunosteo/domain',
|
||||
'tsx',
|
||||
]) {
|
||||
expect(names, path).not.toContain(banned)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it('the lockfile holds no pdf-lib, @pdf-lib or resvg; xmldom only under packages/anatomy', () => {
|
||||
const lock = read('pnpm-lock.yaml')
|
||||
for (const name of ['pdf-lib@', '@pdf-lib/', '@resvg/resvg-js']) {
|
||||
expect(lock, name).not.toContain(name)
|
||||
}
|
||||
const importers = lock.split(/^ (?=[a-z@][^\n]*:$)/m)
|
||||
const withXmldom = importers
|
||||
.filter((block) => block.includes("'@xmldom/xmldom':"))
|
||||
.map((block) => block.split(':')[0]!.trim())
|
||||
expect(withXmldom).toEqual(['packages/anatomy'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('the PDF path runs on the core', () => {
|
||||
it('the export drives the core session', () => {
|
||||
const text = read('apps/app/src/pdf/export.ts')
|
||||
expect(text).toContain("from '@faunosteo/core-wasm'")
|
||||
expect(text).toContain('openExportSession(')
|
||||
})
|
||||
|
||||
it('the import goes through the worker, never a TypeScript scanner', () => {
|
||||
const route = read('apps/app/src/app/routes/HomeRoute.tsx')
|
||||
expect(route).toMatch(/from '@\/core\/worker\//)
|
||||
const handler = read('apps/app/src/core/worker/handler.ts')
|
||||
expect(handler).toContain('import_pdf_bytes(')
|
||||
for (const file of sources('apps/app/src')) {
|
||||
expect(read(file), file).not.toMatch(
|
||||
/readAttachedDocument|readAttachmentBytes/,
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it('pnpm report:compare runs the core binary', () => {
|
||||
const root = JSON.parse(read('package.json')) as {
|
||||
scripts: Record<string, string>
|
||||
}
|
||||
expect(root.scripts['report:compare']).toBe(
|
||||
'bash scripts/report-compare.sh',
|
||||
)
|
||||
const script = read('scripts/report-compare.sh')
|
||||
expect(script).toContain('--features cli --bin report-compare')
|
||||
expect(
|
||||
existsSync(join(ROOT, 'crates/core/src/bin/report_compare.rs')),
|
||||
).toBe(true)
|
||||
})
|
||||
|
||||
it('the frozen N-1 readers and the oracle files stay', () => {
|
||||
for (const path of [
|
||||
'packages/core-wasm/tests/n-1/scanner/attachment/scan.ts',
|
||||
'packages/core-wasm/tests/n-1/domain',
|
||||
'crates/core/tests/oracles/r4/compare/compare.json',
|
||||
'crates/core/tests/oracles/r3/highlight.json',
|
||||
'apps/app/tests/fixtures/goldens/step6',
|
||||
'apps/app/tests/fixtures/goldens/r4',
|
||||
'apps/app/tests/fixtures/pdf/r3',
|
||||
'apps/app/tests/fixtures/pdf/step6',
|
||||
'apps/app/tests/fixtures/pdf/r4/read',
|
||||
'apps/app/tests/fixtures/pdf/r4/tolerance',
|
||||
]) {
|
||||
expect(existsSync(join(ROOT, path)), path).toBe(true)
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -1,774 +0,0 @@
|
||||
/**
|
||||
* R4 freeze O10 (plan 2026-10-17 §B.5.1, §B.1.7): what `pnpm report:compare`
|
||||
* prints and answers today, for the Rust binary of R4.7 to reproduce. The TS
|
||||
* CLI (scripts/compare-reports.ts) runs as pnpm runs it: tsx from apps/app,
|
||||
* INIT_CWD at the repository root, arguments typed from there. Invocations:
|
||||
* every fixture PDF (top level, pdf/r3, pdf/step6, pdf/r4/tolerance and
|
||||
* pdf/r4/read), one folder, the argument errors, and the 11 scenarios of
|
||||
* scripts.compare.test.ts, their documents attached to PDFs under
|
||||
* crates/core/tests/oracles/r4/compare/scenarios/. Absolute paths are made
|
||||
* repo-relative. Rows whose output the plan §A.3 declares as changing in R4
|
||||
* carry `r4Declared`. `MAKE_R4_ORACLES=1` writes the scenario PDFs and
|
||||
* compare.json; never again without the owner.
|
||||
*/
|
||||
import { spawn } from 'node:child_process'
|
||||
import { readdirSync, readFileSync, statSync } from 'node:fs'
|
||||
import { availableParallelism } from 'node:os'
|
||||
import { basename, dirname, resolve } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { AFRelationship, PDFDocument } from 'pdf-lib'
|
||||
import { beforeAll, describe, expect, it } from 'vitest'
|
||||
import { readReportDocument, type ReportV2 } from '@faunosteo/domain'
|
||||
import {
|
||||
PDF_KEYWORD,
|
||||
readAttachedDocument,
|
||||
readAttachmentBytes,
|
||||
} from '@faunosteo/pdf'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import {
|
||||
compareSheetIndex,
|
||||
type CompareSheetIndex,
|
||||
} from '../../scripts/compare/sheet'
|
||||
import {
|
||||
archiveEnvelope,
|
||||
bundledSheet,
|
||||
loadTestSheet,
|
||||
minimalReport,
|
||||
} from '../helpers/sheet'
|
||||
import {
|
||||
checkBytesOracle,
|
||||
checkJsonOracle,
|
||||
MAKE_R4_ORACLES,
|
||||
sha256,
|
||||
} from '../helpers/r4-draw'
|
||||
|
||||
const HERE = dirname(fileURLToPath(import.meta.url))
|
||||
const ROOT = resolve(HERE, '../../../..')
|
||||
const APP_DIR = resolve(ROOT, 'apps/app')
|
||||
const TSX_CLI = resolve(APP_DIR, 'node_modules/tsx/dist/cli.mjs')
|
||||
const CLI_SCRIPT = 'scripts/compare-reports.ts'
|
||||
const FIXTURES = 'apps/app/tests/fixtures'
|
||||
const ORACLE_DIR = 'crates/core/tests/oracles/r4/compare'
|
||||
const SCENARIO_DIR = `${ORACLE_DIR}/scenarios`
|
||||
const OUT = resolve(ROOT, ORACLE_DIR, 'compare.json')
|
||||
const V2_FULL_PDF = `${FIXTURES}/report-v2.full.pdf`
|
||||
const STEP6_FOLDER = `${FIXTURES}/pdf/step6/`
|
||||
const CONCURRENCY = Math.max(1, Math.min(8, availableParallelism()))
|
||||
const RUN_TIMEOUT = 300_000
|
||||
|
||||
/** Every date of the scenario PDFs: they are byte-stable. */
|
||||
const FIXED_DATE = new Date('2026-10-18T09:00:00.000Z')
|
||||
const A4: [number, number] = [595.28, 841.89]
|
||||
const ESC = '\u001b[2K'
|
||||
|
||||
const DECLARED = {
|
||||
zod: 'A.3: the TS prints a ZodError message',
|
||||
infinity: 'A.3: the TS reads a version as Infinity',
|
||||
surrogate: 'A.3: the TS keeps a lone surrogate',
|
||||
overwritten:
|
||||
'A.3 (OD-4): the TS reads a lone surrogate held by an overwritten ' +
|
||||
'duplicate',
|
||||
} as const
|
||||
|
||||
type Group = 'fixture' | 'folder' | 'cli' | 'scenario'
|
||||
|
||||
interface Invocation {
|
||||
group: Group
|
||||
/** The scripts.compare.test.ts case, `describe > it`. */
|
||||
scenario?: string
|
||||
args: string[]
|
||||
}
|
||||
|
||||
interface CliResult {
|
||||
exitCode: number
|
||||
stdout: string[]
|
||||
stderr: string[]
|
||||
}
|
||||
|
||||
interface Scenario {
|
||||
title: string
|
||||
/** Scenario PDF name → the document attached to it. */
|
||||
documents: Record<string, unknown>
|
||||
runs: string[][]
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// The 11 scenarios of scripts.compare.test.ts, inputs reproduced as written
|
||||
|
||||
const scenarioPdf = (name: string) => `${SCENARIO_DIR}/${name}.pdf`
|
||||
|
||||
function minimalV2(): ReportV2 {
|
||||
const path = resolve(ROOT, FIXTURES, 'report-v2.minimal.json')
|
||||
return JSON.parse(readFileSync(path, 'utf8')) as ReportV2
|
||||
}
|
||||
|
||||
function frozenV2Scenario(): Scenario {
|
||||
return {
|
||||
title:
|
||||
'compareReport on the frozen v2 archive PDF > shows every legacy zone ' +
|
||||
'and finding next to its canonical target',
|
||||
documents: {},
|
||||
runs: [[V2_FULL_PDF], ['--summary', V2_FULL_PDF], ['--full', V2_FULL_PDF]],
|
||||
}
|
||||
}
|
||||
|
||||
function unresolvedV2Scenario(): Scenario {
|
||||
const v2: ReportV2 = {
|
||||
...minimalV2(),
|
||||
observations: [
|
||||
{
|
||||
id: 'o',
|
||||
zones: [{ boneId: 'point-gone' }],
|
||||
findings: [
|
||||
{
|
||||
zoneId: 'ssb',
|
||||
path: ['ssb-dysfunctions', 'sutures', 'sphenozygomatic'],
|
||||
labelFrSnapshot: 'Suture sphéno-zygomatique',
|
||||
labelEnSnapshot: 'Sphenozygomatic suture',
|
||||
requiresVetReferral: true,
|
||||
},
|
||||
{
|
||||
zoneId: 'nowhere',
|
||||
path: ['x'],
|
||||
labelFrSnapshot: 'X',
|
||||
labelEnSnapshot: 'X',
|
||||
},
|
||||
],
|
||||
freeTextFindings: [],
|
||||
comment: '',
|
||||
},
|
||||
],
|
||||
}
|
||||
return {
|
||||
title:
|
||||
'compareReport on unresolved items > names a removed finding, a ' +
|
||||
'finding unknown to the catalog and an unknown zone',
|
||||
documents: { 'v2-unresolved-items': v2 },
|
||||
runs: [[scenarioPdf('v2-unresolved-items')]],
|
||||
}
|
||||
}
|
||||
|
||||
function v3AsIsScenario(sheet: SheetIndex): Scenario {
|
||||
const report = minimalReport(sheet, {
|
||||
observations: [
|
||||
{
|
||||
id: 'o3',
|
||||
zones: [{ annotationId: 'hip', viewId: 'main', side: 'left' }],
|
||||
findings: [
|
||||
{
|
||||
findingZoneId: 'hip',
|
||||
path: ['hip-dysfunctions', 'articular', 'restriction-flexion'],
|
||||
labelFrSnapshot: 'Restriction en flexion',
|
||||
labelEnSnapshot: 'Flexion restriction',
|
||||
},
|
||||
{
|
||||
findingZoneId: 'atm',
|
||||
path: ['atm-observations', 'headshaking'],
|
||||
labelFrSnapshot: 'Headshaking (secouement de tête)',
|
||||
labelEnSnapshot: 'Headshaking',
|
||||
unresolved: true,
|
||||
},
|
||||
{
|
||||
findingZoneId: 'hip',
|
||||
path: ['hip-dysfunctions'],
|
||||
labelFrSnapshot: 'Catégorie',
|
||||
labelEnSnapshot: 'Category',
|
||||
},
|
||||
],
|
||||
freeTextFindings: [],
|
||||
comment: '',
|
||||
},
|
||||
],
|
||||
})
|
||||
return {
|
||||
title:
|
||||
'compareReport on unresolved items > reads a v3 report as is and ' +
|
||||
'flags what the sheet cannot place',
|
||||
documents: { 'v3-as-is': report },
|
||||
runs: [[scenarioPdf('v3-as-is')]],
|
||||
}
|
||||
}
|
||||
|
||||
function envelopeScenario(sheet: SheetIndex): Scenario {
|
||||
const envelope = archiveEnvelope(
|
||||
sheet,
|
||||
minimalReport(sheet, {
|
||||
observations: [
|
||||
{
|
||||
id: 'o-env',
|
||||
zones: [{ annotationId: 'hip', viewId: 'main', side: 'left' }],
|
||||
findings: [],
|
||||
freeTextFindings: [],
|
||||
comment: '',
|
||||
},
|
||||
],
|
||||
}),
|
||||
)
|
||||
const names = ['envelope-kept', 'envelope-version-2', 'envelope-report-only']
|
||||
return {
|
||||
title:
|
||||
'compareReport on unresolved items > prints the archive line of an ' +
|
||||
'envelope: snapshot kept, or rejected with its reason',
|
||||
documents: {
|
||||
'envelope-kept': envelope,
|
||||
'envelope-version-2': { ...envelope, envelopeVersion: 2 },
|
||||
'envelope-report-only': envelope.report,
|
||||
},
|
||||
runs: names.map((name) => [scenarioPdf(name)]),
|
||||
}
|
||||
}
|
||||
|
||||
function otherSheetScenario(sheet: SheetIndex): Scenario {
|
||||
const report = minimalReport(sheet, {
|
||||
sheet: { ...sheet.sheet.ref, documentSha256: 'b'.repeat(64) },
|
||||
})
|
||||
return {
|
||||
title:
|
||||
'compareReport on unresolved items > warns when a v3 report was ' +
|
||||
'written on another sheet document',
|
||||
documents: { 'other-sheet-document': report },
|
||||
runs: [[scenarioPdf('other-sheet-document')]],
|
||||
}
|
||||
}
|
||||
|
||||
function migratedDiffScenario(cli: CompareSheetIndex): Scenario {
|
||||
const v2 = minimalV2()
|
||||
const migrated = readReportDocument(v2, cli.sheet.migrationMap).report
|
||||
const [a, b, c] = ['diff-v2', 'diff-v2-migrated', 'diff-v2-changed']
|
||||
return {
|
||||
title:
|
||||
'diff and arguments > diffs the v3 content of a migrated v2 report ' +
|
||||
'and its v3 re-reading',
|
||||
documents: {
|
||||
[a]: v2,
|
||||
[b]: migrated,
|
||||
[c]: { ...migrated, conclusion: 'changed' },
|
||||
},
|
||||
runs: [
|
||||
['--diff', scenarioPdf(a), scenarioPdf(b)],
|
||||
['--diff', scenarioPdf(a), scenarioPdf(c)],
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
function placeholderDiffScenario(cli: CompareSheetIndex): Scenario {
|
||||
const base = readReportDocument(minimalV2(), cli.sheet.migrationMap)
|
||||
const [original, edited] = ['diff-original', 'diff-edited']
|
||||
return {
|
||||
title:
|
||||
'diff and arguments > compares placeholders by default and real ' +
|
||||
'texts with --full',
|
||||
documents: {
|
||||
[original]: { ...base.report, conclusion: 'original' },
|
||||
[edited]: { ...base.report, conclusion: 'changed' },
|
||||
},
|
||||
runs: [
|
||||
['--diff', scenarioPdf(original), scenarioPdf(edited)],
|
||||
['--full', '--diff', scenarioPdf(original), scenarioPdf(edited)],
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
function argumentsScenario(): Scenario {
|
||||
return {
|
||||
title:
|
||||
'diff and arguments > parses files and options, refuses unknown ones',
|
||||
documents: {},
|
||||
runs: [
|
||||
['a.pdf', '--summary', 'dir', '--locale', 'en'],
|
||||
['--full', '--diff', 'a', 'b'],
|
||||
['--identity'],
|
||||
['--locale', 'de'],
|
||||
['--json'],
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
function controlCharactersScenario(sheet: SheetIndex): Scenario {
|
||||
const report = minimalReport(sheet, {
|
||||
observations: [
|
||||
{
|
||||
id: `o${ESC}\n1`,
|
||||
zones: [],
|
||||
findings: [
|
||||
{
|
||||
findingZoneId: 'hip',
|
||||
path: ['hip-dysfunctions', `x${ESC}\ny`],
|
||||
labelFrSnapshot: `L${ESC}`,
|
||||
labelEnSnapshot: 'L',
|
||||
unresolved: true,
|
||||
},
|
||||
],
|
||||
freeTextFindings: [`t${ESC}\nu`],
|
||||
comment: `c${ESC}\nd\u009b
`,
|
||||
},
|
||||
],
|
||||
})
|
||||
return {
|
||||
title:
|
||||
'terminal safety and privacy > escapes control characters in every ' +
|
||||
'untrusted string',
|
||||
documents: { 'control-characters': report },
|
||||
runs: [['--full', scenarioPdf('control-characters')]],
|
||||
}
|
||||
}
|
||||
|
||||
function prototypeIdsScenario(): Scenario {
|
||||
const v2: ReportV2 = {
|
||||
...minimalV2(),
|
||||
observations: [
|
||||
{
|
||||
id: 'o',
|
||||
zones: [{ boneId: '__proto__' }, { boneId: 'constructor' }],
|
||||
findings: [
|
||||
{
|
||||
zoneId: '__proto__',
|
||||
path: ['constructor'],
|
||||
labelFrSnapshot: 'X',
|
||||
labelEnSnapshot: 'X',
|
||||
},
|
||||
],
|
||||
freeTextFindings: [],
|
||||
comment: '',
|
||||
},
|
||||
],
|
||||
}
|
||||
return {
|
||||
title:
|
||||
'terminal safety and privacy > does not throw on prototype-chain ' +
|
||||
'names used as legacy ids',
|
||||
documents: { 'prototype-ids': v2 },
|
||||
runs: [[scenarioPdf('prototype-ids')]],
|
||||
}
|
||||
}
|
||||
|
||||
function privacyScenario(sheet: SheetIndex): Scenario {
|
||||
const report = minimalReport(sheet, {
|
||||
conclusion: 'SECRET-CONCLUSION',
|
||||
recommendations: 'SECRET-RECO',
|
||||
nextAppointment: { kind: 'period', text: 'SECRET-WHEN' },
|
||||
observations: [
|
||||
{
|
||||
id: 'o',
|
||||
zones: [],
|
||||
findings: [],
|
||||
freeTextFindings: ['SECRET-TEXT'],
|
||||
comment: 'SECRET-COMMENT',
|
||||
},
|
||||
],
|
||||
})
|
||||
const pdf = scenarioPdf('hidden-texts')
|
||||
return {
|
||||
title:
|
||||
'terminal safety and privacy > hides names and free texts by default, ' +
|
||||
'shows them with --full',
|
||||
documents: { 'hidden-texts': report },
|
||||
runs: [[pdf], ['--full', pdf]],
|
||||
}
|
||||
}
|
||||
|
||||
function buildScenarios(sheet: SheetIndex, cli: CompareSheetIndex) {
|
||||
return [
|
||||
frozenV2Scenario(),
|
||||
unresolvedV2Scenario(),
|
||||
v3AsIsScenario(sheet),
|
||||
envelopeScenario(sheet),
|
||||
otherSheetScenario(sheet),
|
||||
migratedDiffScenario(cli),
|
||||
placeholderDiffScenario(cli),
|
||||
argumentsScenario(),
|
||||
controlCharactersScenario(sheet),
|
||||
prototypeIdsScenario(),
|
||||
privacyScenario(sheet),
|
||||
]
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Invocation plan
|
||||
|
||||
/** PDFs of a folder, repo-relative and sorted; `deep` walks sub-folders. */
|
||||
function pdfsUnder(folder: string, deep: boolean): string[] {
|
||||
return readdirSync(resolve(ROOT, folder), { withFileTypes: true })
|
||||
.flatMap((entry) => {
|
||||
const path = `${folder}/${entry.name}`
|
||||
if (entry.isDirectory()) return deep ? pdfsUnder(path, true) : []
|
||||
return entry.name.toLowerCase().endsWith('.pdf') ? [path] : []
|
||||
})
|
||||
.sort()
|
||||
}
|
||||
|
||||
function fixturePdfs(): string[] {
|
||||
return [
|
||||
...pdfsUnder(FIXTURES, false),
|
||||
...pdfsUnder(`${FIXTURES}/pdf/r3`, true),
|
||||
...pdfsUnder(`${FIXTURES}/pdf/step6`, false),
|
||||
...pdfsUnder(`${FIXTURES}/pdf/r4/tolerance`, false),
|
||||
...pdfsUnder(`${FIXTURES}/pdf/r4/read`, false),
|
||||
]
|
||||
}
|
||||
|
||||
function invocationPlan(scenarios: readonly Scenario[]): Invocation[] {
|
||||
const of = (group: Group, runs: string[][]) =>
|
||||
runs.map((args): Invocation => ({ group, args }))
|
||||
return [
|
||||
...of(
|
||||
'fixture',
|
||||
fixturePdfs().map((pdf) => [pdf]),
|
||||
),
|
||||
...of('folder', [
|
||||
[STEP6_FOLDER],
|
||||
['--full', '--locale', 'en', STEP6_FOLDER],
|
||||
]),
|
||||
...of('cli', [
|
||||
[],
|
||||
['--diff', V2_FULL_PDF],
|
||||
['--locale'],
|
||||
[V2_FULL_PDF, '--summary', STEP6_FOLDER, '--locale', 'en'],
|
||||
]),
|
||||
...scenarios.flatMap(({ title, runs }) =>
|
||||
runs.map(
|
||||
(args): Invocation => ({ group: 'scenario', scenario: title, args }),
|
||||
),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
/** The files the CLI reads for `args`: options skipped, folders listed. */
|
||||
function inputFiles(args: readonly string[]): string[] {
|
||||
const files: string[] = []
|
||||
for (let i = 0; i < args.length; i += 1) {
|
||||
const arg = args[i]!
|
||||
if (arg === '--locale') i += 1
|
||||
else if (!arg.startsWith('--')) files.push(...expandArg(arg))
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
function expandArg(arg: string): string[] {
|
||||
try {
|
||||
if (!statSync(resolve(ROOT, arg)).isDirectory()) return [arg]
|
||||
} catch {
|
||||
return [arg]
|
||||
}
|
||||
return pdfsUnder(arg.replace(/\/$/, ''), false)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Running the CLI
|
||||
|
||||
function cliEnv(): NodeJS.ProcessEnv {
|
||||
const env: NodeJS.ProcessEnv = { INIT_CWD: ROOT }
|
||||
for (const key of ['PATH', 'HOME', 'TMPDIR'] as const) {
|
||||
if (process.env[key] !== undefined) env[key] = process.env[key]
|
||||
}
|
||||
return env
|
||||
}
|
||||
|
||||
/** The exact text split on "\n", the repository root cut from paths. */
|
||||
function outputLines(chunks: readonly Buffer[]): string[] {
|
||||
const text = Buffer.concat(chunks).toString('utf8')
|
||||
return text.split(`${ROOT}/`).join('').split(ROOT).join('.').split('\n')
|
||||
}
|
||||
|
||||
function runCli(args: readonly string[]): Promise<CliResult> {
|
||||
return new Promise((settle, fail) => {
|
||||
const child = spawn(process.execPath, [TSX_CLI, CLI_SCRIPT, ...args], {
|
||||
cwd: APP_DIR,
|
||||
env: cliEnv(),
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
})
|
||||
const stdout: Buffer[] = []
|
||||
const stderr: Buffer[] = []
|
||||
child.stdout.on('data', (chunk: Buffer) => stdout.push(chunk))
|
||||
child.stderr.on('data', (chunk: Buffer) => stderr.push(chunk))
|
||||
child.on('error', fail)
|
||||
child.on('close', (code, signal) => {
|
||||
if (code === null) fail(new Error(`CLI stopped by ${signal}`))
|
||||
else
|
||||
settle({
|
||||
exitCode: code,
|
||||
stdout: outputLines(stdout),
|
||||
stderr: outputLines(stderr),
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/** `run` over `items`, at most `limit` at a time, results in item order. */
|
||||
async function inPool<T, R>(
|
||||
items: readonly T[],
|
||||
limit: number,
|
||||
run: (item: T) => Promise<R>,
|
||||
): Promise<R[]> {
|
||||
const results = new Array<R>(items.length)
|
||||
let next = 0
|
||||
const lane = async () => {
|
||||
while (next < items.length) {
|
||||
const at = next++
|
||||
results[at] = await run(items[at]!)
|
||||
}
|
||||
}
|
||||
await Promise.all(Array.from({ length: limit }, lane))
|
||||
return results
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// The A.3 declarations, from the outputs and the attachment each file holds
|
||||
|
||||
interface AttachmentFacts {
|
||||
/** A key or string of the parsed attachment holds a lone surrogate. */
|
||||
parsedSurrogate: boolean
|
||||
/** A string literal of the attachment text holds one (overwritten too). */
|
||||
textSurrogate: boolean
|
||||
/** `version`, `envelopeVersion` or `report.version` parsed as ±Infinity. */
|
||||
infiniteVersion: boolean
|
||||
}
|
||||
|
||||
const NO_FACTS: AttachmentFacts = {
|
||||
parsedSurrogate: false,
|
||||
textSurrogate: false,
|
||||
infiniteVersion: false,
|
||||
}
|
||||
const LONE_SURROGATE = /\p{Cs}/u
|
||||
const STRING_LITERAL = /"(?:[^"\\]|\\.)*"/g
|
||||
|
||||
function holdsLoneSurrogate(value: unknown): boolean {
|
||||
if (typeof value === 'string') return LONE_SURROGATE.test(value)
|
||||
if (typeof value !== 'object' || value === null) return false
|
||||
return Object.entries(value).some(
|
||||
([key, item]) => LONE_SURROGATE.test(key) || holdsLoneSurrogate(item),
|
||||
)
|
||||
}
|
||||
|
||||
function decodesToLoneSurrogate(literal: string): boolean {
|
||||
try {
|
||||
return LONE_SURROGATE.test(JSON.parse(literal) as string)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
function literalHoldsLoneSurrogate(text: string): boolean {
|
||||
for (const [literal] of text.matchAll(STRING_LITERAL)) {
|
||||
if (decodesToLoneSurrogate(literal)) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
const versionOf = (value: unknown): unknown =>
|
||||
typeof value === 'object' && value !== null
|
||||
? (value as { version?: unknown }).version
|
||||
: undefined
|
||||
|
||||
function hasInfiniteVersion(raw: unknown): boolean {
|
||||
if (typeof raw !== 'object' || raw === null) return false
|
||||
const doc = raw as { envelopeVersion?: unknown; report?: unknown }
|
||||
return [versionOf(raw), doc.envelopeVersion, versionOf(doc.report)].some(
|
||||
(v) => typeof v === 'number' && !Number.isFinite(v),
|
||||
)
|
||||
}
|
||||
|
||||
async function settled<T>(promise: Promise<T>): Promise<T | undefined> {
|
||||
try {
|
||||
return await promise
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
async function attachmentFacts(file: string): Promise<AttachmentFacts> {
|
||||
let bytes: Uint8Array
|
||||
try {
|
||||
bytes = new Uint8Array(readFileSync(resolve(ROOT, file)))
|
||||
} catch {
|
||||
return NO_FACTS
|
||||
}
|
||||
const raw = await settled(readAttachedDocument(bytes))
|
||||
const text = await settled(
|
||||
readAttachmentBytes(bytes).then((b) => new TextDecoder().decode(b)),
|
||||
)
|
||||
return {
|
||||
parsedSurrogate: raw !== undefined && holdsLoneSurrogate(raw),
|
||||
textSurrogate: text !== undefined && literalHoldsLoneSurrogate(text),
|
||||
infiniteVersion: raw !== undefined && hasInfiniteVersion(raw),
|
||||
}
|
||||
}
|
||||
|
||||
/** A failure line whose reason is a ZodError message (issues as JSON). */
|
||||
function isZodFailure(line: string): boolean {
|
||||
const at = line.indexOf(': [')
|
||||
if (!line.startsWith('!! ') || at < 0) return false
|
||||
const reason = line
|
||||
.slice(at + 2)
|
||||
.replace(/\\u([0-9a-f]{4})/g, (_, hex: string) =>
|
||||
String.fromCharCode(parseInt(hex, 16)),
|
||||
)
|
||||
try {
|
||||
const issues: unknown = JSON.parse(reason)
|
||||
return (
|
||||
Array.isArray(issues) &&
|
||||
issues.length > 0 &&
|
||||
issues.every(
|
||||
(i) => typeof (i as { code?: unknown } | null)?.code === 'string',
|
||||
)
|
||||
)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
function declaredChange(
|
||||
files: readonly string[],
|
||||
stdout: readonly string[],
|
||||
facts: ReadonlyMap<string, AttachmentFacts>,
|
||||
): string | undefined {
|
||||
const reasons = new Set<string>()
|
||||
if (stdout.some(isZodFailure)) reasons.add(DECLARED.zod)
|
||||
for (const file of files) {
|
||||
const fact = facts.get(file) ?? NO_FACTS
|
||||
if (fact.infiniteVersion) reasons.add(DECLARED.infinity)
|
||||
if (stdout.some((l) => l.startsWith(`!! ${basename(file)}: `))) continue
|
||||
if (fact.parsedSurrogate) reasons.add(DECLARED.surrogate)
|
||||
else if (fact.textSurrogate) reasons.add(DECLARED.overwritten)
|
||||
}
|
||||
return reasons.size ? [...reasons].join('; ') : undefined
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Oracle
|
||||
|
||||
/** Shaped like an archive export, as r4.tolerance-oracles builds its corpus. */
|
||||
async function buildPdf(document: unknown): Promise<Uint8Array> {
|
||||
const doc = await PDFDocument.create()
|
||||
doc.addPage(A4)
|
||||
doc.setKeywords([PDF_KEYWORD])
|
||||
doc.setCreationDate(FIXED_DATE)
|
||||
doc.setModificationDate(FIXED_DATE)
|
||||
const bytes = new TextEncoder().encode(JSON.stringify(document))
|
||||
await doc.attach(bytes, 'osteo-report.json', {
|
||||
mimeType: 'application/json',
|
||||
description: 'Faunosteo report archive v1',
|
||||
creationDate: FIXED_DATE,
|
||||
modificationDate: FIXED_DATE,
|
||||
afRelationship: AFRelationship.Data,
|
||||
})
|
||||
return doc.save({ useObjectStreams: false })
|
||||
}
|
||||
|
||||
async function factsOf(files: readonly string[]) {
|
||||
const facts = new Map<string, AttachmentFacts>()
|
||||
for (const file of files) facts.set(file, await attachmentFacts(file))
|
||||
return facts
|
||||
}
|
||||
|
||||
/** sha256 of every input file that exists (argument errors name none). */
|
||||
function inputDigests(files: readonly string[]): Record<string, string> {
|
||||
const digests: Record<string, string> = {}
|
||||
for (const file of files) {
|
||||
const path = resolve(ROOT, file)
|
||||
if (statSync(path, { throwIfNoEntry: false })?.isFile()) {
|
||||
digests[file] = sha256(new Uint8Array(readFileSync(path)))
|
||||
}
|
||||
}
|
||||
return digests
|
||||
}
|
||||
|
||||
async function recordInvocations(plan: readonly Invocation[]) {
|
||||
const results = await inPool(plan, CONCURRENCY, (inv) => runCli(inv.args))
|
||||
const files = [...new Set(plan.flatMap((inv) => inputFiles(inv.args)))]
|
||||
const facts = await factsOf(files)
|
||||
const invocations = plan.map((inv, i) => {
|
||||
const { exitCode, stdout, stderr } = results[i]!
|
||||
const declared = declaredChange(inputFiles(inv.args), stdout, facts)
|
||||
return {
|
||||
...inv,
|
||||
...(declared ? { r4Declared: declared } : {}),
|
||||
exitCode,
|
||||
stdout,
|
||||
stderr,
|
||||
}
|
||||
})
|
||||
return { inputs: inputDigests(files), invocations }
|
||||
}
|
||||
|
||||
function refuseFieldCorpus(): void {
|
||||
if (process.env.FAUNOSTEO_FIELD_CORPUS) {
|
||||
throw new Error('refused: FAUNOSTEO_FIELD_CORPUS is set (plan §B.5.2)')
|
||||
}
|
||||
}
|
||||
|
||||
type OracleRow = Invocation & { r4Declared?: string; exitCode: number }
|
||||
|
||||
function committedRows(): OracleRow[] {
|
||||
return (JSON.parse(readFileSync(OUT, 'utf8')) as { invocations: OracleRow[] })
|
||||
.invocations
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('R4 O10 report:compare outputs of the TS CLI', () => {
|
||||
let cli: CompareSheetIndex
|
||||
let scenarios: Scenario[]
|
||||
|
||||
beforeAll(async () => {
|
||||
if (MAKE_R4_ORACLES) refuseFieldCorpus()
|
||||
const sheet = await loadTestSheet()
|
||||
cli = compareSheetIndex(await bundledSheet())
|
||||
scenarios = buildScenarios(sheet, cli)
|
||||
})
|
||||
|
||||
it('builds byte-stable scenario PDFs (MAKE_R4_ORACLES writes)', async () => {
|
||||
const names = scenarios.flatMap((s) => Object.keys(s.documents))
|
||||
expect(new Set(names).size).toBe(names.length)
|
||||
for (const scenario of scenarios) {
|
||||
for (const [name, document] of Object.entries(scenario.documents)) {
|
||||
const path = resolve(ROOT, scenarioPdf(name))
|
||||
checkBytesOracle(path, await buildPdf(document))
|
||||
}
|
||||
}
|
||||
const listed = readdirSync(resolve(ROOT, SCENARIO_DIR)).sort()
|
||||
expect(listed).toEqual(names.map((n) => `${n}.pdf`).sort())
|
||||
})
|
||||
|
||||
it(
|
||||
'records every invocation (written under MAKE_R4_ORACLES)',
|
||||
async () => {
|
||||
const { inputs, invocations } = await recordInvocations(
|
||||
invocationPlan(scenarios),
|
||||
)
|
||||
checkJsonOracle(OUT, {
|
||||
oracle: 'O10',
|
||||
cli: `apps/app/${CLI_SCRIPT}`,
|
||||
run:
|
||||
'node apps/app/node_modules/tsx/dist/cli.mjs ' +
|
||||
`${CLI_SCRIPT} <args>, cwd apps/app, INIT_CWD at the repository ` +
|
||||
'root (pnpm report:compare typed from the root); arguments and ' +
|
||||
'printed paths are repo-relative',
|
||||
text:
|
||||
'stdout and stderr are split on "\\n": joined with "\\n" they ' +
|
||||
'give the exact text',
|
||||
sheet: cli.sheet.ref,
|
||||
inputs,
|
||||
invocations,
|
||||
})
|
||||
},
|
||||
RUN_TIMEOUT,
|
||||
)
|
||||
|
||||
it('covers the 11 scenarios and marks every A.3 class at least once', () => {
|
||||
const rows = committedRows()
|
||||
const titles = new Set(rows.flatMap((r) => r.scenario ?? []))
|
||||
expect(titles.size).toBe(11)
|
||||
for (const reason of Object.values(DECLARED)) {
|
||||
expect(
|
||||
rows.some((r) => r.r4Declared?.includes(reason)),
|
||||
reason,
|
||||
).toBe(true)
|
||||
}
|
||||
const control = rows.find(
|
||||
(r) => r.group === 'fixture' && r.args[0] === V2_FULL_PDF,
|
||||
)
|
||||
expect(control).toMatchObject({ exitCode: 0 })
|
||||
expect(control).not.toHaveProperty('r4Declared')
|
||||
})
|
||||
})
|
||||
@@ -28,7 +28,7 @@ type SizeName = keyof typeof SIZES
|
||||
const NAMES = Object.keys(SIZES) as SizeName[]
|
||||
const EXPECTED_VIEWS: Record<SizeName, number> = { L: 120, CAP: 1000 }
|
||||
|
||||
/** `PAGE_CAPS` of packages/pdf/src/export.ts at the freeze: the engine does not export it. */
|
||||
/** `PAGE_CAPS` of the engine at the freeze (now `crates/core/src/pdf/write/input.rs`). */
|
||||
const PAGE_CAPS = { observations: 500, viewsPerObservation: 8 } as const
|
||||
|
||||
/** The zones a demo observation holds on one view, with the findings of their finding zones. */
|
||||
@@ -96,7 +96,7 @@ function syntheticReport(
|
||||
}
|
||||
}
|
||||
|
||||
/** Per observation, the views the engine rasterises (packages/pdf observationViewIds). */
|
||||
/** Per observation, the views the engine rasterises (`observationViewIds` at the freeze; the core's `views.rs`). */
|
||||
function rasterisedViews(report: Report, snapshotViews: Set<string>): number[] {
|
||||
return report.observations.map(
|
||||
(o) =>
|
||||
|
||||
@@ -1,776 +0,0 @@
|
||||
/**
|
||||
* R4 freeze O8 (plan 2026-10-17 §B.5.1): the differential tolerance corpus.
|
||||
* Each case is a PDF shaped like a Faunosteo export (one A4 page, the
|
||||
* archive keyword, fixed dates, no object streams) whose attachments are
|
||||
* byte- or text-level mutations of a real envelope or of the bare v3 demo
|
||||
* report. The outcome is the full R3 import path as HomeRoute runs it for a
|
||||
* PDF: `readAttachment` (TS byte scan, TextDecoder, JSON.parse) then
|
||||
* `importAttachment` (JSON.stringify, core `archive.import` in wasm) on the
|
||||
* bundled draft sheet. The UTF-8 validity and JSON.parse columns, and the
|
||||
* candidate the scanner chose, serve the Rust differential test (R4.2).
|
||||
* `MAKE_R4_ORACLES=1` writes `apps/app/tests/fixtures/pdf/r4/tolerance/` and
|
||||
* `crates/core/tests/oracles/r4/tolerance.json`; never without the owner.
|
||||
*/
|
||||
import { createHash } from 'node:crypto'
|
||||
import { mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'
|
||||
import { dirname, resolve } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { AFRelationship, PDFDocument } from 'pdf-lib'
|
||||
import { beforeAll, describe, expect, it } from 'vitest'
|
||||
import type { ArchiveImport } from '@faunosteo/core-wasm/types'
|
||||
import {
|
||||
ArchiveReadError,
|
||||
PDF_KEYWORD,
|
||||
readAttachmentBytes,
|
||||
} from '@faunosteo/pdf'
|
||||
import { importAttachment, ImportError, readAttachment } from '@/pdf/import'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { loadTestSheet } from '../helpers/sheet'
|
||||
|
||||
const HERE = dirname(fileURLToPath(import.meta.url))
|
||||
const ROOT = resolve(HERE, '../../../..')
|
||||
const PDF_DIR_REL = 'apps/app/tests/fixtures/pdf/r4/tolerance'
|
||||
const PDF_DIR = resolve(ROOT, PDF_DIR_REL)
|
||||
const OUT = resolve(ROOT, 'crates/core/tests/oracles/r4/tolerance.json')
|
||||
const MAKE = process.env.MAKE_R4_ORACLES === '1'
|
||||
|
||||
const BASE_FILES = {
|
||||
'report-v3.demo.json': 'apps/app/tests/fixtures/report-v3.demo.json',
|
||||
'envelope.demo.fr.nologos.json':
|
||||
'apps/app/tests/fixtures/goldens/r2/envelope.demo.fr.nologos.json',
|
||||
} as const
|
||||
type BaseName = keyof typeof BASE_FILES | 'none'
|
||||
type Bases = Record<keyof typeof BASE_FILES, string>
|
||||
|
||||
/** Every date of the PDF and its attachments: the corpus is byte-stable. */
|
||||
const FIXED_DATE = new Date('2026-10-18T09:00:00.000Z')
|
||||
const A4: [number, number] = [595.28, 841.89]
|
||||
const ATTACHMENT = 'osteo-report.json'
|
||||
const DECOY = 'osteo-report-decoy.json'
|
||||
|
||||
interface AttachmentSpec {
|
||||
name: string
|
||||
bytes: Uint8Array
|
||||
}
|
||||
|
||||
interface CaseSpec {
|
||||
id: string
|
||||
description: string
|
||||
base: BaseName
|
||||
attachments: AttachmentSpec[]
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Text and byte mutations (the base text is never re-serialised)
|
||||
|
||||
const utf8 = (text: string): Uint8Array => new TextEncoder().encode(text)
|
||||
|
||||
const sha256 = (bytes: Uint8Array | string): string =>
|
||||
createHash('sha256').update(bytes).digest('hex')
|
||||
|
||||
function concatBytes(parts: readonly Uint8Array[]): Uint8Array {
|
||||
const out = new Uint8Array(parts.reduce((n, p) => n + p.byteLength, 0))
|
||||
let at = 0
|
||||
for (const part of parts) {
|
||||
out.set(part, at)
|
||||
at += part.byteLength
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
function anchorEnd(text: string, anchor: string): number {
|
||||
const at = text.indexOf(anchor)
|
||||
if (at < 0) throw new Error(`anchor not found: ${anchor}`)
|
||||
return at + anchor.length
|
||||
}
|
||||
|
||||
/** The first occurrence of `from` replaced; a missing anchor is a generator bug. */
|
||||
function replaceFirst(text: string, from: string, to: string): string {
|
||||
const end = anchorEnd(text, from)
|
||||
return text.slice(0, end - from.length) + to + text.slice(end)
|
||||
}
|
||||
|
||||
/** The UTF-8 bytes with `raw` inserted right after the first `anchor`, searched as bytes. */
|
||||
function bytesAfter(
|
||||
source: Uint8Array | string,
|
||||
anchor: string,
|
||||
raw: number[],
|
||||
): Uint8Array {
|
||||
const bytes = typeof source === 'string' ? utf8(source) : source
|
||||
const needle = utf8(anchor)
|
||||
const at = Buffer.from(bytes).indexOf(needle)
|
||||
if (at < 0) throw new Error(`anchor not found: ${anchor}`)
|
||||
const end = at + needle.byteLength
|
||||
const inserted = Uint8Array.from(raw)
|
||||
return concatBytes([bytes.subarray(0, end), inserted, bytes.subarray(end)])
|
||||
}
|
||||
|
||||
/** Members written before the object's first member (they come first in JS order too). */
|
||||
function leadingMembers(text: string, members: string): string {
|
||||
if (!text.startsWith('{')) throw new Error('base is not an object')
|
||||
return `{${members},${text.slice(1)}`
|
||||
}
|
||||
|
||||
/** Members written after the object's last member. */
|
||||
function trailingMembers(text: string, members: string): string {
|
||||
const close = text.lastIndexOf('}')
|
||||
return `${text.slice(0, close)},\n ${members}\n${text.slice(close)}`
|
||||
}
|
||||
|
||||
const single = (
|
||||
id: string,
|
||||
description: string,
|
||||
base: BaseName,
|
||||
bytes: Uint8Array | string,
|
||||
): CaseSpec => ({
|
||||
id,
|
||||
description,
|
||||
base,
|
||||
attachments: [
|
||||
{
|
||||
name: ATTACHMENT,
|
||||
bytes: typeof bytes === 'string' ? utf8(bytes) : bytes,
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Cases
|
||||
|
||||
const BARE = 'report-v3.demo.json' satisfies BaseName
|
||||
const ENVELOPE = 'envelope.demo.fr.nologos.json' satisfies BaseName
|
||||
/** Inside the conclusion string of the bare report (and of the envelope's report). */
|
||||
const IN_CONCLUSION = '"conclusion": "Dysfonction'
|
||||
const CONCLUSION_END = 'associée.'
|
||||
const FIRST_INDEX = '"index": 3'
|
||||
const SECOND_INDEX = '"index": 4'
|
||||
const SNAPSHOT_CX = '"cx": 1523,'
|
||||
|
||||
function controlCases(b: Bases): CaseSpec[] {
|
||||
return [
|
||||
single(
|
||||
'control-bare-v3',
|
||||
'the bare v3 demo report, unmodified',
|
||||
BARE,
|
||||
b[BARE],
|
||||
),
|
||||
single(
|
||||
'control-envelope',
|
||||
'the R2 demo envelope, unmodified',
|
||||
ENVELOPE,
|
||||
b[ENVELOPE],
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
function utf8Cases(b: Bases): CaseSpec[] {
|
||||
const bare = b[BARE]
|
||||
return [
|
||||
single(
|
||||
'utf8-lone-continuation',
|
||||
'byte 80 inside the conclusion',
|
||||
BARE,
|
||||
bytesAfter(bare, IN_CONCLUSION, [0x80]),
|
||||
),
|
||||
single(
|
||||
'utf8-overlong',
|
||||
'overlong C0 AF and E0 80 AF inside the conclusion',
|
||||
BARE,
|
||||
bytesAfter(bare, IN_CONCLUSION, [0xc0, 0xaf, 0x20, 0xe0, 0x80, 0xaf]),
|
||||
),
|
||||
single(
|
||||
'utf8-truncated',
|
||||
'E2 82 before a space and F0 9F 98 before the closing quote',
|
||||
BARE,
|
||||
bytesAfter(
|
||||
bytesAfter(bare, CONCLUSION_END, [0xf0, 0x9f, 0x98]),
|
||||
IN_CONCLUSION,
|
||||
[0xe2, 0x82],
|
||||
),
|
||||
),
|
||||
single(
|
||||
'utf8-encoded-surrogate',
|
||||
'ED A0 80 (U+D800 encoded) inside the conclusion',
|
||||
BARE,
|
||||
bytesAfter(bare, IN_CONCLUSION, [0xed, 0xa0, 0x80]),
|
||||
),
|
||||
single(
|
||||
'utf8-above-max',
|
||||
'F4 90 80 80 (above U+10FFFF) inside the conclusion',
|
||||
BARE,
|
||||
bytesAfter(bare, IN_CONCLUSION, [0xf4, 0x90, 0x80, 0x80]),
|
||||
),
|
||||
single(
|
||||
'utf8-invalid-in-key',
|
||||
'byte FF inside the "conclusion" key',
|
||||
BARE,
|
||||
bytesAfter(bare, '"conclu', [0xff]),
|
||||
),
|
||||
single(
|
||||
'utf8-invalid-in-unknown-key',
|
||||
'byte FF inside an extra member key',
|
||||
BARE,
|
||||
bytesAfter(leadingMembers(bare, '"extra": true'), '"ext', [0xff]),
|
||||
),
|
||||
single(
|
||||
'utf8-invalid-outside-string',
|
||||
'byte 80 between tokens after the opening brace',
|
||||
BARE,
|
||||
bytesAfter(bare, '{', [0x80]),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
function bomCases(b: Bases): CaseSpec[] {
|
||||
const bare = b[BARE]
|
||||
const bom = [0xef, 0xbb, 0xbf]
|
||||
return [
|
||||
single(
|
||||
'bom-leading',
|
||||
'one UTF-8 BOM before the text',
|
||||
BARE,
|
||||
concatBytes([Uint8Array.from(bom), utf8(bare)]),
|
||||
),
|
||||
single(
|
||||
'bom-double',
|
||||
'two UTF-8 BOMs before the text',
|
||||
BARE,
|
||||
concatBytes([Uint8Array.from([...bom, ...bom]), utf8(bare)]),
|
||||
),
|
||||
single(
|
||||
'bom-middle-whitespace',
|
||||
'a BOM between tokens after the opening brace',
|
||||
BARE,
|
||||
bytesAfter(bare, '{', bom),
|
||||
),
|
||||
single(
|
||||
'bom-in-string',
|
||||
'a BOM inside the conclusion string',
|
||||
BARE,
|
||||
bytesAfter(bare, IN_CONCLUSION, bom),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
function numberCases(b: Bases): CaseSpec[] {
|
||||
const [bare, envelope] = [b[BARE], b[ENVELOPE]]
|
||||
return [
|
||||
single(
|
||||
'overflow-version-bare',
|
||||
'bare report "version": 1e400',
|
||||
BARE,
|
||||
replaceFirst(bare, '"version": 3', '"version": 1e400'),
|
||||
),
|
||||
single(
|
||||
'overflow-envelope-version',
|
||||
'envelope "envelopeVersion": 1e400',
|
||||
ENVELOPE,
|
||||
replaceFirst(
|
||||
envelope,
|
||||
'"envelopeVersion": 1,',
|
||||
'"envelopeVersion": 1e400,',
|
||||
),
|
||||
),
|
||||
single(
|
||||
'overflow-snapshot-coordinate',
|
||||
'a snapshot circle "cx": 1e400',
|
||||
ENVELOPE,
|
||||
replaceFirst(envelope, SNAPSHOT_CX, '"cx": 1e400,'),
|
||||
),
|
||||
single(
|
||||
'overflow-duplicate-overwritten',
|
||||
'vertebra "index": 1e400 overwritten by a later "index": 3',
|
||||
BARE,
|
||||
replaceFirst(bare, FIRST_INDEX, `"index": 1e400, ${FIRST_INDEX}`),
|
||||
),
|
||||
single(
|
||||
'overflow-negative-content',
|
||||
'vertebra "index": -1e400',
|
||||
BARE,
|
||||
replaceFirst(bare, SECOND_INDEX, '"index": -1e400'),
|
||||
),
|
||||
single(
|
||||
'overflow-boundary-finite',
|
||||
'"version": 1.7976931348623158e308 (rounds to the largest finite double)',
|
||||
BARE,
|
||||
replaceFirst(bare, '"version": 3', '"version": 1.7976931348623158e308'),
|
||||
),
|
||||
single(
|
||||
'overflow-boundary-infinite',
|
||||
'"version": 1.7976931348623159e308 (past the halfway point: Infinity)',
|
||||
BARE,
|
||||
replaceFirst(bare, '"version": 3', '"version": 1.7976931348623159e308'),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
function surrogateCases(b: Bases): CaseSpec[] {
|
||||
const bare = b[BARE]
|
||||
const inConclusion = (escape: string) =>
|
||||
replaceFirst(bare, IN_CONCLUSION, `${IN_CONCLUSION} ${escape}`)
|
||||
return [
|
||||
single(
|
||||
'surrogate-content',
|
||||
'escape \\ud800 inside the conclusion',
|
||||
BARE,
|
||||
inConclusion('\\ud800'),
|
||||
),
|
||||
single(
|
||||
'surrogate-version-overwritten',
|
||||
'"version": "\\ud800" overwritten by the later "version": 3',
|
||||
BARE,
|
||||
leadingMembers(bare, '"version": "\\ud800"'),
|
||||
),
|
||||
single(
|
||||
'surrogate-content-overwritten',
|
||||
'OD-4: "conclusion": "\\ud800 …" overwritten by the later conclusion',
|
||||
BARE,
|
||||
leadingMembers(bare, '"conclusion": "\\ud800 écrasée"'),
|
||||
),
|
||||
single(
|
||||
'surrogate-paired',
|
||||
'escape pair \\ud83d\\ude00 inside the conclusion',
|
||||
BARE,
|
||||
inConclusion('\\ud83d\\ude00'),
|
||||
),
|
||||
single(
|
||||
'surrogate-reversed-pair',
|
||||
'escapes \\ude00\\ud83d (two lone halves)',
|
||||
BARE,
|
||||
inConclusion('\\ude00\\ud83d'),
|
||||
),
|
||||
single(
|
||||
'surrogate-in-key',
|
||||
'an extra member whose key is "\\udfff"',
|
||||
BARE,
|
||||
leadingMembers(bare, '"\\udfff": true'),
|
||||
),
|
||||
single(
|
||||
'surrogate-escaped-backslash',
|
||||
'literal text \\\\ud800 (an escaped backslash)',
|
||||
BARE,
|
||||
inConclusion('\\\\ud800'),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
function duplicateCases(b: Bases): CaseSpec[] {
|
||||
const bare = b[BARE]
|
||||
return [
|
||||
single(
|
||||
'dup-version-string-then-number',
|
||||
'"version": "3" then "version": 3',
|
||||
BARE,
|
||||
leadingMembers(bare, '"version": "3"'),
|
||||
),
|
||||
single(
|
||||
'dup-version-number-then-string',
|
||||
'"version": 3 then "version": "3"',
|
||||
BARE,
|
||||
trailingMembers(bare, '"version": "3"'),
|
||||
),
|
||||
single(
|
||||
'dup-content-last-wins',
|
||||
'a second "conclusion" after the last member',
|
||||
BARE,
|
||||
trailingMembers(bare, '"conclusion": "Valeur finale"'),
|
||||
),
|
||||
single(
|
||||
'dup-snapshot-key',
|
||||
'snapshot "status": 42 then "status": "draft"',
|
||||
ENVELOPE,
|
||||
replaceFirst(
|
||||
b[ENVELOPE],
|
||||
'"status": "draft",',
|
||||
'"status": 42, "status": "draft",',
|
||||
),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
function integerCases(b: Bases): CaseSpec[] {
|
||||
const bare = b[BARE]
|
||||
const index = (literal: string) =>
|
||||
replaceFirst(bare, FIRST_INDEX, `"index": ${literal}`)
|
||||
return [
|
||||
single(
|
||||
'int-2pow53-plus-1',
|
||||
'vertebra "index": 9007199254740993',
|
||||
BARE,
|
||||
index('9007199254740993'),
|
||||
),
|
||||
single(
|
||||
'int-u64-overflow',
|
||||
'vertebra "index": 18446744073709551616',
|
||||
BARE,
|
||||
index('18446744073709551616'),
|
||||
),
|
||||
single(
|
||||
'negative-zero',
|
||||
'a snapshot circle "cx": -0',
|
||||
ENVELOPE,
|
||||
replaceFirst(b[ENVELOPE], SNAPSHOT_CX, '"cx": -0,'),
|
||||
),
|
||||
single('float-integer-index', 'vertebra "index": 3.0', BARE, index('3.0')),
|
||||
single(
|
||||
'float-integer-version',
|
||||
'"version": 3.0',
|
||||
BARE,
|
||||
replaceFirst(bare, '"version": 3', '"version": 3.0'),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
function keyCases(b: Bases): CaseSpec[] {
|
||||
const bare = b[BARE]
|
||||
return [
|
||||
single(
|
||||
'proto-key',
|
||||
'"__proto__": { "polluted": true } in the report',
|
||||
BARE,
|
||||
leadingMembers(bare, '"__proto__": { "polluted": true }'),
|
||||
),
|
||||
single(
|
||||
'integer-like-keys',
|
||||
'"1" and "0" members in the report',
|
||||
BARE,
|
||||
leadingMembers(bare, '"1": "un", "0": "zéro"'),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
/** An extra member nesting arrays so the document's depth is `depth`. */
|
||||
function nested(text: string, depth: number): string {
|
||||
const arrays = depth - 1
|
||||
return leadingMembers(
|
||||
text,
|
||||
`"deep": ${'['.repeat(arrays)}${']'.repeat(arrays)}`,
|
||||
)
|
||||
}
|
||||
|
||||
function depthCases(b: Bases): CaseSpec[] {
|
||||
return [
|
||||
single(
|
||||
'depth-64',
|
||||
'an extra member nesting the document to depth 64',
|
||||
BARE,
|
||||
nested(b[BARE], 64),
|
||||
),
|
||||
single(
|
||||
'depth-65',
|
||||
'an extra member nesting the document to depth 65',
|
||||
BARE,
|
||||
nested(b[BARE], 65),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
/** A full report JSON.parse accepts with a numeric version; its raw text keeps \ud800. */
|
||||
function overwrittenSurrogateDecoy(bare: string): Uint8Array {
|
||||
const marked = replaceFirst(bare, IN_CONCLUSION, '"conclusion": "Leurre')
|
||||
return utf8(leadingMembers(marked, '"version": "\\ud800"'))
|
||||
}
|
||||
|
||||
function multiFileCases(b: Bases): CaseSpec[] {
|
||||
const decoy = overwrittenSurrogateDecoy(b[BARE])
|
||||
const [envelope, bare] = [utf8(b[ENVELOPE]), utf8(b[BARE])]
|
||||
const pair = (
|
||||
id: string,
|
||||
description: string,
|
||||
base: BaseName,
|
||||
files: AttachmentSpec[],
|
||||
): CaseSpec => ({ id, description, base, attachments: files })
|
||||
return [
|
||||
pair(
|
||||
'two-files-decoy-first',
|
||||
'decoy report (overwritten "\\ud800" version, conclusion ' +
|
||||
'"Leurre") then the envelope',
|
||||
ENVELOPE,
|
||||
[
|
||||
{ name: DECOY, bytes: decoy },
|
||||
{ name: ATTACHMENT, bytes: envelope },
|
||||
],
|
||||
),
|
||||
pair('two-files-real-first', 'the envelope then the same decoy', ENVELOPE, [
|
||||
{ name: ATTACHMENT, bytes: envelope },
|
||||
{ name: DECOY, bytes: decoy },
|
||||
]),
|
||||
pair(
|
||||
'two-files-surrogate-decoy-first',
|
||||
'decoy {"version":3,"conclusion":"\\ud800"} ' + 'then the bare report',
|
||||
BARE,
|
||||
[
|
||||
{ name: DECOY, bytes: utf8('{"version": 3, "conclusion": "\\ud800"}') },
|
||||
{ name: ATTACHMENT, bytes: bare },
|
||||
],
|
||||
),
|
||||
pair(
|
||||
'two-files-invalid-first',
|
||||
'decoy {"version": 3,} (JSON.parse fails) then the ' + 'bare report',
|
||||
BARE,
|
||||
[
|
||||
{ name: DECOY, bytes: utf8('{"version": 3,}') },
|
||||
{ name: ATTACHMENT, bytes: bare },
|
||||
],
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
function shapeCases(b: Bases): CaseSpec[] {
|
||||
const bare = b[BARE]
|
||||
return [
|
||||
single(
|
||||
'whitespace-padded',
|
||||
'spaces, tab, CR and LF before the opening brace',
|
||||
BARE,
|
||||
` \n\t\r\n ${bare}`,
|
||||
),
|
||||
single(
|
||||
'top-level-array',
|
||||
'the report wrapped in an array',
|
||||
BARE,
|
||||
`[${bare}]`,
|
||||
),
|
||||
single('top-level-number', 'the number 3 alone', 'none', '3'),
|
||||
single('trailing-content', 'the report followed by "x"', BARE, `${bare}x`),
|
||||
]
|
||||
}
|
||||
|
||||
/** OD-3 classes: value counts recorded before R4.2 sets the budget. */
|
||||
function budgetCases(b: Bases): CaseSpec[] {
|
||||
const values = (count: number) =>
|
||||
leadingMembers(
|
||||
b[BARE],
|
||||
`"values": [${new Array(count).fill('0').join(',')}]`,
|
||||
)
|
||||
return [
|
||||
single(
|
||||
'values-100k',
|
||||
'an extra member holding 100,000 zeros',
|
||||
BARE,
|
||||
values(100_000),
|
||||
),
|
||||
single(
|
||||
'values-1m',
|
||||
'an extra member holding 1,000,000 zeros',
|
||||
BARE,
|
||||
values(1_000_000),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
function buildCases(b: Bases): CaseSpec[] {
|
||||
return [
|
||||
...controlCases(b),
|
||||
...utf8Cases(b),
|
||||
...bomCases(b),
|
||||
...numberCases(b),
|
||||
...surrogateCases(b),
|
||||
...duplicateCases(b),
|
||||
...integerCases(b),
|
||||
...keyCases(b),
|
||||
...depthCases(b),
|
||||
...multiFileCases(b),
|
||||
...shapeCases(b),
|
||||
...budgetCases(b),
|
||||
]
|
||||
}
|
||||
|
||||
function loadBases(): Bases {
|
||||
const read = (rel: string) => readFileSync(resolve(ROOT, rel), 'utf8')
|
||||
return {
|
||||
'report-v3.demo.json': read(BASE_FILES['report-v3.demo.json']),
|
||||
'envelope.demo.fr.nologos.json': read(
|
||||
BASE_FILES['envelope.demo.fr.nologos.json'],
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// PDF and outcome
|
||||
|
||||
/** Shaped like `exportReportArchive`'s output: same keyword, attachment options, save flags. */
|
||||
async function buildPdf(
|
||||
attachments: readonly AttachmentSpec[],
|
||||
): Promise<Uint8Array> {
|
||||
const doc = await PDFDocument.create()
|
||||
doc.addPage(A4)
|
||||
doc.setKeywords([PDF_KEYWORD])
|
||||
doc.setCreationDate(FIXED_DATE)
|
||||
doc.setModificationDate(FIXED_DATE)
|
||||
for (const { name, bytes } of attachments) {
|
||||
await doc.attach(bytes, name, {
|
||||
mimeType: 'application/json',
|
||||
description: 'Faunosteo report archive v1',
|
||||
creationDate: FIXED_DATE,
|
||||
modificationDate: FIXED_DATE,
|
||||
afRelationship: AFRelationship.Data,
|
||||
})
|
||||
}
|
||||
return doc.save({ useObjectStreams: false })
|
||||
}
|
||||
|
||||
function jsonParses(bytes: Uint8Array): boolean {
|
||||
try {
|
||||
JSON.parse(new TextDecoder().decode(bytes))
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
function isValidUtf8(bytes: Uint8Array): boolean {
|
||||
try {
|
||||
new TextDecoder('utf-8', { fatal: true }).decode(bytes)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
function attachmentRow({ name, bytes }: AttachmentSpec) {
|
||||
return {
|
||||
name,
|
||||
sha256: sha256(bytes),
|
||||
byteLength: bytes.byteLength,
|
||||
validUtf8: isValidUtf8(bytes),
|
||||
jsonParse: jsonParses(bytes),
|
||||
}
|
||||
}
|
||||
|
||||
/** Index of the attachment the TS scanner retains; null when it retains none. */
|
||||
async function chosenAttachment(pdf: Uint8Array, digests: readonly string[]) {
|
||||
try {
|
||||
const at = digests.indexOf(sha256(await readAttachmentBytes(pdf)))
|
||||
if (at < 0) throw new Error('scanner returned bytes of no attachment')
|
||||
return at
|
||||
} catch (err) {
|
||||
if (err instanceof ArchiveReadError) return null
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
function importedRow({ read, rebase }: ArchiveImport) {
|
||||
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,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function failureRow(err: unknown) {
|
||||
if (err instanceof ImportError) {
|
||||
const found =
|
||||
err.foundVersion === undefined ? {} : { foundVersion: err.foundVersion }
|
||||
return { ok: false, kind: err.kind, ...found }
|
||||
}
|
||||
if (err instanceof Error)
|
||||
return { ok: false, kind: 'error', message: err.message }
|
||||
throw err
|
||||
}
|
||||
|
||||
/** HomeRoute's PDF branch (`readImport`), size pre-check aside: scan, then core import. */
|
||||
async function r3Outcome(pdf: Uint8Array, sheet: SheetIndex) {
|
||||
try {
|
||||
const attachment = await readAttachment(pdf)
|
||||
return importedRow(importAttachment(attachment, sheet))
|
||||
} catch (err) {
|
||||
return failureRow(err)
|
||||
}
|
||||
}
|
||||
|
||||
async function caseRow(spec: CaseSpec, pdf: Uint8Array, sheet: SheetIndex) {
|
||||
const attachments = spec.attachments.map(attachmentRow)
|
||||
return {
|
||||
id: spec.id,
|
||||
description: spec.description,
|
||||
base: spec.base,
|
||||
pdf: `${PDF_DIR_REL}/${spec.id}.pdf`,
|
||||
pdfSha256: sha256(pdf),
|
||||
attachments,
|
||||
chosen: await chosenAttachment(
|
||||
pdf,
|
||||
attachments.map((a) => a.sha256),
|
||||
),
|
||||
outcome: await r3Outcome(pdf, sheet),
|
||||
}
|
||||
}
|
||||
|
||||
const pdfPath = (id: string) => resolve(PDF_DIR, `${id}.pdf`)
|
||||
|
||||
function refuseFieldCorpus(): void {
|
||||
if (process.env.FAUNOSTEO_FIELD_CORPUS) {
|
||||
throw new Error('refused: FAUNOSTEO_FIELD_CORPUS is set (plan §B.5.2)')
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('R4 O8 tolerance corpus on the R3 import path', () => {
|
||||
let sheet: SheetIndex
|
||||
let cases: CaseSpec[]
|
||||
|
||||
beforeAll(async () => {
|
||||
if (MAKE) refuseFieldCorpus()
|
||||
sheet = await loadTestSheet()
|
||||
cases = buildCases(loadBases())
|
||||
})
|
||||
|
||||
it('has unique case ids', () => {
|
||||
const ids = cases.map((c) => c.id)
|
||||
expect(new Set(ids).size).toBe(ids.length)
|
||||
})
|
||||
|
||||
it('builds byte-identical PDFs (written under MAKE_R4_ORACLES)', async () => {
|
||||
if (MAKE) mkdirSync(PDF_DIR, { recursive: true })
|
||||
for (const spec of cases) {
|
||||
const pdf = await buildPdf(spec.attachments)
|
||||
if (MAKE) writeFileSync(pdfPath(spec.id), pdf)
|
||||
else
|
||||
expect(sha256(pdf), spec.id).toBe(
|
||||
sha256(readFileSync(pdfPath(spec.id))),
|
||||
)
|
||||
}
|
||||
const expected = cases.map((c) => `${c.id}.pdf`).sort()
|
||||
expect(readdirSync(PDF_DIR).sort()).toEqual(expected)
|
||||
}, 60_000)
|
||||
|
||||
it('records the R3 outcome of every committed PDF', async () => {
|
||||
const rows = []
|
||||
for (const spec of cases) {
|
||||
const pdf = new Uint8Array(readFileSync(pdfPath(spec.id)))
|
||||
rows.push(await caseRow(spec, pdf, sheet))
|
||||
}
|
||||
const oracle = { oracle: 'O8', sheet: sheet.sheet.ref, cases: rows }
|
||||
if (MAKE) {
|
||||
mkdirSync(dirname(OUT), { recursive: true })
|
||||
writeFileSync(OUT, JSON.stringify(oracle, null, 2) + '\n')
|
||||
}
|
||||
expect(oracle).toEqual(JSON.parse(readFileSync(OUT, 'utf8')))
|
||||
}, 120_000)
|
||||
|
||||
it('imports both controls with their archive state', async () => {
|
||||
const oracle = JSON.parse(readFileSync(OUT, 'utf8'))
|
||||
const row = (id: string) =>
|
||||
oracle.cases.find((c: { id: string }) => c.id === id)
|
||||
expect(row('control-bare-v3').outcome).toMatchObject({
|
||||
ok: true,
|
||||
archiveSha256: null,
|
||||
})
|
||||
expect(row('control-envelope').outcome).toMatchObject({ ok: true })
|
||||
expect(row('control-envelope').outcome.archiveSha256).not.toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -1,366 +0,0 @@
|
||||
/**
|
||||
* R4.1 freeze, writer oracles O1 to O3 (plan 2026-10-17 §B.5.1), recorded on
|
||||
* the application export path with the Step 6 golden inputs (three reports,
|
||||
* fr / en, nologos / logos, fixed 1×1 / 2×1 rasters) at FIXED_EXPORTED_AT:
|
||||
* - O1, the complete draw log of every run, which the Step 6 text, images
|
||||
* and links goldens must equal once rounded;
|
||||
* - O2, the PDFs of those runs, one real-raster PDF (resvg, v2full's first
|
||||
* observation) with every PNG it embedded, one PDF exported through the
|
||||
* store path (D-R11 case);
|
||||
* - O3, the frozen pre-R2 envelopes attached byte for byte by pdf-lib.
|
||||
* `MAKE_R4_ORACLES=1` writes them; never again without an owner decision.
|
||||
*/
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { resolve } from 'node:path'
|
||||
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
|
||||
import { AFRelationship, PageSizes, PDFDocument, StandardFonts } from 'pdf-lib'
|
||||
import type { Branding, Practitioner, Report } from '@faunosteo/core-wasm/types'
|
||||
import {
|
||||
exportReportArchive,
|
||||
readAttachedDocument,
|
||||
readAttachmentBytes,
|
||||
type ExportInput,
|
||||
type Rasterizer,
|
||||
} from '@faunosteo/pdf'
|
||||
import { loadBranding } from '@/pdf/branding'
|
||||
import { defaultPdfLabels } from '@/pdf/labels'
|
||||
import { importAttachment } from '@/pdf/import'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { useReportStore } from '@/state/reportStore'
|
||||
import { resvgRasterizer } from '../../../../packages/pdf/tests/helpers/resvg'
|
||||
import { loadTestSheet, sheetSnapshot } from '../helpers/sheet'
|
||||
import {
|
||||
buildGoldenReports,
|
||||
GOLDEN_REPORTS,
|
||||
type GoldenReportName,
|
||||
} from '../helpers/goldenReports'
|
||||
import {
|
||||
brandingOf,
|
||||
FIXTURES_DIR,
|
||||
ONE_PX_PNG,
|
||||
readGolden,
|
||||
TWO_BY_ONE_PNG,
|
||||
viewIdsOf,
|
||||
type LogoMode,
|
||||
} from '../helpers/goldens'
|
||||
import {
|
||||
checkBytesOracle,
|
||||
checkJsonOracle,
|
||||
drawLogOf,
|
||||
FIXED_EXPORTED_AT,
|
||||
R3_PDF_DIR,
|
||||
R4_GOLDEN_DIR,
|
||||
readPdfFacts,
|
||||
recordFullDrawing,
|
||||
sha256,
|
||||
step6Images,
|
||||
step6Links,
|
||||
step6Texts,
|
||||
STEP6_PDF_DIR,
|
||||
type DrawLog,
|
||||
} from '../helpers/r4-draw'
|
||||
|
||||
const raster = vi.hoisted(() => ({ rasterize: null as Rasterizer | null }))
|
||||
vi.mock('@/pdf/browserRasterizer', () => ({
|
||||
browserRasterizer: vi.fn<Rasterizer>((svg, size) =>
|
||||
raster.rasterize!(svg, size),
|
||||
),
|
||||
}))
|
||||
const logoMode = vi.hoisted(() => ({ current: 'nologos' as LogoMode }))
|
||||
vi.mock('@/pdf/branding', async () => {
|
||||
const { brandingOf } = await import('../helpers/goldens')
|
||||
return { loadBranding: vi.fn(() => brandingOf(logoMode.current)) }
|
||||
})
|
||||
|
||||
const MODES: readonly LogoMode[] = ['nologos', 'logos']
|
||||
const LOCALES: readonly Locale[] = ['fr', 'en']
|
||||
const REAL_DIR = resolve(R3_PDF_DIR, 'real')
|
||||
const R2_DIR = resolve(FIXTURES_DIR, 'goldens/r2')
|
||||
const R2_ENVELOPES = ['demo', 'v2full', 'v2unresolved'].map(
|
||||
(name) => `${name}.fr.nologos`,
|
||||
)
|
||||
const REAL_RASTER_TIMEOUT = 120_000
|
||||
|
||||
let sheet: SheetIndex
|
||||
let reports: Record<GoldenReportName, Report>
|
||||
|
||||
beforeAll(async () => {
|
||||
sheet = await loadTestSheet()
|
||||
reports = buildGoldenReports(sheet).reports
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => undefined)
|
||||
vi.useFakeTimers({ toFake: ['Date'] })
|
||||
vi.setSystemTime(FIXED_EXPORTED_AT)
|
||||
})
|
||||
afterAll(() => {
|
||||
useReportStore.getState().reset()
|
||||
sessionStorage.clear()
|
||||
vi.useRealTimers()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
interface Run {
|
||||
report: Report
|
||||
mode: LogoMode
|
||||
locale: Locale
|
||||
rasterize: Rasterizer
|
||||
}
|
||||
|
||||
interface Exported {
|
||||
pdf: Uint8Array
|
||||
log: DrawLog
|
||||
}
|
||||
|
||||
const fixedRaster =
|
||||
(mode: LogoMode): Rasterizer =>
|
||||
async () =>
|
||||
mode === 'logos' ? TWO_BY_ONE_PNG : ONE_PX_PNG
|
||||
|
||||
/**
|
||||
* One export through the TypeScript engine with the application's inputs (the snapshot
|
||||
* the core builds, the bundled branding of the mode, the app's labels, the fixed export
|
||||
* time), drawn under the full recorder. R4.6 moved `@/pdf/export` onto the core's
|
||||
* session: the oracles stay those of the R3 build, so the engine is called directly, as
|
||||
* the app export called it until then (same inputs, same order).
|
||||
*/
|
||||
async function exportRun(run: Run): Promise<Exported> {
|
||||
raster.rasterize = run.rasterize
|
||||
logoMode.current = run.mode
|
||||
const recorder = recordFullDrawing()
|
||||
try {
|
||||
const { pdf, warnings } = await exportReportArchive({
|
||||
report: run.report as ExportInput['report'],
|
||||
sheet: sheetSnapshot(sheet, run.report) as ExportInput['sheet'],
|
||||
branding: await loadBranding(),
|
||||
labels: defaultPdfLabels,
|
||||
locale: run.locale,
|
||||
rasterizer: run.rasterize,
|
||||
exportedAt: new Date(),
|
||||
})
|
||||
expect(warnings).toEqual([])
|
||||
expect(recorder.unexpectedCalls()).toEqual([])
|
||||
expect(recorder.state.documents.size).toBe(1)
|
||||
return { pdf, log: drawLogOf(recorder, await readPdfFacts(pdf)) }
|
||||
} finally {
|
||||
recorder.stop()
|
||||
}
|
||||
}
|
||||
|
||||
describe.each(GOLDEN_REPORTS)('writer oracles of %s', (name) => {
|
||||
describe.each(MODES)('%s', (mode) => {
|
||||
it.each(LOCALES)(
|
||||
'%s: O1 draw log, O2 PDF, rounded = step6',
|
||||
async (locale) => {
|
||||
const run = { report: reports[name], mode, locale }
|
||||
const out = await exportRun({ ...run, rasterize: fixedRaster(mode) })
|
||||
const id = `${name}.${locale}.${mode}`
|
||||
checkJsonOracle(resolve(R4_GOLDEN_DIR, `draw.${id}.json`), out.log)
|
||||
checkBytesOracle(resolve(R3_PDF_DIR, `${id}.pdf`), out.pdf)
|
||||
expect(step6Texts(out.log)).toEqual(readGolden(`text.${id}.json`))
|
||||
expect(step6Images(out.log)).toEqual(readGolden(`images.${id}.json`))
|
||||
expect(step6Links(out.log)).toEqual(readGolden(`links.${id}.json`))
|
||||
},
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
type RasterSubject =
|
||||
| { kind: 'logo'; slot: string }
|
||||
| { kind: 'view'; observationId: string; viewId: string }
|
||||
|
||||
interface RasterCall {
|
||||
index: number
|
||||
svgSha256: string
|
||||
widthPx: number
|
||||
heightPx: number
|
||||
png: Uint8Array | null
|
||||
}
|
||||
|
||||
const LOGO_SLOTS = ['headerLogo', 'footerLogo', 'poweredBy', 'partner'] as const
|
||||
|
||||
function logoOf(branding: Branding, slot: (typeof LOGO_SLOTS)[number]) {
|
||||
const value = branding[slot]
|
||||
return value && 'kind' in value ? value : (value?.logo ?? null)
|
||||
}
|
||||
|
||||
/**
|
||||
* The engine's rasteriser calls in order: the SVG logos while embedding the
|
||||
* branding (header, footer, powered-by, partner), then each observation's
|
||||
* views, main first (packages/pdf export and observationPage).
|
||||
*/
|
||||
function rasterSubjects(branding: Branding, report: Report): RasterSubject[] {
|
||||
const logos = LOGO_SLOTS.filter(
|
||||
(slot) => logoOf(branding, slot)?.kind === 'svg',
|
||||
).map((slot): RasterSubject => ({ kind: 'logo', slot }))
|
||||
const snapshot = sheetSnapshot(sheet, report)
|
||||
const views = report.observations.flatMap((o) =>
|
||||
viewIdsOf(snapshot, o).map(
|
||||
(viewId): RasterSubject => ({
|
||||
kind: 'view',
|
||||
observationId: o.id,
|
||||
viewId,
|
||||
}),
|
||||
),
|
||||
)
|
||||
return [...logos, ...views]
|
||||
}
|
||||
|
||||
/** resvg wrapped: each call is numbered when it is made, its PNG kept. */
|
||||
function capturingResvg(calls: RasterCall[]): Rasterizer {
|
||||
const resvg = resvgRasterizer()
|
||||
return async (svg, size) => {
|
||||
const call: RasterCall = {
|
||||
index: calls.length,
|
||||
svgSha256: sha256(svg),
|
||||
widthPx: size.widthPx,
|
||||
heightPx: size.heightPx,
|
||||
png: null,
|
||||
}
|
||||
calls.push(call)
|
||||
call.png = await resvg(svg, size)
|
||||
return call.png
|
||||
}
|
||||
}
|
||||
|
||||
const REAL_NOTE =
|
||||
'v2full reduced to its first observation (obs-1, main view), every other ' +
|
||||
'field unchanged: the full report weighed 3 MB in real rasters.'
|
||||
|
||||
/** v2full with its first observation only: one view keeps the set small. */
|
||||
function firstObservationOf(report: Report): Report {
|
||||
return { ...report, observations: report.observations.slice(0, 1) }
|
||||
}
|
||||
|
||||
describe('O2 real raster: v2full first observation, fr, logos, resvg', () => {
|
||||
it(
|
||||
'freezes the PDF, every PNG returned and the call manifest',
|
||||
async () => {
|
||||
const calls: RasterCall[] = []
|
||||
const report = firstObservationOf(reports.v2full)
|
||||
const { pdf } = await exportRun({
|
||||
report,
|
||||
mode: 'logos',
|
||||
locale: 'fr',
|
||||
rasterize: capturingResvg(calls),
|
||||
})
|
||||
const subjects = rasterSubjects(await brandingOf('logos'), report)
|
||||
expect(subjects).toEqual([
|
||||
{ kind: 'logo', slot: 'partner' },
|
||||
{ kind: 'view', observationId: 'obs-1', viewId: 'main' },
|
||||
])
|
||||
expect(calls).toHaveLength(subjects.length)
|
||||
checkBytesOracle(resolve(REAL_DIR, 'v2full-obs1.fr.logos.pdf'), pdf)
|
||||
const manifest = calls.map(({ png, ...call }) => {
|
||||
const file = `raster-${call.index}.png`
|
||||
checkBytesOracle(resolve(REAL_DIR, file), png!)
|
||||
const pngSha256 = sha256(png!)
|
||||
return { ...call, png: file, pngSha256, subject: subjects[call.index] }
|
||||
})
|
||||
const manifestFile = resolve(REAL_DIR, 'manifest.json')
|
||||
checkJsonOracle(manifestFile, { note: REAL_NOTE, calls: manifest })
|
||||
},
|
||||
REAL_RASTER_TIMEOUT,
|
||||
)
|
||||
})
|
||||
|
||||
type KeyOrder = { path: string; keys: string[] }
|
||||
|
||||
function keyOrders(value: unknown, path = 'report'): KeyOrder[] {
|
||||
if (!value || typeof value !== 'object') return []
|
||||
const entries = Object.entries(value)
|
||||
const own = Array.isArray(value)
|
||||
? []
|
||||
: [{ path, keys: entries.map(([k]) => k) }]
|
||||
return [...own, ...entries.flatMap(([k, v]) => keyOrders(v, `${path}.${k}`))]
|
||||
}
|
||||
|
||||
/** Objects whose key sequence differs between two equal values. */
|
||||
function keyOrderDifferences(a: unknown, b: unknown) {
|
||||
const other = new Map(keyOrders(b).map((o) => [o.path, o.keys]))
|
||||
return keyOrders(a)
|
||||
.map((o) => ({ path: o.path, attachment: o.keys, core: other.get(o.path) }))
|
||||
.filter((d) => JSON.stringify(d.attachment) !== JSON.stringify(d.core))
|
||||
}
|
||||
|
||||
/** A partial practitioner, keys out of the contract order (name, cabinet, contact, siret). */
|
||||
const EDITED_PRACTITIONER: Practitioner = {
|
||||
contact: 'secretariat@example.test',
|
||||
name: 'Dr. Claire Dupont-Martin',
|
||||
}
|
||||
|
||||
describe('O2 store path: demo, next appointment set, practitioner edited', () => {
|
||||
it('freezes the PDF and the store report; attachment order is D-R11', async () => {
|
||||
sessionStorage.clear()
|
||||
const store = useReportStore.getState()
|
||||
expect(
|
||||
store.hydrateImport({ report: reports.demo, archive: null }, sheet),
|
||||
).toEqual({ ok: true })
|
||||
store.setNextAppointment({ kind: 'date', date: '2026-11-20' })
|
||||
store.setPractitioner(EDITED_PRACTITIONER)
|
||||
const report = useReportStore.getState().report!
|
||||
const { pdf } = await exportRun({
|
||||
report,
|
||||
mode: 'nologos',
|
||||
locale: 'fr',
|
||||
rasterize: fixedRaster('nologos'),
|
||||
})
|
||||
checkBytesOracle(resolve(R3_PDF_DIR, 'store-path.fr.nologos.pdf'), pdf)
|
||||
checkJsonOracle(resolve(R3_PDF_DIR, 'store-path.report.json'), report)
|
||||
const attached = (await readAttachedDocument(pdf)) as { report: unknown }
|
||||
expect(keyOrders(attached.report)).toEqual(keyOrders(report))
|
||||
const reread = importAttachment(attached, sheet).read.report
|
||||
expect(reread).toEqual(attached.report)
|
||||
const differences = keyOrderDifferences(attached.report, reread)
|
||||
console.info('D-R11 key order, attachment vs core:', differences)
|
||||
expect(differences.length).toBeGreaterThan(0)
|
||||
})
|
||||
})
|
||||
|
||||
interface EnvelopeMeta {
|
||||
locale: 'fr' | 'en'
|
||||
labels: Record<string, Record<string, string>>
|
||||
report: { animal: { name: string }; practitioner: { name: string } }
|
||||
}
|
||||
|
||||
/** The engine's file structure around a given attachment (export.ts, attachment/write.ts). */
|
||||
async function envelopePdf(attachment: Uint8Array): Promise<Uint8Array> {
|
||||
const envelope = JSON.parse(
|
||||
new TextDecoder().decode(attachment),
|
||||
) as EnvelopeMeta
|
||||
const labels = envelope.labels[envelope.locale]
|
||||
const title = `${labels['pdf.title.suffix.bilan']}${envelope.report.animal.name}`
|
||||
const doc = await PDFDocument.create()
|
||||
doc.setTitle(title)
|
||||
doc.setAuthor(envelope.report.practitioner.name)
|
||||
doc.setSubject(labels['pdf.subject'])
|
||||
doc.setKeywords(['faunosteo-archive/1'])
|
||||
doc.setCreationDate(FIXED_EXPORTED_AT)
|
||||
doc.setModificationDate(FIXED_EXPORTED_AT)
|
||||
const font = await doc.embedFont(StandardFonts.Helvetica)
|
||||
doc.addPage(PageSizes.A4).drawText(title, { x: 40, y: 790, size: 12, font })
|
||||
await doc.attach(attachment, 'osteo-report.json', {
|
||||
mimeType: 'application/json',
|
||||
description: 'Faunosteo report archive v1',
|
||||
creationDate: FIXED_EXPORTED_AT,
|
||||
modificationDate: FIXED_EXPORTED_AT,
|
||||
afRelationship: AFRelationship.Data,
|
||||
})
|
||||
return doc.save({ useObjectStreams: false })
|
||||
}
|
||||
|
||||
describe.each(R2_ENVELOPES)('O3 pre-R2 envelope PDF %s', (name) => {
|
||||
// The R2 freeze wrote the decoded attachment text as is, and UTF-8
|
||||
// round-trips JSON.stringify output: the file bytes are the attachment.
|
||||
it('carries the frozen bytes, read back as is', async () => {
|
||||
const golden = new Uint8Array(
|
||||
readFileSync(resolve(R2_DIR, `envelope.${name}.json`)),
|
||||
)
|
||||
const pdf = await envelopePdf(golden)
|
||||
checkBytesOracle(resolve(STEP6_PDF_DIR, `envelope.${name}.pdf`), pdf)
|
||||
const read = await readAttachmentBytes(pdf)
|
||||
expect(Buffer.from(read).equals(Buffer.from(golden))).toBe(true)
|
||||
expect(await readAttachedDocument(pdf)).toEqual(
|
||||
JSON.parse(new TextDecoder().decode(golden)),
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -1,362 +0,0 @@
|
||||
/**
|
||||
* The archive check tool (scripts/compare-reports.ts) on the frozen v2 PDF,
|
||||
* on a synthetic v2 report with removed and unknown items, on a v3 report
|
||||
* and on a Step 6 envelope (archive header line). The tool keeps its own
|
||||
* reading on the frozen TypeScript domain until R4 (`compareSheetIndex`);
|
||||
* the fixtures it compares are built through the application (the core).
|
||||
*/
|
||||
import { describe, it, expect, beforeAll } from 'vitest'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { dirname, resolve } from 'node:path'
|
||||
import {
|
||||
readArchiveDocument,
|
||||
readReportDocument,
|
||||
type ReportV2,
|
||||
} from '@faunosteo/domain'
|
||||
import { readAttachedDocument } from '@faunosteo/pdf'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import {
|
||||
compareSheetIndex,
|
||||
type CompareSheetIndex,
|
||||
} from '../../scripts/compare/sheet'
|
||||
import {
|
||||
compareReport,
|
||||
diffLines,
|
||||
parseArgs,
|
||||
renderComparison,
|
||||
renderSummary,
|
||||
UNRESOLVED,
|
||||
} from '../../scripts/compare/report'
|
||||
import {
|
||||
archiveEnvelope,
|
||||
bundledSheet,
|
||||
loadTestSheet,
|
||||
minimalReport,
|
||||
} from '../helpers/sheet'
|
||||
|
||||
const HERE = dirname(fileURLToPath(import.meta.url))
|
||||
const OPTS = { locale: 'fr' as const, full: false }
|
||||
|
||||
/** The application's sheet (fixture building) and the tool's own reading of it. */
|
||||
let sheet: SheetIndex
|
||||
let cli: CompareSheetIndex
|
||||
|
||||
function minimalV2(): ReportV2 {
|
||||
return JSON.parse(
|
||||
readFileSync(resolve(HERE, '../fixtures/report-v2.minimal.json'), 'utf8'),
|
||||
) as ReportV2
|
||||
}
|
||||
|
||||
function compareRaw(raw: unknown, full = false) {
|
||||
const outcome = readArchiveDocument(raw, cli.sheet.migrationMap)
|
||||
return compareReport(raw, outcome, cli, { ...OPTS, full })
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
sheet = await loadTestSheet()
|
||||
cli = compareSheetIndex(await bundledSheet())
|
||||
})
|
||||
|
||||
describe('compareReport on the frozen v2 archive PDF', () => {
|
||||
it('shows every legacy zone and finding next to its canonical target', async () => {
|
||||
const bytes = readFileSync(resolve(HERE, '../fixtures/report-v2.full.pdf'))
|
||||
const raw = await readAttachedDocument(new Uint8Array(bytes))
|
||||
const c = compareRaw(raw)
|
||||
const lines = renderComparison('full.pdf', c)
|
||||
expect(c.source).toBe('v2')
|
||||
expect(lines[0]).toBe('== full.pdf')
|
||||
expect(lines).toContain(' v2 → v3 (migrated)')
|
||||
expect(lines.join('\n')).toContain(
|
||||
'point-mpbwtst0 left "Hanche (coxo-fémorale)" → hip@main left "Hanche (coxo-fémorale)"',
|
||||
)
|
||||
expect(lines.join('\n')).toContain(
|
||||
'cervical/cervical-observations/stumbling-proprioceptive (!) "Trébuchements / déficit proprioceptif rapporté" → cervical/cervical-consultation-reasons/stumbling-proprioceptive',
|
||||
)
|
||||
expect(lines.join('\n')).toContain('severity modérée → moderate')
|
||||
expect(lines.some((l) => l.includes(UNRESOLVED))).toBe(false)
|
||||
expect(c.counts).toEqual({
|
||||
observations: 3,
|
||||
zones: 5,
|
||||
unresolvedZones: 0,
|
||||
findings: 4,
|
||||
unresolvedFindings: 0,
|
||||
})
|
||||
expect(renderSummary('full.pdf', c)).toBe(
|
||||
'full.pdf · v2→v3 · 3 obs · zones 5/0 unresolved · findings 4/0 unresolved',
|
||||
)
|
||||
expect(lines.join('\n')).not.toContain('practitioner')
|
||||
expect(lines.join('\n')).not.toContain('Dupont')
|
||||
expect(renderComparison('x', compareRaw(raw, true)).join('\n')).toContain(
|
||||
'practitioner "Dr. Claire Dupont"',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('compareReport on unresolved items', () => {
|
||||
it('names a removed finding, a finding unknown to the catalog and an unknown zone', () => {
|
||||
const v2: ReportV2 = {
|
||||
...minimalV2(),
|
||||
observations: [
|
||||
{
|
||||
id: 'o',
|
||||
zones: [{ boneId: 'point-gone' }],
|
||||
findings: [
|
||||
{
|
||||
zoneId: 'ssb',
|
||||
path: ['ssb-dysfunctions', 'sutures', 'sphenozygomatic'],
|
||||
labelFrSnapshot: 'Suture sphéno-zygomatique',
|
||||
labelEnSnapshot: 'Sphenozygomatic suture',
|
||||
requiresVetReferral: true,
|
||||
},
|
||||
{
|
||||
zoneId: 'nowhere',
|
||||
path: ['x'],
|
||||
labelFrSnapshot: 'X',
|
||||
labelEnSnapshot: 'X',
|
||||
},
|
||||
],
|
||||
freeTextFindings: [],
|
||||
comment: '',
|
||||
},
|
||||
],
|
||||
}
|
||||
const text = renderComparison('odd.pdf', compareRaw(v2)).join('\n')
|
||||
expect(text).toContain(
|
||||
`point-gone ${UNRESOLVED} unknown legacy id, kept as point-gone@main`,
|
||||
)
|
||||
expect(text).toContain(
|
||||
`ssb/ssb-dysfunctions/sutures/sphenozygomatic (!) "Suture sphéno-zygomatique" ${UNRESOLVED} removed from the sheet, kept with its label`,
|
||||
)
|
||||
expect(text).toContain(
|
||||
`nowhere/x "X" ${UNRESOLVED} never in the legacy catalog, kept with its label`,
|
||||
)
|
||||
expect(text).toContain('1 zones (1 unresolved) · 2 findings (2 unresolved)')
|
||||
})
|
||||
|
||||
it('reads a v3 report as is and flags what the sheet cannot place', () => {
|
||||
const report = minimalReport(sheet, {
|
||||
observations: [
|
||||
{
|
||||
id: 'o3',
|
||||
zones: [{ annotationId: 'hip', viewId: 'main', side: 'left' }],
|
||||
findings: [
|
||||
{
|
||||
findingZoneId: 'hip',
|
||||
path: ['hip-dysfunctions', 'articular', 'restriction-flexion'],
|
||||
labelFrSnapshot: 'Restriction en flexion',
|
||||
labelEnSnapshot: 'Flexion restriction',
|
||||
},
|
||||
{
|
||||
findingZoneId: 'atm',
|
||||
path: ['atm-observations', 'headshaking'],
|
||||
labelFrSnapshot: 'Headshaking (secouement de tête)',
|
||||
labelEnSnapshot: 'Headshaking',
|
||||
unresolved: true,
|
||||
},
|
||||
{
|
||||
findingZoneId: 'hip',
|
||||
path: ['hip-dysfunctions'],
|
||||
labelFrSnapshot: 'Catégorie',
|
||||
labelEnSnapshot: 'Category',
|
||||
},
|
||||
],
|
||||
freeTextFindings: [],
|
||||
comment: '',
|
||||
},
|
||||
],
|
||||
})
|
||||
const c = compareRaw(report)
|
||||
const text = renderComparison('v3.pdf', c).join('\n')
|
||||
expect(c.source).toBe('v3')
|
||||
expect(text).toContain('v3 (read as is)')
|
||||
expect(text).toContain('hip@main left "Hanche (coxo-fémorale)"')
|
||||
expect(text).toContain(
|
||||
'hip/hip-dysfunctions/articular/restriction-flexion "Restriction en flexion"',
|
||||
)
|
||||
expect(text).toContain(
|
||||
`atm/atm-observations/headshaking "Headshaking (secouement de tête)" ${UNRESOLVED} kept with its snapshot`,
|
||||
)
|
||||
expect(text).toContain('!! points at a category, not a leaf')
|
||||
expect(c.counts.unresolvedFindings).toBe(1)
|
||||
})
|
||||
|
||||
it('prints the archive line of an envelope: snapshot kept, or rejected with its reason', () => {
|
||||
const envelope = archiveEnvelope(
|
||||
sheet,
|
||||
minimalReport(sheet, {
|
||||
observations: [
|
||||
{
|
||||
id: 'o-env',
|
||||
zones: [{ annotationId: 'hip', viewId: 'main', side: 'left' }],
|
||||
findings: [],
|
||||
freeTextFindings: [],
|
||||
comment: '',
|
||||
},
|
||||
],
|
||||
}),
|
||||
)
|
||||
const kept = compareRaw(envelope)
|
||||
expect(kept.source).toBe('v3')
|
||||
expect(kept.header).toContain('archive v1 (snapshot kept)')
|
||||
const text = renderComparison('env.pdf', kept).join('\n')
|
||||
expect(text).toContain('hip@main left "Hanche (coxo-fémorale)"')
|
||||
const rejected = compareRaw({ ...envelope, envelopeVersion: 2 })
|
||||
expect(rejected.header).toContain(
|
||||
'archive v1 (snapshot rejected: unsupported_envelope)',
|
||||
)
|
||||
expect(compareRaw(envelope.report).header).not.toContain(
|
||||
'archive v1 (snapshot kept)',
|
||||
)
|
||||
})
|
||||
|
||||
it('warns when a v3 report was written on another sheet document', () => {
|
||||
const report = minimalReport(sheet, {
|
||||
sheet: { ...sheet.sheet.ref, documentSha256: 'b'.repeat(64) },
|
||||
})
|
||||
const text = renderComparison('other.pdf', compareRaw(report)).join('\n')
|
||||
expect(text).toContain(
|
||||
'!! written on another sheet document (sha bbbbbbbbbbbb…)',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('diff and arguments', () => {
|
||||
it('diffs the v3 content of a migrated v2 report and its v3 re-reading', () => {
|
||||
const v2 = minimalV2()
|
||||
const migrated = readReportDocument(v2, cli.sheet.migrationMap).report
|
||||
const a = compareRaw(v2)
|
||||
const b = compareRaw(migrated)
|
||||
expect(diffLines(a.normalized, b.normalized)).toEqual([])
|
||||
const added = { ...migrated, conclusion: 'changed' }
|
||||
expect(diffLines(a.normalized, compareRaw(added).normalized)).toEqual([
|
||||
'+ conclusion (present)',
|
||||
])
|
||||
})
|
||||
|
||||
it('compares placeholders by default and real texts with --full', () => {
|
||||
const base = readReportDocument(minimalV2(), cli.sheet.migrationMap)
|
||||
const original = { ...base.report, conclusion: 'original' }
|
||||
const edited = { ...base.report, conclusion: 'changed' }
|
||||
expect(
|
||||
diffLines(compareRaw(original).normalized, compareRaw(edited).normalized),
|
||||
).toEqual([])
|
||||
expect(
|
||||
diffLines(
|
||||
compareRaw(original, true).normalized,
|
||||
compareRaw(edited, true).normalized,
|
||||
),
|
||||
).toEqual(['- conclusion "original"', '+ conclusion "changed"'])
|
||||
})
|
||||
|
||||
it('parses files and options, refuses unknown ones', () => {
|
||||
expect(parseArgs(['a.pdf', '--summary', 'dir', '--locale', 'en'])).toEqual({
|
||||
files: ['a.pdf', 'dir'],
|
||||
locale: 'en',
|
||||
full: false,
|
||||
summary: true,
|
||||
diff: false,
|
||||
})
|
||||
expect(parseArgs(['--full', '--diff', 'a', 'b']).full).toBe(true)
|
||||
expect(() => parseArgs(['--identity'])).toThrow('unknown option')
|
||||
expect(() => parseArgs(['--locale', 'de'])).toThrow('fr or en')
|
||||
expect(() => parseArgs(['--json'])).toThrow('unknown option')
|
||||
})
|
||||
})
|
||||
|
||||
describe('terminal safety and privacy', () => {
|
||||
const ESC = '\u001b[2K'
|
||||
|
||||
it('escapes control characters in every untrusted string', () => {
|
||||
const report = minimalReport(sheet, {
|
||||
observations: [
|
||||
{
|
||||
id: `o${ESC}\n1`,
|
||||
zones: [],
|
||||
findings: [
|
||||
{
|
||||
findingZoneId: 'hip',
|
||||
path: ['hip-dysfunctions', `x${ESC}\ny`],
|
||||
labelFrSnapshot: `L${ESC}`,
|
||||
labelEnSnapshot: 'L',
|
||||
unresolved: true,
|
||||
},
|
||||
],
|
||||
freeTextFindings: [`t${ESC}\nu`],
|
||||
comment: `c${ESC}\nd\u009b\u202e\u2028`,
|
||||
},
|
||||
],
|
||||
})
|
||||
const lines = renderComparison('x', compareRaw(report, true))
|
||||
const text = lines.join('\n')
|
||||
expect(text).not.toContain('\u001b')
|
||||
expect(lines.every((l) => !l.includes('\n'))).toBe(true)
|
||||
expect(text).toContain('\\u001b')
|
||||
expect(text).toContain('\\u000a')
|
||||
expect(text).toContain('[o\\u001b[2K\\u000a1]')
|
||||
for (const raw of ['\u009b', '\u202e', '\u2028']) {
|
||||
expect(text).not.toContain(raw)
|
||||
}
|
||||
for (const escaped of ['\\u009b', '\\u202e', '\\u2028']) {
|
||||
expect(text).toContain(escaped)
|
||||
}
|
||||
})
|
||||
|
||||
it('does not throw on prototype-chain names used as legacy ids', () => {
|
||||
const v2: ReportV2 = {
|
||||
...minimalV2(),
|
||||
observations: [
|
||||
{
|
||||
id: 'o',
|
||||
zones: [{ boneId: '__proto__' }, { boneId: 'constructor' }],
|
||||
findings: [
|
||||
{
|
||||
zoneId: '__proto__',
|
||||
path: ['constructor'],
|
||||
labelFrSnapshot: 'X',
|
||||
labelEnSnapshot: 'X',
|
||||
},
|
||||
],
|
||||
freeTextFindings: [],
|
||||
comment: '',
|
||||
},
|
||||
],
|
||||
}
|
||||
const text = renderComparison('p.pdf', compareRaw(v2)).join('\n')
|
||||
expect(text).toContain(`__proto__ ${UNRESOLVED}`)
|
||||
expect(text).toContain(`constructor ${UNRESOLVED}`)
|
||||
expect(text).toContain(`__proto__/constructor "X" ${UNRESOLVED}`)
|
||||
})
|
||||
|
||||
it('hides names and free texts by default, shows them with --full', () => {
|
||||
const report = minimalReport(sheet, {
|
||||
conclusion: 'SECRET-CONCLUSION',
|
||||
recommendations: 'SECRET-RECO',
|
||||
nextAppointment: { kind: 'period', text: 'SECRET-WHEN' },
|
||||
observations: [
|
||||
{
|
||||
id: 'o',
|
||||
zones: [],
|
||||
findings: [],
|
||||
freeTextFindings: ['SECRET-TEXT'],
|
||||
comment: 'SECRET-COMMENT',
|
||||
},
|
||||
],
|
||||
})
|
||||
const hidden = renderComparison('x', compareRaw(report)).join('\n')
|
||||
expect(hidden).not.toContain('SECRET')
|
||||
expect(hidden).toContain('text (1 entries)')
|
||||
expect(hidden).toContain('comment (present)')
|
||||
expect(hidden).toContain('conclusion (present)')
|
||||
expect(hidden).toContain('recommendations (present)')
|
||||
expect(hidden).toContain('next appointment (period)')
|
||||
const shown = renderComparison('x', compareRaw(report, true)).join('\n')
|
||||
for (const word of ['TEXT', 'COMMENT', 'CONCLUSION', 'RECO', 'WHEN']) {
|
||||
expect(shown).toContain(`SECRET-${word}`)
|
||||
}
|
||||
expect(compareRaw(report).normalized.join('\n')).not.toContain('SECRET')
|
||||
expect(compareRaw(report, true).normalized.join('\n')).toContain(
|
||||
'SECRET-CONCLUSION',
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { ReportSchema } from '@faunosteo/domain'
|
||||
import { ReportSchema } from '@faunosteo/core-wasm/n-1'
|
||||
import { generateId } from '@/lib/id'
|
||||
import { todayIso, nowIso } from '@/lib/dates'
|
||||
import { assert } from '@/lib/assert'
|
||||
@@ -19,7 +19,7 @@ describe('skeleton smoke test', () => {
|
||||
expect(nowIso()).toMatch(/^\d{4}-\d{2}-\d{2}T/)
|
||||
})
|
||||
|
||||
it('ReportSchema parses a valid minimal v3 report', () => {
|
||||
it('the N-1 ReportSchema parses a valid minimal v3 report', () => {
|
||||
const result = ReportSchema.safeParse({
|
||||
version: 3,
|
||||
id: generateId(),
|
||||
@@ -36,7 +36,7 @@ describe('skeleton smoke test', () => {
|
||||
expect(result.success).toBe(true)
|
||||
})
|
||||
|
||||
it('ReportSchema rejects a report missing required fields', () => {
|
||||
it('the N-1 ReportSchema rejects a report missing required fields', () => {
|
||||
expect(ReportSchema.safeParse({ version: 1, id: '' }).success).toBe(false)
|
||||
})
|
||||
|
||||
|
||||
Reference in new issue
Block a user