fix(browser): capture popup network and report early event gaps (#614)

* fix(browser): capture selected events in popup pages

Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>

* fix(browser): capture popup network and report early event gaps

* fix(browser): deduplicate owned popup discovery

* test(browser): group popup selection coverage with the scenario event suite

Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>

* test(browser): verify popup evidence through CLI and MCP

* fix(browser): expose shared-context event coverage limits

---------

Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>
Co-authored-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
N0zoM1z0
2026-10-06 01:58:54 +08:00
committed by GitHub
co-authored by Rudy Celekli morluto
parent 5f3ba3e688
commit f557207887
8 changed files with 366 additions and 10 deletions
@@ -26,6 +26,8 @@ export interface BrowserScenarioSessionPort {
readonly dropped: number;
readonly items: readonly BrowserScenarioEvent[];
};
/** Gaps in selected event families that prevent complete capture claims. */
eventLimitations?(): readonly string[];
perform(action: BrowserScenarioAction, signal?: AbortSignal): Promise<void>;
capture(
requested: ReadonlySet<SnapshotKind>,
@@ -99,6 +99,7 @@ class FakeSession implements BrowserScenarioSessionPort {
performCalls = 0;
captureCalls = 0;
onPerform: (() => void) | undefined;
eventGaps: readonly string[] = [];
private step = 0;
constructor(
@@ -138,6 +139,10 @@ class FakeSession implements BrowserScenarioSessionPort {
return { retained: 0, dropped: 0, items: [] };
}
eventLimitations(): readonly string[] {
return this.eventGaps;
}
async perform(_action: BrowserScenarioAction, signal?: AbortSignal) {
this.performCalls += 1;
this.onPerform?.();
@@ -184,6 +189,24 @@ class FakeSession implements BrowserScenarioSessionPort {
}
describe("PlaywrightBrowserScenarioProvider", () => {
it("makes reported event gaps ineligible for equality even in launch mode", async () => {
const session = new FakeSession("launch");
session.eventGaps = ["Popup frames before discovery are unavailable"];
const provider = new PlaywrightBrowserScenarioProvider({
open: () => Promise.resolve(session),
});
const result = await provider.captureScenario(
scenario({ events: ["frames"] }),
);
expect(result.ok).toBe(true);
if (!result.ok) throw result.error;
expect(result.value.completeness).toMatchObject({
status: "incomplete",
equality_eligible: false,
missing_sections: ["events"],
});
expect(result.value.limitations).toContain(session.eventGaps[0]);
});
it("returns an initial state and every step beyond the former action ceiling", async () => {
const session = new FakeSession("launch");
const provider = new PlaywrightBrowserScenarioProvider({
@@ -234,6 +234,7 @@ const globalCompleteness = (
({ completeness }) => completeness.truncated_sections,
);
if (session.mode === "connect") missing.push("events");
if ((session.eventLimitations?.().length ?? 0) > 0) missing.push("events");
return classifyBrowserScenarioCompleteness(missing, truncated);
};
@@ -299,6 +300,7 @@ const runScenario = async (
"Event sequence records provider receipt order; simultaneous browser causality is not inferred.",
"Response bodies are not retained.",
"Storage values are hashed only after declared-secret redaction.",
...(session.eventLimitations?.() ?? []),
...(session.mode === "connect"
? [
"CDP attachment cannot recover pre-attach events or guarantee launch-time context options.",
+60 -10
View File
@@ -1,4 +1,5 @@
import type {
BrowserContext,
ConsoleMessage,
Download,
Frame,
@@ -34,6 +35,9 @@ type UnindexedEvent = BrowserScenarioEvent extends infer Event
interface EventCaptureOptions {
readonly page: Page;
readonly context?: BrowserContext;
/** A fresh launch context is wholly owned; connected contexts can contain other tabs. */
readonly ownsContext?: boolean;
readonly enabled: ReadonlySet<EventName>;
readonly secrets: BrowserScenarioSecrets;
}
@@ -45,10 +49,41 @@ export class PlaywrightScenarioEvents {
private sequence = 0;
private readonly enabled: ReadonlySet<EventName>;
private readonly secrets: BrowserScenarioSecrets;
private readonly ownsContext: boolean;
private readonly pages = new Set<Page>();
private readonly incompleteFamilies = new Set<EventName>();
constructor(options: EventCaptureOptions) {
this.enabled = options.enabled;
this.secrets = options.secrets;
this.ownsContext = options.ownsContext === true;
this.pages.add(options.page);
if (this.ownsContext && this.enabled.size > 0)
(options.context ?? options.page.context()).on("page", (page) =>
this.popup(page),
);
if (this.enabled.has("network")) {
const context = options.context ?? options.page.context();
const inScope = (request: Request): boolean => {
if (this.ownsContext) return true;
try {
return this.pages.has(request.frame().page());
} catch {
// Initial popup navigations can lack a frame. Never guess ownership
// from a URL in a shared context; connect captures are attach-limited.
return false;
}
};
context.on("request", (request) => {
if (inScope(request)) this.request("request", request);
});
context.on("response", (response) => {
if (inScope(response.request())) this.response(response);
});
context.on("requestfailed", (request) => {
if (inScope(request)) this.request("request-failed", request);
});
}
this.observePage(options.page);
}
@@ -64,6 +99,20 @@ export class PlaywrightScenarioEvents {
return this.sequence;
}
/** Report selected popup event families with unrecoverable pre-discovery gaps. */
limitations(): readonly string[] {
const popupGaps = [...this.incompleteFamilies].map(
(family) =>
`Popup ${family} events before Page discovery are unavailable; later events are retained.`,
);
return this.enabled.has("network") && !this.ownsContext
? [
"Shared-context network capture excludes requests without a known root or descendant Page frame.",
...popupGaps,
]
: popupGaps;
}
result(): {
readonly retained: number;
readonly dropped: number;
@@ -98,13 +147,6 @@ export class PlaywrightScenarioEvents {
error.stack === undefined ? null : this.secrets.redact(error.stack),
});
});
if (this.enabled.has("network")) {
page.on("request", (request) => this.request("request", request));
page.on("response", (response) => this.response(response));
page.on("requestfailed", (request) =>
this.request("request-failed", request),
);
}
if (this.enabled.has("websockets"))
page.on("websocket", (socket) => this.webSocket(socket));
if (this.enabled.has("frames")) {
@@ -116,9 +158,8 @@ export class PlaywrightScenarioEvents {
}
if (this.enabled.has("workers"))
page.on("worker", (worker) => this.worker(worker));
// Every selectable event can also be emitted by a child popup page, and a
// popup is only reachable through this one subscription, so any selection at
// all must observe popups. Lifecycle events stay gated on `popups` below.
// Any selected family needs descendant discovery in shared contexts.
// Popup lifecycle records remain gated on their own selector below.
if (this.enabled.size > 0) page.on("popup", (popup) => this.popup(popup));
if (this.enabled.has("downloads"))
page.on("download", (download) => this.download(download));
@@ -241,6 +282,15 @@ export class PlaywrightScenarioEvents {
}
private popup(page: Page): void {
if (this.pages.has(page)) return;
this.pages.add(page);
for (const family of [
"frames",
"workers",
"websockets",
"downloads",
] as const)
if (this.enabled.has(family)) this.incompleteFamilies.add(family);
const opened = {
url: page.url() === "" ? null : this.safeUrl(page.url()),
name: null,
+7
View File
@@ -163,6 +163,8 @@ export class PlaywrightScenarioSession implements BrowserScenarioSessionPort {
);
const events = new PlaywrightScenarioEvents({
page: opened.page,
context: opened.context,
ownsContext: scenario.browser.mode === "launch",
enabled: new Set(scenario.capture.events),
secrets,
});
@@ -212,6 +214,11 @@ export class PlaywrightScenarioSession implements BrowserScenarioSessionPort {
return this.eventCapture.result();
}
/** Preserve event coverage gaps for the aggregate capture's completeness. */
eventLimitations(): readonly string[] {
return this.eventCapture.limitations();
}
async perform(
action: BrowserScenarioAction,
signal?: AbortSignal,