forked from nicobailon/pi-subagents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi-spawn.test.ts
More file actions
120 lines (113 loc) · 4.18 KB
/
pi-spawn.test.ts
File metadata and controls
120 lines (113 loc) · 4.18 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import assert from "node:assert/strict";
import * as path from "node:path";
import { describe, it } from "node:test";
import { getPiSpawnCommand, resolveWindowsPiCliScript, type PiSpawnDeps } from "./pi-spawn.ts";
function makeDeps(input: {
platform?: NodeJS.Platform;
execPath?: string;
argv1?: string;
existing?: string[];
packageJsonPath?: string;
packageJsonContent?: string;
}): PiSpawnDeps {
const existing = new Set(input.existing ?? []);
const packageJsonPath = input.packageJsonPath;
const packageJsonContent = input.packageJsonContent;
return {
platform: input.platform,
execPath: input.execPath,
argv1: input.argv1,
existsSync: (filePath) => existing.has(filePath),
readFileSync: (_filePath, _encoding) => {
if (!packageJsonPath || !packageJsonContent) {
throw new Error("package json not configured");
}
return packageJsonContent;
},
resolvePackageJson: () => {
if (!packageJsonPath) throw new Error("package json path missing");
return packageJsonPath;
},
};
}
describe("getPiSpawnCommand", () => {
it("uses plain pi command on non-Windows", () => {
const args = ["--mode", "json", "Task: check output"];
const result = getPiSpawnCommand(args, { platform: "darwin" });
assert.deepEqual(result, { command: "pi", args });
});
it("uses node + argv1 script on Windows when argv1 is runnable JS", () => {
const argv1 = "/tmp/pi-entry.mjs";
const deps = makeDeps({
platform: "win32",
execPath: "/usr/local/bin/node",
argv1,
existing: [argv1],
});
const args = ["--mode", "json", 'Task: Read C:/dev/file.md and review "quotes" & pipes | too'];
const result = getPiSpawnCommand(args, deps);
assert.equal(result.command, "/usr/local/bin/node");
assert.equal(result.args[0], argv1);
assert.equal(result.args[3], args[2]);
});
it("resolves CLI script from package bin when argv1 is not runnable JS", () => {
const packageJsonPath = "/opt/pi/package.json";
// Compute expected path the same way the production code does:
// path.resolve(path.dirname(packageJsonPath), binPath) — which on Windows
// prepends the current drive letter to POSIX absolute paths.
const cliPath = path.resolve(path.dirname(packageJsonPath), "dist/cli/index.js");
const deps = makeDeps({
platform: "win32",
execPath: "/usr/local/bin/node",
argv1: "/opt/pi/subagent-runner.ts",
packageJsonPath,
packageJsonContent: JSON.stringify({ bin: { pi: "dist/cli/index.js" } }),
existing: [packageJsonPath, cliPath],
});
const result = getPiSpawnCommand(["-p", "Task: hello"], deps);
assert.equal(result.command, "/usr/local/bin/node");
assert.equal(result.args[0], cliPath);
});
it("falls back to pi when Windows CLI script cannot be resolved", () => {
const deps = makeDeps({
platform: "win32",
argv1: "/opt/pi/subagent-runner.ts",
existing: [],
});
const args = ["-p", "Task: hello"];
const result = getPiSpawnCommand(args, deps);
assert.deepEqual(result, { command: "pi", args });
});
});
describe("getPiSpawnCommand with piPackageRoot", () => {
it("resolves CLI script via piPackageRoot when argv1 is not runnable", () => {
const packageJsonPath = "/opt/pi/package.json";
const cliPath = path.resolve(path.dirname(packageJsonPath), "dist/cli/index.js");
const deps = makeDeps({
platform: "win32",
execPath: "/usr/local/bin/node",
argv1: "/opt/pi/subagent-runner.ts",
packageJsonPath,
packageJsonContent: JSON.stringify({ bin: { pi: "dist/cli/index.js" } }),
existing: [packageJsonPath, cliPath],
});
deps.piPackageRoot = "/opt/pi";
const result = getPiSpawnCommand(["-p", "Task: hello"], deps);
assert.equal(result.command, "/usr/local/bin/node");
assert.equal(result.args[0], cliPath);
});
});
describe("resolveWindowsPiCliScript", () => {
it("supports package bin as string", () => {
const packageJsonPath = "/opt/pi/package.json";
const cliPath = path.resolve(path.dirname(packageJsonPath), "dist/cli/index.mjs");
const deps = makeDeps({
platform: "win32",
argv1: "/opt/pi/subagent-runner.ts",
packageJsonPath,
packageJsonContent: JSON.stringify({ bin: "dist/cli/index.mjs" }),
existing: [packageJsonPath, cliPath],
});
assert.equal(resolveWindowsPiCliScript(deps), cliPath);
});
});