Files
faunosteo/scripts/browser/lib/page-probes.mjs
T
bchanot 08f01ef363 test(tests): R4.3d browser proofs on the production build: app import baseline on the Rust path, CSP with and without the keyword, leak series, WebKit repetition
Same perimeter as the TS baseline (226 files, 3 engines, real Home page): 666/678 outcome
and message agreements, the 12 others the 4 declared files; W5 0 long tasks (TS 91 and
173 ms); 50 MiB file in WebKit 145/126 MiB against 324 with the worker read and 76 for the
TS. CSP: editor opens with the keyword, the engine message without, in 3 engines, the
worker's wasm-eval refusal reported by Chromium and Firefox. Leak series on the app:
Chromium ≤ 2 MiB per import after init, Firefox GC sawtooth reclaimed, WebKit grows with
same-tab navigations (control too) and levels off on one page; 20/20 WebKit workers closed.
2026-10-18 19:48:31 +02:00

160 lines
5.0 KiB
JavaScript

/**
* Init scripts (run in the page before its own scripts). They record what
* the measures need and call through to every function they wrap, so the
* application behaves as without them. Each function is serialised by
* Playwright: it must not reference anything outside its own body.
*/
/** Seeds the tab as the app leaves it (persistence.ts, EditorRoute). */
export function seedTab({ draft, locale, skipDisclaimer }) {
if (location.origin === 'null') return
if (draft !== null && sessionStorage.getItem('osteo:draft') === null) {
sessionStorage.setItem('osteo:draft', draft)
}
if (skipDisclaimer) sessionStorage.setItem('osteo:disclaimer-shown', '1')
localStorage.setItem('osteo:locale', locale)
}
export function installProbes() {
const probe = {
png: [],
dataUrls: 0,
clickAt: null,
pdfBlobAt: null,
changeAt: null,
navAt: null,
alertAt: null,
longTaskSupported: false,
longTasks: [],
csp: [],
}
window.__harness = probe
const now = () => performance.now()
// Every PNG the browser rasteriser encodes goes through toBlob.
function wrapCanvas() {
const proto = HTMLCanvasElement.prototype
const toBlob = proto.toBlob
proto.toBlob = function (callback, ...rest) {
const size = { width: this.width, height: this.height }
const record = (blob) => {
probe.png.push({ ...size, bytes: blob ? blob.size : null })
callback(blob)
}
return toBlob.call(this, record, ...rest)
}
const toDataURL = proto.toDataURL
proto.toDataURL = function (...args) {
probe.dataUrls += 1
return toDataURL.apply(this, args)
}
}
// download.ts hands the finished PDF to URL.createObjectURL.
function wrapObjectUrl() {
const create = URL.createObjectURL
URL.createObjectURL = function (object) {
if (object instanceof Blob && object.type === 'application/pdf') {
probe.pdfBlobAt = now()
}
return create.call(URL, object)
}
}
// HomeRoute ends an import with an alert, a navigation, or both.
function wrapNavigationAndAlert() {
const push = history.pushState
history.pushState = function (...args) {
if (probe.navAt === null) probe.navAt = now()
return push.apply(this, args)
}
const alert = window.alert
window.alert = function (message) {
if (probe.alertAt === null) probe.alertAt = now()
return alert.call(window, message)
}
}
function observeLongTasks() {
const types = PerformanceObserver.supportedEntryTypes ?? []
if (!types.includes('longtask')) return
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
probe.longTasks.push({ start: entry.startTime, ms: entry.duration })
}
})
observer.observe({ type: 'longtask' })
probe.longTaskSupported = true
}
function listenToEvents() {
// The first click only: download.ts clicks its own anchor afterwards.
window.addEventListener(
'click',
() => {
if (probe.clickAt === null) probe.clickAt = now()
},
true,
)
window.addEventListener(
'change',
(e) => {
if (e.target?.type === 'file' && probe.changeAt === null) {
probe.changeAt = now()
}
},
true,
)
document.addEventListener('securitypolicyviolation', (e) =>
probe.csp.push(`${e.effectiveDirective} ${e.blockedURI}`),
)
}
wrapCanvas()
wrapObjectUrl()
wrapNavigationAndAlert()
observeLongTasks()
listenToEvents()
}
/**
* Counts the inline import error lines HomeRoute inserts (`window.__errorLines`):
* a new pick removes the line, its end adds it again, so several picks on one
* page each end with one more insertion, even when the text repeats.
*/
export function countErrorLines() {
window.__errorLines = 0
const isLine = (n) => n.nodeType === 1 && n.matches('p.text-red-700')
new MutationObserver((records) => {
for (const r of records) {
window.__errorLines += [...r.addedNodes].filter(isLine).length
}
}).observe(document.body, { childList: true, subtree: true })
}
/** Reads the probe back (a structured-clone copy). */
export const readProbe = () => window.__harness
/**
* A page of a fresh context with the probes and the tab seed, dialogs
* accepted and recorded (the app's alerts block its script until then).
*/
export async function probedPage(context, seed) {
await context.addInitScript(seedTab, seed)
await context.addInitScript(installProbes)
const page = await context.newPage()
const events = { dialogs: [], pageErrors: [], consoleErrors: [] }
let firstDialog
events.firstDialog = new Promise((done) => (firstDialog = done))
page.on('dialog', (dialog) => {
events.dialogs.push({ type: dialog.type(), message: dialog.message() })
firstDialog(dialog.message())
dialog.accept().catch(() => undefined)
})
page.on('pageerror', (e) => events.pageErrors.push(String(e.message)))
page.on('console', (m) => {
if (m.type() === 'error') events.consoleErrors.push(m.text().slice(0, 300))
})
return { page, events }
}