@mrrisega/dsh-remote 0.7.10-beta.3 → 0.7.11-beta.3
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/clients/dsh-remote/agent/README.md +412 -0
- package/clients/dsh-remote/agent/agent-e2e-codex.mjs +185 -0
- package/clients/dsh-remote/agent/agent-e2e-real.mjs +443 -0
- package/clients/dsh-remote/agent/codex.mjs +736 -0
- package/clients/dsh-remote/agent/detect.mjs +399 -0
- package/clients/dsh-remote/agent/events.mjs +511 -0
- package/clients/dsh-remote/agent/gemini.mjs +503 -0
- package/clients/dsh-remote/agent/index.mjs +339 -0
- package/clients/dsh-remote/agent/opencode.mjs +628 -0
- package/clients/dsh-remote/agent/permission-bridge.mjs +534 -0
- package/clients/dsh-remote/agent/proc.mjs +179 -0
- package/clients/dsh-remote/agent/version.mjs +148 -0
- package/clients/dsh-remote/approval-coordinator.mjs +150 -0
- package/clients/dsh-remote/decision-poller.mjs +235 -0
- package/clients/dsh-remote/dsh-bridge.mjs +214 -1
- package/clients/dsh-remote/push-reporter.mjs +54 -0
- package/clients/dsh-remote/test/agent-codex.test.mjs +535 -0
- package/clients/dsh-remote/test/agent-detect.test.mjs +390 -0
- package/clients/dsh-remote/test/agent-events.test.mjs +340 -0
- package/clients/dsh-remote/test/agent-gemini.test.mjs +355 -0
- package/clients/dsh-remote/test/agent-jsonl-driver.test.mjs +165 -0
- package/clients/dsh-remote/test/agent-opencode.test.mjs +369 -0
- package/clients/dsh-remote/test/agent-permission-bridge.test.mjs +340 -0
- package/clients/dsh-remote/test/agent-proc.test.mjs +175 -0
- package/clients/dsh-remote/test/agent-runtime.test.mjs +282 -0
- package/clients/dsh-remote/test/agent-version.test.mjs +122 -0
- package/clients/dsh-remote/test/approval-coordinator.test.mjs +192 -0
- package/clients/dsh-remote/test/decision-poller.test.mjs +328 -0
- package/clients/dsh-remote/test/wecom-bot.test.mjs +1438 -0
- package/clients/dsh-remote/wecom-bot.mjs +2444 -0
- package/dsh-setup.mjs +18 -3
- package/package.json +2 -2
- package/packages/dsh-remote-web/lib/client.js +1048 -223
- package/packages/dsh-remote-web/lib/index.js +299 -2
- package/packages/dsh-remote-web/package.json +1 -1
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/README.md +412 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/agent-e2e-codex.mjs +185 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/agent-e2e-real.mjs +443 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/codex.mjs +736 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/detect.mjs +399 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/events.mjs +511 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/gemini.mjs +503 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/index.mjs +339 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/opencode.mjs +628 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/permission-bridge.mjs +534 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/proc.mjs +179 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/agent/version.mjs +148 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/approval-coordinator.mjs +150 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/decision-poller.mjs +235 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/dsh-bridge.mjs +258 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/push-reporter.mjs +533 -0
- package/packages/dsh-remote-web/runtime/clients/dsh-remote/wecom-bot.mjs +2444 -0
- package/packages/dsh-remote-web/runtime/dsh-setup.mjs +1 -1
- package/packages/dsh-remote-web/test/activation-entry.test.mjs +11 -1
- package/packages/dsh-remote-web/test/announcements-ui.test.mjs +8 -3
- package/packages/dsh-remote-web/test/connect-ui.test.mjs +16 -4
- package/packages/dsh-remote-web/test/im-bots.test.mjs +359 -0
- package/packages/dsh-remote-web/test/lib-panel-ia.mjs +98 -0
- package/packages/dsh-remote-web/test/login-selfheal-ui.test.mjs +13 -5
- package/packages/dsh-remote-web/test/panel-ia.test.mjs +798 -0
- package/packages/dsh-remote-web/test/password-reset.test.mjs +11 -1
- package/packages/dsh-remote-web/test/plugin-link.test.mjs +78 -0
- package/packages/dsh-remote-web/test/purchase-agreement-ui.test.mjs +10 -1
- package/packages/dsh-remote-web/test/remote-access-ui.test.mjs +20 -8
- package/packages/dsh-remote-web/test/settings-entry.test.mjs +30 -21
- package/packages/dsh-remote-web/test/sms-captcha-ui.test.mjs +15 -2
- package/packages/dsh-remote-web/test/telemetry.test.mjs +5 -3
- package/packages/dsh-remote-web/test/wechat-bot-ui.test.mjs +57 -130
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* OpenCode(HTTP/SSE)适配契约测试(M2 第 5 周)。
|
|
4
|
+
*
|
|
5
|
+
* ## 夹具来源(官方,逐字对照)
|
|
6
|
+
*
|
|
7
|
+
* | 夹具 | 出处 |
|
|
8
|
+
* |---|---|
|
|
9
|
+
* | `Event` 联合、`message.part.updated`、`permission.updated`、`session.status/idle/error`、`Part`/`ToolState`/`Permission` 字段 | npm 包 `@opencode-ai/sdk@1.19.5` 的 `dist/gen/types.gen.d.ts`(官方发布产物) |
|
|
10
|
+
* | `GET /event` 是 SSE、`POST /session/:id/permissions/:permissionID`、`prompt_async` | <https://opencode.ai/docs/server/> |
|
|
11
|
+
*
|
|
12
|
+
* > 本机**未安装** OpenCode ⇒ 末尾真实探测用例会 SKIP;映射是按官方类型定义写的,**没有真实流校准**。
|
|
13
|
+
*
|
|
14
|
+
* 用法: node --test clients/dsh-remote/test/agent-opencode.test.mjs
|
|
15
|
+
*/
|
|
16
|
+
import assert from "node:assert/strict";
|
|
17
|
+
import { EventEmitter } from "node:events";
|
|
18
|
+
import { spawnSync as nodeSpawnSync } from "node:child_process";
|
|
19
|
+
import test from "node:test";
|
|
20
|
+
|
|
21
|
+
import {
|
|
22
|
+
OPENCODE_EVENTS,
|
|
23
|
+
OPENCODE_IGNORED_EVENTS,
|
|
24
|
+
OPENCODE_BIN_ENV,
|
|
25
|
+
attachOpenCodeSse,
|
|
26
|
+
buildOpenCodeCandidates,
|
|
27
|
+
createOpenCodeDriver,
|
|
28
|
+
createSseParser,
|
|
29
|
+
detectOpenCode,
|
|
30
|
+
mapOpenCodeLine,
|
|
31
|
+
mapOpenCodeObject,
|
|
32
|
+
parseSseBlock,
|
|
33
|
+
unwrapOpenCodeEvent
|
|
34
|
+
} from "../agent/opencode.mjs";
|
|
35
|
+
|
|
36
|
+
const NOW = 1_700_000_000_000;
|
|
37
|
+
const at = () => NOW;
|
|
38
|
+
const evt = (type, properties) => ({ type, properties });
|
|
39
|
+
const line = (obj) => JSON.stringify(obj);
|
|
40
|
+
|
|
41
|
+
/** 官方 `Part`/`ToolState` 形状的夹具(字段名逐字对照 SDK types)。 */
|
|
42
|
+
const TEXT_PART = {
|
|
43
|
+
id: "prt_text_1", sessionID: "ses_1", messageID: "msg_1", type: "text", text: "正在分析代码"
|
|
44
|
+
};
|
|
45
|
+
const TOOL_RUNNING = {
|
|
46
|
+
id: "prt_tool_1", sessionID: "ses_1", messageID: "msg_1", type: "tool",
|
|
47
|
+
callID: "call_1", tool: "bash",
|
|
48
|
+
state: { status: "running", input: { command: "curl -H 'Authorization: Bearer sk-secret' https://x" }, time: { start: 1 } }
|
|
49
|
+
};
|
|
50
|
+
const TOOL_DONE = {
|
|
51
|
+
...TOOL_RUNNING,
|
|
52
|
+
state: { status: "completed", input: {}, output: "HTTP/2 200", title: "bash", metadata: {}, time: { start: 1, end: 2 } }
|
|
53
|
+
};
|
|
54
|
+
const TOOL_ERROR = {
|
|
55
|
+
...TOOL_RUNNING,
|
|
56
|
+
state: { status: "error", input: {}, error: "command failed", time: { start: 1, end: 2 } }
|
|
57
|
+
};
|
|
58
|
+
const PERMISSION = {
|
|
59
|
+
id: "perm_1", type: "bash", pattern: "rm -rf *", sessionID: "ses_1", messageID: "msg_1",
|
|
60
|
+
callID: "call_1", title: "Run rm -rf build", metadata: {}, time: { created: 1 }
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
test("事件词表:认识的事件与忽略清单覆盖官方 Event 联合的关键项", () => {
|
|
64
|
+
assert.ok(OPENCODE_EVENTS.includes("message.part.updated"));
|
|
65
|
+
assert.ok(OPENCODE_EVENTS.includes("permission.updated"));
|
|
66
|
+
assert.ok(OPENCODE_IGNORED_EVENTS.includes("message.updated"));
|
|
67
|
+
assert.ok(OPENCODE_IGNORED_EVENTS.includes("session.created"));
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("server.connected → status(connected)", () => {
|
|
71
|
+
const r = mapOpenCodeObject(evt("server.connected", {}), { now: at });
|
|
72
|
+
assert.equal(r.events[0].status, "connected");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("/global/event 的 {directory,payload} 包装能自动解包", () => {
|
|
76
|
+
const wrapped = { directory: "/repo", payload: evt("session.idle", { sessionID: "ses_1" }) };
|
|
77
|
+
assert.equal(unwrapOpenCodeEvent(wrapped).type, "session.idle");
|
|
78
|
+
const r = mapOpenCodeObject(wrapped, { now: at });
|
|
79
|
+
assert.equal(r.events[0].type, "turn.end");
|
|
80
|
+
assert.equal(r.events[0].reason, "completed");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("session.status:busy→working / idle→idle / retry→retrying(带 attempt 与 next)", () => {
|
|
84
|
+
assert.equal(mapOpenCodeObject(evt("session.status", { sessionID: "s", status: { type: "busy" } }), { now: at }).events[0].status, "working");
|
|
85
|
+
assert.equal(mapOpenCodeObject(evt("session.status", { sessionID: "s", status: { type: "idle" } }), { now: at }).events[0].status, "idle");
|
|
86
|
+
const retry = mapOpenCodeObject(evt("session.status", { sessionID: "s", status: { type: "retry", attempt: 2, next: 5000 } }), { now: at });
|
|
87
|
+
assert.equal(retry.events[0].status, "retrying");
|
|
88
|
+
assert.equal(retry.events[0].attempt, 2);
|
|
89
|
+
assert.equal(retry.events[0].retryDelayMs, 5000);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("session.idle → turn.end(completed)(文档语义:会话空闲=回合结束)", () => {
|
|
93
|
+
const r = mapOpenCodeObject(evt("session.idle", { sessionID: "ses_1" }), { now: at });
|
|
94
|
+
assert.equal(r.events[0].type, "turn.end");
|
|
95
|
+
assert.equal(r.events[0].reason, "completed");
|
|
96
|
+
assert.equal(r.events[0].isError, false);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("session.error → turn.end(error):带 error name 与 message 预览", () => {
|
|
100
|
+
const r = mapOpenCodeObject(evt("session.error", {
|
|
101
|
+
sessionID: "ses_1",
|
|
102
|
+
error: { name: "ApiError", data: { message: "rate limited" } }
|
|
103
|
+
}), { now: at });
|
|
104
|
+
assert.equal(r.events[0].type, "turn.end");
|
|
105
|
+
assert.equal(r.events[0].reason, "error");
|
|
106
|
+
assert.equal(r.events[0].isError, true);
|
|
107
|
+
assert.equal(r.events[0].errorName, "ApiError");
|
|
108
|
+
assert.match(r.events[0].errorMessage, /rate limited/);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("message.part.updated(text):有 delta 用增量(final=false),无 delta 用全文(final=true)", () => {
|
|
112
|
+
const delta = mapOpenCodeObject(evt("message.part.updated", { part: TEXT_PART, delta: "正在" }), { now: at });
|
|
113
|
+
assert.equal(delta.events[0].type, "message.delta");
|
|
114
|
+
assert.equal(delta.events[0].final, false);
|
|
115
|
+
assert.equal(delta.events[0].text, "正在");
|
|
116
|
+
|
|
117
|
+
const full = mapOpenCodeObject(evt("message.part.updated", { part: TEXT_PART }), { now: at });
|
|
118
|
+
assert.equal(full.events[0].final, true);
|
|
119
|
+
assert.equal(full.events[0].text, "正在分析代码");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("message.part.updated(reasoning) → status(thinking) 只带字节数", () => {
|
|
123
|
+
const part = { id: "prt_r", sessionID: "s", messageID: "m", type: "reasoning", text: "I should rm -rf", time: { start: 1 } };
|
|
124
|
+
const r = mapOpenCodeObject(evt("message.part.updated", { part }), { now: at });
|
|
125
|
+
assert.equal(r.events[0].status, "thinking");
|
|
126
|
+
assert.ok(r.events[0].textBytes > 0);
|
|
127
|
+
assert.equal(JSON.stringify(r.events).includes("rm -rf"), false);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("tool part(running) → tool.call:工具名映射 kind、只带输入字节数、不落命令原文", () => {
|
|
131
|
+
const r = mapOpenCodeObject(evt("message.part.updated", { part: TOOL_RUNNING }), { now: at });
|
|
132
|
+
const ev = r.events[0];
|
|
133
|
+
assert.equal(ev.type, "tool.call");
|
|
134
|
+
assert.equal(ev.toolCallId, "call_1");
|
|
135
|
+
assert.equal(ev.name, "bash");
|
|
136
|
+
assert.equal(ev.kind, "execute");
|
|
137
|
+
assert.ok(ev.inputBytes > 0);
|
|
138
|
+
assert.equal(JSON.stringify(ev).includes("sk-secret"), false, "命令原文不落");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("tool part(completed/error) → tool.result:isError 与截断预览", () => {
|
|
142
|
+
const done = mapOpenCodeObject(evt("message.part.updated", { part: TOOL_DONE }), { now: at });
|
|
143
|
+
assert.equal(done.events[0].type, "tool.result");
|
|
144
|
+
assert.equal(done.events[0].isError, false);
|
|
145
|
+
assert.match(done.events[0].outputPreview, /HTTP\/2 200/);
|
|
146
|
+
|
|
147
|
+
const bad = mapOpenCodeObject(evt("message.part.updated", { part: TOOL_ERROR }), { now: at });
|
|
148
|
+
assert.equal(bad.events[0].isError, true);
|
|
149
|
+
assert.match(bad.events[0].outputPreview, /command failed/);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test("permission.updated → status(waiting_approval):不落 title/pattern(可能含命令)", () => {
|
|
153
|
+
const r = mapOpenCodeObject(evt("permission.updated", PERMISSION), { now: at });
|
|
154
|
+
const ev = r.events[0];
|
|
155
|
+
assert.equal(ev.status, "waiting_approval");
|
|
156
|
+
assert.equal(ev.requestId, "perm_1");
|
|
157
|
+
assert.equal(ev.permType, "bash");
|
|
158
|
+
assert.equal(ev.toolCallId, "call_1");
|
|
159
|
+
const dump = JSON.stringify(ev);
|
|
160
|
+
assert.equal(dump.includes("rm -rf"), false, "pattern 不落");
|
|
161
|
+
assert.equal(dump.includes("Run rm -rf build"), false, "title 不落");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("permission.replied → status(approval_replied)", () => {
|
|
165
|
+
const r = mapOpenCodeObject(evt("permission.replied", { sessionID: "s", permissionID: "perm_1", response: "once" }), { now: at });
|
|
166
|
+
assert.equal(r.events[0].status, "approval_replied");
|
|
167
|
+
assert.equal(r.events[0].requestId, "perm_1");
|
|
168
|
+
assert.equal(r.events[0].response, "once");
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test("忽略清单里的事件 → 0 事件;未知类型 → ok:false(计 unknown)", () => {
|
|
172
|
+
for (const t of ["message.updated", "session.created", "file.edited", "todo.updated", "pty.exited"]) {
|
|
173
|
+
assert.deepEqual(mapOpenCodeObject(evt(t, {}), { now: at }), { ok: true, events: [] }, `${t} 应忽略`);
|
|
174
|
+
}
|
|
175
|
+
assert.equal(mapOpenCodeObject(evt("brand.new.event", {}), { now: at }).reason, "unknown-type:brand.new.event");
|
|
176
|
+
assert.equal(mapOpenCodeLine("not json", { now: at }).reason, "bad-json");
|
|
177
|
+
assert.equal(mapOpenCodeObject({}, { now: at }).reason, "not-event");
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("驱动:一段真实形状的 SSE 事件序列 → unknown=0、5 类事件齐", () => {
|
|
181
|
+
const d = createOpenCodeDriver({ now: at });
|
|
182
|
+
const lines = [
|
|
183
|
+
line(evt("server.connected", {})),
|
|
184
|
+
line(evt("message.part.updated", { part: TEXT_PART, delta: "正在" })),
|
|
185
|
+
line(evt("message.part.updated", { part: TOOL_RUNNING })),
|
|
186
|
+
line(evt("message.part.updated", { part: TOOL_DONE })),
|
|
187
|
+
line(evt("permission.updated", PERMISSION)),
|
|
188
|
+
line(evt("session.idle", { sessionID: "ses_1" }))
|
|
189
|
+
];
|
|
190
|
+
const events = [];
|
|
191
|
+
for (const l of lines) events.push(...d.writeLine(l));
|
|
192
|
+
const s = d.stats();
|
|
193
|
+
assert.equal(s.driverName, "opencode-sse");
|
|
194
|
+
assert.equal(s.unknown, 0);
|
|
195
|
+
assert.deepEqual(events.map((e) => e.type), ["status", "message.delta", "tool.call", "tool.result", "status", "turn.end"]);
|
|
196
|
+
assert.equal(s.byType["tool.call"], 1);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// ── SSE 桥接(本模块的核心差异点) ─────────────────────────────────
|
|
200
|
+
|
|
201
|
+
test("SSE 解析:一个 block、多个 block、跨 chunk 半截、CRLF、注释心跳", () => {
|
|
202
|
+
const p = createSseParser();
|
|
203
|
+
assert.equal(p.push("data: {\"type\":\"server.connected\",\"properties\":{}}\n\n").length, 1);
|
|
204
|
+
const two = p.push("data: {\"a\":1}\n\ndata: {\"b\":2}\n\n");
|
|
205
|
+
assert.deepEqual(two.map((m) => m.data), [{ a: 1 }, { b: 2 }]);
|
|
206
|
+
|
|
207
|
+
// 跨 chunk:先来半截
|
|
208
|
+
assert.deepEqual(p.push("data: {\"type\":\"sess"), []);
|
|
209
|
+
const rest = p.push("ion.idle\",\"properties\":{}}\n\n");
|
|
210
|
+
assert.equal(rest.length, 1);
|
|
211
|
+
assert.equal(rest[0].data.type, "session.idle");
|
|
212
|
+
|
|
213
|
+
// CRLF + 注释行(心跳)
|
|
214
|
+
const crlf = p.push(": keepalive\r\ndata: {\"x\":1}\r\n\r\n");
|
|
215
|
+
assert.deepEqual(crlf.map((m) => m.data), [{ x: 1 }]);
|
|
216
|
+
assert.equal(p.buffered(), "");
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test("SSE 解析:多行 data 用换行拼接;坏 JSON 返回 data:null 且不抛", () => {
|
|
220
|
+
const multi = parseSseBlock('event: msg\ndata: {"a":\ndata: 1}');
|
|
221
|
+
assert.deepEqual(multi.data, { a: 1 });
|
|
222
|
+
const bad = parseSseBlock("data: {not json");
|
|
223
|
+
assert.equal(bad.data, null);
|
|
224
|
+
assert.match(bad.raw, /not json/);
|
|
225
|
+
assert.equal(parseSseBlock(": only heartbeat"), null);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test("SSE 解析:flush 处理没有结尾空行的最后一块", () => {
|
|
229
|
+
const p = createSseParser();
|
|
230
|
+
p.push('data: {"last":true}');
|
|
231
|
+
assert.deepEqual(p.push(""), []);
|
|
232
|
+
const out = p.flush();
|
|
233
|
+
assert.deepEqual(out.map((m) => m.data), [{ last: true }]);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test("attachOpenCodeSse:把 SSE 流接到驱动上(含坏块计数、detach、end flush)", () => {
|
|
237
|
+
const stream = new EventEmitter();
|
|
238
|
+
const d = createOpenCodeDriver({ now: at });
|
|
239
|
+
const logs = [];
|
|
240
|
+
const sse = attachOpenCodeSse({ stream, driver: d, log: (e) => logs.push(e) });
|
|
241
|
+
|
|
242
|
+
stream.emit("data", 'data: {"type":"server.connected","properties":{}}\n\n');
|
|
243
|
+
stream.emit("data", 'data: {"type":"session.idle","properties":{"sessionID":"s"}}\n\n');
|
|
244
|
+
stream.emit("data", ": heartbeat\n\n"); // 心跳不产生消息
|
|
245
|
+
stream.emit("data", "data: {bad json\n\n"); // 坏块
|
|
246
|
+
stream.emit("end");
|
|
247
|
+
|
|
248
|
+
const s = d.stats();
|
|
249
|
+
assert.equal(s.events, 2, "两条有效事件");
|
|
250
|
+
assert.equal(s.unknown, 0, "坏 SSE 块在桥接层就被拦下,不进驱动 unknown");
|
|
251
|
+
assert.deepEqual(sse.stats(), { chunks: 4, badBlocks: 1 });
|
|
252
|
+
assert.equal(logs[0].event, "sse.bad-json");
|
|
253
|
+
|
|
254
|
+
// detach 后不再喂
|
|
255
|
+
sse.detach();
|
|
256
|
+
stream.emit("data", 'data: {"type":"server.connected","properties":{}}\n\n');
|
|
257
|
+
assert.equal(d.stats().events, 2);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// ── CLI 探测(假实现) ────────────────────────────────────────────
|
|
261
|
+
|
|
262
|
+
const SERVE_HELP = "Usage: opencode serve [options]\n --port <number> Port to listen on (default: 4096)\n --hostname <string>\n --cors <origin>\n";
|
|
263
|
+
|
|
264
|
+
function makeFs(overrides = {}) {
|
|
265
|
+
const enoent = () => {
|
|
266
|
+
throw Object.assign(new Error("ENOENT"), { code: "ENOENT" });
|
|
267
|
+
};
|
|
268
|
+
return {
|
|
269
|
+
existsSync: () => false,
|
|
270
|
+
statSync: () => enoent(),
|
|
271
|
+
realpathSync: (p) => p,
|
|
272
|
+
readdirSync: () => {
|
|
273
|
+
throw Object.assign(new Error("ENOENT"), { code: "ENOENT" });
|
|
274
|
+
},
|
|
275
|
+
...overrides
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function fakeSpawn({ version = "1.19.5", help = SERVE_HELP } = {}) {
|
|
280
|
+
return (command, args) => {
|
|
281
|
+
if (args.includes("--version")) return { status: 0, stdout: `${version}\n`, stderr: "" };
|
|
282
|
+
if (args.includes("serve") && args.includes("--help")) return { status: 0, stdout: help, stderr: "" };
|
|
283
|
+
return { status: 0, stdout: "", stderr: "" };
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
test("候选:显式 env / PATH 解软链 / npm 包 bin 目录扫描(.js 走 node)", () => {
|
|
288
|
+
const c = buildOpenCodeCandidates({
|
|
289
|
+
env: { [OPENCODE_BIN_ENV]: "/custom/opencode", PATH: "/usr/bin" },
|
|
290
|
+
platform: "linux",
|
|
291
|
+
execPath: "/usr/bin/node",
|
|
292
|
+
fs: makeFs({
|
|
293
|
+
statSync: () => ({ isFile: () => true, mode: 0o755 }),
|
|
294
|
+
readdirSync: () => ["opencode", "opencode.js"]
|
|
295
|
+
}),
|
|
296
|
+
npmRoot: "/usr/lib/node_modules",
|
|
297
|
+
isExecutable: (p) => p === "/usr/bin/opencode" || p.endsWith("/opencode"),
|
|
298
|
+
realpath: (p) => (p === "/usr/bin/opencode" ? "/real/opencode" : p)
|
|
299
|
+
});
|
|
300
|
+
const kinds = c.map((x) => x.kind);
|
|
301
|
+
// npm 目录扫描顺序取决于 readdir 顺序:原生二进制与 .js 入口都可能先出,断言集合即可
|
|
302
|
+
assert.deepEqual([...kinds].sort(), ["explicit", "npm-native", "npm-node", "path"]);
|
|
303
|
+
assert.equal(c[1].command, "/real/opencode");
|
|
304
|
+
const nodeEntry = c.find((x) => x.kind === "npm-node");
|
|
305
|
+
assert.ok(nodeEntry.argsPrefix[0].endsWith("opencode.js"));
|
|
306
|
+
// npm 包不存在时 readdir 抛错也不崩
|
|
307
|
+
const empty = buildOpenCodeCandidates({ env: {}, fs: makeFs(), npmRoot: "/none", isExecutable: () => false });
|
|
308
|
+
assert.deepEqual(empty, []);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
test("探测:server API 可用 → ok + mode=approval + approvalChannel=http", () => {
|
|
312
|
+
const r = detectOpenCode({
|
|
313
|
+
env: { [OPENCODE_BIN_ENV]: "/fake/opencode", PATH: "" },
|
|
314
|
+
platform: "linux",
|
|
315
|
+
fs: makeFs(),
|
|
316
|
+
spawnSync: fakeSpawn(),
|
|
317
|
+
npmRoot: ""
|
|
318
|
+
});
|
|
319
|
+
assert.equal(r.ok, true);
|
|
320
|
+
assert.equal(r.version.text, "1.19.5");
|
|
321
|
+
assert.equal(r.caps.serverApi, true);
|
|
322
|
+
assert.equal(r.caps.sse, true);
|
|
323
|
+
assert.equal(r.caps.permissionReply, true);
|
|
324
|
+
assert.equal(r.mode, "approval");
|
|
325
|
+
assert.equal(r.approvalChannel, "http");
|
|
326
|
+
assert.equal(r.capsEvidence.matched.port, true);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test("探测:CLI 缺失 → cli-missing;版本认不出 → version-unknown;serve --help 失败 → warning", () => {
|
|
330
|
+
const missing = detectOpenCode({ env: { PATH: "" }, platform: "linux", fs: makeFs(), spawnSync: fakeSpawn(), npmRoot: "" });
|
|
331
|
+
assert.equal(missing.status, "cli-missing");
|
|
332
|
+
|
|
333
|
+
const unknown = detectOpenCode({
|
|
334
|
+
env: { [OPENCODE_BIN_ENV]: "/fake/opencode" },
|
|
335
|
+
platform: "linux",
|
|
336
|
+
fs: makeFs(),
|
|
337
|
+
spawnSync: () => ({ status: 0, stdout: "weird", stderr: "" }),
|
|
338
|
+
npmRoot: ""
|
|
339
|
+
});
|
|
340
|
+
assert.equal(unknown.status, "version-unknown");
|
|
341
|
+
|
|
342
|
+
const capsFail = detectOpenCode({
|
|
343
|
+
env: { [OPENCODE_BIN_ENV]: "/fake/opencode" },
|
|
344
|
+
platform: "linux",
|
|
345
|
+
fs: makeFs(),
|
|
346
|
+
npmRoot: "",
|
|
347
|
+
spawnSync: (command, args) => {
|
|
348
|
+
if (args.includes("--version")) return { status: 0, stdout: "1.19.5", stderr: "" };
|
|
349
|
+
return { status: 2, stdout: "", stderr: "boom" };
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
assert.equal(capsFail.ok, true);
|
|
353
|
+
assert.equal(capsFail.caps.serverApi, false);
|
|
354
|
+
assert.ok(capsFail.warnings.some((w) => w.code === "caps-probe-failed"));
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
test("本机 opencode 真实探测(未安装则 SKIP)", { skip: (() => {
|
|
358
|
+
try {
|
|
359
|
+
const r = nodeSpawnSync("opencode", ["--version"], { encoding: "utf8", timeout: 8000, windowsHide: true, stdio: ["ignore", "pipe", "pipe"] });
|
|
360
|
+
return r.status === 0 ? false : "本机未安装 opencode CLI";
|
|
361
|
+
} catch {
|
|
362
|
+
return "本机未安装 opencode CLI";
|
|
363
|
+
}
|
|
364
|
+
})() }, () => {
|
|
365
|
+
const r = detectOpenCode({ env: { ...process.env, [OPENCODE_BIN_ENV]: "" }, platform: process.platform, npmRoot: "" });
|
|
366
|
+
assert.equal(r.ok, true);
|
|
367
|
+
assert.match(r.version.text, /^\d+\.\d+\.\d+$/);
|
|
368
|
+
assert.equal(r.caps.serverApi, true);
|
|
369
|
+
});
|