mirror of
https://github.com/Tencent/BrowserSkill.git
synced 2026-09-10 13:01:23 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
321494d93f | ||
|
|
f2184e74c5 | ||
|
|
eeb633d68e | ||
|
|
6d181105f2 | ||
|
|
ebad0ed1b5 | ||
|
|
498f8fb4a7 | ||
|
|
cef59137e4 | ||
|
|
f70978bfcf | ||
|
|
1498cd425c |
@@ -254,6 +254,69 @@ describe("ChromiumCdp", () => {
|
|||||||
expect(cdp.isAttached(7)).toBe(false);
|
expect(cdp.isAttached(7)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
])("waits for detach before reconnecting a returned tab (detach rejects: %s)", async (rejectDetach) => {
|
||||||
|
const { api } = fakeApi();
|
||||||
|
const attached = new Set<number>();
|
||||||
|
let finishDetach!: () => void;
|
||||||
|
vi.mocked(api.attach).mockImplementation(async ({ tabId }) => {
|
||||||
|
if (attached.has(tabId!)) throw new Error("Another debugger is already attached");
|
||||||
|
attached.add(tabId!);
|
||||||
|
});
|
||||||
|
vi.mocked(api.detach).mockImplementation(async ({ tabId }) => {
|
||||||
|
attached.delete(tabId!);
|
||||||
|
});
|
||||||
|
vi.mocked(api.detach).mockImplementationOnce(
|
||||||
|
({ tabId }) =>
|
||||||
|
new Promise<void>((resolve, reject) => {
|
||||||
|
finishDetach = () => {
|
||||||
|
attached.delete(tabId!);
|
||||||
|
if (rejectDetach) reject(new Error("tab already detached"));
|
||||||
|
else resolve();
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const cdp = new ChromiumCdp(api);
|
||||||
|
cdp.trackSessionTab("aa11", 7);
|
||||||
|
await cdp.ensureAttached(7);
|
||||||
|
const returning = cdp.releaseSessionTab("aa11", 7);
|
||||||
|
await vi.waitFor(() => expect(api.detach).toHaveBeenCalledOnce());
|
||||||
|
let duplicateDetachFinished = false;
|
||||||
|
const duplicateDetach = cdp.detach(7).then(() => {
|
||||||
|
duplicateDetachFinished = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
cdp.trackSessionTab("bb22", 7);
|
||||||
|
// Register rejection handlers immediately; the pre-fix driver rejects
|
||||||
|
// both commands while the browser still has the previous attachment.
|
||||||
|
const pending = Promise.allSettled([
|
||||||
|
cdp.send(7, "Runtime.evaluate", { expression: "document.title" }),
|
||||||
|
cdp.send(7, "DOM.getDocument"),
|
||||||
|
]);
|
||||||
|
await cdp.send(8, "DOM.getDocument");
|
||||||
|
const finishedEarly = duplicateDetachFinished;
|
||||||
|
finishDetach();
|
||||||
|
await Promise.all([returning, duplicateDetach]);
|
||||||
|
|
||||||
|
expect(await pending).toEqual([
|
||||||
|
{ status: "fulfilled", value: { ok: true } },
|
||||||
|
{ status: "fulfilled", value: { ok: true } },
|
||||||
|
]);
|
||||||
|
expect(finishedEarly).toBe(false);
|
||||||
|
expect(vi.mocked(api.attach).mock.calls.filter(([target]) => target.tabId === 7)).toHaveLength(
|
||||||
|
2,
|
||||||
|
);
|
||||||
|
expect(cdp.isAttached(7)).toBe(true);
|
||||||
|
expect(cdp.isAttached(8)).toBe(true);
|
||||||
|
await cdp.detachSession("bb22");
|
||||||
|
expect(api.detach).toHaveBeenCalledTimes(2);
|
||||||
|
expect(cdp.isAttached(7)).toBe(false);
|
||||||
|
expect(attached.has(7)).toBe(false);
|
||||||
|
expect(cdp.isAttached(8)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
it("detachAll() iterates every cached tab", async () => {
|
it("detachAll() iterates every cached tab", async () => {
|
||||||
const { api } = fakeApi();
|
const { api } = fakeApi();
|
||||||
const cdp = new ChromiumCdp(api);
|
const cdp = new ChromiumCdp(api);
|
||||||
@@ -651,6 +714,155 @@ describe("ChromiumCdp", () => {
|
|||||||
expect(cdp.networkEntriesSince(33, 0, 50, 1000).entries[0].url).toBeUndefined();
|
expect(cdp.networkEntriesSince(33, 0, 50, 1000).entries[0].url).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("releases only the returning session's claim on a tab", async () => {
|
||||||
|
const { api } = fakeApi();
|
||||||
|
const cdp = new ChromiumCdp(api);
|
||||||
|
await cdp.ensureAttached(1);
|
||||||
|
await cdp.ensureAttached(2);
|
||||||
|
cdp.trackSessionTab("aa11", 1);
|
||||||
|
cdp.trackSessionTab("bb22", 1);
|
||||||
|
cdp.trackSessionTab("aa11", 2);
|
||||||
|
|
||||||
|
await cdp.releaseSessionTab("unknown", 1);
|
||||||
|
await cdp.releaseSessionTab("aa11", 1);
|
||||||
|
expect(api.detach).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
await cdp.releaseSessionTab("bb22", 1);
|
||||||
|
await cdp.releaseSessionTab("bb22", 1);
|
||||||
|
expect(api.detach).toHaveBeenCalledExactlyOnceWith({ tabId: 1 });
|
||||||
|
expect(cdp.isAttached(2)).toBe(true);
|
||||||
|
|
||||||
|
await cdp.detachSession("aa11");
|
||||||
|
expect(api.detach).toHaveBeenCalledTimes(2);
|
||||||
|
expect(api.detach).toHaveBeenLastCalledWith({ tabId: 2 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
])("releases a tab with an attachment in flight (new owner: %s)", async (newOwner) => {
|
||||||
|
const { api } = fakeApi();
|
||||||
|
let finishAttach!: () => void;
|
||||||
|
vi.mocked(api.attach).mockImplementationOnce(
|
||||||
|
() =>
|
||||||
|
new Promise<void>((resolve) => {
|
||||||
|
finishAttach = resolve;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const cdp = new ChromiumCdp(api);
|
||||||
|
cdp.trackSessionTab("aa11", 1);
|
||||||
|
const attaching = cdp.ensureAttached(1);
|
||||||
|
const releasing = cdp.releaseSessionTab("aa11", 1);
|
||||||
|
if (newOwner) cdp.trackSessionTab("bb22", 1);
|
||||||
|
finishAttach();
|
||||||
|
await Promise.all([attaching, releasing]);
|
||||||
|
|
||||||
|
expect(cdp.isAttached(1)).toBe(newOwner);
|
||||||
|
expect(api.detach).toHaveBeenCalledTimes(newOwner ? 0 : 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("stops accepting dialogs on a returned tab even when another session keeps CDP attached", async () => {
|
||||||
|
const { api, onEvent } = fakeApi();
|
||||||
|
let controlled = true;
|
||||||
|
const cdp = new ChromiumCdp(api, { shouldAutoAcceptDialog: () => controlled });
|
||||||
|
cdp.trackSessionTab("aa11", 1);
|
||||||
|
cdp.trackSessionTab("bb22", 1);
|
||||||
|
await cdp.ensureAttached(1);
|
||||||
|
onEvent.fire({ tabId: 1 }, "Page.javascriptDialogOpening", {
|
||||||
|
type: "confirm",
|
||||||
|
message: "before return",
|
||||||
|
});
|
||||||
|
await vi.waitFor(() => expect(cdp.dialogsSince(1, 0)).toHaveLength(1));
|
||||||
|
vi.mocked(api.sendCommand).mockClear();
|
||||||
|
|
||||||
|
controlled = false;
|
||||||
|
await cdp.releaseSessionTab("aa11", 1);
|
||||||
|
expect(cdp.isAttached(1)).toBe(true);
|
||||||
|
onEvent.fire({ tabId: 1 }, "Page.javascriptDialogOpening", {
|
||||||
|
type: "confirm",
|
||||||
|
message: "after return",
|
||||||
|
});
|
||||||
|
await Promise.resolve();
|
||||||
|
expect(api.sendCommand).not.toHaveBeenCalled();
|
||||||
|
expect(cdp.dialogsSince(1, 0)).toHaveLength(1);
|
||||||
|
|
||||||
|
controlled = true;
|
||||||
|
onEvent.fire({ tabId: 1 }, "Page.javascriptDialogOpening", {
|
||||||
|
type: "alert",
|
||||||
|
message: "borrowed again",
|
||||||
|
});
|
||||||
|
await vi.waitFor(() => expect(cdp.dialogsSince(1, 0)).toHaveLength(2));
|
||||||
|
expect(api.sendCommand).toHaveBeenCalledExactlyOnceWith(
|
||||||
|
{ tabId: 1 },
|
||||||
|
"Page.handleJavaScriptDialog",
|
||||||
|
{ accept: true },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("ignores dialog events after detach, including a pending eligibility check", async () => {
|
||||||
|
const { api, onEvent } = fakeApi();
|
||||||
|
let resolveEligibility!: (value: boolean) => void;
|
||||||
|
const shouldAutoAcceptDialog = vi.fn(
|
||||||
|
() =>
|
||||||
|
new Promise<boolean>((resolve) => {
|
||||||
|
resolveEligibility = resolve;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const cdp = new ChromiumCdp(api, { shouldAutoAcceptDialog });
|
||||||
|
await cdp.ensureAttached(1);
|
||||||
|
onEvent.fire({ tabId: 1 }, "Page.javascriptDialogOpening", { type: "alert" });
|
||||||
|
await cdp.detach(1);
|
||||||
|
resolveEligibility(true);
|
||||||
|
await Promise.resolve();
|
||||||
|
onEvent.fire({ tabId: 1 }, "Page.javascriptDialogOpening", { type: "alert" });
|
||||||
|
|
||||||
|
expect(shouldAutoAcceptDialog).toHaveBeenCalledOnce();
|
||||||
|
expect(api.sendCommand).not.toHaveBeenCalledWith(
|
||||||
|
expect.anything(),
|
||||||
|
"Page.handleJavaScriptDialog",
|
||||||
|
expect.anything(),
|
||||||
|
);
|
||||||
|
expect(cdp.dialogsSince(1, 0)).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not accept dialogs when the tab's current scope cannot be determined", async () => {
|
||||||
|
const { api, onEvent } = fakeApi();
|
||||||
|
const shouldAutoAcceptDialog = vi.fn(async () => {
|
||||||
|
throw new Error("tab is gone");
|
||||||
|
});
|
||||||
|
const cdp = new ChromiumCdp(api, { shouldAutoAcceptDialog });
|
||||||
|
await cdp.ensureAttached(1);
|
||||||
|
vi.mocked(api.sendCommand).mockClear();
|
||||||
|
|
||||||
|
onEvent.fire({ tabId: 1 }, "Page.javascriptDialogOpening", { type: "confirm" });
|
||||||
|
await Promise.resolve();
|
||||||
|
|
||||||
|
expect(shouldAutoAcceptDialog).toHaveBeenCalledWith(1);
|
||||||
|
expect(api.sendCommand).not.toHaveBeenCalled();
|
||||||
|
expect(cdp.dialogsSince(1, 0)).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not restore dialog state when an acceptance completes after detach", async () => {
|
||||||
|
const { api, onEvent } = fakeApi();
|
||||||
|
const cdp = new ChromiumCdp(api);
|
||||||
|
await cdp.ensureAttached(1);
|
||||||
|
let finishAccept!: () => void;
|
||||||
|
vi.mocked(api.sendCommand).mockImplementationOnce(
|
||||||
|
() =>
|
||||||
|
new Promise<void>((resolve) => {
|
||||||
|
finishAccept = resolve;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
onEvent.fire({ tabId: 1 }, "Page.javascriptDialogOpening", { type: "alert" });
|
||||||
|
await cdp.detach(1);
|
||||||
|
finishAccept();
|
||||||
|
await Promise.resolve();
|
||||||
|
|
||||||
|
expect(cdp.dialogsSince(1, 0)).toEqual([]);
|
||||||
|
expect(cdp.dialogCursor(1)).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
it("detachSession only detaches tabs no other session owns", async () => {
|
it("detachSession only detaches tabs no other session owns", async () => {
|
||||||
const { api } = fakeApi();
|
const { api } = fakeApi();
|
||||||
const cdp = new ChromiumCdp(api);
|
const cdp = new ChromiumCdp(api);
|
||||||
|
|||||||
@@ -165,6 +165,7 @@ export class ChromiumCdp {
|
|||||||
private readonly api: CdpDebuggerApi;
|
private readonly api: CdpDebuggerApi;
|
||||||
private readonly attachedTabs = new Set<number>();
|
private readonly attachedTabs = new Set<number>();
|
||||||
private readonly attachInFlight = new Map<number, Promise<void>>();
|
private readonly attachInFlight = new Map<number, Promise<void>>();
|
||||||
|
private readonly detachInFlight = new Map<number, Promise<void>>();
|
||||||
private readonly tabOwners = new Map<number, Set<string>>();
|
private readonly tabOwners = new Map<number, Set<string>>();
|
||||||
private readonly dialogBuffers = new Map<number, JavaScriptDialogInfo[]>();
|
private readonly dialogBuffers = new Map<number, JavaScriptDialogInfo[]>();
|
||||||
private readonly dialogSequences = new Map<number, number>();
|
private readonly dialogSequences = new Map<number, number>();
|
||||||
@@ -182,7 +183,13 @@ export class ChromiumCdp {
|
|||||||
private networkSubscription: { dispose(): void } | null = null;
|
private networkSubscription: { dispose(): void } | null = null;
|
||||||
private frameTargetSubscription: { dispose(): void } | null = null;
|
private frameTargetSubscription: { dispose(): void } | null = null;
|
||||||
|
|
||||||
constructor(api: CdpDebuggerApi = chromeDebuggerApi) {
|
constructor(
|
||||||
|
api: CdpDebuggerApi = chromeDebuggerApi,
|
||||||
|
private readonly options: {
|
||||||
|
/** CDP observation alone does not authorize dismissing user dialogs. */
|
||||||
|
shouldAutoAcceptDialog?: (tabId: number) => boolean | Promise<boolean>;
|
||||||
|
} = {},
|
||||||
|
) {
|
||||||
this.api = api;
|
this.api = api;
|
||||||
this.bindAutoDetach();
|
this.bindAutoDetach();
|
||||||
this.bindDialogHandler();
|
this.bindDialogHandler();
|
||||||
@@ -193,6 +200,10 @@ export class ChromiumCdp {
|
|||||||
|
|
||||||
/** Attach to `tabId` if we haven't already in this driver. */
|
/** Attach to `tabId` if we haven't already in this driver. */
|
||||||
async ensureAttached(tabId: number): Promise<void> {
|
async ensureAttached(tabId: number): Promise<void> {
|
||||||
|
// Returning a tab clears the cache before Chrome finishes detaching.
|
||||||
|
// New observers must wait before opening the next connection to that tab.
|
||||||
|
const detaching = this.detachInFlight.get(tabId);
|
||||||
|
if (detaching) await detaching;
|
||||||
if (this.attachedTabs.has(tabId)) return;
|
if (this.attachedTabs.has(tabId)) return;
|
||||||
const existing = this.attachInFlight.get(tabId);
|
const existing = this.attachInFlight.get(tabId);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
@@ -425,6 +436,11 @@ export class ChromiumCdp {
|
|||||||
|
|
||||||
/** Detach if attached; never throws. */
|
/** Detach if attached; never throws. */
|
||||||
async detach(tabId: number): Promise<void> {
|
async detach(tabId: number): Promise<void> {
|
||||||
|
const existing = this.detachInFlight.get(tabId);
|
||||||
|
if (existing) {
|
||||||
|
await existing;
|
||||||
|
return;
|
||||||
|
}
|
||||||
this.attachInFlight.delete(tabId);
|
this.attachInFlight.delete(tabId);
|
||||||
if (!this.attachedTabs.has(tabId)) return;
|
if (!this.attachedTabs.has(tabId)) return;
|
||||||
this.attachedTabs.delete(tabId);
|
this.attachedTabs.delete(tabId);
|
||||||
@@ -432,13 +448,19 @@ export class ChromiumCdp {
|
|||||||
this.clearConsoleState(tabId);
|
this.clearConsoleState(tabId);
|
||||||
this.clearNetworkState(tabId);
|
this.clearNetworkState(tabId);
|
||||||
this.clearFrameState(tabId);
|
this.clearFrameState(tabId);
|
||||||
try {
|
const detach = (async () => {
|
||||||
await this.api.detach({ tabId });
|
try {
|
||||||
} catch (err) {
|
await this.api.detach({ tabId });
|
||||||
// Tab may already be gone — Chrome auto-detaches on close. Log
|
} catch (err) {
|
||||||
// at debug so production builds aren't noisy.
|
// Tab may already be gone — Chrome auto-detaches on close. Log
|
||||||
console.debug("[bsk cdp] detach failed (likely tab already closed)", err);
|
// at debug so production builds aren't noisy.
|
||||||
}
|
console.debug("[bsk cdp] detach failed (likely tab already closed)", err);
|
||||||
|
}
|
||||||
|
})().finally(() => {
|
||||||
|
this.detachInFlight.delete(tabId);
|
||||||
|
});
|
||||||
|
this.detachInFlight.set(tabId, detach);
|
||||||
|
await detach;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** True iff `ensureAttached(tabId)` has succeeded since the last detach. */
|
/** True iff `ensureAttached(tabId)` has succeeded since the last detach. */
|
||||||
@@ -453,6 +475,17 @@ export class ChromiumCdp {
|
|||||||
this.tabOwners.set(tabId, owners);
|
this.tabOwners.set(tabId, owners);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Release one session's claim, preserving attachments still used by another. */
|
||||||
|
async releaseSessionTab(sessionId: string, tabId: number): Promise<void> {
|
||||||
|
const owners = this.tabOwners.get(tabId);
|
||||||
|
if (!owners?.delete(sessionId) || owners.size > 0) return;
|
||||||
|
this.tabOwners.delete(tabId);
|
||||||
|
// An observation may still be attaching when the tab is returned. Wait
|
||||||
|
// for it so detach cannot miss the attachment or remove a new owner's claim.
|
||||||
|
await this.attachInFlight.get(tabId)?.catch(() => {});
|
||||||
|
if (!this.tabOwners.has(tabId)) await this.detach(tabId);
|
||||||
|
}
|
||||||
|
|
||||||
/** Subscribe to all CDP events. Returned disposable removes the listener. */
|
/** Subscribe to all CDP events. Returned disposable removes the listener. */
|
||||||
onEvent(handler: (source: CdpDebuggee, method: string, params: unknown) => void): {
|
onEvent(handler: (source: CdpDebuggee, method: string, params: unknown) => void): {
|
||||||
dispose(): void;
|
dispose(): void;
|
||||||
@@ -639,13 +672,23 @@ export class ChromiumCdp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async onJavaScriptDialogOpening(tabId: number, params: unknown): Promise<void> {
|
private async onJavaScriptDialogOpening(tabId: number, params: unknown): Promise<void> {
|
||||||
|
if (!this.attachedTabs.has(tabId) && !this.attachInFlight.has(tabId)) return;
|
||||||
const parsed = parseDialogOpeningParams(params);
|
const parsed = parseDialogOpeningParams(params);
|
||||||
try {
|
try {
|
||||||
|
if (
|
||||||
|
this.options.shouldAutoAcceptDialog &&
|
||||||
|
!(await this.options.shouldAutoAcceptDialog(tabId))
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// The tab may have been returned while its current scope was checked.
|
||||||
|
if (!this.attachedTabs.has(tabId) && !this.attachInFlight.has(tabId)) return;
|
||||||
const handleParams: { accept: boolean; promptText?: string } = { accept: true };
|
const handleParams: { accept: boolean; promptText?: string } = { accept: true };
|
||||||
if (parsed.type === "prompt") {
|
if (parsed.type === "prompt") {
|
||||||
handleParams.promptText = parsed.defaultPrompt ?? "";
|
handleParams.promptText = parsed.defaultPrompt ?? "";
|
||||||
}
|
}
|
||||||
await this.api.sendCommand({ tabId }, "Page.handleJavaScriptDialog", handleParams);
|
await this.api.sendCommand({ tabId }, "Page.handleJavaScriptDialog", handleParams);
|
||||||
|
if (!this.attachedTabs.has(tabId) && !this.attachInFlight.has(tabId)) return;
|
||||||
const sequence = (this.dialogSequences.get(tabId) ?? 0) + 1;
|
const sequence = (this.dialogSequences.get(tabId) ?? 0) + 1;
|
||||||
this.dialogSequences.set(tabId, sequence);
|
this.dialogSequences.set(tabId, sequence);
|
||||||
this.appendDialog(tabId, {
|
this.appendDialog(tabId, {
|
||||||
@@ -835,15 +878,9 @@ export class ChromiumCdp {
|
|||||||
|
|
||||||
/** Detach tabs only when no other live session has claimed them. */
|
/** Detach tabs only when no other live session has claimed them. */
|
||||||
async detachSession(sessionId: string): Promise<void> {
|
async detachSession(sessionId: string): Promise<void> {
|
||||||
const tabsToDetach: number[] = [];
|
await Promise.all(
|
||||||
for (const [tabId, owners] of this.tabOwners) {
|
Array.from(this.tabOwners.keys(), (tabId) => this.releaseSessionTab(sessionId, tabId)),
|
||||||
owners.delete(sessionId);
|
);
|
||||||
if (owners.size === 0) {
|
|
||||||
this.tabOwners.delete(tabId);
|
|
||||||
tabsToDetach.push(tabId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await Promise.all(tabsToDetach.map((tabId) => this.detach(tabId)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,12 @@ export default defineBackground(() => {
|
|||||||
const controller = new ConnectionController();
|
const controller = new ConnectionController();
|
||||||
const transport = new WSTransport({ url: __BSK_DAEMON_WS_URL__ });
|
const transport = new WSTransport({ url: __BSK_DAEMON_WS_URL__ });
|
||||||
const sessions = new SessionManager();
|
const sessions = new SessionManager();
|
||||||
const cdp = new ChromiumCdp();
|
const cdp = new ChromiumCdp(undefined, {
|
||||||
|
shouldAutoAcceptDialog: async (tabId) => {
|
||||||
|
const tab = await chrome.tabs.get(tabId);
|
||||||
|
return sessions.findByWindowId(tab.windowId) !== null;
|
||||||
|
},
|
||||||
|
});
|
||||||
const sessionsLive = attachSessionsLiveFlag({ manager: sessions });
|
const sessionsLive = attachSessionsLiveFlag({ manager: sessions });
|
||||||
let overlayGeneration = 0;
|
let overlayGeneration = 0;
|
||||||
const controlModes = new Map<string, OverlayMode>();
|
const controlModes = new Map<string, OverlayMode>();
|
||||||
|
|||||||
@@ -747,6 +747,135 @@ describe("ToolDispatcher", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
{ tab_id: 7, code: "not_found" },
|
||||||
|
{ tab_id: undefined, code: "invalid_params" },
|
||||||
|
])("keeps hover when tab_return is rejected with $code", async ({ tab_id, code }) => {
|
||||||
|
const sendMessage = vi.fn(async () => undefined);
|
||||||
|
vi.stubGlobal("chrome", {
|
||||||
|
tabs: {
|
||||||
|
sendMessage,
|
||||||
|
get: vi.fn(async () => ({ id: 7, windowId: 100, active: true })),
|
||||||
|
query: vi.fn(async () => [{ id: 7, windowId: 100, active: true }]),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const { transport, sent, deliver } = fakeTransport();
|
||||||
|
const sessions = new SessionManager({
|
||||||
|
agentWindow: {
|
||||||
|
create: vi.fn(async () => 100),
|
||||||
|
remove: vi.fn(async () => {}),
|
||||||
|
ensureActiveTab: vi.fn(async () => 1),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
await sessions.start("aa11");
|
||||||
|
const cdp = {
|
||||||
|
send: vi.fn(async <T>() => ({}) as T),
|
||||||
|
detachSession: vi.fn(async () => {}),
|
||||||
|
releaseSessionTab: vi.fn(async () => {}),
|
||||||
|
};
|
||||||
|
const dispatcher = new ToolDispatcher({
|
||||||
|
transport,
|
||||||
|
sessions,
|
||||||
|
cdp: cdp as unknown as TestDispatcherCdp,
|
||||||
|
});
|
||||||
|
const helpers = dispatcher as unknown as {
|
||||||
|
rememberHover: (sessionId: string, result: unknown) => unknown;
|
||||||
|
setHoverBypass: (sessionId: string, tabId: number, enabled: boolean) => Promise<void>;
|
||||||
|
withHoverReassert: <T>(
|
||||||
|
params: { session_id: string; tab_id?: number },
|
||||||
|
work: () => Promise<T>,
|
||||||
|
) => Promise<T>;
|
||||||
|
};
|
||||||
|
helpers.rememberHover("aa11", { tab_id: 7, x: 10, y: 20 });
|
||||||
|
await helpers.setHoverBypass("aa11", 7, true);
|
||||||
|
dispatcher.start();
|
||||||
|
deliver(makeRequest("tool.tab_return", { session_id: "aa11", tab_id }));
|
||||||
|
await vi.waitFor(() => expect(sent).toHaveLength(1));
|
||||||
|
|
||||||
|
expect(sent[0]).toMatchObject({ error: { code } });
|
||||||
|
expect(cdp.releaseSessionTab).not.toHaveBeenCalled();
|
||||||
|
expect(sendMessage).not.toHaveBeenCalledWith(7, expect.objectContaining({ enabled: false }));
|
||||||
|
await helpers.withHoverReassert({ session_id: "aa11", tab_id: 7 }, async () => ({}));
|
||||||
|
expect(cdp.send).toHaveBeenCalledExactlyOnceWith(7, "Input.dispatchMouseEvent", {
|
||||||
|
type: "mouseMoved",
|
||||||
|
x: 10,
|
||||||
|
y: 20,
|
||||||
|
});
|
||||||
|
dispatcher.stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("releases the returned tab's hover before moving it and leaves other tabs alone", async () => {
|
||||||
|
const sendMessage = vi.fn(async () => undefined);
|
||||||
|
const move = vi.fn(async () => {
|
||||||
|
expect(sendMessage).toHaveBeenCalledWith(7, expect.objectContaining({ enabled: false }));
|
||||||
|
return { id: 7, windowId: 200, index: 4 };
|
||||||
|
});
|
||||||
|
vi.stubGlobal("chrome", {
|
||||||
|
tabs: { sendMessage, move },
|
||||||
|
windows: { get: vi.fn(async () => ({ id: 200 })) },
|
||||||
|
});
|
||||||
|
const { transport, sent, deliver } = fakeTransport();
|
||||||
|
const sessions = new SessionManager({
|
||||||
|
agentWindow: {
|
||||||
|
create: vi.fn(async () => 100),
|
||||||
|
remove: vi.fn(async () => {}),
|
||||||
|
ensureActiveTab: vi.fn(async () => 1),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const ctx = await sessions.start("aa11");
|
||||||
|
ctx.borrowedTabs.set(7, { tabId: 7, originalWindowId: 200, originalIndex: 4 });
|
||||||
|
const cdp = {
|
||||||
|
send: vi.fn(async <T>() => ({}) as T),
|
||||||
|
detachSession: vi.fn(async () => {}),
|
||||||
|
releaseSessionTab: vi.fn(async () => {}),
|
||||||
|
};
|
||||||
|
const dispatcher = new ToolDispatcher({
|
||||||
|
transport,
|
||||||
|
sessions,
|
||||||
|
cdp: cdp as unknown as TestDispatcherCdp,
|
||||||
|
});
|
||||||
|
const helpers = dispatcher as unknown as {
|
||||||
|
rememberHover: (sessionId: string, result: unknown) => unknown;
|
||||||
|
setHoverBypass: (sessionId: string, tabId: number, enabled: boolean) => Promise<void>;
|
||||||
|
withHoverReassert: <T>(
|
||||||
|
params: { session_id: string; tab_id?: number },
|
||||||
|
work: () => Promise<T>,
|
||||||
|
) => Promise<T>;
|
||||||
|
};
|
||||||
|
for (const [sessionId, tabId] of [
|
||||||
|
["aa11", 7],
|
||||||
|
["aa11", 8],
|
||||||
|
["bb22", 9],
|
||||||
|
] as const) {
|
||||||
|
helpers.rememberHover(sessionId, { tab_id: tabId, x: 10, y: 20 });
|
||||||
|
await helpers.setHoverBypass(sessionId, tabId, true);
|
||||||
|
}
|
||||||
|
dispatcher.start();
|
||||||
|
deliver(makeRequest("tool.tab_return", { session_id: "aa11", tab_id: 7 }));
|
||||||
|
await vi.waitFor(() => expect(sent).toHaveLength(1));
|
||||||
|
|
||||||
|
expect(sent[0]).toMatchObject({ result: { tab_id: 7, returned_to_window_id: 200 } });
|
||||||
|
expect(cdp.releaseSessionTab).toHaveBeenCalledExactlyOnceWith("aa11", 7);
|
||||||
|
await helpers.withHoverReassert({ session_id: "aa11", tab_id: 7 }, async () => ({}));
|
||||||
|
expect(cdp.send).not.toHaveBeenCalled();
|
||||||
|
for (const [sessionId, tabId] of [
|
||||||
|
["aa11", 8],
|
||||||
|
["bb22", 9],
|
||||||
|
] as const) {
|
||||||
|
expect(sendMessage).not.toHaveBeenCalledWith(
|
||||||
|
tabId,
|
||||||
|
expect.objectContaining({ enabled: false }),
|
||||||
|
);
|
||||||
|
await helpers.withHoverReassert({ session_id: sessionId, tab_id: tabId }, async () => ({}));
|
||||||
|
expect(cdp.send).toHaveBeenLastCalledWith(tabId, "Input.dispatchMouseEvent", {
|
||||||
|
type: "mouseMoved",
|
||||||
|
x: 10,
|
||||||
|
y: 20,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
dispatcher.stop();
|
||||||
|
});
|
||||||
|
|
||||||
it("transfers hover overlay bypass ownership between sessions", async () => {
|
it("transfers hover overlay bypass ownership between sessions", async () => {
|
||||||
vi.stubGlobal("chrome", {
|
vi.stubGlobal("chrome", {
|
||||||
tabs: {
|
tabs: {
|
||||||
|
|||||||
@@ -0,0 +1,573 @@
|
|||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { SessionManager } from "@/session-manager/manager";
|
||||||
|
import type { CdpRunner } from "@/tools/shared";
|
||||||
|
import { handleFill } from "../interaction";
|
||||||
|
|
||||||
|
interface ScriptParams {
|
||||||
|
functionDeclaration: string;
|
||||||
|
arguments?: Array<{ value: unknown }>;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function setup(markup = '<input value="old">') {
|
||||||
|
document.body.innerHTML = markup;
|
||||||
|
const element = document.body.firstElementChild as HTMLInputElement;
|
||||||
|
const manager = new SessionManager({
|
||||||
|
agentWindow: {
|
||||||
|
create: async () => 100,
|
||||||
|
remove: async () => {},
|
||||||
|
ensureActiveTab: async () => 4,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const ctx = await manager.start("aa11");
|
||||||
|
ctx.refStore.set("e1", 12, { tabId: 4 });
|
||||||
|
const script = vi.fn(async (params: ScriptParams): Promise<unknown> => {
|
||||||
|
try {
|
||||||
|
const fn = new Function(`return (${params.functionDeclaration})`)();
|
||||||
|
return {
|
||||||
|
result: {
|
||||||
|
value: await fn.apply(
|
||||||
|
element,
|
||||||
|
(params.arguments ?? []).map((arg) => arg.value),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} catch {
|
||||||
|
return { exceptionDetails: { text: "test page script failed" } };
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// Number/email controls have a browser caret even though their selection
|
||||||
|
// getters return null. Model that caret instead of assuming it is at the end.
|
||||||
|
const hiddenCaret = { start: 0, end: 0 };
|
||||||
|
const moveToEnd = vi.fn(async () => {
|
||||||
|
if (typeof element.selectionStart === "number") {
|
||||||
|
element.setSelectionRange(element.value.length, element.value.length);
|
||||||
|
} else {
|
||||||
|
hiddenCaret.start = hiddenCaret.end = element.value.length;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const insert = vi.fn(async (text: string) => {
|
||||||
|
const focused = document.activeElement as HTMLInputElement;
|
||||||
|
if (focused.readOnly || focused.disabled) return;
|
||||||
|
const start = focused.selectionStart ?? hiddenCaret.start;
|
||||||
|
const end = focused.selectionEnd ?? hiddenCaret.end;
|
||||||
|
const value = focused.value.slice(0, start) + text + focused.value.slice(end);
|
||||||
|
focused.value = focused.maxLength >= 0 ? value.slice(0, focused.maxLength) : value;
|
||||||
|
hiddenCaret.start = hiddenCaret.end = Math.min(start + text.length, focused.value.length);
|
||||||
|
focused.dispatchEvent(new Event("input", { bubbles: true }));
|
||||||
|
});
|
||||||
|
const release = vi.fn(async () => ({}));
|
||||||
|
const send = vi.fn(async (_tabId: number, method: string, params?: object) => {
|
||||||
|
switch (method) {
|
||||||
|
case "DOM.describeNode":
|
||||||
|
return {
|
||||||
|
node: {
|
||||||
|
nodeName: element.tagName,
|
||||||
|
attributes: Array.from(element.attributes).flatMap((attr) => [attr.name, attr.value]),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
case "DOM.scrollIntoViewIfNeeded":
|
||||||
|
return {};
|
||||||
|
case "DOM.focus":
|
||||||
|
element.focus();
|
||||||
|
return {};
|
||||||
|
case "DOM.resolveNode":
|
||||||
|
return { object: { objectId: "fill-target" } };
|
||||||
|
case "Runtime.callFunctionOn":
|
||||||
|
return script(params as ScriptParams);
|
||||||
|
case "Input.insertText":
|
||||||
|
await insert((params as { text: string }).text);
|
||||||
|
return {};
|
||||||
|
case "Input.dispatchKeyEvent":
|
||||||
|
if ((params as { commands?: string[] }).commands?.includes("moveToEndOfDocument")) {
|
||||||
|
await moveToEnd();
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
case "Runtime.releaseObject":
|
||||||
|
return release();
|
||||||
|
default:
|
||||||
|
throw new Error(`unexpected ${method}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
element,
|
||||||
|
hiddenCaret,
|
||||||
|
moveToEnd,
|
||||||
|
script,
|
||||||
|
insert,
|
||||||
|
release,
|
||||||
|
send,
|
||||||
|
fill: (value = "hello", clearBefore = true, signal?: AbortSignal) =>
|
||||||
|
handleFill(
|
||||||
|
manager,
|
||||||
|
{ session_id: "aa11", ref: "e1", value, clear_before: clearBefore },
|
||||||
|
{
|
||||||
|
cdp: { send: send as CdpRunner["send"] },
|
||||||
|
tabsApi: {
|
||||||
|
get: async (id) => ({ id, windowId: 100, active: true }) as chrome.tabs.Tab,
|
||||||
|
query: async () => [{ id: 4, windowId: 100, active: true } as chrome.tabs.Tab],
|
||||||
|
},
|
||||||
|
signal,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.restoreAllMocks();
|
||||||
|
document.body.innerHTML = "";
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("fill result verification", () => {
|
||||||
|
it.each([
|
||||||
|
"readonly",
|
||||||
|
"disabled",
|
||||||
|
])("preserves a %s field before rejecting it", async (attribute) => {
|
||||||
|
const h = await setup(`<input ${attribute} value="old">`);
|
||||||
|
expect(await h.fill()).toMatchObject({
|
||||||
|
code: "invalid_params",
|
||||||
|
data: { reason: "target_not_fillable" },
|
||||||
|
});
|
||||||
|
expect(h.element.value).toBe("old");
|
||||||
|
expect(h.insert).not.toHaveBeenCalled();
|
||||||
|
expect(h.release).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
"checkbox",
|
||||||
|
"radio",
|
||||||
|
"range",
|
||||||
|
"date",
|
||||||
|
"file",
|
||||||
|
])("rejects an unsupported %s input before clearing", async (type) => {
|
||||||
|
const h = await setup(`<input type="${type}">`);
|
||||||
|
const before = h.element.value;
|
||||||
|
expect(await h.fill()).toMatchObject({
|
||||||
|
code: "invalid_params",
|
||||||
|
data: { reason: "target_not_fillable" },
|
||||||
|
});
|
||||||
|
expect(h.element.value).toBe(before);
|
||||||
|
expect(h.insert).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects an overlong value before clearing", async () => {
|
||||||
|
const h = await setup('<input maxlength="3" value="old">');
|
||||||
|
expect(await h.fill()).toMatchObject({
|
||||||
|
code: "invalid_params",
|
||||||
|
message: /maxlength/,
|
||||||
|
data: { reason: "fill_value_invalid" },
|
||||||
|
});
|
||||||
|
expect(h.element.value).toBe("old");
|
||||||
|
expect(h.insert).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
])("classifies a DOM.focus rejection on a disabled=%s target", async (disabled) => {
|
||||||
|
const h = await setup(`<input ${disabled ? "disabled" : ""} value="old">`);
|
||||||
|
const send = h.send.getMockImplementation()!;
|
||||||
|
h.send.mockImplementation(async (tabId, method, params) => {
|
||||||
|
if (method === "DOM.focus") throw new Error("Element is not focusable");
|
||||||
|
return send(tabId, method, params);
|
||||||
|
});
|
||||||
|
expect(await h.fill()).toMatchObject({
|
||||||
|
code: disabled ? "invalid_params" : "cdp_failed",
|
||||||
|
data: { reason: disabled ? "target_not_fillable" : "fill_failed" },
|
||||||
|
});
|
||||||
|
expect(h.element.value).toBe("old");
|
||||||
|
expect(h.insert).not.toHaveBeenCalled();
|
||||||
|
expect(h.release).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("fills a background document without requiring foreground browser focus", async () => {
|
||||||
|
const h = await setup();
|
||||||
|
vi.spyOn(document, "hasFocus").mockReturnValue(false);
|
||||||
|
expect(await h.fill()).toMatchObject({ value_length: 5 });
|
||||||
|
expect(h.element.value).toBe("hello");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("restores focus once after clearing without typing into another field", async () => {
|
||||||
|
const h = await setup();
|
||||||
|
const other = document.createElement("input");
|
||||||
|
document.body.append(other);
|
||||||
|
h.element.addEventListener("input", () => other.focus());
|
||||||
|
expect(await h.fill()).toMatchObject({ value_length: 5 });
|
||||||
|
expect(h.element.value).toBe("hello");
|
||||||
|
expect(other.value).toBe("");
|
||||||
|
expect(h.send.mock.calls.filter(([, method]) => method === "DOM.focus")).toHaveLength(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
"focus",
|
||||||
|
"value",
|
||||||
|
])("rechecks %s after restoring focus, without retrying again", async (action) => {
|
||||||
|
const h = await setup();
|
||||||
|
const other = document.createElement("input");
|
||||||
|
document.body.append(other);
|
||||||
|
h.element.addEventListener(
|
||||||
|
"input",
|
||||||
|
() => {
|
||||||
|
other.focus();
|
||||||
|
h.element.addEventListener("focus", () => {
|
||||||
|
if (action === "focus") queueMicrotask(() => other.focus());
|
||||||
|
else h.element.value = "changed";
|
||||||
|
});
|
||||||
|
},
|
||||||
|
{ once: true },
|
||||||
|
);
|
||||||
|
expect(await h.fill()).toMatchObject({
|
||||||
|
code: "cdp_failed",
|
||||||
|
data: { reason: action === "focus" ? "fill_focus_lost" : "fill_target_changed" },
|
||||||
|
});
|
||||||
|
expect(other.value).toBe("");
|
||||||
|
expect(h.insert).not.toHaveBeenCalled();
|
||||||
|
expect(h.send.mock.calls.filter(([, method]) => method === "DOM.focus")).toHaveLength(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
"remove",
|
||||||
|
"readonly",
|
||||||
|
"restore",
|
||||||
|
"microtask-restore",
|
||||||
|
])("stops when clearing causes the page to %s the target", async (action) => {
|
||||||
|
const h = await setup();
|
||||||
|
h.element.addEventListener("input", () => {
|
||||||
|
if (action === "remove") h.element.remove();
|
||||||
|
if (action === "readonly") h.element.readOnly = true;
|
||||||
|
if (action === "restore") h.element.value = "restored";
|
||||||
|
if (action === "microtask-restore")
|
||||||
|
queueMicrotask(() => {
|
||||||
|
h.element.value = "restored";
|
||||||
|
});
|
||||||
|
});
|
||||||
|
expect(await h.fill()).toMatchObject({
|
||||||
|
code: "cdp_failed",
|
||||||
|
data: { reason: "fill_target_changed" },
|
||||||
|
});
|
||||||
|
expect(h.insert).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("fails when insertText is acknowledged without changing the value", async () => {
|
||||||
|
const h = await setup();
|
||||||
|
h.insert.mockImplementation(async () => {});
|
||||||
|
expect(await h.fill()).toMatchObject({
|
||||||
|
code: "cdp_failed",
|
||||||
|
data: { reason: "fill_value_mismatch" },
|
||||||
|
});
|
||||||
|
expect(h.release).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("detects truncation when maxlength changes after preparation", async () => {
|
||||||
|
const h = await setup();
|
||||||
|
h.element.addEventListener(
|
||||||
|
"input",
|
||||||
|
() => {
|
||||||
|
h.element.maxLength = 3;
|
||||||
|
},
|
||||||
|
{ once: true },
|
||||||
|
);
|
||||||
|
expect(await h.fill()).toMatchObject({
|
||||||
|
code: "cdp_failed",
|
||||||
|
data: { reason: "fill_value_mismatch" },
|
||||||
|
});
|
||||||
|
expect(h.element.value).toBe("hel");
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
"input",
|
||||||
|
"change",
|
||||||
|
"microtask-change",
|
||||||
|
])("detects a value reverted by the %s handler", async (event) => {
|
||||||
|
const h = await setup();
|
||||||
|
h.element.addEventListener(event === "microtask-change" ? "change" : event, () => {
|
||||||
|
if (event === "microtask-change")
|
||||||
|
queueMicrotask(() => {
|
||||||
|
h.element.value = "";
|
||||||
|
});
|
||||||
|
else h.element.value = "";
|
||||||
|
});
|
||||||
|
expect(await h.fill()).toMatchObject({ code: "cdp_failed", message: /expected value/ });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("compares content rather than length without including field values in the error", async () => {
|
||||||
|
const h = await setup('<input type="password">');
|
||||||
|
h.element.addEventListener("change", () => {
|
||||||
|
h.element.value = "other-secret";
|
||||||
|
});
|
||||||
|
const result = await h.fill("given-secret");
|
||||||
|
expect(result).toMatchObject({ code: "cdp_failed", message: /expected value/ });
|
||||||
|
expect(JSON.stringify(result)).not.toMatch(/given-secret|other-secret/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not report success for a target replaced by a change handler", async () => {
|
||||||
|
const h = await setup();
|
||||||
|
h.element.addEventListener("change", () => h.element.replaceWith(h.element.cloneNode()));
|
||||||
|
expect(await h.fill()).toMatchObject({
|
||||||
|
code: "cdp_failed",
|
||||||
|
data: { reason: "fill_target_changed" },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
1, 2, 3, 4, 5,
|
||||||
|
])("rejects script exceptions at phase %s and releases the target", async (phase) => {
|
||||||
|
const h = await setup();
|
||||||
|
vi.spyOn(document, "hasFocus").mockReturnValue(false);
|
||||||
|
const run = h.script.getMockImplementation()!;
|
||||||
|
let count = 0;
|
||||||
|
h.script.mockImplementation(async (params) =>
|
||||||
|
++count === phase ? { exceptionDetails: { text: "page secret" } } : run(params),
|
||||||
|
);
|
||||||
|
const result = await h.fill();
|
||||||
|
expect(result).toMatchObject({ code: "cdp_failed", data: { reason: "fill_failed" } });
|
||||||
|
expect(JSON.stringify(result)).not.toContain("page secret");
|
||||||
|
if (phase <= 3) expect(h.insert).not.toHaveBeenCalled();
|
||||||
|
expect(h.release).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([1, 2, 3, 5])("rejects missing results at phase %s", async (phase) => {
|
||||||
|
const h = await setup();
|
||||||
|
vi.spyOn(document, "hasFocus").mockReturnValue(false);
|
||||||
|
const run = h.script.getMockImplementation()!;
|
||||||
|
let count = 0;
|
||||||
|
h.script.mockImplementation(async (params) =>
|
||||||
|
++count === phase ? { result: { type: "undefined" } } : run(params),
|
||||||
|
);
|
||||||
|
expect(await h.fill()).toMatchObject({ code: "cdp_failed" });
|
||||||
|
if (phase <= 3) expect(h.insert).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each(["input", "change"])("checks state after nested microtasks from %s", async (event) => {
|
||||||
|
const h = await setup();
|
||||||
|
h.element.addEventListener(event, () => {
|
||||||
|
queueMicrotask(() =>
|
||||||
|
queueMicrotask(() => {
|
||||||
|
h.element.value = "reverted";
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
expect(await h.fill()).toMatchObject({ code: "cdp_failed" });
|
||||||
|
if (event === "input") expect(h.insert).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
"<input>",
|
||||||
|
'<input type="password">',
|
||||||
|
"<textarea></textarea>",
|
||||||
|
])("verifies successful text in %s", async (markup) => {
|
||||||
|
const h = await setup(markup);
|
||||||
|
expect(await h.fill("你好🙂")).toMatchObject({ value_length: 4 });
|
||||||
|
expect(h.element.value).toBe("你好🙂");
|
||||||
|
expect(h.release).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("verifies append against the old value plus the requested text", async () => {
|
||||||
|
const h = await setup();
|
||||||
|
h.element.focus();
|
||||||
|
h.element.setSelectionRange(3, 3);
|
||||||
|
expect(await h.fill("!", false)).toMatchObject({ value_length: 4 });
|
||||||
|
expect(h.element.value).toBe("old!");
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
["<textarea></textarea>", "a\r\nb", "a\nb"],
|
||||||
|
["<input>", "a\nb", "a b"],
|
||||||
|
["<input>", "a\r\nb", "a b"],
|
||||||
|
[
|
||||||
|
'<input type="email" multiple>',
|
||||||
|
" a@example.com , b@example.com ",
|
||||||
|
"a@example.com,b@example.com",
|
||||||
|
],
|
||||||
|
])("accepts native normalization for %s", async (markup, requested, normalized) => {
|
||||||
|
if (markup.startsWith("<textarea")) {
|
||||||
|
// Happy DOM does not implement Chrome's textarea line-ending normalization.
|
||||||
|
const setter = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value")!.set!;
|
||||||
|
vi.spyOn(HTMLTextAreaElement.prototype, "value", "set").mockImplementation(function (
|
||||||
|
this: HTMLTextAreaElement,
|
||||||
|
value,
|
||||||
|
) {
|
||||||
|
setter.call(this, value.replace(/\r\n?/g, "\n"));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const h = await setup(markup);
|
||||||
|
h.insert.mockImplementation(async () => {
|
||||||
|
h.element.value = normalized;
|
||||||
|
});
|
||||||
|
expect(await h.fill(requested)).toMatchObject({ value_length: normalized.length });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not count an editable padding break as an extra typed character", async () => {
|
||||||
|
const h = await setup('<div contenteditable="true" tabindex="0"></div>');
|
||||||
|
// These two layout/editability properties are missing in Happy DOM.
|
||||||
|
let innerText = "";
|
||||||
|
Object.defineProperty(h.element, "isContentEditable", { get: () => true });
|
||||||
|
Object.defineProperty(h.element, "innerText", { get: () => innerText });
|
||||||
|
h.insert.mockImplementation(async () => {
|
||||||
|
h.element.innerHTML = "one<div><br></div>";
|
||||||
|
innerText = "one\n\n";
|
||||||
|
});
|
||||||
|
expect(await h.fill("one\n")).toMatchObject({ value_length: 4 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
[0, 0],
|
||||||
|
[1, 1],
|
||||||
|
[0, 3],
|
||||||
|
])("appends after moving selection %s:%s to the end", async (start, end) => {
|
||||||
|
const h = await setup();
|
||||||
|
h.element.focus();
|
||||||
|
h.element.setSelectionRange(start, end);
|
||||||
|
expect(await h.fill("🙂", false)).toMatchObject({ value_length: 5 });
|
||||||
|
expect(h.element.value).toBe("old🙂");
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each(
|
||||||
|
["number", "email"].flatMap((type) =>
|
||||||
|
[
|
||||||
|
[0, 0],
|
||||||
|
[1, 1],
|
||||||
|
[0, 2],
|
||||||
|
].map(([start, end]) => ({ type, start, end })),
|
||||||
|
),
|
||||||
|
)("appends to $type from selection $start:$end without using its unsupported API", async ({
|
||||||
|
type,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
}) => {
|
||||||
|
const h = await setup(`<input type="${type}" value="12">`);
|
||||||
|
Object.assign(h.hiddenCaret, { start, end });
|
||||||
|
vi.spyOn(h.element, "selectionStart", "get").mockReturnValue(null);
|
||||||
|
vi.spyOn(h.element, "selectionEnd", "get").mockReturnValue(null);
|
||||||
|
const selection = vi.spyOn(h.element, "setSelectionRange").mockImplementation(() => {
|
||||||
|
throw new DOMException("unsupported", "InvalidStateError");
|
||||||
|
});
|
||||||
|
expect(await h.fill("3", false)).toMatchObject({ value_length: 3 });
|
||||||
|
expect(h.element.value).toBe("123");
|
||||||
|
expect(selection).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
"number",
|
||||||
|
"email",
|
||||||
|
])("does not report successful %s append when the caret command is ignored", async (type) => {
|
||||||
|
const h = await setup(`<input type="${type}" value="12">`);
|
||||||
|
vi.spyOn(h.element, "selectionStart", "get").mockReturnValue(null);
|
||||||
|
vi.spyOn(h.element, "selectionEnd", "get").mockReturnValue(null);
|
||||||
|
h.moveToEnd.mockResolvedValueOnce(undefined);
|
||||||
|
expect(await h.fill("3", false)).toMatchObject({
|
||||||
|
code: "cdp_failed",
|
||||||
|
data: { reason: "fill_value_mismatch" },
|
||||||
|
});
|
||||||
|
expect(h.element.value).toBe("312");
|
||||||
|
expect(h.insert).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects invalid number text before clearing", async () => {
|
||||||
|
const h = await setup('<input type="number" value="12">');
|
||||||
|
expect(await h.fill("abc")).toMatchObject({
|
||||||
|
code: "invalid_params",
|
||||||
|
data: { reason: "fill_value_invalid" },
|
||||||
|
});
|
||||||
|
expect(h.element.value).toBe("12");
|
||||||
|
expect(h.insert).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reports page formatting as unconfirmed without retrying the write", async () => {
|
||||||
|
const h = await setup();
|
||||||
|
h.element.addEventListener("change", () => {
|
||||||
|
h.element.value = h.element.value.toUpperCase();
|
||||||
|
});
|
||||||
|
expect(await h.fill()).toMatchObject({
|
||||||
|
code: "cdp_failed",
|
||||||
|
data: { reason: "fill_value_mismatch" },
|
||||||
|
});
|
||||||
|
expect(h.element.value).toBe("HELLO");
|
||||||
|
expect(h.insert).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
["focus", false],
|
||||||
|
["focus", true],
|
||||||
|
["value", false],
|
||||||
|
["value", true],
|
||||||
|
["cancel", false],
|
||||||
|
["cancel", true],
|
||||||
|
["throw", false],
|
||||||
|
["throw", true],
|
||||||
|
] as const)("stops when positioning triggers %s (clear=%s) and releases the key", async (action, clearBefore) => {
|
||||||
|
const h = await setup();
|
||||||
|
vi.spyOn(document, "hasFocus").mockReturnValue(false);
|
||||||
|
const controller = new AbortController();
|
||||||
|
const other = document.createElement("input");
|
||||||
|
document.body.append(other);
|
||||||
|
const send = h.send.getMockImplementation()!;
|
||||||
|
h.send.mockImplementation(async (tabId, method, params) => {
|
||||||
|
const reply = await send(tabId, method, params);
|
||||||
|
if (
|
||||||
|
method === "Input.dispatchKeyEvent" &&
|
||||||
|
(params as { type: string }).type === "rawKeyDown"
|
||||||
|
) {
|
||||||
|
if (action === "focus") other.focus();
|
||||||
|
if (action === "value") h.element.value = "changed";
|
||||||
|
if (action === "cancel") controller.abort();
|
||||||
|
if (action === "throw") throw new Error("dispatch failed");
|
||||||
|
}
|
||||||
|
return reply;
|
||||||
|
});
|
||||||
|
const result = await h.fill("!", clearBefore, controller.signal);
|
||||||
|
expect(result).toMatchObject({ code: action === "cancel" ? "cancelled" : "cdp_failed" });
|
||||||
|
expect(h.insert).not.toHaveBeenCalled();
|
||||||
|
expect(other.value).toBe("");
|
||||||
|
expect(h.send).toHaveBeenCalledWith(
|
||||||
|
4,
|
||||||
|
"Input.dispatchKeyEvent",
|
||||||
|
expect.objectContaining({ type: "keyUp" }),
|
||||||
|
);
|
||||||
|
expect(h.release).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([true, false])("verifies an empty request with clear_before=%s", async (clearBefore) => {
|
||||||
|
const h = await setup();
|
||||||
|
expect(await h.fill("", clearBefore)).toMatchObject({ value_length: clearBefore ? 0 : 3 });
|
||||||
|
expect(h.element.value).toBe(clearBefore ? "" : "old");
|
||||||
|
expect(h.insert).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("cancels after insertion without sending change events", async () => {
|
||||||
|
const h = await setup();
|
||||||
|
const controller = new AbortController();
|
||||||
|
h.insert.mockImplementation(async () => controller.abort());
|
||||||
|
expect(await h.fill("hello", true, controller.signal)).toMatchObject({ code: "cancelled" });
|
||||||
|
expect(h.script).toHaveBeenCalledTimes(2);
|
||||||
|
expect(h.release).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("cancels after resolving the remote object without clearing the field", async () => {
|
||||||
|
const h = await setup();
|
||||||
|
const controller = new AbortController();
|
||||||
|
const send = h.send.getMockImplementation()!;
|
||||||
|
h.send.mockImplementation(async (tabId, method, params) => {
|
||||||
|
const result = await send(tabId, method, params);
|
||||||
|
if (method === "DOM.resolveNode") controller.abort();
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
expect(await h.fill("hello", true, controller.signal)).toMatchObject({ code: "cancelled" });
|
||||||
|
expect(h.element.value).toBe("old");
|
||||||
|
expect(h.script).not.toHaveBeenCalled();
|
||||||
|
expect(h.release).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("honors cancellation during verification", async () => {
|
||||||
|
const h = await setup();
|
||||||
|
const controller = new AbortController();
|
||||||
|
h.element.addEventListener("change", () => controller.abort());
|
||||||
|
expect(await h.fill("hello", true, controller.signal)).toMatchObject({ code: "cancelled" });
|
||||||
|
expect(h.release).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps the verified outcome when releasing the remote object fails", async () => {
|
||||||
|
const h = await setup();
|
||||||
|
h.release.mockRejectedValueOnce(new Error("target closed"));
|
||||||
|
expect(await h.fill()).toMatchObject({ value_length: 5 });
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -540,6 +540,21 @@ describe("handleHover", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function successfulFillScript(params: unknown) {
|
||||||
|
const script = params as { arguments?: Array<{ value: unknown }>; functionDeclaration: string };
|
||||||
|
const args = script.arguments ?? [];
|
||||||
|
return {
|
||||||
|
result: {
|
||||||
|
value:
|
||||||
|
args.length === 2
|
||||||
|
? { before: "", expected: args[0].value }
|
||||||
|
: script.functionDeclaration.startsWith("function(expected)")
|
||||||
|
? { connected: true, matches: true, valueLength: String(args[0].value).length }
|
||||||
|
: "ready",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
describe("handleFill", () => {
|
describe("handleFill", () => {
|
||||||
it("returns not_found for unknown ref", async () => {
|
it("returns not_found for unknown ref", async () => {
|
||||||
const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]) });
|
const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]) });
|
||||||
@@ -586,7 +601,8 @@ describe("handleFill", () => {
|
|||||||
"DOM.scrollIntoViewIfNeeded": () => ({}),
|
"DOM.scrollIntoViewIfNeeded": () => ({}),
|
||||||
"DOM.focus": () => ({}),
|
"DOM.focus": () => ({}),
|
||||||
"DOM.resolveNode": () => ({ object: { objectId: "obj-1" } }),
|
"DOM.resolveNode": () => ({ object: { objectId: "obj-1" } }),
|
||||||
"Runtime.callFunctionOn": () => ({ result: { type: "undefined" } }),
|
"Runtime.callFunctionOn": successfulFillScript,
|
||||||
|
"Runtime.releaseObject": () => ({}),
|
||||||
"Input.insertText": () => ({}),
|
"Input.insertText": () => ({}),
|
||||||
});
|
});
|
||||||
const res = await handleFill(
|
const res = await handleFill(
|
||||||
@@ -600,13 +616,12 @@ describe("handleFill", () => {
|
|||||||
expect(res.used_ref).toBe("e1");
|
expect(res.used_ref).toBe("e1");
|
||||||
const insert = fake.sent.find((c) => c.method === "Input.insertText");
|
const insert = fake.sent.find((c) => c.method === "Input.insertText");
|
||||||
expect(insert?.params).toEqual({ text: "hello" });
|
expect(insert?.params).toEqual({ text: "hello" });
|
||||||
// clear_before defaults to true → callFunctionOn invoked once to
|
// Foreground replacement needs no extra caret-positioning round trip.
|
||||||
// clear, once to fire input/change after typing.
|
|
||||||
const callFns = fake.sent.filter((c) => c.method === "Runtime.callFunctionOn");
|
const callFns = fake.sent.filter((c) => c.method === "Runtime.callFunctionOn");
|
||||||
expect(callFns.length).toBeGreaterThanOrEqual(2);
|
expect(callFns).toHaveLength(4);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("clear_before=false skips the wipe call", async () => {
|
it("passes clear_before=false to preparation and verifies the result", async () => {
|
||||||
const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]) });
|
const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]) });
|
||||||
const ctx = await sm.start("aa11");
|
const ctx = await sm.start("aa11");
|
||||||
ctx.refStore.set("e1", 1, { tabId: 4 });
|
ctx.refStore.set("e1", 1, { tabId: 4 });
|
||||||
@@ -618,12 +633,12 @@ describe("handleFill", () => {
|
|||||||
"DOM.focus": () => ({}),
|
"DOM.focus": () => ({}),
|
||||||
"DOM.resolveNode": () => ({ object: { objectId: "obj-2" } }),
|
"DOM.resolveNode": () => ({ object: { objectId: "obj-2" } }),
|
||||||
"Runtime.callFunctionOn": (p) => {
|
"Runtime.callFunctionOn": (p) => {
|
||||||
const fn = (p as { functionDeclaration?: string }).functionDeclaration ?? "";
|
const args = (p as { arguments?: Array<{ value: unknown }> }).arguments ?? [];
|
||||||
// No "this.value = ''" clearing on a no-clear path; only the
|
if (args.length === 2) expect(args[1].value).toBe(false);
|
||||||
// post-input dispatchEvent.
|
return successfulFillScript(p);
|
||||||
expect(fn).not.toMatch(/this\.value\s*=\s*''/);
|
|
||||||
return { result: { type: "undefined" } };
|
|
||||||
},
|
},
|
||||||
|
"Input.dispatchKeyEvent": () => ({}),
|
||||||
|
"Runtime.releaseObject": () => ({}),
|
||||||
"Input.insertText": () => ({}),
|
"Input.insertText": () => ({}),
|
||||||
});
|
});
|
||||||
const res = await handleFill(
|
const res = await handleFill(
|
||||||
@@ -650,7 +665,8 @@ describe("handleFill", () => {
|
|||||||
"DOM.scrollIntoViewIfNeeded": () => ({}),
|
"DOM.scrollIntoViewIfNeeded": () => ({}),
|
||||||
"DOM.focus": () => ({}),
|
"DOM.focus": () => ({}),
|
||||||
"DOM.resolveNode": () => ({ object: { objectId: "obj-3" } }),
|
"DOM.resolveNode": () => ({ object: { objectId: "obj-3" } }),
|
||||||
"Runtime.callFunctionOn": () => ({ result: { type: "undefined" } }),
|
"Runtime.callFunctionOn": successfulFillScript,
|
||||||
|
"Runtime.releaseObject": () => ({}),
|
||||||
"Input.insertText": () => ({}),
|
"Input.insertText": () => ({}),
|
||||||
});
|
});
|
||||||
const res = await handleFill(
|
const res = await handleFill(
|
||||||
|
|||||||
@@ -257,11 +257,15 @@ describe("handleSessionStop with auto-return", () => {
|
|||||||
moves: [],
|
moves: [],
|
||||||
};
|
};
|
||||||
const { tabs, windows } = makeApis(state, { moveThrowsFor: new Set([1]) });
|
const { tabs, windows } = makeApis(state, { moveThrowsFor: new Set([1]) });
|
||||||
|
const cdp = {
|
||||||
|
detachSession: vi.fn(async () => {}),
|
||||||
|
releaseSessionTab: vi.fn(async () => {}),
|
||||||
|
};
|
||||||
|
|
||||||
const res = await handleSessionStop(
|
const res = await handleSessionStop(
|
||||||
sm,
|
sm,
|
||||||
{ session_id: "aa11" },
|
{ session_id: "aa11" },
|
||||||
{ tabManagement: { tabs, windows } },
|
{ cdp, tabManagement: { tabs, windows } },
|
||||||
);
|
);
|
||||||
if ("code" in res) throw new Error(`unexpected error: ${JSON.stringify(res)}`);
|
if ("code" in res) throw new Error(`unexpected error: ${JSON.stringify(res)}`);
|
||||||
expect(res.return_failures?.map((f) => f.tab_id)).toEqual([1]);
|
expect(res.return_failures?.map((f) => f.tab_id)).toEqual([1]);
|
||||||
@@ -269,6 +273,8 @@ describe("handleSessionStop with auto-return", () => {
|
|||||||
expect(sm.has("aa11")).toBe(true);
|
expect(sm.has("aa11")).toBe(true);
|
||||||
expect(ctx.borrowedTabs.has(1)).toBe(true);
|
expect(ctx.borrowedTabs.has(1)).toBe(true);
|
||||||
expect(ctx.borrowedTabs.has(2)).toBe(false);
|
expect(ctx.borrowedTabs.has(2)).toBe(false);
|
||||||
|
expect(cdp.releaseSessionTab).toHaveBeenCalledExactlyOnceWith("aa11", 2);
|
||||||
|
expect(cdp.detachSession).not.toHaveBeenCalled();
|
||||||
expect(aw.remove).not.toHaveBeenCalled();
|
expect(aw.remove).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
import { type CdpDebuggerApi, ChromiumCdp } from "@/browser-driver/chromium-cdp";
|
||||||
import { SessionManager } from "@/session-manager/manager";
|
import { SessionManager } from "@/session-manager/manager";
|
||||||
|
import { handleHover } from "../interaction";
|
||||||
import {
|
import {
|
||||||
type AgentOverlayResetApi,
|
type AgentOverlayResetApi,
|
||||||
type ChromeWindowsApi,
|
type ChromeWindowsApi,
|
||||||
@@ -547,6 +549,148 @@ describe("handleTabBorrow", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("handleTabReturn", () => {
|
describe("handleTabReturn", () => {
|
||||||
|
it.each([
|
||||||
|
"not_found",
|
||||||
|
"cdp_failed",
|
||||||
|
])("detaches after selector resolution fails with %s", async (code) => {
|
||||||
|
const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]) });
|
||||||
|
const ctx = await sm.start("aa11");
|
||||||
|
ctx.borrowedTabs.set(7, { tabId: 7, originalWindowId: 200, originalIndex: 4 });
|
||||||
|
const state: FakeTabState = {
|
||||||
|
tabs: new Map([[7, { id: 7, windowId: 100 } as chrome.tabs.Tab]]),
|
||||||
|
nextTabId: 50,
|
||||||
|
windowsClosed: new Set(),
|
||||||
|
};
|
||||||
|
const { api } = makeTabMutationApi(state);
|
||||||
|
const { api: windows } = makeWindowsApi(state);
|
||||||
|
const debuggerApi = {
|
||||||
|
attach: vi.fn(async () => {}),
|
||||||
|
detach: vi.fn(async () => {}),
|
||||||
|
sendCommand: vi.fn(async (_target: unknown, method: string) => {
|
||||||
|
if (method === "DOM.getDocument") return { root: { nodeId: 1 } };
|
||||||
|
if (method === "DOM.querySelector") {
|
||||||
|
if (code === "cdp_failed") throw new Error("selector query failed");
|
||||||
|
return { nodeId: 0 };
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}),
|
||||||
|
onEvent: { addListener: vi.fn(), removeListener: vi.fn() },
|
||||||
|
onDetach: { addListener: vi.fn(), removeListener: vi.fn() },
|
||||||
|
};
|
||||||
|
const cdp = new ChromiumCdp(debuggerApi as unknown as CdpDebuggerApi);
|
||||||
|
|
||||||
|
const hover = await handleHover(
|
||||||
|
sm,
|
||||||
|
{ session_id: "aa11", tab_id: 7, selector: "#missing" },
|
||||||
|
{
|
||||||
|
cdp,
|
||||||
|
tabsApi: { get: api.get, query: vi.fn(async () => []) },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
expect(hover).toMatchObject({ code });
|
||||||
|
expect(cdp.isAttached(7)).toBe(true);
|
||||||
|
|
||||||
|
const returned = await handleTabReturn(
|
||||||
|
sm,
|
||||||
|
{ session_id: "aa11", tab_id: 7 },
|
||||||
|
{
|
||||||
|
tabs: api,
|
||||||
|
windows,
|
||||||
|
cdp,
|
||||||
|
agentOverlayReset: { resetAgentOverlays: vi.fn(async () => {}) },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
expect(returned).toMatchObject({ returned_to_window_id: 200 });
|
||||||
|
expect(ctx.borrowedTabs.has(7)).toBe(false);
|
||||||
|
expect(cdp.isAttached(7)).toBe(false);
|
||||||
|
expect(debuggerApi.detach).toHaveBeenCalledExactlyOnceWith({ tabId: 7 });
|
||||||
|
cdp.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("honours cancellation while preparing a validated return", async () => {
|
||||||
|
const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]) });
|
||||||
|
const ctx = await sm.start("aa11");
|
||||||
|
ctx.borrowedTabs.set(7, { tabId: 7, originalWindowId: 200, originalIndex: 4 });
|
||||||
|
const state: FakeTabState = {
|
||||||
|
tabs: new Map([[7, { id: 7, windowId: 100 } as chrome.tabs.Tab]]),
|
||||||
|
nextTabId: 50,
|
||||||
|
windowsClosed: new Set(),
|
||||||
|
};
|
||||||
|
const { api, spies } = makeTabMutationApi(state);
|
||||||
|
const { api: windows } = makeWindowsApi(state);
|
||||||
|
const controller = new AbortController();
|
||||||
|
const beforeReturn = vi.fn(async () => {
|
||||||
|
controller.abort();
|
||||||
|
});
|
||||||
|
const result = await handleTabReturn(
|
||||||
|
sm,
|
||||||
|
{ session_id: "aa11", tab_id: 7 },
|
||||||
|
{
|
||||||
|
tabs: api,
|
||||||
|
windows,
|
||||||
|
signal: controller.signal,
|
||||||
|
beforeReturn,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result).toMatchObject({ code: "cancelled" });
|
||||||
|
expect(beforeReturn).toHaveBeenCalledExactlyOnceWith("aa11", 7);
|
||||||
|
expect(spies.move).not.toHaveBeenCalled();
|
||||||
|
expect(ctx.borrowedTabs.has(7)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
"original",
|
||||||
|
"fallback",
|
||||||
|
"cleanup_failed",
|
||||||
|
"failed",
|
||||||
|
"cancelled",
|
||||||
|
])("releases CDP only after a successful return (%s)", async (mode) => {
|
||||||
|
const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]) });
|
||||||
|
const ctx = await sm.start("aa11");
|
||||||
|
ctx.borrowedTabs.set(7, { tabId: 7, originalWindowId: 200, originalIndex: 4 });
|
||||||
|
const state: FakeTabState = {
|
||||||
|
tabs: new Map([[7, { id: 7, windowId: 100 } as chrome.tabs.Tab]]),
|
||||||
|
nextTabId: 50,
|
||||||
|
windowsClosed: new Set(),
|
||||||
|
};
|
||||||
|
const { api, spies } = makeTabMutationApi(state);
|
||||||
|
const { api: windowsApi } = makeWindowsApi(state, { lastFocused: 500 });
|
||||||
|
const success = mode !== "failed" && mode !== "cancelled";
|
||||||
|
if (mode === "fallback") spies.move.mockRejectedValueOnce(new Error("original move failed"));
|
||||||
|
if (mode === "failed") spies.move.mockRejectedValue(new Error("move failed"));
|
||||||
|
let windowAtRelease: number | undefined;
|
||||||
|
const cdp = {
|
||||||
|
releaseSessionTab: vi.fn(async () => {
|
||||||
|
windowAtRelease = state.tabs.get(7)?.windowId;
|
||||||
|
if (mode === "cleanup_failed") throw new Error("debugger release failed");
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
const controller = new AbortController();
|
||||||
|
if (mode === "cancelled") controller.abort();
|
||||||
|
|
||||||
|
const result = await handleTabReturn(
|
||||||
|
sm,
|
||||||
|
{ session_id: "aa11", tab_id: 7 },
|
||||||
|
{
|
||||||
|
tabs: api,
|
||||||
|
windows: windowsApi,
|
||||||
|
cdp,
|
||||||
|
signal: controller.signal,
|
||||||
|
agentOverlayReset: { resetAgentOverlays: vi.fn(async () => {}) },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect("code" in result).toBe(!success);
|
||||||
|
expect(ctx.borrowedTabs.has(7)).toBe(!success);
|
||||||
|
expect(cdp.releaseSessionTab).toHaveBeenCalledTimes(success ? 1 : 0);
|
||||||
|
if (success) {
|
||||||
|
expect(cdp.releaseSessionTab).toHaveBeenCalledWith("aa11", 7);
|
||||||
|
expect(windowAtRelease).toBe(mode === "fallback" ? 500 : 200);
|
||||||
|
expect(spies.move).toHaveBeenCalledTimes(mode === "fallback" ? 2 : 1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it("returns the tab to its original window/index and clears borrowedTabs", async () => {
|
it("returns the tab to its original window/index and clears borrowedTabs", async () => {
|
||||||
const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]) });
|
const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]) });
|
||||||
const ctx = await sm.start("aa11");
|
const ctx = await sm.start("aa11");
|
||||||
|
|||||||
@@ -345,7 +345,11 @@ export class ToolDispatcher {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
case "tool.tab_return":
|
case "tool.tab_return":
|
||||||
return handleTabReturn(this.sessions, req.params as TabReturnParams, { signal });
|
return handleTabReturn(this.sessions, req.params as TabReturnParams, {
|
||||||
|
signal,
|
||||||
|
cdp: this.cdp,
|
||||||
|
beforeReturn: (sessionId, tabId) => this.releaseHoverLatch(sessionId, tabId),
|
||||||
|
});
|
||||||
case "tool.window_resize":
|
case "tool.window_resize":
|
||||||
return handleWindowResize(
|
return handleWindowResize(
|
||||||
this.sessions,
|
this.sessions,
|
||||||
|
|||||||
@@ -183,6 +183,8 @@ export async function resolveBackendNode(
|
|||||||
}
|
}
|
||||||
// selector path
|
// selector path
|
||||||
try {
|
try {
|
||||||
|
// Selector lookup itself attaches CDP, even when no element is found.
|
||||||
|
cdp.trackSessionTab?.(ctx.sessionId, target.tabId);
|
||||||
const doc = await cdp.send<{ root?: { nodeId?: number } }>(target.tabId, "DOM.getDocument", {
|
const doc = await cdp.send<{ root?: { nodeId?: number } }>(target.tabId, "DOM.getDocument", {
|
||||||
depth: 0,
|
depth: 0,
|
||||||
});
|
});
|
||||||
@@ -607,6 +609,53 @@ type SelectMutationResult =
|
|||||||
// tool.fill
|
// tool.fill
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
type FillFailureReason =
|
||||||
|
| "target_not_fillable"
|
||||||
|
| "fill_value_invalid"
|
||||||
|
| "fill_target_changed"
|
||||||
|
| "fill_focus_lost"
|
||||||
|
| "fill_value_mismatch"
|
||||||
|
| "fill_failed";
|
||||||
|
|
||||||
|
type FillPreparation =
|
||||||
|
| { before: string; expected: string }
|
||||||
|
| { reason: FillFailureReason; error: string };
|
||||||
|
|
||||||
|
type FillReadiness = "ready" | "background" | "fill_target_changed" | "fill_focus_lost";
|
||||||
|
|
||||||
|
function fillError(reason: FillFailureReason, message: string): RpcError {
|
||||||
|
return rpcError(
|
||||||
|
reason === "target_not_fillable" || reason === "fill_value_invalid"
|
||||||
|
? "invalid_params"
|
||||||
|
: "cdp_failed",
|
||||||
|
reason,
|
||||||
|
message,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface FillScriptReply<T> {
|
||||||
|
result?: { value?: T };
|
||||||
|
exceptionDetails?: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
const FILL_EDITABLE_FUNCTION = `function() {
|
||||||
|
const tag = this.tagName.toLowerCase();
|
||||||
|
const supported = tag === 'input'
|
||||||
|
? ['text', 'search', 'tel', 'url', 'email', 'password', 'number'].includes(this.type)
|
||||||
|
: tag === 'textarea' || this.isContentEditable;
|
||||||
|
return this.isConnected && supported && !this.readOnly && !this.matches(':disabled');
|
||||||
|
}`;
|
||||||
|
|
||||||
|
// Chrome renders a trailing editable newline with an empty <div><br></div>.
|
||||||
|
// innerText includes the padding break; it is not an extra typed character.
|
||||||
|
const FILL_VALUE_FUNCTION = `function() {
|
||||||
|
if (!this.isContentEditable) return this.value;
|
||||||
|
const value = this.innerText;
|
||||||
|
const tail = this.lastChild;
|
||||||
|
const padding = tail && tail.nodeName === 'DIV' && tail.childNodes.length === 1 && tail.firstChild.nodeName === 'BR';
|
||||||
|
return padding && value.endsWith('\\n\\n') ? value.slice(0, -1) : value;
|
||||||
|
}`;
|
||||||
|
|
||||||
export async function handleFill(
|
export async function handleFill(
|
||||||
manager: SessionManager,
|
manager: SessionManager,
|
||||||
params: FillParams,
|
params: FillParams,
|
||||||
@@ -658,12 +707,8 @@ export async function handleFill(
|
|||||||
if (throwIfAborted(deps.signal)) {
|
if (throwIfAborted(deps.signal)) {
|
||||||
return { code: "cancelled", message: "fill aborted" };
|
return { code: "cancelled", message: "fill aborted" };
|
||||||
}
|
}
|
||||||
await nodeCdp.send(target.tabId, "DOM.focus", { backendNodeId: node.backendNodeId });
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return {
|
return fillError("fill_failed", err instanceof Error ? err.message : String(err));
|
||||||
code: "cdp_failed",
|
|
||||||
message: err instanceof Error ? err.message : String(err),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (throwIfAborted(deps.signal)) {
|
if (throwIfAborted(deps.signal)) {
|
||||||
@@ -676,57 +721,252 @@ export async function handleFill(
|
|||||||
const clearBefore = params.clear_before ?? true;
|
const clearBefore = params.clear_before ?? true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (clearBefore) {
|
if (throwIfAborted(deps.signal)) {
|
||||||
// Clear input/textarea value or wipe contenteditable innerText,
|
return { code: "cancelled", message: "fill aborted" };
|
||||||
// then fire `input` so frameworks observe the empty state.
|
}
|
||||||
await nodeCdp.send(target.tabId, "Runtime.callFunctionOn", {
|
try {
|
||||||
objectId,
|
await nodeCdp.send(target.tabId, "DOM.focus", { backendNodeId: node.backendNodeId });
|
||||||
functionDeclaration: `function() {
|
} catch (error) {
|
||||||
if (this.isContentEditable) { this.textContent = ''; }
|
// Chrome rejects DOM.focus for disabled controls, including inherited
|
||||||
else {
|
// fieldset state. Classify the live target before treating this as a
|
||||||
const proto = this instanceof HTMLTextAreaElement
|
// browser failure; the normal path needs no extra round trip.
|
||||||
? HTMLTextAreaElement.prototype
|
const editable = await nodeCdp.send<FillScriptReply<boolean>>(
|
||||||
: HTMLInputElement.prototype;
|
target.tabId,
|
||||||
const descriptor = Object.getOwnPropertyDescriptor(proto, 'value');
|
"Runtime.callFunctionOn",
|
||||||
if (descriptor && descriptor.set) descriptor.set.call(this, '');
|
{ objectId, functionDeclaration: FILL_EDITABLE_FUNCTION, returnByValue: true },
|
||||||
else this.value = '';
|
);
|
||||||
}
|
if (throwIfAborted(deps.signal)) {
|
||||||
this.dispatchEvent(new Event('input', { bubbles: true }));
|
return { code: "cancelled", message: "fill aborted" };
|
||||||
}`,
|
}
|
||||||
returnByValue: true,
|
if (!editable.exceptionDetails && editable.result?.value === false) {
|
||||||
});
|
return fillError(
|
||||||
|
"target_not_fillable",
|
||||||
|
"fill target is not editable or its input type is unsupported",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
if (throwIfAborted(deps.signal)) {
|
if (throwIfAborted(deps.signal)) {
|
||||||
return { code: "cancelled", message: "fill aborted" };
|
return { code: "cancelled", message: "fill aborted" };
|
||||||
}
|
}
|
||||||
|
// Check editability before clearing. Keep the expected result tied to
|
||||||
|
// this object, including the existing value on the append path.
|
||||||
|
const prepared = await nodeCdp.send<FillScriptReply<FillPreparation>>(
|
||||||
|
target.tabId,
|
||||||
|
"Runtime.callFunctionOn",
|
||||||
|
{
|
||||||
|
objectId,
|
||||||
|
functionDeclaration: `function(value, clearBefore) {
|
||||||
|
const tag = this.tagName.toLowerCase();
|
||||||
|
const native = tag === 'input' || tag === 'textarea';
|
||||||
|
if (!(${FILL_EDITABLE_FUNCTION}).call(this)) return { reason: 'target_not_fillable', error: 'fill target is not editable or its input type is unsupported' };
|
||||||
|
if (this.getRootNode().activeElement !== this) return { reason: 'fill_focus_lost', error: 'fill target does not have focus' };
|
||||||
|
const before = clearBefore ? '' : (${FILL_VALUE_FUNCTION}).call(this);
|
||||||
|
let expected = before + value;
|
||||||
|
if (native) {
|
||||||
|
// Use the browser's own value sanitization (e.g. textarea
|
||||||
|
// line endings) without changing the live control.
|
||||||
|
const normalizer = this.ownerDocument.createElement(tag);
|
||||||
|
if (tag === 'input') {
|
||||||
|
normalizer.type = this.type;
|
||||||
|
normalizer.multiple = this.multiple;
|
||||||
|
}
|
||||||
|
// insertText treats line breaks in a single-line input as spaces.
|
||||||
|
if (tag === 'input') expected = expected.replace(/\\r\\n|\\r|\\n/g, ' ');
|
||||||
|
normalizer.value = expected;
|
||||||
|
if (expected !== '' && normalizer.value === '' && this.type === 'number') {
|
||||||
|
return { reason: 'fill_value_invalid', error: 'fill value is not valid for a number input' };
|
||||||
|
}
|
||||||
|
expected = normalizer.value;
|
||||||
|
if (this.type !== 'number' && this.maxLength >= 0 && expected.length > this.maxLength) {
|
||||||
|
return { reason: 'fill_value_invalid', error: 'fill value exceeds the target maxlength' };
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
expected = expected.replace(/\\r\\n?/g, '\\n');
|
||||||
|
}
|
||||||
|
if (clearBefore) {
|
||||||
|
if (native) {
|
||||||
|
const proto = tag === 'textarea' ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype;
|
||||||
|
Object.getOwnPropertyDescriptor(proto, 'value').set.call(this, '');
|
||||||
|
} else {
|
||||||
|
this.textContent = '';
|
||||||
|
}
|
||||||
|
this.dispatchEvent(new Event('input', { bubbles: true }));
|
||||||
|
}
|
||||||
|
return { before, expected };
|
||||||
|
}`,
|
||||||
|
arguments: [{ value: params.value }, { value: clearBefore }],
|
||||||
|
returnByValue: true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (throwIfAborted(deps.signal)) {
|
||||||
|
return { code: "cancelled", message: "fill aborted" };
|
||||||
|
}
|
||||||
|
if (prepared.exceptionDetails) {
|
||||||
|
return fillError("fill_failed", "fill preparation script failed");
|
||||||
|
}
|
||||||
|
const preparation = prepared.result?.value;
|
||||||
|
if (preparation && "error" in preparation) {
|
||||||
|
return fillError(preparation.reason, preparation.error);
|
||||||
|
}
|
||||||
|
if (typeof preparation?.before !== "string" || typeof preparation?.expected !== "string") {
|
||||||
|
return fillError("fill_failed", "fill preparation returned an unexpected result");
|
||||||
|
}
|
||||||
|
// A separate call runs after the clearing event's microtasks drain.
|
||||||
|
// DOM focus works in background tabs. Use document.hasFocus() only to
|
||||||
|
// detect deferred focus events, never to reject background input.
|
||||||
|
const checkReady = async (): Promise<FillReadiness | RpcError> => {
|
||||||
|
const reply = await nodeCdp.send<FillScriptReply<FillReadiness>>(
|
||||||
|
target.tabId,
|
||||||
|
"Runtime.callFunctionOn",
|
||||||
|
{
|
||||||
|
objectId,
|
||||||
|
functionDeclaration: `function(before) {
|
||||||
|
if (!(${FILL_EDITABLE_FUNCTION}).call(this) ||
|
||||||
|
(${FILL_VALUE_FUNCTION}).call(this) !== before) return 'fill_target_changed';
|
||||||
|
if (this.getRootNode().activeElement !== this) return 'fill_focus_lost';
|
||||||
|
return this.ownerDocument.hasFocus() ? 'ready' : 'background';
|
||||||
|
}`,
|
||||||
|
arguments: [{ value: preparation.before }],
|
||||||
|
returnByValue: true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
const state = reply.result?.value;
|
||||||
|
if (
|
||||||
|
reply.exceptionDetails ||
|
||||||
|
(state !== "ready" &&
|
||||||
|
state !== "background" &&
|
||||||
|
state !== "fill_target_changed" &&
|
||||||
|
state !== "fill_focus_lost")
|
||||||
|
) {
|
||||||
|
return fillError(
|
||||||
|
"fill_failed",
|
||||||
|
"fill readiness script failed or returned an unexpected result",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
};
|
||||||
|
let ready = await checkReady();
|
||||||
|
if (throwIfAborted(deps.signal)) {
|
||||||
|
return { code: "cancelled", message: "fill aborted" };
|
||||||
|
}
|
||||||
|
// Restore focus once only if clearing left this same target editable
|
||||||
|
// and unchanged. Recheck after focus handlers have run.
|
||||||
|
if (clearBefore && ready === "fill_focus_lost") {
|
||||||
|
await nodeCdp.send(target.tabId, "DOM.focus", { backendNodeId: node.backendNodeId });
|
||||||
|
if (throwIfAborted(deps.signal)) {
|
||||||
|
return { code: "cancelled", message: "fill aborted" };
|
||||||
|
}
|
||||||
|
ready = await checkReady();
|
||||||
|
}
|
||||||
|
if (throwIfAborted(deps.signal)) {
|
||||||
|
return { code: "cancelled", message: "fill aborted" };
|
||||||
|
}
|
||||||
|
if (typeof ready !== "string") return ready;
|
||||||
|
if (ready !== "ready" && ready !== "background") {
|
||||||
|
return fillError(ready, "fill target changed or lost focus before typing");
|
||||||
|
}
|
||||||
|
if (params.value !== "" && (!clearBefore || ready === "background")) {
|
||||||
|
// Native editing commands also support number/email inputs, whose
|
||||||
|
// selection APIs cannot set a caret, and multiline contenteditables.
|
||||||
|
// Also do this for background replacement: CDP input focuses the
|
||||||
|
// renderer, delivering deferred focus events before it inserts text.
|
||||||
|
// Use a command-only event so this does not invoke an End shortcut.
|
||||||
|
try {
|
||||||
|
await deps.cdp.send(target.tabId, "Input.dispatchKeyEvent", {
|
||||||
|
type: "rawKeyDown",
|
||||||
|
commands: ["moveToEndOfDocument"],
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
await deps.cdp.send(target.tabId, "Input.dispatchKeyEvent", { type: "keyUp" });
|
||||||
|
}
|
||||||
|
if (throwIfAborted(deps.signal)) {
|
||||||
|
return { code: "cancelled", message: "fill aborted" };
|
||||||
|
}
|
||||||
|
// Focus and key handlers can change the target or move focus too.
|
||||||
|
ready = await checkReady();
|
||||||
|
if (throwIfAborted(deps.signal)) {
|
||||||
|
return { code: "cancelled", message: "fill aborted" };
|
||||||
|
}
|
||||||
|
if (typeof ready !== "string") return ready;
|
||||||
|
if (ready !== "ready" && ready !== "background") {
|
||||||
|
return fillError(ready, "fill target changed or lost focus while positioning the caret");
|
||||||
|
}
|
||||||
|
}
|
||||||
// CDP `Input.insertText` handles IME / multi-byte input out of the
|
// CDP `Input.insertText` handles IME / multi-byte input out of the
|
||||||
// box, much more reliably than per-key `dispatchKeyEvent`.
|
// box, much more reliably than per-key `dispatchKeyEvent`.
|
||||||
await deps.cdp.send(target.tabId, "Input.insertText", { text: params.value });
|
if (params.value !== "") {
|
||||||
|
await deps.cdp.send(target.tabId, "Input.insertText", { text: params.value });
|
||||||
|
}
|
||||||
if (throwIfAborted(deps.signal)) {
|
if (throwIfAborted(deps.signal)) {
|
||||||
return { code: "cancelled", message: "fill aborted" };
|
return { code: "cancelled", message: "fill aborted" };
|
||||||
}
|
}
|
||||||
// Fire `input` + `change` so React / Vue controlled inputs commit.
|
// Keep the existing notifications, then read in a separate call so
|
||||||
await nodeCdp.send(target.tabId, "Runtime.callFunctionOn", {
|
// nested microtasks queued by the page cannot race the verification.
|
||||||
|
const notified = await nodeCdp.send<FillScriptReply<unknown>>(
|
||||||
|
target.tabId,
|
||||||
|
"Runtime.callFunctionOn",
|
||||||
|
{
|
||||||
|
objectId,
|
||||||
|
functionDeclaration: `function() {
|
||||||
|
this.dispatchEvent(new Event('input', { bubbles: true }));
|
||||||
|
this.dispatchEvent(new Event('change', { bubbles: true }));
|
||||||
|
}`,
|
||||||
|
returnByValue: true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (throwIfAborted(deps.signal)) {
|
||||||
|
return { code: "cancelled", message: "fill aborted" };
|
||||||
|
}
|
||||||
|
if (notified.exceptionDetails) {
|
||||||
|
return fillError("fill_failed", "fill notification script failed");
|
||||||
|
}
|
||||||
|
const verified = await nodeCdp.send<
|
||||||
|
FillScriptReply<{ connected: boolean; matches: boolean; valueLength: number }>
|
||||||
|
>(target.tabId, "Runtime.callFunctionOn", {
|
||||||
objectId,
|
objectId,
|
||||||
functionDeclaration: `function() {
|
functionDeclaration: `function(expected) {
|
||||||
this.dispatchEvent(new Event('input', { bubbles: true }));
|
const value = (${FILL_VALUE_FUNCTION}).call(this);
|
||||||
this.dispatchEvent(new Event('change', { bubbles: true }));
|
return { connected: this.isConnected, matches: value === expected, valueLength: value.length };
|
||||||
}`,
|
}`,
|
||||||
|
arguments: [{ value: preparation.expected }],
|
||||||
returnByValue: true,
|
returnByValue: true,
|
||||||
});
|
});
|
||||||
|
if (throwIfAborted(deps.signal)) {
|
||||||
|
return { code: "cancelled", message: "fill aborted" };
|
||||||
|
}
|
||||||
|
const verification = verified.result?.value;
|
||||||
|
if (
|
||||||
|
verified.exceptionDetails ||
|
||||||
|
typeof verification?.connected !== "boolean" ||
|
||||||
|
typeof verification?.matches !== "boolean" ||
|
||||||
|
typeof verification?.valueLength !== "number"
|
||||||
|
) {
|
||||||
|
return fillError(
|
||||||
|
"fill_failed",
|
||||||
|
"fill verification script failed or returned an unexpected result",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (!verification.connected) {
|
||||||
|
return fillError("fill_target_changed", "fill target was removed or replaced during input");
|
||||||
|
}
|
||||||
|
if (!verification.matches) {
|
||||||
|
return fillError(
|
||||||
|
"fill_value_mismatch",
|
||||||
|
"fill could not verify the expected value; observe the page before deciding whether to retry",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return attachDialogs(deps.cdp, target.tabId, dialogCursor, {
|
||||||
|
tab_id: target.tabId,
|
||||||
|
used_ref: node.usedRef,
|
||||||
|
used_selector: node.usedSelector,
|
||||||
|
value_length: verification.valueLength,
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return {
|
return fillError("fill_failed", err instanceof Error ? err.message : String(err));
|
||||||
code: "cdp_failed",
|
} finally {
|
||||||
message: err instanceof Error ? err.message : String(err),
|
await nodeCdp.send(target.tabId, "Runtime.releaseObject", { objectId }).catch(() => undefined);
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return attachDialogs(deps.cdp, target.tabId, dialogCursor, {
|
|
||||||
tab_id: target.tabId,
|
|
||||||
used_ref: node.usedRef,
|
|
||||||
used_selector: node.usedSelector,
|
|
||||||
value_length: params.value.length,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { type SessionManager, SessionStartCleanupError } from "@/session-manager
|
|||||||
import type { RpcError } from "@/transport/types";
|
import type { RpcError } from "@/transport/types";
|
||||||
import { rpcError } from "./errors";
|
import { rpcError } from "./errors";
|
||||||
import { clearRecordingForSession } from "./record";
|
import { clearRecordingForSession } from "./record";
|
||||||
import type { ChromeTabsApi } from "./shared";
|
import type { CdpRunner, ChromeTabsApi } from "./shared";
|
||||||
import { isRpcError } from "./shared";
|
import { isRpcError } from "./shared";
|
||||||
import { returnBorrowedTab, type TabManagementDeps } from "./tabs";
|
import { returnBorrowedTab, type TabManagementDeps } from "./tabs";
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@ export interface SessionStopResult {
|
|||||||
|
|
||||||
export interface SessionStopDeps {
|
export interface SessionStopDeps {
|
||||||
signal?: AbortSignal;
|
signal?: AbortSignal;
|
||||||
cdp?: {
|
cdp?: Pick<CdpRunner, "releaseSessionTab"> & {
|
||||||
detachSession(sessionId: string): Promise<void>;
|
detachSession(sessionId: string): Promise<void>;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
@@ -209,6 +209,7 @@ export async function handleSessionStop(
|
|||||||
try {
|
try {
|
||||||
const tabManagement = {
|
const tabManagement = {
|
||||||
...(deps.tabManagement ?? {}),
|
...(deps.tabManagement ?? {}),
|
||||||
|
cdp: deps.cdp ?? deps.tabManagement?.cdp,
|
||||||
signal: deps.signal,
|
signal: deps.signal,
|
||||||
isAgentWindowId:
|
isAgentWindowId:
|
||||||
deps.tabManagement?.isAgentWindowId ??
|
deps.tabManagement?.isAgentWindowId ??
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ export interface CdpRunner {
|
|||||||
getFrameGraph?(tabId: number): Promise<CdpFrameGraph>;
|
getFrameGraph?(tabId: number): Promise<CdpFrameGraph>;
|
||||||
ensureAttachedToUrl?(tabId: number, expectedUrl: string | undefined): Promise<void>;
|
ensureAttachedToUrl?(tabId: number, expectedUrl: string | undefined): Promise<void>;
|
||||||
trackSessionTab?(sessionId: string, tabId: number): void;
|
trackSessionTab?(sessionId: string, tabId: number): void;
|
||||||
|
releaseSessionTab?(sessionId: string, tabId: number): Promise<void>;
|
||||||
onEvent?(handler: (source: CdpDebuggee, method: string, params: unknown) => void): {
|
onEvent?(handler: (source: CdpDebuggee, method: string, params: unknown) => void): {
|
||||||
dispose(): void;
|
dispose(): void;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import {
|
|||||||
import type { SessionContext, SessionManager } from "@/session-manager/manager";
|
import type { SessionContext, SessionManager } from "@/session-manager/manager";
|
||||||
import type { RpcError } from "@/transport/types";
|
import type { RpcError } from "@/transport/types";
|
||||||
import { rpcError } from "./errors";
|
import { rpcError } from "./errors";
|
||||||
import { isRpcError, lookupSession } from "./shared";
|
import { type CdpRunner, isRpcError, lookupSession } from "./shared";
|
||||||
|
|
||||||
export type TabScope = "user" | "agent" | "all";
|
export type TabScope = "user" | "agent" | "all";
|
||||||
|
|
||||||
@@ -287,6 +287,10 @@ export interface TabManagementDeps {
|
|||||||
approveBorrow?: BorrowConfirmationApprover;
|
approveBorrow?: BorrowConfirmationApprover;
|
||||||
/** Clears Agent-scoped overlays after a borrowed tab is returned. */
|
/** Clears Agent-scoped overlays after a borrowed tab is returned. */
|
||||||
agentOverlayReset?: AgentOverlayResetApi;
|
agentOverlayReset?: AgentOverlayResetApi;
|
||||||
|
/** Releases this session's CDP claim after a borrowed tab is returned. */
|
||||||
|
cdp?: Pick<CdpRunner, "releaseSessionTab">;
|
||||||
|
/** Runs after tab_return validation, before moving the borrowed tab. */
|
||||||
|
beforeReturn?: (sessionId: string, tabId: number) => Promise<void>;
|
||||||
/**
|
/**
|
||||||
* Reports whether `windowId` is any live session's Agent Window.
|
* Reports whether `windowId` is any live session's Agent Window.
|
||||||
* `tab_return`'s fallback window picker uses this to avoid moving a
|
* `tab_return`'s fallback window picker uses this to avoid moving a
|
||||||
@@ -945,16 +949,22 @@ async function cleanupUnusedFallbackWindow(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function resetAgentOverlaysInReturnedTab(
|
async function releaseReturnedTabState(
|
||||||
ctx: SessionContext,
|
ctx: SessionContext,
|
||||||
tabId: number,
|
tabId: number,
|
||||||
deps: TabManagementDeps,
|
deps: TabManagementDeps,
|
||||||
): void {
|
): Promise<void> {
|
||||||
void getAgentOverlayResetApi(deps)
|
void getAgentOverlayResetApi(deps)
|
||||||
.resetAgentOverlays(tabId, ctx.sessionId)
|
.resetAgentOverlays(tabId, ctx.sessionId)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.debug("[bsk tab_return] agent overlay reset failed", err);
|
console.debug("[bsk tab_return] agent overlay reset failed", err);
|
||||||
});
|
});
|
||||||
|
try {
|
||||||
|
await deps.cdp?.releaseSessionTab?.(ctx.sessionId, tabId);
|
||||||
|
} catch (err) {
|
||||||
|
// A successful move must not be retried because debugger cleanup failed.
|
||||||
|
console.debug("[bsk tab_return] CDP release failed", err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1028,7 +1038,7 @@ export async function returnBorrowedTab(
|
|||||||
});
|
});
|
||||||
const movedTab = Array.isArray(moved) ? moved[0] : moved;
|
const movedTab = Array.isArray(moved) ? moved[0] : moved;
|
||||||
const finalIndex = typeof movedTab?.index === "number" ? movedTab.index : targetIndex;
|
const finalIndex = typeof movedTab?.index === "number" ? movedTab.index : targetIndex;
|
||||||
resetAgentOverlaysInReturnedTab(ctx, tabId, deps);
|
await releaseReturnedTabState(ctx, tabId, deps);
|
||||||
return {
|
return {
|
||||||
tabId,
|
tabId,
|
||||||
toWindowId: targetWindowId,
|
toWindowId: targetWindowId,
|
||||||
@@ -1070,7 +1080,7 @@ export async function returnBorrowedTab(
|
|||||||
});
|
});
|
||||||
const movedTab = Array.isArray(moved) ? moved[0] : moved;
|
const movedTab = Array.isArray(moved) ? moved[0] : moved;
|
||||||
const finalIndex = typeof movedTab?.index === "number" ? movedTab.index : target.index;
|
const finalIndex = typeof movedTab?.index === "number" ? movedTab.index : target.index;
|
||||||
resetAgentOverlaysInReturnedTab(ctx, tabId, deps);
|
await releaseReturnedTabState(ctx, tabId, deps);
|
||||||
return {
|
return {
|
||||||
tabId,
|
tabId,
|
||||||
toWindowId: target.windowId,
|
toWindowId: target.windowId,
|
||||||
@@ -1116,6 +1126,7 @@ export async function handleTabReturn(
|
|||||||
message: `tab_return: tab ${params.tab_id} is not borrowed by this session`,
|
message: `tab_return: tab ${params.tab_id} is not borrowed by this session`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
await deps.beforeReturn?.(ctx.sessionId, params.tab_id);
|
||||||
const outcome = await returnBorrowedTab(ctx, params.tab_id, {
|
const outcome = await returnBorrowedTab(ctx, params.tab_id, {
|
||||||
...deps,
|
...deps,
|
||||||
isAgentWindowId:
|
isAgentWindowId:
|
||||||
|
|||||||
@@ -215,21 +215,29 @@ function makeCdp(snapshot: unknown) {
|
|||||||
if (method === "Runtime.evaluate") {
|
if (method === "Runtime.evaluate") {
|
||||||
return {
|
return {
|
||||||
result: {
|
result: {
|
||||||
value: {
|
deepSerializedValue: {
|
||||||
controls: [
|
type: "array",
|
||||||
|
value: [
|
||||||
{
|
{
|
||||||
state: "filled",
|
type: "array",
|
||||||
sensitive: true,
|
value: [
|
||||||
defaultValue: "hunter2",
|
{ type: "node", value: { backendNodeId: 13 } },
|
||||||
value: "hunter2",
|
{
|
||||||
placeholder: "secret",
|
type: "string",
|
||||||
|
value: JSON.stringify({
|
||||||
|
state: "filled",
|
||||||
|
sensitive: true,
|
||||||
|
placeholder: "secret",
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
childFrames: [],
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
if (method === "Runtime.releaseObjectGroup") return {};
|
||||||
throw new Error(`unexpected ${method}`);
|
throw new Error(`unexpected ${method}`);
|
||||||
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
}) as unknown as <T>(tabId: number, method: string, params?: object) => Promise<T>,
|
||||||
};
|
};
|
||||||
@@ -273,8 +281,11 @@ describe("captureViewModel", () => {
|
|||||||
expect(cdp.send).toHaveBeenCalledWith(
|
expect(cdp.send).toHaveBeenCalledWith(
|
||||||
4,
|
4,
|
||||||
"Runtime.evaluate",
|
"Runtime.evaluate",
|
||||||
expect.objectContaining({ returnByValue: true }),
|
expect.objectContaining({
|
||||||
|
serializationOptions: expect.objectContaining({ serialization: "deep" }),
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
|
expect(cdp.send).toHaveBeenCalledWith(4, "Runtime.releaseObjectGroup", expect.anything());
|
||||||
expect(cdp.send).not.toHaveBeenCalledWith(4, "DOM.resolveNode", expect.anything());
|
expect(cdp.send).not.toHaveBeenCalledWith(4, "DOM.resolveNode", expect.anything());
|
||||||
expect(cdp.send).not.toHaveBeenCalledWith(4, "Runtime.callFunctionOn", expect.anything());
|
expect(cdp.send).not.toHaveBeenCalledWith(4, "Runtime.callFunctionOn", expect.anything());
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,270 @@
|
|||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
import { type CdpRunner, cdpRunnerForTarget } from "../../shared";
|
||||||
|
import { captureViewModel } from "../capture";
|
||||||
|
|
||||||
|
interface Control {
|
||||||
|
id: number;
|
||||||
|
value: string;
|
||||||
|
type?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function snapshot(groups: Control[][]) {
|
||||||
|
const strings: string[] = [];
|
||||||
|
const str = (value: string) => {
|
||||||
|
let index = strings.indexOf(value);
|
||||||
|
if (index < 0) index = strings.push(value) - 1;
|
||||||
|
return index;
|
||||||
|
};
|
||||||
|
const documents = groups.map((controls, groupIndex) => ({
|
||||||
|
frameId: `frame-${groupIndex}`,
|
||||||
|
nodes: {
|
||||||
|
parentIndex: [-1, 0, ...controls.map(() => 1)],
|
||||||
|
nodeName: [str("html"), str("body"), ...controls.map(() => str("input"))],
|
||||||
|
backendNodeId: [8000 + groupIndex * 2, 8001 + groupIndex * 2, ...controls.map((c) => c.id)],
|
||||||
|
attributes: [
|
||||||
|
[],
|
||||||
|
[],
|
||||||
|
...controls.map((c) => [
|
||||||
|
str("type"),
|
||||||
|
str(c.type ?? "text"),
|
||||||
|
str("placeholder"),
|
||||||
|
str("snapshot placeholder"),
|
||||||
|
]),
|
||||||
|
],
|
||||||
|
inputValue: {
|
||||||
|
index: controls.map((_, i) => i + 2),
|
||||||
|
value: controls.map((c) => str(c.value)),
|
||||||
|
},
|
||||||
|
contentDocumentIndex: { index: [] as number[], value: [] as number[] },
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
const root = documents[0].nodes;
|
||||||
|
for (let i = 1; i < documents.length; i++) {
|
||||||
|
root.contentDocumentIndex.index.push(root.backendNodeId.length);
|
||||||
|
root.contentDocumentIndex.value.push(i);
|
||||||
|
root.parentIndex.push(1);
|
||||||
|
root.nodeName.push(str("iframe"));
|
||||||
|
root.backendNodeId.push(9000 + i);
|
||||||
|
root.attributes.push([]);
|
||||||
|
}
|
||||||
|
return { strings, documents };
|
||||||
|
}
|
||||||
|
|
||||||
|
function state(value: string) {
|
||||||
|
return {
|
||||||
|
value,
|
||||||
|
defaultValue: "",
|
||||||
|
placeholder: "live placeholder",
|
||||||
|
state: value ? "filled" : "empty",
|
||||||
|
sensitive: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function reply(entries: Array<[number, unknown]>) {
|
||||||
|
return {
|
||||||
|
result: {
|
||||||
|
deepSerializedValue: {
|
||||||
|
type: "array",
|
||||||
|
value: entries.map(([backendNodeId, fields]) => ({
|
||||||
|
type: "array",
|
||||||
|
value: [
|
||||||
|
{ type: "node", value: { backendNodeId } },
|
||||||
|
{ type: "string", value: JSON.stringify(fields) },
|
||||||
|
],
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function fakeCdp(groups: Control[][], evaluate: (params: Record<string, unknown>) => unknown) {
|
||||||
|
const snap = snapshot(groups);
|
||||||
|
const send = vi.fn(async (_tabId: number, method: string, params?: object) => {
|
||||||
|
if (method === "DOMSnapshot.captureSnapshot") return snap;
|
||||||
|
if (method === "Runtime.evaluate") return evaluate(params as Record<string, unknown>);
|
||||||
|
return {};
|
||||||
|
});
|
||||||
|
return { send, cdp: { send: send as CdpRunner["send"] } };
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("form state identity", () => {
|
||||||
|
it("matches reordered controls by identity and preserves controls missing from the batch", async () => {
|
||||||
|
// The snapshot includes a shadow input. The runtime query skips it,
|
||||||
|
// encounters a new input, and sees the remaining inputs in a new order.
|
||||||
|
const { cdp } = fakeCdp(
|
||||||
|
[
|
||||||
|
[
|
||||||
|
{ id: 10, value: "" },
|
||||||
|
{ id: 11, value: "old email" },
|
||||||
|
{ id: 12, value: "old city" },
|
||||||
|
],
|
||||||
|
],
|
||||||
|
() =>
|
||||||
|
reply([
|
||||||
|
[99, state("new control")],
|
||||||
|
[12, state("上海")],
|
||||||
|
[11, state("alice@example.com")],
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
const captured = await captureViewModel(cdp, 7);
|
||||||
|
expect(captured.nodes.find((n) => n.backendNodeId === 10)).toMatchObject({
|
||||||
|
formValue: "",
|
||||||
|
formState: "empty",
|
||||||
|
formPlaceholder: "snapshot placeholder",
|
||||||
|
});
|
||||||
|
expect(captured.nodes.find((n) => n.backendNodeId === 11)).toMatchObject({
|
||||||
|
formValue: "alice@example.com",
|
||||||
|
});
|
||||||
|
expect(captured.nodes.find((n) => n.backendNodeId === 12)).toMatchObject({ formValue: "上海" });
|
||||||
|
expect(captured.nodes.some((n) => n.backendNodeId === 99)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not shift state between frames when one frame is absent from the runtime batch", async () => {
|
||||||
|
const { cdp } = fakeCdp(
|
||||||
|
[
|
||||||
|
[],
|
||||||
|
[{ id: 11, value: "inaccessible frame" }],
|
||||||
|
[{ id: 21, value: "old second" }],
|
||||||
|
[{ id: 31, value: "old third" }],
|
||||||
|
],
|
||||||
|
() =>
|
||||||
|
reply([
|
||||||
|
[31, state("third frame")],
|
||||||
|
[21, state("second frame")],
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
const captured = await captureViewModel(cdp, 7);
|
||||||
|
const controls = [...captured.iframeNodes.values()].flat();
|
||||||
|
expect(controls.find((n) => n.backendNodeId === 11)?.formValue).toBe("inaccessible frame");
|
||||||
|
expect(controls.find((n) => n.backendNodeId === 21)?.formValue).toBe("second frame");
|
||||||
|
expect(controls.find((n) => n.backendNodeId === 31)?.formValue).toBe("third frame");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps identical backend ids in different OOPIF targets separate", async () => {
|
||||||
|
const sendToTarget = vi.fn(async (target, method) => {
|
||||||
|
if (method === "DOMSnapshot.captureSnapshot") return snapshot([[{ id: 11, value: "old" }]]);
|
||||||
|
if (method === "Runtime.evaluate") return reply([[11, state(target.sessionId)]]);
|
||||||
|
return {};
|
||||||
|
});
|
||||||
|
const send = vi.fn(async () => {
|
||||||
|
throw new Error("unexpected top-level target");
|
||||||
|
});
|
||||||
|
const cdp = { send, sendToTarget } as unknown as CdpRunner;
|
||||||
|
for (const sessionId of ["left-frame", "right-frame"]) {
|
||||||
|
const captured = await captureViewModel(cdpRunnerForTarget(cdp, { tabId: 7, sessionId }), 7);
|
||||||
|
expect(captured.nodes.find((n) => n.backendNodeId === 11)?.formValue).toBe(sessionId);
|
||||||
|
expect(sendToTarget).toHaveBeenCalledWith(
|
||||||
|
{ tabId: 7, sessionId },
|
||||||
|
"Runtime.releaseObjectGroup",
|
||||||
|
expect.anything(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
expect(send).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
"unsupported",
|
||||||
|
"legacy",
|
||||||
|
"malformed",
|
||||||
|
"invalid-state",
|
||||||
|
])("preserves snapshot data when enrichment is %s", async (mode) => {
|
||||||
|
const { cdp, send } = fakeCdp([[{ id: 11, value: "snapshot value" }]], () => {
|
||||||
|
if (mode === "unsupported") throw new Error("serialization unsupported");
|
||||||
|
if (mode === "legacy") return { result: { value: { controls: [state("wrong value")] } } };
|
||||||
|
const result = reply([
|
||||||
|
[11, mode === "invalid-state" ? { value: "wrong value" } : state("wrong value")],
|
||||||
|
]);
|
||||||
|
if (mode === "malformed")
|
||||||
|
result.result.deepSerializedValue.value[0].value[1].value = "not json";
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
const captured = await captureViewModel(cdp, 7);
|
||||||
|
expect(captured.nodes.find((n) => n.backendNodeId === 11)).toMatchObject({
|
||||||
|
formValue: "snapshot value",
|
||||||
|
formPlaceholder: "snapshot placeholder",
|
||||||
|
});
|
||||||
|
expect(send).toHaveBeenCalledWith(7, "Runtime.releaseObjectGroup", expect.anything());
|
||||||
|
});
|
||||||
|
|
||||||
|
it("releases the batch's remote objects when capture is cancelled", async () => {
|
||||||
|
const controller = new AbortController();
|
||||||
|
const { cdp, send } = fakeCdp([[{ id: 11, value: "old" }]], () => {
|
||||||
|
controller.abort();
|
||||||
|
return reply([[11, state("new")]]);
|
||||||
|
});
|
||||||
|
await expect(captureViewModel(cdp, 7, { signal: controller.signal })).rejects.toMatchObject({
|
||||||
|
name: "AbortError",
|
||||||
|
});
|
||||||
|
expect(send).toHaveBeenCalledWith(7, "Runtime.releaseObjectGroup", expect.anything());
|
||||||
|
});
|
||||||
|
|
||||||
|
it("skips entries without node identity while still applying valid entries", async () => {
|
||||||
|
const { cdp } = fakeCdp(
|
||||||
|
[
|
||||||
|
[
|
||||||
|
{ id: 11, value: "snapshot value" },
|
||||||
|
{ id: 12, value: "old value" },
|
||||||
|
],
|
||||||
|
],
|
||||||
|
() => {
|
||||||
|
const result = reply([
|
||||||
|
[11, state("wrong value")],
|
||||||
|
[12, state("live value")],
|
||||||
|
]);
|
||||||
|
const element = result.result.deepSerializedValue.value[0].value[0];
|
||||||
|
delete (element.value as { backendNodeId?: number }).backendNodeId;
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
const captured = await captureViewModel(cdp, 7);
|
||||||
|
expect(captured.nodes.find((n) => n.backendNodeId === 11)?.formValue).toBe("snapshot value");
|
||||||
|
expect(captured.nodes.find((n) => n.backendNodeId === 12)?.formValue).toBe("live value");
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
"password",
|
||||||
|
"text",
|
||||||
|
])("redacts a sensitive %s control after identity matching", async (type) => {
|
||||||
|
const { cdp } = fakeCdp([[{ id: 11, value: "secret", type }]], () =>
|
||||||
|
reply([[11, { ...state("secret"), sensitive: true }]]),
|
||||||
|
);
|
||||||
|
const captured = await captureViewModel(cdp, 7);
|
||||||
|
const control = captured.nodes.find((n) => n.backendNodeId === 11);
|
||||||
|
expect(control?.formState).toBe("filled");
|
||||||
|
expect(control?.formValue).toBeUndefined();
|
||||||
|
expect(control?.formDefaultValue).toBeUndefined();
|
||||||
|
expect(control?.attrs.value).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps one bounded batch without per-control CDP queries", async () => {
|
||||||
|
const controls = Array.from({ length: 251 }, (_, i) => ({ id: i + 1, value: "old" }));
|
||||||
|
const elements = controls.map((c) => ({
|
||||||
|
backendNodeId: c.id,
|
||||||
|
tagName: "INPUT",
|
||||||
|
type: "text",
|
||||||
|
value: `live ${c.id}`,
|
||||||
|
defaultValue: "",
|
||||||
|
placeholder: "",
|
||||||
|
}));
|
||||||
|
const { cdp, send } = fakeCdp([controls], (params) => {
|
||||||
|
const doc = {
|
||||||
|
querySelectorAll: (selector: string) =>
|
||||||
|
selector === "input,textarea,select" ? elements : [],
|
||||||
|
};
|
||||||
|
const entries = new Function("document", `return ${params.expression}`)(doc) as Array<
|
||||||
|
[{ backendNodeId: number }, string]
|
||||||
|
>;
|
||||||
|
expect(entries).toHaveLength(250);
|
||||||
|
return reply(entries.map(([element, json]) => [element.backendNodeId, JSON.parse(json)]));
|
||||||
|
});
|
||||||
|
const captured = await captureViewModel(cdp, 7);
|
||||||
|
expect(captured.nodes.find((n) => n.backendNodeId === 250)?.formValue).toBe("live 250");
|
||||||
|
expect(captured.nodes.find((n) => n.backendNodeId === 251)?.formValue).toBe("old");
|
||||||
|
expect(send.mock.calls.filter(([, method]) => method === "Runtime.evaluate")).toHaveLength(1);
|
||||||
|
expect(
|
||||||
|
send.mock.calls.filter(([, method]) => method === "Runtime.releaseObjectGroup"),
|
||||||
|
).toHaveLength(1);
|
||||||
|
expect(send).not.toHaveBeenCalledWith(7, "DOM.resolveNode", expect.anything());
|
||||||
|
expect(send).not.toHaveBeenCalledWith(7, "Runtime.callFunctionOn", expect.anything());
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -190,9 +190,9 @@ interface CapturedFormState {
|
|||||||
sensitive?: boolean;
|
sensitive?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CapturedFrameFormState {
|
interface DeepSerializedValue {
|
||||||
controls: CapturedFormState[];
|
type: string;
|
||||||
childFrames: CapturedFrameFormState[];
|
value?: unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
function formStateBatchExpression(maxControls: number): string {
|
function formStateBatchExpression(maxControls: number): string {
|
||||||
@@ -215,43 +215,63 @@ function formStateBatchExpression(maxControls: number): string {
|
|||||||
...(sensitive ? {} : { value: rawValue, defaultValue }),
|
...(sensitive ? {} : { value: rawValue, defaultValue }),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
const controls = [];
|
||||||
const collect = (doc) => {
|
const collect = (doc) => {
|
||||||
const controls = [];
|
|
||||||
for (const el of Array.from(doc.querySelectorAll(controlSelector))) {
|
for (const el of Array.from(doc.querySelectorAll(controlSelector))) {
|
||||||
if (remaining <= 0) break;
|
if (remaining <= 0) break;
|
||||||
controls.push(controlState(el));
|
// Deep serialization supplies the node's backend id. Keep the
|
||||||
|
// state as JSON so decoding needs no general-purpose V8 deserializer.
|
||||||
|
controls.push([el, JSON.stringify(controlState(el))]);
|
||||||
remaining -= 1;
|
remaining -= 1;
|
||||||
}
|
}
|
||||||
const childFrames = [];
|
|
||||||
for (const frame of Array.from(doc.querySelectorAll("iframe"))) {
|
for (const frame of Array.from(doc.querySelectorAll("iframe"))) {
|
||||||
|
if (remaining <= 0) break;
|
||||||
let childDoc = null;
|
let childDoc = null;
|
||||||
try { childDoc = frame.contentDocument; } catch { childDoc = null; }
|
try { childDoc = frame.contentDocument; } catch { childDoc = null; }
|
||||||
childFrames.push(childDoc && remaining > 0 ? collect(childDoc) : { controls: [], childFrames: [] });
|
if (childDoc) collect(childDoc);
|
||||||
}
|
}
|
||||||
return { controls, childFrames };
|
|
||||||
};
|
};
|
||||||
return collect(document);
|
collect(document);
|
||||||
|
return controls;
|
||||||
})()`;
|
})()`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function flattenFrameFormStates(
|
function formStatesByBackendId(result: RuntimeEvaluateReply): Map<number, CapturedFormState> {
|
||||||
frame: CapturedFrameFormState | undefined,
|
const states = new Map<number, CapturedFormState>();
|
||||||
out: CapturedFormState[][] = [],
|
const serialized = result.result?.deepSerializedValue;
|
||||||
): CapturedFormState[][] {
|
if (serialized?.type !== "array" || !Array.isArray(serialized.value)) return states;
|
||||||
if (!frame) return out;
|
for (const entry of serialized.value as DeepSerializedValue[]) {
|
||||||
out.push(Array.isArray(frame.controls) ? frame.controls : []);
|
if (entry?.type !== "array" || !Array.isArray(entry.value)) continue;
|
||||||
for (const child of Array.isArray(frame.childFrames) ? frame.childFrames : []) {
|
const [element, json] = entry.value as DeepSerializedValue[];
|
||||||
flattenFrameFormStates(child, out);
|
if (element?.type !== "node" || json?.type !== "string" || typeof json.value !== "string") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const backendNodeId = (element.value as { backendNodeId?: number } | undefined)?.backendNodeId;
|
||||||
|
if (typeof backendNodeId !== "number" || !Number.isSafeInteger(backendNodeId)) continue;
|
||||||
|
try {
|
||||||
|
const state = JSON.parse(json.value) as CapturedFormState | null;
|
||||||
|
if (
|
||||||
|
!state ||
|
||||||
|
!["empty", "filled", "default"].includes(state.state ?? "") ||
|
||||||
|
typeof state.sensitive !== "boolean" ||
|
||||||
|
typeof state.placeholder !== "string" ||
|
||||||
|
(!state.sensitive &&
|
||||||
|
(typeof state.value !== "string" || typeof state.defaultValue !== "string"))
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
states.set(backendNodeId, state);
|
||||||
|
} catch {
|
||||||
|
// A malformed entry must not overwrite the snapshot's own state.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return out;
|
return states;
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyFormStates(nodes: CapturedNode[], states: CapturedFormState[]): void {
|
function applyFormStates(nodes: CapturedNode[], states: Map<number, CapturedFormState>): void {
|
||||||
let index = 0;
|
|
||||||
for (const node of nodes) {
|
for (const node of nodes) {
|
||||||
if (!isFormControlTag(node.tag)) continue;
|
if (!isFormControlTag(node.tag)) continue;
|
||||||
const state = states[index];
|
const state = states.get(node.backendNodeId);
|
||||||
index += 1;
|
|
||||||
if (!state) continue;
|
if (!state) continue;
|
||||||
node.formState = state.state;
|
node.formState = state.state;
|
||||||
node.formPlaceholder = state.placeholder ?? "";
|
node.formPlaceholder = state.placeholder ?? "";
|
||||||
@@ -276,26 +296,37 @@ async function enrichFormControlStates(
|
|||||||
nodes.some((node) => isFormControlTag(node.tag)),
|
nodes.some((node) => isFormControlTag(node.tag)),
|
||||||
);
|
);
|
||||||
if (!hasControls) return;
|
if (!hasControls) return;
|
||||||
|
const objectGroup = `bsk-vom-forms-${crypto.randomUUID()}`;
|
||||||
try {
|
try {
|
||||||
throwIfAborted(signal);
|
throwIfAborted(signal);
|
||||||
const result = await cdp.send<RuntimeEvaluateReply>(tabId, "Runtime.evaluate", {
|
const result = await cdp.send<RuntimeEvaluateReply>(tabId, "Runtime.evaluate", {
|
||||||
expression: formStateBatchExpression(MAX_FORM_ENRICH_CONTROLS),
|
expression: formStateBatchExpression(MAX_FORM_ENRICH_CONTROLS),
|
||||||
returnByValue: true,
|
objectGroup,
|
||||||
|
serializationOptions: {
|
||||||
|
serialization: "deep",
|
||||||
|
maxDepth: 3,
|
||||||
|
additionalParameters: { maxNodeDepth: 0, includeShadowTree: "none" },
|
||||||
|
},
|
||||||
});
|
});
|
||||||
throwIfAborted(signal);
|
throwIfAborted(signal);
|
||||||
const frameStates = flattenFrameFormStates(runtimeValue<CapturedFrameFormState>(result));
|
// Backend ids are scoped to this capture's CDP target. OOPIF targets
|
||||||
for (let i = 0; i < frameNodeGroups.length; i += 1) {
|
// use their own captureViewModel call and never share this lookup.
|
||||||
applyFormStates(frameNodeGroups[i], frameStates[i] ?? []);
|
const states = formStatesByBackendId(result);
|
||||||
|
for (const nodes of frameNodeGroups) {
|
||||||
|
applyFormStates(nodes, states);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (isAbortError(error)) throw error;
|
if (isAbortError(error)) throw error;
|
||||||
// Best-effort enrichment. DOMSnapshot/AX data still carries the nodes.
|
// Best-effort enrichment. DOMSnapshot/AX data still carries the nodes.
|
||||||
|
} finally {
|
||||||
|
await cdp.send(tabId, "Runtime.releaseObjectGroup", { objectGroup }).catch(() => undefined);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface RuntimeEvaluateReply {
|
interface RuntimeEvaluateReply {
|
||||||
result?: {
|
result?: {
|
||||||
value?: unknown;
|
value?: unknown;
|
||||||
|
deepSerializedValue?: DeepSerializedValue;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,11 @@ export type RpcErrorReason =
|
|||||||
| "ref_not_found"
|
| "ref_not_found"
|
||||||
| "selector_not_found"
|
| "selector_not_found"
|
||||||
| "target_not_fillable"
|
| "target_not_fillable"
|
||||||
|
| "fill_value_invalid"
|
||||||
|
| "fill_target_changed"
|
||||||
|
| "fill_focus_lost"
|
||||||
|
| "fill_value_mismatch"
|
||||||
|
| "fill_failed"
|
||||||
| "target_not_select"
|
| "target_not_select"
|
||||||
| "option_not_found"
|
| "option_not_found"
|
||||||
| "single_select_value_count"
|
| "single_select_value_count"
|
||||||
|
|||||||
@@ -172,6 +172,10 @@ intended. Read `bsk upload --help` and `bsk download --help` for all flags and e
|
|||||||
- Stale ref: observe again and retry the intended action once.
|
- Stale ref: observe again and retry the intended action once.
|
||||||
- Unknown tab or session: list current tabs/sessions; never guess identifiers.
|
- Unknown tab or session: list current tabs/sessions; never guess identifiers.
|
||||||
- Timeout: inspect current page state before deciding whether one longer purposeful wait is useful.
|
- Timeout: inspect current page state before deciding whether one longer purposeful wait is useful.
|
||||||
|
- Fill result unconfirmed (`fill_value_mismatch`): observe the field first; the page may have
|
||||||
|
formatted the value. Continue if the visible result satisfies the user's intent. Otherwise correct
|
||||||
|
the remaining difference; do not blindly repeat fill or immediately request human help. For other
|
||||||
|
fill errors, follow the returned hint and inspect current state before retrying.
|
||||||
- Unsupported command: continue with available capabilities; suggest updating only when the missing
|
- Unsupported command: continue with available capabilities; suggest updating only when the missing
|
||||||
command is necessary.
|
command is necessary.
|
||||||
- Unrecoverable failure: report the blocker and stop the session in a finally-style path.
|
- Unrecoverable failure: report the blocker and stop the session in a finally-style path.
|
||||||
|
|||||||
@@ -43,6 +43,11 @@ pub mod reason {
|
|||||||
pub const REF_NOT_FOUND: &str = "ref_not_found";
|
pub const REF_NOT_FOUND: &str = "ref_not_found";
|
||||||
pub const SELECTOR_NOT_FOUND: &str = "selector_not_found";
|
pub const SELECTOR_NOT_FOUND: &str = "selector_not_found";
|
||||||
pub const TARGET_NOT_FILLABLE: &str = "target_not_fillable";
|
pub const TARGET_NOT_FILLABLE: &str = "target_not_fillable";
|
||||||
|
pub const FILL_VALUE_INVALID: &str = "fill_value_invalid";
|
||||||
|
pub const FILL_TARGET_CHANGED: &str = "fill_target_changed";
|
||||||
|
pub const FILL_FOCUS_LOST: &str = "fill_focus_lost";
|
||||||
|
pub const FILL_VALUE_MISMATCH: &str = "fill_value_mismatch";
|
||||||
|
pub const FILL_FAILED: &str = "fill_failed";
|
||||||
pub const TARGET_NOT_SELECT: &str = "target_not_select";
|
pub const TARGET_NOT_SELECT: &str = "target_not_select";
|
||||||
pub const OPTION_NOT_FOUND: &str = "option_not_found";
|
pub const OPTION_NOT_FOUND: &str = "option_not_found";
|
||||||
pub const SINGLE_SELECT_VALUE_COUNT: &str = "single_select_value_count";
|
pub const SINGLE_SELECT_VALUE_COUNT: &str = "single_select_value_count";
|
||||||
@@ -247,7 +252,42 @@ pub fn info_for_error(code: ErrorCode, data: Option<&serde_json::Value>) -> Rend
|
|||||||
(ErrorCode::InvalidParams, reason::TARGET_NOT_FILLABLE) => RenderInfo {
|
(ErrorCode::InvalidParams, reason::TARGET_NOT_FILLABLE) => RenderInfo {
|
||||||
summary: "target element is not fillable",
|
summary: "target element is not fillable",
|
||||||
hint: Some(
|
hint: Some(
|
||||||
"choose an input, textarea, or contenteditable element from the latest snapshot",
|
"observe the page and choose an enabled, editable text input, textarea, or contenteditable; use the appropriate interaction for other control types",
|
||||||
|
),
|
||||||
|
exit_code: base.exit_code,
|
||||||
|
},
|
||||||
|
(ErrorCode::InvalidParams, reason::FILL_VALUE_INVALID) => RenderInfo {
|
||||||
|
summary: "the requested text does not fit the field constraints",
|
||||||
|
hint: Some(
|
||||||
|
"check the field's input type and maxlength; use a compatible value without silently truncating or changing the user's intended data",
|
||||||
|
),
|
||||||
|
exit_code: base.exit_code,
|
||||||
|
},
|
||||||
|
(ErrorCode::CdpFailed, reason::FILL_TARGET_CHANGED) => RenderInfo {
|
||||||
|
summary: "the fill target changed during the action",
|
||||||
|
hint: Some(
|
||||||
|
"observe the current page and field value; if the intended result is missing, use a fresh editable target before retrying",
|
||||||
|
),
|
||||||
|
exit_code: base.exit_code,
|
||||||
|
},
|
||||||
|
(ErrorCode::CdpFailed, reason::FILL_FOCUS_LOST) => RenderInfo {
|
||||||
|
summary: "the page moved focus away from the fill target",
|
||||||
|
hint: Some(
|
||||||
|
"observe the page for a dialog or rerender and resolve it before retrying; fill manages field focus and does not require bringing the browser to the foreground",
|
||||||
|
),
|
||||||
|
exit_code: base.exit_code,
|
||||||
|
},
|
||||||
|
(ErrorCode::CdpFailed, reason::FILL_VALUE_MISMATCH) => RenderInfo {
|
||||||
|
summary: "the fill result could not be confirmed",
|
||||||
|
hint: Some(
|
||||||
|
"observe the field before retrying; the page may have formatted the value. Continue if the visible result satisfies the user's intent; otherwise correct the remaining difference. Do not blindly repeat fill or immediately request human help",
|
||||||
|
),
|
||||||
|
exit_code: base.exit_code,
|
||||||
|
},
|
||||||
|
(ErrorCode::CdpFailed, reason::FILL_FAILED) => RenderInfo {
|
||||||
|
summary: "fill encountered a browser or page-script error",
|
||||||
|
hint: Some(
|
||||||
|
"observe the page before retrying because the field may already have changed; retry only if the intended result is missing and the target is still available",
|
||||||
),
|
),
|
||||||
exit_code: base.exit_code,
|
exit_code: base.exit_code,
|
||||||
},
|
},
|
||||||
@@ -442,6 +482,56 @@ mod tests {
|
|||||||
ErrorCode::NoBrowserConnected,
|
ErrorCode::NoBrowserConnected,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn fill_reasons_provide_recovery_without_changing_exit_codes() {
|
||||||
|
for (code, reason, hint_fragment) in [
|
||||||
|
(
|
||||||
|
ErrorCode::InvalidParams,
|
||||||
|
reason::TARGET_NOT_FILLABLE,
|
||||||
|
"enabled, editable",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
ErrorCode::InvalidParams,
|
||||||
|
reason::FILL_VALUE_INVALID,
|
||||||
|
"maxlength",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
ErrorCode::CdpFailed,
|
||||||
|
reason::FILL_TARGET_CHANGED,
|
||||||
|
"fresh editable target",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
ErrorCode::CdpFailed,
|
||||||
|
reason::FILL_FOCUS_LOST,
|
||||||
|
"does not require bringing the browser",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
ErrorCode::CdpFailed,
|
||||||
|
reason::FILL_VALUE_MISMATCH,
|
||||||
|
"Continue if the visible result satisfies",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
ErrorCode::CdpFailed,
|
||||||
|
reason::FILL_FAILED,
|
||||||
|
"may already have changed",
|
||||||
|
),
|
||||||
|
] {
|
||||||
|
let data = serde_json::json!({ "reason": reason });
|
||||||
|
let info = info_for_error(code, Some(&data));
|
||||||
|
assert!(info.hint.unwrap().contains(hint_fragment), "{reason}");
|
||||||
|
assert_eq!(info.exit_code, info_for(code).exit_code);
|
||||||
|
}
|
||||||
|
let mismatch = serde_json::json!({ "reason": reason::FILL_VALUE_MISMATCH });
|
||||||
|
let info = info_for_error(ErrorCode::CdpFailed, Some(&mismatch));
|
||||||
|
assert_eq!(info.summary, "the fill result could not be confirmed");
|
||||||
|
assert!(info.hint.unwrap().contains("Do not blindly repeat fill"));
|
||||||
|
let unknown = serde_json::json!({ "reason": "future_fill_reason" });
|
||||||
|
assert_eq!(
|
||||||
|
info_for_error(ErrorCode::CdpFailed, Some(&unknown)),
|
||||||
|
info_for(ErrorCode::CdpFailed)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn element_not_visible_overrides_permission_denied_copy() {
|
fn element_not_visible_overrides_permission_denied_copy() {
|
||||||
let data = serde_json::json!({ "reason": reason::ELEMENT_NOT_VISIBLE });
|
let data = serde_json::json!({ "reason": reason::ELEMENT_NOT_VISIBLE });
|
||||||
|
|||||||
@@ -97,6 +97,10 @@ Continue from returned sequence cursors instead of rereading the same buffer.
|
|||||||
- Unknown tab: list tabs instead of guessing.
|
- Unknown tab: list tabs instead of guessing.
|
||||||
- Unknown session: list owned sessions or start one; never try foreign ids.
|
- Unknown session: list owned sessions or start one; never try foreign ids.
|
||||||
- Timeout: inspect current state before deciding whether one longer purposeful wait is useful.
|
- Timeout: inspect current state before deciding whether one longer purposeful wait is useful.
|
||||||
|
- Fill result unconfirmed: observe the field first; the page may have formatted the value. Continue
|
||||||
|
if the visible result satisfies the user's intent. Otherwise correct the remaining difference;
|
||||||
|
do not blindly repeat fill or immediately request human help. For other fill errors, follow the
|
||||||
|
returned hint and inspect current state before retrying.
|
||||||
- Unrecoverable failure: report the blocker and stop the owned session.
|
- Unrecoverable failure: report the blocker and stop the owned session.
|
||||||
|
|
||||||
Arbitrary page-script evaluation and interaction recording are intentionally unsupported. Do not
|
Arbitrary page-script evaluation and interaction recording are intentionally unsupported. Do not
|
||||||
|
|||||||
@@ -636,7 +636,9 @@ function defineBrowserOperations(deps: ToolDeps, register: DefinitionRegistrar):
|
|||||||
session: SESSION_PARAM,
|
session: SESSION_PARAM,
|
||||||
noClear: {
|
noClear: {
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
description: "Skip the default wipe-the-field-first pass (append instead).",
|
description:
|
||||||
|
"Append to the end of the existing value instead of clearing it. " +
|
||||||
|
"The tool moves the caret automatically; no prior click or selection is needed.",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
output: {
|
output: {
|
||||||
|
|||||||
@@ -124,6 +124,28 @@ describe("parseBskJson", () => {
|
|||||||
expect(() => parseBskJson({ ...base, code: 1, stderr: "boom" }, "x")).toThrow(/boom/);
|
expect(() => parseBskJson({ ...base, code: 1, stderr: "boom" }, "x")).toThrow(/boom/);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("surfaces fill recovery guidance to the model without retrying", async () => {
|
||||||
|
const hint =
|
||||||
|
"observe the field before retrying; the page may have formatted the value. Continue if the visible result satisfies the user's intent";
|
||||||
|
const reply = {
|
||||||
|
...base,
|
||||||
|
code: 3,
|
||||||
|
stdout: JSON.stringify({
|
||||||
|
code: "cdp_failed",
|
||||||
|
message: "fill could not verify the expected value",
|
||||||
|
data: { reason: "fill_value_mismatch" },
|
||||||
|
hint,
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
let calls = 0;
|
||||||
|
const result = await runWithSessionBusyRetry(async () => {
|
||||||
|
calls++;
|
||||||
|
return reply;
|
||||||
|
});
|
||||||
|
expect(calls).toBe(1);
|
||||||
|
expect(() => parseBskJson(result, "fill")).toThrow(hint);
|
||||||
|
});
|
||||||
|
|
||||||
it("reports killed-by-interrupt children (null exit code) as interrupted", () => {
|
it("reports killed-by-interrupt children (null exit code) as interrupted", () => {
|
||||||
expect(() => parseBskJson({ ...base, code: null }, "navigate")).toThrow(/interrupted/);
|
expect(() => parseBskJson({ ...base, code: null }, "navigate")).toThrow(/interrupted/);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -172,6 +172,10 @@ intended. Read `bsk upload --help` and `bsk download --help` for all flags and e
|
|||||||
- Stale ref: observe again and retry the intended action once.
|
- Stale ref: observe again and retry the intended action once.
|
||||||
- Unknown tab or session: list current tabs/sessions; never guess identifiers.
|
- Unknown tab or session: list current tabs/sessions; never guess identifiers.
|
||||||
- Timeout: inspect current page state before deciding whether one longer purposeful wait is useful.
|
- Timeout: inspect current page state before deciding whether one longer purposeful wait is useful.
|
||||||
|
- Fill result unconfirmed (`fill_value_mismatch`): observe the field first; the page may have
|
||||||
|
formatted the value. Continue if the visible result satisfies the user's intent. Otherwise correct
|
||||||
|
the remaining difference; do not blindly repeat fill or immediately request human help. For other
|
||||||
|
fill errors, follow the returned hint and inspect current state before retrying.
|
||||||
- Unsupported command: continue with available capabilities; suggest updating only when the missing
|
- Unsupported command: continue with available capabilities; suggest updating only when the missing
|
||||||
command is necessary.
|
command is necessary.
|
||||||
- Unrecoverable failure: report the blocker and stop the session in a finally-style path.
|
- Unrecoverable failure: report the blocker and stop the session in a finally-style path.
|
||||||
|
|||||||
Reference in New Issue
Block a user