@mrrisega/dsh-remote 0.7.10-beta.3 → 0.7.12-beta.1
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 +220 -1
- package/clients/dsh-remote/im-format.mjs +372 -0
- 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/im-format.test.mjs +229 -0
- package/clients/dsh-remote/test/upstream-discovery.test.mjs +203 -2
- package/clients/dsh-remote/test/wecom-bot.test.mjs +1718 -0
- package/clients/dsh-remote/upstream-discovery.mjs +495 -43
- package/clients/dsh-remote/wecom-bot.mjs +2670 -0
- package/dsh-setup.mjs +95 -9
- package/package.json +2 -2
- package/packages/dsh-remote-web/lib/client.js +1377 -292
- package/packages/dsh-remote-web/lib/index.js +419 -12
- 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 +18 -3
- 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 +400 -0
- package/packages/dsh-remote-web/test/lib-panel-ia.mjs +98 -0
- package/packages/dsh-remote-web/test/login-fast.test.mjs +104 -0
- package/packages/dsh-remote-web/test/login-selfheal-ui.test.mjs +17 -6
- package/packages/dsh-remote-web/test/panel-ia.test.mjs +996 -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 +14 -5
- package/packages/dsh-remote-web/test/remote-access-ui.test.mjs +69 -119
- package/packages/dsh-remote-web/test/self-manage.test.mjs +5 -1
- package/packages/dsh-remote-web/test/settings-entry.test.mjs +42 -25
- package/packages/dsh-remote-web/test/sms-captcha-ui.test.mjs +15 -2
- package/packages/dsh-remote-web/test/telemetry.test.mjs +6 -4
- package/packages/dsh-remote-web/test/wechat-bot-ui.test.mjs +57 -130
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
|
|
4
|
+
import { createDecisionPoller, toDshOutcome, MAX_ITEMS_PER_TICK } from "../decision-poller.mjs";
|
|
5
|
+
|
|
6
|
+
/** 按脚本返回的假 fetch;记录每次调用。 */
|
|
7
|
+
function makeFetch(script) {
|
|
8
|
+
const calls = [];
|
|
9
|
+
const fn = async (url, init) => {
|
|
10
|
+
calls.push({ url, init });
|
|
11
|
+
const step = script.shift();
|
|
12
|
+
if (!step) throw new Error("fetch script exhausted: " + url);
|
|
13
|
+
if (step instanceof Error) throw step;
|
|
14
|
+
return {
|
|
15
|
+
status: step.status ?? 200,
|
|
16
|
+
async json() { return step.body ?? { ok: true }; }
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
fn.calls = calls;
|
|
20
|
+
return fn;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function mk(opts) {
|
|
24
|
+
return createDecisionPoller({
|
|
25
|
+
apiBase: "https://api.example.com",
|
|
26
|
+
deviceId: "dev-x",
|
|
27
|
+
tokenProvider: () => "tok",
|
|
28
|
+
apply: async () => ({ ok: true }),
|
|
29
|
+
fetchImpl: makeFetch([]),
|
|
30
|
+
...opts
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
test("① 手机 decision → DSH outcome:allow 只能是 allowed-once,未知一律拒绝", () => {
|
|
35
|
+
assert.equal(toDshOutcome("allow"), "allowed-once");
|
|
36
|
+
assert.equal(toDshOutcome("deny"), "rejected");
|
|
37
|
+
assert.equal(toDshOutcome("weird"), "rejected");
|
|
38
|
+
assert.equal(toDshOutcome(""), "rejected");
|
|
39
|
+
assert.equal(toDshOutcome(undefined), "rejected");
|
|
40
|
+
// 绝不把 allow 映射成永久放行
|
|
41
|
+
assert.notEqual(toDshOutcome("allow"), "allowed-always");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("② start 门禁:缺 apiBase / deviceId / apply 都不启动", () => {
|
|
45
|
+
assert.equal(mk({ apiBase: "" }).start().lastError, "no_api_base");
|
|
46
|
+
assert.equal(mk({ deviceId: "" }).start().lastError, "no_device");
|
|
47
|
+
assert.equal(mk({ apply: null }).start().lastError, "no_apply");
|
|
48
|
+
assert.equal(mk().start().started, true);
|
|
49
|
+
mk().stop();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("③ happy path:拉取 → 应用 → ack,参数正确", async () => {
|
|
53
|
+
const applied = [];
|
|
54
|
+
const fetchImpl = makeFetch([
|
|
55
|
+
{ status: 200, body: { ok: true, items: [
|
|
56
|
+
{ id: "d1", event_id: "dev-x:approval:evt_1", request_id: "evt_1", decision: "allow", decided_at: 1, expires_at: Date.now() + 60000 }
|
|
57
|
+
] } },
|
|
58
|
+
{ status: 200, body: { ok: true, acked: 1 } }
|
|
59
|
+
]);
|
|
60
|
+
const p = createDecisionPoller({
|
|
61
|
+
apiBase: "https://api.example.com",
|
|
62
|
+
deviceId: "dev-x",
|
|
63
|
+
tokenProvider: () => "tok",
|
|
64
|
+
apply: async (item) => { applied.push(item); },
|
|
65
|
+
fetchImpl,
|
|
66
|
+
intervalMs: 60_000
|
|
67
|
+
});
|
|
68
|
+
await p._tick();
|
|
69
|
+
assert.equal(applied.length, 1);
|
|
70
|
+
assert.equal(applied[0].request_id, "evt_1");
|
|
71
|
+
assert.equal(applied[0].decision, "allow");
|
|
72
|
+
assert.equal(applied[0].outcome, "allowed-once");
|
|
73
|
+
// 第一个请求是 pending(带 device_id),第二个是 ack
|
|
74
|
+
assert.match(fetchImpl.calls[0].url, /\/api\/notify\/pending\?device_id=dev-x/);
|
|
75
|
+
assert.equal(fetchImpl.calls[0].init.headers.authorization, "Bearer tok");
|
|
76
|
+
assert.match(fetchImpl.calls[1].url, /\/api\/notify\/ack$/);
|
|
77
|
+
assert.deepEqual(JSON.parse(fetchImpl.calls[1].init.body).ids, ["d1"]);
|
|
78
|
+
assert.equal(p.status().applied, 1);
|
|
79
|
+
assert.equal(p.status().acked, 1);
|
|
80
|
+
p.stop();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("④ apply 抛错也必须 ack(否则每 3s 重放同一条)", async () => {
|
|
84
|
+
const fetchImpl = makeFetch([
|
|
85
|
+
{ status: 200, body: { ok: true, items: [
|
|
86
|
+
{ id: "d1", event_id: "e1", request_id: "evt_1", decision: "deny", expires_at: Date.now() + 60000 }
|
|
87
|
+
] } },
|
|
88
|
+
{ status: 200, body: { ok: true } }
|
|
89
|
+
]);
|
|
90
|
+
const p = createDecisionPoller({
|
|
91
|
+
apiBase: "https://api.example.com",
|
|
92
|
+
deviceId: "dev-x",
|
|
93
|
+
tokenProvider: () => "tok",
|
|
94
|
+
apply: async () => { throw new Error("session gone"); },
|
|
95
|
+
fetchImpl
|
|
96
|
+
});
|
|
97
|
+
await p._tick();
|
|
98
|
+
assert.equal(p.status().applied, 0);
|
|
99
|
+
assert.equal(p.status().failed, 1);
|
|
100
|
+
assert.match(fetchImpl.calls[1].url, /\/api\/notify\/ack$/);
|
|
101
|
+
assert.deepEqual(JSON.parse(fetchImpl.calls[1].init.body).ids, ["d1"]);
|
|
102
|
+
p.stop();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("⑤ 已过期条目:不应用,但要 ack 掉避免反复拉", async () => {
|
|
106
|
+
const applied = [];
|
|
107
|
+
const fetchImpl = makeFetch([
|
|
108
|
+
{ status: 200, body: { ok: true, items: [
|
|
109
|
+
{ id: "old", event_id: "e", request_id: "evt_old", decision: "allow", expires_at: Date.now() - 1000 }
|
|
110
|
+
] } },
|
|
111
|
+
{ status: 200, body: { ok: true } }
|
|
112
|
+
]);
|
|
113
|
+
const p = createDecisionPoller({
|
|
114
|
+
apiBase: "https://api.example.com",
|
|
115
|
+
deviceId: "dev-x",
|
|
116
|
+
tokenProvider: () => "tok",
|
|
117
|
+
apply: async (i) => { applied.push(i); },
|
|
118
|
+
fetchImpl
|
|
119
|
+
});
|
|
120
|
+
await p._tick();
|
|
121
|
+
assert.equal(applied.length, 0, "过期决策绝不能应用");
|
|
122
|
+
assert.deepEqual(JSON.parse(fetchImpl.calls[1].init.body).ids, ["old"]);
|
|
123
|
+
p.stop();
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("⑥ 401:不应用、不 ack、不抛", async () => {
|
|
127
|
+
const applied = [];
|
|
128
|
+
const fetchImpl = makeFetch([{ status: 401 }]);
|
|
129
|
+
const p = createDecisionPoller({
|
|
130
|
+
apiBase: "https://api.example.com",
|
|
131
|
+
deviceId: "dev-x",
|
|
132
|
+
tokenProvider: () => "expired",
|
|
133
|
+
apply: async (i) => { applied.push(i); },
|
|
134
|
+
fetchImpl
|
|
135
|
+
});
|
|
136
|
+
await p._tick();
|
|
137
|
+
assert.equal(applied.length, 0);
|
|
138
|
+
assert.equal(p.status().lastError, "auth_failed");
|
|
139
|
+
assert.equal(fetchImpl.calls.length, 1, "401 后不应再发 ack");
|
|
140
|
+
p.stop();
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("⑦ 没有 token:一个请求都不发", async () => {
|
|
144
|
+
const fetchImpl = makeFetch([]);
|
|
145
|
+
const p = createDecisionPoller({
|
|
146
|
+
apiBase: "https://api.example.com",
|
|
147
|
+
deviceId: "dev-x",
|
|
148
|
+
tokenProvider: () => "",
|
|
149
|
+
apply: async () => {},
|
|
150
|
+
fetchImpl
|
|
151
|
+
});
|
|
152
|
+
await p._tick();
|
|
153
|
+
assert.equal(fetchImpl.calls.length, 0);
|
|
154
|
+
assert.equal(p.status().lastError, "no_token");
|
|
155
|
+
p.stop();
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("⑧ ack 失败不影响主流程(幂等,下轮重复拉也没副作用)", async () => {
|
|
159
|
+
const applied = [];
|
|
160
|
+
const fetchImpl = makeFetch([
|
|
161
|
+
{ status: 200, body: { ok: true, items: [
|
|
162
|
+
{ id: "d1", event_id: "e", request_id: "evt_1", decision: "allow", expires_at: Date.now() + 60000 }
|
|
163
|
+
] } },
|
|
164
|
+
{ status: 500, body: { ok: false } }
|
|
165
|
+
]);
|
|
166
|
+
const p = createDecisionPoller({
|
|
167
|
+
apiBase: "https://api.example.com",
|
|
168
|
+
deviceId: "dev-x",
|
|
169
|
+
tokenProvider: () => "tok",
|
|
170
|
+
apply: async (i) => { applied.push(i); },
|
|
171
|
+
fetchImpl
|
|
172
|
+
});
|
|
173
|
+
await p._tick();
|
|
174
|
+
assert.equal(applied.length, 1);
|
|
175
|
+
assert.equal(p.status().acked, 0);
|
|
176
|
+
p.stop();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("⑨ 单次最多处理 20 条(有界,避免并发打爆 dsh)", async () => {
|
|
180
|
+
const items = Array.from({ length: 35 }, (_, i) => ({
|
|
181
|
+
id: "d" + i, event_id: "e" + i, request_id: "evt_" + i, decision: "deny", expires_at: Date.now() + 60000
|
|
182
|
+
}));
|
|
183
|
+
const applied = [];
|
|
184
|
+
const fetchImpl = makeFetch([
|
|
185
|
+
{ status: 200, body: { ok: true, items } },
|
|
186
|
+
{ status: 200, body: { ok: true } }
|
|
187
|
+
]);
|
|
188
|
+
const p = createDecisionPoller({
|
|
189
|
+
apiBase: "https://api.example.com",
|
|
190
|
+
deviceId: "dev-x",
|
|
191
|
+
tokenProvider: () => "tok",
|
|
192
|
+
apply: async (i) => { applied.push(i); },
|
|
193
|
+
fetchImpl
|
|
194
|
+
});
|
|
195
|
+
await p._tick();
|
|
196
|
+
assert.equal(applied.length, MAX_ITEMS_PER_TICK);
|
|
197
|
+
assert.equal(JSON.parse(fetchImpl.calls[1].init.body).ids.length, MAX_ITEMS_PER_TICK);
|
|
198
|
+
p.stop();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("⑩ 未知 decision 值按 deny 处理(fail-closed)", async () => {
|
|
202
|
+
const applied = [];
|
|
203
|
+
const fetchImpl = makeFetch([
|
|
204
|
+
{ status: 200, body: { ok: true, items: [
|
|
205
|
+
{ id: "d1", event_id: "e", request_id: "evt_1", decision: "maybe", expires_at: Date.now() + 60000 }
|
|
206
|
+
] } },
|
|
207
|
+
{ status: 200, body: { ok: true } }
|
|
208
|
+
]);
|
|
209
|
+
const p = createDecisionPoller({
|
|
210
|
+
apiBase: "https://api.example.com",
|
|
211
|
+
deviceId: "dev-x",
|
|
212
|
+
tokenProvider: () => "tok",
|
|
213
|
+
apply: async (i) => { applied.push(i); },
|
|
214
|
+
fetchImpl
|
|
215
|
+
});
|
|
216
|
+
await p._tick();
|
|
217
|
+
assert.equal(applied[0].decision, "deny");
|
|
218
|
+
assert.equal(applied[0].outcome, "rejected");
|
|
219
|
+
p.stop();
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test("⑪ 网络异常吞掉,不抛、不崩", async () => {
|
|
223
|
+
const fetchImpl = makeFetch([new Error("ECONNRESET")]);
|
|
224
|
+
const p = createDecisionPoller({
|
|
225
|
+
apiBase: "https://api.example.com",
|
|
226
|
+
deviceId: "dev-x",
|
|
227
|
+
tokenProvider: () => "tok",
|
|
228
|
+
apply: async () => {},
|
|
229
|
+
fetchImpl
|
|
230
|
+
});
|
|
231
|
+
await p._tick();
|
|
232
|
+
assert.match(p.status().lastError, /ECONNRESET/);
|
|
233
|
+
p.stop();
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test("⑫ stop() 后不再轮询", async () => {
|
|
237
|
+
const fetchImpl = makeFetch([]);
|
|
238
|
+
const p = createDecisionPoller({
|
|
239
|
+
apiBase: "https://api.example.com",
|
|
240
|
+
deviceId: "dev-x",
|
|
241
|
+
tokenProvider: () => "tok",
|
|
242
|
+
apply: async () => {},
|
|
243
|
+
fetchImpl,
|
|
244
|
+
intervalMs: 10
|
|
245
|
+
});
|
|
246
|
+
p.start();
|
|
247
|
+
p.stop();
|
|
248
|
+
await new Promise((r) => setTimeout(r, 40));
|
|
249
|
+
assert.equal(fetchImpl.calls.length, 0, "stop 后不应再发任何请求");
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
test("⑬ 403 device_mismatch / 400 invalid_device_id:停止轮询(重试永远不会成功)", async () => {
|
|
253
|
+
const f1 = makeFetch([{ status: 403 }]);
|
|
254
|
+
const p1 = createDecisionPoller({
|
|
255
|
+
apiBase: "https://api.example.com", deviceId: "dev-x", tokenProvider: () => "tok",
|
|
256
|
+
apply: async () => {}, fetchImpl: f1, intervalMs: 10
|
|
257
|
+
});
|
|
258
|
+
p1.start();
|
|
259
|
+
await p1._tick();
|
|
260
|
+
assert.equal(p1.status().lastError, "device_mismatch");
|
|
261
|
+
const before = f1.calls.length;
|
|
262
|
+
await new Promise((r) => setTimeout(r, 40));
|
|
263
|
+
assert.equal(f1.calls.length, before, "403 之后不应再轮询");
|
|
264
|
+
p1.stop();
|
|
265
|
+
|
|
266
|
+
const f2 = makeFetch([{ status: 400 }]);
|
|
267
|
+
const p2 = createDecisionPoller({
|
|
268
|
+
apiBase: "https://api.example.com", deviceId: "dev-x", tokenProvider: () => "tok",
|
|
269
|
+
apply: async () => {}, fetchImpl: f2, intervalMs: 10
|
|
270
|
+
});
|
|
271
|
+
await p2._tick();
|
|
272
|
+
assert.equal(p2.status().lastError, "invalid_device_id");
|
|
273
|
+
p2.stop();
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test("⑭ 401 之后仍会继续轮询(token 刷新后要能恢复)", async () => {
|
|
277
|
+
const f = makeFetch([{ status: 401 }]);
|
|
278
|
+
const p = createDecisionPoller({
|
|
279
|
+
apiBase: "https://api.example.com", deviceId: "dev-x", tokenProvider: () => "expired",
|
|
280
|
+
apply: async () => {}, fetchImpl: f, intervalMs: 10
|
|
281
|
+
});
|
|
282
|
+
p.start();
|
|
283
|
+
await p._tick();
|
|
284
|
+
assert.equal(p.status().lastError, "auth_failed");
|
|
285
|
+
await new Promise((r) => setTimeout(r, 30));
|
|
286
|
+
assert.ok(f.calls.length >= 2, "401 不应停止轮询(等 token 刷新)");
|
|
287
|
+
p.stop();
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
test("⑮ 至少一次语义:同一条 id 被重投时只 apply 一次、但仍 ack(幂等去重)", async () => {
|
|
291
|
+
const applied = [];
|
|
292
|
+
const item = { id: "dec_same", event_id: "e", request_id: "evt_same", decision: "allow", expires_at: Date.now() + 60000, redeliver_count: 0 };
|
|
293
|
+
const fetchImpl = makeFetch([
|
|
294
|
+
{ status: 200, body: { ok: true, items: [item] } },
|
|
295
|
+
{ status: 200, body: { ok: true } },
|
|
296
|
+
// 服务端重投同一条(redeliver_count=1)
|
|
297
|
+
{ status: 200, body: { ok: true, items: [{ ...item, redeliver_count: 1 }] } },
|
|
298
|
+
{ status: 200, body: { ok: true } }
|
|
299
|
+
]);
|
|
300
|
+
const p = createDecisionPoller({
|
|
301
|
+
apiBase: "https://api.example.com", deviceId: "dev-x", tokenProvider: () => "tok",
|
|
302
|
+
apply: async (i) => { applied.push(i); }, fetchImpl
|
|
303
|
+
});
|
|
304
|
+
await p._tick();
|
|
305
|
+
await p._tick();
|
|
306
|
+
assert.equal(applied.length, 1, "重投不应重复 apply");
|
|
307
|
+
assert.equal(p.status().applied, 1);
|
|
308
|
+
assert.equal(p.status().skipped, 1);
|
|
309
|
+
assert.equal(p.status().acked, 2, "两次都应 ack(否则服务端会一直重投)");
|
|
310
|
+
p.stop();
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
test("⑯ 重投回执会写 warn 日志留痕(便于排查重复审批)", async () => {
|
|
314
|
+
const logs = [];
|
|
315
|
+
const item = { id: "dec_r", event_id: "e", request_id: "evt_r", decision: "deny", expires_at: Date.now() + 60000, redeliver_count: 2 };
|
|
316
|
+
const fetchImpl = makeFetch([
|
|
317
|
+
{ status: 200, body: { ok: true, items: [item] } },
|
|
318
|
+
{ status: 200, body: { ok: true } }
|
|
319
|
+
]);
|
|
320
|
+
const p = createDecisionPoller({
|
|
321
|
+
apiBase: "https://api.example.com", deviceId: "dev-x", tokenProvider: () => "tok",
|
|
322
|
+
apply: async () => {}, fetchImpl,
|
|
323
|
+
log: (lvl, msg) => logs.push(lvl + ":" + msg)
|
|
324
|
+
});
|
|
325
|
+
await p._tick();
|
|
326
|
+
assert.ok(logs.some((l) => l.startsWith("warn:") && /重投/.test(l)));
|
|
327
|
+
p.stop();
|
|
328
|
+
});
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* im-format — 通道感知消息格式化单测(2026-11-08 业主反馈「企微/飞书推回来的表格都是乱的」)。
|
|
3
|
+
*
|
|
4
|
+
* 只测**纯函数**(不连网、不起进程):表格→可读列表、标题/引用/分隔线/任务列表/嵌套列表、
|
|
5
|
+
* 代码块原样保留、UTF-8 字节截断(4096,优先段落边界、绝不切坏中文)、各通道分支。
|
|
6
|
+
*
|
|
7
|
+
* 接入测(wecom-bot 发出的报文里没有裸表格)在 wecom-bot.test.mjs 里。
|
|
8
|
+
*/
|
|
9
|
+
import assert from "node:assert/strict";
|
|
10
|
+
import test from "node:test";
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
CHANNEL_CAPS,
|
|
14
|
+
TRUNCATED_NOTE,
|
|
15
|
+
convertMarkdownTable,
|
|
16
|
+
displayWidth,
|
|
17
|
+
formatForChannel,
|
|
18
|
+
normalizeChannelName,
|
|
19
|
+
splitTableRow,
|
|
20
|
+
truncateUtf8Safe,
|
|
21
|
+
utf8Bytes
|
|
22
|
+
} from "../im-format.mjs";
|
|
23
|
+
|
|
24
|
+
const fmt = (text, opts = {}) => formatForChannel("wecom", { text, ...opts });
|
|
25
|
+
|
|
26
|
+
/** 展示用:断言"没有裸表格"(分隔行 / 行首竖线表格)。 */
|
|
27
|
+
const hasRawTable = (s) => /\|\s*-{2,}/.test(s) || /^\s*\|.*\|\s*$/m.test(s);
|
|
28
|
+
|
|
29
|
+
// ─────────────────────────── 通道能力表 ───────────────────────────
|
|
30
|
+
|
|
31
|
+
test("CHANNEL_CAPS:企微/钉钉 markdown 无表格、飞书无 markdown、微信特殊格式、plain 兜底", () => {
|
|
32
|
+
assert.deepEqual(CHANNEL_CAPS.wecom, { markdown: true, tables: false, card: true });
|
|
33
|
+
assert.deepEqual(CHANNEL_CAPS.dingtalk, { markdown: true, tables: false, card: true });
|
|
34
|
+
assert.deepEqual(CHANNEL_CAPS.feishu, { markdown: false, tables: false, card: true });
|
|
35
|
+
assert.deepEqual(CHANNEL_CAPS.wechat, { markdown: false, special: true });
|
|
36
|
+
assert.deepEqual(CHANNEL_CAPS.plain, { markdown: false });
|
|
37
|
+
// 通道别名与未知通道
|
|
38
|
+
assert.equal(normalizeChannelName("wxwork"), "wecom");
|
|
39
|
+
assert.equal(normalizeChannelName("Lark"), "feishu");
|
|
40
|
+
assert.equal(normalizeChannelName("dingding"), "dingtalk");
|
|
41
|
+
assert.equal(normalizeChannelName("weixin"), "wechat");
|
|
42
|
+
assert.equal(normalizeChannelName("something-else"), "plain");
|
|
43
|
+
assert.equal(normalizeChannelName(""), "plain");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// ─────────────────────────── 表格 ───────────────────────────
|
|
47
|
+
|
|
48
|
+
test("★ 表格 → 可读列表(2 列中文):不含 |---,每条记录一行「• 列:值」", () => {
|
|
49
|
+
const md = [
|
|
50
|
+
"查询结果如下:",
|
|
51
|
+
"",
|
|
52
|
+
"| 姓名 | 年龄 |",
|
|
53
|
+
"|---|---|",
|
|
54
|
+
"| 张三 | 30 |",
|
|
55
|
+
"| 李四 | 25 |",
|
|
56
|
+
"",
|
|
57
|
+
"以上。"
|
|
58
|
+
].join("\n");
|
|
59
|
+
const out = fmt(md);
|
|
60
|
+
assert.ok(!out.includes("|---"), "★ 不得残留表格分隔行");
|
|
61
|
+
assert.ok(!hasRawTable(out), "★ 不得残留裸表格");
|
|
62
|
+
assert.match(out, /^• 姓名:张三 · 年龄:30$/m);
|
|
63
|
+
assert.match(out, /^• 姓名:李四 · 年龄:25$/m);
|
|
64
|
+
assert.match(out, /查询结果如下/);
|
|
65
|
+
assert.match(out, /以上。/);
|
|
66
|
+
// 表头只作为列名出现,不额外留一行"| 姓名 | 年龄 |"
|
|
67
|
+
assert.ok(!out.includes("| 姓名 |"));
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("表格 1 列 / 3 列:1 列逐条「• 列:值」;3 列窄表仍单行", () => {
|
|
71
|
+
const one = fmt("| 项目 |\n|---|\n| A |\n| B |");
|
|
72
|
+
assert.deepEqual(one.split("\n"), ["• 项目:A", "• 项目:B"]);
|
|
73
|
+
|
|
74
|
+
const three = fmt("| 名称 | 数量 | 价格 |\n|---|---|---|\n| 苹果 | 2 | 5 |");
|
|
75
|
+
assert.match(three, /^• 名称:苹果 · 数量:2 · 价格:5$/m);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("★ 表格 5 列(超宽):拆成「• 记录 N」+ 每列一行,避免客户端折行成乱码", () => {
|
|
79
|
+
const md = [
|
|
80
|
+
"| 序号 | 模块 | 状态 | 负责人 | 备注 |",
|
|
81
|
+
"|---|---|---|---|---|",
|
|
82
|
+
"| 1 | 登录 | 完成 | 张三 | 已经上线验证过了 |",
|
|
83
|
+
"| 2 | 支付 | 进行中 | 李四 | 等第三方回调 |"
|
|
84
|
+
].join("\n");
|
|
85
|
+
const out = fmt(md);
|
|
86
|
+
assert.ok(!hasRawTable(out));
|
|
87
|
+
assert.match(out, /^• 记录 1$/m);
|
|
88
|
+
assert.match(out, /^ {2}序号:1$/m);
|
|
89
|
+
assert.match(out, /^ {2}模块:登录$/m);
|
|
90
|
+
assert.match(out, /^ {2}负责人:张三$/m);
|
|
91
|
+
assert.match(out, /^• 记录 2$/m);
|
|
92
|
+
// 每行都不该太宽(拆开是为了可读;阈值 60 显示列 + 列名余量)
|
|
93
|
+
for (const line of out.split("\n")) {
|
|
94
|
+
assert.ok(displayWidth(line) <= 80, `行太宽(${displayWidth(line)}):${line}`);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("表格转义竖线:`\\|` 是单元格内容,不是分隔符", () => {
|
|
99
|
+
assert.deepEqual(splitTableRow("| a \\| b | c |"), ["a | b", "c"]);
|
|
100
|
+
const out = fmt("| 表达式 | 值 |\n|---|---|\n| a \\| b | 真 |");
|
|
101
|
+
assert.match(out, /• 表达式:a \| b · 值:真/);
|
|
102
|
+
assert.ok(!hasRawTable(out));
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("无分隔行 / 无表头 / 空单元格:不崩、不生造列", () => {
|
|
106
|
+
// 没有 |---| 分隔行的两行表:第一行当表头
|
|
107
|
+
const out = fmt("| k | v |\n| a | 1 |");
|
|
108
|
+
assert.match(out, /^• k:a · v:1$/m);
|
|
109
|
+
// 单元格为空 → 该列不输出
|
|
110
|
+
const sparse = convertMarkdownTable(["| a | b | c |", "|---|---|---|", "| 1 | | 3 |"]);
|
|
111
|
+
assert.deepEqual(sparse, ["• a:1 · c:3"]);
|
|
112
|
+
// 全空行表 → 空数组(不要留空壳)
|
|
113
|
+
assert.deepEqual(convertMarkdownTable(["| a | b |", "|---|---|"]), []);
|
|
114
|
+
assert.deepEqual(convertMarkdownTable([]), []);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// ─────────────────────────── 标题 / 引用 / 分隔线 / 列表 ───────────────────────────
|
|
118
|
+
|
|
119
|
+
test("★ 标题→加粗、引用→›、分隔线→空行、任务列表→☐/☑、嵌套列表压平一层、代码块原样保留", () => {
|
|
120
|
+
const md = [
|
|
121
|
+
"# 一级标题",
|
|
122
|
+
"## 二级标题",
|
|
123
|
+
"> 引用一句话",
|
|
124
|
+
"",
|
|
125
|
+
"---",
|
|
126
|
+
"",
|
|
127
|
+
"- [ ] 待办事项",
|
|
128
|
+
"- [x] 已完成",
|
|
129
|
+
"- 父项",
|
|
130
|
+
" - 子项",
|
|
131
|
+
" - 孙项",
|
|
132
|
+
"",
|
|
133
|
+
"```js",
|
|
134
|
+
"const a = 1; // 代码块里 # 和 | 都不该被转换",
|
|
135
|
+
"| not | a | table |",
|
|
136
|
+
"```"
|
|
137
|
+
].join("\n");
|
|
138
|
+
const out = fmt(md);
|
|
139
|
+
assert.match(out, /^\*\*一级标题\*\*$/m, "标题 → 加粗");
|
|
140
|
+
assert.match(out, /^\*\*二级标题\*\*$/m);
|
|
141
|
+
assert.match(out, /^› 引用一句话$/m, "引用 → ›");
|
|
142
|
+
assert.ok(!/^-{3,}$/m.test(out), "★ 分隔线不得残留");
|
|
143
|
+
assert.match(out, /^- ☐ 待办事项$/m, "未完成 → ☐");
|
|
144
|
+
assert.match(out, /^- ☑ 已完成$/m, "已完成 → ☑");
|
|
145
|
+
assert.match(out, /^- 父项$/m);
|
|
146
|
+
assert.match(out, /^ {2}· 子项$/m, "嵌套压平成一层 ·");
|
|
147
|
+
assert.match(out, /^ {2}· 孙项$/m, "第三层也压到同一层");
|
|
148
|
+
assert.ok(out.includes("```js"), "★ 代码块围栏必须保留");
|
|
149
|
+
assert.ok(out.includes("const a = 1;"), "代码内容保留");
|
|
150
|
+
assert.ok(out.includes("| not | a | table |"), "★ 代码块里的表格语法不得被转换");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("inline code / 加粗 / 链接保留(企微 markdown 支持)", () => {
|
|
154
|
+
const out = fmt("用 `npm test` 跑一下,**注意**看 [文档](https://example.com/x)。");
|
|
155
|
+
assert.equal(out, "用 `npm test` 跑一下,**注意**看 [文档](https://example.com/x)。");
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("非 markdown 通道(plain/feishu)降级:去掉 ** 与 `,列表换 •,代码围栏去掉", () => {
|
|
159
|
+
const md = "# 标题\n\n**重点** 和 `code`\n\n- 条目\n\n```\nplain code\n```";
|
|
160
|
+
const plain = formatForChannel("plain", { text: md });
|
|
161
|
+
assert.ok(!plain.includes("**"), "plain 不得保留加粗标记");
|
|
162
|
+
assert.ok(!plain.includes("`code`"), "plain 不得保留 inline code 标记");
|
|
163
|
+
assert.match(plain, /^• 条目$/m);
|
|
164
|
+
assert.ok(!plain.includes("```"), "plain 去掉代码围栏");
|
|
165
|
+
assert.ok(plain.includes("plain code"), "代码内容保留");
|
|
166
|
+
// feishu:markdown=false,走同一套降级
|
|
167
|
+
const feishu = formatForChannel("feishu", { text: md });
|
|
168
|
+
assert.ok(!feishu.includes("**"));
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// ─────────────────────────── 截断 ───────────────────────────
|
|
172
|
+
|
|
173
|
+
test("★ wecom 4096 字节截断:中文按字节算、优先段落边界、末尾带截断提示、不切坏 UTF-8", () => {
|
|
174
|
+
const para1 = "第一段:" + "甲".repeat(600) + "。"; // 1800+ 字节
|
|
175
|
+
const para2 = "第二段:" + "乙".repeat(600) + "。";
|
|
176
|
+
const para3 = "第三段:" + "丙".repeat(600) + "。";
|
|
177
|
+
const md = [para1, para2, para3].join("\n\n");
|
|
178
|
+
assert.ok(utf8Bytes(md) > 4096, "输入必须超过 4096 字节");
|
|
179
|
+
const out = fmt(md);
|
|
180
|
+
assert.ok(utf8Bytes(out) <= 4096, `输出必须 ≤4096 字节(实际 ${utf8Bytes(out)})`);
|
|
181
|
+
assert.ok(out.endsWith(TRUNCATED_NOTE), "必须带「…(已截断,完整内容看电脑)」");
|
|
182
|
+
assert.ok(out.startsWith("第一段:"), "保留开头");
|
|
183
|
+
assert.ok(!out.includes("第三段:"), "后面的段落被截掉");
|
|
184
|
+
// 不切在 UTF-8 中间:字符串往返必须一致(出现 U+FFFD 说明切坏了)
|
|
185
|
+
assert.equal(Buffer.from(out, "utf8").toString("utf8"), out);
|
|
186
|
+
assert.ok(!out.includes("\uFFFD"));
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("截断:段落太大时退到行边界;单行超长时按码点切(不破坏代理对/中文)", () => {
|
|
190
|
+
// 每行都很短但整体超限 → 在行边界截断(不会出现半行)
|
|
191
|
+
const manyLines = Array.from({ length: 400 }, (_, i) => `第 ${i} 行:数据`).join("\n");
|
|
192
|
+
const out = fmt(manyLines, { maxBytes: 300 });
|
|
193
|
+
assert.ok(utf8Bytes(out) <= 300);
|
|
194
|
+
assert.ok(out.endsWith(TRUNCATED_NOTE));
|
|
195
|
+
assert.equal(Buffer.from(out, "utf8").toString("utf8"), out);
|
|
196
|
+
// 单行超长(含 emoji 4 字节)→ 逐码点截断,不产生 U+FFFD
|
|
197
|
+
const huge = "😀".repeat(2000);
|
|
198
|
+
const cut = truncateUtf8Safe(huge, { maxBytes: 100 });
|
|
199
|
+
assert.ok(utf8Bytes(cut) <= 100);
|
|
200
|
+
assert.equal(Buffer.from(cut, "utf8").toString("utf8"), cut);
|
|
201
|
+
assert.ok(!cut.includes("\uFFFD"));
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test("maxChars 与「调用方已截断」去重:不会出现两个「已截断」", () => {
|
|
205
|
+
const out = fmt("甲".repeat(1000), { maxChars: 100 });
|
|
206
|
+
assert.ok([...out].length <= 100, "字符上限生效");
|
|
207
|
+
assert.equal((out.match(/已截断/g) || []).length, 1);
|
|
208
|
+
// 调用方自带 suffix(回答回传先按字符截 1500 + 本提示):模块不得重复追加
|
|
209
|
+
const pre = `${"乙".repeat(1500)}${TRUNCATED_NOTE}`;
|
|
210
|
+
const out2 = fmt(pre);
|
|
211
|
+
assert.equal((out2.match(/已截断/g) || []).length, 1, "★ 不得出现两个截断提示");
|
|
212
|
+
assert.ok(out2.endsWith(TRUNCATED_NOTE));
|
|
213
|
+
// 自带 suffix 且其实没超限 → 保留原提示
|
|
214
|
+
const shortPre = `短内容${TRUNCATED_NOTE}`;
|
|
215
|
+
assert.equal(fmt(shortPre), shortPre);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// ─────────────────────────── wechat / 未知通道 ───────────────────────────
|
|
219
|
+
|
|
220
|
+
test("wechat 分支:走现有特殊格式(本模块只做 plain 兜底、不截断)", () => {
|
|
221
|
+
const big = "| a | b |\n|---|---|\n| 1 | 2 |\n" + "内容".repeat(5000);
|
|
222
|
+
const out = formatForChannel("wechat", { text: big });
|
|
223
|
+
assert.ok(!hasRawTable(out), "兜底也不留裸表格");
|
|
224
|
+
assert.ok(!out.endsWith(TRUNCATED_NOTE), "wechat 不在这里截断(由微信通道自己管长度/格式)");
|
|
225
|
+
// 未知通道 → plain(fail-safe,不发坏 markdown)
|
|
226
|
+
const unknown = formatForChannel("mystery-channel", { text: "# 标题\n\n**粗**" });
|
|
227
|
+
assert.ok(!unknown.includes("**"));
|
|
228
|
+
assert.equal(unknown, "标题\n\n粗");
|
|
229
|
+
});
|