aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/util/daemon-socket-test.ts25
-rw-r--r--test/util/paths-test.ts50
2 files changed, 74 insertions, 1 deletions
diff --git a/test/util/daemon-socket-test.ts b/test/util/daemon-socket-test.ts
index eee0500..80a03e4 100644
--- a/test/util/daemon-socket-test.ts
+++ b/test/util/daemon-socket-test.ts
@@ -1,10 +1,11 @@
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { fileURLToPath } from "node:url";
+import { existsSync } from "node:fs";
import { Type } from "typebox";
import type { ExtensionAPI, ToolDefinition } from "@mariozechner/pi-coding-agent";
import type { TSchema } from "typebox";
-import { daemonSocketMayExist } from "../../src/daemon/spawn";
+import { daemonSocketMayExist, daemonSpawnCommand } from "../../src/daemon/spawn";
import { type AnyBrowserToolDefinition, registerBrowserTool } from "../../src/util/tool";
import type { BrowserClient } from "../../src/client";
import { ok } from "../../src/util/result";
@@ -32,6 +33,28 @@ describe("daemon socket presence check", () => {
});
});
+describe("daemon spawn command", () => {
+ test("the daemon runs on this process's node, not a shell", () => {
+ const spec = daemonSpawnCommand();
+ assert.notEqual(spec, null);
+ assert.equal(spec?.command, process.execPath);
+ });
+
+ test("the resolved tsx CLI is a real file on disk", () => {
+ const cli = daemonSpawnCommand()?.args[0];
+ assert.equal(typeof cli, "string");
+ // A hoisted install puts tsx beside the package, so a `<pkg>/node_modules/.bin` guess would miss it.
+ assert.equal(existsSync(String(cli)), true, `tsx CLI not found at ${String(cli)}`);
+ assert.match(String(cli), /cli\.mjs$/);
+ });
+
+ test("the daemon entrypoint is passed to tsx", () => {
+ const script = daemonSpawnCommand()?.args[1];
+ assert.match(String(script), /[\\/]daemon[\\/]index\.ts$/);
+ assert.equal(existsSync(String(script)), true);
+ });
+});
+
type GateProbe = { readonly ran: ReadonlyArray<string>; readonly text: string };
const runGate = async (ensureAlive: boolean | undefined): Promise<GateProbe> => {
diff --git a/test/util/paths-test.ts b/test/util/paths-test.ts
new file mode 100644
index 0000000..4fff247
--- /dev/null
+++ b/test/util/paths-test.ts
@@ -0,0 +1,50 @@
+// Runs on every OS in the CI matrix on purpose: `isPathWithin` delegates to `node:path`, so
+// only a real Windows run proves the backslash and drive-letter-case cases the old check failed.
+
+import { test, describe } from "node:test";
+import assert from "node:assert/strict";
+import { tmpdir } from "node:os";
+import { join, resolve, sep } from "node:path";
+import { isPathWithin } from "../../src/util/paths";
+
+const root = resolve(tmpdir());
+
+describe("path containment", () => {
+ test("a directory contains itself", () => {
+ assert.equal(isPathWithin(root, root), true);
+ });
+
+ test("a direct child is contained", () => {
+ assert.equal(isPathWithin(root, join(root, "script.js")), true);
+ });
+
+ test("a nested child is contained", () => {
+ assert.equal(isPathWithin(root, join(root, "a", "b", "script.js")), true);
+ });
+
+ test("a native-separator path is contained", () => {
+ assert.equal(isPathWithin(root, `${root}${sep}script.js`), true);
+ });
+
+ test("an unnormalised path that still lands inside is contained", () => {
+ assert.equal(isPathWithin(root, join(root, "a", "..", "script.js")), true);
+ });
+
+ test("an unnormalised path that escapes the root is rejected", () => {
+ assert.equal(isPathWithin(root, join(root, "..", "..", "evil.js")), false);
+ });
+
+ test("a sibling directory sharing the root's name prefix is rejected", () => {
+ assert.equal(isPathWithin(root, `${root}-sibling${sep}script.js`), false);
+ });
+
+ test("the root's own parent is rejected", () => {
+ assert.equal(isPathWithin(root, join(root, "..")), false);
+ });
+
+ test("the roots the run-script tool allows accept a file written into them", () => {
+ for (const allowed of [tmpdir(), process.cwd()]) {
+ assert.equal(isPathWithin(allowed, join(allowed, "bh-script.mjs")), true, allowed);
+ }
+ });
+});