From 9da97d1a41baae10e477622f7e38db34d993e374 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 12 Jan 2026 22:08:43 +0000 Subject: [PATCH] test(browser): expand scrollintoview coverage --- src/browser/pw-tools-core.test.ts | 68 +++++++++++++++++++++++++++++++ src/browser/server.test.ts | 14 +++++++ 2 files changed, 82 insertions(+) diff --git a/src/browser/pw-tools-core.test.ts b/src/browser/pw-tools-core.test.ts index b60bee2e9..008a8054e 100644 --- a/src/browser/pw-tools-core.test.ts +++ b/src/browser/pw-tools-core.test.ts @@ -412,6 +412,74 @@ describe("pw-tools-core", () => { expect(scrollIntoViewIfNeeded).toHaveBeenCalledWith({ timeout: 20_000 }); }); + it("requires a ref for scrollIntoView", async () => { + currentRefLocator = { scrollIntoViewIfNeeded: vi.fn(async () => {}) }; + currentPage = {}; + + const mod = await importModule(); + await expect( + mod.scrollIntoViewViaPlaywright({ + cdpUrl: "http://127.0.0.1:18792", + targetId: "T1", + ref: " ", + }), + ).rejects.toThrow(/ref is required/i); + }); + + it("clamps timeoutMs for scrollIntoView", async () => { + const scrollIntoViewIfNeeded = vi.fn(async () => {}); + currentRefLocator = { scrollIntoViewIfNeeded }; + currentPage = {}; + + const mod = await importModule(); + await mod.scrollIntoViewViaPlaywright({ + cdpUrl: "http://127.0.0.1:18792", + targetId: "T1", + ref: "1", + timeoutMs: 50, + }); + + expect(scrollIntoViewIfNeeded).toHaveBeenCalledWith({ timeout: 500 }); + }); + + it("rewrites strict mode violations for scrollIntoView", async () => { + const scrollIntoViewIfNeeded = vi.fn(async () => { + throw new Error( + 'Error: strict mode violation: locator("aria-ref=1") resolved to 2 elements', + ); + }); + currentRefLocator = { scrollIntoViewIfNeeded }; + currentPage = {}; + + const mod = await importModule(); + await expect( + mod.scrollIntoViewViaPlaywright({ + cdpUrl: "http://127.0.0.1:18792", + targetId: "T1", + ref: "1", + }), + ).rejects.toThrow(/Run a new snapshot/i); + }); + + it("rewrites not-visible timeouts for scrollIntoView", async () => { + const scrollIntoViewIfNeeded = vi.fn(async () => { + throw new Error( + 'Timeout 5000ms exceeded. waiting for locator("aria-ref=1") to be visible', + ); + }); + currentRefLocator = { scrollIntoViewIfNeeded }; + currentPage = {}; + + const mod = await importModule(); + await expect( + mod.scrollIntoViewViaPlaywright({ + cdpUrl: "http://127.0.0.1:18792", + targetId: "T1", + ref: "1", + }), + ).rejects.toThrow(/not found or not visible/i); + }); + it("rewrites strict mode violations into snapshot hints", async () => { const click = vi.fn(async () => { throw new Error( diff --git a/src/browser/server.test.ts b/src/browser/server.test.ts index 9dd482d5a..47e644b1e 100644 --- a/src/browser/server.test.ts +++ b/src/browser/server.test.ts @@ -712,6 +712,20 @@ describe("browser control server", () => { }); expect(clickMissingRef.status).toBe(400); + const scrollMissingRef = await realFetch(`${base}/act`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ kind: "scrollIntoView" }), + }); + expect(scrollMissingRef.status).toBe(400); + + const scrollSelectorUnsupported = await realFetch(`${base}/act`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ kind: "scrollIntoView", selector: "button.save" }), + }); + expect(scrollSelectorUnsupported.status).toBe(400); + const clickBadButton = await realFetch(`${base}/act`, { method: "POST", headers: { "Content-Type": "application/json" },