dsh-notify-say 0.4.82 → 0.4.87
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/CHANGELOG.md +24 -0
- package/README.md +65 -8
- package/lib/client.js +59 -5
- package/lib/index.js +3375 -3253
- package/lib/protocol-ownership.js +123 -0
- package/package.json +2 -2
- package/test/client-bundle.test.mjs +40 -0
- package/test/docs-consistency.test.mjs +84 -12
- package/test/http-integration.test.mjs +76 -0
- package/test/idle-gate-contract.test.mjs +41 -6
- package/test/preview.test.mjs +22 -0
- package/test/protocol-ownership.test.mjs +205 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 自定义协议的所有权判定与清理决策(v0.4.83)。
|
|
3
|
+
*
|
|
4
|
+
* ## 这组测试防什么
|
|
5
|
+
*
|
|
6
|
+
* 两个真实风险,都在 2026-10-20 的发布前审查中被点名:
|
|
7
|
+
*
|
|
8
|
+
* ### 1. 删别人的注册
|
|
9
|
+
*
|
|
10
|
+
* 原实现里 `syncJumpProtocol` 对所有非 `ok` 状态一律 `unregister` ——
|
|
11
|
+
* 包括 `foreign`(键在、但指向别人的程序)。等于**别人的东西被我们删了**。
|
|
12
|
+
*
|
|
13
|
+
* ### 2. 卸载后留尾巴
|
|
14
|
+
*
|
|
15
|
+
* DSH 官方不区分「热重载」与「真正卸载」(源码 `dsh-plugin-manager`:
|
|
16
|
+
* "With HMR, an installed package is removed before another version is
|
|
17
|
+
* installed, so its plugins stop first")。所以不能在 dispose 里注销 ——
|
|
18
|
+
* 实测会误删热重载后的注册。
|
|
19
|
+
*
|
|
20
|
+
* 采用的方案:**启动时自清理**。判据是「注册指向的处理器文件已不存在」,
|
|
21
|
+
* 这个判据在热重载/升级/正常运行时永远为假,所以**不会误删**。
|
|
22
|
+
*
|
|
23
|
+
* ## 测试策略(v0.4.83 改进)
|
|
24
|
+
*
|
|
25
|
+
* 判定与决策已经抽成 `lib/protocol-ownership.js` 的**纯函数**,
|
|
26
|
+
* 所以这里直接测生产代码 —— 不再是"复制一份逻辑来验"。
|
|
27
|
+
* `exists` 是注入的,测试不碰真实文件系统与注册表(只有一个集成用例除外)。
|
|
28
|
+
*/
|
|
29
|
+
import { test } from 'node:test';
|
|
30
|
+
import assert from 'node:assert/strict';
|
|
31
|
+
import { classifyProtocolOwner, planProtocolSync } from '../lib/protocol-ownership.js';
|
|
32
|
+
|
|
33
|
+
const ROOT = 'E:\\0Deepseek\\tools\\plugins\\dsh-notify-desktop';
|
|
34
|
+
|
|
35
|
+
/** 拼一条"像我们写的"注册命令。 */
|
|
36
|
+
const ourCmd = (handler) =>
|
|
37
|
+
`"C:\\Windows\\System32\\wscript.exe" "${ROOT}\\${handler}" "%1"`;
|
|
38
|
+
|
|
39
|
+
// ══ C1~C4: classifyProtocolOwner 的四种状态 ═════════════════════════════
|
|
40
|
+
|
|
41
|
+
test('C1: 空值 → missing(没注册)', () => {
|
|
42
|
+
assert.equal(classifyProtocolOwner('', ROOT), 'missing');
|
|
43
|
+
assert.equal(classifyProtocolOwner(undefined, ROOT), 'missing');
|
|
44
|
+
assert.equal(classifyProtocolOwner(' ', ROOT), 'missing');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('C2: 指向本插件且文件存在 → ok', () => {
|
|
48
|
+
const r = classifyProtocolOwner(
|
|
49
|
+
ourCmd('jump-handler.vbs'), ROOT, { exists: () => true },
|
|
50
|
+
);
|
|
51
|
+
assert.equal(r, 'ok');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('C3: 指向本插件但文件不存在 → stale(卸载后的尾巴)', () => {
|
|
55
|
+
const r = classifyProtocolOwner(
|
|
56
|
+
ourCmd('jump-handler.vbs'), ROOT, { exists: () => false },
|
|
57
|
+
);
|
|
58
|
+
assert.equal(r, 'stale', '文件没了必须判 stale —— 这是自清理的唯一入口');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('C4: 指向别的程序 → foreign(绝不能碰)', () => {
|
|
62
|
+
const r = classifyProtocolOwner(
|
|
63
|
+
'"C:\\OtherApp\\other.exe" "%1"', ROOT, { exists: () => true },
|
|
64
|
+
);
|
|
65
|
+
assert.equal(r, 'foreign', '不是我们写的注册必须判 foreign');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// ══ C5~C7: 边界与容错 ══════════════════════════════════════════════════
|
|
69
|
+
|
|
70
|
+
test('C5: 同名文件但在别人目录下 → foreign(不能只看文件名)', () => {
|
|
71
|
+
const r = classifyProtocolOwner(
|
|
72
|
+
'"C:\\Windows\\System32\\wscript.exe" "D:\\OtherPlugin\\jump-handler.vbs" "%1"',
|
|
73
|
+
ROOT,
|
|
74
|
+
{ exists: () => true },
|
|
75
|
+
);
|
|
76
|
+
assert.equal(r, 'foreign',
|
|
77
|
+
'别的插件也可能叫 jump-handler.vbs —— 必须比对目录,否则会删别人');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('C6: 路径大小写与斜杠混用仍能认出是我们', () => {
|
|
81
|
+
const variants = [
|
|
82
|
+
ourCmd('jump-handler.vbs'),
|
|
83
|
+
ourCmd('jump-handler.vbs').toLowerCase(),
|
|
84
|
+
ourCmd('jump-handler.vbs').toUpperCase(),
|
|
85
|
+
ourCmd('jump-handler.vbs').replace(/\\/g, '/'),
|
|
86
|
+
];
|
|
87
|
+
for (const v of variants) {
|
|
88
|
+
assert.equal(
|
|
89
|
+
classifyProtocolOwner(v, ROOT, { exists: () => true }), 'ok',
|
|
90
|
+
`这个变体应被认出是我们的: ${v}`,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('C7: 拿不到 exists 判定时不冒险(判 ok,不删)', () => {
|
|
96
|
+
// 误删的代价(用户点 Toast 没反应)高于"多留一条注册"
|
|
97
|
+
const r = classifyProtocolOwner(ourCmd('jump-handler.vbs'), ROOT, {});
|
|
98
|
+
assert.equal(r, 'ok', '无法确认时宁可保留,不能删');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// ══ D1~D5: planProtocolSync 的决策矩阵 ═════════════════════════════════
|
|
102
|
+
|
|
103
|
+
test('D1: foreign 无论开关如何,都不动它', () => {
|
|
104
|
+
for (const enabled of [true, false]) {
|
|
105
|
+
const p = planProtocolSync('foreign', enabled);
|
|
106
|
+
assert.equal(p.action, 'none', 'foreign 绝不能产生 unregister/register 动作');
|
|
107
|
+
assert.equal(p.downgrade, true, 'foreign 必须标记降级');
|
|
108
|
+
assert.equal(p.usable, false, 'foreign 时精准跳转不可用');
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('D2: ok + 开关开 → reuse(不重写注册表)', () => {
|
|
113
|
+
const p = planProtocolSync('ok', true);
|
|
114
|
+
assert.equal(p.action, 'reuse');
|
|
115
|
+
assert.equal(p.usable, true);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('D3: ok + 开关关 → unregister(是我们的,可以清)', () => {
|
|
119
|
+
const p = planProtocolSync('ok', false);
|
|
120
|
+
assert.equal(p.action, 'unregister');
|
|
121
|
+
assert.equal(p.usable, false);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test('D4: stale + 开关开 → register 且先清理', () => {
|
|
125
|
+
const p = planProtocolSync('stale', true);
|
|
126
|
+
assert.equal(p.action, 'register');
|
|
127
|
+
assert.equal(p.cleanupFirst, true, 'stale 必须先清理失效项再注册');
|
|
128
|
+
assert.equal(p.usable, true);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test('D5: missing + 开关开 → register;开关关 → 什么都不做', () => {
|
|
132
|
+
assert.equal(planProtocolSync('missing', true).action, 'register');
|
|
133
|
+
assert.equal(planProtocolSync('missing', false).action, 'none');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// ══ D6: 不变量 —— 只有我们的东西才允许被删 ═════════════════════════════
|
|
137
|
+
|
|
138
|
+
test('D6: 任何输入组合下,unregister 只可能发生在 ok/stale', () => {
|
|
139
|
+
const states = ['missing', 'ok', 'stale', 'foreign'];
|
|
140
|
+
for (const s of states) {
|
|
141
|
+
for (const enabled of [true, false]) {
|
|
142
|
+
const p = planProtocolSync(s, enabled);
|
|
143
|
+
if (p.action === 'unregister' || p.cleanupFirst) {
|
|
144
|
+
assert.ok(
|
|
145
|
+
s === 'ok' || s === 'stale',
|
|
146
|
+
`违反不变量:state=${s} enabled=${enabled} 时产生了删除动作`,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// ══ I1: 真实集成 —— index.js 确实接到这套判定上 ═════════════════════════
|
|
154
|
+
|
|
155
|
+
test('I1: index.js 必须使用纯函数(不能自己又写一套判定)', async () => {
|
|
156
|
+
const { readFileSync } = await import('node:fs');
|
|
157
|
+
const { join, resolve, dirname } = await import('node:path');
|
|
158
|
+
const { fileURLToPath } = await import('node:url');
|
|
159
|
+
const ROOT_DIR = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
160
|
+
const SRC = readFileSync(join(ROOT_DIR, 'lib', 'index.js'), 'utf8');
|
|
161
|
+
|
|
162
|
+
assert.match(SRC, /from '\.\/protocol-ownership\.js'/,
|
|
163
|
+
'index.js 必须 import 判定模块 —— 否则规则会出现两份');
|
|
164
|
+
assert.match(SRC, /classifyProtocolOwner\(/,
|
|
165
|
+
'probe 必须调用 classifyProtocolOwner');
|
|
166
|
+
assert.match(SRC, /planProtocolSync\(/,
|
|
167
|
+
'sync 必须调用 planProtocolSync');
|
|
168
|
+
|
|
169
|
+
// 反向断言:不能残留手写的四态判定(那说明有人又写了一份)
|
|
170
|
+
const probeAt = SRC.indexOf('function probeJumpProtocol()');
|
|
171
|
+
const probeBody = SRC.slice(probeAt, probeAt + 2000);
|
|
172
|
+
assert.doesNotMatch(probeBody, /return 'foreign'/,
|
|
173
|
+
'probe 里不该再有手写的 foreign 判定 —— 判定已收进纯函数');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// ══ I2: 设置页改开关必须立即生效(不是重启才生效)═════════════════════════
|
|
177
|
+
//
|
|
178
|
+
// ## 为什么必须有这条
|
|
179
|
+
//
|
|
180
|
+
// 2026-10-20 真机验收发现:在设置页切换「点击通知跳到对应会话」后,
|
|
181
|
+
// **注册表根本没动** —— 必须重启 DSH 才生效。而 README 写着"立即生效"。
|
|
182
|
+
//
|
|
183
|
+
// 根因:`syncJumpProtocol()` 只在 `apply()`(插件启动)里被调用过一次,
|
|
184
|
+
// 配置变更回调 `onChanged` 只调了 `refreshConfig()`。
|
|
185
|
+
//
|
|
186
|
+
// 这类缺陷特别难发现:改配置 → 界面显示已保存 → 但底层没动作。
|
|
187
|
+
// 用户会以为是"功能坏了",而日志里连一行记录都没有。
|
|
188
|
+
test('I2: 配置变更回调必须同步协议状态(否则要重启才生效)', async () => {
|
|
189
|
+
const { readFileSync } = await import('node:fs');
|
|
190
|
+
const { join, resolve, dirname } = await import('node:path');
|
|
191
|
+
const { fileURLToPath } = await import('node:url');
|
|
192
|
+
const ROOT_DIR = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
193
|
+
const SRC = readFileSync(join(ROOT_DIR, 'lib', 'index.js'), 'utf8');
|
|
194
|
+
|
|
195
|
+
// 取 createConfigRoute 的 onChanged 回调体
|
|
196
|
+
const m = SRC.match(/onChanged:\s*\(\)\s*=>\s*\{([\s\S]*?)\n\s{2}\},/);
|
|
197
|
+
assert.ok(m, '找不到 configRoute 的 onChanged 回调(源码结构变了,请更新本测试)');
|
|
198
|
+
|
|
199
|
+
const body = m[1];
|
|
200
|
+
assert.match(body, /refreshConfig\(\)/, 'onChanged 必须重读配置');
|
|
201
|
+
assert.match(body, /syncJumpProtocol\(\)/,
|
|
202
|
+
'onChanged 必须同步协议状态 —— 否则设置页开关要重启 DSH 才生效'
|
|
203
|
+
+ '(2026-10-20 真机验收实测踩到)');
|
|
204
|
+
});
|
|
205
|
+
|