import { EventEmitter } from "node:events"; import type { Page, TestInfo } from "@playwright/test"; import { afterEach, expect, it, vi } from "vitest"; import { diagnosticPath, emitBrowserFailureDiagnostics, observeBrowserDiagnostics, } from "./browser-diagnostics.ts"; afterEach(() => { vi.restoreAllMocks(); vi.useRealTimers(); }); function fixture() { const page = Object.assign(new EventEmitter(), { evaluate: vi.fn(async () => [ { readyState: 4, networkState: 1, paused: false, ended: false, errorCode: null, currentTime: 1.5, privateValue: "MEDIA_SECRET", }, ]), mainFrame: () => frame, }); const frame = { url: () => "http://127.0.0.1:3000/?token=NAV_SECRET" }; return { page, frame, observed: observeBrowserDiagnostics(page as unknown as Page), }; } it("exports only known local resource paths without credentials, query strings or fixture names", () => { expect( diagnosticPath( "http://user:PRIVATE@127.0.0.1:1234/browser/elk-safe.js?token=PRIVATE#PRIVATE", ), ).toBe("/browser/elk-safe.js"); expect( diagnosticPath( "http://127.0.0.1:1234/.system/specs/PRIVATE/movie.webm?key=PRIVATE", ), ).toBe("[fixture].webm"); expect(diagnosticPath("http://127.0.0.1:1234/api/state?secret=PRIVATE")).toBe( "/api/state", ); for (const url of [ "https://private.example/PRIVATE", "file:///PRIVATE", "http://127.0.0.1:1234/PRIVATE", "invalid PRIVATE", ]) expect(diagnosticPath(url)).not.toContain("PRIVATE"); }); it("retains bounded lifecycle/request correlation and publishes only metadata within its byte budget", async () => { const { page, frame, observed } = fixture(); for (let i = 0; i < 100; i++) { const request = { url: () => `http://127.0.0.1:1234/browser/${"a".repeat(1000)}-${i}.js?key=QUERY_SECRET`, isNavigationRequest: () => false, failure: () => ({ errorText: "net::ERR_ABORTED" }), }; page.emit("request", request); if (i === 99) { page.emit("framenavigated", frame); page.emit("requestfailed", request); } } const snapshot = await observed.snapshot(); expect(snapshot.events).toHaveLength(64); expect(snapshot.dropped).toBeGreaterThan(0); expect(snapshot.events.at(-1)).toMatchObject({ kind: "requestfailed", document: 1, aborted: true, }); const lastRequest = snapshot.events.findLast( (event) => event.kind === "request", ); expect(snapshot.events.at(-1)?.request).toBe(lastRequest?.request); const output = vi.spyOn(console, "error").mockImplementation(() => {}); await emitBrowserFailureDiagnostics( "the-system", { line: 42, project: { name: "chromium" } } as TestInfo, [observed], ); const line = String(output.mock.calls[0]?.[0]); const json = line.slice("PI_EXT_BROWSER_DIAGNOSTICS=".length); const report = JSON.parse(json); expect(report).toMatchObject({ schema: 1, suite: "the-system", line: 42, project: "chromium", }); expect(Buffer.byteLength(json)).toBeLessThanOrEqual(24_000); expect(report.pages[0].events.length).toBeGreaterThan(0); expect(line).not.toMatch(/QUERY_SECRET|NAV_SECRET|MEDIA_SECRET|privateValue/); expect((await observed.snapshot()).events).toHaveLength(64); }); it("bounds an unavailable media-state query", async () => { vi.useFakeTimers(); const { page, observed } = fixture(); page.evaluate.mockImplementation(() => new Promise(() => {})); const snapshot = observed.snapshot(); await vi.advanceTimersByTimeAsync(500); expect((await snapshot).media).toBeNull(); });