fix(pdf,tests): report:compare escapes control characters, prints names and free text only with --full, stats before read, missing file as a per-file failure; inferRegionCode regression test; import.ts JSDoc reattached

This commit is contained in:
bchanot committed 2026-10-10 15:53:07 +02:00
1 parent dc78a00835
commit b1b3e4927e
6 files changed
+257 -72

No files matched your search

+32 -20
View File
@@ -2,14 +2,17 @@
* Archive check: reads the report attached to one or more Faunosteo PDFs and
* prints it next to its v3 reading on the bundled reference sheet.
*
* pnpm report:compare <file.pdf | folder>... [--summary] [--identity]
* pnpm report:compare <file.pdf | folder>... [--summary] [--full]
* [--locale fr|en] [--diff a.pdf b.pdf]
*
* A v2 archive PDF is shown legacy id → canonical id, line by line; a v3 PDF
* is checked against the sheet. Items the sheet cannot place are marked
* `!! UNRESOLVED` with the reason. `--diff` compares the v3 content of two
* PDFs (an archive and its re-export after import, typically). Names are
* printed only with `--identity`. Reads only; never writes a file.
* PDFs (an archive and its re-export after import, typically) and prints a
* differing text line in clear. By default only identifiers, catalog labels,
* counts and placeholders are printed; names and free texts need `--full`.
* Untrusted strings are printed with control characters escaped. It writes
* no output file.
*/
import { readFileSync, readdirSync, statSync } from 'node:fs'
import { basename, join, resolve } from 'node:path'
@@ -27,6 +30,7 @@ import { buildSheetIndex, type SheetIndex } from '../src/services/sheetIndex'
import {
compareReport,
diffLines,
esc,
parseArgs,
renderComparison,
renderSummary,
@@ -56,18 +60,18 @@ function loadSheet(): SheetIndex {
)
}
function expandFiles(args: string[]): string[] {
const out: string[] = []
for (const arg of args) {
const path = resolve(CWD, arg)
if (statSync(path).isDirectory()) {
const pdfs = readdirSync(path)
.filter((f) => f.toLowerCase().endsWith('.pdf'))
.sort()
out.push(...pdfs.map((f) => join(path, f)))
} else out.push(path)
/** A folder stands for its PDFs; any other argument stays one entry. */
function expandArg(arg: string): string[] {
const path = resolve(CWD, arg)
try {
if (!statSync(path).isDirectory()) return [path]
} catch {
return [path] // reported as a failed entry by compareFile
}
return out
return readdirSync(path)
.filter((f) => f.toLowerCase().endsWith('.pdf'))
.sort()
.map((f) => join(path, f))
}
async function compareFile(
@@ -75,8 +79,8 @@ async function compareFile(
sheet: SheetIndex,
args: CliArgs,
): Promise<Comparison> {
if (statSync(path).size > MAX_PDF_BYTES) throw new Error('file too large')
const bytes = new Uint8Array(readFileSync(path))
if (bytes.byteLength > MAX_PDF_BYTES) throw new Error('file too large')
const pdf = await PDFDocument.load(bytes, { ignoreEncryption: true })
const raw = await readAttachedDocument(pdf)
const version = (raw as { version: number }).version
@@ -91,12 +95,20 @@ async function compareFile(
}
}
/** A missing or unreadable file is a failed entry, not a crash. */
function failureReason(err: unknown): string {
const code = (err as NodeJS.ErrnoException | undefined)?.code
if (code === 'ENOENT') return 'file not found'
if (code === 'EACCES') return 'file not readable'
return err instanceof Error ? err.message : String(err)
}
async function main(argv: string[]): Promise<number> {
const args = parseArgs(argv)
const files = expandFiles(args.files)
const files = args.files.flatMap(expandArg)
if (files.length === 0 || (args.diff && files.length !== 2)) {
console.error(
'usage: pnpm report:compare <file.pdf | folder>... [--summary] [--identity] [--locale fr|en] [--diff a.pdf b.pdf]',
'usage: pnpm report:compare <file.pdf | folder>... [--summary] [--full] [--locale fr|en] [--diff a.pdf b.pdf]',
)
return 1
}
@@ -104,7 +116,7 @@ async function main(argv: string[]): Promise<number> {
const results: Comparison[] = []
let failures = 0
for (const file of files) {
const name = basename(file)
const name = esc(basename(file))
try {
const c = await compareFile(file, sheet, args)
results.push(c)
@@ -116,7 +128,7 @@ async function main(argv: string[]): Promise<number> {
}
} catch (err) {
failures += 1
console.log(`!! ${name}: ${err instanceof Error ? err.message : err}`)
console.log(`!! ${name}: ${esc(failureReason(err))}`)
}
}
if (args.diff && results.length === 2) {
@@ -129,7 +141,7 @@ async function main(argv: string[]): Promise<number> {
main(process.argv.slice(2)).then(
(code) => process.exit(code),
(err) => {
console.error(err instanceof Error ? err.message : err)
console.error(esc(err instanceof Error ? err.message : String(err)))
process.exit(1)
},
)