/** * 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, /** R4.6: the `faunosteo:export` User Timing entry's detail (the Rust driver's split). */ exportTiming: 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 startedAt = now() const record = (blob) => { probe.png.push({ ...size, bytes: blob ? blob.size : null, startedAt, at: now(), }) 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), with the export timing entry if any. */ export const readProbe = () => { const probe = window.__harness const entry = performance.getEntriesByName('faunosteo:export').at(-1) if (entry) { probe.exportTiming = { startTime: entry.startTime, duration: entry.duration, ...(entry.detail ?? {}), } } return probe } /** * 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 } }