dsh-notify-say 0.5.0 → 0.5.2

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,379 @@
1
+ /**
2
+ * 中文语音包的结构与格式测试(v0.5.1)。
3
+ *
4
+ * ## 为什么需要这些测试
5
+ *
6
+ * 中文音色和日语音色走**完全相同的**播放与打包链路,这意味着任何一处
7
+ * 命名错位都会静默失效:
8
+ *
9
+ * - 文件名叫错 → 运行时找不到 → 用户切到中文音色后**没有任何声音**,
10
+ * 而且日志里只有一句「没找到 wav」,很难定位。
11
+ * - 采样率不一致(比如 16000 而不是 24000)→ 播放不变调是运气,
12
+ * 变调了用户只会觉得「这个插件坏了」。
13
+ * - `-extra-N` / `-kansai-N` 后缀归并不对 → 发布时被当成其他音色裁掉。
14
+ *
15
+ * 这三类问题都不会抛异常,只会让功能默默消失。所以必须用断言盯住。
16
+ *
17
+ * ## 与其他测试的分工
18
+ *
19
+ * `voice-label.test.mjs` 管**显示名与打包脚本一致**(防两份名字漂移)。
20
+ * 本文件管**磁盘上的实际文件**是否合规(防文件本身出问题)。
21
+ */
22
+ import { test } from 'node:test';
23
+ import assert from 'node:assert/strict';
24
+ import { readFileSync, existsSync, readdirSync, statSync, openSync, readSync, closeSync, mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
25
+ import { resolve, dirname, join } from 'node:path';
26
+ import { tmpdir } from 'node:os';
27
+ import { fileURLToPath } from 'node:url';
28
+
29
+ const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
30
+ const VOICE_DIR = join(ROOT, 'sounds', 'voice-lines');
31
+ const BUILD_PS1 = join(ROOT, 'tools', 'build-voice-packs.ps1');
32
+ const RELEASE_PS1 = join(ROOT, 'tools', 'release-build.ps1');
33
+
34
+ // ── 两种环境:自用目录 vs 发布包(npm 用户拿到的)──────────────────────────
35
+ //
36
+ // ⚠️ 这不是假设,是实测踩到的:2026-10-23 在 `<scratch>/rel-check`
37
+ // (release-build.ps1 的产物)里跑 `node --test`,Z1/Z2/Z5/Z6/Z7/Z8 六条全红。
38
+ // 原因有两个,都属于「测试假设了开发环境」而不是资产真有问题:
39
+ //
40
+ // 1. 发布包**只带 1 个中文音色**(zh-soft-female,12 个 wav),
41
+ // 而 Z1/Z2 断言四个音色都有文件。
42
+ // 2. 发布包的 `tools\` 里**只有 notify-config.ps1**,没有
43
+ // build-voice-packs.ps1 / release-build.ps1(维护者工具不随包发布),
44
+ // 所以 Z5~Z8 读不到目标文件。
45
+ //
46
+ // 外部用户在 GitHub 上跑 `npm test` 看到六条红灯,会以为插件坏了。
47
+ // 处理方式与 docs-consistency.test.mjs 的 D6/D7/D8b 完全一致:// · 结构性断言(Z1/Z2)改为**只校验磁盘上实际存在的音色子集**;
48
+ // · 维护者工具相关断言(Z5~Z8)用 `skip` 明确跳过,不伪装成通过。
49
+
50
+ /** 发布包里没有维护者打包脚本 → 这些断言无从执行(跳过,不算通过)。 */
51
+ const SKIP_DEVONLY = existsSync(BUILD_PS1) && existsSync(RELEASE_PS1)
52
+ ? false
53
+ : 'build-voice-packs.ps1 / release-build.ps1 是维护者工具,不在发布包内';
54
+
55
+ /**
56
+ * 当前环境「必须存在」的中文音色。
57
+ *
58
+ * 这是本文件的核心判据,2026-10-23 第二轮审查后重写。
59
+ *
60
+ * ⚠️ 上一版是 `presentZhVoices()`:遍历「磁盘上有文件的音色」。审查实测抓到
61
+ * 它的致命退化 —— 把 `zh-mid-male` 整组 12 个 wav 全删,测试**依然全绿**,
62
+ * 因为该音色自动退出了断言范围。判据从「必须存在」变成了「存在的必须合规」。
63
+ * 而 `lib/client.js` 的音色下拉用的是**全量 VOICE_KEYS**(不按已安装过滤),
64
+ * 缺文件的音色照样出现在设置页,用户选中即静音 —— 这正是最该防的静默故障。
65
+ *
66
+ * 现在的判据分两种环境,都是显式的:
67
+ * · 自用目录(有维护者脚本)→ **4 个必须全在**。删任何一组都红。
68
+ * · 发布包(无维护者脚本)→ release-build.ps1 只带 1 个,所以「至少 1 个」,
69
+ * 但**有文件的那组必须四池齐全**(半缺一定红)。
70
+ *
71
+ * 为什么不按审查建议的「要么齐全要么整组缺席」:那个规则在自用目录里删整组
72
+ * 同样是绿的,等于没修这个缺陷。
73
+ */
74
+ function requiredZhVoices() {
75
+ const all = Object.keys(ZH_VOICES);
76
+ if (existsSync(RELEASE_PS1)) return all; // 自用目录:一个都不能少
77
+ const present = all.filter((v) => hasAnyWav(v));
78
+ return present; // 发布包:有文件的那几组
79
+ }
80
+
81
+ /** 某个音色在任意池里有 wav 吗。 */
82
+ function hasAnyWav(voice) {
83
+ return Object.keys(POOLS).some((pool) => wavsInPool(voice, pool).length > 0);
84
+ }
85
+
86
+ /** 中文音色 key → 期望的中文名(与 build-voice-packs.ps1 的 Name 同源)。 */
87
+ const ZH_VOICES = {
88
+ 'zh-low-male': '低沉男声',
89
+ 'zh-mid-male': '平稳男声',
90
+ 'zh-clear-female': '清亮女声',
91
+ 'zh-soft-female': '温柔女声',
92
+ };
93
+
94
+ /** 四个池:done 在根目录,其余在子目录。 */
95
+ const POOLS = Object.freeze({
96
+ done: '',
97
+ question: 'question',
98
+ approval: 'approval',
99
+ fail: 'fail',
100
+ });
101
+
102
+ /** 读取 WAV 头,返回 { sampleRate, channels, bitsPerSample }。 */
103
+ function readWavHeader(path) {
104
+ const fd = openSync(path, 'r');
105
+ try {
106
+ const buf = Buffer.alloc(44);
107
+ readSync(fd, buf, 0, 44, 0);
108
+ assert.equal(buf.toString('ascii', 0, 4), 'RIFF', `${path} 不是 RIFF`);
109
+ assert.equal(buf.toString('ascii', 8, 12), 'WAVE', `${path} 不是 WAVE`);
110
+ return {
111
+ channels: buf.readUInt16LE(22),
112
+ sampleRate: buf.readUInt32LE(24),
113
+ bitsPerSample: buf.readUInt16LE(34),
114
+ };
115
+ } finally {
116
+ closeSync(fd);
117
+ }
118
+ }
119
+
120
+ /** 某个音色在某个池里的全部 wav 文件名(按池规则)。 */
121
+ function wavsInPool(voice, pool, voiceDir = VOICE_DIR) {
122
+ const dir = POOLS[pool] ? join(voiceDir, POOLS[pool]) : voiceDir;
123
+ if (!existsSync(dir)) return [];
124
+ const files = readdirSync(dir).filter((f) => /\.wav$/i.test(f));
125
+ if (pool === 'done') {
126
+ // done 池:<voice>.wav / <voice>-extra-N.wav
127
+ return files.filter((f) =>
128
+ f === `${voice}.wav` || new RegExp(`^${voice}-extra-\\d+\\.wav$`).test(f));
129
+ }
130
+ // 子池:<voice>~N.wav
131
+ return files.filter((f) => new RegExp(`^${voice}~\\d+\\.wav$`).test(f));
132
+ }
133
+
134
+ /**
135
+ * 纯判据:给定一个语音目录,返回「缺池」问题列表。**不读磁盘以外的任何状态。**
136
+ *
137
+ * 抽成纯函数是为了让 Z11 能拿临时目录直接测它 —— 上一版把判据写成
138
+ * 源码文本切片断言(Z9),审查实测既能绕过(加一行 `void presentZhVoices;`
139
+ * 就骗过)又会误报(加一段无关遍历就红)。行为断言才是对的形态。
140
+ *
141
+ * @param {string} voiceDir 语音根目录
142
+ * @param {string[]} required 必须存在的音色 key
143
+ * @returns {string[]} 问题描述,空数组表示通过
144
+ */
145
+ function findIncompleteVoices(voiceDir, required) {
146
+ const problems = [];
147
+ if (required.length === 0) {
148
+ problems.push(`一个中文音色都没找到(${voiceDir})—— 资产真的丢了,不是环境差异`);
149
+ return problems;
150
+ }
151
+ for (const voice of required) {
152
+ const per = {};
153
+ for (const pool of Object.keys(POOLS)) {
154
+ per[pool] = wavsInPool(voice, pool, voiceDir).length;
155
+ }
156
+ const missing = Object.keys(POOLS).filter((pool) => per[pool] === 0);
157
+ if (missing.length === Object.keys(POOLS).length) {
158
+ problems.push(`${voice}: 整组缺席 —— 但设置页的音色下拉是全量 VOICE_KEYS,`
159
+ + `用户选中它会没有任何声音`);
160
+ } else if (missing.length > 0) {
161
+ problems.push(`${voice}: 缺 ${missing.join('/')} 池`
162
+ + `(done=${per.done} q=${per.question} a=${per.approval} f=${per.fail})`);
163
+ }
164
+ }
165
+ return problems;
166
+ }
167
+
168
+ // ── Z1: 必须存在的中文音色,每个池都要有文件 ──────────────────────────────
169
+ test('Z1: 必须存在的中文音色在四个池里都有 wav', () => {
170
+ const required = requiredZhVoices();
171
+ const problems = findIncompleteVoices(VOICE_DIR, required);
172
+ assert.deepEqual(problems, [],
173
+ '中文音色缺文件 —— 运行时找不到会**静默无声**:\n' + problems.join('\n'));
174
+ });
175
+
176
+ // ── Z2: 每个音色恰好 12 条(done 3 + 其余各 3) ────────────────────────────
177
+ test('Z2: 每个中文音色正好 12 条台词', () => {
178
+ const problems = [];
179
+ for (const voice of requiredZhVoices()) {
180
+ const per = {};
181
+ let total = 0;
182
+ for (const pool of Object.keys(POOLS)) {
183
+ per[pool] = wavsInPool(voice, pool).length;
184
+ total += per[pool];
185
+ }
186
+ if (total !== 12) {
187
+ problems.push(`${voice}: 共 ${total} 条(done=${per.done} q=${per.question} a=${per.approval} f=${per.fail})`);
188
+ }
189
+ // 每个池至少 2 条,否则随机轮换等于没有
190
+ for (const pool of ['question', 'approval', 'fail']) {
191
+ if (per[pool] < 2) problems.push(`${voice}/${pool}: 只有 ${per[pool]} 条,轮换不起来`);
192
+ }
193
+ }
194
+ assert.deepEqual(problems, [], problems.join('\n'));
195
+ });
196
+
197
+ // ── Z3: 格式与日语语音包一致(24000 Hz / 单声道 / 16 bit) ─────────────────
198
+ test('Z3: 中文 wav 的格式与现有日语语音包一致', () => {
199
+ const bad = [];
200
+ let checked = 0;
201
+ for (const voice of requiredZhVoices()) {
202
+ for (const pool of Object.keys(POOLS)) {
203
+ const dir = POOLS[pool] ? join(VOICE_DIR, POOLS[pool]) : VOICE_DIR;
204
+ for (const f of wavsInPool(voice, pool)) {
205
+ const h = readWavHeader(join(dir, f));
206
+ checked += 1;
207
+ if (h.sampleRate !== 24000 || h.channels !== 1 || h.bitsPerSample !== 16) {
208
+ bad.push(`${pool || 'done'}/${f}: ${h.sampleRate}Hz/${h.channels}ch/${h.bitsPerSample}bit`);
209
+ }
210
+ }
211
+ }
212
+ }
213
+ assert.ok(checked > 0, '没有检查到任何中文 wav');
214
+ assert.deepEqual(bad, [],
215
+ '格式与日语包不一致,播放可能变调或失败(期望 24000Hz/1ch/16bit):\n' + bad.join('\n'));
216
+ });
217
+
218
+ // ── Z4: 与日语包的量级接近(防止混进长音频) ──────────────────────────────
219
+ test('Z4: 单条中文 wav 时长在合理区间(0.8 ~ 4.0 秒)', () => {
220
+ // 只算数据字节数推算时长:PCM_16/24kHz/单声道 → 每秒 48000 字节 + 44 头
221
+ const BYTES_PER_SEC = 24000 * 1 * 2;
222
+ const bad = [];
223
+ for (const voice of requiredZhVoices()) {
224
+ for (const pool of Object.keys(POOLS)) {
225
+ const dir = POOLS[pool] ? join(VOICE_DIR, POOLS[pool]) : VOICE_DIR;
226
+ for (const f of wavsInPool(voice, pool)) {
227
+ const size = statSync(join(dir, f)).size;
228
+ const sec = (size - 44) / BYTES_PER_SEC;
229
+ if (sec < 0.8 || sec > 4.0) bad.push(`${pool || 'done'}/${f}: ${sec.toFixed(2)}s`);
230
+ }
231
+ }
232
+ }
233
+ assert.deepEqual(bad, [],
234
+ '时长异常 —— 过短可能是空文件,过长会让提醒很吵:\n' + bad.join('\n'));
235
+ });
236
+
237
+ // ── Z5: 打包脚本认得这四个音色(否则打不出下载包) ────────────────────────
238
+ test('Z5: build-voice-packs.ps1 的审核清单含四个中文音色', { skip: SKIP_DEVONLY }, () => {
239
+ const src = readFileSync(BUILD_PS1, 'utf8');
240
+ const missing = [];
241
+ for (const [key, name] of Object.entries(ZH_VOICES)) {
242
+ // 形如:'zh-low-male' = @{ Name = '低沉男声'; ...
243
+ const re = new RegExp(`'${key}'\\s*=\\s*@\\{\\s*Name\\s*=\\s*'${name}'`);
244
+ if (!re.test(src)) missing.push(`${key}(期望名「${name}」)`);
245
+ }
246
+ assert.deepEqual(missing, [],
247
+ '打包脚本不认这些音色 → 用户下载不到:\n' + missing.join('\n'));
248
+ });
249
+
250
+ // ── Z6: 中文包的 TERMS.md 不能写成 VOICEVOX 生成 ──────────────────────────
251
+ test('Z6: 打包脚本按引擎写 TERMS.md,中文包不冒用 VOICEVOX 署名', { skip: SKIP_DEVONLY }, () => {
252
+ // 这是**诚实性**断言,不是格式断言。
253
+ // 中文音频由 EmotiVoice 合成,若 TERMS.md 里写「由 VOICEVOX 生成」,
254
+ // 就是对用户和权利人的双重误导。
255
+ const src = readFileSync(BUILD_PS1, 'utf8');
256
+ assert.match(src, /Engine\s*=\s*'EmotiVoice'/, '中文音色应标注 Engine');
257
+ assert.match(src, /if\s*\(\s*\$meta\.Engine\s*-eq\s*'EmotiVoice'\s*\)/,
258
+ 'TERMS.md 生成必须按引擎分支');
259
+ // 分支里提到 EmotiVoice,且提到 Apache-2.0 与 CC BY 4.0 两层许可
260
+ const emoBlock = src.slice(src.indexOf("$meta.Engine -eq 'EmotiVoice'"));
261
+ const block = emoBlock.slice(0, emoBlock.indexOf('} else {'));
262
+ assert.match(block, /EmotiVoice/, '中文分支应说明由 EmotiVoice 生成');
263
+ assert.match(block, /Apache/, '应写明 Apache-2.0');
264
+ assert.match(block, /CC BY 4\.0/, '应写明训练语料的 CC BY 4.0');
265
+ assert.doesNotMatch(block, /VOICEVOX/, '中文分支里不应出现 VOICEVOX');
266
+ });
267
+
268
+ // ── Z7: 发布脚本的 key 归并能认出 -extra-N ────────────────────────────────
269
+ test('Z7: release-build.ps1 带宽变体后缀归并,不会把 -extra-N 当成别的音色', { skip: SKIP_DEVONLY }, () => {
270
+ const src = readFileSync(RELEASE_PS1, 'utf8');
271
+ // 必须有 `-(extra|kansai)-N` 这一道,否则 zundamon-normal-extra-1 会被裁掉
272
+ assert.match(src, /-replace\s+'-\(\?:extra\|kansai\)-\\d\+\$'/,
273
+ 'release-build.ps1 必须归并 -extra-N / -kansai-N 后缀(v0.5.0 曾因此丢 5 个自带 wav)');
274
+ // 且必须仍保留 ~N 那一道(子池编号)
275
+ assert.match(src, /-replace\s+'~\\d\+\$'/,
276
+ '仍需归并子池的 ~N 后缀');
277
+ });
278
+
279
+ // ── Z8: 自带中文音色的默认 key 与 client.js 的说法一致 ────────────────────
280
+ test('Z8: 发布脚本默认自带的中文音色是 zh-soft-female', { skip: SKIP_DEVONLY }, () => {
281
+ const src = readFileSync(RELEASE_PS1, 'utf8');
282
+ assert.match(src, /\$VoiceKey\s*=\s*@\([^)]*'zh-soft-female'/,
283
+ '默认应自带一个中文音色(温柔女声),否则中文用户开箱听到的是日语包念中文');
284
+ });
285
+
286
+ // ── Z11: 判据本身要能失败(行为断言,替代旧的文本切片元测试)──────────────
287
+ test('Z11: findIncompleteVoices 对「整组缺席」和「半缺」都必须报错', () => {
288
+ // ## 为什么这条取代了原来的 Z9
289
+ //
290
+ // 旧 Z9 拿 `src.slice(indexOf("test('Z1"), indexOf("test('Z5"))` 切源码文本,
291
+ // 断言里面出现 `presentZhVoices()` 字样。2026-10-23 只读审查实测出两个绕过 +
292
+ // 一个误报:
293
+ // · 绕过 A:`const voices = Object.keys(ZH_VOICES).slice();` + 一行
294
+ // `void presentZhVoices;` → 语义回到全量,Z9 仍绿。
295
+ // · 绕过 B:把 presentZhVoices() 函数体改成 return 全量 key → Z9 仍绿
296
+ // (函数定义落在切片之外)。
297
+ // · 误报:在 Z1 里加一段与资产无关的 Object.keys(ZH_VOICES) 遍历 → Z9 立刻红。
298
+ //
299
+ // 结论:文本切片元测试不适合当"判据形态守卫"。改成**行为断言** ——
300
+ // 造临时目录,直接看判据函数在该报错时到底报不报。
301
+ const tmp = mkdtempSync(join(tmpdir(), 'zh-voice-probe-'));
302
+
303
+ /** 在临时目录里造一个音色的四池 wav(内容是垃圾,这里只验文件是否存在)。 */
304
+ const makeVoice = (dir, voice, pools) => {
305
+ for (const pool of pools) {
306
+ const sub = POOLS[pool] ? join(dir, POOLS[pool]) : dir;
307
+ mkdirSync(sub, { recursive: true });
308
+ const name = pool === 'done' ? `${voice}.wav` : `${voice}~1.wav`;
309
+ writeFileSync(join(sub, name), 'RIFF----WAVEfmt ');
310
+ }
311
+ };
312
+
313
+ try {
314
+ const ALL = Object.keys(POOLS);
315
+
316
+ // ① 齐全 → 不报错
317
+ const okDir = join(tmp, 'ok');
318
+ makeVoice(okDir, 'zh-a', ALL);
319
+ assert.deepEqual(findIncompleteVoices(okDir, ['zh-a']), [],
320
+ '四池齐全时不该报错');
321
+
322
+ // ② 整组缺席 → 必须报错(这是旧子集判据放过的那类真缺陷)
323
+ const goneDir = join(tmp, 'gone');
324
+ makeVoice(goneDir, 'zh-b', ALL); // 只有 zh-b,断言要求 zh-a
325
+ const goneProblems = findIncompleteVoices(goneDir, ['zh-a']);
326
+ assert.ok(goneProblems.length > 0, '整组缺席必须报错');
327
+ assert.match(goneProblems.join('\n'), /zh-a.*整组缺席/s,
328
+ '整组缺席的错误信息要指名道姓,否则排查时看不出少了谁');
329
+
330
+ // ③ 半缺(有 done 缺 fail)→ 必须报错
331
+ const halfDir = join(tmp, 'half');
332
+ makeVoice(halfDir, 'zh-c', ['done', 'question', 'approval']);
333
+ const halfProblems = findIncompleteVoices(halfDir, ['zh-c']);
334
+ assert.ok(halfProblems.length > 0, '半缺必须报错');
335
+ assert.match(halfProblems.join('\n'), /zh-c.*缺\s*fail/s,
336
+ '半缺的错误信息要指出缺哪个池');
337
+
338
+ // ④ 空要求 → 报「资产真的丢了」,不是静默通过
339
+ const emptyProblems = findIncompleteVoices(join(tmp, 'nope'), []);
340
+ assert.equal(emptyProblems.length, 1, '一个必需音色都没有时必须报错,不能空过');
341
+ assert.match(emptyProblems[0], /资产真的丢了/);
342
+
343
+ // ⑤ 发布包形态(只剩 1 个音色)不该误报 —— 这是当初引入子集判据的原因
344
+ const packDir = join(tmp, 'pack');
345
+ makeVoice(packDir, 'zh-soft-female', ALL);
346
+ assert.deepEqual(findIncompleteVoices(packDir, ['zh-soft-female']), [],
347
+ '发布包只带 1 个音色时不该报错');
348
+ } finally {
349
+ rmSync(tmp, { recursive: true, force: true });
350
+ }
351
+ });
352
+
353
+ // ── Z12: 自用目录必须 4 个中文音色全在 ────────────────────────────────────
354
+ test('Z12: 自用目录里 4 个中文音色一个都不能少', { skip: SKIP_DEVONLY }, () => {
355
+ // Z11 证明「判据函数」会对整组缺席报错;本条证明「本环境喂给它的要求」是对的。
356
+ // 两件事分开测:判据对 ≠ 输入对。
357
+ const required = requiredZhVoices();
358
+ assert.deepEqual([...required].sort(), Object.keys(ZH_VOICES).sort(),
359
+ '维护者环境必须 4 个中文音色齐全,否则打包时会静默漏传');
360
+ });
361
+
362
+ // ── Z10: GUI 的授权提示必须提到中文音色的引擎 ─────────────────────────────
363
+ test('Z10: GUI 授权提示写上中文语音由 EmotiVoice 生成', () => {
364
+ // 为什么这条要独立存在:
365
+ //
366
+ // `client-contract.test.mjs` 的 C33 断言的是 `/VOICEVOX 生成/`。加了中文音色后,
367
+ // 那句文案改成了「日语语音由 VOICEVOX 生成,中文语音由 EmotiVoice 生成」——
368
+ // C33 依然匹配(子串命中),但它**只约束了日语那一半**:如果哪天有人把
369
+ // 「中文语音由 EmotiVoice 生成」删掉、只留日语半句,C33 还是绿的。
370
+ //
371
+ // 而这不是措辞问题:EmotiVoice 是 Apache-2.0,VOICEVOX 音色是各自条款,
372
+ // 把中文音色冒称成 VOICEVOX 生成,就是署名错误(诚实性约束)。
373
+ // 所以这里补一条只盯中文那一半的断言。C33 保持原样,两者互补。
374
+ const client = readFileSync(join(ROOT, 'lib', 'client.js'), 'utf8');
375
+ assert.match(client, /中文语音由 EmotiVoice 生成/,
376
+ 'GUI 必须如实写明中文语音由 EmotiVoice 生成,不能只写 VOICEVOX');
377
+ assert.doesNotMatch(client, /这些语音由 VOICEVOX 生成/,
378
+ '不能再笼统地说「这些语音由 VOICEVOX 生成」—— 中文音色不是 VOICEVOX 生成的');
379
+ });