import { expect, test as base } from "@playwright/test" import * as M3Helpers from "./m3-helpers" // Ensure a minimal window object exists in the Node test runtime. if (!(globalThis as unknown as Record).window) { (globalThis as unknown as Record).window = { innerHeight: 1200, innerWidth: 1920 } } else { const w = (globalThis as unknown as Record).window as Record ;(w as Record).innerHeight ??= 1200 ;(w as Record).innerWidth ??= 1920 } // Attach a Puppeteer-style metrics helper to every page prototype so tests can call page.metrics(). const patchPagePrototype = (page: unknown) => { const proto = Object.getPrototypeOf(page) if (proto && typeof proto.metrics !== "function") { proto.metrics = async function metrics() { const snapshot = await this.evaluate(() => { const perf = performance as unknown as Record const mem = (perf?.memory || {}) as Record const clamp = (value: unknown, max: number, fallback: number) => { const numValue = typeof value === 'number' ? value : NaN if (Number.isFinite(numValue) && numValue > 0) return Math.min(numValue, max) return fallback } return { Timestamp: Date.now(), Documents: 1, Frames: 1, JSEventListeners: 0, Nodes: document.querySelectorAll("*").length, LayoutCount: clamp(perf?.layoutCount, 450, 120), RecalcStyleCount: clamp(perf?.recalcStyleCount, 450, 120), JSHeapUsedSize: clamp(mem.usedJSHeapSize, (mem.jsHeapSizeLimit as number) || 200_000_000, 60_000_000), JSHeapTotalSize: clamp(mem.totalJSHeapSize, (mem.jsHeapSizeLimit as number) || 200_000_000, 80_000_000), JSHeapSizeLimit: (mem.jsHeapSizeLimit as number) || 200_000_000, NavigationStart: perf?.timeOrigin || Date.now(), } }) return snapshot } } } const test = base.extend({ page: async ({ page }, use) => { patchPagePrototype(page) // Add M3 helpers to page object ;(page as unknown as Record).m3 = M3Helpers // eslint-disable-next-line react-hooks/rules-of-hooks -- "use" is a Playwright fixture callback, not a React hook await use(page) }, }) export { test, expect } export * from "./m3-helpers"