dsh-notify-say 0.5.3 → 0.5.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-notify-say",
3
- "version": "0.5.3",
3
+ "version": "0.5.7",
4
4
  "description": "Desktop notifications for DSH Desktop that actually speak: Windows TTS out of the box (zero download), VOICEVOX voice packs optional, a selectable system-sound fallback, and an idle gate that waits for running subagents before saying the turn is done. Includes a settings-page GUI (sound source, per-event overrides, jump-back-to-session, cooldowns) and a self hot-reload for development.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -58,11 +58,13 @@
58
58
  "dsh": "\u003e=0.2.1-rc.2 \u003c0.3.0"
59
59
  },
60
60
  "scripts": {
61
- "test": "node --test",
62
- "test:all": "node --test \u0026\u0026 node --import ./test/route-loader-register.mjs --test test/fail-route.test.mjs test/audio-wiring.test.mjs",
63
- "test:route": "node --import ./test/route-loader-register.mjs --test test/fail-route.test.mjs",
64
- "test:audio-wire": "node --import ./test/route-loader-register.mjs --test test/audio-wiring.test.mjs",
65
- "check:client": "node ./test/client-bundle.test.mjs"
61
+ "test": "node --import ./test/test-env-setup.mjs --test",
62
+ "test:route": "node --import ./test/test-env-setup.mjs --import ./test/route-loader-register.mjs --test test/fail-route.test.mjs",
63
+ "test:audio-wire": "node --import ./test/test-env-setup.mjs --import ./test/route-loader-register.mjs --test test/audio-wiring.test.mjs",
64
+ "test:toast": "node --import ./test/test-env-setup.mjs --import ./test/route-loader-register.mjs --test test/toast-sound-channels.test.mjs",
65
+ "test:all": "node --import ./test/test-env-setup.mjs --test \u0026\u0026 node --import ./test/test-env-setup.mjs --import ./test/route-loader-register.mjs --test test/fail-route.test.mjs test/audio-wiring.test.mjs test/toast-sound-channels.test.mjs test/voice-label.test.mjs test/ptc-question.test.mjs",
66
+ "check:client": "node ./test/client-bundle.test.mjs",
67
+ "test:ptc": "node --import ./test/test-env-setup.mjs --import ./test/route-loader-register.mjs --test test/ptc-question.test.mjs"
66
68
  },
67
69
  "dsh": {
68
70
  "manifestVersion": 1,
@@ -210,3 +210,68 @@ test('R8: 文件层损坏时拒绝写入(不覆盖用户数据)', async () =
210
210
  // 原文件必须保持原样,供人工救
211
211
  assert.equal(readFileSync(p, 'utf8'), '{ this is not valid json');
212
212
  });
