Files
faunosteo/scripts/browser/import-baseline.mjs
T
bchanot 4439fd2420 test(tests): R4.1e browser harness and TS baselines (Chromium, Firefox, WebKit)
Playwright harness under scripts/browser: 127.0.0.1 server with the DEPLOY
CSP, export and import baselines of the TypeScript engine under a 4 GiB RSS
watchdog (not a guaranteed memory limit), O9 browser columns in
inflate.browsers.json (26 rows agree with Node in all three engines).
Results of 2026-10-18 committed; the O9 Node test no longer expects browser
columns in inflate.json.
2026-10-18 15:05:28 +02:00

273 lines
9.0 KiB
JavaScript

// Import baseline of the TypeScript reader (R4.1e, plan O12, §B.3): for each
// file, a fresh context opens Home and sets the file on the import input;
// the run ends at the first of: navigation to /editor, an alert, the inline
// import error. Records the duration, the alert and error texts, the final
// URL, the browser's summed RSS (before, peak) and, in Chromium, the main
// thread's long tasks. One browser per engine, relaunched after a stop.
//
// PLAYWRIGHT_NODE_MODULES=... node scripts/browser/import-baseline.mjs \
// [--engines chromium,firefox,webkit] [--out <file>]
import {
existsSync,
mkdtempSync,
readFileSync,
rmdirSync,
unlinkSync,
writeFileSync,
} from 'node:fs'
import { tmpdir } from 'node:os'
import { basename, join } from 'node:path'
import { pathToFileURL } from 'node:url'
import {
delay,
firstLine,
isStop,
tagged,
timeoutAfter,
untilStopped,
} from './lib/async.mjs'
import { runHarnessCli } from './lib/harness.mjs'
import { expandGenerator, filesUnder, isGenerator } from './lib/inputs.mjs'
import { FIXTURES_DIR, ORACLES_R4_DIR, relativeToRepo } from './lib/paths.mjs'
import { probedPage, readProbe } from './lib/page-probes.mjs'
import { closeEngine, launchEngine } from './lib/playwright.mjs'
import { sum } from './lib/stats.mjs'
const IMPORT_TIMEOUT_MS = 120_000
const SETTLE_MS = 5000
const PICK_LABEL = 'Choisir un fichier' // fr.ts home.import.cta
const IMPORT_ERROR = 'input[type="file"] ~ p.text-red-700'
const READ_DIR = join(FIXTURES_DIR, 'pdf/r4/read')
const isPdf = (path) => path.endsWith('.pdf')
const BASE_FILES = [
'report-v2.full.pdf',
'report-v2.minimal.pdf',
'report-v3.bare.pdf',
]
function pdfsIn(group, dir, recursive = false) {
const paths = filesUnder(join(FIXTURES_DIR, dir), recursive).filter(isPdf)
return paths.map((path) => ({ group, path }))
}
function fixedGroups() {
return [
...BASE_FILES.map((f) => ({ group: 'base', path: join(FIXTURES_DIR, f) })),
...pdfsIn('r3', 'pdf/r3', true),
...pdfsIn('step6', 'pdf/step6'),
...pdfsIn('tolerance', 'pdf/r4/tolerance'),
]
}
/** The hostile set as read.json (O7) lists it; absent listed files reported. */
function hostileGroup() {
const oraclePath = join(ORACLES_R4_DIR, 'read.json')
if (!existsSync(READ_DIR)) {
return { inputs: [], missing: ['pdf/r4/read/'], listedBy: null }
}
const listed = existsSync(oraclePath)
? JSON.parse(readFileSync(oraclePath, 'utf8')).inputs
: filesUnder(READ_DIR).map((path) => ({ file: basename(path), kind: null }))
const present = listed.filter((i) => existsSync(join(READ_DIR, i.file)))
const missing = listed.filter((i) => !present.includes(i)).map((i) => i.file)
const inputs = present.map((i) => ({
group: 'read',
kind: i.kind,
path: join(READ_DIR, i.file),
}))
return {
inputs,
missing,
listedBy: existsSync(oraclePath) ? relativeToRepo(oraclePath) : 'directory',
}
}
/** A generator description becomes a temporary file, removed after the run. */
function materialise(input, scratch) {
if (!isGenerator(input.path)) {
return { path: input.path, cleanup: () => undefined }
}
const path = join(scratch, `${basename(input.path, '.gen.json')}.pdf`)
writeFileSync(path, expandGenerator(input.path))
return { path, cleanup: () => unlinkSync(path) }
}
async function openHome(session, baseUrl) {
const context = await session.browser.newContext({ locale: 'fr-FR' })
const seed = { draft: null, locale: 'fr', skipDisclaimer: false }
const { page, events } = await probedPage(context, seed)
await page.goto(`${baseUrl}/`)
const pick = page.getByRole('button', { name: PICK_LABEL })
await pick.and(page.locator(':enabled')).waitFor()
return { context, page, events }
}
/** The first end of an import; the waits start before the file is set. */
function firstEnd(tab, path) {
const { page, events } = tab
const ends = Promise.race([
tagged('editor', page.waitForURL('**/editor', { timeout: 0 })),
tagged('dialog', events.firstDialog),
tagged('error_text', page.locator(IMPORT_ERROR).waitFor({ timeout: 0 })),
timeoutAfter(IMPORT_TIMEOUT_MS),
])
const set = page.setInputFiles('input[type="file"]', path)
return tagged('set', set).then((r) => (r.kind === 'error' ? r : ends))
}
/** After an alert the app may still navigate or show its error. */
async function settle(tab, first) {
if (first.kind !== 'dialog') return delay(300)
const { page } = tab
await Promise.race([
tagged('editor', page.waitForURL('**/editor', { timeout: SETTLE_MS })),
tagged(
'error_text',
page.locator(IMPORT_ERROR).waitFor({ timeout: SETTLE_MS }),
),
])
}
function longTasksOf(probe, since) {
if (!probe.longTaskSupported) return null
const tasks = probe.longTasks.filter((t) => t.start >= since).map((t) => t.ms)
return {
count: tasks.length,
maxMs: tasks.length ? Math.max(...tasks) : 0,
totalMs: sum(tasks),
}
}
/** Page clock: the input's change event to the first alert or navigation. */
function pageMsOf(probe) {
const ends = [probe.alertAt, probe.navAt].filter((t) => t !== null)
if (probe.changeAt === null || ends.length === 0) return null
return Math.min(...ends) - probe.changeAt
}
async function finalState(tab, since) {
const { page, events } = tab
const errorText = await page
.locator(IMPORT_ERROR)
.textContent({ timeout: 300 })
.catch(() => null)
const probe = await page.evaluate(readProbe)
return {
finalPath: new URL(page.url()).pathname,
errorText,
dialogs: events.dialogs.map((d) => d.message),
pageMs: pageMsOf(probe),
longTasks: longTasksOf(probe, since),
cspViolations: probe.csp,
pageErrors: events.pageErrors,
}
}
function resultOf(final) {
if (final.finalPath === '/editor') return 'editor'
if (final.errorText) return 'error'
return final.dialogs.length ? 'dialog' : 'none'
}
/** Before the file was set, and the peak until the import's first end. */
function rssOf(before, after) {
return {
rssBeforeBytes: before.currentBytes,
rssBeforeContentBytes: before.currentContentBytes,
rssPeakBytes: after.peakBytes,
rssPeakContentBytes: after.peakContentBytes,
}
}
async function measureImport(session, tab, path) {
const since = await tab.page.evaluate(() => performance.now())
const before = session.watchdog.snapshot()
await session.watchdog.resetPeak()
const start = performance.now()
const first = await untilStopped(session, firstEnd(tab, path))
const rss = rssOf(before, session.watchdog.snapshot())
const head = { firstEnd: first.kind, durationMs: first.at - start, ...rss }
if (isStop(first) || first.kind === 'timeout' || first.kind === 'error') {
return { outcome: first.kind, error: first.error ?? null, ...head }
}
await settle(tab, first)
const final = await finalState(tab, since)
return { outcome: resultOf(final), ...head, ...final }
}
async function importRun(session, baseUrl, input, scratch) {
const file = materialise(input, scratch)
const head = {
group: input.group,
kind: input.kind ?? null,
file: relativeToRepo(input.path),
}
let tab = null
try {
tab = await openHome(session, baseUrl)
return { ...head, ...(await measureImport(session, tab, file.path)) }
} catch (error) {
const outcome = session.watchdog.snapshot().stopped
? 'stopped_by_ceiling'
: 'error'
return { ...head, outcome, error: firstLine(error) }
} finally {
await tab?.context.close().catch(() => undefined)
file.cleanup()
}
}
async function importEngine(pw, { engine, baseUrl, inputs, scratch, log }) {
let session = await launchEngine(pw, engine)
const runs = []
try {
for (const input of inputs) {
const run = await importRun(session, baseUrl, input, scratch)
runs.push({ engine, engineVersion: session.version, ...run })
const ms = Math.round(run.durationMs ?? 0)
log(`import ${engine} ${run.file}: ${run.outcome} ${ms} ms`)
if (run.outcome === 'watchdog_failed') throw new Error('watchdog failed')
if (run.outcome !== 'stopped_by_ceiling') continue
await closeEngine(session)
session = await launchEngine(pw, engine)
}
} finally {
await closeEngine(session)
}
return runs
}
export async function runImportBaseline(context) {
const { pw, baseUrl, engines, filters, log } = context
const hostile = hostileGroup()
const wanted = (i) => !filters.groups || filters.groups.includes(i.group)
const inputs = [...fixedGroups(), ...hostile.inputs].filter(wanted)
const scratch = mkdtempSync(join(tmpdir(), 'faunosteo-import-'))
const runs = []
try {
for (const engine of engines) {
runs.push(
...(await importEngine(pw, { engine, baseUrl, inputs, scratch, log })),
)
}
} finally {
rmdirSync(scratch)
}
return {
timeoutMs: IMPORT_TIMEOUT_MS,
files: inputs.length,
hostile: {
listedBy: hostile.listedBy,
present: hostile.inputs.length,
missing: hostile.missing,
},
runs,
}
}
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
await runHarnessCli({ import: runImportBaseline })
}