// The application's import, TypeScript reader (R4.1) vs Rust import worker // (R4.3): two results files of import-baseline.mjs, so the same Home page, // files, groups, engines, RSS metrics and watchdog; only the build differs. // Per engine and group: outcome and message agreement, median Node-clock // durations, max summed and content RSS growth, the longest Chromium long // task (W5: none over 50 ms). Per file, the outcome (editor, inline error) // and the message shown (the alert lines after an import, the inline error // text) must be identical, except the declared differences (plan §A.3: // OD-3, OD-4), each expected with its own message in every engine. Any // other difference is printed as NOT DECLARED and the exit code is 1. The // `read` group's files of 10 MiB and more get their own table. // // node scripts/browser/compare-app-imports.mjs import { readFileSync } from 'node:fs' import { join } from 'node:path' import { DECLARED, messageKeys, slugOf } from './compare-imports.mjs' import { inputBytes } from './lib/import-inputs.mjs' import { REPO_ROOT } from './lib/paths.mjs' import { median } from './lib/stats.mjs' const MiB = 1024 * 1024 const LARGE_BYTES = 10 * MiB const FIXTURES = 'apps/app/tests/fixtures/' /** The message each declared file shows on the Rust side (plan §B.1.8). */ const DECLARED_KEYS = { 'values-1m': 'error_attachment_stripped', 'surrogate-content-overwritten': 'error_lone_surrogate', 'surrogate-version-overwritten': 'error_lone_surrogate', 'two-files-decoy-first': 'error_lone_surrogate', } const orDash = (format) => (value) => value === null || value === undefined ? '–' : format(value) const mb = orDash((bytes) => (bytes / MiB).toFixed(1)) const ms = orDash((value) => value.toFixed(0)) const shortFile = (file) => file.replace(FIXTURES, '') const short = (text) => text.replace(/\s+/g, ' ').slice(0, 60) function table(header, rows) { const line = (cells) => `| ${cells.join(' | ')} |` const rule = line(header.map(() => '---')) return [line(header), rule, ...rows.map(line)].join('\n') } /** What the practitioner sees: the outcome and the message, keyed. */ function verdictOf(run, keys) { const keyOf = (text) => keys.find((k) => k.re.test(text))?.key ?? '?' if (run.outcome === 'editor' || run.outcome === 'dialog') { const message = (run.dialogs ?? []).join(' | ') const label = message ? `${run.outcome} «${short(message)}»` : run.outcome return { verdict: run.outcome, message, label } } if (run.outcome === 'error' && run.errorText) { const key = keyOf(run.errorText) const label = `error ${key}` return { verdict: 'error', message: run.errorText, label, key } } const message = run.error ?? '' return { verdict: `harness ${run.outcome}`, message, label: run.outcome } } /** One row per (engine, file) present on either side. */ function pairs(tsRuns, rustRuns, keys) { const index = (runs) => new Map(runs.map((r) => [`${r.engine} ${r.file}`, r])) const ts = index(tsRuns) const rust = index(rustRuns) return [...new Set([...ts.keys(), ...rust.keys()])].map((id) => { const t = ts.get(id) const r = rust.get(id) const run = t ?? r return { engine: run.engine, group: run.group, file: run.file, ts: t ?? null, rust: r ?? null, tsV: t ? verdictOf(t, keys) : null, rustV: r ? verdictOf(r, keys) : null, } }) } const sameOutcome = (p) => p.tsV && p.rustV && p.tsV.verdict === p.rustV.verdict const sameMessage = (p) => sameOutcome(p) && p.tsV.message === p.rustV.message const growth = (r) => r.rssPeakBytes - r.rssBeforeBytes const contentGrowth = (r) => r.rssPeakContentBytes - r.rssBeforeContentBytes const finite = (list) => list.filter(Number.isFinite) const maxOf = (list) => (finite(list).length ? Math.max(...finite(list)) : null) const longest = (runs) => maxOf(runs.map((r) => r.longTasks?.maxMs ?? null)) const pair = (a, b) => `${mb(a)} / ${mb(b)}` function groupRow([key, list]) { const [engine, group] = key.split(' ') const ts = list.map((p) => p.ts).filter(Boolean) const rust = list.map((p) => p.rust).filter(Boolean) const withLongTask = rust.filter((r) => r.longTasks?.count > 0).length return [ engine, group, String(list.length), String(list.filter(sameOutcome).length), String(list.filter(sameMessage).length), ms(median(finite(ts.map((r) => r.durationMs)))), ms(median(finite(rust.map((r) => r.durationMs)))), pair(maxOf(ts.map(growth)), maxOf(ts.map(contentGrowth))), pair(maxOf(rust.map(growth)), maxOf(rust.map(contentGrowth))), ms(longest(ts)), ms(longest(rust)), engine === 'chromium' ? String(withLongTask) : '–', ] } function groupTable(rows) { const groups = new Map() for (const p of rows) { const key = `${p.engine} ${p.group}` groups.set(key, [...(groups.get(key) ?? []), p]) } const header = [ 'Engine', 'Group', 'Files', 'Same outcome', 'Same message', 'TS median ms', 'Rust median ms', 'TS max growth summed / content MiB', 'Rust max growth summed / content MiB', 'TS longest long task ms', 'Rust longest long task ms', 'Rust runs with a long task', ] return table(header, [...groups].map(groupRow)) } /** The `read` files of 10 MiB and more: TS beside Rust, per engine. */ function largeTable(rows) { const large = rows.filter( (p) => p.group === 'read' && inputBytes(join(REPO_ROOT, p.file)) >= LARGE_BYTES, ) const header = [ 'File', 'MiB', 'Engine', 'TS outcome', 'Rust outcome', 'TS ms', 'Rust ms', 'TS growth summed / content MiB', 'Rust growth summed / content MiB', ] const cells = (p) => [ shortFile(p.file).replace('pdf/r4/read/', ''), mb(inputBytes(join(REPO_ROOT, p.file))), p.engine, p.tsV?.label ?? 'not run', p.rustV?.label ?? 'not run', ms(p.ts?.durationMs), ms(p.rust?.durationMs), p.ts ? pair(growth(p.ts), contentGrowth(p.ts)) : '–', p.rust ? pair(growth(p.rust), contentGrowth(p.rust)) : '–', ] return table(header, large.map(cells)) } /** Every outcome or message difference, grouped by file, declared apart. */ function differences(rows) { const byFile = new Map() for (const p of rows.filter((row) => !sameMessage(row))) { const what = `TS ${p.tsV?.label ?? 'not run'} / Rust ${p.rustV?.label ?? 'not run'}` + (sameOutcome(p) ? ' (same outcome, message differs)' : '') const key = `${p.file}\u0000${what}` byFile.set(key, [...(byFile.get(key) ?? []), p.engine]) } return [...byFile].map(([key, engines]) => { const [file, what] = key.split('\u0000') const declared = DECLARED[slugOf(file)] const tag = declared ? `declared (${declared})` : 'NOT DECLARED' const line = `- ${shortFile(file)} [${engines.join(', ')}]: ${what}: ${tag}` return { line, declared } }) } /** Each declared file, in every engine: TS editor, Rust its own message. */ function declaredCheck(rows, engines) { return Object.entries(DECLARED_KEYS).map(([slug, key]) => { const cells = engines.map((engine) => { const p = rows.find((r) => r.engine === engine && slugOf(r.file) === slug) if (!p?.tsV || !p.rustV) return { ok: false, text: `${engine}: missing` } const ok = p.tsV.verdict === 'editor' && p.rustV.key === key const text = `${engine}: TS ${p.tsV.label} / Rust ${p.rustV.label}` return { ok, text: ok ? text : `${text} NOT AS DECLARED` } }) const ok = cells.every((c) => c.ok) const line = `- ${slug} (expected ${key}): ${cells.map((c) => c.text).join('; ')}` return { ok, line } }) } function perimeterLines(rows) { const only = (side, list) => list.map((p) => `- only in ${side}: ${p.engine} ${shortFile(p.file)}`) return [ ...only( 'TS', rows.filter((p) => !p.rust), ), ...only( 'Rust', rows.filter((p) => !p.ts), ), ] } function headLines(ts, rust, rows) { const versions = (r) => Object.entries(r.engines) .map(([e, v]) => `${e} ${v}`) .join(', ') const build = (r) => `build ${r.build?.headCommit ?? '?'} (${r.build?.builtAt ?? '?'}, ` + `${r.build?.wasm?.name ?? 'no wasm'})` return [ `TS app: started ${ts.startedAt}, ${versions(ts)}, ${build(ts)}.`, `Rust app: started ${rust.startedAt}, ${versions(rust)}, ${build(rust)}.`, `Watchdog: ${rust.watchdog.caveat}`, `Same outcome: ${rows.filter(sameOutcome).length} of ${rows.length}; ` + `same outcome and message: ${rows.filter(sameMessage).length}.`, ] } function readResults([tsPath, rustPath]) { if (!tsPath || !rustPath) { throw new Error( 'usage: compare-app-imports.mjs ', ) } const read = (path) => JSON.parse(readFileSync(path, 'utf8')) return { ts: read(tsPath), rust: read(rustPath) } } function comparison(ts, rust, rows) { const engines = [...new Set(rows.map((p) => p.engine))] const diffs = differences(rows) const undeclared = diffs.filter((d) => !d.declared) const declared = declaredCheck(rows, engines) const failedDeclared = declared.filter((d) => !d.ok) const perimeter = perimeterLines(rows) const parts = [ ...headLines(ts, rust, rows), 'TS app vs Rust app by engine and group:', groupTable(rows), 'Large files of the read group (10 MiB and more):', largeTable(rows), perimeter.length ? `PERIMETER DIFFERS (${perimeter.length}):\n${perimeter.join('\n')}` : 'Perimeter: every (engine, file) pair is on both sides.', `Differences (${diffs.length}; not declared: ${undeclared.length}):`, diffs.map((d) => d.line).join('\n') || 'none', 'Declared differences, per engine:', declared.map((d) => d.line).join('\n'), ] const failures = undeclared.length + failedDeclared.length + perimeter.length if (failures) { parts.push( `WARNING: ${undeclared.length} undeclared difference(s), ` + `${failedDeclared.length} declared file(s) not as declared, ` + `${perimeter.length} perimeter difference(s).`, ) } return { text: parts.join('\n\n'), failures } } const { ts, rust } = readResults(process.argv.slice(2)) const rows = pairs(ts.import.runs, rust.import.runs, await messageKeys()) const { text, failures } = comparison(ts, rust, rows) console.log(text) if (failures) process.exitCode = 1