|
1 | 1 | // Browser tests cover agent.act.normalize plugin behavior. |
2 | 2 | import { describe, expect, it } from "vitest"; |
3 | 3 | import { MAX_SAFE_TIMEOUT_DELAY_MS } from "../timer-delay.js"; |
4 | | -import { normalizeActRequest } from "./agent.act.normalize.js"; |
| 4 | +import { canonicalizeActTargetIds, normalizeActRequest } from "./agent.act.normalize.js"; |
| 5 | + |
| 6 | +describe("canonicalizeActTargetIds", () => { |
| 7 | + const canonical = "abcd1234"; |
| 8 | + const tab = { targetId: canonical, suggestedTargetId: "sg-1", tabId: "tab-7", label: "Inbox" }; |
| 9 | + |
| 10 | + it("rewrites every same-tab alias to the canonical targetId before dispatch", () => { |
| 11 | + for (const alias of ["abcd", "tab-7", "Inbox", "sg-1", canonical]) { |
| 12 | + const action = { kind: "click", ref: "1", targetId: alias } as const; |
| 13 | + expect(canonicalizeActTargetIds(action, tab)).toBeNull(); |
| 14 | + expect(action.targetId).toBe(canonical); |
| 15 | + } |
| 16 | + }); |
| 17 | + |
| 18 | + it("canonicalizes batch sub-action aliases recursively", () => { |
| 19 | + const action = { |
| 20 | + kind: "batch", |
| 21 | + targetId: "abcd", |
| 22 | + actions: [ |
| 23 | + { kind: "click", ref: "1", targetId: "tab-7" }, |
| 24 | + { kind: "batch", actions: [{ kind: "resize", width: 2, height: 2, targetId: "Inbox" }] }, |
| 25 | + ], |
| 26 | + } satisfies Parameters<typeof canonicalizeActTargetIds>[0]; |
| 27 | + expect(canonicalizeActTargetIds(action, tab, [tab])).toBeNull(); |
| 28 | + expect(action.targetId).toBe(canonical); |
| 29 | + const [first, nested] = action.actions; |
| 30 | + expect(first?.targetId).toBe(canonical); |
| 31 | + if (nested?.kind !== "batch") { |
| 32 | + throw new Error("expected nested batch"); |
| 33 | + } |
| 34 | + expect(nested.actions[0]?.targetId).toBe(canonical); |
| 35 | + }); |
| 36 | + |
| 37 | + it("leaves an absent targetId unset so dispatch falls back to the request tab", () => { |
| 38 | + const action: Parameters<typeof canonicalizeActTargetIds>[0] = { kind: "click", ref: "1" }; |
| 39 | + expect(canonicalizeActTargetIds(action, tab)).toBeNull(); |
| 40 | + expect(action.targetId).toBeUndefined(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("rejects ids that resolve to a different tab", () => { |
| 44 | + expect(canonicalizeActTargetIds({ kind: "click", ref: "1", targetId: "zzzz9999" }, tab)).toBe( |
| 45 | + "action targetId must match request targetId", |
| 46 | + ); |
| 47 | + expect( |
| 48 | + canonicalizeActTargetIds( |
| 49 | + { kind: "batch", actions: [{ kind: "click", ref: "1", targetId: "zzzz9999" }] }, |
| 50 | + tab, |
| 51 | + ), |
| 52 | + ).toBe("batched action targetId must match request targetId"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("rejects a batched targetId prefix that is ambiguous across tabs", () => { |
| 56 | + expect( |
| 57 | + canonicalizeActTargetIds( |
| 58 | + { kind: "batch", actions: [{ kind: "click", ref: "1", targetId: "abcd" }] }, |
| 59 | + tab, |
| 60 | + [tab, { targetId: "abcd9999" }], |
| 61 | + ), |
| 62 | + ).toBe("batched action targetId must match request targetId"); |
| 63 | + }); |
| 64 | +}); |
5 | 65 |
|
6 | 66 | describe("normalizeActRequest numeric fields", () => { |
7 | 67 | it("keeps structured numeric action options", () => { |
|
0 commit comments