dsh-notify-say 0.5.2 → 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.
@@ -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
+ });
@@ -0,0 +1,238 @@
1
+ /**
2
+ * 音色回退的语言一致性(issue #1 回归)。
3
+ *
4
+ * ## 这条测试守的是什么
5
+ *
6
+ * issue #1(dustinmoon78,2026-10-25):用户在设置页选了**中文**音色
7
+ * `zh-clear-female`,而发布包只自带两个音色(日语 `zundamon-normal` +
8
+ * 中文 `zh-soft-female`)。原选曲链找不到指定音色时一路落到
9
+ * `sounds/master-voice.wav` —— 那文件与 `zundamon-normal.wav` 是同一个文件
10
+ * (sha256 相同),说的是「主人、任務完了です!」。
11
+ *
12
+ * 于是:用户明确点了中文,听到的是日语;而且**全程没有一行日志**,
13
+ * 用户只能凭耳朵发现"怎么变日语了",提 issue 时也拿不出证据。
14
+ *
15
+ * ## 修复后的契约(三段,逐条断言)
16
+ *
17
+ * ① **同语系优先**:点选的音色取不到时,先在同一个池里换一个**同语言**的音色,
18
+ * 再考虑任何跨语言的兜底。中文请求绝不该先嫁接到日语上。
19
+ * ② **不越池**:`fail` 仍然是 `fail` 池(不得跑到 done 池去播庆祝台词)——
20
+ * 这是 2026-10-17 那条更早的护栏,新逻辑不能把它弄破。
21
+ * ③ **留痕**:真的换了音色时,日志里必须有一行 `[voice-miss]`,
22
+ * 且 `-Preview` 的输出里带 `[voice-miss]` 标记。
23
+ *
24
+ * ## 为什么这里只用"发布包那套音色"来测
25
+ *
26
+ * 自用目录里 17 个音色全在,`zh-clear-female` 是**装了**的,那条代码路径根本
27
+ * 走不到(实测:自用目录下 `-Voice zh-clear-female` 直接选中它,不会回退)。
28
+ * 用户踩坑的是**发布包**。所以本文件用 `-VoiceRoot` 指向一个只含
29
+ * `zundamon-normal` + `zh-soft-female` 的临时根,复刻发布包形态。
30
+ *
31
+ * ⚠️ 不能用 `-Voice` 指向真实不存在的 key 来测语言回退:那种情况下
32
+ * `Get-VoiceLangOf` 只能按 `zh-` 前缀猜,虽然也能过,但**测不到**
33
+ * "用户点的是真实存在、只是没装的音色"这个真实场景。
34
+ *
35
+ * ⚠️ worktree 长路径:MCI 在 >130 字符的路径上 open 返回 304,
36
+ * 所以本文件**只断言 pick= 字符串**,不断言真的出声(同 fail-voice.test.mjs 的说明)。
37
+ *
38
+ * 运行:node --import ./test/test-env-setup.mjs --test test/voice-fallback.test.mjs
39
+ */
40
+ import { test } from 'node:test';
41
+ import assert from 'node:assert/strict';
42
+ import { spawnSync } from 'node:child_process';
43
+ import { cpSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, existsSync } from 'node:fs';
44
+ import { tmpdir } from 'node:os';
45
+ import { fileURLToPath } from 'node:url';
46
+ import { dirname, join } from 'node:path';
47
+
48
+ const HERE = dirname(fileURLToPath(import.meta.url));
49
+ const PLUGIN = join(HERE, '..');
50
+ const PS1 = join(PLUGIN, 'dsh-notify.ps1');
51
+ const REAL_SOUNDS = join(PLUGIN, 'sounds');
52
+
53
+ /** 发布包里真实存在的两个音色(见 release-build.ps1 的默认自带清单)。 */
54
+ const SHIPPED_JA = 'zundamon-normal';
55
+ const SHIPPED_ZH = 'zh-soft-female';
56
+ /** 自用目录有、发布包**没有**的中文音色 —— issue #1 里用户点的就是这个。 */
57
+ const NOT_SHIPPED_ZH = 'zh-clear-female';
58
+
59
+ /**
60
+ * 造一个"发布包形态"的语音根:只有 zundamon-normal 与 zh-soft-female 两族。
61
+ *
62
+ * 直接从真实 sounds 目录**按音色前缀复制**,不重新合成 —— 音频内容无关紧要,
63
+ * 本测试只断言"选中了哪个文件名"。
64
+ */
65
+ function makeShippedVoiceRoot() {
66
+ const root = mkdtempSync(join(tmpdir(), 'ns-shipped-'));
67
+ const dest = join(root, 'voice-lines');
68
+ mkdirSync(dest, { recursive: true });
69
+ for (const pool of ['', 'fail', 'question', 'approval']) {
70
+ const srcDir = pool ? join(REAL_SOUNDS, 'voice-lines', pool) : join(REAL_SOUNDS, 'voice-lines');
71
+ if (!existsSync(srcDir)) continue;
72
+ const outDir = pool ? join(dest, pool) : dest;
73
+ mkdirSync(outDir, { recursive: true });
74
+ for (const key of [SHIPPED_JA, SHIPPED_ZH]) {
75
+ // 该池里此音色的所有变体(<key>.wav 与 <key>~N.wav)
76
+ for (const f of readdirSync(srcDir)) {
77
+ if (!f.endsWith('.wav')) continue;
78
+ const base = f.replace(/~.*$/, '').replace(/\.wav$/, '');
79
+ if (base !== key) continue;
80
+ cpSync(join(srcDir, f), join(outDir, f));
81
+ }
82
+ }
83
+ }
84
+ return root;
85
+ }
86
+
87
+ /** 跑 ps1 -Preview,返回 stdout / stderr / exit code。用 spawnSync(见 fail-voice.test.mjs 踩坑③)。 */
88
+ function runPs1(args, logDir) {
89
+ const env = { ...process.env };
90
+ if (logDir) env.DSH_NOTIFY_SAY_LOG_DIR = logDir;
91
+ const r = spawnSync('powershell',
92
+ ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', PS1, ...args],
93
+ { encoding: 'utf8', timeout: 60_000, env });
94
+ return {
95
+ out: String(r.stdout || ''),
96
+ err: String(r.stderr || ''),
97
+ code: typeof r.status === 'number' ? r.status : 1,
98
+ };
99
+ }
100
+
101
+ /** 从输出里提取 pick=<name>。 */
102
+ function pickOf(out) {
103
+ const m = /pick=([^\s+]+)/.exec(out.replace('(静音)', ''));
104
+ return m ? m[1] : '';
105
+ }
106
+
107
+ /** 文件名 → 音色 key(去掉 ~N 变体后缀与 .wav)。 */
108
+ function keyOf(file) {
109
+ return file.replace(/~.*$/, '').replace(/\.wav$/, '');
110
+ }
111
+
112
+ /** 某音色 key 是否属于中文(与 ps1 / client.js 同一命名契约:zh- 前缀)。 */
113
+ function isZh(key) {
114
+ return key.startsWith('zh-');
115
+ }
116
+
117
+ // ── 1. 同语系回退:中文请求不该先落到日语 ──────────────────────────────────
118
+ test('F1: 发布包形态下点未安装的中文音色 → 退到**中文**已装音色,不是 master-voice', () => {
119
+ const root = makeShippedVoiceRoot();
120
+ try {
121
+ const { out, err, code } = runPs1(
122
+ ['-Kind', 'done', '-Preview', '-NoSound', '-Voice', NOT_SHIPPED_ZH, '-VoiceRoot', join(root, 'voice-lines'), '-Volume', '0'],
123
+ );
124
+ assert.equal(code, 0, `ps1 应正常退出;stderr=${err.slice(0, 300)}`);
125
+ const pick = pickOf(out);
126
+ assert.notEqual(pick, '', `应选到一个文件,实际输出=${out}`);
127
+ assert.equal(isZh(keyOf(pick)), true,
128
+ `点中文音色时应播中文,实际 pick=${pick}(master-voice 是日语「任務完了です」,正是 issue #1)`);
129
+ assert.notEqual(pick, 'master-voice.wav', '不得落到 master-voice.wav');
130
+ } finally {
131
+ rmSync(root, { recursive: true, force: true });
132
+ }
133
+ });
134
+
135
+ // ── 2. 四个 kind 都要覆盖(issue #1 只写了 done,实际四类都有)────────────────
136
+ for (const kind of ['done', 'question', 'approval', 'fail']) {
137
+ test(`F2-${kind}: -Kind ${kind} + 未安装中文音色 → 仍是中文(四类都不得跨语言)`, () => {
138
+ const root = makeShippedVoiceRoot();
139
+ try {
140
+ const { out, code } = runPs1(
141
+ ['-Kind', kind, '-Preview', '-NoSound', '-Voice', NOT_SHIPPED_ZH, '-VoiceRoot', join(root, 'voice-lines'), '-Volume', '0'],
142
+ );
143
+ assert.equal(code, 0, 'ps1 应正常退出');
144
+ const pick = pickOf(out);
145
+ assert.equal(isZh(keyOf(pick)), true,
146
+ `kind=${kind} 点中文却播了 ${pick};四类都必须同语系回退`);
147
+ } finally {
148
+ rmSync(root, { recursive: true, force: true });
149
+ }
150
+ });
151
+ }
152
+
153
+ // ── 3. 不越池:fail 不得跑到 done 池去播庆祝台词 ────────────────────────────
154
+ test('F3: fail 池缺该中文音色时,仍在 fail 池内换中文音色(不越到 done 池)', () => {
155
+ const root = makeShippedVoiceRoot();
156
+ try {
157
+ const { out, code } = runPs1(
158
+ ['-Kind', 'fail', '-Preview', '-NoSound', '-Voice', NOT_SHIPPED_ZH, '-VoiceRoot', join(root, 'voice-lines'), '-Volume', '0'],
159
+ );
160
+ assert.equal(code, 0, 'ps1 应正常退出');
161
+ const pick = pickOf(out);
162
+ // done 池根目录的 <key>.wav(无 ~N)是"任务完成"台词;fail 必须选带 ~N 的子池变体
163
+ assert.match(pick, /~\d+\.wav$/, `fail 必须留在 fail 池(~N 变体),实际 pick=${pick}`);
164
+ assert.doesNotMatch(pick, /master-voice/, 'fail 不得回落到 master-voice(语义相反)');
165
+ } finally {
166
+ rmSync(root, { recursive: true, force: true });
167
+ }
168
+ });
169
+
170
+ // ── 4. 留痕:换音色必须写 [voice-miss] ──────────────────────────────────────
171
+ test('F4: 真的换了音色时,日志写 [voice-miss] 且 Preview 输出带标记', () => {
172
+ const root = makeShippedVoiceRoot();
173
+ const logDir = mkdtempSync(join(tmpdir(), 'ns-miss-'));
174
+ try {
175
+ const { out, code } = runPs1(
176
+ ['-Kind', 'done', '-Preview', '-NoSound', '-Voice', NOT_SHIPPED_ZH, '-VoiceRoot', join(root, 'voice-lines'), '-Volume', '0'],
177
+ logDir,
178
+ );
179
+ assert.equal(code, 0, 'ps1 应正常退出');
180
+ assert.match(out, /\[voice-miss\]/, 'Preview 输出必须带 [voice-miss](用户当场就能看出换过音色)');
181
+ const logFile = join(logDir, 'notify.log');
182
+ assert.equal(existsSync(logFile), true, `日志文件必须存在:${logFile}`);
183
+ const log = readFileSync(logFile, 'utf8');
184
+ assert.match(log, /\[voice-miss\]/, '日志必须有一行 [voice-miss]');
185
+ assert.match(log, new RegExp(`requested=${NOT_SHIPPED_ZH}`),
186
+ '日志必须写明用户请求的是哪个音色');
187
+ } finally {
188
+ rmSync(root, { recursive: true, force: true });
189
+ rmSync(logDir, { recursive: true, force: true });
190
+ }
191
+ });
192
+
193
+ // ── 5. 没换音色时不得误报(否则日志会被噪声淹没,等于没留痕)────────────────
194
+ test('F5: 点的是已装音色 → 不写 [voice-miss](避免误报把留痕淹掉)', () => {
195
+ const root = makeShippedVoiceRoot();
196
+ const logDir = mkdtempSync(join(tmpdir(), 'ns-nomiss-'));
197
+ try {
198
+ const { out, code } = runPs1(
199
+ ['-Kind', 'done', '-Preview', '-NoSound', '-Voice', SHIPPED_ZH, '-VoiceRoot', join(root, 'voice-lines'), '-Volume', '0'],
200
+ logDir,
201
+ );
202
+ assert.equal(code, 0, 'ps1 应正常退出');
203
+ assert.equal(keyOf(pickOf(out)), SHIPPED_ZH, `应精确选中 ${SHIPPED_ZH},实际 ${pickOf(out)}`);
204
+ assert.doesNotMatch(out, /\[voice-miss\]/, '选中了请求的音色时不该报 miss');
205
+ const log = readFileSync(join(logDir, 'notify.log'), 'utf8');
206
+ assert.doesNotMatch(log, /\[voice-miss\]/, '选中了请求的音色时不该写 miss 日志');
207
+ } finally {
208
+ rmSync(root, { recursive: true, force: true });
209
+ rmSync(logDir, { recursive: true, force: true });
210
+ }
211
+ });
212
+
213
+ // ── 6. 反向:日语请求不该被送到中文去 ──────────────────────────────────────
214
+ test('F6: 点未安装的日语音色 → 退到日语已装音色,不是中文', () => {
215
+ const root = makeShippedVoiceRoot();
216
+ try {
217
+ const { out, code } = runPs1(
218
+ ['-Kind', 'done', '-Preview', '-NoSound', '-Voice', 'ameharu-hau', '-VoiceRoot', join(root, 'voice-lines'), '-Volume', '0'],
219
+ );
220
+ assert.equal(code, 0, 'ps1 应正常退出');
221
+ const pick = pickOf(out);
222
+ assert.equal(isZh(keyOf(pick)), false,
223
+ `点日语却播了中文 ${pick};同语系回退是双向的`);
224
+ } finally {
225
+ rmSync(root, { recursive: true, force: true });
226
+ }
227
+ });
228
+
229
+ // ── 7. 契约漂移守卫:ps1 与 client.js 必须用同一套语言判据 ──────────────────
230
+ test('F7: 语言判据在 ps1 与 client.js 之间不漂移(都按 zh- 前缀)', () => {
231
+ const ps1 = readFileSync(PS1, 'utf8');
232
+ // ps1 侧:Get-VoiceLangOf 必须按 zh- 前缀判,而不是维护第二份清单
233
+ assert.match(ps1, /function Get-VoiceLangOf/, 'ps1 必须有 Get-VoiceLangOf');
234
+ assert.match(ps1, /if \(\$Key -like 'zh-\*'\) \{ return 'zh' \}/,
235
+ 'Get-VoiceLangOf 必须按 zh- 前缀判中文(清单会漂移,前缀不会)');
236
+ // 同语系回退必须用**不退回未过滤**的那个取法
237
+ assert.match(ps1, /function Pick-RandomWavInLang/, 'ps1 必须有 Pick-RandomWavInLang');
238
+ });