@morgan9668/dsh-task-notify 0.0.0-stage → 1.1.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/LICENSE +21 -0
- package/README.md +126 -2
- package/__tests__/client.mjs +274 -0
- package/__tests__/e2e.mjs +192 -0
- package/__tests__/settings.mjs +455 -0
- package/__tests__/unit.mjs +432 -0
- package/client.js +402 -0
- package/cordis.patch.yml +63 -0
- package/docs/DETAIL.md +76 -0
- package/index.js +450 -0
- package/locale/en.json +10 -0
- package/locale/zh.json +10 -0
- package/package.json +56 -4
- package/src/channels.js +239 -0
- package/src/render.js +207 -0
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 纯逻辑自测:不联外网、不发真实通知。
|
|
4
|
+
*
|
|
5
|
+
* node __tests__/unit.mjs
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import assert from 'node:assert/strict';
|
|
9
|
+
import { createHmac } from 'node:crypto';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
clampBody,
|
|
13
|
+
dingtalkSignature,
|
|
14
|
+
feishuSignature,
|
|
15
|
+
isChannelReady,
|
|
16
|
+
sendToChannel,
|
|
17
|
+
signedDingtalkUrl,
|
|
18
|
+
BODY_LIMITS,
|
|
19
|
+
} from '../src/channels.js';
|
|
20
|
+
import {
|
|
21
|
+
formatDuration,
|
|
22
|
+
formatNumber,
|
|
23
|
+
lastReplyText,
|
|
24
|
+
renderNotification,
|
|
25
|
+
reasonLabel,
|
|
26
|
+
sessionTitle,
|
|
27
|
+
statusBadge,
|
|
28
|
+
sumUsage,
|
|
29
|
+
toolSummary,
|
|
30
|
+
} from '../src/render.js';
|
|
31
|
+
import { normalizeConfig, readyChannels } from '../index.js';
|
|
32
|
+
|
|
33
|
+
let failures = 0;
|
|
34
|
+
const queue = [];
|
|
35
|
+
const test = (name, fn) => queue.push({ name, fn });
|
|
36
|
+
|
|
37
|
+
async function run() {
|
|
38
|
+
for (const { name, fn } of queue) {
|
|
39
|
+
try {
|
|
40
|
+
await fn();
|
|
41
|
+
console.log(`✓ ${name}`);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
failures += 1;
|
|
44
|
+
console.log(`✗ ${name}`);
|
|
45
|
+
console.log(` ${error instanceof Error ? error.message : String(error)}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
console.log(failures === 0 ? '\n全部通过' : `\n${failures} 项失败`);
|
|
49
|
+
process.exit(failures === 0 ? 0 : 1);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/* ---------------- 签名 ---------------- */
|
|
53
|
+
|
|
54
|
+
test('钉钉签名与官方算法一致(独立复算)', () => {
|
|
55
|
+
const secret = 'SECtest123456789';
|
|
56
|
+
const timestamp = '1700000000000';
|
|
57
|
+
const expected = createHmac('sha256', secret)
|
|
58
|
+
.update(`${timestamp}\n${secret}`, 'utf8')
|
|
59
|
+
.digest('base64');
|
|
60
|
+
assert.equal(dingtalkSignature(timestamp, secret), expected);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('飞书签名把 timestamp\\nsecret 当作 key', () => {
|
|
64
|
+
const secret = 'feishu-secret';
|
|
65
|
+
const timestamp = '1700000000';
|
|
66
|
+
const expected = createHmac('sha256', `${timestamp}\n${secret}`).update('').digest('base64');
|
|
67
|
+
assert.equal(feishuSignature(timestamp, secret), expected);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('钉钉地址带 timestamp 与 sign,且保留原 query', () => {
|
|
71
|
+
const url = signedDingtalkUrl(
|
|
72
|
+
'https://oapi.dingtalk.com/robot/send?access_token=abc',
|
|
73
|
+
'mysecret',
|
|
74
|
+
1700000000000,
|
|
75
|
+
);
|
|
76
|
+
const parsed = new URL(url);
|
|
77
|
+
assert.equal(parsed.searchParams.get('access_token'), 'abc');
|
|
78
|
+
assert.equal(parsed.searchParams.get('timestamp'), '1700000000000');
|
|
79
|
+
assert.equal(parsed.searchParams.get('sign'), dingtalkSignature('1700000000000', 'mysecret'));
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('未配 secret 时钉钉地址不加签名', () => {
|
|
83
|
+
const raw = 'https://oapi.dingtalk.com/robot/send?access_token=abc';
|
|
84
|
+
assert.equal(signedDingtalkUrl(raw, ''), raw);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
/* ---------------- 正文裁剪 ---------------- */
|
|
88
|
+
|
|
89
|
+
test('超长正文按渠道上限裁剪并附说明', () => {
|
|
90
|
+
const long = '啊'.repeat(5000);
|
|
91
|
+
const out = clampBody(long, 100);
|
|
92
|
+
assert.ok(out.length <= 100, `实际长度 ${out.length}`);
|
|
93
|
+
assert.ok(out.includes('已截断'));
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('未超长时原样返回', () => {
|
|
97
|
+
assert.equal(clampBody('短文本', 100), '短文本');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('各渠道上限都是正数且微信最小', () => {
|
|
101
|
+
for (const [kind, limit] of Object.entries(BODY_LIMITS)) {
|
|
102
|
+
assert.ok(limit > 0, `${kind} 上限应为正`);
|
|
103
|
+
}
|
|
104
|
+
assert.ok(BODY_LIMITS.wechat <= BODY_LIMITS.dingtalk);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
/* ---------------- 数据提取 ---------------- */
|
|
108
|
+
|
|
109
|
+
function makeEvents() {
|
|
110
|
+
return [
|
|
111
|
+
{ type: 'turn/start', data: { turn: 3 } },
|
|
112
|
+
{ type: 'session/title', data: { title: '重构通知插件' } },
|
|
113
|
+
{ type: 'tool/call', data: { turn: 3, name: 'bash' } },
|
|
114
|
+
{ type: 'tool/call', data: { turn: 3, name: 'bash' } },
|
|
115
|
+
{ type: 'tool/call', data: { turn: 3, name: 'read' } },
|
|
116
|
+
{
|
|
117
|
+
type: 'assistant/message',
|
|
118
|
+
data: {
|
|
119
|
+
turn: 3,
|
|
120
|
+
usage: { inputTokens: 12340, outputTokens: 2891, totalTokens: 15231, cachedReadTokens: 800 },
|
|
121
|
+
message: {
|
|
122
|
+
content: [
|
|
123
|
+
{ type: 'reasoning', text: '不该出现在正文里的思考' },
|
|
124
|
+
{ type: 'text', text: '已完成后端接入。' },
|
|
125
|
+
{ type: 'text', text: '并补齐了三个渠道的签名。' },
|
|
126
|
+
],
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
{ type: 'turn/end', data: { turn: 3, reason: { kind: 'completed' } } },
|
|
131
|
+
];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
test('token 用量按回合累加 assistant/message 的 usage', () => {
|
|
135
|
+
const usage = sumUsage(makeEvents(), 3);
|
|
136
|
+
assert.equal(usage.available, true);
|
|
137
|
+
assert.equal(usage.inputTokens, 12340);
|
|
138
|
+
assert.equal(usage.outputTokens, 2891);
|
|
139
|
+
assert.equal(usage.totalTokens, 15231);
|
|
140
|
+
assert.equal(usage.cachedReadTokens, 800);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test('多步推理的 token 要累加而不是取最后一条', () => {
|
|
144
|
+
const events = [
|
|
145
|
+
{ type: 'assistant/message', data: { turn: 1, usage: { inputTokens: 100, outputTokens: 10, totalTokens: 110 } } },
|
|
146
|
+
{ type: 'assistant/message', data: { turn: 1, usage: { inputTokens: 200, outputTokens: 20, totalTokens: 220 } } },
|
|
147
|
+
];
|
|
148
|
+
const usage = sumUsage(events, 1);
|
|
149
|
+
assert.equal(usage.inputTokens, 300);
|
|
150
|
+
assert.equal(usage.outputTokens, 30);
|
|
151
|
+
assert.equal(usage.totalTokens, 330);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test('没有 usage 时如实标记未提供,不伪造 0', () => {
|
|
155
|
+
const usage = sumUsage([{ type: 'assistant/message', data: { message: { content: [] } } }], 1);
|
|
156
|
+
assert.equal(usage.available, false);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test('totalTokens 缺失时用输入+输出兜底', () => {
|
|
160
|
+
const usage = sumUsage(
|
|
161
|
+
[{ type: 'assistant/message', data: { turn: 1, usage: { inputTokens: 50, outputTokens: 5 } } }],
|
|
162
|
+
1,
|
|
163
|
+
);
|
|
164
|
+
assert.equal(usage.totalTokens, 55);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test('只取 text 块,reasoning 不进正文', () => {
|
|
168
|
+
const text = lastReplyText(makeEvents(), 400);
|
|
169
|
+
assert.ok(text.includes('已完成后端接入。'));
|
|
170
|
+
assert.ok(!text.includes('不该出现在正文里的思考'));
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test('长回复按上限截断并带省略号', () => {
|
|
174
|
+
const events = [
|
|
175
|
+
{ type: 'assistant/message', data: { message: { content: [{ type: 'text', text: 'x'.repeat(100) }] } } },
|
|
176
|
+
];
|
|
177
|
+
const text = lastReplyText(events, 10);
|
|
178
|
+
assert.ok(text.length <= 11, `实际 ${text.length}`);
|
|
179
|
+
assert.ok(text.endsWith('…'));
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test('工具统计只算本回合并给出前几名', () => {
|
|
183
|
+
const tools = toolSummary(makeEvents(), 3);
|
|
184
|
+
assert.equal(tools.total, 3);
|
|
185
|
+
assert.ok(tools.top.includes('bash×2'));
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test('会话标题取最近一条', () => {
|
|
189
|
+
const events = [
|
|
190
|
+
{ type: 'session/title', data: { title: '旧标题' } },
|
|
191
|
+
{ type: 'session/title', data: { title: '新标题' } },
|
|
192
|
+
];
|
|
193
|
+
assert.equal(sessionTitle(events), '新标题');
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test('无标题时给出默认可读文案', () => {
|
|
197
|
+
assert.equal(sessionTitle([]), 'DeepSeek Harness 任务');
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
/* ---------------- 呈现 ---------------- */
|
|
201
|
+
|
|
202
|
+
test('耗时按中文习惯格式化', () => {
|
|
203
|
+
assert.equal(formatDuration(840), '840毫秒');
|
|
204
|
+
assert.equal(formatDuration(5000), '5秒');
|
|
205
|
+
assert.equal(formatDuration(192000), '3分12秒');
|
|
206
|
+
assert.equal(formatDuration(3720000), '1小时02分');
|
|
207
|
+
assert.equal(formatDuration(Number.NaN), '未知');
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test('数字带千分位', () => {
|
|
211
|
+
assert.equal(formatNumber(15231), '15,231');
|
|
212
|
+
assert.equal(formatNumber(0), '0');
|
|
213
|
+
assert.equal(formatNumber(Number.NaN), '—');
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test('不同结局给不同的状态徽记(出错不能显示成功)', () => {
|
|
217
|
+
assert.ok(statusBadge('completed').includes('完成'));
|
|
218
|
+
assert.ok(statusBadge('error').includes('出错'));
|
|
219
|
+
assert.notEqual(statusBadge('error'), statusBadge('completed'));
|
|
220
|
+
assert.equal(reasonLabel('completed'), '已完成');
|
|
221
|
+
assert.equal(reasonLabel('max-tokens'), '超出输出上限');
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test('未收录的结局原样返回,不硬翻', () => {
|
|
225
|
+
assert.equal(reasonLabel('weird-reason'), 'weird-reason');
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test('渲染结果包含三块必需内容:概要、耗时、token', () => {
|
|
229
|
+
const rendered = renderNotification({
|
|
230
|
+
reasonKind: 'completed',
|
|
231
|
+
durationMs: 192000,
|
|
232
|
+
events: makeEvents(),
|
|
233
|
+
turn: 3,
|
|
234
|
+
maxExcerptChars: 400,
|
|
235
|
+
modelLabel: 'workbuddy/glm-5.3',
|
|
236
|
+
});
|
|
237
|
+
assert.ok(rendered.title.includes('重构通知插件'), rendered.title);
|
|
238
|
+
assert.ok(rendered.title.includes('workbuddy/glm-5.3'), rendered.title);
|
|
239
|
+
assert.ok(rendered.body.includes('任务概要'), '缺概要块');
|
|
240
|
+
assert.ok(rendered.body.includes('耗时'), '缺耗时块');
|
|
241
|
+
assert.ok(rendered.body.includes('token 消耗'), '缺 token 块');
|
|
242
|
+
assert.ok(rendered.body.includes('3分12秒'), '耗时未渲染');
|
|
243
|
+
assert.ok(rendered.body.includes('15,231'), 'token 合计未渲染');
|
|
244
|
+
assert.ok(rendered.body.includes('12,340'), '输入 token 未渲染');
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test('没有用量数据时通知如实说明', () => {
|
|
248
|
+
const rendered = renderNotification({
|
|
249
|
+
reasonKind: 'completed',
|
|
250
|
+
durationMs: 1000,
|
|
251
|
+
events: [{ type: 'assistant/message', data: { message: { content: [{ type: 'text', text: 'ok' }] } } }],
|
|
252
|
+
});
|
|
253
|
+
assert.ok(rendered.body.includes('未提供用量'), rendered.body);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
/* ---------------- 配置 ---------------- */
|
|
257
|
+
|
|
258
|
+
test('空配置下没有任何可用渠道', () => {
|
|
259
|
+
const config = normalizeConfig({});
|
|
260
|
+
assert.deepEqual(readyChannels(config), []);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test('填了 webhook 但没开 enabled 仍不可用', () => {
|
|
264
|
+
const config = normalizeConfig({
|
|
265
|
+
channels: { wecom: { enabled: false, webhook: 'https://example.com/hook' } },
|
|
266
|
+
});
|
|
267
|
+
assert.deepEqual(readyChannels(config), []);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test('开启并填 webhook 的渠道被识别为可用', () => {
|
|
271
|
+
const config = normalizeConfig({
|
|
272
|
+
channels: {
|
|
273
|
+
wecom: { enabled: true, webhook: 'https://qyapi.weixin.qq.com/x' },
|
|
274
|
+
dingtalk: { enabled: true, webhook: 'https://oapi.dingtalk.com/y', secret: 's' },
|
|
275
|
+
},
|
|
276
|
+
});
|
|
277
|
+
assert.deepEqual(readyChannels(config).sort(), ['dingtalk', 'wecom']);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
test('微信通道没有任何凭据时不算可用', () => {
|
|
281
|
+
const config = normalizeConfig({ channels: { wechat: { enabled: true } } });
|
|
282
|
+
assert.equal(isChannelReady(config.channels.wechat, 'wechat'), false);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
test('微信通道填了 Server酱 key 即可用', () => {
|
|
286
|
+
const config = normalizeConfig({
|
|
287
|
+
channels: { wechat: { enabled: true, serverchanSendKey: 'sctp123tabc' } },
|
|
288
|
+
});
|
|
289
|
+
assert.equal(isChannelReady(config.channels.wechat, 'wechat'), true);
|
|
290
|
+
assert.ok(readyChannels(config).includes('wechat'));
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
test('非法数值回落到默认值', () => {
|
|
294
|
+
const config = normalizeConfig({ maxExcerptChars: -5, timeoutMs: 0, reasons: [] });
|
|
295
|
+
assert.equal(config.maxExcerptChars, 400);
|
|
296
|
+
assert.equal(config.timeoutMs, 8000);
|
|
297
|
+
assert.deepEqual(config.reasons, ['completed', 'error', 'blocked']);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
test('webhook 前后空白被裁掉', () => {
|
|
301
|
+
const config = normalizeConfig({
|
|
302
|
+
channels: { wecom: { enabled: true, webhook: ' https://example.com/h ' } },
|
|
303
|
+
});
|
|
304
|
+
assert.equal(config.channels.wecom.webhook, 'https://example.com/h');
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
/* ---------------- 发送行为(假 fetch) ---------------- */
|
|
308
|
+
|
|
309
|
+
function fakeFetch(response, capture = {}) {
|
|
310
|
+
return async (url, init) => {
|
|
311
|
+
capture.url = url;
|
|
312
|
+
capture.init = init;
|
|
313
|
+
capture.body = init?.body;
|
|
314
|
+
return response;
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const okJson = (obj) => ({
|
|
319
|
+
ok: true,
|
|
320
|
+
status: 200,
|
|
321
|
+
text: async () => JSON.stringify(obj),
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
test('企业微信:errcode=0 才算成功', async () => {
|
|
325
|
+
const capture = {};
|
|
326
|
+
const result = await sendToChannel(
|
|
327
|
+
{ enabled: true, webhook: 'https://qyapi.weixin.qq.com/hook' },
|
|
328
|
+
'wecom',
|
|
329
|
+
{ title: 'T', body: 'B' },
|
|
330
|
+
{ timeoutMs: 1000, log: () => {}, fetchImpl: fakeFetch(okJson({ errcode: 0, errmsg: 'ok' }), capture) },
|
|
331
|
+
);
|
|
332
|
+
assert.equal(result.ok, true);
|
|
333
|
+
const payload = JSON.parse(capture.body);
|
|
334
|
+
assert.equal(payload.msgtype, 'markdown');
|
|
335
|
+
assert.ok(payload.markdown.content.includes('T'));
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
test('企业微信:200 但 errcode≠0 判为失败(不能当成功)', async () => {
|
|
339
|
+
const result = await sendToChannel(
|
|
340
|
+
{ enabled: true, webhook: 'https://qyapi.weixin.qq.com/hook' },
|
|
341
|
+
'wecom',
|
|
342
|
+
{ title: 'T', body: 'B' },
|
|
343
|
+
{ timeoutMs: 1000, log: () => {}, fetchImpl: fakeFetch(okJson({ errcode: 93000, errmsg: 'invalid webhook url' })) },
|
|
344
|
+
);
|
|
345
|
+
assert.equal(result.ok, false);
|
|
346
|
+
assert.ok(result.detail.includes('93000'), result.detail);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
test('钉钉:发往带签名的地址,且带 markdown 结构', async () => {
|
|
350
|
+
const capture = {};
|
|
351
|
+
const result = await sendToChannel(
|
|
352
|
+
{ enabled: true, webhook: 'https://oapi.dingtalk.com/robot/send?access_token=x', secret: 'sec' },
|
|
353
|
+
'dingtalk',
|
|
354
|
+
{ title: 'T', body: 'B' },
|
|
355
|
+
{ timeoutMs: 1000, log: () => {}, fetchImpl: fakeFetch(okJson({ errcode: 0 }), capture) },
|
|
356
|
+
);
|
|
357
|
+
assert.equal(result.ok, true);
|
|
358
|
+
assert.ok(capture.url.includes('sign='), '缺少签名参数');
|
|
359
|
+
const payload = JSON.parse(capture.body);
|
|
360
|
+
assert.equal(payload.msgtype, 'markdown');
|
|
361
|
+
assert.ok(payload.markdown.title === 'T');
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
test('飞书:配了 secret 时带上 timestamp 与 sign', async () => {
|
|
365
|
+
const capture = {};
|
|
366
|
+
await sendToChannel(
|
|
367
|
+
{ enabled: true, webhook: 'https://open.feishu.cn/hook', secret: 'fs' },
|
|
368
|
+
'feishu',
|
|
369
|
+
{ title: 'T', body: 'B' },
|
|
370
|
+
{ timeoutMs: 1000, log: () => {}, fetchImpl: fakeFetch(okJson({ code: 0 }), capture) },
|
|
371
|
+
);
|
|
372
|
+
const payload = JSON.parse(capture.body);
|
|
373
|
+
assert.ok(payload.timestamp, '缺 timestamp');
|
|
374
|
+
assert.ok(payload.sign, '缺 sign');
|
|
375
|
+
assert.equal(payload.msg_type, 'interactive');
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
test('飞书:code≠0 判为失败', async () => {
|
|
379
|
+
const result = await sendToChannel(
|
|
380
|
+
{ enabled: true, webhook: 'https://open.feishu.cn/hook' },
|
|
381
|
+
'feishu',
|
|
382
|
+
{ title: 'T', body: 'B' },
|
|
383
|
+
{ timeoutMs: 1000, log: () => {}, fetchImpl: fakeFetch(okJson({ code: 19021, msg: 'sign match fail' })) },
|
|
384
|
+
);
|
|
385
|
+
assert.equal(result.ok, false);
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
test('HTTP 非 2xx 判为失败并保留状态码', async () => {
|
|
389
|
+
const result = await sendToChannel(
|
|
390
|
+
{ enabled: true, webhook: 'https://example.com/h' },
|
|
391
|
+
'wecom',
|
|
392
|
+
{ title: 'T', body: 'B' },
|
|
393
|
+
{ timeoutMs: 1000, log: () => {}, fetchImpl: fakeFetch({ ok: false, status: 404, text: async () => 'not found' }) },
|
|
394
|
+
);
|
|
395
|
+
assert.equal(result.ok, false);
|
|
396
|
+
assert.ok(result.detail.includes('404'));
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
test('网络异常不会抛出,只回报失败', async () => {
|
|
400
|
+
const result = await sendToChannel(
|
|
401
|
+
{ enabled: true, webhook: 'https://example.com/h' },
|
|
402
|
+
'wecom',
|
|
403
|
+
{ title: 'T', body: 'B' },
|
|
404
|
+
{ timeoutMs: 1000, log: () => {}, fetchImpl: async () => { throw new Error('ECONNREFUSED'); } },
|
|
405
|
+
);
|
|
406
|
+
assert.equal(result.ok, false);
|
|
407
|
+
assert.ok(result.detail.includes('ECONNREFUSED'));
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
test('200 但正文不是 JSON 记为已送出不可确认,不冒充成功也不谎报失败', async () => {
|
|
411
|
+
const result = await sendToChannel(
|
|
412
|
+
{ enabled: true, webhook: 'https://example.com/h' },
|
|
413
|
+
'wecom',
|
|
414
|
+
{ title: 'T', body: 'B' },
|
|
415
|
+
{ timeoutMs: 1000, log: () => {}, fetchImpl: fakeFetch({ ok: true, status: 200, text: async () => '<html>ok</html>' }) },
|
|
416
|
+
);
|
|
417
|
+
assert.equal(result.ok, true);
|
|
418
|
+
assert.ok(result.detail.includes('不可确认'));
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
test('微信通道无凭据时如实回报未配置', async () => {
|
|
422
|
+
const result = await sendToChannel(
|
|
423
|
+
{ enabled: true },
|
|
424
|
+
'wechat',
|
|
425
|
+
{ title: 'T', body: 'B' },
|
|
426
|
+
{ timeoutMs: 1000, log: () => {}, fetchImpl: async () => { throw new Error('不该被调用'); } },
|
|
427
|
+
);
|
|
428
|
+
assert.equal(result.ok, false);
|
|
429
|
+
assert.ok(result.detail.includes('未配置'));
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
await run();
|