diff options
Diffstat (limited to 'test-suite/unit/background-policy.test.mjs')
| -rw-r--r-- | test-suite/unit/background-policy.test.mjs | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/test-suite/unit/background-policy.test.mjs b/test-suite/unit/background-policy.test.mjs index 2388d78..98eaa7c 100644 --- a/test-suite/unit/background-policy.test.mjs +++ b/test-suite/unit/background-policy.test.mjs @@ -71,7 +71,7 @@ function piHarness({ session = "alpha", send } = {}) { } const flagCases = [ - [{}, false], [{ background: false }, false], [{ background: true }, true], + [{}, true], [{ background: false }, false], [{ background: true }, true], [{ foreground: true }, false], [{ foreground: false }, true], [{ background: false, foreground: false }, false], [{ background: true, foreground: true }, true], ]; @@ -96,6 +96,7 @@ test("session background on overrides all per-call/legacy foreground flags; off test("every registered page tool, tab.new, and chrome_launch(url) use the central policy", async () => { const h = piHarness(); + await h.background("on"); const cases = [ ["chrome_launch", { url: "https://example.test" }, "tab.new"], ["chrome_tab", { action: "new", group: false, groupTitle: "other" }, "tab.new"], @@ -143,7 +144,11 @@ test("typing options and verification pass through real tool registrations witho test("activation is rejected before dispatch; existing on/off/toggle/status commands suffice", async () => { const h = piHarness(); - await assert.rejects(h.tool("chrome_tab", { action: "activate", targetId: "2", background: false }), /background mode.*\/chrome background off/); + await h.tool("chrome_tab", { action: "activate", targetId: "2" }); + assert.equal(h.calls.at(-1).params.foreground, true, "activation is allowed by default"); + h.calls.length = 0; + await h.background("on"); + await assert.rejects(h.tool("chrome_tab", { action: "activate", targetId: "2", background: false }), /background mode on/); assert.equal(h.calls.length, 0); await h.background("off"); await h.tool("chrome_tab", { action: "activate", targetId: "2" }); @@ -199,9 +204,9 @@ test("shared-owner sessions retain independent policies; per-call background sti const sharedCalls = []; const send = async (action, params) => { sharedCalls.push({ action, params }); return {}; }; const a = piHarness({ session: "A", send }), b = piHarness({ session: "B", send }); - await b.background("off"); + await a.background("on"); await a.send("tab.new", { background: false }); - await b.send("tab.new", {}); + await b.send("tab.new", { background: false }); assert.equal(sharedCalls[0].params.sessionKey, "session:A"); assert.equal(sharedCalls[0].params.foreground, false); assert.equal(sharedCalls[0].action, "tab.new.background"); @@ -297,6 +302,14 @@ function workerHarness({ withWindows = true } = {}) { }; return h; } +// A new Pi tab must not take over the user's window: it is either inactive, or the only tab of a +// new, unfocused window of the session's own (so it is visible to the agent without stealing focus). +function assertNewTabStaysOutOfTheWay(h, tab) { + if (tab.active === false) return; + assert.notEqual(tab.windowId, 1, "an active new tab must not be in the user's window"); + assert.ok(h.calls.some((c) => c.action === "windows.create" && c.params.focused === false), "its window was created unfocused"); +} + function assertNoFocus(h) { assert.equal(h.focusedWindow(), 1); assert.equal(h.tabs.get(1).active, true); @@ -306,6 +319,7 @@ function assertNoFocus(h) { test("Pi-to-worker background aliases preserve grouping, screenshot output, and ownership-aware cleanup", async () => { const h = workerHarness(); const pi = piHarness({ send: (action, params) => h.w.dispatch(action, params) }); + await pi.background("on"); const opened = await pi.tool("chrome_tab", { action: "new", group: false, groupTitle: "wrong", background: false }); assert.equal(opened.details.result.group.title, "Pi Session: alpha"); const launched = await pi.tool("chrome_launch", { url: "https://fixture.test" }); @@ -325,7 +339,7 @@ test("worker advertises support, keeps new tabs inactive, and rejects activation assert.equal((await h.w.dispatch("tab.version", {})).capabilities.hardBackground, true); for (const flags of [{}, { foreground: false }, { background: true, foreground: true }]) { const result = await h.w.dispatch("tab.new", { ...flags, sessionKey: "alpha", groupTitle: "Pi Session: alpha" }); - assert.equal(result.tab.active, false); + assertNewTabStaysOutOfTheWay(h, result.tab); await assert.rejects(h.w.dispatch("tab.activate", { ...flags, targetId: "missing" }), /background mode/); } assertNoFocus(h); @@ -339,7 +353,7 @@ test("worker advertises support, keeps new tabs inactive, and rejects activation test("background-only worker aliases force safe behavior even with foreground flags", async () => { const h = workerHarness(); const opened = await h.w.dispatch("tab.new.background", { foreground: true, background: false }); - assert.equal(opened.tab.active, false); + assertNewTabStaysOutOfTheWay(h, opened.tab); const shot = await h.w.dispatch("page.screenshot.background", { targetId: "2", foreground: true, background: false }); assert.equal(shot.method, "cdp"); assertNoFocus(h); @@ -477,7 +491,7 @@ test("full-page tiles remain CDP-only, pin the resolved target, and restore both scrolls.push(clone(args)); }; let captures = 0; - h.cdpResult = () => { if (++captures === 2 && fail) throw new Error("tile failed"); return { data: "dGlsZQ==" }; }; + h.cdpResult = (method) => { if (method === "Page.captureScreenshot" && ++captures === 2 && fail) throw new Error("tile failed"); return { data: "dGlsZQ==" }; }; const shot = h.w.dispatch("page.screenshot", { urlIncludes: "target.test", fullPage: true, background: true, foreground: true }); if (fail) await assert.rejects(shot, /tile failed/); else { |
