aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAman Kumar <145339995+amankumarsingh77@users.noreply.github.com>2026-10-11 23:33:25 +0530
committerGitHub <noreply@github.com>2026-10-11 23:33:25 +0530
commit0fe64593bcc3379032840b35833ea39f23aa9322 (patch)
treef01055f53a8a3d92a244b7fc7ecb0b88b6bf252b /test
parent7833e2ecd5dc5fd9d7e63322680711d341d885ee (diff)
fix(daemon): stop importing typebox in the daemon process (#38)
pi installs packages with dev and peer dependencies omitted and supplies typebox only to the extension host. The daemon is a separate Node process whose import graph reached typebox via src/cdp/discovery.ts, so from a pi-managed install it crashed with "Cannot find module 'typebox'" before binding its socket. Parse /json/version with the package's own guards, and add a test that keeps every package reachable from the daemon a runtime dependency. Fixes #34 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Diffstat (limited to 'test')
-rw-r--r--test/cdp/discovery-test.ts19
-rw-r--r--test/daemon/entry-imports-test.ts50
2 files changed, 68 insertions, 1 deletions
diff --git a/test/cdp/discovery-test.ts b/test/cdp/discovery-test.ts
index 7fc18fb..61b42b7 100644
--- a/test/cdp/discovery-test.ts
+++ b/test/cdp/discovery-test.ts
@@ -1,6 +1,6 @@
import { test, describe } from "node:test";
import assert from "node:assert/strict";
-import { endpointFromEnv } from "../../src/cdp/discovery";
+import { endpointFromEnv, parseVersionResponse } from "../../src/cdp/discovery";
describe("BU_CDP_WS endpoint override", () => {
test("an unset variable leaves discovery to run normally", () => {
@@ -27,3 +27,20 @@ describe("BU_CDP_WS endpoint override", () => {
assert.equal(endpointFromEnv({ BU_CDP_WS: " " }), undefined);
});
});
+
+describe("/json/version parsing", () => {
+ test("extracts webSocketDebuggerUrl from a well-formed response", () => {
+ const raw = JSON.stringify({ Browser: "Chrome/154.0", webSocketDebuggerUrl: "ws://127.0.0.1:9222/devtools/browser/abc" });
+ assert.equal(parseVersionResponse(raw), "ws://127.0.0.1:9222/devtools/browser/abc");
+ });
+
+ test("a response without the url yields null", () => {
+ assert.equal(parseVersionResponse(JSON.stringify({ Browser: "Chrome/154.0" })), null);
+ });
+
+ test("a non-string url, a non-object body, and invalid JSON all yield null", () => {
+ assert.equal(parseVersionResponse(JSON.stringify({ webSocketDebuggerUrl: 42 })), null);
+ assert.equal(parseVersionResponse(JSON.stringify([1, 2])), null);
+ assert.equal(parseVersionResponse("not json"), null);
+ });
+});
diff --git a/test/daemon/entry-imports-test.ts b/test/daemon/entry-imports-test.ts
new file mode 100644
index 0000000..24119a6
--- /dev/null
+++ b/test/daemon/entry-imports-test.ts
@@ -0,0 +1,50 @@
+import { test, describe } from "node:test";
+import assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import { dirname, join, resolve } from "node:path";
+import { fileURLToPath } from "node:url";
+
+// The daemon is spawned as a separate Node process that resolves modules from the package directory
+// upward. pi installs packages with dev and peer dependencies omitted and maps host-provided packages
+// (typebox) only inside the extension host, so anything the daemon imports must be a runtime
+// dependency of this package or a Node builtin. Issue #34 was `Cannot find module 'typebox'`.
+const RUNTIME_DEPENDENCIES: ReadonlySet<string> = new Set(
+ Object.keys(JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).dependencies ?? {}),
+);
+
+const srcRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..", "src");
+
+const importSpecifiers = (file: string): ReadonlyArray<string> =>
+ [...readFileSync(file, "utf8").matchAll(/^(?:import|export)[^'"\n]*from\s*["']([^'"]+)["']/gm)].map((m) => m[1] ?? "");
+
+const externalImportsReachableFrom = (entry: string): ReadonlyMap<string, string> => {
+ const external = new Map<string, string>();
+ const seen = new Set<string>();
+ const walk = (file: string): void => {
+ if (seen.has(file)) return;
+ seen.add(file);
+ for (const spec of importSpecifiers(file)) {
+ if (spec.startsWith(".")) {
+ let target = resolve(dirname(file), spec);
+ if (!target.endsWith(".ts")) target += ".ts";
+ walk(target);
+ } else if (!spec.startsWith("node:")) {
+ external.set(spec, file.slice(srcRoot.length + 1));
+ }
+ }
+ };
+ walk(entry);
+ return external;
+};
+
+describe("daemon entrypoint imports", () => {
+ test("the daemon only reaches runtime dependencies, never host-provided or dev packages", () => {
+ const external = externalImportsReachableFrom(join(srcRoot, "daemon", "index.ts"));
+ const offenders = [...external].filter(([spec]) => !RUNTIME_DEPENDENCIES.has(spec.split("/")[0] ?? spec));
+ assert.deepEqual(
+ offenders,
+ [],
+ `daemon imports packages that are not in "dependencies": ${offenders.map(([s, f]) => `${s} (via ${f})`).join(", ")}`,
+ );
+ });
+});