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.
- package/.gitattributes +39 -0
- package/CHANGELOG.md +13 -0
- package/README.md +34 -11
- package/THIRD-PARTY-NOTICES.md +54 -1
- package/lib/client.js +17 -5
- package/package.json +76 -77
- package/sounds/voice-lines/approval/zh-soft-female~1.wav +0 -0
- package/sounds/voice-lines/approval/zh-soft-female~2.wav +0 -0
- package/sounds/voice-lines/approval/zh-soft-female~3.wav +0 -0
- package/sounds/voice-lines/fail/zh-soft-female~1.wav +0 -0
- package/sounds/voice-lines/fail/zh-soft-female~2.wav +0 -0
- package/sounds/voice-lines/fail/zh-soft-female~3.wav +0 -0
- package/sounds/voice-lines/question/zh-soft-female~1.wav +0 -0
- package/sounds/voice-lines/question/zh-soft-female~2.wav +0 -0
- package/sounds/voice-lines/question/zh-soft-female~3.wav +0 -0
- package/sounds/voice-lines/zh-soft-female-extra-1.wav +0 -0
- package/sounds/voice-lines/zh-soft-female-extra-2.wav +0 -0
- package/sounds/voice-lines/zh-soft-female.wav +0 -0
- package/sounds/voice-lines/zundamon-normal-extra-1.wav +0 -0
- package/sounds/voice-lines/zundamon-normal-extra-2.wav +0 -0
- package/sounds/voice-lines/zundamon-normal-kansai-1.wav +0 -0
- package/sounds/voice-lines/zundamon-normal-kansai-2.wav +0 -0
- package/sounds/voice-lines/zundamon-normal-kansai-3.wav +0 -0
- package/test/docs-consistency.test.mjs +1566 -5
- package/test/protocol-ownership.test.mjs +724 -51
- package/test/zh-voice-pack.test.mjs +379 -0
|
@@ -44,6 +44,45 @@ import {
|
|
|
44
44
|
/** 我们写进注册表键的身份标记值(应与 package.json 的 name 一致)。 */
|
|
45
45
|
const OWNER = OWNER_ID;
|
|
46
46
|
|
|
47
|
+
// ══ 源码切片工具:按**真实函数边界**取函数体 ═══════════════════════════════
|
|
48
|
+
//
|
|
49
|
+
// ## 为什么不能用 `SRC.slice(at, at + 1200)`(只读审查 B3,2026-10-22 实测)
|
|
50
|
+
//
|
|
51
|
+
// 硬编码窗口是魔法数,和函数实际长度无关。实测(按闭合括号量的**函数体**长度):
|
|
52
|
+
// readProtocolRegistration 1572 > 窗口 1400
|
|
53
|
+
// registerJumpProtocol 3246 > 窗口 1200
|
|
54
|
+
// unregisterJumpProtocol 1083 < 窗口 1200
|
|
55
|
+
// cleanupLegacyProtocols 1547 > 窗口 900
|
|
56
|
+
// syncJumpProtocol 1652 > 窗口 1200
|
|
57
|
+
// probeJumpProtocol 613 < 窗口 2000
|
|
58
|
+
//
|
|
59
|
+
// 也就是说 B3 不是「将来会坏」——**四个函数的旧窗口当时就已经小于函数体长了**,
|
|
60
|
+
// 守卫没被截掉纯属位置巧合(都在函数开头几行)。谁在前置加一段注释,
|
|
61
|
+
// 守卫就滑出窗口 → 静默变松(守护卫滑出去、而别处关键词滑进来),
|
|
62
|
+
// 或静默变红(守卫还在、但窗口截在它前面)。两种都是假信号。
|
|
63
|
+
//
|
|
64
|
+
// ## 做法
|
|
65
|
+
//
|
|
66
|
+
// 取 `function` 那一行**自己的缩进**,再按同一缩进取闭合括号。
|
|
67
|
+
// 顶层函数(0 空格,如 lib/client.js)与嵌套函数(2 空格,如 lib/index.js
|
|
68
|
+
// 里 apply() 内的那些)都适用。
|
|
69
|
+
// 找不到边界就 fail-closed 抛错 —— 宁可让测试红,也不要用一个猜出来的范围。
|
|
70
|
+
function functionBody(src, name) {
|
|
71
|
+
const at = src.indexOf(`function ${name}(`);
|
|
72
|
+
if (at < 0) throw new Error(`找不到函数 ${name}(源码结构变了,请更新本测试)`);
|
|
73
|
+
|
|
74
|
+
// 取 `function` 那一行的缩进:lib/index.js 的内部函数都在 apply() 里(2 空格),
|
|
75
|
+
// lib/client.js 的顶层函数是 0 空格。按同一缩进取闭合括号,两种源码都能用。
|
|
76
|
+
const lineStart = src.lastIndexOf('\n', at) + 1;
|
|
77
|
+
const indent = src.slice(lineStart, at); // 只含空格/Tab
|
|
78
|
+
if (!/^[ \t]*$/.test(indent)) {
|
|
79
|
+
throw new Error(`${name} 不在行首(源码结构变了,请更新本测试)`);
|
|
80
|
+
}
|
|
81
|
+
const end = src.indexOf(`\n${indent}}`, at);
|
|
82
|
+
if (end < 0) throw new Error(`找不到 ${name} 的结束边界(源码结构变了,请更新本测试)`);
|
|
83
|
+
return src.slice(at, end + indent.length + 2);
|
|
84
|
+
}
|
|
85
|
+
|
|
47
86
|
/** 拼一条"像我们写的"注册命令(目录随便写,归属只认身份标记/handler 名)。 */
|
|
48
87
|
const ourCmd = (handler) =>
|
|
49
88
|
`"C:\\Windows\\System32\\wscript.exe" "E:\\0Deepseek\\tools\\plugins\\dsh-notify-say\\${handler}" "%1"`;
|
|
@@ -300,8 +339,9 @@ test('I1: index.js 必须使用纯函数(不能自己又写一套判定)', a
|
|
|
300
339
|
'sync 必须调用 isLegacyRegistration 清理历史协议');
|
|
301
340
|
|
|
302
341
|
// 反向断言:不能残留手写的四态判定(那说明有人又写了一份)
|
|
303
|
-
|
|
304
|
-
|
|
342
|
+
// 用真实函数边界而不是 SRC.slice(at, at + 2000):窗口是魔法数,
|
|
343
|
+
// probeJumpProtocol 现在 613 字,但谁在前面加一段注释就可能滑出去。
|
|
344
|
+
const probeBody = functionBody(SRC, 'probeJumpProtocol');
|
|
305
345
|
assert.doesNotMatch(probeBody, /return 'foreign'/,
|
|
306
346
|
'probe 里不该再有手写的 foreign 判定 —— 判定已收进纯函数');
|
|
307
347
|
});
|
|
@@ -346,7 +386,7 @@ test('I2b: readProtocolRegistration 的 PowerShell 必须设 OutputEncoding=UTF8
|
|
|
346
386
|
|
|
347
387
|
const fnAt = SRC.indexOf('function readProtocolRegistration(');
|
|
348
388
|
assert.ok(fnAt > 0, '找不到 readProtocolRegistration(源码结构变了,请更新本测试)');
|
|
349
|
-
const body = SRC
|
|
389
|
+
const body = functionBody(SRC, 'readProtocolRegistration');
|
|
350
390
|
|
|
351
391
|
assert.match(body, /\[Console\]::OutputEncoding = \[Text\.Encoding\]::UTF8/,
|
|
352
392
|
'读注册表的 PowerShell 必须先把 OutputEncoding 设成 UTF8 —— '
|
|
@@ -447,7 +487,7 @@ test('I6: cleanupLegacyProtocols 必须有进程内缓存(只查一次)', as
|
|
|
447
487
|
|
|
448
488
|
const fnAt = SRC.indexOf('function cleanupLegacyProtocols()');
|
|
449
489
|
assert.ok(fnAt > 0, '找不到 cleanupLegacyProtocols(源码结构变了,请更新本测试)');
|
|
450
|
-
const body = SRC
|
|
490
|
+
const body = functionBody(SRC, 'cleanupLegacyProtocols');
|
|
451
491
|
|
|
452
492
|
assert.match(body, /if \(legacyProtocolsChecked\) return;/,
|
|
453
493
|
'重复调用必须直接返回 —— 否则每次配置变更都多开 PowerShell 读注册表');
|
|
@@ -471,67 +511,282 @@ test('I6: cleanupLegacyProtocols 必须有进程内缓存(只查一次)', as
|
|
|
471
511
|
//
|
|
472
512
|
// 正确顺序:**先**确认新键可用(reuse / 注册成功),**再**清老键。
|
|
473
513
|
//
|
|
474
|
-
// ## ⚠️
|
|
514
|
+
// ## ⚠️ 第二版仍是假绿(只读审查 B1,2026-10-22 实测)
|
|
515
|
+
//
|
|
516
|
+
// 第二版按行号比大小:只要每个裸 cleanup 的行号都「晚于 probeLine」就算过。
|
|
517
|
+
// 但 `lib/index.js:1809` 的 `const probed = probeJumpProtocol();` 正是函数体
|
|
518
|
+
// **第 2 行** → probeLine 恒为 2 → 任何 cleanup 行号都 > 2 → 断言恒成立。
|
|
519
|
+
//
|
|
520
|
+
// 实测(复制整包到临时目录、注入后跑本文件):
|
|
521
|
+
// 在 register 分支里、`registerJumpProtocol()` **之前**插一行裸
|
|
522
|
+
// `cleanupLegacyProtocols();` → 35 pass / 0 fail,**依然全绿**。
|
|
523
|
+
// 而那正是 I8 注释自述要挡的「更糟的回归」。
|
|
475
524
|
//
|
|
476
|
-
//
|
|
477
|
-
// `unregisterJumpProtocol();` 这行(子串里就含 `registerJumpProtocol()`),
|
|
478
|
-
// 而不是真正的 `registerJumpProtocol()` 调用。位置更靠前,于是断言恒成立。
|
|
525
|
+
// ## 现在的判据(白名单式,比大小 → 比归属)
|
|
479
526
|
//
|
|
480
|
-
//
|
|
481
|
-
//
|
|
527
|
+
// 不再问「它比谁早」,而是问「**它在哪个分支块里**」:
|
|
528
|
+
// ① 三个分支块(unregister / reuse / register)的位置先解析出来;
|
|
529
|
+
// ② 每一处 cleanup 调用必须**落在某个分支块内部** —— 落在函数顶层
|
|
530
|
+
// (任何分支之外)就是「无条件先清」,直接红;
|
|
531
|
+
// ③ register 块内部只允许 `if (okReg) cleanupLegacyProtocols();` 这一种形态,
|
|
532
|
+
// 裸调用一律红(注册可能失败,失败时必须保留老键)。
|
|
482
533
|
//
|
|
483
|
-
//
|
|
484
|
-
// ① register 分支内:`const okReg = registerJumpProtocol();` 在 `if (okReg) cleanup...` 之前
|
|
485
|
-
// ② 函数整体:不存在比 register 更早的裸 cleanup 调用(unregister 分支除外)
|
|
534
|
+
// 这样无论怎么调整语句顺序、加多少行注释,判据都不受影响。
|
|
486
535
|
function lineIndexOf(src, needle) {
|
|
487
536
|
const at = src.indexOf(needle);
|
|
488
537
|
if (at < 0) return -1;
|
|
489
538
|
return src.slice(0, at).split('\n').length; // 1-based 行号
|
|
490
539
|
}
|
|
491
540
|
|
|
492
|
-
|
|
541
|
+
// ══ 代码区判据:把「字符串冒充代码」的假绿堵死 ══════════════════════════════
|
|
542
|
+
//
|
|
543
|
+
// ## 为什么需要(只读复核 R3,2026-10-22 实测,两处都是全绿)
|
|
544
|
+
//
|
|
545
|
+
// 只要判据是「在这行里能匹配到某段文本」,那**把真代码换成一段含同样文本的
|
|
546
|
+
// 字符串**就能同时满足判据和破坏行为:
|
|
547
|
+
// · 把 reuse 分支的 `cleanupLegacyProtocols();` 换成
|
|
548
|
+
// `appendNotifyLog('…cleanupLegacyProtocols();…')` → I8 仍数到 3 处;
|
|
549
|
+
// · 把两条真守卫换成 `const _g = "__STUB_CHILD_PROCESS__ === true"` → I9 仍绿,
|
|
550
|
+
// 而测试会真的写开发者注册表。
|
|
551
|
+
//
|
|
552
|
+
// ## 判据
|
|
553
|
+
//
|
|
554
|
+
// `scanSource().masked` 里**字符串内容与注释一律是空格**,只有真代码非空格。
|
|
555
|
+
// 于是「匹配起点那一列在 masked 里是不是空格」就是最小充分判据:
|
|
556
|
+
// 起点在空格上 = 这段文本长在字符串/注释里,不是代码。
|
|
557
|
+
//
|
|
558
|
+
// 为什么只查**起点**而不是整段:跨行模板串会让整段覆盖多行,逐字符比对要跨行拼接;
|
|
559
|
+
// 起点足以区分「代码里的 `foo();`」与「字符串里的 `foo();`」,实测这条就够。
|
|
560
|
+
function isCodeAt(masked, lineIdx, col) {
|
|
561
|
+
const row = masked[lineIdx];
|
|
562
|
+
if (row == null) return false;
|
|
563
|
+
const ch = row[col];
|
|
564
|
+
return ch !== undefined && ch !== ' ';
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* 在单行 `lines[lineIdx]` 上跑 `re`,只返回**起点落在代码区**的匹配。
|
|
569
|
+
*
|
|
570
|
+
* @returns {Array<{col:number, text:string}>}
|
|
571
|
+
*/
|
|
572
|
+
function codeMatchesOnLine(lines, masked, lineIdx, re) {
|
|
573
|
+
const line = lines[lineIdx] || '';
|
|
574
|
+
const out = [];
|
|
575
|
+
const flags = re.flags.includes('g') ? re.flags : `${re.flags}g`;
|
|
576
|
+
const rx = new RegExp(re.source, flags);
|
|
577
|
+
let m;
|
|
578
|
+
while ((m = rx.exec(line)) !== null) {
|
|
579
|
+
if (isCodeAt(masked, lineIdx, m.index)) out.push({ col: m.index, text: m[0] });
|
|
580
|
+
if (m[0] === '') rx.lastIndex += 1; // 防零宽匹配死循环
|
|
581
|
+
}
|
|
582
|
+
return out;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/** 在多行上跑同一个 `re`,只返回代码区匹配(`line` 为 1-based)。 */
|
|
586
|
+
function codeMatches(lines, masked, re) {
|
|
587
|
+
const out = [];
|
|
588
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
589
|
+
for (const m of codeMatchesOnLine(lines, masked, i, re)) out.push({ line: i + 1, ...m });
|
|
590
|
+
}
|
|
591
|
+
return out;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* 整行 trim 后匹配 `re`,且要求**首个非空列落在代码区**。
|
|
596
|
+
*
|
|
597
|
+
* 用于「这一行是不是一句真语句」这类判据(`if (v) foo();` / `if (v) {`)。
|
|
598
|
+
*
|
|
599
|
+
* 为什么不直接用 `codeMatchesOnLine`:那些正则都带 `^` 锚点,
|
|
600
|
+
* 而 `masked` 保留了原始缩进 —— `^` 只能匹配到行首空格,永远匹配不上。
|
|
601
|
+
* 取 `trim()` 丢掉缩进后,再用「首个非空列」去问 `masked` 是不是空格,
|
|
602
|
+
* 就同时满足了「带锚点」与「必须是代码」两个要求。
|
|
603
|
+
*
|
|
604
|
+
* 反例(都实测过):`appendNotifyLog('if (okReg) cleanup();')` 的
|
|
605
|
+
* 首个非空列是 `a`(代码),但 trim 后不匹配带 `^if` 的正则 → 正确判否;
|
|
606
|
+
* `' if (okReg) cleanup();'`(整行是字符串内容)首个非空列在 masked 里是空格 → 判否。
|
|
607
|
+
*/
|
|
608
|
+
function statementInCode(lines, masked, lineIdx, re) {
|
|
609
|
+
const line = lines[lineIdx] || '';
|
|
610
|
+
const trimmed = line.trim();
|
|
611
|
+
if (!re.test(trimmed)) return false;
|
|
612
|
+
const firstCol = line.length - line.trimStart().length;
|
|
613
|
+
return isCodeAt(masked, lineIdx, firstCol);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* 从 `if (...)` 那行开始,按花括号配对找出这个分支块的 [起, 止] 行号(1-based,含)。
|
|
618
|
+
*
|
|
619
|
+
* @param {string[]} masked `scanSource().masked`(数括号用)
|
|
620
|
+
* @param {string[]} lines `scanSource().lines`(匹配头部用,字符串原文在)
|
|
621
|
+
* @param {RegExp} headerRe 匹配 `if (...)` 头部的正则
|
|
622
|
+
* @param {number} [withinLine] 可选:只要**包含这一行**的那个块。
|
|
623
|
+
* 不给则返回第一个匹配的块。
|
|
624
|
+
*
|
|
625
|
+
* ## 为什么需要 withinLine(自测发现,2026-10-22 实测)
|
|
626
|
+
*
|
|
627
|
+
* 原实现用 `findIndex` 取**第一个**匹配的 `if (v) {`。源码里只要出现两个
|
|
628
|
+
* 同条件的 if 块,就会认错块 → 假红:
|
|
629
|
+
*
|
|
630
|
+
* if (okReg) { /* 标记 */ } // ← 被 findIndex 选中
|
|
631
|
+
* if (okReg) {
|
|
632
|
+
* cleanupLegacyProtocols(); // ← 合法,却因为不在第一个块里被判「裸调用」
|
|
633
|
+
* }
|
|
634
|
+
*
|
|
635
|
+
* 实测该形态 34 pass / 1 fail。假红和假绿一样有害 —— 它训练人改测试让它变绿。
|
|
636
|
+
* 所以「找哪个块」必须由**被检查的那一行**决定,而不是靠「第一个匹配」。
|
|
637
|
+
*
|
|
638
|
+
* ## 为什么 lines 与 masked 分开(2026-10-22 实测)
|
|
639
|
+
*
|
|
640
|
+
* headerRe 要匹配 `if (plan.action === 'unregister')`,字符串原文必须在;
|
|
641
|
+
* 数括号又不能把字符串里的 `{` 算进去。所以两个数组各管一件事。
|
|
642
|
+
*
|
|
643
|
+
* ## 为什么开头也必须查代码区(只读复核 R3,2026-10-22 实测)
|
|
644
|
+
*
|
|
645
|
+
* 头部原先直接 `headerRe.test(lines[i])`,而 `lines` 里字符串原文还在。
|
|
646
|
+
* 于是**一条恰好含有这段文本的日志**会被认成「分支头」:
|
|
647
|
+
*
|
|
648
|
+
* appendNotifyLog(`if (plan.action === 'unregister') { cleanupLegacyProtocols(); }`);
|
|
649
|
+
*
|
|
650
|
+
* 这类日志会多认一个分支头,并从字符串行开始数括号 → 假红 35p/1f。
|
|
651
|
+
* 现在改用 `codeMatchesOnLine`:匹配**起点必须在代码区**。
|
|
652
|
+
* 注意不能简单地改用 `masked` 匹配 —— 头部本身含字符串字面量
|
|
653
|
+
* (`=== 'unregister'`),masked 会把它抹空,那样一条都匹配不到。
|
|
654
|
+
*/
|
|
655
|
+
function branchRange(masked, lines, headerRe, withinLine) {
|
|
656
|
+
const starts = [];
|
|
657
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
658
|
+
if (codeMatchesOnLine(lines, masked, i, headerRe).length > 0) starts.push(i);
|
|
659
|
+
}
|
|
660
|
+
if (starts.length === 0) return null;
|
|
661
|
+
|
|
662
|
+
const rangeOf = (start) => {
|
|
663
|
+
const end = findBlockEnd(masked, start);
|
|
664
|
+
return end < 0 ? null : [start + 1, end + 1];
|
|
665
|
+
};
|
|
666
|
+
|
|
667
|
+
if (withinLine == null) return rangeOf(starts[0]);
|
|
668
|
+
|
|
669
|
+
// 只认**包含目标行**的那个块;有多个包含时取最靠后的起点(最贴近的块)。
|
|
670
|
+
let best = null;
|
|
671
|
+
for (const s of starts) {
|
|
672
|
+
if (s + 1 > withinLine) continue; // 块在目标行之后才开 → 不可能包含
|
|
673
|
+
const r = rangeOf(s);
|
|
674
|
+
if (r && withinLine >= r[0] && withinLine <= r[1]) best = r;
|
|
675
|
+
}
|
|
676
|
+
return best;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
test('I8: cleanupLegacyProtocols 只能在「新键已可用」的分支里调用', async () => {
|
|
493
680
|
const { readFileSync } = await import('node:fs');
|
|
494
681
|
const { join, resolve, dirname } = await import('node:path');
|
|
495
682
|
const { fileURLToPath } = await import('node:url');
|
|
496
683
|
const ROOT_DIR = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
497
684
|
const SRC = readFileSync(join(ROOT_DIR, 'lib', 'index.js'), 'utf8');
|
|
498
685
|
|
|
499
|
-
const
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
686
|
+
const rawBody = functionBody(SRC, 'syncJumpProtocol');
|
|
687
|
+
// ⚠️ 必须用**剥掉注释**的行做扫描(只读审查 R2,2026-10-22 实测):
|
|
688
|
+
// 原先用原始行,把 reuse 分支的 `cleanupLegacyProtocols();` 注释掉
|
|
689
|
+
// (行为消失、文本还在)→ ③ 照样数到 3 处 → I8 全绿。
|
|
690
|
+
// 剥注释后这行不再计入,④ 的 `seen === 3` 就会红。
|
|
691
|
+
const { lines, masked } = scanSource(rawBody);
|
|
692
|
+
const body = lines.join('\n');
|
|
504
693
|
|
|
505
694
|
// ── ① register 分支内部:先注册,再按结果决定是否清理 ──────────────────
|
|
506
|
-
const regCall = body.indexOf('const okReg = registerJumpProtocol();');
|
|
507
|
-
assert.ok(regCall > 0,
|
|
508
|
-
'找不到 `const okReg = registerJumpProtocol();`(源码结构变了,请更新本测试)');
|
|
509
|
-
const guardedCleanup = body.indexOf('if (okReg) cleanupLegacyProtocols();');
|
|
510
|
-
assert.ok(guardedCleanup > 0,
|
|
511
|
-
'注册结果必须被用来决定是否清老键(失败时保留老键)');
|
|
512
|
-
assert.ok(guardedCleanup > regCall,
|
|
513
|
-
'if (okReg) cleanupLegacyProtocols() 必须排在 registerJumpProtocol() 之后');
|
|
514
|
-
|
|
515
|
-
// ── ② 全函数:不允许出现"比探测/注册更早"的裸 cleanup 调用 ────────────
|
|
516
695
|
//
|
|
517
|
-
//
|
|
518
|
-
//
|
|
519
|
-
//
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
696
|
+
// ⚠️ 不能用字面量匹配单个精确行(只读审查 R3,2026-10-22 实测):
|
|
697
|
+
// 原先断言 `if (okReg) cleanupLegacyProtocols();` 的**精确文本**,
|
|
698
|
+
// 于是两种语义完全等价的合法重构都会假红:
|
|
699
|
+
// · `if (okReg) { cleanupLegacyProtocols(); }`(加花括号、拆多行)
|
|
700
|
+
// · 把变量 `okReg` 改名成 `registered`
|
|
701
|
+
// 假红和假绿一样有害 —— 它训练人「改测试让它变绿」。
|
|
702
|
+
//
|
|
703
|
+
// 现在的判据是**语义**:先抓「哪个变量接住了 registerJumpProtocol() 的返回值」,
|
|
704
|
+
// 再要求 register 分支里的 cleanup 受**那个变量**守卫。名字随便改都不影响。
|
|
705
|
+
const regAssign = /\bconst\s+([A-Za-z_$][\w$]*)\s*=\s*registerJumpProtocol\(\)\s*;/.exec(body);
|
|
706
|
+
assert.ok(regAssign,
|
|
707
|
+
'找不到 `const xxx = registerJumpProtocol();`(源码结构变了,请更新本测试)');
|
|
708
|
+
const okVar = regAssign[1];
|
|
709
|
+
const regCallLine = lineIndexOf(body, regAssign[0]);
|
|
710
|
+
const escVar = okVar.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
711
|
+
|
|
712
|
+
/** 这个变量是否守卫了第 idx 行(0-based)的 cleanup 调用。 */
|
|
713
|
+
function guardedByVar(idx, varName) {
|
|
714
|
+
const esc = varName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
715
|
+
// ⚠️ 两种形态都必须查**代码区**(只读复核 R3,2026-10-22 实测):
|
|
716
|
+
// 原先只看 `lines[idx].trim()` 能否匹配,于是字符串里的同名文本照样算数 ——
|
|
717
|
+
// 一行 `appendNotifyLog('if (okReg) cleanupLegacyProtocols();')` 就算"受守卫"。
|
|
718
|
+
// 现在用 `statementInCode`:整行 trim 后匹配,且**首个非空列必须在代码区**。
|
|
719
|
+
//
|
|
720
|
+
// 形态 A:同一行 `if (v) cleanupLegacyProtocols();`
|
|
721
|
+
const formA = new RegExp(`^if\\s*\\(\\s*${esc}\\s*\\)\\s*cleanupLegacyProtocols\\s*\\(\\s*\\)\\s*;`);
|
|
722
|
+
if (statementInCode(lines, masked, idx, formA)) return true;
|
|
723
|
+
// 形态 B:包在 `if (v) { ... }` 块里 —— 往上找最近的 `if (v) {`,
|
|
724
|
+
// 并且必须确认**这个块真的包含目标行**(不能只看「上面出现过」)。
|
|
725
|
+
// ⚠️ 这里必须传 withinLine:源码里有两个同条件 if 块时,不传会认错块 → 假红。
|
|
726
|
+
const formB = new RegExp(`^if\\s*\\(\\s*${esc}\\s*\\)\\s*\\{`);
|
|
727
|
+
for (let j = idx - 1; j >= 0; j -= 1) {
|
|
728
|
+
if (!statementInCode(lines, masked, j, formB)) continue;
|
|
729
|
+
const r = branchRange(masked, lines, new RegExp(`^\\s*if\\s*\\(\\s*${esc}\\s*\\)\\s*\\{`), idx + 1);
|
|
730
|
+
if (r && idx + 1 >= r[0] && idx + 1 <= r[1]) return true;
|
|
731
|
+
}
|
|
732
|
+
return false;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// ── ② 三个分支块的真实范围 ──────────────────────────────────────────────
|
|
736
|
+
const ranges = {
|
|
737
|
+
unregister: branchRange(masked, lines, /if \(plan\.action === 'unregister'\)/),
|
|
738
|
+
reuse: branchRange(masked, lines, /if \(plan\.action === 'reuse'\)/),
|
|
739
|
+
register: branchRange(masked, lines, /if \(plan\.action === 'register'\)/),
|
|
740
|
+
};
|
|
741
|
+
for (const [k, v] of Object.entries(ranges)) {
|
|
742
|
+
assert.ok(v, `找不到 ${k} 分支块(源码结构变了,请更新本测试)`);
|
|
534
743
|
}
|
|
744
|
+
const inRange = (ln, [a, b]) => ln >= a && ln <= b;
|
|
745
|
+
|
|
746
|
+
// ── ③ 每一处 cleanup 调用都必须落在某个分支块内 ─────────────────────────
|
|
747
|
+
//
|
|
748
|
+
// ⚠️ 必须是**代码区**的调用,不能用 `lines` 裸匹配(只读复核 R3,2026-10-22 实测):
|
|
749
|
+
// 把 reuse 分支的 `cleanupLegacyProtocols();` 换成
|
|
750
|
+
// `appendNotifyLog('…cleanupLegacyProtocols();…')` —— 行为消失、文本还在 ——
|
|
751
|
+
// 原实现照样数到 3 处 → **36p/0f 全绿**。现在走 `codeMatches`,
|
|
752
|
+
// 匹配起点落在字符串里的一律不算。
|
|
753
|
+
const CLEANUP_CALL_RE = /cleanupLegacyProtocols\s*\(\s*\)\s*;/;
|
|
754
|
+
const calls = codeMatches(lines, masked, CLEANUP_CALL_RE);
|
|
755
|
+
|
|
756
|
+
let seen = 0;
|
|
757
|
+
let guardedInRegister = 0;
|
|
758
|
+
for (const call of calls) {
|
|
759
|
+
const ln = call.line;
|
|
760
|
+
const i = ln - 1;
|
|
761
|
+
seen++;
|
|
762
|
+
const where = Object.entries(ranges).find(([, r]) => inRange(ln, r));
|
|
763
|
+
assert.ok(where,
|
|
764
|
+
`第 ${ln} 行的 cleanupLegacyProtocols() 不在任何分支块内:`
|
|
765
|
+
+ `「${lines[i].trim()}」—— 那是无条件先清老键。`
|
|
766
|
+
+ '新键一旦注册失败用户就两头落空(连唤窗都没了)');
|
|
767
|
+
|
|
768
|
+
// register 分支里只允许受「注册结果」保护的那一种
|
|
769
|
+
if (where[0] === 'register') {
|
|
770
|
+
assert.ok(ln > regCallLine,
|
|
771
|
+
`register 分支第 ${ln} 行的 cleanupLegacyProtocols() 排在 `
|
|
772
|
+
+ `第 ${regCallLine} 行 registerJumpProtocol() 之前 —— 还没注册就先清了`);
|
|
773
|
+
assert.ok(guardedByVar(i, okVar),
|
|
774
|
+
`register 分支第 ${ln} 行的 cleanupLegacyProtocols() 必须受 `
|
|
775
|
+
+ `«if (${escVar})» 守卫 —— 裸调用会在注册失败时也清掉老键。`
|
|
776
|
+
+ `「${lines[i].trim()}」`);
|
|
777
|
+
guardedInRegister += 1;
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
// ── ④ 防"整段被删掉后无人可查"────────────────────────────────────────
|
|
782
|
+
assert.equal(seen, 3,
|
|
783
|
+
`syncJumpProtocol 里应有且只有 3 处 cleanupLegacyProtocols() 调用`
|
|
784
|
+
+ `(unregister / reuse / register-success),实际 ${seen} 处。`
|
|
785
|
+
+ '少了 = 某条路径不再清老键;多了 = 出现了未经判定的新调用点。'
|
|
786
|
+
+ '(只数**代码区**的调用:写在字符串/注释里的同名文本不算,'
|
|
787
|
+
+ '否则「注释掉调用、留一段文本」就能冒充"还在清")');
|
|
788
|
+
assert.equal(guardedInRegister, 1,
|
|
789
|
+
`register 分支里应有且只有 1 处受 ${escVar} 守卫的 cleanup,实际 ${guardedInRegister} 处`);
|
|
535
790
|
});
|
|
536
791
|
|
|
537
792
|
// ══ I9: 四个碰注册表的函数必须**全部**有测试守卫 ═════════════════════════════
|
|
@@ -547,6 +802,359 @@ test('I8: cleanupLegacyProtocols 必须在 registerJumpProtocol 之后调用', a
|
|
|
547
802
|
// 单测会往开发者(乃至 CI)机器上写注册表。开关关着的测试则会真的删。
|
|
548
803
|
//
|
|
549
804
|
// 只给 cleanup 补守卫 = 只堵了一半。四个函数必须同一口径。
|
|
805
|
+
//
|
|
806
|
+
// ## ⚠️ 裸子串会假绿(只读审查 B2,2026-10-22 实测)
|
|
807
|
+
//
|
|
808
|
+
// 第一版断言的是 `/__STUB_CHILD_PROCESS__/` 与 `/DSH_NOTIFY_NO_REGISTRY/`。
|
|
809
|
+
// 「真守卫」和「注释里提一句关键词」对子串匹配是同一个信号。
|
|
810
|
+
// 实测(index.js 副本):
|
|
811
|
+
// 删掉 register 的两条真守卫、只在旁边留一行
|
|
812
|
+
// `// 这里原本有 __STUB_CHILD_PROCESS__ 和 DSH_NOTIFY_NO_REGISTRY 守卫`
|
|
813
|
+
// → **I9 依然全绿**,而测试已经会真的写开发者的注册表。
|
|
814
|
+
//
|
|
815
|
+
// 现在要求匹配**守卫语句本身**(赋值/比较形态),且**必须在代码区**:
|
|
816
|
+
// 注释不可能满足(注释在 masked 里是空格),写在字符串里也不可能满足
|
|
817
|
+
// (字符串内容在 masked 里同样是空格)。
|
|
818
|
+
//
|
|
819
|
+
// ⚠️ 已删除 `stripComments`(只读复核 R3,2026-10-22 实测):
|
|
820
|
+
// 它只去注释、**不动字符串**,于是 `const _g = "__STUB_CHILD_PROCESS__ === true";`
|
|
821
|
+
// 能同时骗过形态与位置断言 —— 36p/0f 全绿而测试会真写注册表。
|
|
822
|
+
// 判据只留 `scanSource` 一套口径,不再有第二套"去注释"实现。
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* 词法扫描源码,产出两份**逐行等长**的数组(行号、列号都与原文一致):
|
|
826
|
+
*
|
|
827
|
+
* lines[i] 去掉注释,**字符串字面量原样保留**
|
|
828
|
+
* → 给正则匹配用(`if (plan.action === 'unregister')` 要能匹配到)
|
|
829
|
+
* masked[i] 去掉注释,且字符串/模板串的**内容抹成空格**
|
|
830
|
+
* → 给花括号计数用(`const m = "{";` 里的 `{` 不能算进深度)
|
|
831
|
+
*
|
|
832
|
+
* ## 为什么必须真词法扫描(2026-10-22 实测假红)
|
|
833
|
+
*
|
|
834
|
+
* 被扫的 `syncJumpProtocol` 里有跨行模板串:
|
|
835
|
+
* appendNotifyLog(
|
|
836
|
+
* `[jump-proto] 新键 ${JUMP_PROTOCOL}: 注册失败…`
|
|
837
|
+
* + `(${LEGACY_JUMP_PROTOCOLS.map((p) => p + '://').join(', ')})—— `
|
|
838
|
+
* + '留着它用户至少还能跳,删了两头落空'
|
|
839
|
+
* );
|
|
840
|
+
* 按字符裸扫 + 行尾复位 `'`/`"` 的实现,一进模板串就再也没出来(同一行后面的
|
|
841
|
+
* `'://'` 被当成开了普通字符串),之后所有 `}` 都被当成字符串内容吞掉 →
|
|
842
|
+
* 三个分支块全算成 [15,45](函数末尾)→ I8 假红
|
|
843
|
+
* 「register 分支里应有且只有 1 处受 okReg 守卫的 cleanup,实际 0 处」。
|
|
844
|
+
*
|
|
845
|
+
* 所以必须正确处理:模板串跨行、`${}` 嵌套(含嵌套里的字符串)、
|
|
846
|
+
* 字符串里的转义、注释里的引号。这也是为什么不再手写第二套扫描。
|
|
847
|
+
*/
|
|
848
|
+
function scanSource(src) {
|
|
849
|
+
const lines = []; // 去注释,字符串原样
|
|
850
|
+
const masked = []; // 去注释,字符串内容抹空
|
|
851
|
+
|
|
852
|
+
let line = '';
|
|
853
|
+
let mask = '';
|
|
854
|
+
|
|
855
|
+
const endLine = () => { lines.push(line); masked.push(mask); line = ''; mask = ''; };
|
|
856
|
+
|
|
857
|
+
/** 代码字符:两边都保留。 */
|
|
858
|
+
const keep = (ch) => {
|
|
859
|
+
if (ch === '\n') { endLine(); return; }
|
|
860
|
+
line += ch; mask += ch;
|
|
861
|
+
};
|
|
862
|
+
/** 字符串内容:`lines` 保留原文(正则需要),`masked` 抹空(括号不能算)。 */
|
|
863
|
+
const hide = (ch) => {
|
|
864
|
+
if (ch === '\n') { endLine(); return; }
|
|
865
|
+
line += ch; mask += ' ';
|
|
866
|
+
};
|
|
867
|
+
/** 注释内容:两边都不算代码;`lines` 用空格占位以保住列号。 */
|
|
868
|
+
const drop = (ch) => {
|
|
869
|
+
if (ch === '\n') { endLine(); return; }
|
|
870
|
+
line += ' '; mask += ' ';
|
|
871
|
+
};
|
|
872
|
+
|
|
873
|
+
// 模式栈,栈底是 code。元素:{m:'code'} | {m:'str',q} | {m:'tpl'} | {m:'expr',depth}
|
|
874
|
+
// expr = 模板串 `${...}` 的表达式内部:按代码扫描,`}` 深度归零时弹回 tpl。
|
|
875
|
+
const modes = [{ m: 'code' }];
|
|
876
|
+
let i = 0;
|
|
877
|
+
|
|
878
|
+
while (i < src.length) {
|
|
879
|
+
const ch = src[i];
|
|
880
|
+
const top = modes[modes.length - 1];
|
|
881
|
+
|
|
882
|
+
// ── 普通字符串 ───────────────────────────────────────────────
|
|
883
|
+
if (top.m === 'str') {
|
|
884
|
+
if (ch === '\\' && src[i + 1] !== undefined) { hide(ch); hide(src[i + 1]); i += 2; continue; }
|
|
885
|
+
if (ch === top.q) { hide(ch); modes.pop(); i += 1; continue; }
|
|
886
|
+
if (ch === '\n') { hide(ch); modes.pop(); i += 1; continue; } // 防呆:普通串不跨行
|
|
887
|
+
hide(ch); i += 1; continue;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// ── 模板串 ───────────────────────────────────────────────────
|
|
891
|
+
if (top.m === 'tpl') {
|
|
892
|
+
if (ch === '\\' && src[i + 1] !== undefined) { hide(ch); hide(src[i + 1]); i += 2; continue; }
|
|
893
|
+
if (ch === '`') { hide(ch); modes.pop(); i += 1; continue; }
|
|
894
|
+
if (ch === '$' && src[i + 1] === '{') {
|
|
895
|
+
hide(ch); hide('{');
|
|
896
|
+
modes.push({ m: 'expr', depth: 0 });
|
|
897
|
+
i += 2; continue;
|
|
898
|
+
}
|
|
899
|
+
hide(ch); i += 1; continue; // 允许跨行
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
// ── ${...} 内部:} 归零则回到 tpl ─────────────────────────────
|
|
903
|
+
if (top.m === 'expr') {
|
|
904
|
+
if (ch === '{') { top.depth += 1; keep(ch); i += 1; continue; }
|
|
905
|
+
if (ch === '}') {
|
|
906
|
+
if (top.depth === 0) { hide(ch); modes.pop(); i += 1; continue; }
|
|
907
|
+
top.depth -= 1; keep(ch); i += 1; continue;
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
// ── 注释 ─────────────────────────────────────────────────────
|
|
912
|
+
if (ch === '/' && src[i + 1] === '/') {
|
|
913
|
+
while (i < src.length && src[i] !== '\n') { drop(src[i]); i += 1; }
|
|
914
|
+
continue;
|
|
915
|
+
}
|
|
916
|
+
if (ch === '/' && src[i + 1] === '*') {
|
|
917
|
+
drop('/'); drop('*'); i += 2;
|
|
918
|
+
while (i < src.length) {
|
|
919
|
+
if (src[i] === '*' && src[i + 1] === '/') { drop('*'); drop('/'); i += 2; break; }
|
|
920
|
+
drop(src[i]); i += 1;
|
|
921
|
+
}
|
|
922
|
+
continue;
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
// ── 开串 ─────────────────────────────────────────────────────
|
|
926
|
+
if (ch === "'" || ch === '"') { hide(ch); modes.push({ m: 'str', q: ch }); i += 1; continue; }
|
|
927
|
+
if (ch === '`') { hide(ch); modes.push({ m: 'tpl' }); i += 1; continue; }
|
|
928
|
+
|
|
929
|
+
keep(ch); i += 1;
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
endLine();
|
|
933
|
+
return { lines, masked };
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
/**
|
|
937
|
+
* 找出 `masked` 里的**正则字面量**,返回 `{line, col, text}`(`line` 1-based)。
|
|
938
|
+
*
|
|
939
|
+
* ## 为什么需要(只读复核 R3,2026-10-22 实测)
|
|
940
|
+
*
|
|
941
|
+
* I10 原先只查「含 `\{` 或 `\}` 的正则」,于是下面这条完整绕过它:
|
|
942
|
+
* 在 `if (okReg) {` 里塞 `const _re = /{/;` ← 无转义,检查不到
|
|
943
|
+
* 再把块撑到函数末尾,并在文件别处插 `const _c = /}/;` 补平括号
|
|
944
|
+
* → 整文件级配平仍然通过,而「块外裸调 cleanup」会被算成「受 okReg 守卫」,
|
|
945
|
+
* I8 的主判据整体旁路,**36p/0f 全绿**。
|
|
946
|
+
* `/[{}]/`、`/[({]/` 这类字符类写法同样不在原检查面内。
|
|
947
|
+
*
|
|
948
|
+
* ## 判别方式(标准 JS 词法启发式)
|
|
949
|
+
*
|
|
950
|
+
* 正则字面量只能出现在「表达式起始」位置,即前一个非空字符是
|
|
951
|
+
* `( , = : [ ! & | ? { } ;` 之一或行首;而 `/` 作为除号时前面是
|
|
952
|
+
* 标识符、数字、`)`、`]`、字符串结尾。据此在本仓代码上无误判(已实测)。
|
|
953
|
+
*
|
|
954
|
+
* 只认**同一行闭合**的字面量:跨行正则里出现花括号的情况本仓不存在,
|
|
955
|
+
* 漏检会由 I10 的「按函数体配平」兜住(正则撑歪括号 → 该函数体配不平)。
|
|
956
|
+
*/
|
|
957
|
+
function findRegexLiterals(masked) {
|
|
958
|
+
const out = [];
|
|
959
|
+
const BEFORE = /[(,=:[!&|?{};]/; // 允许出现在正则左侧的字符
|
|
960
|
+
for (let i = 0; i < masked.length; i += 1) {
|
|
961
|
+
const line = masked[i] || '';
|
|
962
|
+
for (let k = 0; k < line.length; k += 1) {
|
|
963
|
+
if (line[k] !== '/') continue;
|
|
964
|
+
if (line[k + 1] === '/' || line[k + 1] === '*') continue; // 注释已被 scanSource 剥掉,防御性跳过
|
|
965
|
+
// 前一个非空字符
|
|
966
|
+
let p = k - 1;
|
|
967
|
+
while (p >= 0 && line[p] === ' ') p -= 1;
|
|
968
|
+
const atExpressionStart = p < 0 || BEFORE.test(line[p]);
|
|
969
|
+
if (!atExpressionStart) continue;
|
|
970
|
+
// 同一行内找闭合的 `/`(跳过转义与字符类)
|
|
971
|
+
let q = k + 1;
|
|
972
|
+
let inClass = false;
|
|
973
|
+
let closed = -1;
|
|
974
|
+
while (q < line.length) {
|
|
975
|
+
const ch = line[q];
|
|
976
|
+
if (ch === '\\') { q += 2; continue; }
|
|
977
|
+
if (ch === '[') { inClass = true; q += 1; continue; }
|
|
978
|
+
if (ch === ']') { inClass = false; q += 1; continue; }
|
|
979
|
+
if (ch === '/' && !inClass) { closed = q; break; }
|
|
980
|
+
q += 1;
|
|
981
|
+
}
|
|
982
|
+
if (closed < 0) continue; // 跨行正则:不在此处判定,交给配平自检兜底
|
|
983
|
+
out.push({ line: i + 1, col: k, text: line.slice(k, closed + 1) });
|
|
984
|
+
k = closed;
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
return out;
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
/**
|
|
991
|
+
* 自检:`scanSource` 的两条硬保证 —— 逐行等长、括号配平。
|
|
992
|
+
*
|
|
993
|
+
* 为什么要在**生产测试里**做这个(2026-10-22 自查发现):
|
|
994
|
+
*
|
|
995
|
+
* `scanSource` 是手写状态机,它不认识**正则字面量**。实测:
|
|
996
|
+
* const re = /\{/; → masked 里多出一个 `{`
|
|
997
|
+
* const re = /}/; → masked 里多出一个 `}`
|
|
998
|
+
* const re = /["']/; → 引号被当成开串,**整行后半截被抹空**
|
|
999
|
+
* 前两种会让某个块的结束行算错 → 判据指向错误位置;第三种更糟,会把紧随其后的
|
|
1000
|
+
* 代码一起吞掉。行注释 `// foo({` 不受影响(注释先被识别)。
|
|
1001
|
+
*
|
|
1002
|
+
* 现在被扫的 5 个函数里**一个正则字面量都没有**(已实测),所以当前无误判。
|
|
1003
|
+
* 但这是个静默陷阱:将来有人在这些函数里写 `/^\{/`,判据会悄悄失准而不报错。
|
|
1004
|
+
* 所以这里把它变成**显式断言**:
|
|
1005
|
+
* ① 两份数组必须逐行等长、且与原文同长(否则 code.slice(0,k) 取错列)
|
|
1006
|
+
* ② 剥出来的代码里 `{` 与 `}` 必须配平(否则说明有字面量/正则被误算)
|
|
1007
|
+
* ③ 被扫的函数体里不许出现**任何**含花括号的正则字面量 ——
|
|
1008
|
+
* 不论转义形态(`/\{/`)还是字符类形态(`/[{}]/`)
|
|
1009
|
+
* ④ **每个函数体各自配平**(不只整文件配平)—— 见 findRegexLiterals 里的
|
|
1010
|
+
* 绕过实例:一处多算 `{`、别处补一个 `}`,整文件级看不出来
|
|
1011
|
+
* —— 一旦有人踩到,测试会红并直接告诉他原因,而不是等到判据莫名其妙地假红。
|
|
1012
|
+
*/
|
|
1013
|
+
test('I10: scanSource 自检(逐行等长 / 逐函数体配平 / 无含花括号正则)', async () => {
|
|
1014
|
+
const { readFileSync } = await import('node:fs');
|
|
1015
|
+
const { join, resolve, dirname } = await import('node:path');
|
|
1016
|
+
const { fileURLToPath } = await import('node:url');
|
|
1017
|
+
const ROOT_DIR = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
1018
|
+
|
|
1019
|
+
const SCANNED_FNS = ['probeJumpProtocol', 'registerJumpProtocol', 'unregisterJumpProtocol',
|
|
1020
|
+
'cleanupLegacyProtocols', 'syncJumpProtocol'];
|
|
1021
|
+
|
|
1022
|
+
for (const rel of [['lib', 'index.js'], ['lib', 'client.js']]) {
|
|
1023
|
+
const src = readFileSync(join(ROOT_DIR, ...rel), 'utf8');
|
|
1024
|
+
const rawLines = src.split('\n');
|
|
1025
|
+
const { lines, masked } = scanSource(src);
|
|
1026
|
+
const name = rel.join('/');
|
|
1027
|
+
|
|
1028
|
+
assert.equal(lines.length, rawLines.length,
|
|
1029
|
+
`${name}: scanSource 产出的行数必须与原文一致(换行不能被吞)`);
|
|
1030
|
+
assert.equal(masked.length, lines.length,
|
|
1031
|
+
`${name}: lines 与 masked 必须逐行等长`);
|
|
1032
|
+
|
|
1033
|
+
for (let i = 0; i < rawLines.length; i += 1) {
|
|
1034
|
+
assert.equal(masked[i].length, lines[i].length,
|
|
1035
|
+
`${name}:${i + 1} 行 masked 与 lines 长度不等 —— 列号会错位,head 会取错`);
|
|
1036
|
+
assert.equal(lines[i].length, rawLines[i].length,
|
|
1037
|
+
`${name}:${i + 1} 行 lines 与原文长度不等 —— 行号会错位`);
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
// 括号配平:整文件级别的粗校验,能抓到「某个字面量里的括号被算进深度」
|
|
1041
|
+
const stripped = masked.join('\n').replace(/[^{}]/g, '');
|
|
1042
|
+
const open = (stripped.match(/\{/g) || []).length;
|
|
1043
|
+
const close = (stripped.match(/\}/g) || []).length;
|
|
1044
|
+
assert.equal(open, close,
|
|
1045
|
+
`${name}: 剥出的代码里 { (${open}) 与 } (${close}) 不配平 —— `
|
|
1046
|
+
+ '说明有字符串/正则里的花括号被当成了代码括号。');
|
|
1047
|
+
|
|
1048
|
+
// ③ + ④ 逐函数体:括号必须配平,且不许出现含花括号的正则字面量
|
|
1049
|
+
for (const fn of SCANNED_FNS) {
|
|
1050
|
+
const at = src.indexOf(`function ${fn}(`);
|
|
1051
|
+
if (at < 0) continue; // 函数没了由 I8/I9 报,这里不管
|
|
1052
|
+
const body = functionBody(src, fn);
|
|
1053
|
+
const scanned = scanSource(body);
|
|
1054
|
+
|
|
1055
|
+
const braces = scanned.masked.join('\n').replace(/[^{}]/g, '');
|
|
1056
|
+
const o = (braces.match(/\{/g) || []).length;
|
|
1057
|
+
const c = (braces.match(/\}/g) || []).length;
|
|
1058
|
+
assert.equal(o, c,
|
|
1059
|
+
`${fn} 的函数体自身括号不配平({ ${o} / } ${c})—— `
|
|
1060
|
+
+ '这会让「找块边界」整段失准。\n'
|
|
1061
|
+
+ '常见原因:函数里有含花括号的正则字面量(scanSource 不识别正则)。\n'
|
|
1062
|
+
+ '修法:改用 new RegExp(\'\\\\{\') / new RegExp(\'}\'),或给 scanSource 补正则识别。');
|
|
1063
|
+
|
|
1064
|
+
const bad = findRegexLiterals(scanned.masked)
|
|
1065
|
+
.filter((r) => /[{}]/.test(r.text));
|
|
1066
|
+
assert.deepEqual(bad, [],
|
|
1067
|
+
`${fn} 里出现了含花括号的正则字面量,scanSource 会把它们算进块深度:\n`
|
|
1068
|
+
+ bad.map((r) => ` · 第 ${r.line} 行:${r.text}`).join('\n') + '\n'
|
|
1069
|
+
+ '这会让 I8/I9 的块边界静默失准 —— 实测 `/ {/`(无转义)配合别处补一个 `/ }/` '
|
|
1070
|
+
+ '可以整体旁路 I8 判据而仍然全绿。\n'
|
|
1071
|
+
+ '修法二选一:① 改用 new RegExp(\'\\\\{\');② 给 scanSource 补上正则字面量的识别。');
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
});
|
|
1075
|
+
|
|
1076
|
+
/**
|
|
1077
|
+
* 按花括号配对维护一个「开启块」栈,扫描 `masked` 里 [起, 止列] 这段。
|
|
1078
|
+
*
|
|
1079
|
+
* @param {string[]} masked `scanSource().masked`(字符串内容已抹空)
|
|
1080
|
+
* @param {number} limitLine 扫到第几行(0-based,含)
|
|
1081
|
+
* @param {number} limitCol limitLine 上扫到第几列(不含)
|
|
1082
|
+
* @returns {Array<{line:number, head:string}>} 未闭合块的栈
|
|
1083
|
+
*
|
|
1084
|
+
* ⚠️ 只接受 `masked`,不接受原始行 —— 见 scanSource 的注释:
|
|
1085
|
+
* 拿含字符串原文的行去数括号,会被 `const m = "{";` 或跨行模板串撑歪。
|
|
1086
|
+
*/
|
|
1087
|
+
function scanBlockOpeners(masked, limitLine, limitCol) {
|
|
1088
|
+
const openers = []; // 栈:{ line, head }
|
|
1089
|
+
for (let i = 0; i <= limitLine; i += 1) {
|
|
1090
|
+
const code = masked[i] || '';
|
|
1091
|
+
const end = (i === limitLine) ? limitCol : code.length;
|
|
1092
|
+
for (let k = 0; k < end; k += 1) {
|
|
1093
|
+
const ch = code[k];
|
|
1094
|
+
if (ch === '{') openers.push({ line: i, head: code.slice(0, k + 1).trim() });
|
|
1095
|
+
else if (ch === '}') openers.pop();
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
return openers;
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
/**
|
|
1102
|
+
* 从 `start` 行(0-based)开始,按花括号配对找出这个块的**结束行**(0-based,含)。
|
|
1103
|
+
* 找不到(文件里括号不配对)时返回 -1。
|
|
1104
|
+
*
|
|
1105
|
+
* @param {string[]} masked `scanSource().masked`
|
|
1106
|
+
*/
|
|
1107
|
+
function findBlockEnd(masked, start) {
|
|
1108
|
+
let depth = 0;
|
|
1109
|
+
let opened = false;
|
|
1110
|
+
for (let i = start; i < masked.length; i += 1) {
|
|
1111
|
+
for (const ch of masked[i] || '') {
|
|
1112
|
+
if (ch === '{') { depth += 1; opened = true; } else if (ch === '}') depth -= 1;
|
|
1113
|
+
}
|
|
1114
|
+
if (opened && depth === 0) return i;
|
|
1115
|
+
}
|
|
1116
|
+
return -1;
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
/**
|
|
1120
|
+
* 这一行是否处在「函数体顶层」或「函数体顶层 try 块」里 —— 而不是嵌在
|
|
1121
|
+
* 别的 `if` / `while` / 回调里。
|
|
1122
|
+
*
|
|
1123
|
+
* ## 为什么需要(只读审查 R1 补充,2026-10-22 实测)
|
|
1124
|
+
*
|
|
1125
|
+
* 只查「守卫语句存在 + 排在效果语句之前」还会漏一种:把守卫塞进永不执行的分支
|
|
1126
|
+
* `if (false) { <两条守卫> }`
|
|
1127
|
+
* —— 形态合规、行号也在效果之前,但守卫根本不执行,测试照样真的写注册表。
|
|
1128
|
+
* 实测该变异 **35p/0f 全绿**。
|
|
1129
|
+
*
|
|
1130
|
+
* 判据:从文件头一直扫到「守卫关键词所在的那一列(不含)」,
|
|
1131
|
+
* 最内层开启块的 head 必须是 `try {` 或函数声明行。
|
|
1132
|
+
* head 截到 `{` 为止,所以守卫与函数声明压在同一行时也不会被误杀。
|
|
1133
|
+
*
|
|
1134
|
+
* @param {string[]} masked `scanSource().masked`
|
|
1135
|
+
*/
|
|
1136
|
+
function isTopLevelInFunction(masked, idx0, colOfGuard) {
|
|
1137
|
+
const limitCol = Number.isFinite(colOfGuard) ? colOfGuard : (masked[idx0] || '').length;
|
|
1138
|
+
const openers = scanBlockOpeners(masked, idx0, limitCol);
|
|
1139
|
+
if (openers.length === 0) return false;
|
|
1140
|
+
const inner = openers[openers.length - 1].head;
|
|
1141
|
+
// 顶层 try 块
|
|
1142
|
+
if (/^try\s*\{$/.test(inner)) return true;
|
|
1143
|
+
// 函数体本身:head 形如 `function foo() {` / `export function foo(a, b) {`
|
|
1144
|
+
// / `async function foo() {`
|
|
1145
|
+
if (/^(?:export\s+)?(?:async\s+)?function\s+[A-Za-z_$][\w$]*\s*\([^)]*\)\s*\{$/.test(inner)) return true;
|
|
1146
|
+
return false;
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
/** 真正的守卫语句:把 `__STUB_CHILD_PROCESS__` / `DSH_NOTIFY_NO_REGISTRY` 当**条件**用。 */
|
|
1150
|
+
//
|
|
1151
|
+
// ⚠️ 只匹配「形态」还是不够(只读审查 R1,2026-10-22 实测):
|
|
1152
|
+
// 把 register 的两条守卫整体挪到**写注册表之后** —— 形态完全合规、
|
|
1153
|
+
// 但已经守卫不了任何东西 —— I9 仍然 35p/0f 全绿。
|
|
1154
|
+
// 所以位置也要查,见下面 I9 的 ④。
|
|
1155
|
+
const STUB_GUARD_RE = /__STUB_CHILD_PROCESS__\s*===\s*true/;
|
|
1156
|
+
const NOREG_GUARD_RE = /process\.env\.DSH_NOTIFY_NO_REGISTRY\s*===\s*['"]1['"]/;
|
|
1157
|
+
|
|
550
1158
|
test('I9: probe/register/unregister/cleanup 四个函数都必须有测试守卫', async () => {
|
|
551
1159
|
const { readFileSync } = await import('node:fs');
|
|
552
1160
|
const { join, resolve, dirname } = await import('node:path');
|
|
@@ -561,14 +1169,79 @@ test('I9: probe/register/unregister/cleanup 四个函数都必须有测试守卫
|
|
|
561
1169
|
'cleanupLegacyProtocols',
|
|
562
1170
|
];
|
|
563
1171
|
|
|
1172
|
+
// ⚠️ ④ 位置判据(只读审查 R1,2026-10-22 实测):
|
|
1173
|
+
// 光验「守卫语句存在且形态对」会 fail-open —— 把两条守卫整体**挪到
|
|
1174
|
+
// 写注册表之后**,形态完全合规、但已经守卫不了任何东西,仍然全绿。
|
|
1175
|
+
// 所以还要查:**每一条**碰注册表的语句都必须排在**最后一条守卫之后**。
|
|
1176
|
+
//
|
|
1177
|
+
// 「碰注册表」= 读/写/删三类的真实调用或命令串。宁可多列几个锚点,
|
|
1178
|
+
// 也不要漏 —— 判据方向是"我认识的必须都被挡住",认不出来的新写法
|
|
1179
|
+
// 会被 fns 清单和 I8 的白名单分别兜一层。
|
|
1180
|
+
const REGISTRY_EFFECTS = [
|
|
1181
|
+
{ re: /readProtocolRegistration\s*\(/, what: 'readProtocolRegistration()(读注册表)' },
|
|
1182
|
+
{ re: /\bexecFileSync\s*\(/, what: 'execFileSync()(起 PowerShell 读写注册表)' },
|
|
1183
|
+
{ re: /Remove-Item/, what: 'Remove-Item(删注册表键)' },
|
|
1184
|
+
{ re: /Set-ItemProperty/, what: 'Set-ItemProperty(写注册表键)' },
|
|
1185
|
+
{ re: /New-Item /, what: 'New-Item(建注册表键)' },
|
|
1186
|
+
];
|
|
1187
|
+
|
|
564
1188
|
for (const fn of fns) {
|
|
565
1189
|
const at = SRC.indexOf(`function ${fn}(`);
|
|
566
1190
|
assert.ok(at > 0, `找不到 ${fn}(源码结构变了,请更新本测试)`);
|
|
567
|
-
const
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
1191
|
+
const src = functionBody(SRC, fn);
|
|
1192
|
+
|
|
1193
|
+
// ⚠️ 不再用 `stripComments` 的纯文本匹配(只读复核 R3,2026-10-22 实测):
|
|
1194
|
+
// `stripComments` 只去注释、**不动字符串**,于是把两条真守卫整体换成
|
|
1195
|
+
// const _g = "__STUB_CHILD_PROCESS__ === true";
|
|
1196
|
+
// 就能同时骗过形态断言与位置断言 —— **36p/0f 全绿**,而测试会真的写注册表。
|
|
1197
|
+
// 现在统一走 `scanSource`:`masked` 里字符串与注释都是空格,
|
|
1198
|
+
// 只有真代码非空格;`codeMatches` 只认起点在代码区的匹配。
|
|
1199
|
+
const scanned = scanSource(src); // 按行剥注释,行号不变
|
|
1200
|
+
const code = scanned.lines;
|
|
1201
|
+
const masked = scanned.masked;
|
|
1202
|
+
|
|
1203
|
+
const guardHits = (gre) => codeMatches(code, masked, gre);
|
|
1204
|
+
|
|
1205
|
+
assert.ok(guardHits(STUB_GUARD_RE).length > 0,
|
|
1206
|
+
`${fn} 必须有 «__STUB_CHILD_PROCESS__ === true» 守卫 —— `
|
|
1207
|
+
+ '否则走 loader 的测试会真的碰注册表。'
|
|
1208
|
+
+ '(判据是**代码区**的守卫语句本身;写在注释或字符串里都不算)');
|
|
1209
|
+
assert.ok(guardHits(NOREG_GUARD_RE).length > 0,
|
|
1210
|
+
`${fn} 必须有 «process.env.DSH_NOTIFY_NO_REGISTRY === "1"» 守卫 —— `
|
|
1211
|
+
+ '否则单测会真的读写开发者机器注册表。'
|
|
1212
|
+
+ '(判据是**代码区**的守卫语句本身;写在注释或字符串里都不算)');
|
|
1213
|
+
|
|
1214
|
+
// ④ 位置:守卫必须**排在所有碰注册表的语句之前**
|
|
1215
|
+
const idxOf = (gre) => {
|
|
1216
|
+
const hits = guardHits(gre);
|
|
1217
|
+
return hits.length > 0 ? hits[hits.length - 1].line : -1;
|
|
1218
|
+
};
|
|
1219
|
+
const lastGuard = Math.max(idxOf(STUB_GUARD_RE), idxOf(NOREG_GUARD_RE));
|
|
1220
|
+
assert.ok(lastGuard > 0, `${fn} 找不到守卫所在行(不该发生,上一步已匹配到)`);
|
|
1221
|
+
|
|
1222
|
+
// ⑤ 守卫必须**真的会执行**:不能塞在 if/while/回调里(R1 补充,实测)
|
|
1223
|
+
for (const gre of [STUB_GUARD_RE, NOREG_GUARD_RE]) {
|
|
1224
|
+
for (const hit of guardHits(gre)) {
|
|
1225
|
+
const gLine = hit.line;
|
|
1226
|
+
assert.ok(isTopLevelInFunction(masked, gLine - 1, hit.col),
|
|
1227
|
+
`${fn} 第 ${gLine} 行的守卫不在函数体顶层(或顶层 try 的顶层)—— `
|
|
1228
|
+
+ '它被包在别的条件/回调里,可能永远不执行。'
|
|
1229
|
+
+ `「${code[gLine - 1].trim()}」\n`
|
|
1230
|
+
+ '实测反例:`if (false) { <两条守卫> }` 形态合规、行号也够早,'
|
|
1231
|
+
+ '但守卫不执行,测试照样真的写注册表。');
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
for (const eff of REGISTRY_EFFECTS) {
|
|
1236
|
+
const hits = codeMatches(code, masked, eff.re);
|
|
1237
|
+
if (hits.length === 0) continue;
|
|
1238
|
+
const effLine = hits[0].line;
|
|
1239
|
+
assert.ok(effLine > lastGuard,
|
|
1240
|
+
`${fn} 的「${eff.what}」在第 ${effLine} 行,`
|
|
1241
|
+
+ `但守卫在第 ${lastGuard} 行之后才结束 —— 守卫形同虚设。\n`
|
|
1242
|
+
+ '守卫必须写在**任何**碰注册表的语句之前,'
|
|
1243
|
+
+ '否则测试会真的读写开发者(乃至 CI)机器的注册表。');
|
|
1244
|
+
}
|
|
572
1245
|
}
|
|
573
1246
|
});
|
|
574
1247
|
// ══ I7: 调用 apply() 的测试必须显式关闭注册表(防误删用户注册)════════════
|