test(browser): consolidate CDP workflows and colocate parser coverage

This commit is contained in:
morluto
2026-10-08 04:03:24 +08:00
parent 1fd1989d34
commit 26829045d0
18 changed files with 104 additions and 397 deletions
@@ -1,65 +1,8 @@
import { expect, it, onTestFinished } from "vitest";
import { CdpCaptureEvents } from "../../../src/browser/CdpCaptureEvents.js";
import { inspectWebPageInputSchema } from "../../../src/domain/browserObservation.js";
import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js";
import type { CdpEvent } from "../../../src/browser/CdpConnection.js";
const events = (): CdpCaptureEvents =>
new CdpCaptureEvents(
inspectWebPageInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: ["https://example.test"],
target_id: "page-1",
}),
new Set(["https://example.test"]),
);
const frameNavigated = (url: string, loaderId: string): CdpEvent => ({
method: "Page.frameNavigated",
params: {
frame: { id: "main", url, loaderId },
},
});
it("ignores a repeated frameNavigated for the document already committed", () => {
const state = events();
state.beginAuthorizedFrame("main");
state.ingest(frameNavigated("https://example.test/page", "loader-1"));
expect(state.navigationDuringCapture).toBe(true);
state.navigationDuringCapture = false;
// A re-fire with the same URL and loader is not a navigation.
state.ingest(frameNavigated("https://example.test/page", "loader-1"));
expect(state.navigationDuringCapture).toBe(false);
});
it("still reports a genuine change of document", () => {
const state = events();
state.beginAuthorizedFrame("main");
state.ingest(frameNavigated("https://example.test/page", "loader-1"));
state.navigationDuringCapture = false;
state.ingest(frameNavigated("https://example.test/other", "loader-2"));
expect(state.navigationDuringCapture).toBe(true);
});
it("does not forget a navigation that happened during the observation window", () => {
const state = events();
state.beginAuthorizedFrame("main");
state.ingest(frameNavigated("https://example.test/page", "loader-1"));
// The final capture re-attaches to the resolved frame; clearing the flag here
// used to report a clean capture while carrying pre-navigation state.
state.beginFinalCapture("main");
expect(state.navigationDuringCapture).toBe(true);
});
it("keeps a settled observation window clean", () => {
const state = events();
state.beginAuthorizedFrame("main");
state.beginFinalCapture("main");
expect(state.navigationDuringCapture).toBe(false);
});
it("captures a page without aborting on a duplicate frame event", async () => {
const browser = await startFakeCdpBrowser({ sessionTimeline: "same_origin" });