Files
faunosteo/scripts/browser/lib/page-probes.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

145 lines
4.5 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()
}
/** 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 }
}