diff options
Diffstat (limited to 'test-suite/unit/shared-window.test.mjs')
| -rw-r--r-- | test-suite/unit/shared-window.test.mjs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/test-suite/unit/shared-window.test.mjs b/test-suite/unit/shared-window.test.mjs new file mode 100644 index 0000000..66041f4 --- /dev/null +++ b/test-suite/unit/shared-window.test.mjs @@ -0,0 +1,61 @@ +// Each session has its own window: a window shows only one tab, so sessions sharing one hid each +// other's tabs (no real input, blank screenshots). A session's extra tabs go into its own window. +import assert from "node:assert/strict"; +import fs from "node:fs"; +import vm from "node:vm"; +import { test } from "node:test"; + +const workerSource = fs.readFileSync(new URL("../../extensions/chrome-profile-bridge/browser-extension/service_worker.js", import.meta.url), "utf8"); + +function worker({ groups = [], windows = { 5: { id: 5, type: "normal" } } } = {}) { + const calls = []; + const listener = { addListener() {}, removeListener() {} }; + let nextTab = 100; + const w = { + console, setTimeout, clearTimeout, setInterval: () => 0, navigator: { userAgent: "unit-test" }, fetch: async () => { throw new Error("offline"); }, + chrome: { + runtime: { id: "test", getManifest: () => ({ version: "0.0.0" }), onInstalled: listener, onStartup: listener }, + alarms: { create() {}, onAlarm: listener }, action: { onClicked: listener }, webNavigation: { onCommitted: listener }, + debugger: { onDetach: listener, onEvent: listener, getTargets: (cb) => cb([]) }, + storage: { session: { get: async () => ({}), set: async (v) => { calls.push({ method: 'storage.set', json: JSON.stringify(v) }); } } }, + tabGroups: { query: async () => groups }, + windows: { + get: async (id) => windows[id] ?? Promise.reject(new Error("No window")), + create: async (props) => { calls.push({ method: "windows.create", props }); return { id: 9, tabs: [{ id: 900, windowId: 9 }] }; }, + }, + tabs: { + create: async (props) => { calls.push({ method: "tabs.create", props }); return { id: nextTab++, windowId: props.windowId }; }, + query: async () => [], get: async () => null, onUpdated: listener, + }, + }, + }; + w.self = w; + vm.runInNewContext(workerSource, w); + return { w, calls }; +} + +const piGroup = (windowId, name = "Pi Session: claude-code 1 other") => ({ id: 1, windowId, title: name }); +const ops = (calls) => calls.filter((c) => c.method !== "storage.set").map((c) => c.method); + +test("a new session gets its own window even when another session's window exists", async () => { + const { w, calls } = worker({ groups: [piGroup(5)] }); + await w.createAutomationTarget("claude-code 2 mine", "Pi Session: claude-code 2 mine"); + assert.deepEqual(ops(calls), ["windows.create"]); +}); + +test("a session that already has a group adds its tab to that group's window", async () => { + const { w, calls } = worker({ groups: [piGroup(5, "Pi Session: claude-code 2 mine")] }); + await w.createAutomationTarget("claude-code 2 mine", "Pi Session: claude-code 2 mine"); + assert.deepEqual(ops(calls), ["tabs.create"]); + assert.equal(calls.find((c) => c.method === "tabs.create").props.windowId, 5); +}); + +test("chrome_tab new for a session with no group goes to the session's own window, not the focused one", async () => { + const { w, calls } = worker({ groups: [piGroup(5)] }); + w.groupTab = async (tab) => tab; + w.bringToFront = async () => {}; + await w.dispatch("tab.new", { url: "https://x.test/", groupTitle: "Pi Session: claude-code 2 mine", sessionKey: "claude-code 2 mine" }); + assert.deepEqual(ops(calls), ["windows.create"]); + assert.equal(calls[0].props.url, "https://x.test/"); + assert.equal(calls[0].props.focused, false); +}); |
