1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
// Agent sessions share one window: a new session's tab goes into the window that already holds a
// "Pi Session:" group, and a window is created only when there is none.
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 });
test("a new session's tab goes into the window that already holds another Pi session", async () => {
const { w, calls } = worker({ groups: [piGroup(5)] });
await w.createAutomationTarget("claude-code 2 mine", "Pi Session: claude-code 2 mine");
assert.deepEqual(calls.filter((c) => c.method !== "storage.set").map((c) => c.method), ["tabs.create"]);
assert.equal(calls[0].props.windowId, 5);
});
test("with no Pi window yet, the first session creates one", async () => {
const { w, calls } = worker({ groups: [] });
await w.createAutomationTarget("claude-code 2 mine", "Pi Session: claude-code 2 mine");
assert.deepEqual(calls.filter((c) => c.method !== "storage.set").map((c) => c.method), ["windows.create"]);
});
test("groups that are not Pi sessions do not attract agent tabs", async () => {
const { w, calls } = worker({ groups: [{ id: 2, windowId: 5, title: "Work" }] });
await w.createAutomationTarget("claude-code 2 mine", "Pi Session: claude-code 2 mine");
assert.deepEqual(calls.filter((c) => c.method !== "storage.set").map((c) => c.method), ["windows.create"]);
});
test("a Pi group whose window is gone, or is not a normal window, is skipped", async () => {
const { w, calls } = worker({ groups: [piGroup(7), piGroup(8)], windows: { 8: { id: 8, type: "popup" } } });
await w.createAutomationTarget("claude-code 2 mine", "Pi Session: claude-code 2 mine");
assert.deepEqual(calls.filter((c) => c.method !== "storage.set").map((c) => c.method), ["windows.create"]);
});
test("the shared window is never recorded as ours, so cleanup cannot close it", async () => {
const { w, calls } = worker({ groups: [piGroup(5)] });
await w.createAutomationTarget("claude-code 2 mine", "Pi Session: claude-code 2 mine");
const stored = calls.filter((c) => c.method === "storage.set").map((c) => c.json).join("");
assert.doesNotMatch(stored, /"windowId":5/);
});
test("chrome_tab new for a session with no group uses the shared 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: "about:blank", groupTitle: "Pi Session: claude-code 2 mine", sessionKey: "claude-code 2 mine" });
const created = calls.find((c) => c.method === "tabs.create");
assert.equal(created.props.windowId, 5);
});
|