From aa66c9b4f2bb909e2e35fbb7f24313e98c2e24b1 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sat, 28 Feb 2026 09:18:55 -0800 Subject: [PATCH] test(browser): cover ambiguous same-url target resolution --- src/browser/routes/agent.snapshot.test.ts | 56 +++++++++++++++++++---- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/src/browser/routes/agent.snapshot.test.ts b/src/browser/routes/agent.snapshot.test.ts index 77b802bdf7db..be879eaa8704 100644 --- a/src/browser/routes/agent.snapshot.test.ts +++ b/src/browser/routes/agent.snapshot.test.ts @@ -29,17 +29,57 @@ describe("resolveTargetIdAfterNavigate", () => { expect(result).toBe("new-456"); }); - it("prefers non-stale targetId when multiple tabs share the URL", async () => { - const result = await resolveTargetIdAfterNavigate({ + it("falls back to original targetId when same-URL matches stay ambiguous", async () => { + vi.useFakeTimers(); + let calls = 0; + + const result$ = resolveTargetIdAfterNavigate({ oldTargetId: "old-123", navigatedUrl: "https://example.com", - listTabs: staticListTabs([ - { targetId: "preexisting-000", url: "https://example.com" }, - { targetId: "fresh-777", url: "https://example.com" }, - ]), + listTabs: async () => { + calls++; + return [ + { targetId: "preexisting-000", url: "https://example.com" }, + { targetId: "fresh-777", url: "https://example.com" }, + ]; + }, }); - // Both differ from old targetId; the first non-stale match wins. - expect(result).toBe("preexisting-000"); + + await vi.advanceTimersByTimeAsync(800); + const result = await result$; + + expect(result).toBe("old-123"); + expect(calls).toBe(2); + + vi.useRealTimers(); + }); + + it("resolves targetId when retry disambiguates same-URL matches", async () => { + vi.useFakeTimers(); + let calls = 0; + + const result$ = resolveTargetIdAfterNavigate({ + oldTargetId: "old-123", + navigatedUrl: "https://example.com", + listTabs: async () => { + calls++; + if (calls === 1) { + return [ + { targetId: "preexisting-000", url: "https://example.com" }, + { targetId: "fresh-777", url: "https://example.com" }, + ]; + } + return [{ targetId: "fresh-777", url: "https://example.com" }]; + }, + }); + + await vi.advanceTimersByTimeAsync(800); + const result = await result$; + + expect(result).toBe("fresh-777"); + expect(calls).toBe(2); + + vi.useRealTimers(); }); it("retries and resolves targetId when first listTabs has no URL match", async () => {