213
+
214
+ // ── R9: installed 字段(v0.5.6,issue #3 的后端一半)────────────────────────
215
+ //
216
+ // 为什么这条必须在**后端**测:前端 voice-options.test.mjs 是拿桩数据渲染的,
217
+ // 它证明"拿到 installed 就能多出选项",证明不了"/config 真的会返回 installed"。
218
+ // 中间隔着 createConfigRoute 的 opts 透传、installedList() 的过滤与异常兜底,
219
+ // 任何一处写错,前端那 6 条断言照样全绿 —— 而用户的下拉依旧看不到已下载音色。
220
+ test('R9: 注入 getInstalled 时,GET /config 带上已安装音色列表', async () => {
221
+ const p = configPath();
222
+ try { if (p && existsSync(p)) unlinkSync(p); } catch { /* ignore */ }
223
+ const r = createConfigRoute({
224
+ onChanged: () => {},
225
+ getEffective: () => ({ volume: 100 }),
226
+ getInstalled: () => ['zundamon-normal', 'zh-soft-female', 'tohoku-zunko'],
227
+ });
228
+
229
+ const { code, doc } = await call(r, { url: GET_URL });
230
+ assert.equal(code, 200);
231
+ assert.deepEqual(doc.installed, ['zundamon-normal', 'zh-soft-female', 'tohoku-zunko']);
232
+ // 原有字段一个都不能少 —— 这是改响应形状时最容易碰坏的地方
233
+ assert.ok(doc.values && typeof doc.values === 'object', 'values 必须仍在');
234
+ assert.ok(Array.isArray(doc.keys), 'keys 必须仍在');
235
+ });
236
+
237
+ test('R10: 没注入 getInstalled 时不返回 installed(既有行为逐字不变)', async () => {
238
+ const p = configPath();
239
+ try { if (p && existsSync(p)) unlinkSync(p); } catch { /* ignore */ }
240
+ const r = createConfigRoute({ onChanged: () => {}, getEffective: () => ({ volume: 100 }) });
241
+
242
+ const { code, doc } = await call(r, { url: GET_URL });
243
+ assert.equal(code, 200);
244
+ assert.equal(Object.prototype.hasOwnProperty.call(doc, 'installed'), false,
245
+ '不注入就不该出现这个键:老的调用方/测试不该看到新字段');
246
+ });
247
+
248
+ test('R11: getInstalled 抛异常时退回空数组,而不是让 /config 整个 500', async () => {
249
+ // 真实场景:用户目录权限异常、盘符掉了、readdir 抛 EPERM。
250
+ // 这时正确的降级是"下拉退回内置清单",绝不能连配置页都打不开。
251
+ const p = configPath();
252
+ try { if (p && existsSync(p)) unlinkSync(p); } catch { /* ignore */ }
253
+ const r = createConfigRoute({
254
+ onChanged: () => {},
255
+ getEffective: () => ({ volume: 100 }),
256
+ getInstalled: () => { throw new Error('EPERM'); },
257
+ });
258
+
259
+ const { code, doc } = await call(r, { url: GET_URL });
260
+ assert.equal(code, 200, '一个可选字段拿不到,不该拖垮整个配置读取');
261
+ assert.deepEqual(doc.installed, []);
262
+ });
263
+
264
+ test('R12: getInstalled 返回脏数据时只保留非空字符串', async () => {
265
+ const p = configPath();
266
+ try { if (p && existsSync(p)) unlinkSync(p); } catch { /* ignore */ }
267
+ const r = createConfigRoute({
268
+ onChanged: () => {},
269
+ getEffective: () => ({ volume: 100 }),
270
+ getInstalled: () => ['ok-key', '', null, 42, { a: 1 }, 'zh-mid-male'],
271
+ });
272
+
273
+ const { code, doc } = await call(r, { url: GET_URL });
274
+ assert.equal(code, 200);
275
+ assert.deepEqual(doc.installed, ['ok-key', 'zh-mid-male'],
276
+ '非字符串必须滤掉,否则前端 appendInstalled 会拿对象去算前缀');
277
+ });
@@ -28,15 +28,25 @@ const CLIENT = readFileSync(join(ROOT, 'lib', 'client.js'), 'utf8');
28
28
  async function withTemp(fn) {
29
29
  const old = process.env.TEMP;
30
30
  const oldTmp = process.env.TMP;
31
+ // v0.5.4:日志目录现在优先认 DSH_NOTIFY_SAY_LOG_DIR(见 lib/log-path.js)。
32
+ // 只改 TEMP 已经不管用了 —— 测试预加载的 test-env-setup.mjs 会把覆盖变量指向
33
+ // 另一个临时目录,于是 seedLog 写的文件根本不是被测代码读的那个。
34
+ // 三个都要设:覆盖变量(优先级最高)+ TEMP/TMP(无覆盖时的回落路径,G1/G7 用它)。
35
+ const oldLogDir = process.env.DSH_NOTIFY_SAY_LOG_DIR;
31
36
  const dir = join(tmpdir(), `diag-${Math.random().toString(36).slice(2, 10)}`);
32
37
  mkdirSync(dir, { recursive: true });
33
38
  process.env.TEMP = dir;
34
39
  process.env.TMP = dir;
40
+ // 注意:覆盖变量指的是**日志目录本身**(不含 dsh-notify-say 这一层),
41
+ // 而 seedLog() 写的是 <dir>/dsh-notify-say —— 两者必须对齐,否则照样读不到。
42
+ process.env.DSH_NOTIFY_SAY_LOG_DIR = join(dir, 'dsh-notify-say');
35
43
  try {
36
44
  return await fn(dir);
37
45
  } finally {
38
46
  if (old === undefined) delete process.env.TEMP; else process.env.TEMP = old;
39
47
  if (oldTmp === undefined) delete process.env.TMP; else process.env.TMP = oldTmp;
48
+ if (oldLogDir === undefined) delete process.env.DSH_NOTIFY_SAY_LOG_DIR;
49
+ else process.env.DSH_NOTIFY_SAY_LOG_DIR = oldLogDir;
40
50
  try { rmSync(dir, { recursive: true, force: true }); } catch { /* ignore */ }
41
51
  }
42
52
  }
