pi-ate-workflow 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +102 -0
- package/README.md +104 -0
- package/agent-loader.ts +88 -0
- package/index.ts +18 -0
- package/output.ts +89 -0
- package/package.json +50 -0
- package/research-analysis.ts +64 -0
- package/research-catalog.ts +33 -0
- package/research-scan.ts +65 -0
- package/research-state.ts +46 -0
- package/research-types.ts +53 -0
- package/research-utils.ts +107 -0
- package/research.ts +52 -0
- package/state.ts +121 -0
- package/subagent-spawn-once.ts +164 -0
- package/subagent-spawn-visible.ts +194 -0
- package/subagent-utils.ts +38 -0
- package/subagent.ts +93 -0
- package/tests/agent-loader.test.ts +183 -0
- package/tests/agent-schema-extra.test.ts +107 -0
- package/tests/agent-schema.test.ts +116 -0
- package/tests/output.test.ts +107 -0
- package/tests/research-analysis.test.ts +155 -0
- package/tests/research-extra.test.ts +175 -0
- package/tests/research.test.ts +418 -0
- package/tests/smoke.subagent.test.ts +127 -0
- package/tests/state-manager.test.ts +245 -0
- package/tests/state.test.ts +298 -0
- package/tests/subagent-api.test.ts +158 -0
- package/tests/subagent-spawn-once.test.ts +154 -0
- package/tests/subagent-spawn-visible.test.ts +139 -0
- package/tests/subagent-utils.test.ts +124 -0
- package/tests/test-helpers.ts +40 -0
- package/tests/ui-updater.test.ts +129 -0
- package/tests/utils-find-session.test.ts +114 -0
- package/tests/utils-status-summary.test.ts +158 -0
- package/tests/utils.test.ts +48 -0
- package/tests/widget.test.ts +114 -0
- package/tests/workflow-execution.test.ts +165 -0
- package/tests/workflow-registration.test.ts +155 -0
- package/tests/workflow.test.ts +207 -0
- package/types.ts +24 -0
- package/utils.ts +74 -0
- package/vitest.config.ts +21 -0
- package/widget.ts +104 -0
- package/workflow.ts +113 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 测试 subagent-spawn-once.ts:spawnOnce
|
|
3
|
+
*
|
|
4
|
+
* 通过 mock node:child_process 的 spawn 来测试各种场景。
|
|
5
|
+
*/
|
|
6
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
7
|
+
|
|
8
|
+
// mock getPiCommand
|
|
9
|
+
vi.mock("../subagent-utils", () => ({
|
|
10
|
+
getPiCommand: vi.fn(() => ({ command: "node", args: ["/usr/local/bin/pi"] })),
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
import { EventEmitter } from "node:events";
|
|
14
|
+
|
|
15
|
+
let mockProc: any;
|
|
16
|
+
const spawnCalls: any[][] = [];
|
|
17
|
+
|
|
18
|
+
vi.mock("node:child_process", async (importOriginal) => {
|
|
19
|
+
const actual = await importOriginal() as any;
|
|
20
|
+
return {
|
|
21
|
+
...actual,
|
|
22
|
+
spawn: (...args: any[]) => {
|
|
23
|
+
spawnCalls.push(args);
|
|
24
|
+
return mockProc;
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
import { spawnOnce } from "../subagent-spawn-once";
|
|
30
|
+
|
|
31
|
+
function createMockProcess() {
|
|
32
|
+
const proc = new EventEmitter() as any;
|
|
33
|
+
proc.stdout = new EventEmitter();
|
|
34
|
+
proc.stderr = new EventEmitter();
|
|
35
|
+
proc.kill = vi.fn();
|
|
36
|
+
proc.killed = false;
|
|
37
|
+
return proc;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe("spawnOnce", () => {
|
|
41
|
+
beforeEach(() => {
|
|
42
|
+
vi.useFakeTimers();
|
|
43
|
+
mockProc = createMockProcess();
|
|
44
|
+
spawnCalls.length = 0;
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterEach(() => {
|
|
48
|
+
vi.useRealTimers();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("正常执行并收集输出", async () => {
|
|
52
|
+
const resultPromise = spawnOnce("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] });
|
|
53
|
+
|
|
54
|
+
// 模拟 session 开始
|
|
55
|
+
mockProc.stdout.emit("data", Buffer.from('{"type":"session","version":1,"id":"sess-1"}\n'));
|
|
56
|
+
// 模拟 message_end
|
|
57
|
+
mockProc.stdout.emit("data", Buffer.from('{"type":"message_end","message":{"role":"assistant","content":[{"type":"text","text":"hello"}]}}\n'));
|
|
58
|
+
// 模拟 close
|
|
59
|
+
mockProc.emit("close", 0);
|
|
60
|
+
|
|
61
|
+
const result = await resultPromise;
|
|
62
|
+
expect(result.exitCode).toBe(0);
|
|
63
|
+
expect(result.output).toContain("hello");
|
|
64
|
+
expect(result.subSessionId).toBe("sess-1");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("工具事件通过 onEvent 回调", async () => {
|
|
68
|
+
const onEvent = vi.fn();
|
|
69
|
+
const resultPromise = spawnOnce("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] }, undefined, undefined, undefined, onEvent);
|
|
70
|
+
|
|
71
|
+
mockProc.stdout.emit("data", Buffer.from('{"type":"tool_execution_start","toolName":"read"}\n'));
|
|
72
|
+
mockProc.emit("close", 0);
|
|
73
|
+
|
|
74
|
+
await resultPromise;
|
|
75
|
+
expect(onEvent).toHaveBeenCalledWith(expect.objectContaining({ type: "tool", toolName: "read" }));
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("thinking 事件通过 onEvent 回调", async () => {
|
|
79
|
+
const onEvent = vi.fn();
|
|
80
|
+
const resultPromise = spawnOnce("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] }, undefined, undefined, undefined, onEvent);
|
|
81
|
+
|
|
82
|
+
mockProc.stdout.emit("data", Buffer.from('{"type":"message_update","assistantMessageEvent":{"type":"text_delta","delta":"thinking..."}}\n'));
|
|
83
|
+
mockProc.emit("close", 0);
|
|
84
|
+
|
|
85
|
+
await resultPromise;
|
|
86
|
+
expect(onEvent).toHaveBeenCalledWith(expect.objectContaining({ type: "thinking" }));
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("进程错误时返回 error 信息", async () => {
|
|
90
|
+
const resultPromise = spawnOnce("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] });
|
|
91
|
+
|
|
92
|
+
mockProc.emit("error", new Error("spawn failed"));
|
|
93
|
+
|
|
94
|
+
const result = await resultPromise;
|
|
95
|
+
expect(result.exitCode).toBe(1);
|
|
96
|
+
expect(result.stderr).toBe("spawn failed");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("超时时终止进程", async () => {
|
|
100
|
+
const resultPromise = spawnOnce("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] }, undefined, undefined, 5000);
|
|
101
|
+
|
|
102
|
+
// 推进时间到超时
|
|
103
|
+
vi.advanceTimersByTime(6000);
|
|
104
|
+
|
|
105
|
+
const result = await resultPromise;
|
|
106
|
+
expect(result.timedOut).toBe(true);
|
|
107
|
+
expect(mockProc.kill).toHaveBeenCalled();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("signal abort 时终止进程(有 sessionId)", async () => {
|
|
111
|
+
const controller = new AbortController();
|
|
112
|
+
const resultPromise = spawnOnce("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] }, controller.signal);
|
|
113
|
+
|
|
114
|
+
// 先发送 session 事件以捕获 sessionId
|
|
115
|
+
mockProc.stdout.emit("data", Buffer.from('{"type":"session","version":1,"id":"sess-abort"}\n'));
|
|
116
|
+
|
|
117
|
+
controller.abort();
|
|
118
|
+
|
|
119
|
+
const result = await resultPromise;
|
|
120
|
+
expect(result.exitCode).toBe(1);
|
|
121
|
+
expect(result.stderr).toContain("aborted");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("没有输出时返回 (no output)", async () => {
|
|
125
|
+
const resultPromise = spawnOnce("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] });
|
|
126
|
+
|
|
127
|
+
mockProc.emit("close", 0);
|
|
128
|
+
|
|
129
|
+
const result = await resultPromise;
|
|
130
|
+
expect(result.output).toBe("(no output)");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("model override 传递给 pi 命令", async () => {
|
|
134
|
+
const resultPromise = spawnOnce("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] }, undefined, "gpt-4");
|
|
135
|
+
|
|
136
|
+
mockProc.emit("close", 0);
|
|
137
|
+
await resultPromise;
|
|
138
|
+
|
|
139
|
+
const args = spawnCalls[0][1];
|
|
140
|
+
expect(args).toContain("--model");
|
|
141
|
+
expect(args).toContain("gpt-4");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("agentDef.model 作为默认 model", async () => {
|
|
145
|
+
const resultPromise = spawnOnce("task", "/cwd", "/tmp/prompt.md", { tools: ["read"], model: "claude-3" });
|
|
146
|
+
|
|
147
|
+
mockProc.emit("close", 0);
|
|
148
|
+
await resultPromise;
|
|
149
|
+
|
|
150
|
+
const args = spawnCalls[0][1];
|
|
151
|
+
expect(args).toContain("--model");
|
|
152
|
+
expect(args).toContain("claude-3");
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 测试 subagent-spawn-visible.ts:spawnVisible
|
|
3
|
+
*
|
|
4
|
+
* spawnVisible 深度依赖 tmux + execFileSync + 轮询。
|
|
5
|
+
* 当 tmux 不可用时,会 fallback 到 spawnOnce。
|
|
6
|
+
* 这里主要测试:tmux 成功路径 + fallback 路径 + 超时 + abort。
|
|
7
|
+
*/
|
|
8
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
9
|
+
|
|
10
|
+
vi.mock("../subagent-utils", () => ({
|
|
11
|
+
getPiCommand: vi.fn(() => ({ command: "node", args: ["/usr/local/bin/pi"] })),
|
|
12
|
+
}));
|
|
13
|
+
vi.mock("@earendil-works/pi-coding-agent", () => ({}));
|
|
14
|
+
vi.mock("../subagent-spawn-once", () => ({
|
|
15
|
+
spawnOnce: vi.fn(() => Promise.resolve({ exitCode: 0, output: "fallback", stderr: "" })),
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
const { childProcessMock } = vi.hoisted(() => {
|
|
19
|
+
const results: { error?: Error; stdout?: string }[] = [];
|
|
20
|
+
let callIdx = 0;
|
|
21
|
+
return {
|
|
22
|
+
childProcessMock: {
|
|
23
|
+
__results: results,
|
|
24
|
+
__reset: () => { callIdx = 0; results.length = 0; },
|
|
25
|
+
execFileSync: (...args: any[]) => {
|
|
26
|
+
const r = results[callIdx++];
|
|
27
|
+
if (r?.error) throw r.error;
|
|
28
|
+
return r?.stdout ?? "";
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
vi.mock("node:child_process", async (importOriginal) => {
|
|
35
|
+
const actual = await importOriginal() as any;
|
|
36
|
+
return { ...actual, execFileSync: childProcessMock.execFileSync };
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const { fsMock } = vi.hoisted(() => {
|
|
40
|
+
const mock = {
|
|
41
|
+
existsSync: vi.fn(() => false),
|
|
42
|
+
readFileSync: vi.fn(() => ""),
|
|
43
|
+
writeFileSync: vi.fn(),
|
|
44
|
+
mkdirSync: vi.fn(),
|
|
45
|
+
mkdtempSync: vi.fn(() => "/tmp/pi-visible-test"),
|
|
46
|
+
rmSync: vi.fn(),
|
|
47
|
+
};
|
|
48
|
+
return { fsMock: mock };
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
vi.mock("node:fs", () => fsMock);
|
|
52
|
+
|
|
53
|
+
import { spawnVisible } from "../subagent-spawn-visible";
|
|
54
|
+
import { spawnOnce } from "../subagent-spawn-once";
|
|
55
|
+
|
|
56
|
+
describe("spawnVisible", () => {
|
|
57
|
+
beforeEach(() => {
|
|
58
|
+
vi.useFakeTimers({ shouldAdvanceTime: true });
|
|
59
|
+
childProcessMock.__reset();
|
|
60
|
+
fsMock.existsSync.mockReturnValue(false);
|
|
61
|
+
fsMock.readFileSync.mockReturnValue("");
|
|
62
|
+
});
|
|
63
|
+
afterEach(() => {
|
|
64
|
+
vi.useRealTimers();
|
|
65
|
+
vi.restoreAllMocks();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("tmux 不可用时 fallback 到 spawnOnce", async () => {
|
|
69
|
+
// tmux split-window 失败 → fallback
|
|
70
|
+
childProcessMock.__results.push({ error: new Error("tmux not found") });
|
|
71
|
+
|
|
72
|
+
const result = await spawnVisible("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] });
|
|
73
|
+
expect(spawnOnce).toHaveBeenCalled();
|
|
74
|
+
expect(result.exitCode).toBe(0);
|
|
75
|
+
expect(result.output).toBe("fallback");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("tmux 成功 + pane 消失后读取结果文件", async () => {
|
|
79
|
+
// 1st call: tmux split-window → 成功,返回 pane ID
|
|
80
|
+
childProcessMock.__results.push({ stdout: " %42\n" });
|
|
81
|
+
// 2nd+ calls: tmux list-panes → 失败(pane 已消失)触发 finish
|
|
82
|
+
childProcessMock.__results.push({ error: new Error("no pane") });
|
|
83
|
+
childProcessMock.__results.push({ error: new Error("no pane") });
|
|
84
|
+
// finish 内部调 execFileSync("sleep",...)
|
|
85
|
+
childProcessMock.__results.push({ stdout: "" });
|
|
86
|
+
|
|
87
|
+
// session 文件有内容
|
|
88
|
+
fsMock.existsSync.mockReturnValue(true);
|
|
89
|
+
fsMock.readFileSync.mockReturnValue('{"type":"message","message":{"role":"assistant","content":[{"type":"text","text":"hello"}]}}\n');
|
|
90
|
+
|
|
91
|
+
const resultPromise = spawnVisible("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] });
|
|
92
|
+
|
|
93
|
+
// 推进时间让轮询执行(2s 间隔)
|
|
94
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
95
|
+
|
|
96
|
+
const result = await resultPromise;
|
|
97
|
+
expect(result.output).toContain("hello");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("超时时返回 timedOut", async () => {
|
|
101
|
+
// tmux 成功
|
|
102
|
+
childProcessMock.__results.push({ stdout: " %42\n" });
|
|
103
|
+
// list-panes 一直成功(pane 存在),但无 session 文件
|
|
104
|
+
for (let i = 0; i < 100; i++) {
|
|
105
|
+
childProcessMock.__results.push({ stdout: "%42: [100x50] [active]" });
|
|
106
|
+
}
|
|
107
|
+
fsMock.existsSync.mockReturnValue(false);
|
|
108
|
+
|
|
109
|
+
const resultPromise = spawnVisible("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] }, undefined, undefined, 1000);
|
|
110
|
+
|
|
111
|
+
// 推进到超时
|
|
112
|
+
await vi.advanceTimersByTimeAsync(3000);
|
|
113
|
+
|
|
114
|
+
const result = await resultPromise;
|
|
115
|
+
expect(result.timedOut).toBe(true);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("signal abort 时终止", async () => {
|
|
119
|
+
const controller = new AbortController();
|
|
120
|
+
|
|
121
|
+
// tmux 成功
|
|
122
|
+
childProcessMock.__results.push({ stdout: " %42\n" });
|
|
123
|
+
// list-panes 持续成功
|
|
124
|
+
for (let i = 0; i < 100; i++) {
|
|
125
|
+
childProcessMock.__results.push({ stdout: "%42" });
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
fsMock.existsSync.mockReturnValue(false);
|
|
129
|
+
|
|
130
|
+
const resultPromise = spawnVisible("task", "/cwd", "/tmp/prompt.md", { tools: ["read"] }, controller.signal, undefined, 10000);
|
|
131
|
+
|
|
132
|
+
// 触发 abort
|
|
133
|
+
controller.abort();
|
|
134
|
+
|
|
135
|
+
const result = await resultPromise;
|
|
136
|
+
expect(result.stderr).toContain("aborted");
|
|
137
|
+
expect(result.exitCode).toBe(1);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests: subagent-utils.ts — getPiCommand + writeTempPrompt
|
|
3
|
+
*
|
|
4
|
+
* 测试场景(7 用例):
|
|
5
|
+
* 1) argv[1] 文件存在 → execPath + argv[1]
|
|
6
|
+
* 2) argv[1] 文件不存在 + execPath=node → pi
|
|
7
|
+
* 3) argv[1] undefined → pi
|
|
8
|
+
* 4) argv[1] bun virtual → pi
|
|
9
|
+
* 5) execPath 非 node/bun → execPath
|
|
10
|
+
* 6) writeTempPrompt 创建文件
|
|
11
|
+
* 7) writeTempPrompt 内容正确
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { describe, it, expect, vi, afterEach } from "vitest";
|
|
15
|
+
import * as fs from "node:fs";
|
|
16
|
+
import * as path from "node:path";
|
|
17
|
+
|
|
18
|
+
// ---- mock fs.existsSync using a mutable flag ----
|
|
19
|
+
let mockExistsResult = true;
|
|
20
|
+
vi.mock("node:fs", async (importOriginal) => {
|
|
21
|
+
const actual = await importOriginal<typeof import("node:fs")>();
|
|
22
|
+
return {
|
|
23
|
+
...actual,
|
|
24
|
+
existsSync: (p: string) => {
|
|
25
|
+
// For the specific test path, use mock; for everything else, use real
|
|
26
|
+
if (p === "/mock/exists/script.ts" || p === "/mock/not-exists/script.ts") {
|
|
27
|
+
return mockExistsResult;
|
|
28
|
+
}
|
|
29
|
+
return actual.existsSync(p);
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Mock os.tmpdir for writeTempPrompt
|
|
35
|
+
vi.mock("node:os", () => ({ tmpdir: () => "/tmp", default: { tmpdir: () => "/tmp" } }));
|
|
36
|
+
|
|
37
|
+
function setArgv(argv1?: string) {
|
|
38
|
+
const base = process.argv[0] || "/usr/bin/node";
|
|
39
|
+
const arr = argv1 !== undefined ? [base, argv1] : [base];
|
|
40
|
+
Object.defineProperty(process, "argv", { value: arr, configurable: true });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function setExecPath(p: string) {
|
|
44
|
+
Object.defineProperty(process, "execPath", { value: p, configurable: true });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
import { getPiCommand, writeTempPrompt } from "../subagent-utils";
|
|
48
|
+
|
|
49
|
+
describe("getPiCommand", () => {
|
|
50
|
+
const origArgv = process.argv;
|
|
51
|
+
const origExecPath = process.execPath;
|
|
52
|
+
|
|
53
|
+
afterEach(() => {
|
|
54
|
+
Object.defineProperty(process, "argv", { value: origArgv, configurable: true });
|
|
55
|
+
Object.defineProperty(process, "execPath", { value: origExecPath, configurable: true });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("argv[1] 文件存在 → execPath + argv[1]", () => {
|
|
59
|
+
setArgv("/mock/exists/script.ts");
|
|
60
|
+
setExecPath("/usr/bin/node");
|
|
61
|
+
mockExistsResult = true;
|
|
62
|
+
const result = getPiCommand();
|
|
63
|
+
expect(result.command).toBe("/usr/bin/node");
|
|
64
|
+
expect(result.args).toEqual(["/mock/exists/script.ts"]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("argv[1] 文件不存在 + execPath=node → pi", () => {
|
|
68
|
+
setArgv("/mock/not-exists/script.ts");
|
|
69
|
+
setExecPath("/usr/bin/node");
|
|
70
|
+
mockExistsResult = false;
|
|
71
|
+
const result = getPiCommand();
|
|
72
|
+
expect(result.command).toBe("pi");
|
|
73
|
+
expect(result.args).toEqual([]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("argv[1] 为 undefined → pi", () => {
|
|
77
|
+
setArgv(undefined);
|
|
78
|
+
setExecPath("/usr/bin/node");
|
|
79
|
+
mockExistsResult = false;
|
|
80
|
+
const result = getPiCommand();
|
|
81
|
+
expect(result.command).toBe("pi");
|
|
82
|
+
expect(result.args).toEqual([]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("argv[1] 是 bun virtual script → pi", () => {
|
|
86
|
+
setArgv("/$bunfs/root/some/script.ts");
|
|
87
|
+
setExecPath("/usr/bin/bun");
|
|
88
|
+
const result = getPiCommand();
|
|
89
|
+
expect(result.command).toBe("pi");
|
|
90
|
+
expect(result.args).toEqual([]);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("execPath 不是 node/bun 且 argv[1] 不存在 → execPath", () => {
|
|
94
|
+
setArgv("/mock/not-exists/script.ts");
|
|
95
|
+
setExecPath("/usr/local/bin/python3");
|
|
96
|
+
mockExistsResult = false;
|
|
97
|
+
const result = getPiCommand();
|
|
98
|
+
expect(result.command).toBe("/usr/local/bin/python3");
|
|
99
|
+
expect(result.args).toEqual([]);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe("writeTempPrompt", () => {
|
|
104
|
+
it("创建临时目录和文件,返回正确路径", async () => {
|
|
105
|
+
const result = await writeTempPrompt("test content");
|
|
106
|
+
expect(result).toMatch(/pi-pv-/);
|
|
107
|
+
expect(result).toMatch(/system-prompt\.md$/);
|
|
108
|
+
// Cleanup
|
|
109
|
+
const dir = path.dirname(result);
|
|
110
|
+
fs.unlinkSync(result);
|
|
111
|
+
fs.rmdirSync(dir);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("文件内容正确写入", async () => {
|
|
115
|
+
const content = "Hello, World!";
|
|
116
|
+
const result = await writeTempPrompt(content);
|
|
117
|
+
const written = fs.readFileSync(result, "utf-8");
|
|
118
|
+
expect(written).toBe(content);
|
|
119
|
+
// Cleanup
|
|
120
|
+
const dir = path.dirname(result);
|
|
121
|
+
fs.unlinkSync(result);
|
|
122
|
+
fs.rmdirSync(dir);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 共享 mock 工具函数:createMockContext 等
|
|
3
|
+
* 供 state.test.ts / workflow.test.ts 使用
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { vi } from "vitest";
|
|
7
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
8
|
+
|
|
9
|
+
export function createMockContext(entries: any[] = []): ExtensionContext {
|
|
10
|
+
return {
|
|
11
|
+
cwd: "/tmp/test",
|
|
12
|
+
hasUI: true,
|
|
13
|
+
ui: {
|
|
14
|
+
setStatus: vi.fn(),
|
|
15
|
+
setWidget: vi.fn(),
|
|
16
|
+
select: vi.fn(),
|
|
17
|
+
confirm: vi.fn(),
|
|
18
|
+
input: vi.fn(),
|
|
19
|
+
notify: vi.fn(),
|
|
20
|
+
onTerminalInput: vi.fn(),
|
|
21
|
+
setWorkingMessage: vi.fn(),
|
|
22
|
+
setWorkingVisible: vi.fn(),
|
|
23
|
+
setWorkingIndicator: vi.fn(),
|
|
24
|
+
setHiddenThinkingLabel: vi.fn(),
|
|
25
|
+
},
|
|
26
|
+
sessionManager: {
|
|
27
|
+
getEntries: vi.fn().mockReturnValue(entries),
|
|
28
|
+
} as any,
|
|
29
|
+
isIdle: vi.fn().mockReturnValue(true),
|
|
30
|
+
signal: undefined,
|
|
31
|
+
abort: vi.fn(),
|
|
32
|
+
hasPendingMessages: vi.fn().mockReturnValue(false),
|
|
33
|
+
shutdown: vi.fn(),
|
|
34
|
+
getContextUsage: vi.fn(),
|
|
35
|
+
compact: vi.fn(),
|
|
36
|
+
getSystemPrompt: vi.fn().mockReturnValue(""),
|
|
37
|
+
model: undefined,
|
|
38
|
+
modelRegistry: {} as any,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests: state.ts — createUIUpdater (update/clear)
|
|
3
|
+
*
|
|
4
|
+
* 测试场景(6 用例):
|
|
5
|
+
* 1) 每个已知 phase 设置正确的 icon+label
|
|
6
|
+
* 2) 未知 phase → 清除 UI
|
|
7
|
+
* 3) clear() 清除 status 和 widget
|
|
8
|
+
* 4) 重复 update 正确覆盖
|
|
9
|
+
* 5) phase 值字符串匹配
|
|
10
|
+
* 6) extraWidget 透传(预留)
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { describe, it, expect, vi } from "vitest";
|
|
14
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
15
|
+
import { createMockContext } from "./test-helpers";
|
|
16
|
+
|
|
17
|
+
// ============================================================
|
|
18
|
+
// 内联实现:createUIUpdater
|
|
19
|
+
// ============================================================
|
|
20
|
+
|
|
21
|
+
interface UIUpdater<T> {
|
|
22
|
+
update(ctx: ExtensionContext, state: T): void;
|
|
23
|
+
clear(ctx: ExtensionContext): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function createUIUpdater<T>(options: {
|
|
27
|
+
statusKey: string;
|
|
28
|
+
phases: Array<{ value: string; icon: string; label: string; color: string }>;
|
|
29
|
+
}): UIUpdater<T> {
|
|
30
|
+
return {
|
|
31
|
+
update(ctx: ExtensionContext, state: T): void {
|
|
32
|
+
const phase = (state as any).phase as string;
|
|
33
|
+
const pc = options.phases.find(p => p.value === phase);
|
|
34
|
+
if (pc && pc.icon) {
|
|
35
|
+
ctx.ui.setStatus(options.statusKey, `${pc.icon} ${pc.label}`);
|
|
36
|
+
ctx.ui.setWidget(options.statusKey, [`${pc.icon} ${pc.label}`]);
|
|
37
|
+
} else {
|
|
38
|
+
ctx.ui.setStatus(options.statusKey, undefined);
|
|
39
|
+
ctx.ui.setWidget(options.statusKey, undefined);
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
clear(ctx: ExtensionContext): void {
|
|
43
|
+
ctx.ui.setStatus(options.statusKey, undefined);
|
|
44
|
+
ctx.ui.setWidget(options.statusKey, undefined);
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ============================================================
|
|
50
|
+
// Tests
|
|
51
|
+
// ============================================================
|
|
52
|
+
|
|
53
|
+
describe("createUIUpdater — update", () => {
|
|
54
|
+
const phases = [
|
|
55
|
+
{ value: "idle", icon: "", label: "", color: "" },
|
|
56
|
+
{ value: "planning", icon: "📝", label: "Planning", color: "accent" },
|
|
57
|
+
{ value: "verifying", icon: "🔍", label: "Reviewing", color: "warning" },
|
|
58
|
+
{ value: "fixing", icon: "🔧", label: "Fixing", color: "error" },
|
|
59
|
+
{ value: "review-decision", icon: "📋", label: "Deciding", color: "accent" },
|
|
60
|
+
{ value: "executing", icon: "🚀", label: "Executing", color: "success" },
|
|
61
|
+
];
|
|
62
|
+
const opts = { statusKey: "plan-verify", phases };
|
|
63
|
+
|
|
64
|
+
it("每个已知 phase 设置正确的 icon+label", () => {
|
|
65
|
+
const updater = createUIUpdater(opts);
|
|
66
|
+
for (const phase of phases) {
|
|
67
|
+
const ctx = createMockContext();
|
|
68
|
+
updater.update(ctx, { phase: phase.value, issues: [], counter: 0 });
|
|
69
|
+
if (phase.value === "idle") {
|
|
70
|
+
expect(ctx.ui.setStatus).toHaveBeenCalledWith("plan-verify", undefined);
|
|
71
|
+
expect(ctx.ui.setWidget).toHaveBeenCalledWith("plan-verify", undefined);
|
|
72
|
+
} else {
|
|
73
|
+
expect(ctx.ui.setStatus).toHaveBeenCalledWith("plan-verify", `${phase.icon} ${phase.label}`);
|
|
74
|
+
expect(ctx.ui.setWidget).toHaveBeenCalledWith("plan-verify", [`${phase.icon} ${phase.label}`]);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("未知 phase → 清除 UI", () => {
|
|
80
|
+
const updater = createUIUpdater(opts);
|
|
81
|
+
const ctx = createMockContext();
|
|
82
|
+
updater.update(ctx, { phase: "unknown" as any, issues: [], counter: 0 });
|
|
83
|
+
expect(ctx.ui.setStatus).toHaveBeenCalledWith("plan-verify", undefined);
|
|
84
|
+
expect(ctx.ui.setWidget).toHaveBeenCalledWith("plan-verify", undefined);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe("createUIUpdater — clear", () => {
|
|
89
|
+
it("clear() 清除 status 和 widget", () => {
|
|
90
|
+
const updater = createUIUpdater({ statusKey: "plan-verify", phases: [] });
|
|
91
|
+
const ctx = createMockContext();
|
|
92
|
+
updater.clear(ctx);
|
|
93
|
+
expect(ctx.ui.setStatus).toHaveBeenCalledWith("plan-verify", undefined);
|
|
94
|
+
expect(ctx.ui.setWidget).toHaveBeenCalledWith("plan-verify", undefined);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe("createUIUpdater — 覆盖行为", () => {
|
|
99
|
+
it("重复 update 正确覆盖之前的设置", () => {
|
|
100
|
+
const phases = [
|
|
101
|
+
{ value: "planning", icon: "📝", label: "Planning", color: "accent" },
|
|
102
|
+
{ value: "executing", icon: "🚀", label: "Executing", color: "success" },
|
|
103
|
+
];
|
|
104
|
+
const updater = createUIUpdater({ statusKey: "pv", phases });
|
|
105
|
+
const ctx = createMockContext();
|
|
106
|
+
|
|
107
|
+
updater.update(ctx, { phase: "planning" });
|
|
108
|
+
expect(ctx.ui.setStatus).toHaveBeenLastCalledWith("pv", "📝 Planning");
|
|
109
|
+
updater.update(ctx, { phase: "executing" });
|
|
110
|
+
expect(ctx.ui.setStatus).toHaveBeenLastCalledWith("pv", "🚀 Executing");
|
|
111
|
+
updater.clear(ctx);
|
|
112
|
+
expect(ctx.ui.setStatus).toHaveBeenLastCalledWith("pv", undefined);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("createUIUpdater — 字符串匹配", () => {
|
|
117
|
+
it("phase 值使用字符串字面量匹配", () => {
|
|
118
|
+
const customPhases = [
|
|
119
|
+
{ value: "step-1", icon: "1️⃣", label: "Step 1", color: "accent" },
|
|
120
|
+
{ value: "step-2", icon: "2️⃣", label: "Step 2", color: "warning" },
|
|
121
|
+
];
|
|
122
|
+
const updater = createUIUpdater<{ phase: string }>({ statusKey: "test", phases: customPhases });
|
|
123
|
+
const ctx = createMockContext();
|
|
124
|
+
updater.update(ctx, { phase: "step-1" });
|
|
125
|
+
expect(ctx.ui.setStatus).toHaveBeenCalledWith("test", "1️⃣ Step 1");
|
|
126
|
+
updater.update(ctx, { phase: "step-2" });
|
|
127
|
+
expect(ctx.ui.setStatus).toHaveBeenCalledWith("test", "2️⃣ Step 2");
|
|
128
|
+
});
|
|
129
|
+
});
|