-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathpi-spawn.test.ts
More file actions
379 lines (362 loc) · 12 KB
/
Copy pathpi-spawn.test.ts
File metadata and controls
379 lines (362 loc) · 12 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import assert from "node:assert/strict";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { describe, it } from "node:test";
import {
getPiSpawnCommand,
resolvePiCliScript,
type PiSpawnDeps,
} from "../../src/runs/shared/pi-spawn.ts";
function makeDeps(input: {
platform?: NodeJS.Platform;
execPath?: string;
argv1?: string;
existing?: string[];
packageJsonPath?: string;
packageJsonContent?: string;
packageEntry?: string;
env?: NodeJS.ProcessEnv;
}): 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: packageJsonPath ? () => packageJsonPath : undefined,
resolvePackageEntry: input.packageEntry
? () => input.packageEntry!
: undefined,
env: input.env ?? {},
};
}
describe("getPiSpawnCommand", () => {
it("honors explicit PI_SUBAGENT_PI_BINARY override on any platform", () => {
const args = ["--mode", "json", "Task: check output"];
const result = getPiSpawnCommand(args, {
platform: "win32",
execPath: "/usr/local/bin/node",
argv1: "/tmp/pi-entry.mjs",
env: {
PI_SUBAGENT_PI_BINARY: "/nix/store/pi-wrapper/bin/nhost-code-agent",
},
existsSync: () => true,
});
assert.deepEqual(result, {
command: "/nix/store/pi-wrapper/bin/nhost-code-agent",
args,
});
});
it("ignores a blank PI_SUBAGENT_PI_BINARY override", () => {
const args = ["--mode", "json", "Task: check output"];
const result = getPiSpawnCommand(args, {
platform: "darwin",
argv1: "/missing/host.js",
existsSync: () => false,
resolvePackageJson: () => {
throw new Error("Pi package unavailable");
},
env: { PI_SUBAGENT_PI_BINARY: " " },
});
assert.deepEqual(result, { command: "pi", args });
});
for (const [platform, execPath] of [
["darwin", "/opt/pi/pi"],
["win32", "C:\\Program Files\\Pi\\pi.exe"],
] as const) {
it(`uses the standalone Pi executable directly on ${platform}`, () => {
const packageJsonPath = "/opt/pi-package/package.json";
const cliPath = path.resolve(
path.dirname(packageJsonPath),
"dist/cli.js",
);
const deps = makeDeps({
platform,
execPath,
argv1: "/missing/host.js",
packageJsonPath,
packageJsonContent: JSON.stringify({ bin: { pi: "dist/cli.js" } }),
existing: [packageJsonPath, cliPath],
});
const args = ["--mode", "json", "-p", "Task: review diff"];
assert.deepEqual(getPiSpawnCommand(args, deps), {
command: execPath,
args,
});
});
}
for (const platform of ["darwin", "linux", "win32"] as const) {
it(`uses node + argv1 on ${platform} when argv1 belongs to the Pi package`, () => {
const tempDir = fs.mkdtempSync(
path.join(os.tmpdir(), "pi-spawn-argv-entry-"),
);
try {
const argv1 = path.join(tempDir, "dist", "cli.js");
fs.mkdirSync(path.dirname(argv1), { recursive: true });
fs.writeFileSync(argv1, "#!/usr/bin/env node\n");
fs.writeFileSync(
path.join(tempDir, "package.json"),
JSON.stringify({ name: "@earendil-works/pi-coding-agent" }),
);
const args = ["--mode", "json", 'Task: review "quotes" & pipes | too'];
const result = getPiSpawnCommand(args, {
platform,
execPath: "/usr/local/bin/node",
argv1,
env: {},
});
assert.deepEqual(result, {
command: "/usr/local/bin/node",
args: [fs.realpathSync(argv1), ...args],
});
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
}
it("uses node + package bin on POSIX when argv1 is not a verified Pi entry", () => {
const packageJsonPath = "/opt/pi/package.json";
const cliPath = path.resolve(
path.dirname(packageJsonPath),
"dist/cli/index.js",
);
const deps = makeDeps({
platform: "darwin",
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 args = ["-p", "Task: hello"];
const result = getPiSpawnCommand(args, deps);
assert.deepEqual(result, {
command: "/usr/local/bin/node",
args: [cliPath, ...args],
});
});
it("falls back to plain pi command on POSIX when CLI script cannot be resolved", () => {
const args = ["--mode", "json", "Task: check output"];
const result = getPiSpawnCommand(args, {
platform: "darwin",
argv1: "/missing/host.js",
existsSync: () => false,
resolvePackageJson: () => {
throw new Error("Pi package unavailable");
},
env: {},
});
assert.deepEqual(result, { command: "pi", args });
});
it("ignores embedded host entry points and resolves the Pi package bin on every platform", () => {
const tempDir = fs.mkdtempSync(
path.join(os.tmpdir(), "pi-spawn-embedded-host-"),
);
try {
const hostRoot = path.join(tempDir, "pi-web");
const hostEntry = path.join(hostRoot, "dist", "server.js");
const hostPackageJson = path.join(hostRoot, "package.json");
const piRoot = path.join(
tempDir,
"node_modules",
"@earendil-works",
"pi-coding-agent",
);
const piCli = path.join(piRoot, "dist", "cli.js");
fs.mkdirSync(path.dirname(hostEntry), { recursive: true });
fs.mkdirSync(path.dirname(piCli), { recursive: true });
fs.writeFileSync(hostEntry, "export {};\n");
fs.writeFileSync(
hostPackageJson,
JSON.stringify({ name: "@jmfederico/pi-web" }),
);
fs.writeFileSync(piCli, "#!/usr/bin/env node\n");
fs.writeFileSync(
path.join(piRoot, "package.json"),
JSON.stringify({
name: "@earendil-works/pi-coding-agent",
bin: { pi: "dist/cli.js" },
}),
);
for (const platform of ["darwin", "linux", "win32"] as const) {
const result = getPiSpawnCommand(["-p", "Task: hello"], {
platform,
execPath: "/usr/local/bin/node",
argv1: hostEntry,
resolvePackageJson: () => path.join(piRoot, "package.json"),
env: {},
});
assert.deepEqual(result, {
command: "/usr/local/bin/node",
args: [piCli, "-p", "Task: hello"],
});
}
fs.writeFileSync(hostPackageJson, "{");
for (const platform of ["darwin", "linux", "win32"] as const) {
const malformedHostResult = getPiSpawnCommand(["-p", "Task: hello"], {
platform,
execPath: "/usr/local/bin/node",
argv1: hostEntry,
resolvePackageJson: () => path.join(piRoot, "package.json"),
env: {},
});
assert.deepEqual(malformedHostResult, {
command: "/usr/local/bin/node",
args: [piCli, "-p", "Task: hello"],
});
}
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
it("validates argv1 ownership against its canonical target", () => {
const tempDir = fs.mkdtempSync(
path.join(os.tmpdir(), "pi-spawn-canonical-entry-"),
);
try {
const hostRoot = path.join(tempDir, "embedded-host");
const hostEntry = path.join(hostRoot, "dist", "server.js");
const piRoot = path.join(tempDir, "node_modules", "@earendil-works", "pi-coding-agent");
const disguisedEntry = path.join(piRoot, "dist", "cli.js");
const piCli = path.join(piRoot, "dist", "real-cli.js");
fs.mkdirSync(path.dirname(hostEntry), { recursive: true });
fs.mkdirSync(path.dirname(piCli), { recursive: true });
fs.writeFileSync(hostEntry, "export {};\n");
fs.writeFileSync(path.join(hostRoot, "package.json"), JSON.stringify({ name: "embedded-host" }));
fs.writeFileSync(piCli, "#!/usr/bin/env node\n");
fs.writeFileSync(path.join(piRoot, "package.json"), JSON.stringify({
name: "@earendil-works/pi-coding-agent",
bin: { pi: "dist/real-cli.js" },
}));
const result = getPiSpawnCommand(["-p", "Task: hello"], {
execPath: "/usr/local/bin/node",
argv1: disguisedEntry,
existsSync: (filePath) => filePath === disguisedEntry || fs.existsSync(filePath),
realpathSync: (filePath) => filePath === disguisedEntry ? hostEntry : fs.realpathSync(filePath),
resolvePackageJson: () => path.join(piRoot, "package.json"),
env: {},
});
assert.deepEqual(result, {
command: "/usr/local/bin/node",
args: [piCli, "-p", "Task: hello"],
});
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
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 });
});
it("walks from package main entry to resolve package bin", () => {
const tempDir = fs.mkdtempSync(
path.join(os.tmpdir(), "pi-spawn-package-root-"),
);
try {
const packageRoot = path.join(
tempDir,
"node_modules",
"@earendil-works",
"pi-coding-agent",
);
const entry = path.join(packageRoot, "dist", "index.js");
const cliPath = path.join(packageRoot, "dist", "cli", "index.js");
fs.mkdirSync(path.dirname(entry), { recursive: true });
fs.mkdirSync(path.dirname(cliPath), { recursive: true });
fs.writeFileSync(entry, "export {};\n");
fs.writeFileSync(cliPath, "#!/usr/bin/env node\n");
fs.writeFileSync(
path.join(packageRoot, "package.json"),
JSON.stringify({
name: "@earendil-works/pi-coding-agent",
bin: { pi: "dist/cli/index.js" },
}),
);
const result = getPiSpawnCommand(["-p", "Task: hello"], {
platform: "win32",
execPath: "/usr/local/bin/node",
argv1: "/opt/pi/subagent-runner.ts",
resolvePackageEntry: () => entry,
env: {},
});
assert.equal(result.command, "/usr/local/bin/node");
assert.equal(result.args[0], cliPath);
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
});
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("resolvePiCliScript", () => {
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(resolvePiCliScript(deps), cliPath);
});
});