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.
- package/CHANGELOG.md +29 -2
- package/README.md +19 -9
- package/dsh-notify.ps1 +130 -7
- package/lib/client.js +143 -5
- package/lib/config-route.js +22 -0
- package/lib/config-schema.js +16 -0
- package/lib/config-store.js +6 -0
- package/lib/diagnostics.js +6 -2
- package/lib/index.js +266 -16
- package/lib/log-path.js +60 -0
- package/package.json +81 -77
- package/test/config-route.test.mjs +65 -0
- package/test/diagnostics.test.mjs +10 -0
- package/test/docs-consistency.test.mjs +230 -0
- package/test/fail-voice.test.mjs +140 -1
- package/test/log-path.test.mjs +127 -0
- package/test/ptc-question.test.mjs +187 -0
- package/test/test-env-setup.mjs +40 -0
- package/test/voice-fallback.test.mjs +238 -0
- package/test/voice-label.test.mjs +699 -1
- package/test/voice-options.test.mjs +261 -0
- package/test/voice-pack-download.test.mjs +13 -6
- package/tools/notify-config.ps1 +12 -9
- package/tts-speak.ps1 +7 -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
|
}
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
import { test } from 'node:test';
|
|
20
20
|
import assert from 'node:assert/strict';
|
|
21
21
|
import { readFileSync, existsSync, readdirSync, statSync, globSync } from 'node:fs';
|
|
22
|
+
import { execFileSync } from 'node:child_process';
|
|
22
23
|
import { join, resolve, dirname } from 'node:path';
|
|
23
24
|
import { fileURLToPath } from 'node:url';
|
|
24
25
|
|
|
@@ -796,6 +797,162 @@ test('D16: PowerShell 脚本用 LF(与 .gitattributes 一致)', () => {
|
|
|
796
797
|
'.ps1 里出现了 CR (CRLF 行尾) —— 与 .gitattributes 的 eol=lf 不一致,'
|
|
797
798
|
+ '会导致发布包与 GitHub 版本字节不同:\n' + bad.join('\n'));
|
|
798
799
|
});
|
|
800
|
+
|
|
801
|
+
// ── D16b: 所有文本扩展名都必须是 LF(不只是 .ps1)─────────────────────
|
|
802
|
+
test('D16b: 所有声明了 eol=lf 的文本文件都不许出现 CR', () => {
|
|
803
|
+
// ⚠️ 2026-10-25 实测漏网:
|
|
804
|
+
//
|
|
805
|
+
// `.gitattributes` 声明的是 *.ps1 / *.js / *.mjs / *.md / *.json / *.yml
|
|
806
|
+
// / *.yaml / *.txt 全部 `text eol=lf`。但 D16 只检查 .ps1。
|
|
807
|
+
//
|
|
808
|
+
// 后果(我当场踩到):用会写 CRLF 的编辑器/工具改 `test/voice-label.test.mjs`
|
|
809
|
+
// 之后,该文件变成 547 个 CRLF,而**整套 589 条测试照样全绿** ——
|
|
810
|
+
// 只有 `git commit` 时会冒出一句 warning,然后 git 在索引里悄悄归一化。
|
|
811
|
+
// 于是"工作树字节"与"提交/发布出去的字节"又一次不同,
|
|
812
|
+
// 而这正是上方 .gitattributes 注释里写明要杜绝的事。
|
|
813
|
+
//
|
|
814
|
+
// 判据来源必须与 .gitattributes 一致:直接从文件里解析出扩展名清单,
|
|
815
|
+
// 而不是在测试里另抄一份(抄一份就会漂移)。
|
|
816
|
+
//
|
|
817
|
+
// ⚠️ 2026-10-25 合并到主仓时立刻红了一次,暴露出一个真实缺陷:
|
|
818
|
+
// `.gitattributes` 的 `eol=lf` **只对 git 跟踪的文件生效** ——
|
|
819
|
+
// 被 .gitignore 排除的目录(本项目的 `.snapshots/` 存着历史快照,
|
|
820
|
+
// 是当年用 CRLF 工具生成的)不影响任何人 checkout 到的字节。
|
|
821
|
+
// 但这条判据只排除 node_modules / .tmp,于是把 `.snapshots/` 也扫了,
|
|
822
|
+
// 报「30 个文件违规」—— 全是历史快照,与本次改动无关。
|
|
823
|
+
//
|
|
824
|
+
// worktree 里没有 `.snapshots/`,所以这个假红**只在主仓才出现**。
|
|
825
|
+
// 修法不是加一个 `.snapshots` 白名单(那会随新目录名再次复发),
|
|
826
|
+
// 而是用 git 本身来问「这个文件被跟踪吗 / 被忽略吗」:
|
|
827
|
+
// 未跟踪 或 被忽略 ⇒ 跳过(eol=lf 对它没有约束力)
|
|
828
|
+
const GA = readFileSync(join(ROOT, '.gitattributes'), 'utf8');
|
|
829
|
+
const exts = [];
|
|
830
|
+
for (const line of GA.split('\n')) {
|
|
831
|
+
const t = line.trim();
|
|
832
|
+
if (!t || t.startsWith('#')) continue;
|
|
833
|
+
const m = t.match(/^(\*\.[\w.]+)\s+.*\beol=lf\b/);
|
|
834
|
+
if (m) exts.push(m[1].slice(1).toLowerCase()); // '*.mjs' → '.mjs'
|
|
835
|
+
}
|
|
836
|
+
assert.ok(exts.length >= 5,
|
|
837
|
+
`.gitattributes 里应解析出至少 5 个 eol=lf 扩展名,实际 ${exts.length} 个:${exts.join(',')}`
|
|
838
|
+
+ ' —— 若解析失败说明 .gitattributes 格式变了,下面这条判据会变成空过滤');
|
|
839
|
+
|
|
840
|
+
// 「哪些文件被 .gitignore 排除」—— 用 git 自己回答,不靠测试里另抄一份忽略规则。
|
|
841
|
+
//
|
|
842
|
+
// ## 这段被改过四次,每次都是同一个坑的另一面(留档,别再来第五次)
|
|
843
|
+
//
|
|
844
|
+
// 目标只有一个:**被 .gitignore 排除的文件不该被检查**,因为
|
|
845
|
+
// `.gitattributes` 的 `eol=lf` 对它们没有约束力(它们不参与 checkout,
|
|
846
|
+
// 不影响任何用户拿到的字节)。
|
|
847
|
+
//
|
|
848
|
+
// ① 首次:只排除 node_modules / .tmp → 主仓 `.snapshots/`(被忽略的
|
|
849
|
+
// 历史快照,30 个文件全是 CRLF)被当成违规,纯假红。
|
|
850
|
+
// ② 第二次:用 `--show-toplevel == ROOT` 判断"ROOT 是仓库根"。
|
|
851
|
+
// **判据错**:插件目录本来就在大仓子目录里(toplevel=E:/0Deepseek),
|
|
852
|
+
// 主仓里恒为 false → 又退回全扫 → 假红复发。
|
|
853
|
+
// 被自己写的 prove-snapshots.mjs 抓出来。
|
|
854
|
+
// ③ 第三次:改成"ROOT 在 toplevel 之内",然后**跳过所有未跟踪文件**。
|
|
855
|
+
// **判据还是错**:`release.ps1` 第 3.5 步把发布包建在仓库内
|
|
856
|
+
// (`…\.tmp\…`),里面 107 个文件全是「未跟踪但没被忽略」,
|
|
857
|
+
// 于是被全部跳过 → 扫到 0 个 → 反向证明报红。
|
|
858
|
+
// 被三形态矩阵(shape-matrix.mjs)抓出来。
|
|
859
|
+
// ④ 现在:**只跳过被忽略的,不跳过"未跟踪"的**。
|
|
860
|
+
// 两者语义完全不同:
|
|
861
|
+
// · 被忽略(`.snapshots/`、`.tmp/`)→ eol=lf 管不到 → 跳过 ✓
|
|
862
|
+
// · 未跟踪但没被忽略(刚新建的文件、发布包副本)→ 将来会入库
|
|
863
|
+
// → 必须照常检查 ✓(这个严格性正是想要的)
|
|
864
|
+
// 这样三种形态都自然正确,不需要判断"是不是仓库根":
|
|
865
|
+
// · 源码目录:忽略清单含 .snapshots → 跳过它,其余照查
|
|
866
|
+
// · 发布包(仓库内):忽略清单为空(发布包里没有 .gitignore 规则生效)
|
|
867
|
+
// → 全都照查,与修复前一致
|
|
868
|
+
// · 发布包(仓库外):没有 git → 全都照查
|
|
869
|
+
//
|
|
870
|
+
// ⑤ 第四次收尾时又发现一个边界(三形态矩阵的形态③抓的):
|
|
871
|
+
// **ROOT 自身落在被忽略的目录里时不能信忽略清单。**
|
|
872
|
+
// 典型场景:把发布包建在仓库的 `.tmp/` 底下(我自己就干过),
|
|
873
|
+
// 此时 `git ls-files --others --ignored` 会把该目录下**每一个**文件
|
|
874
|
+
// 都报成"被忽略"(因为父目录 `.tmp/` 被忽略),于是全部跳过 → 扫到 0。
|
|
875
|
+
// 判据:先问 `git check-ignore` 判定 ROOT 本身是否被忽略;
|
|
876
|
+
// 被忽略 ⇒ ignoredSet 保持 null(全都查)。
|
|
877
|
+
let ignoredSet = null;
|
|
878
|
+
try {
|
|
879
|
+
// 先判 ROOT 本身是否落在忽略范围内。退出码 0 = 被忽略。
|
|
880
|
+
let rootIgnored = false;
|
|
881
|
+
try {
|
|
882
|
+
execFileSync('git', ['check-ignore', '-q', '.'], { cwd: ROOT, stdio: 'pipe', timeout: 15_000 });
|
|
883
|
+
rootIgnored = true; // 退出码 0 时 execFileSync 不抛
|
|
884
|
+
} catch (e) {
|
|
885
|
+
rootIgnored = e && e.status === 0;
|
|
886
|
+
}
|
|
887
|
+
if (!rootIgnored) {
|
|
888
|
+
// -z:NUL 分隔,避免中文/空格文件名被转义成带引号的形式。
|
|
889
|
+
const out = execFileSync(
|
|
890
|
+
'git', ['ls-files', '--others', '--ignored', '--exclude-standard', '-z'],
|
|
891
|
+
{ cwd: ROOT, encoding: 'utf8', timeout: 30_000, maxBuffer: 64 * 1024 * 1024, stdio: 'pipe' },
|
|
892
|
+
);
|
|
893
|
+
// 只取**目录级别**的忽略前缀,逐文件比对太脆(新文件随时增删)。
|
|
894
|
+
// 一个被忽略的目录,其下所有文件的相对路径都以 `<dir>/` 开头。
|
|
895
|
+
const entries = out.split('\0').filter(Boolean);
|
|
896
|
+
ignoredSet = new Set();
|
|
897
|
+
for (const e of entries) {
|
|
898
|
+
ignoredSet.add(e.replace(/\/$/, ''));
|
|
899
|
+
const seg = e.split('/')[0];
|
|
900
|
+
if (seg && seg !== e) ignoredSet.add(seg);
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
} catch { /* 不是 git 仓库 / 无 git:保持 null,全都查 */ }
|
|
904
|
+
|
|
905
|
+
// 判断某个相对路径是否落在被忽略的范围里
|
|
906
|
+
const isIgnored = (rel) => {
|
|
907
|
+
if (ignoredSet === null || ignoredSet.size === 0) return false;
|
|
908
|
+
if (ignoredSet.has(rel)) return true;
|
|
909
|
+
// 任一前缀段命中(例如 `.snapshots` 命中 `.snapshots/20261020-122245/x.js`)
|
|
910
|
+
let acc = '';
|
|
911
|
+
for (const seg of rel.split('/')) {
|
|
912
|
+
acc = acc ? `${acc}/${seg}` : seg;
|
|
913
|
+
if (ignoredSet.has(acc)) return true;
|
|
914
|
+
}
|
|
915
|
+
return false;
|
|
916
|
+
};
|
|
917
|
+
|
|
918
|
+
const bad = [];
|
|
919
|
+
let scanned = 0;
|
|
920
|
+
let skippedIgnored = 0;
|
|
921
|
+
const walk = (dir) => {
|
|
922
|
+
let entries = [];
|
|
923
|
+
try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return; }
|
|
924
|
+
for (const e of entries) {
|
|
925
|
+
if (e.name === 'node_modules' || e.name.startsWith('.tmp')) continue;
|
|
926
|
+
const p = join(dir, e.name);
|
|
927
|
+
if (e.isDirectory()) { walk(p); continue; }
|
|
928
|
+
const low = e.name.toLowerCase();
|
|
929
|
+
if (!exts.some((x) => low.endsWith(x))) continue;
|
|
930
|
+
// 只有**被 .gitignore 排除**的才跳过(见上方四轮说明)。
|
|
931
|
+
// "未跟踪但没被忽略"的文件必须照查 —— 它将来会入库。
|
|
932
|
+
if (isIgnored(p.slice(ROOT.length + 1).replace(/\\/g, '/'))) {
|
|
933
|
+
skippedIgnored += 1;
|
|
934
|
+
continue;
|
|
935
|
+
}
|
|
936
|
+
scanned += 1;
|
|
937
|
+
try {
|
|
938
|
+
const buf = readFileSync(p);
|
|
939
|
+
if (buf.includes(13)) bad.push(p.slice(ROOT.length + 1));
|
|
940
|
+
} catch { /* 读不到就跳过 */ }
|
|
941
|
+
}
|
|
942
|
+
};
|
|
943
|
+
walk(ROOT);
|
|
944
|
+
|
|
945
|
+
// 反向证明:确认真的扫到了文件,否则这条是恒真空过滤
|
|
946
|
+
assert.ok(scanned >= 30,
|
|
947
|
+
`只扫到 ${scanned} 个未被忽略的文本文件(跳过被忽略的 ${skippedIgnored} 个),`
|
|
948
|
+
+ '明显偏少 —— 判据可能失效(空过滤)');
|
|
949
|
+
|
|
950
|
+
assert.deepEqual(bad, [],
|
|
951
|
+
`以下文本文件出现 CR(CRLF 行尾),与 .gitattributes 的 eol=lf 不一致。`
|
|
952
|
+
+ `这会让工作树字节与提交/发布出去的字节不同,任何按字节校验都会对不上。`
|
|
953
|
+
+ `\n检查范围(从 .gitattributes 解析): ${exts.join(' ')}`
|
|
954
|
+
+ `\n共扫描 ${scanned} 个文件,违规 ${bad.length} 个:\n` + bad.join('\n'));
|
|
955
|
+
});
|
|
799
956
|
// ── D17: README 的联网说明必须涵盖"检查更新" ────────────────────────────
|
|
800
957
|
test('D17: README 如实说明检查更新也会联网', () => {
|
|
801
958
|
// ⚠️ 2026-10-18 第四轮审阅指出:v0.4.48 加了"挂载时自动查版本",
|
|
@@ -2187,3 +2344,76 @@ test('D20: engines.dsh 必须有实测下界 + 上界,且与 README 一致', (
|
|
|
2187
2344
|
assert.doesNotMatch(readme, /0\.1\.7-rc\.2 或更高/,
|
|
2188
2345
|
'README 不能再说"0.1.7-rc.2 或更高" —— 那个版本没测过');
|
|
2189
2346
|
});
|
|
2347
|
+
|
|
2348
|
+
// ── D21: 发布包生成的 package.json 必须是纯 LF ────────────────────────────
|
|
2349
|
+
//
|
|
2350
|
+
// ## 这条是 2026-10-25 在**发布包里**跑测试时抓出来的
|
|
2351
|
+
//
|
|
2352
|
+
// `release.ps1` 第 3.5 步会「在发布包里跑一遍 npm run test:all」(模拟 CI 的
|
|
2353
|
+
// 真实环境)。D16b 在那里红了:
|
|
2354
|
+
//
|
|
2355
|
+
// 共扫描 107 个文件,违规 1 个: package.json
|
|
2356
|
+
//
|
|
2357
|
+
// 根因不在 D16b,而在生成端 —— `release-build.ps1` 用
|
|
2358
|
+
// `ConvertTo-Json` 产出 JSON,而它**在 PowerShell 5.1 和 7.6 上都输出 CRLF**
|
|
2359
|
+
// (实测 `.Contains([char]13)` 为 True)。于是:
|
|
2360
|
+
//
|
|
2361
|
+
// 源仓 package.json 纯 LF(.gitattributes 的 `*.json text eol=lf`)
|
|
2362
|
+
// 发布包 package.json CRLF ← ConvertTo-Json 写出来的
|
|
2363
|
+
// npm 上 v0.5.2 那份 CRLF ← 实测 CR=77,历史产物一直如此
|
|
2364
|
+
//
|
|
2365
|
+
// 站内 D16 系列只查**源目录**,所以这个差异从来没在开发机上暴露过;
|
|
2366
|
+
// 只有「在发布包里跑测试」那一步才看得见。这属于"我们的产物与我们声称的
|
|
2367
|
+
// 字节不一致",正是 .gitattributes 注释里写明要杜绝的事。
|
|
2368
|
+
//
|
|
2369
|
+
// ## 为什么用静态判据而不是真跑一次 release-build
|
|
2370
|
+
//
|
|
2371
|
+
// 真跑一次实测 4.03 秒(复制 138 个文件 + 语音包筛选 + git log 生成 CHANGELOG),
|
|
2372
|
+
// 超出日常检查器 ≤3 秒的预算,且会往磁盘写 6 MB。所以这里只静态断言
|
|
2373
|
+
// 「归一化那一行还在、并且排在 WriteAllText 之前」;
|
|
2374
|
+
// 真正的端到端证据由 `release.ps1` 的第 3.5 步(在发布包里跑全量测试)提供。
|
|
2375
|
+
test('D21: 生成 package.json 前必须把行尾归一成 LF', { skip: SKIP_DEVONLY }, () => {
|
|
2376
|
+
const rb = readFileSync(RELEASE_BUILD, 'utf8');
|
|
2377
|
+
|
|
2378
|
+
// ① 归一化语句必须存在
|
|
2379
|
+
const NORMALIZE = /^\$json\s*=\s*\$json\s*-replace\s+"`r`n"\s*,\s*"`n"/m;
|
|
2380
|
+
const mNorm = rb.match(NORMALIZE);
|
|
2381
|
+
assert.ok(mNorm,
|
|
2382
|
+
'release-build.ps1 里找不到把 $json 行尾归一成 LF 的语句。\n'
|
|
2383
|
+
+ 'ConvertTo-Json 在 PowerShell 5.1 与 7.6 上都输出 CRLF,'
|
|
2384
|
+
+ '不归一化就会让发布包/npm 上的 package.json 与源仓字节不同'
|
|
2385
|
+
+ '(2026-10-25 实测 v0.5.2 的 npm 包 CR=77)。');
|
|
2386
|
+
|
|
2387
|
+
// ② 它必须排在写 package.json 之前 —— 否则"归一化了但没写进去"
|
|
2388
|
+
const iNorm = rb.indexOf(mNorm[0]);
|
|
2389
|
+
const NORM_NOTE = '归一化语句必须出现在写 package.json 之前,否则等于没生效';
|
|
2390
|
+
const writePj = rb.indexOf("WriteAllText((Join-Path $Destination 'package.json')");
|
|
2391
|
+
assert.ok(writePj > 0,
|
|
2392
|
+
'找不到写 package.json 的 WriteAllText —— 若生成方式变了,请同步改本条用例');
|
|
2393
|
+
assert.ok(iNorm < writePj, NORM_NOTE);
|
|
2394
|
+
|
|
2395
|
+
// ③ 归一化之后不许再整体重赋 $json —— 否则归一化会被后面那行覆盖掉。
|
|
2396
|
+
// ⚠️ 第一版这里写成 `/\$json\s*=\s*(?!\$json)/`,**判据本身是错的**:
|
|
2397
|
+
// `\s*` 可以回溯到只匹配 0 个字符,负向先行断言落在空格上就通过了,
|
|
2398
|
+
// 于是 `$json = $json -replace ...` 这**唯一合法**的形态反被判违规(假红)。
|
|
2399
|
+
// 改成数赋值次数:区间内 `$json =` 只该出现 1 次(即归一化那行自己的左边)。
|
|
2400
|
+
const tail = rb.slice(iNorm, writePj);
|
|
2401
|
+
const assigns = tail.match(/\$json\s*=/g) || [];
|
|
2402
|
+
assert.equal(assigns.length, 1,
|
|
2403
|
+
'归一化语句与写入之间又出现了 $json 赋值(共 ' + assigns.length + ' 处 '
|
|
2404
|
+
+ '`$json =`,只该有归一化那一处) —— 后赋值会把归一化结果覆盖掉');
|
|
2405
|
+
|
|
2406
|
+
// ④ 写入用的必须就是 $json 本身(不是被改名/被换掉的另一个变量)
|
|
2407
|
+
const writeStmt = rb.slice(writePj, writePj + 200);
|
|
2408
|
+
assert.ok(/\$json\s*\+/.test(writeStmt),
|
|
2409
|
+
'写 package.json 时用的不是 $json —— 归一化作用不到真正落盘的字节上');
|
|
2410
|
+
|
|
2411
|
+
// ④ 反向证明:把归一化那行删掉,判据必须能报红
|
|
2412
|
+
// (照搬 D19d 的做法:不能只有"看起来还在",要能证明它真的会失败)
|
|
2413
|
+
{
|
|
2414
|
+
const mutated = rb.replace(mNorm[0], '# [D21 变异:删掉归一化]');
|
|
2415
|
+
assert.notEqual(mutated, rb, 'D21 变异未生效 —— 正则与真实源码不匹配');
|
|
2416
|
+
assert.ok(!NORMALIZE.test(mutated),
|
|
2417
|
+
'D21 的判据抓不住"归一化被删掉"这种情况,等于装饰');
|
|
2418
|
+
}
|
|
2419
|
+
});
|
package/test/fail-voice.test.mjs
CHANGED
|
@@ -188,6 +188,145 @@ test('B5: -Kind done 未回归(仍从 done 池选)', () => {
|
|
|
188
188
|
});
|
|
189
189
|
|
|
190
190
|
test('B6: 未知 Kind 被 ValidateSet 拒绝(fail-closed,不会静默走错池)', () => {
|
|
191
|
-
|
|
191
|
+
// -Volume 0 是必需的不是可选的:ValidateSet 在参数绑定阶段就拒绝,
|
|
192
|
+
// 现在确实走不到播放,但依赖「错误刚好发生在绑定期」是脆弱假设。
|
|
193
|
+
// 下面 S1 会强制要求这里带上它。
|
|
194
|
+
const { code } = runPs1(['-Kind', 'bogus-kind', '-Preview', '-Volume', '0']);
|
|
192
195
|
assert.notEqual(code, 0, 'ValidateSet 应拒绝未知 Kind');
|
|
193
196
|
});
|
|
197
|
+
|
|
198
|
+
// ══════════════════════════════════════════════════════════════════════════
|
|
199
|
+
// S. 测试静默护栏 —— 跑测试不该在用户耳边出声
|
|
200
|
+
//
|
|
201
|
+
// ## 为什么这几条在这里
|
|
202
|
+
//
|
|
203
|
+
// 2026-10-24 用户在我跑测试期间听到「一直在滴滴滴」,问「不知道为什么」。
|
|
204
|
+
// 排查出两个**不同**的原因,都值得钉死:
|
|
205
|
+
//
|
|
206
|
+
// ### 原因一(真实发生):跑 ps1 的测试忘传 -Volume 0
|
|
207
|
+
// `dsh-notify.ps1` 的静音开关是 `if ($script:notifyVol -eq 0) { return }`
|
|
208
|
+
// (Play-VoiceLine:499 / Play-Chime:415)。不传 `-Volume 0` 它就**真的播音频**。
|
|
209
|
+
// 当天我用 `-Preview` 手动验证中文音色时没传,连续放了 6 段中文语音;
|
|
210
|
+
// 同一形状的隐患存在于任何新增的 runPs1(...) 调用点 —— 全靠人工记得加。
|
|
211
|
+
// 放在本文件是因为「这里就是跑 ps1 的地方」,护栏和被守护的代码挨着。
|
|
212
|
+
//
|
|
213
|
+
// ### 原因二(潜在,尚未发生):JS 层蜂鸣不经过 ps1
|
|
214
|
+
// fallbackBeep() 直接 spawn [console]::beep,-Volume 0 管不到它。
|
|
215
|
+
// 它现在没吵到人纯属巧合(测试把 SystemRoot 指到不存在路径,蜂鸣的
|
|
216
|
+
// powershell 也起不来)。已在 index.js 里按既有 __STUB_CHILD_PROCESS__
|
|
217
|
+
// 约定显式挡掉,S2 补一条断言防回退。
|
|
218
|
+
//
|
|
219
|
+
// ## 判据设计
|
|
220
|
+
// 不用「跑一遍听有没有声音」(不可自动化),而是静态扫描调用点:
|
|
221
|
+
// 任何把 ps1 的 -Preview / -Kind 真跑起来的地方,同一条 argv 里
|
|
222
|
+
// 必须有 `-Volume', '0'`。这条判据能稳定复现、误报低。
|
|
223
|
+
// ══════════════════════════════════════════════════════════════════════════
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* 找出所有会**真跑** dsh-notify.ps1 的 argv 构造点,检查是否带 `-Volume 0`。
|
|
227
|
+
*
|
|
228
|
+
* 只认同一个 `['...', ...]` 数组字面量里的内容 —— 跨行也算,
|
|
229
|
+
* 因为 spawn 的 argv 常常拆成多行写。
|
|
230
|
+
*
|
|
231
|
+
* ⚠️ 必须跳过**注释行**:文档里写「这里原本是 runPs1(['-Kind', ...])」
|
|
232
|
+
* 是很正常的说明方式,若把注释也当违规,判据会逼着人不敢写注释。
|
|
233
|
+
* (第一版没跳,结果把本文件里的说明文字报成了违规。)
|
|
234
|
+
*
|
|
235
|
+
* @returns {string[]} 问题描述,空数组 = 全部合规
|
|
236
|
+
*/
|
|
237
|
+
function silentArgvProblems() {
|
|
238
|
+
const problems = [];
|
|
239
|
+
for (const name of readdirSync(HERE)) {
|
|
240
|
+
if (!name.endsWith('.test.mjs')) continue;
|
|
241
|
+
const src = readFileSync(join(HERE, name), 'utf8');
|
|
242
|
+
const lines = src.split(/\r?\n/);
|
|
243
|
+
let inBlockComment = false;
|
|
244
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
245
|
+
const line = lines[i];
|
|
246
|
+
// 维护块注释状态(`/** ... */` 与 `/* ... */`)
|
|
247
|
+
const trimmed = line.trim();
|
|
248
|
+
if (inBlockComment) {
|
|
249
|
+
if (trimmed.includes('*/')) inBlockComment = false;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (trimmed.startsWith('/*')) {
|
|
253
|
+
if (!trimmed.includes('*/')) inBlockComment = true;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
if (trimmed.startsWith('//')) continue;
|
|
257
|
+
|
|
258
|
+
// 定位一次 argv 数组的字面量起点(`['-Kind'` 或 `['-Preview'`)
|
|
259
|
+
if (!/\[\s*'-(Kind|Preview|Mode|Voice)'/.test(line)) continue;
|
|
260
|
+
// 向后收集到本数组闭合(简单括号计数,够用:这些数组里没有嵌套括号)
|
|
261
|
+
let depth = 0;
|
|
262
|
+
let buf = '';
|
|
263
|
+
let j = i;
|
|
264
|
+
for (; j < lines.length; j += 1) {
|
|
265
|
+
const ln = lines[j];
|
|
266
|
+
for (const ch of ln) {
|
|
267
|
+
if (ch === '[') depth += 1;
|
|
268
|
+
else if (ch === ']') depth -= 1;
|
|
269
|
+
}
|
|
270
|
+
buf += ln + '\n';
|
|
271
|
+
if (depth <= 0 && j >= i) break;
|
|
272
|
+
}
|
|
273
|
+
// 只关心真的跑 ps1 的(带 -Preview 或 -Kind),而不是在断言源码文本
|
|
274
|
+
if (!/'-Preview'/.test(buf) && !/'-Kind'/.test(buf)) continue;
|
|
275
|
+
// 已经是字符串/正则字面量(在断言别处源码)→ 跳过
|
|
276
|
+
if (/assert\.|match\(|regex|RE\b/.test(lines[Math.max(0, i - 1)])) continue;
|
|
277
|
+
const silent = /'-Volume'\s*,\s*'0'/.test(buf) || /"-Volume"\s*,\s*"0"/.test(buf);
|
|
278
|
+
if (!silent) {
|
|
279
|
+
problems.push(`${name}:${i + 1} 真跑 ps1 却没传 -Volume 0 → 会在用户耳边出声:\n`
|
|
280
|
+
+ buf.split('\n').slice(0, 3).map((s) => ' ' + s.trim()).join('\n'));
|
|
281
|
+
}
|
|
282
|
+
i = j; // 跳过已消费的行,避免同一数组被重复报告
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return problems;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
test('S1: 任何真跑 dsh-notify.ps1 的测试都必须传 -Volume 0(否则真的会响)', () => {
|
|
289
|
+
const problems = silentArgvProblems();
|
|
290
|
+
assert.deepEqual(problems, [],
|
|
291
|
+
'这些测试跑 ps1 时会真的播放音频。跑测试不该吵到正在用电脑的人:\n '
|
|
292
|
+
+ problems.join('\n ')
|
|
293
|
+
+ '\n\n修法:在该 argv 里加 \'-Volume\', \'0\'。'
|
|
294
|
+
+ '\n(-Volume 0 在 Play-VoiceLine 与 Play-Chime 里都是「在打开文件之前就 return」的真静音)');
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
test('S2: fallbackBeep 必须有测试环境守卫(JS 层蜂鸣绕过 -Volume 0)', () => {
|
|
298
|
+
// 定位函数体
|
|
299
|
+
const m = readFileSync(LIB, 'utf8').match(/function fallbackBeep\(which\)\s*\{([\s\S]*?)\n \}/);
|
|
300
|
+
assert.ok(m, '应能定位 fallbackBeep 函数体');
|
|
301
|
+
const body = m[1];
|
|
302
|
+
|
|
303
|
+
assert.match(body, /__STUB_CHILD_PROCESS__\s*===\s*true/,
|
|
304
|
+
'fallbackBeep 必须在测试环境(__STUB_CHILD_PROCESS__ === true)下不真蜂鸣。\n'
|
|
305
|
+
+ '它 spawn 的是 [console]::beep,完全绕过 ps1 的 -Volume 0 静音;\n'
|
|
306
|
+
+ '现在不出声只是因为测试把 SystemRoot 指到了不存在的路径 —— 那是巧合,不是约定。');
|
|
307
|
+
|
|
308
|
+
// 守卫必须是**条件**而不是注释:去掉注释后仍要能找到
|
|
309
|
+
const noComment = body.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/[^\n]*/g, '');
|
|
310
|
+
assert.match(noComment, /__STUB_CHILD_PROCESS__/,
|
|
311
|
+
'这个守卫必须在**生效代码**里,不能只是注释里提了一句');
|
|
312
|
+
|
|
313
|
+
// 必须真的提前 return(否则守卫写了等于没写)
|
|
314
|
+
assert.match(noComment, /if\s*\(\s*stubbed\s*\)\s*return/,
|
|
315
|
+
'守卫后面必须 `if (stubbed) return`,否则 spawn 照样发生');
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
test('S3: 静音判据本身要能失败(反向证明,防恒真空过滤)', () => {
|
|
319
|
+
// 拿一段"故意漏传 -Volume 0"的假源码喂给同一个判据逻辑,必须报出问题。
|
|
320
|
+
// 不这么做的话,S1 可能在判据写错时一直绿 —— #469 踩过这个坑
|
|
321
|
+
// (「筛出问题后必须为空」的断言,若筛子本身永远筛不出东西,它就恒真)。
|
|
322
|
+
//
|
|
323
|
+
// ⚠️ 假源码**不能写成完整的 argv 字面量**:S1 会扫到它,
|
|
324
|
+
// 把本文件自己报成违规(第一版就这么红了)。用字符串拼接,让静态扫描
|
|
325
|
+
// 看不到完整的字面量,而判据逻辑本身照样能被喂到。
|
|
326
|
+
const fake = "const r = runPs1(['" + "-Kind', 'done', '-Preview']);\n";
|
|
327
|
+
const hasSilent = /'-Volume'\s*,\s*'0'/.test(fake);
|
|
328
|
+
assert.equal(hasSilent, false, '这段假源码确实没传 -Volume 0,判据应能识别');
|
|
329
|
+
// 再验证正例:加了之后判据应认为合规
|
|
330
|
+
const fakeOk = "const r = runPs1(['" + "-Kind', 'done', '-Preview', '-Volume', '0']);\n";
|
|
331
|
+
assert.equal(/'-Volume'\s*,\s*'0'/.test(fakeOk), true, '加了 -Volume 0 后判据应认为合规');
|
|
332
|
+
});
|
|
@@ -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
|
+
});
|