aboutsummaryrefslogtreecommitdiff
path: root/test/manual/bridge-test.ts
diff options
context:
space:
mode:
authorAman Kumar <145339995+amankumarsingh77@users.noreply.github.com>2026-06-21 00:46:44 +0530
committerGitHub <noreply@github.com>2026-06-21 00:46:44 +0530
commitda5d31af999b728c31ab0169373531cab4543f0c (patch)
tree5d350ad1428ff332372a9c724ac46c964d8ca477 /test/manual/bridge-test.ts
parent8db684d43366d335760e106eae39a74376bd7640 (diff)
release: v0.6.0 — per-tab isolation, Unix socket daemon, on-demand init (#6)
* feat: Unix socket daemon with auto-reconnect for persistent Chrome CDP Eliminates the 'Allow Remote Debugging' Chrome prompt that appeared on every pi session by multiplexing pi instances through a single persistent daemon process. Key additions: - Unix socket IPC (net.createServer + readline JSON framing) - CdpBridge with ID multiplexer + event router - DaemonTransport implementing CdpTransport interface - Auto-spawn with ensureDaemon() (lazy, checks socket) Fixes since initial implementation: - Bridge auto-reconnects to Chrome with exponential backoff (1s-60s). Previously the bridge tried once at startup and permanently failed if Chrome wasn't ready — causing 'Chrome not connected' on every browser tool call. - Daemon spawn uses project-local tsx binary instead of global --import flag, fixing silent spawn failure (ERR_MODULE_NOT_FOUND). - Stale ctx crash: persistState in onOwnershipChange wrapped in try/catch to prevent CDP event consumer crashes. 49 test assertions across 4 test files. Zero TypeScript errors. * feat: on-demand browser initialization Browser daemon no longer auto-starts on session_start. Instead: - session_start creates the client lazily but does NOT connect or spawn the daemon. Zero Chrome prompts for non-browser pi sessions. - Browser tool wrapper checks for daemon socket before each call. Socket missing → clear error: 'Run /browser-setup first'. Socket exists → proceeds silently (daemon already running). - /browser-setup is the explicit, one-time init point. It spawns the daemon and connects to Chrome only when the user opts in. - SKILL.md updated with Connection Setup instructions telling agents to direct users to /browser-setup when socket is missing. * fix: checkChromeRunning on macOS — match by substring not exact ps -A -o comm= returns full executable paths on modern macOS (e.g. /Applications/Google Chrome.app/Contents/MacOS/Google Chrome), not the basename. The old exact-match check (browserProcesses.includes(l)) never matched, causing false 'No browser instance running' errors. Changed to substring includes() with helper/renderer/crashpad filtering. * feat: browser_setup as agent-callable tool The agent can now self-recover from 'Browser harness not initialized' errors by calling browser_setup directly — no user mediation needed. - Extracted shared performSetup() from the slash command into setup.ts - Created src/domains/setup.ts with ensureAlive:false to bypass the socket guard (this tool IS the setup, can't require itself) - Registered browser_setup in the tool registry - Updated SKILL.md: agent calls browser_setup directly, not ask user - Removed stale test/manual/spawn-test.ts * release: v0.6.0 — per-tab isolation, Unix socket daemon, on-demand init
Diffstat (limited to 'test/manual/bridge-test.ts')
-rw-r--r--test/manual/bridge-test.ts182
1 files changed, 182 insertions, 0 deletions
diff --git a/test/manual/bridge-test.ts b/test/manual/bridge-test.ts
new file mode 100644
index 0000000..ecb4100
--- /dev/null
+++ b/test/manual/bridge-test.ts
@@ -0,0 +1,182 @@
+/**
+ * Manual integration test for CdpBridge against real Chrome.
+ * Requires: Chrome with remote debugging enabled (chrome://inspect/#remote-debugging).
+ * Run: npx tsx test/manual/bridge-test.ts
+ */
+import { createCdpBridge, type SendToClient, type CdpBridge } from "../../src/daemon/bridge";
+import type { WireRequest, WireResponse, WireEvent } from "../../src/daemon/protocol";
+
+let passed = 0;
+let failed = 0;
+const check = (cond: boolean, label: string) => {
+ if (cond) { passed++; console.log(` ✓ ${label}`); }
+ else { failed++; console.error(` ✗ ${label}`); }
+};
+
+async function main() {
+ console.log("Starting CdpBridge...");
+ const bridge = createCdpBridge();
+
+ // ── Test 1: Connect to Chrome ──────────────────────────────────────────
+ // start() now runs auto-retry in the background. Wait a moment for
+ // the initial connection attempt to complete.
+ await bridge.start();
+ await sleep(3000); // allow time for WebSocket handshake
+ if (!bridge.isAlive()) {
+ console.error("Cannot continue — Chrome not reachable. Run /browser-setup first.");
+ console.error("(The bridge will keep retrying in the background.)");
+ process.exit(1);
+ }
+ console.log(" ✓ Connect to Chrome: ok");
+ check(bridge.isAlive(), "Bridge reports alive after connect");
+
+ // ── Test 2: Send Target.getTargets ─────────────────────────────────────
+ const responses: WireResponse[] = [];
+ const events: WireEvent[] = [];
+ let eventCount = 0;
+
+ const send: SendToClient = (clientId, msg) => {
+ if (msg.type === "response") responses.push(msg);
+ else events.push(msg);
+ };
+
+ bridge.onEvent((evt, _targets) => { eventCount++; });
+
+ const req1: WireRequest = { type: "request", id: 1, method: "Target.getTargets" };
+ bridge.handleRequest(req1, "pi-test", send);
+
+ await sleep(2000);
+
+ const res1 = responses.find(r => r.id === 1);
+ check(res1 != null, "Target.getTargets response received");
+ if (res1?.result) {
+ const r = res1.result as any;
+ check(Array.isArray(r.targetInfos), "targetInfos is array");
+ console.log(` Found ${r.targetInfos?.length ?? 0} targets`);
+ }
+
+ // ── Test 3: Create a new tab ───────────────────────────────────────────
+ responses.length = 0;
+ const req2: WireRequest = {
+ type: "request", id: 2, method: "Target.createTarget",
+ params: { url: "about:blank", newWindow: true },
+ };
+ bridge.handleRequest(req2, "pi-test", send);
+ await sleep(2000);
+
+ const res2 = responses.find(r => r.id === 2);
+ check(res2 != null, "Target.createTarget response received");
+ const targetId = (res2?.result as any)?.targetId as string | undefined;
+ check(typeof targetId === "string", `Created target: ${targetId ?? "FAILED"}`);
+
+ if (!targetId) {
+ console.error("Cannot continue without a target");
+ await bridge.stop();
+ process.exit(1);
+ }
+
+ // ── Test 4: Attach to target ───────────────────────────────────────────
+ responses.length = 0;
+ const req3: WireRequest = {
+ type: "request", id: 3, method: "Target.attachToTarget",
+ params: { targetId, flatten: true },
+ };
+ bridge.handleRequest(req3, "pi-test", send);
+ await sleep(2000);
+
+ const res3 = responses.find(r => r.id === 3);
+ check(res3 != null, "Target.attachToTarget response received");
+ const sessionId = (res3?.result as any)?.sessionId as string | undefined;
+ check(typeof sessionId === "string", `Session ID: ${sessionId ?? "FAILED"}`);
+
+ // Verify session ownership was tracked
+ if (sessionId) {
+ const owner = bridge.getSessionOwner(sessionId);
+ check(owner === "pi-test", "Session ownership tracked correctly");
+ }
+
+ if (!sessionId) {
+ await bridge.stop();
+ process.exit(1);
+ }
+
+ // ── Test 5: Navigate tab via session ───────────────────────────────────
+ responses.length = 0;
+ const req4: WireRequest = {
+ type: "request", id: 4, method: "Page.navigate",
+ params: { url: "https://example.com" },
+ sessionId,
+ };
+ bridge.handleRequest(req4, "pi-test", send);
+ await sleep(3000);
+
+ const res4 = responses.find(r => r.id === 4);
+ check(res4 != null, "Page.navigate response received");
+ const frameId = (res4?.result as any)?.frameId as string | undefined;
+ check(typeof frameId === "string", `Navigated to example.com (frame: ${frameId ?? "FAILED"})`);
+
+ // ── Test 6: Evaluate JS in the page ────────────────────────────────────
+ responses.length = 0;
+ const req5: WireRequest = {
+ type: "request", id: 5, method: "Runtime.evaluate",
+ params: { expression: "document.title", returnByValue: true },
+ sessionId,
+ };
+ bridge.handleRequest(req5, "pi-test", send);
+ await sleep(2000);
+
+ const res5 = responses.find(r => r.id === 5);
+ check(res5 != null, "Runtime.evaluate response received");
+ const title = (res5?.result as any)?.result?.value as string | undefined;
+ check(title === "Example Domain", `Page title: "${title ?? "FAILED"}" (expected "Example Domain")`);
+
+ // ── Test 7: Detach from target ─────────────────────────────────────────
+ responses.length = 0;
+ const req6: WireRequest = {
+ type: "request", id: 6, method: "Target.detachFromTarget",
+ params: { sessionId },
+ };
+ bridge.handleRequest(req6, "pi-test", send);
+ await sleep(1000);
+
+ const res6 = responses.find(r => r.id === 6);
+ check(res6 != null, "Target.detachFromTarget response received");
+
+ // Session ownership should be released
+ const ownerAfter = bridge.getSessionOwner(sessionId);
+ check(ownerAfter === undefined, "Session ownership released after detach");
+
+ // ── Test 8: Close target ───────────────────────────────────────────────
+ responses.length = 0;
+ const req7: WireRequest = {
+ type: "request", id: 7, method: "Target.closeTarget",
+ params: { targetId },
+ };
+ bridge.handleRequest(req7, "pi-test", send);
+ await sleep(1000);
+
+ const res7 = responses.find(r => r.id === 7);
+ check(res7 != null, "Target.closeTarget response received");
+
+ // ── Test 9: Remove client ──────────────────────────────────────────────
+ bridge.removeClient("pi-test");
+ // After removal, no pending requests should remain — verify by checking
+ // no errors are thrown when handling subsequent events.
+
+ // ── Cleanup ────────────────────────────────────────────────────────────
+ console.log("\nStopping bridge...");
+ await bridge.stop();
+ console.log("Bridge stopped ✓");
+
+ console.log(`\n${passed} passed, ${failed} failed`);
+ process.exit(failed > 0 ? 1 : 0);
+}
+
+function sleep(ms: number): Promise<void> {
+ return new Promise(r => setTimeout(r, ms));
+}
+
+main().catch((e) => {
+ console.error("Test failed:", e);
+ process.exit(1);
+});