* feat(browser): capture correlated network content and lifecycle evidence Keep request identities and redirect relationships across ordered scenario events. Capture selected browser-exposed bytes and duplicate headers through modular readers, settle bounded reads before cleanup, and preserve unavailable states. Keep content-selection mismatches and legacy network coverage unknown during comparison. Verify real CLI/MCP, installed package, and browser lifecycle paths. Refs #774 Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis) Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing) * fix(browser): align capture defaults and redact encoded media metadata Correct the complete documented request and legacy default expectation caught by CI. Cover and redact encoded declared values in captured body media types. Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis) Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing) * test(browser): verify polling capture cutoff completeness Accept incomplete event coverage only when the continuously polling fixture has explicit unfinished requests; retain failures for other missing or truncated sections. Include actual completeness and unfinished transactions in diagnostics. Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis) Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)
302 lines
9.3 KiB
TypeScript
302 lines
9.3 KiB
TypeScript
import { type BrowserContext, type Page } from "playwright-core";
|
|
|
|
import type {
|
|
BrowserScenario,
|
|
BrowserScenarioAction,
|
|
} from "../domain/browserScenario.js";
|
|
import { BrowserObservationError } from "../domain/browserObservationError.js";
|
|
import type {
|
|
BrowserScenarioSessionFactory,
|
|
BrowserScenarioSessionPort,
|
|
} from "./BrowserScenarioSessionPort.js";
|
|
import { BrowserScenarioSecrets } from "./BrowserScenarioSecrets.js";
|
|
import {
|
|
closePlaywrightScenarioBrowser,
|
|
openPlaywrightScenarioBrowser,
|
|
type OpenedScenarioBrowser,
|
|
} from "./PlaywrightScenarioBrowser.js";
|
|
import { performPlaywrightScenarioAction } from "./PlaywrightScenarioActions.js";
|
|
import { capturePlaywrightStepArtifacts } from "./PlaywrightScenarioArtifacts.js";
|
|
import { PlaywrightScenarioEvents } from "./PlaywrightScenarioEvents.js";
|
|
import { withPlaywrightExecutionBoundary } from "./PlaywrightExecutionBoundary.js";
|
|
|
|
const OPERATION = "capture_browser_scenario" as const;
|
|
|
|
const storageSeeds = (
|
|
blocks: BrowserScenario["storage"]["local_storage"],
|
|
secrets: BrowserScenarioSecrets,
|
|
): Record<string, string[][]> => {
|
|
const seeds: Record<string, string[][]> = {};
|
|
for (const { origin, entries } of blocks)
|
|
(seeds[origin] ??= []).push(
|
|
...entries.map(({ name, value }) => [name, secrets.value(value)]),
|
|
);
|
|
return seeds;
|
|
};
|
|
|
|
const installStorageSeeds = async (
|
|
context: BrowserContext,
|
|
page: Page,
|
|
scenario: BrowserScenario,
|
|
secrets: BrowserScenarioSecrets,
|
|
): Promise<void> => {
|
|
await context.addCookies(
|
|
scenario.storage.cookies.map((cookie) => ({
|
|
name: cookie.name,
|
|
value: secrets.value(cookie.value),
|
|
url: secrets.url(cookie.destination),
|
|
httpOnly: cookie.http_only,
|
|
secure: cookie.secure,
|
|
sameSite: cookie.same_site,
|
|
})),
|
|
);
|
|
const storage = {
|
|
local: storageSeeds(scenario.storage.local_storage, secrets),
|
|
session: storageSeeds(scenario.storage.session_storage, secrets),
|
|
};
|
|
const payload = JSON.stringify(storage).replaceAll("<", "\\u003c");
|
|
await page.addInitScript(`(() => {
|
|
const seeds = ${payload};
|
|
const local = seeds.local[window.location.origin] ?? [];
|
|
const session = seeds.session[window.location.origin] ?? [];
|
|
for (const [name, value] of local) window.localStorage.setItem(name, value);
|
|
for (const [name, value] of session)
|
|
window.sessionStorage.setItem(name, value);
|
|
})()`);
|
|
};
|
|
|
|
const blockAttachedServiceWorkers = async (page: Page): Promise<void> => {
|
|
const controller = await page.evaluate(
|
|
"Boolean(navigator.serviceWorker?.controller)",
|
|
);
|
|
if (controller)
|
|
throw new BrowserObservationError(OPERATION, "target_not_allowed");
|
|
const blockRegistration = `(() => {
|
|
if (!("serviceWorker" in navigator)) return;
|
|
void navigator.serviceWorker.getRegistrations()
|
|
.then((registrations) => Promise.all(registrations.map((item) => item.unregister())));
|
|
Object.defineProperty(ServiceWorkerContainer.prototype, "register", {
|
|
configurable: false,
|
|
value: () => Promise.reject(new Error("service workers are blocked by scenario policy"))
|
|
});
|
|
})()`;
|
|
await page.addInitScript(blockRegistration);
|
|
await page.evaluate(blockRegistration);
|
|
};
|
|
|
|
const initializePage = async (
|
|
context: BrowserContext,
|
|
page: Page,
|
|
scenario: BrowserScenario,
|
|
secrets: BrowserScenarioSecrets,
|
|
): Promise<void> => {
|
|
context.setDefaultTimeout(0);
|
|
context.setDefaultNavigationTimeout(0);
|
|
if (scenario.browser.mode === "connect")
|
|
await blockAttachedServiceWorkers(page);
|
|
await installStorageSeeds(context, page, scenario, secrets);
|
|
};
|
|
|
|
export class PlaywrightScenarioSession implements BrowserScenarioSessionPort {
|
|
readonly mode: "launch" | "connect";
|
|
readonly processOwnership: "provider-owned" | "external";
|
|
readonly product = "Chromium";
|
|
readonly version: string;
|
|
readonly initialUrl: string;
|
|
private closed = false;
|
|
private readonly secrets: BrowserScenarioSecrets;
|
|
private readonly eventCapture: PlaywrightScenarioEvents;
|
|
private readonly signal: AbortSignal | undefined;
|
|
|
|
private constructor(
|
|
private readonly opened: OpenedScenarioBrowser,
|
|
options: {
|
|
readonly mode: BrowserScenario["browser"]["mode"];
|
|
readonly secrets: BrowserScenarioSecrets;
|
|
readonly eventCapture: PlaywrightScenarioEvents;
|
|
readonly signal?: AbortSignal;
|
|
},
|
|
) {
|
|
this.mode = options.mode;
|
|
this.processOwnership =
|
|
options.mode === "launch" ? "provider-owned" : "external";
|
|
this.secrets = options.secrets;
|
|
this.eventCapture = options.eventCapture;
|
|
this.signal = options.signal;
|
|
this.version = opened.browser.version();
|
|
this.initialUrl = opened.page.url();
|
|
}
|
|
|
|
static async open(
|
|
scenario: BrowserScenario,
|
|
environment: Readonly<Record<string, string | undefined>>,
|
|
options: {
|
|
readonly signal?: AbortSignal;
|
|
},
|
|
): Promise<PlaywrightScenarioSession> {
|
|
if (options.signal?.aborted === true)
|
|
throw new BrowserObservationError(OPERATION, "cancelled");
|
|
const secrets = BrowserScenarioSecrets.resolve(scenario, environment);
|
|
if (secrets === undefined)
|
|
throw new BrowserObservationError(OPERATION, "secret_unavailable");
|
|
const opening = openPlaywrightScenarioBrowser(scenario, environment);
|
|
let opened: OpenedScenarioBrowser;
|
|
try {
|
|
opened = await withPlaywrightExecutionBoundary(
|
|
() => opening,
|
|
undefined,
|
|
options.signal,
|
|
);
|
|
} catch (cause: unknown) {
|
|
void opening
|
|
.then((lateOpened) =>
|
|
closePlaywrightScenarioBrowser(lateOpened, options.signal),
|
|
)
|
|
.catch((cause: unknown) => {
|
|
// best-effort cleanup: late-open cleanup must not mask the boundary failure.
|
|
void cause;
|
|
});
|
|
throw cause;
|
|
}
|
|
try {
|
|
await withPlaywrightExecutionBoundary(
|
|
() => initializePage(opened.context, opened.page, scenario, secrets),
|
|
undefined,
|
|
options.signal,
|
|
);
|
|
const events = new PlaywrightScenarioEvents({
|
|
page: opened.page,
|
|
context: opened.context,
|
|
ownsContext: scenario.browser.mode === "launch",
|
|
enabled: new Set(scenario.capture.events),
|
|
secrets,
|
|
network: scenario.capture.network,
|
|
...(options.signal === undefined ? {} : { signal: options.signal }),
|
|
});
|
|
const session = new PlaywrightScenarioSession(opened, {
|
|
mode: scenario.browser.mode,
|
|
secrets,
|
|
eventCapture: events,
|
|
...(options.signal === undefined ? {} : { signal: options.signal }),
|
|
});
|
|
await withPlaywrightExecutionBoundary(
|
|
() =>
|
|
opened.page.goto(secrets.url(scenario.start_url), {
|
|
waitUntil: "load",
|
|
timeout: 0,
|
|
}),
|
|
undefined,
|
|
options.signal,
|
|
);
|
|
return session;
|
|
} catch (cause: unknown) {
|
|
await closePlaywrightScenarioBrowser(opened, options.signal);
|
|
throw cause;
|
|
}
|
|
}
|
|
|
|
currentUrl(): string {
|
|
return this.opened.page.url();
|
|
}
|
|
|
|
sanitizeUrl(value: string) {
|
|
return this.secrets.sanitizeUrl(value);
|
|
}
|
|
|
|
setStep(index: number): void {
|
|
this.eventCapture.setStep(index);
|
|
}
|
|
|
|
nextEventSequence(): number {
|
|
return this.eventCapture.nextSequence();
|
|
}
|
|
|
|
lastEventSequence(): number {
|
|
return this.eventCapture.lastSequence();
|
|
}
|
|
|
|
events() {
|
|
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,
|
|
): Promise<void> {
|
|
await withPlaywrightExecutionBoundary(
|
|
() =>
|
|
performPlaywrightScenarioAction({
|
|
page: this.opened.page,
|
|
action,
|
|
secrets: this.secrets,
|
|
}),
|
|
"timeout_ms" in action ? action.timeout_ms : undefined,
|
|
signal,
|
|
);
|
|
}
|
|
|
|
async capture(
|
|
requested: ReadonlySet<
|
|
BrowserScenario["capture"]["after_each_step"][number]
|
|
>,
|
|
signal?: AbortSignal,
|
|
) {
|
|
return withPlaywrightExecutionBoundary(
|
|
() =>
|
|
capturePlaywrightStepArtifacts({
|
|
context: this.opened.context,
|
|
page: this.opened.page,
|
|
secrets: this.secrets,
|
|
requested,
|
|
}),
|
|
undefined,
|
|
signal,
|
|
);
|
|
}
|
|
|
|
async close() {
|
|
if (this.closed)
|
|
return this.mode === "launch"
|
|
? ("terminated-owned-process" as const)
|
|
: ("disconnected-external" as const);
|
|
this.closed = true;
|
|
try {
|
|
await this.eventCapture.finish();
|
|
} finally {
|
|
await closePlaywrightScenarioBrowser(this.opened, this.signal);
|
|
}
|
|
return this.mode === "launch"
|
|
? ("terminated-owned-process" as const)
|
|
: ("disconnected-external" as const);
|
|
}
|
|
|
|
redactError(error: unknown): string {
|
|
return this.secrets.redact(
|
|
error instanceof Error ? error.message : "browser action failed",
|
|
);
|
|
}
|
|
}
|
|
|
|
/** Production Playwright/CDP session factory. */
|
|
export class PlaywrightScenarioSessionFactory implements BrowserScenarioSessionFactory {
|
|
constructor(
|
|
private readonly environment: Readonly<
|
|
Record<string, string | undefined>
|
|
> = process.env,
|
|
) {}
|
|
|
|
open(
|
|
scenario: BrowserScenario,
|
|
options: {
|
|
readonly signal?: AbortSignal;
|
|
} = {},
|
|
): Promise<BrowserScenarioSessionPort> {
|
|
return PlaywrightScenarioSession.open(scenario, this.environment, options);
|
|
}
|
|
}
|