dsh-notify-say 0.4.61 → 0.4.63

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,132 @@
1
+ /**
2
+ * v0.4.62:Toast 标题 / 第三行 / 多会话跳转规则 / 队列。
3
+ *
4
+ * 背景(用户 2026-10-19 的决定):
5
+ * ① Toast 标题改成会话标题(sessionProjections 的 title 投影),
6
+ * 因为点 Toast 跳不到指定会话(dsh://open 是固定串),
7
+ * 至少让标题告诉用户"是哪个会话完成了"。
8
+ * ② 第三行放项目名,但只在标题里没包含它时才给。
9
+ * ③ 多会话跳转规则做成可选,默认 earliest。
10
+ *
11
+ * 这些断言的价值:三者都是"看着对但很容易悄悄做错"的地方 ——
12
+ * 标题拿不到时的兜底、第三行的去重条件、队列的排序与去重语义。
13
+ */
14
+ import { test } from 'node:test';
15
+ import assert from 'node:assert/strict';
16
+ import { readFileSync } from 'node:fs';
17
+ import { fileURLToPath } from 'node:url';
18
+ import { dirname, join } from 'node:path';
19
+
20
+ const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
21
+ const INDEX = readFileSync(join(ROOT, 'lib/index.js'), 'utf8');
22
+ const CLIENT = readFileSync(join(ROOT, 'lib/client.js'), 'utf8');
23
+ const BRIDGE = readFileSync(join(ROOT, 'lib/session-bridge.js'), 'utf8');
24
+ const PS1 = readFileSync(join(ROOT, 'dsh-notify.ps1'), 'utf8');
25
+ const SCHEMA = readFileSync(join(ROOT, 'lib/config-schema.js'), 'utf8');
26
+
27
+ function grab(src, start, end) {
28
+ const i = src.indexOf(start);
29
+ assert.ok(i >= 0, '切片起点缺失: ' + start);
30
+ // 终点:从起点之后找下一个「同缩进或更浅的 function 声明」或指定锚点
31
+ const rest = src.slice(i + start.length);
32
+ const m = rest.search(/\n {0,4}(?:function |\/\*\*)/);
33
+ const j = m >= 0 ? i + start.length + m : -1;
34
+ if (j <= i) { const k = src.indexOf(end, i); assert.ok(k > i, '切片终点缺失: ' + start); return src.slice(i, k); }
35
+ return src.slice(i, j);
36
+ }
37
+
38
+ // ══ T1-T3:会话标题读取 ═════════════════════════════════════════════════
39
+ test('T1: 用 sessionProjections 的 title 投影读会话标题', () => {
40
+ const fn = grab(INDEX, 'function sessionTitleOf(session) {', '\n }\n');
41
+ assert.match(fn, /svc\('sessionProjections'\)/, '必须取 sessionProjections 服务');
42
+ assert.match(fn, /stateOf\(session, 'title'\)/, '必须读 title 投影');
43
+ });
44
+
45
+ test('T2: 标题三级兜底 —— 服务挂了也要有结果,绝不抛出', () => {
46
+ const fn = grab(INDEX, 'function sessionTitleOf(session) {', '\n }\n');
47
+ // 三级来源都要在本函数体内(① sessionTitle ② sessionProjections ③ id 前缀)
48
+ assert.match(fn, /svc\('sessionTitle'\)/, '① 先用 sessionTitle 服务');
49
+ assert.match(fn, /svc\('sessionProjections'\)/, '② 再用 title 投影');
50
+ assert.match(fn, /sessionKey\(session\)/, '③ 最后退回会话 id');
51
+ assert.match(fn, /try\s*\{[\s\S]*?catch\s*\{/, '每一级都要自己兜异常');
52
+ assert.doesNotMatch(fn, /^\s*throw /m, '任何路径都不该抛出');
53
+ });
54
+
55
+ test('T3: stateOf 的两种返回形状都要兼容(字符串 / {val})', () => {
56
+ const fn = grab(INDEX, 'function sessionTitleOf(session) {', '\n }\n');
57
+ assert.match(fn, /typeof v === 'string'/, '要兼容直接返回字符串');
58
+ assert.match(fn, /v\.val/, '要兼容返回 { ver, seq, val } 行');
59
+ });
60
+
61
+ // ══ T4-T6:Toast 第三行 ═════════════════════════════════════════════════
62
+ test('T4: 项目名取 cwd 最后一段', () => {
63
+ const fn = grab(INDEX, 'function projectNameOf(session) {', '\n }\n');
64
+ assert.match(fn, /session\.header\.cwd/, '从 header.cwd 取');
65
+ assert.match(fn, /split\(\/\[\\\\\/\]\+\/\)/, '要同时兼容 \\ 和 /');
66
+ assert.match(fn, /parts\[parts\.length - 1\]/, '取最后一段');
67
+ });
68
+
69
+ test('T5: 标题里已包含项目名时不给第三行(防重复)', () => {
70
+ assert.match(
71
+ INDEX,
72
+ /if \(detail && String\(title \|\| ''\)\.toLowerCase\(\)\.includes\(detail\.toLowerCase\(\)\)\) detail = ''/,
73
+ '去重条件必须存在且不区分大小写',
74
+ );
75
+ });
76
+
77
+ test('T6: ps1 模板按需加第三行 —— 空 Detail 不加 <text>', () => {
78
+ assert.match(PS1, /\[string\]\$Detail = "",/, '必须有 -Detail 参数且默认空');
79
+ assert.match(PS1, /if \(\$Detail -ne ""\) \{ \$detailXml = "<text>"/, '空值不能加 <text>');
80
+ assert.match(PS1, /\$bodyXml<\/text>\$detailXml<\/binding>/, '模板要接上 detailXml');
81
+ });
82
+
83
+ // ══ T7-T9:队列语义 ═════════════════════════════════════════════════════
84
+ test('T7: session-bridge 已从单槽改为队列', () => {
85
+ assert.match(BRIDGE, /let pending = \[\];/, '必须是数组');
86
+ assert.doesNotMatch(BRIDGE, /let last = null;/, '旧的单槽必须消失');
87
+ });
88
+
89
+ test('T8: 同一会话重复完成不刷新 at(否则"最早"语义失效)', () => {
90
+ assert.match(BRIDGE, /if \(pending\.some\(\(e\) => e\.sessionId === sid\)\) return;/, '必须按会话去重');
91
+ const fn = grab(BRIDGE, 'function setLast(sessionId, now) {', '\n }\n');
92
+ assert.match(fn, /pending\.sort\(\(a, b\) => a\.at - b\.at\)/, '必须按 at 升序');
93
+ });
94
+
95
+ test('T9: consume 取走整个队列并清空,同时保留旧字段', () => {
96
+ assert.match(BRIDGE, /const taken = pending;/, '要整体取走');
97
+ assert.match(BRIDGE, /pending = \[\];/, '取走即清空');
98
+ assert.match(BRIDGE, /sessions: taken, sessionId: taken\[0\]\.sessionId, at: taken\[0\]\.at/, '顶层要保留旧字段给老客户端');
99
+ });
100
+
101
+ // ══ T10-T12:规则选择 ═══════════════════════════════════════════════════
102
+ test('T10: pickJumpTarget 三种规则 + 单会话不受规则影响', () => {
103
+ const fn = grab(CLIENT, 'function pickJumpTarget(list, rule) {', '\n }\n');
104
+ assert.match(fn, /if \(list\.length === 1\) return list\[0\]/, '单会话必须直接返回它');
105
+ assert.match(fn, /if \(rule === 'none'\) return null/, 'none 返回 null');
106
+ assert.match(fn, /if \(rule === 'latest'\) return sorted\[sorted\.length - 1\]/, 'latest 取末尾');
107
+ assert.match(fn, /return sorted\[0\]/, '默认(含未知规则)取最早');
108
+ });
109
+
110
+ test('T11: 默认规则是 earliest(服务端与客户端一致)', () => {
111
+ assert.match(SCHEMA, /multiJumpRule: 'earliest'/, 'FIELD_DEFAULTS 必须是 earliest');
112
+ // ⚠️ schemastery 没有 z.enum,必须是 union+const(踩坑:z.enum 直接 TypeError)
113
+ assert.match(SCHEMA, /\.union\(\[z\.const\('earliest'\), z\.const\('latest'\), z\.const\('none'\)\]\)/, '三值用 union+const');
114
+ assert.doesNotMatch(SCHEMA, /z\.enum\(/, 'schemastery 不支持 z.enum');
115
+ assert.match(CLIENT, /typeof v\.multiJumpRule === 'string' \? v\.multiJumpRule : 'earliest'/, '客户端兜底也得是 earliest');
116
+ assert.match(CLIENT, /var curRule = typeof v\.multiJumpRule === 'string' \? v\.multiJumpRule : 'earliest'/, 'GUI 兜底也得是 earliest');
117
+ });
118
+
119
+ test('T12: 客户端能同时吃新队列和旧单条响应', () => {
120
+ assert.match(CLIENT, /if \(doc\.sessions && doc\.sessions\.length\)/, '优先读新字段');
121
+ assert.match(CLIENT, /else if \(typeof doc\.sessionId === 'string' && doc\.sessionId\)/, '旧形状也要吃');
122
+ });
123
+
124
+ // ══ T13:配置透传链路 ══════════════════════════════════════════════════
125
+ test('T13: multiJumpRule 在 schema/store/index 三处白名单与透传都在', () => {
126
+ const store = readFileSync(join(ROOT, 'lib/config-store.js'), 'utf8');
127
+ for (const [name, src] of [['config-store', store], ['index', INDEX]]) {
128
+ assert.match(src, /jumpWhenForeground', 'multiJumpRule'/, name + ' 白名单');
129
+ }
130
+ assert.match(store, /multiJumpRule: cfg\.multiJumpRule,/, 'config-store 透传');
131
+ assert.match(INDEX, /multiJumpRule: g\.multiJumpRule,/, 'effectiveForClient 透传');
132
+ });