@@ -0,0 +1,127 @@
1
+ /**
2
+ * 日志路径隔离测试(v0.5.4)。
3
+ *
4
+ * ## 为什么要有这个文件
5
+ *
6
+ * 2026-11-09 在真机排查 DSH 0.2.2-rc.1 兼容性时,`notify.log` 里出现 12 条
7
+ * `[play-fail] ... spawn Z:\__no_such_root__\...\powershell.exe ENOENT`,
8
+ * 全部集中在跑 `npm test` 的那一秒 —— **是测试写进来的,不是真机故障**。
9
+ *
10
+ * 根因:测试隔离了 `DSH_HOME`,却没隔离日志目录;`wiring.test.mjs` 会把
11
+ * `SystemRoot` 换成 `Z:\__no_such_root__` 再调 `apply()`,失败的 spawn
12
+ * 被 `appendNotifyLog` 原样写进用户真实日志。任何看到那几行的人都会
13
+ * 误判「插件在真机上起不来 PowerShell」—— 这比日志变脏严重得多。
14
+ *
15
+ * ## 检查什么
16
+ *
17
+ * 1. `DSH_NOTIFY_SAY_LOG_DIR` 覆盖生效,且空白视为未设置(与 `safeHome` 同口径)
18
+ * 2. 没有 TEMP/TMP 时返回空串,**不能**拼出相对路径写进 cwd
19
+ * 3. `diagnostics.js` 的 `logDirPath()` 与宿主侧走同一份实现(否则导出会漏行)
20
+ * 4. 两个 ps1 读同一个环境变量(Node 侧与 PowerShell 侧必须落在同一文件)
21
+ * 5. `package.json` 里所有 `node --test` 脚本都预加载 `test-env-setup.mjs`
22
+ */
23
+ import { test } from 'node:test';
24
+ import assert from 'node:assert/strict';
25
+ import { readFileSync } from 'node:fs';
26
+ import { tmpdir } from 'node:os';
27
+ import { resolve, dirname, join } from 'node:path';
28
+ import { fileURLToPath } from 'node:url';
29
+
30
+ import { notifyLogDir, notifyLogPath, LOG_DIR_ENV } from '../lib/log-path.js';
31
+ import { logDirPath } from '../lib/diagnostics.js';
32
+
33
+ const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
34
+ const PKG = JSON.parse(readFileSync(join(ROOT, 'package.json'), 'utf8'));
35
+
36
+ // ── L1: 覆盖变量生效(这是修复的核心)──────────────────────────────────
37
+ test('L1: DSH_NOTIFY_SAY_LOG_DIR 覆盖 TEMP', () => {
38
+ const dir = notifyLogDir({ env: { DSH_NOTIFY_SAY_LOG_DIR: 'E:\\tmp\\logs', TEMP: 'E:\\real\\temp' } });
39
+ assert.equal(dir, 'E:\\tmp\\logs', '显式覆盖必须优先于 TEMP');
40
+ });
41
+
42
+ // ── L2: 空白覆盖视为未设置 ──────────────────────────────────────────────
43
+ //
44
+ // 判据必须与 `config-store.js` 的 `safeHome` 一致:**空白不算设置**。
45
+ // 否则 `DSH_NOTIFY_SAY_LOG_DIR=""` 会解析出 cwd 相对路径。
46
+ test('L2: 空白 / 全空格的覆盖值视为未设置', () => {
47
+ for (const blank of ['', ' ', '\t']) {
48
+ const dir = notifyLogDir({ env: { DSH_NOTIFY_SAY_LOG_DIR: blank, TEMP: 'E:\\real\\temp' } });
49
+ assert.equal(dir, join('E:\\real\\temp', 'dsh-notify-say'),
50
+ `覆盖值为 ${JSON.stringify(blank)} 时应回落到 TEMP`);
51
+ }
52
+ });
53
+
54
+ // ── L3: 没有 TEMP/TMP 时给空串(绝不写相对路径)────────────────────────
55
+ test('L3: 无 TEMP/TMP 且无覆盖时返回空串', () => {
56
+ assert.equal(notifyLogDir({ env: {} }), '', '不能拼出相对路径 —— 那会把日志写进宿主 cwd');
57
+ assert.equal(notifyLogPath({ env: {} }), '', '同理');
58
+ });
59
+
60
+ // ── L4: 文件名与 TEMP 回落 ──────────────────────────────────────────────
61
+ test('L4: 默认落到 TEMP\\dsh-notify-say\\notify.log', () => {
62
+ const env = { TEMP: 'E:\\real\\temp' };
63
+ assert.equal(notifyLogDir({ env }), join('E:\\real\\temp', 'dsh-notify-say'));
64
+ assert.equal(notifyLogPath({ env }), join('E:\\real\\temp', 'dsh-notify-say', 'notify.log'));
65
+ });
66
+
67
+ // ── L5: TMP 作为 TEMP 的备选 ────────────────────────────────────────────
68
+ test('L5: TEMP 缺失时用 TMP', () => {
69
+ assert.equal(notifyLogDir({ env: { TMP: 'E:\\only\\tmp' } }), join('E:\\only\\tmp', 'dsh-notify-say'));
70
+ });
71
+
72
+ // ── L6: diagnostics 必须同源(否则导出会漏掉 ps1 写的行)────────────────
73
+ test('L6: diagnostics.logDirPath 与宿主侧同源', () => {
74
+ const old = process.env[LOG_DIR_ENV];
75
+ const probe = join(tmpdir(), 'logpath-same-source-probe');
76
+ try {
77
+ process.env[LOG_DIR_ENV] = probe;
78
+ assert.equal(logDirPath(), probe,
79
+ 'diagnostics 必须认同一份覆盖变量 —— 否则测试/便携模式下导出会读错目录');
80
+ assert.equal(logDirPath(), notifyLogDir(), '两处必须返回完全相同的目录');
81
+ } finally {
82
+ if (old === undefined) delete process.env[LOG_DIR_ENV];
83
+ else process.env[LOG_DIR_ENV] = old;
84
+ }
85
+ });
86
+
87
+ // ── L7: 两个 ps1 必须读同一个环境变量 ──────────────────────────────────
88
+ //
89
+ // `[plan]`/`[skip]` 是 Node 写的,`[toast]`/`[preview]`/`[tts]` 是 ps1 写的。
90
+ // 一边认覆盖、一边不认,排查时就会看到"半个故事"。
91
+ test('L7: dsh-notify.ps1 与 tts-speak.ps1 都认 DSH_NOTIFY_SAY_LOG_DIR', () => {
92
+ for (const rel of ['dsh-notify.ps1', 'tts-speak.ps1']) {
93
+ const src = readFileSync(join(ROOT, rel), 'utf8');
94
+ assert.match(src, /DSH_NOTIFY_SAY_LOG_DIR/,
95
+ `${rel} 必须读 ${LOG_DIR_ENV}(与 lib/log-path.js 同口径)`);
96
+ assert.match(src, /IsNullOrWhiteSpace/,
97
+ `${rel} 必须用 IsNullOrWhiteSpace 判空 —— 空白不能当成有效目录`);
98
+ }
99
+ });
100
+
101
+ // ── L8: 所有 node --test 脚本都必须预加载隔离 ──────────────────────────
102
+ //
103
+ // 这条是防"以后新加一个脚本又漏了"。实测代价见文件头。
104
+ test('L8: package.json 里每个 node --test 脚本都预加载 test-env-setup.mjs', () => {
105
+ const offenders = [];
106
+ for (const [name, cmd] of Object.entries(PKG.scripts || {})) {
107
+ if (!String(cmd).includes('node ') || !String(cmd).includes('--test')) continue;
108
+ if (!String(cmd).includes('--import ./test/test-env-setup.mjs')) offenders.push(`${name}: ${cmd}`);
109
+ }
110
+ assert.deepEqual(offenders, [],
111
+ '这些脚本会在跑测试时写用户真实 notify.log —— 必须加 --import ./test/test-env-setup.mjs');
112
+ });
113
+
114
+ // ── L9: 隔离真的生效了(当前进程确实被重定向)──────────────────────────
115
+ //
116
+ // 直接 `node --test test/log-path.test.mjs` 时不会预加载,这里 skip;
117
+ // L8 保证正式入口一定预加载,所以不存在"两边都松"的空档。
118
+ test('L9: 跑测试时日志目录已被重定向到临时目录',
119
+ { skip: !process.env[LOG_DIR_ENV] && '未预加载 test-env-setup.mjs(L8 保证正式入口会预加载)' },
120
+ () => {
121
+ const dir = notifyLogDir();
122
+ assert.ok(dir, '预加载后必须能解析出目录');
123
+ assert.ok(dir.startsWith(tmpdir()),
124
+ `测试日志目录必须在系统临时区,实际是 ${dir}`);
125
+ assert.notEqual(dir, join(process.env.TEMP || '', 'dsh-notify-say'),
126
+ '绝不能落回用户真实的 %TEMP%\\dsh-notify-say');
127
+ });
@@ -0,0 +1,187 @@
1
+ /**
2
+ * dsh-notify-say「PTC / code-mode 下的提问提醒」测试 —— v0.5.7(issue #5)。
3
+ *
4
+ * ## 这个文件在防什么
5
+ *
6
+ * PTC(code-mode)模式下,模型手里只有 `run_code` 一个工具,提问工具是**子调用**:
7
+ * 顶层的 `tool/call` 事件里 `name` 恒为 `run_code`,`QUESTION_TOOLS` 判定永远不命中
8
+ * → 通知一声不响,用户只能干等。issue #5 原话是"在 code-mode 下永远不会触发"。
9
+ *
10
+ * 真名与真参数只在 `tool/ptc-dispatch-start` 里(宿主
11
+ * `@deepseek-ai/dsh-tools/lib/types/types.d.ts` 的 `PtcDispatchStartEventData`)。
12
+ * 这个行为**不能靠读源码文本断言**:订阅写错事件名、把 `subCallId` 当 `callId`、
13
+ * 或者忘了结算时清理幂等键,源码正则都照样全绿。
14
+ *
15
+ * ## 观测手段
16
+ *
17
+ * 与 `fail-route.test.mjs` 同款:用 `--import` loader 把 `node:child_process` 换成
18
+ * 记录型假实现 → `spawn()` 只把参数记进 `globalThis.__SPAWN_CALLS`,
19
+ * 不弹 Toast、不出声、不依赖真实 SystemRoot。断言的是**插件实际传给 ps1 的 -Kind**,
20
+ * 这是"用户到底会不会听见提问提醒"的最短证据链。
21
+ *
22
+ * ⚠️ 运行方式:本文件需要 loader,**不能用默认 `node --test`**(否则用例全 skip)。
23
+ * 用 `npm run test:ptc`,或:
24
+ * node --import ./test/test-env-setup.mjs --import ./test/route-loader-register.mjs --test test/ptc-question.test.mjs
25
+ */
26
+ import { test } from 'node:test';
27
+ import assert from 'node:assert/strict';
28
+ import { mkdtempSync, rmSync } from 'node:fs';
29
+ import { tmpdir } from 'node:os';
30
+ import { join } from 'node:path';
31
+
32
+ // ⚠️ 必须在 import lib/index.js **之前**把 DSH_HOME 指到空目录:
33
+ // 插件会读 `$DSH_HOME/storages/dsh-notify-say/config.json` 作为运行时覆盖层,
34
+ // 优先级高于 apply() 传入的 config —— 用真实 DSH_HOME 的话,
35
+ // 用户只要在 GUI 里关过 notifyOnQuestion,本文件的断言就会红。
36
+ const ISOLATED_HOME = mkdtempSync(join(tmpdir(), 'notify-ptc-home-'));
37
+ process.env.DSH_HOME = ISOLATED_HOME;
38
+ process.on('exit', () => {
39
+ try { rmSync(ISOLATED_HOME, { recursive: true, force: true }); } catch { /* best effort */ }
40
+ });
41
+
42
+ // ⚠️ 显式关闭注册表访问(`test/protocol-ownership.test.mjs` 的 I7 会检查这一行)。
43
+ // 本文件的 spawn 已被 loader 换掉,理论上碰不到注册表;但 I7 的规矩是"调 apply()
44
+ // 就必须显式声明",不靠副作用 —— 那个桩哪天被改动,跑测试就会真删用户的协议键。
45
+ process.env.DSH_NOTIFY_NO_REGISTRY = '1';
46
+
47
+ const SKIP = !process.env.DSH_ROUTE_LOADER && '需 npm run test:ptc(带 --import loader);默认 node --test 跳过';
48
+
49
+ /** 造最小 ctx 替身(与 wiring / fail-route 同款)。 */
50
+ function mkCtx() {
51
+ const handlers = new Map();
52
+ return {
53
+ handlers,
54
+ on(name, fn) { if (!handlers.has(name)) handlers.set(name, []); handlers.get(name).push(fn); },
55
+ get() { return undefined; },
56
+ effect(fn) { const d = fn(); return () => { if (typeof d === 'function') d(); }; },
57
+ emit(name, ...args) { for (const fn of handlers.get(name) || []) fn(...args); },
58
+ };
59
+ }
60
+
61
+ function mkSession(id, extra = {}) {
62
+ return { id, header: { id, ...extra } };
63
+ }
64
+
65
+ /** 从记录的 spawn 调用里取出所有传给 ps1 的 -Kind 值。 */
66
+ function kindsOf(calls) {
67
+ const out = [];
68
+ for (const c of calls || []) {
69
+ const args = Array.isArray(c.args) ? c.args : [];
70
+ const i = args.indexOf('-Kind');
71
+ if (i >= 0 && i + 1 < args.length) out.push(String(args[i + 1]));
72
+ }
73
+ return out;
74
+ }
75
+
76
+ /**
77
+ * 起一次插件实例,跑 `body`,返回它期间产生的 -Kind 列表。
78
+ * 每个用例独立 apply()(插件本就支持多次 apply,模块缓存共享但处理器是新的一份)。
79
+ */
80
+ async function withPlugin(cfg, body) {
81
+ const { apply } = await import('../lib/index.js');
82
+ const ctx = mkCtx();
83
+ globalThis.__SPAWN_CALLS = [];
84
+ apply(ctx, { voice: true, volume: 0, showToast: true, ...cfg });
85
+ await body(ctx);
86
+ return kindsOf(globalThis.__SPAWN_CALLS);
87
+ }
88
+
89
+ /** 一条 PTC 子调用开始事件(name/arguments 就是真实派发的工具与参数)。 */
90
+ function ptcStart(subCallId, name, args) {
91
+ return {
92
+ type: 'tool/ptc-dispatch-start',
93
+ data: {
94
+ rootCallId: 'root-1',
95
+ parentCallId: 'root-1',
96
+ subCallId,
97
+ name,
98
+ arguments: args,
99
+ },
100
+ };
101
+ }
102
+
103
+ const ASK = { questions: [{ question: '要继续吗', header: '确认', options: [{ label: '是' }, { label: '否' }] }] };
104
+
105
+ test('P0: loader stub 生效(否则本文件的断言无意义)', { skip: SKIP }, () => {
106
+ assert.equal(globalThis.__STUB_CHILD_PROCESS__, true, 'stub 未生效:必须用 npm run test:ptc 跑');
107
+ });
108
+
109
+ test('P1: PTC 子调用提问 → ps1 收到 -Kind question(issue #5 的主症状)', { skip: SKIP }, async () => {
110
+ const sess = mkSession('ptc-sess-1');
111
+ const kinds = await withPlugin({}, (ctx) => {
112
+ ctx.emit('session/event', sess, ptcStart('root-1:ptc:1', 'ask_user_question', ASK));
113
+ });
114
+ assert.deepEqual(kinds, ['question'], `PTC 提问必须播 question,实际 ${JSON.stringify(kinds)}`);
115
+ // 负例:绝不能因为顶层 name 是 run_code 就什么都不播
116
+ assert.notDeepEqual(kinds, [], 'PTC 下提问不得静默');
117
+ });
118
+
119
+ test('P2: PTC 子调用 exit_plan_mode 也走 question(不是 done)', { skip: SKIP }, async () => {
120
+ const sess = mkSession('ptc-sess-2');
121
+ const kinds = await withPlugin({}, (ctx) => {
122
+ ctx.emit('session/event', sess, ptcStart('root-2:ptc:1', 'exit_plan_mode', { plan: '# 计划\n先改 A' }));
123
+ });
124
+ assert.deepEqual(kinds, ['question'], `exit_plan_mode 必须播 question,实际 ${JSON.stringify(kinds)}`);
125
+ });
126
+
127
+ test('P3: PTC 里的普通工具(非提问工具)不得播报 —— 负例', { skip: SKIP }, async () => {
128
+ const sess = mkSession('ptc-sess-3');
129
+ const kinds = await withPlugin({}, (ctx) => {
130
+ ctx.emit('session/event', sess, ptcStart('root-3:ptc:1', 'bash', { command: 'ls' }));
131
+ ctx.emit('session/event', sess, ptcStart('root-3:ptc:2', 'read_file', { path: 'a.txt' }));
132
+ });
133
+ assert.deepEqual(kinds, [], `非提问工具不应播报,实际 ${JSON.stringify(kinds)}`);
134
+ });
135
+
136
+ test('P4: 同一 subCallId 重复派发只播一次(幂等键 = subCallId)', { skip: SKIP }, async () => {
137
+ const sess = mkSession('ptc-sess-4');
138
+ const kinds = await withPlugin({}, (ctx) => {
139
+ const ev = ptcStart('root-4:ptc:1', 'ask_user_question', ASK);
140
+ ctx.emit('session/event', sess, ev);
141
+ ctx.emit('session/event', sess, ev);
142
+ });
143
+ assert.deepEqual(kinds, ['question'], `同 subCallId 只应播一次,实际 ${JSON.stringify(kinds)}`);
144
+ });
145
+
146
+ test('P5: 子调用结算后同类提问可再次提醒(幂等标记必须清)', { skip: SKIP }, async () => {
147
+ const sess = mkSession('ptc-sess-5');
148
+ const id = 'root-5:ptc:1';
149
+ const kinds = await withPlugin({}, (ctx) => {
150
+ ctx.emit('session/event', sess, ptcStart(id, 'ask_user_question', ASK));
151
+ // 子调用结算(内容回到 run_code 程序里)
152
+ ctx.emit('session/event', sess, {
153
+ type: 'tool/ptc-dispatch',
154
+ data: { rootCallId: 'root-5', parentCallId: 'root-5', subCallId: id, name: 'ask_user_question', arguments: ASK, isError: false, content: [] },
155
+ });
156
+ // 同一个 subCallId 再来一次 —— 只有标记被清掉才会再播
157
+ ctx.emit('session/event', sess, ptcStart(id, 'ask_user_question', ASK));
158
+ });
159
+ assert.deepEqual(kinds, ['question', 'question'], `结算后应能再播一次,实际 ${JSON.stringify(kinds)}`);
160
+ });
161
+
162
+ test('P6: notifyOnQuestion=false 时 PTC 提问也不播(开关仍然生效)', { skip: SKIP }, async () => {
163
+ const sess = mkSession('ptc-sess-6');
164
+ const kinds = await withPlugin({ notifyOnQuestion: false }, (ctx) => {
165
+ ctx.emit('session/event', sess, ptcStart('root-6:ptc:1', 'ask_user_question', ASK));
166
+ });
167
+ assert.deepEqual(kinds, [], `关掉提问提醒后不得播报,实际 ${JSON.stringify(kinds)}`);
168
+ });
169
+
170
+ test('P7: 子代理会话里 PTC 提问默认不播(subagents=false 门仍生效)', { skip: SKIP }, async () => {
171
+ const sess = mkSession('ptc-child-1', { origin: 'subagent', parentSession: 'ptc-main-1' });
172
+ const kinds = await withPlugin({}, (ctx) => {
173
+ ctx.emit('session/event', sess, ptcStart('root-7:ptc:1', 'ask_user_question', ASK));
174
+ });
175
+ assert.deepEqual(kinds, [], `子代理的提问默认不播,实际 ${JSON.stringify(kinds)}`);
176
+ });
177
+
178
+ test('P8: 顶层 tool/call 路径没被改坏(回归)', { skip: SKIP }, async () => {
179
+ const sess = mkSession('top-sess-1');
180
+ const kinds = await withPlugin({}, (ctx) => {
181
+ ctx.emit('session/event', sess, {
182
+ type: 'tool/call',
183
+ data: { callId: 'call-1', name: 'ask_user_question', arguments: ASK },
184
+ });
185
+ });
186
+ assert.deepEqual(kinds, ['question'], `顶层提问仍须播报,实际 ${JSON.stringify(kinds)}`);
187
+ });
@@ -0,0 +1,40 @@
1
+ /**
2
+ * 测试环境隔离(`--import` 预加载,在**任何**测试文件之前执行)。
3
+ *
4
+ * ## 为什么必须有这个文件(v0.5.4,2026-11-09 真机踩到)
5
+ *
6
+ * `lib/log-path.js` 决定 `notify.log` 写哪里。测试原本只隔离了 `DSH_HOME`
7
+ * (`test/wiring.test.mjs` / `test/real-cordis.test.mjs` 用 `mkdtempSync`),
8
+ * **没有隔离日志目录** —— 而 `wiring.test.mjs` 会把 `SystemRoot` 换成
9
+ * `Z:\__no_such_root__` 并调用 `apply()`,那些失败的 spawn 会被
10
+ * `appendNotifyLog` 原样写进用户 `%TEMP%\dsh-notify-say\notify.log`。
11
+ *
12
+ * 实测后果:真机日志里出现 12 条
13
+ * `[play-fail] ... spawn Z:\__no_such_root__\...\powershell.exe ENOENT`,
14
+ * 全部集中在跑 `npm test` 的那一秒。后来排查 DSH 0.2.2-rc.1 兼容性时,
15
+ * 这几行足以让人误判「插件在真机上起不来 PowerShell」。
16
+ *
17
+ * ## 用法
18
+ *
19
+ * 只能走 `--import`(不是 `--loader`,后者在新版 Node 已废弃):
20
+ * `node --import ./test/test-env-setup.mjs --test`
21
+ * `package.json` 里所有 `node --test` 脚本都已带上;`test/log-path.test.mjs`
22
+ * 的 L7 会断言这件事,防止有人以后新加脚本时又漏。
23
+ *
24
+ * ⚠️ 本文件**不是**测试文件(不以 `.test.` 命名),默认 `node --test` 不会执行它。
25
+ */
26
+
27
+ import { mkdtempSync, rmSync } from 'node:fs';
28
+ import { tmpdir } from 'node:os';
29
+ import { join } from 'node:path';
30
+
31
+ const LOG_DIR = mkdtempSync(join(tmpdir(), 'notify-test-logs-'));
32
+
33
+ // 宿主 spawn ps1 时会继承 process.env,所以 ps1 侧的 [toast]/[preview]/[tts]
34
+ // 也会落到这个临时目录 —— 正是我们要的:Node 侧与 ps1 侧仍然同口径。
35
+ process.env.DSH_NOTIFY_SAY_LOG_DIR = LOG_DIR;
36
+
37
+ // 进程退出时清掉;测试进程被强杀时残留一个几十字节的目录,无害。
38
+ process.on('exit', () => {
39
+ try { rmSync(LOG_DIR, { recursive: true, force: true }); } catch { /* best effort */ }
40
+ });