Files
faunosteo/scripts/r4-j2-kit.mjs
T

153 lines
5.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// R4 J2 device acceptance kit (R4.6, the Rust export): gathers in one folder
// the archive PDFs the owner imports from a phone or a computer and then
// exports again with the Rust writer, so that the exported file can be
// compared with the reference of the same bilan (docs/development/R4_J2.md).
// 1-reference/ the R3 exports of the TypeScript engine (FR / EN, with and
// without logos, the real-raster export, the store path):
// import one, export it, compare the two files side by side.
// 2-large/ the L (60 observations, 120 views) and CAP (500 / 1,000)
// synthetic reports as archive PDFs written by the Rust
// session with 2 × 1 stub rasters (small files: the phone
// draws the real rasters when it exports them).
// Synthetic data only. Usage: node scripts/r4-j2-kit.mjs [out-dir]
// (default: a `faunosteo-j2-kit` folder next to the repository; the core
// module built: pnpm wasm:build).
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { join, resolve } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
const ROOT = resolve(fileURLToPath(new URL('..', import.meta.url)))
const FIXTURES = join(ROOT, 'apps/app/tests/fixtures')
const SEED = join(ROOT, 'supabase/seed/anatomical-sheets/equine/draft')
const PKG = join(ROOT, 'packages/core-wasm/pkg')
const OUT = resolve(process.argv[2] ?? join(ROOT, '..', 'faunosteo-j2-kit'))
const TWO_BY_ONE_PNG = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAIAAAB7QOjdAAAAD0lEQVR4nGPYEWX///9/AA0PBE/1T7nxAAAAAElFTkSuQmCC',
'base64',
)
const REFERENCE = [
'demo.fr.logos',
'demo.en.logos',
'demo.fr.nologos',
'v2full.fr.logos',
'v2unresolved.fr.logos',
'store-path.fr.nologos',
]
const read = (path) => readFileSync(path, 'utf8')
/** The bundled draft exactly as `apps/app/vite/sheet-bundle.ts` embeds it. */
function draftBundle() {
const manifest = JSON.parse(read(join(SEED, 'assets.json')))
const assets = {}
const assetHashes = {}
for (const asset of manifest) {
assets[asset.svg_asset_id] = read(join(SEED, 'assets', asset.file))
assetHashes[asset.svg_asset_id] = asset.sha256
}
return JSON.stringify({
status: 'draft',
ref: JSON.parse(read(join(SEED, 'sheet-ref.json'))),
document: JSON.parse(read(join(SEED, 'document.json'))),
migrationMap: JSON.parse(read(join(SEED, 'report-migration-map.json'))),
assets,
assetHashes,
})
}
/** The shipped module, instantiated once. */
async function loadGlue() {
const glue = await import(pathToFileURL(join(PKG, 'faunosteo_wasm.js')).href)
const module = await WebAssembly.compile(
readFileSync(join(PKG, 'faunosteo_wasm_bg.wasm')),
)
await glue.default({ module_or_path: module })
return glue
}
function unwrap(text, what) {
const outcome = JSON.parse(text)
if (!outcome.ok) throw new Error(`${what}: ${outcome.kind} ${outcome.detail ?? ''}`)
return outcome.value
}
function copy(from, toDir, name) {
mkdirSync(toDir, { recursive: true })
writeFileSync(join(toDir, name), readFileSync(from))
}
/** The synthetic report in the demo export's envelope, exported by the session. */
function largeArchive(glue, demo, name) {
const report = JSON.parse(read(join(FIXTURES, `r4/reports/${name}.json`)))
report.sheet = demo.report.sheet
const input = JSON.stringify({
report,
sheetSnapshot: demo.sheetSnapshot,
branding: demo.branding,
labels: demo.labels,
locale: 'fr',
exportedAt: new Date().toISOString(),
})
const session = new glue.ExportSession(input)
try {
for (;;) {
const step = unwrap(session.next_step(), 'next')
if (step.step === 'done') break
for (const request of step.requests) {
unwrap(session.add_raster(request.key, TWO_BY_ONE_PNG), request.key)
}
}
const count = session.finish()
const parts = []
for (let i = 0; i < count; i += 1) parts.push(session.take_part())
return Buffer.concat(parts)
} finally {
session.free()
}
}
const lines = [
'# Kit J2 (R4.6) — fichiers de la recette de l’export Rust',
'',
'## 1-reference/ (exports du moteur TypeScript, R3)',
'',
'Importer le fichier depuis l’accueil, puis « Exporter en PDF » dans l’éditeur :',
'le fichier exporté se compare au fichier importé (mêmes pages, mêmes textes,',
'mêmes images, mêmes liens ; seule la date d’export change).',
'',
]
for (const name of REFERENCE) {
copy(join(FIXTURES, `pdf/r3/${name}.pdf`), join(OUT, '1-reference'), `r3-${name}.pdf`)
lines.push(`- \`r3-${name}.pdf\``)
}
copy(
join(FIXTURES, 'pdf/r3/real/v2full-obs1.fr.logos.pdf'),
join(OUT, '1-reference'),
'r3-real.v2full-obs1.fr.logos.pdf',
)
lines.push('- `r3-real.v2full-obs1.fr.logos.pdf` (rasters réels du navigateur)', '')
lines.push(
'## 2-large/ (rapports synthétiques, écrits par la session Rust)',
'',
'Importer, puis exporter : le téléphone dessine lui-même les 120 (L) ou 1 000 (CAP)',
'vues. L doit aboutir sur le téléphone (E4) ; CAP est réservé à l’ordinateur.',
'',
)
const glue = await loadGlue()
const sheet = new glue.SheetHandle(draftBundle())
const demo = unwrap(
sheet.import_pdf(new Uint8Array(readFileSync(join(FIXTURES, 'pdf/r3/demo.fr.logos.pdf')))),
'demo',
).read.archive
for (const name of ['L', 'CAP']) {
const pdf = largeArchive(glue, demo, name)
mkdirSync(join(OUT, '2-large'), { recursive: true })
writeFileSync(join(OUT, '2-large', `${name}.fr.logos.pdf`), pdf)
lines.push(`- \`${name}.fr.logos.pdf\` (${(pdf.byteLength / 1024).toFixed(0)} Ko)`)
}
lines.push('')
writeFileSync(join(OUT, 'ATTENDU.md'), `${lines.join('\n')}\n`)
console.log(`kit written to ${OUT}`)