dsh-soul 0.6.2 → 0.7.1
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/LICENSE +21 -0
- package/README.md +41 -23
- package/README_EN.md +45 -23
- package/assets/icon.svg +21 -0
- package/client/index.mjs +538 -101
- package/index.mjs +577 -93
- package/lib/config.mjs +56 -8
- package/lib/injection.mjs +2 -2
- package/lib/personas.mjs +184 -0
- package/lib/store.mjs +125 -0
- package/package.json +16 -4
- package/scripts/lib/skip-report.mjs +73 -0
- package/scripts/verify-client.mjs +700 -0
- package/scripts/verify-compat.mjs +58 -9
- package/scripts/verify-config.mjs +1308 -3
- package/scripts/verify-e2e-prompt.mjs +627 -0
- package/scripts/verify-live-prompt.mjs +445 -0
- package/scripts/verify-nav-icon.mjs +479 -0
- package/scripts/verify-store.mjs +310 -0
- package/scripts/verify-trail.mjs +21 -8
|
@@ -3,23 +3,179 @@
|
|
|
3
3
|
// 覆盖 lib/config.mjs 的 migrateConfig / sanitizeConfig:
|
|
4
4
|
// - 旧版本 style+tone 迁移、废弃字段清理、特质脏数据回退
|
|
5
5
|
// - 白名单 / 类型 / 长度 / 枚举校验的接受与拒绝路径
|
|
6
|
+
// 覆盖 lib/personas.mjs 的内置人设与合并 / 匹配语义(含「应用后必须命中自己」闭环)。
|
|
7
|
+
// 另含清单契约自检:插件图标(DSH 在 app-boot 的 iconOf 中判定)、前后端契约一致性。
|
|
6
8
|
// 零依赖,直接 `node scripts/verify-config.mjs` 运行。
|
|
7
9
|
|
|
8
10
|
import assert from 'node:assert/strict'
|
|
11
|
+
import { existsSync, readdirSync, readFileSync, realpathSync, statSync } from 'node:fs'
|
|
12
|
+
import { createRequire } from 'node:module'
|
|
13
|
+
import { homedir } from 'node:os'
|
|
14
|
+
import { extname, isAbsolute, join, relative, resolve, sep } from 'node:path'
|
|
15
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
9
16
|
import {
|
|
10
17
|
DEFAULT_CONFIG,
|
|
11
18
|
FIELD_LIMITS,
|
|
19
|
+
LANGUAGE_VALUES,
|
|
20
|
+
PERSONA_FIELDS,
|
|
21
|
+
PERSONA_NAME_MAX,
|
|
22
|
+
PROFILE_FIELDS,
|
|
23
|
+
PROMPT_INPUT_FIELDS,
|
|
24
|
+
REPLY_LENGTH_VALUES,
|
|
25
|
+
STYLE_VALUES,
|
|
26
|
+
TRAIT_VALUES,
|
|
12
27
|
migrateConfig,
|
|
13
28
|
normalizePersonas,
|
|
14
29
|
sanitizeConfig,
|
|
15
30
|
sanitizePersonaName
|
|
16
31
|
} from '../lib/config.mjs'
|
|
32
|
+
import {
|
|
33
|
+
BUILTIN_PERSONAS,
|
|
34
|
+
builtinPersonaNames,
|
|
35
|
+
declaredPersonaKeys,
|
|
36
|
+
isBuiltinPersona,
|
|
37
|
+
mergePersonas,
|
|
38
|
+
personaMatches,
|
|
39
|
+
resolvePersona
|
|
40
|
+
} from '../lib/personas.mjs'
|
|
41
|
+
import { assertCount, skipNote } from './lib/skip-report.mjs'
|
|
42
|
+
|
|
43
|
+
// 基线运行应跑出的断言数。跑完时校验,防止「脚本加/删用例」与文档口径悄悄脱节。
|
|
44
|
+
//
|
|
45
|
+
// 注意:本脚本没有环境相关的跳过分支(只读仓库内的文件与纯函数,不依赖浏览器 / DSH /
|
|
46
|
+
// 平台能力),所以链里的 `--strict` 对它是空操作 —— 这是**事实描述**,不是缺陷。
|
|
47
|
+
// 它的数字由这里的 assertCount 守住;需要 --strict 的是那些有跳过能力的脚本。
|
|
48
|
+
const EXPECTED_ASSERTIONS = 91
|
|
17
49
|
|
|
18
50
|
let passed = 0
|
|
51
|
+
const failures = []
|
|
19
52
|
function check(name, fn) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
53
|
+
try {
|
|
54
|
+
fn()
|
|
55
|
+
passed++
|
|
56
|
+
console.log(` ✓ ${name}`)
|
|
57
|
+
} catch (err) {
|
|
58
|
+
failures.push({ name, err })
|
|
59
|
+
console.log(` ✗ ${name}`)
|
|
60
|
+
console.log(` ${String(err && err.message || err).split('\n')[0]}`)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 按括号配平取出从 anchor 起的那一个对象字面量。
|
|
65
|
+
// 扫描时会跳过字符串与行注释 —— 文案里出现的 `{n}` 占位符、行尾的 `// {` 之类
|
|
66
|
+
// 都不该影响配平(否则「取出来的对象」会一路吃到文件末尾)。
|
|
67
|
+
function extractObject(source, anchor) {
|
|
68
|
+
const at = source.indexOf(anchor)
|
|
69
|
+
if (at === -1) return ''
|
|
70
|
+
let i = source.indexOf('{', at)
|
|
71
|
+
if (i === -1) return ''
|
|
72
|
+
const from = i
|
|
73
|
+
let depth = 0
|
|
74
|
+
let quote = null
|
|
75
|
+
for (; i < source.length; i++) {
|
|
76
|
+
const c = source[i]
|
|
77
|
+
if (quote) {
|
|
78
|
+
if (c === '\\') { i++; continue }
|
|
79
|
+
if (c === quote) quote = null
|
|
80
|
+
continue
|
|
81
|
+
}
|
|
82
|
+
if (c === "'" || c === '"' || c === '`') { quote = c; continue }
|
|
83
|
+
if (c === '/' && source[i + 1] === '/') {
|
|
84
|
+
const nl = source.indexOf('\n', i)
|
|
85
|
+
i = nl === -1 ? source.length : nl
|
|
86
|
+
continue
|
|
87
|
+
}
|
|
88
|
+
if (c === '{') depth++
|
|
89
|
+
else if (c === '}') { depth--; if (depth === 0) return source.slice(from, i + 1) }
|
|
90
|
+
}
|
|
91
|
+
return source.slice(from)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 键必须是「行首(允许缩进)的 key:」—— 只按 `\w+:` 扫会把文案里的冒号误判成键
|
|
95
|
+
// (例如 'Use /soul: show' 里的 soul)。
|
|
96
|
+
function objectKeys(text) {
|
|
97
|
+
const keys = new Set()
|
|
98
|
+
for (const m of text.matchAll(/^[ \t]*(?:'([^']+)'|"([^"]+)"|([A-Za-z_$][\w$]*))\s*:/gm)) {
|
|
99
|
+
keys.add(m[1] ?? m[2] ?? m[3])
|
|
100
|
+
}
|
|
101
|
+
return keys
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// 剥离行注释 / 块注释(字符串内容保留——它们是真实代码的一部分)。
|
|
105
|
+
// **长度保持不变**:被剥掉的字符以空格占位。这样剥离结果与原串共享同一套下标,
|
|
106
|
+
// extractCall 才能先在剥离文本上定位锚点、再回原串切片。
|
|
107
|
+
// 所有「对源码做正则断言」的地方都应先过这一步:否则把真实调用注释掉、在注释里留下
|
|
108
|
+
// 同样的字样,断言就被 satisfies(M5 类漏洞,变异实验实测可绕过)。
|
|
109
|
+
function stripComments(source) {
|
|
110
|
+
const out = source.split('')
|
|
111
|
+
let i = 0
|
|
112
|
+
let quote = null
|
|
113
|
+
const blank = (from, to) => { for (let k = from; k < to && k < out.length; k++) if (out[k] !== '\n') out[k] = ' ' }
|
|
114
|
+
while (i < source.length) {
|
|
115
|
+
const c = source[i]
|
|
116
|
+
if (quote) {
|
|
117
|
+
if (c === '\\') { i += 2; continue }
|
|
118
|
+
if (c === quote) quote = null
|
|
119
|
+
i++
|
|
120
|
+
continue
|
|
121
|
+
}
|
|
122
|
+
if (c === "'" || c === '"' || c === '`') { quote = c; i++; continue }
|
|
123
|
+
if (c === '/' && source[i + 1] === '/') {
|
|
124
|
+
const nl = source.indexOf('\n', i)
|
|
125
|
+
blank(i, nl === -1 ? source.length : nl)
|
|
126
|
+
i = nl === -1 ? source.length : nl
|
|
127
|
+
continue
|
|
128
|
+
}
|
|
129
|
+
if (c === '/' && source[i + 1] === '*') {
|
|
130
|
+
const end = source.indexOf('*/', i + 2)
|
|
131
|
+
const to = end === -1 ? source.length : end + 2
|
|
132
|
+
blank(i, to)
|
|
133
|
+
i = to
|
|
134
|
+
continue
|
|
135
|
+
}
|
|
136
|
+
i++
|
|
137
|
+
}
|
|
138
|
+
return out.join('')
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 从 anchor 起按括号配平取出**完整调用**文本。定位与配平都在**去注释后**的文本上进行
|
|
142
|
+
// (注释里的 anchor 与不等宽括号都不该影响判定),再回**原串**切片 —— stripComments
|
|
143
|
+
// 刻意保持长度不变,两套文本共享同一套下标。
|
|
144
|
+
// anchor 可带可不带 `(`:`skipExit(` 与 `e('span', { ... }`(调用括号在 anchor 内)都支持,
|
|
145
|
+
// 一律取「anchor 起点之后的第一个 `(`」作为配平起点。
|
|
146
|
+
// 之所以不用缩进/固定窗口猜终点:后者会随代码重排而捕获越界或假失败(徽标断言曾用
|
|
147
|
+
// `\s{22}` 定界,实测把摘要列、操作区全部吞进捕获,断言失去判断力)。
|
|
148
|
+
function extractCall(source, anchor) {
|
|
149
|
+
const stripped = stripComments(source)
|
|
150
|
+
const at = stripped.indexOf(anchor)
|
|
151
|
+
if (at === -1) return ''
|
|
152
|
+
let open = -1
|
|
153
|
+
for (let i = at; i < stripped.length; i++) {
|
|
154
|
+
if (stripped[i] === '(') { open = i; break }
|
|
155
|
+
if (stripped[i] === '\n' && i > at + anchor.length) return ''
|
|
156
|
+
}
|
|
157
|
+
if (open === -1) return ''
|
|
158
|
+
let depth = 0
|
|
159
|
+
let quote = null
|
|
160
|
+
for (let i = open; i < stripped.length; i++) {
|
|
161
|
+
const c = stripped[i]
|
|
162
|
+
if (quote) {
|
|
163
|
+
if (c === '\\') { i++; continue }
|
|
164
|
+
if (c === quote) quote = null
|
|
165
|
+
continue
|
|
166
|
+
}
|
|
167
|
+
if (c === "'" || c === '"' || c === '`') { quote = c; continue }
|
|
168
|
+
if (c === '(') depth++
|
|
169
|
+
else if (c === ')') { depth--; if (depth === 0) return source.slice(at, i + 1) }
|
|
170
|
+
}
|
|
171
|
+
return ''
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// 取某个具名文案表内 zh / en 各自的键集(表名 → 在表内定位 `zh: {` / `en: {`)。
|
|
175
|
+
function tableLangKeys(source, tableName, lang) {
|
|
176
|
+
const at = source.indexOf(`const ${tableName} = {`)
|
|
177
|
+
if (at === -1) return new Set()
|
|
178
|
+
return objectKeys(extractObject(source.slice(at), lang === 'zh' ? 'zh: {' : 'en: {'))
|
|
23
179
|
}
|
|
24
180
|
|
|
25
181
|
console.log('migrateConfig')
|
|
@@ -177,6 +333,76 @@ check('migrateConfig:personas 归一化透传,缺省时保持缺席;确认
|
|
|
177
333
|
assert.equal(dirty.requireToolConfirmation, false)
|
|
178
334
|
})
|
|
179
335
|
|
|
336
|
+
console.log('特质:表格 / 回复长度')
|
|
337
|
+
|
|
338
|
+
const indexSource = readFileSync(new URL('../index.mjs', import.meta.url), 'utf8')
|
|
339
|
+
// 断言一律对**去注释后**的源码做匹配:注释里写一句 // const VERSION = '0.0.0' 或
|
|
340
|
+
// // await writeConfigFile(...) 就能骗过裸正则(M5 类,变异实验实测可绕过)。
|
|
341
|
+
// stripComments 长度保持不变,因此位置与字符串内容都不受影响。
|
|
342
|
+
const indexCode = stripComments(indexSource)
|
|
343
|
+
|
|
344
|
+
// 从 index.mjs 的 PROMPT_TEXT 中按缩进切片取出某个文案子表(zh / en 各一处)。
|
|
345
|
+
// 用「块数量必须为 2」做结构断言:文案表若被重构或改名,这里会直接失败而不是静默放过。
|
|
346
|
+
function promptTextBlocks(key) {
|
|
347
|
+
const re = new RegExp(`\\n ${key}: \\{\\n([\\s\\S]*?)\\n \\\},`, 'g')
|
|
348
|
+
return [...indexCode.matchAll(re)].map((m) => m[1])
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
check('DEFAULT_CONFIG 含表格与回复长度的默认值', () => {
|
|
352
|
+
assert.equal(DEFAULT_CONFIG.tables, 'default')
|
|
353
|
+
assert.equal(DEFAULT_CONFIG.replyLength, 'normal')
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
check('sanitizeConfig:表格与回复长度合法值通过', () => {
|
|
357
|
+
const { patch, errors } = sanitizeConfig({ tables: 'more', replyLength: 'detailed' })
|
|
358
|
+
assert.deepEqual(errors, {})
|
|
359
|
+
assert.equal(patch.tables, 'more')
|
|
360
|
+
assert.equal(patch.replyLength, 'detailed')
|
|
361
|
+
})
|
|
362
|
+
|
|
363
|
+
check('sanitizeConfig:表格与回复长度非法枚举被拒绝', () => {
|
|
364
|
+
assert.ok(sanitizeConfig({ tables: 'always' }).errors.tables)
|
|
365
|
+
assert.ok(sanitizeConfig({ replyLength: 'verbose' }).errors.replyLength)
|
|
366
|
+
assert.equal('tables' in sanitizeConfig({ tables: 'x' }).patch, false)
|
|
367
|
+
assert.equal('replyLength' in sanitizeConfig({ replyLength: 'x' }).patch, false)
|
|
368
|
+
})
|
|
369
|
+
|
|
370
|
+
check('migrateConfig:表格与回复长度脏数据回退为默认值', () => {
|
|
371
|
+
const dirty = migrateConfig({ tables: 'max', replyLength: 3 })
|
|
372
|
+
assert.equal(dirty.tables, 'default')
|
|
373
|
+
assert.equal(dirty.replyLength, 'normal')
|
|
374
|
+
})
|
|
375
|
+
|
|
376
|
+
check('PERSONA_FIELDS 覆盖新增的两个维度(人设预设可保存与还原)', () => {
|
|
377
|
+
assert.ok(PERSONA_FIELDS.includes('tables'))
|
|
378
|
+
assert.ok(PERSONA_FIELDS.includes('replyLength'))
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
check('提示词文案:zh / en 均含表格与回复长度,且块数量恰为 2', () => {
|
|
382
|
+
for (const key of ['tables', 'replyLength']) {
|
|
383
|
+
const blocks = promptTextBlocks(key)
|
|
384
|
+
assert.equal(blocks.length, 2, `${key} 文案块数量应为 2(zh / en)`)
|
|
385
|
+
for (const block of blocks) {
|
|
386
|
+
assert.match(block, /.{20,}/, `${key} 文案块不应为空`)
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
const tables = promptTextBlocks('tables')
|
|
390
|
+
for (const block of tables) {
|
|
391
|
+
assert.ok(block.includes('more:') && block.includes('less:'), 'tables 文案应含 more / less')
|
|
392
|
+
assert.equal(block.includes('default:'), false, 'tables=default 不应产生提示词文案')
|
|
393
|
+
}
|
|
394
|
+
})
|
|
395
|
+
|
|
396
|
+
check('提示词文案:replyLength 的 normal(适中)不产生任何文案', () => {
|
|
397
|
+
// 这是「新增维度不改变现有用户行为」的核心不变量:
|
|
398
|
+
// buildBehavior 仅在文案表命中时 push,所以 normal 必须没有对应键。
|
|
399
|
+
const blocks = promptTextBlocks('replyLength')
|
|
400
|
+
for (const block of blocks) {
|
|
401
|
+
assert.ok(block.includes('concise:') && block.includes('detailed:'), 'replyLength 文案应含 concise / detailed')
|
|
402
|
+
assert.equal(block.includes('normal:'), false, 'replyLength=normal 不应产生提示词文案')
|
|
403
|
+
}
|
|
404
|
+
})
|
|
405
|
+
|
|
180
406
|
console.log('输入框光轨')
|
|
181
407
|
|
|
182
408
|
check('DEFAULT_CONFIG 含光轨默认值', () => {
|
|
@@ -232,4 +458,1083 @@ check('migrateConfig:合法光轨配置保留,颜色转大写', () => {
|
|
|
232
458
|
assert.equal(config.trailWidth, 'thick')
|
|
233
459
|
})
|
|
234
460
|
|
|
461
|
+
console.log('插件清单与图标')
|
|
462
|
+
|
|
463
|
+
// DSH 读取 package.json 的 icon 字段(app-boot 的 readPluginMeta / iconOf),
|
|
464
|
+
// 在插件管理列表与侧栏入口渲染。图标缺失只是回退默认插图,但路径写错、
|
|
465
|
+
// 或没进 files 白名单,就会出现「仓库里有、装完没有」的静默失败,故固化为检查。
|
|
466
|
+
const manifestPath = fileURLToPath(new URL('../package.json', import.meta.url))
|
|
467
|
+
const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'))
|
|
468
|
+
const packageRoot = resolve(manifestPath, '..')
|
|
469
|
+
const iconRef = typeof manifest.icon === 'string' ? manifest.icon : ''
|
|
470
|
+
const iconPath = iconRef ? resolve(packageRoot, iconRef) : ''
|
|
471
|
+
const iconRelative = iconRef ? relative(packageRoot, iconPath) : ''
|
|
472
|
+
|
|
473
|
+
check('清单:icon 为包内相对路径,且指向真实文件', () => {
|
|
474
|
+
assert.ok(iconRef, 'package.json 缺少 icon 字段')
|
|
475
|
+
// 宿主的判定(dsh-app-boot 的 iconOf):
|
|
476
|
+
// 1) 绝对路径、Windows 盘符、以及任何带 URL scheme 的值直接抛错
|
|
477
|
+
// 2) 以 manifest 所在目录的 realpath 为根,relative(root, realpath(target)) 不得为 ..
|
|
478
|
+
assert.equal(isAbsolute(iconRef), false, 'icon 必须是相对路径(会随包一起发布)')
|
|
479
|
+
assert.equal(/^[A-Za-z][A-Za-z\d+.-]*:/.test(iconRef), false, 'icon 不能是带 scheme 的 URL(宿主只接受相对路径)')
|
|
480
|
+
assert.equal(statSync(iconPath).isFile(), true, `icon 指向的不是文件:${iconRef}`)
|
|
481
|
+
const outside = relative(realpathSync(packageRoot), realpathSync(iconPath))
|
|
482
|
+
assert.equal(
|
|
483
|
+
outside === '..' || outside.startsWith(`..${sep}`) || isAbsolute(outside),
|
|
484
|
+
false,
|
|
485
|
+
'icon 不能通过符号链接逃出包目录'
|
|
486
|
+
)
|
|
487
|
+
})
|
|
488
|
+
|
|
489
|
+
check('清单:icon 扩展名在宿主白名单内(SVG / PNG / JPEG / WebP)', () => {
|
|
490
|
+
assert.ok(
|
|
491
|
+
['.svg', '.png', '.jpg', '.jpeg', '.webp'].includes(extname(iconPath).toLowerCase()),
|
|
492
|
+
`宿主只接受 SVG / PNG / JPEG / WebP,当前为 ${extname(iconPath) || '(无扩展名)'}`
|
|
493
|
+
)
|
|
494
|
+
})
|
|
495
|
+
|
|
496
|
+
check('清单:icon 体积不超过 256 KiB(宿主上限,超出会拒绝装载)', () => {
|
|
497
|
+
const { size } = statSync(iconPath)
|
|
498
|
+
assert.ok(size > 0, 'icon 不应为空文件')
|
|
499
|
+
assert.ok(size <= 256 * 1024, `icon 为 ${size} 字节,超过宿主上限 262144 字节`)
|
|
500
|
+
})
|
|
501
|
+
|
|
502
|
+
check('清单:icon 落在 files 白名单内(会随 npm 包一起发布)', () => {
|
|
503
|
+
assert.ok(Array.isArray(manifest.files) && manifest.files.length > 0, 'package.json 缺少 files 白名单')
|
|
504
|
+
const topLevel = iconRelative.split(sep)[0]
|
|
505
|
+
assert.ok(manifest.files.includes(topLevel), `files 白名单应包含 ${topLevel},否则图标不会进发布包`)
|
|
506
|
+
})
|
|
507
|
+
|
|
508
|
+
const iconSvg = iconRef ? readFileSync(iconPath, 'utf8') : ''
|
|
509
|
+
|
|
510
|
+
check('图标:SVG 自带命名空间,且 viewBox 为正方形', () => {
|
|
511
|
+
assert.match(iconSvg, /<svg[\s>]/, 'icon 不是 SVG 文档')
|
|
512
|
+
assert.match(iconSvg, /xmlns="http:\/\/www\.w3\.org\/2000\/svg"/, 'SVG 缺少 xmlns,脱离独立文档渲染时会失败')
|
|
513
|
+
const viewBox = iconSvg.match(/viewBox="([\d.\-\s]+)"/)
|
|
514
|
+
assert.ok(viewBox, 'SVG 缺少 viewBox,无法按容器尺寸缩放')
|
|
515
|
+
const parts = viewBox[1].trim().split(/\s+/).map(Number)
|
|
516
|
+
assert.equal(parts.length, 4, 'viewBox 应为 4 个数字')
|
|
517
|
+
assert.equal(parts[2], parts[3], 'viewBox 应为正方形(图标显示区是方的,非方会被拉伸或留白)')
|
|
518
|
+
})
|
|
519
|
+
|
|
520
|
+
check('图标:不含脚本、位图与外部引用(发布物第一方自足)', () => {
|
|
521
|
+
assert.equal(/<script/i.test(iconSvg), false, 'SVG 不应内嵌脚本')
|
|
522
|
+
assert.equal(/<image[\s>]/i.test(iconSvg), false, 'SVG 不应内嵌位图')
|
|
523
|
+
assert.equal(/<foreignObject/i.test(iconSvg), false, 'SVG 不应使用 foreignObject')
|
|
524
|
+
assert.equal(/(?:href|xlink:href)\s*=/i.test(iconSvg), false, 'SVG 不应包含任何链接引用')
|
|
525
|
+
const withoutXmlns = iconSvg.replace(/xmlns="http:\/\/www\.w3\.org\/2000\/svg"/g, '')
|
|
526
|
+
assert.equal(/https?:\/\//i.test(withoutXmlns), false, 'SVG 不应引用外部资源(xmlns 除外)')
|
|
527
|
+
})
|
|
528
|
+
|
|
529
|
+
check('图标:渐变引用都能解析到已定义的 id', () => {
|
|
530
|
+
const defined = new Set([...iconSvg.matchAll(/\sid="([^"]+)"/g)].map((m) => m[1]))
|
|
531
|
+
const refs = [...iconSvg.matchAll(/url\(#([^)]+)\)/g)].map((m) => m[1])
|
|
532
|
+
assert.ok(refs.length > 0, '图标应使用渐变填充,以适配浅色 / 深色主题')
|
|
533
|
+
for (const ref of refs) {
|
|
534
|
+
assert.ok(defined.has(ref), `引用了未定义的 id:${ref}`)
|
|
535
|
+
}
|
|
536
|
+
})
|
|
537
|
+
|
|
538
|
+
// ==================== 内置人设预设 ====================
|
|
539
|
+
|
|
540
|
+
console.log('\n内置人设预设')
|
|
541
|
+
|
|
542
|
+
check('内置人设非空,名称合法且不超上限', () => {
|
|
543
|
+
const names = builtinPersonaNames()
|
|
544
|
+
assert.ok(names.length >= 4, `内置人设过少:${names.length}`)
|
|
545
|
+
for (const name of names) {
|
|
546
|
+
assert.equal(sanitizePersonaName(name), name, `内置名不合法:${name}`)
|
|
547
|
+
assert.ok(name.length <= PERSONA_NAME_MAX, `内置名超长:${name}`)
|
|
548
|
+
assert.ok(name.trim() === name, `内置名含首尾空白:${name}`)
|
|
549
|
+
}
|
|
550
|
+
})
|
|
551
|
+
|
|
552
|
+
check('内置人设只声明 PERSONA_FIELDS 内的键', () => {
|
|
553
|
+
for (const name of builtinPersonaNames()) {
|
|
554
|
+
for (const key of Object.keys(BUILTIN_PERSONAS[name])) {
|
|
555
|
+
assert.ok(PERSONA_FIELDS.includes(key), `${name} 含非人设字段:${key}`)
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
})
|
|
559
|
+
|
|
560
|
+
check('内置人设不声明「关于你」、输出语言与总开关', () => {
|
|
561
|
+
// 「关于你」的三个字段根本不在 PERSONA_FIELDS 里(见下面「预设范围」一组),这里
|
|
562
|
+
// 另外钉住内置数据本身也不写;language 在预设范围内,但内置是给任何语言用户共用的
|
|
563
|
+
// 通用人格,写死会让另一种语言的用户应用一次就被强行切回;enabled 是总开关,
|
|
564
|
+
// 预设不该能替你关掉个性化。
|
|
565
|
+
for (const name of builtinPersonaNames()) {
|
|
566
|
+
for (const key of [...PROFILE_FIELDS, 'language', 'enabled']) {
|
|
567
|
+
assert.equal(key in BUILTIN_PERSONAS[name], false, `${name} 不应声明 ${key}`)
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
})
|
|
571
|
+
|
|
572
|
+
// ==================== 预设范围:「关于你」不属于预设 ====================
|
|
573
|
+
|
|
574
|
+
check('预设范围只含人设,与「关于你」无交集', () => {
|
|
575
|
+
// 总纲:三个身份字段不在 PERSONA_FIELDS 里,于是保存(快照)/ 应用(取键)/
|
|
576
|
+
// 匹配(判据)/ 磁盘归一化四条路径都会自然地把它们排除在外 —— 不止内置预设,
|
|
577
|
+
// 用户自建预设也一样。
|
|
578
|
+
assert.deepEqual(PROFILE_FIELDS, ['nickname', 'occupation', 'bio'])
|
|
579
|
+
for (const key of PROFILE_FIELDS) {
|
|
580
|
+
assert.equal(PERSONA_FIELDS.includes(key), false, `「关于你」的 ${key} 不应出现在 PERSONA_FIELDS`)
|
|
581
|
+
}
|
|
582
|
+
// 反向防空过:人设维度必须还在,否则「无交集」会因为 PERSONA_FIELDS 被误清空而假通过
|
|
583
|
+
assert.ok(PERSONA_FIELDS.includes('style'), 'style 应在预设范围内')
|
|
584
|
+
assert.ok(PERSONA_FIELDS.includes('customInstructions'), 'customInstructions 应在预设范围内')
|
|
585
|
+
// 输出语言属于人设(决定 Agent 用什么语言作答):自建预设会保存并在应用时还原它
|
|
586
|
+
assert.ok(PERSONA_FIELDS.includes('language'), 'language 应在预设范围内')
|
|
587
|
+
assert.equal(PERSONA_FIELDS.includes('enabled'), false, 'enabled 是全局开关,不进预设')
|
|
588
|
+
assert.deepEqual(
|
|
589
|
+
[...PROMPT_INPUT_FIELDS].sort(),
|
|
590
|
+
[...new Set(['enabled', ...PROFILE_FIELDS, ...PERSONA_FIELDS])].sort(),
|
|
591
|
+
'编译读取范围应恰好等于「总开关 + 关于你 + 人设」'
|
|
592
|
+
)
|
|
593
|
+
})
|
|
594
|
+
|
|
595
|
+
check('用户自建预设里残留的「关于你」一律不算数(匹配与取值都忽略)', () => {
|
|
596
|
+
// 历史数据或手改文件都可能残留这些键:既不该影响 ★ 匹配,也不该被应用。
|
|
597
|
+
const legacy = { nickname: '老昵称', occupation: '老职业', bio: '老简介', style: 'roast', updatedAt: 'x' }
|
|
598
|
+
assert.deepEqual(declaredPersonaKeys(legacy), ['style'], '「关于你」不应算作已声明字段')
|
|
599
|
+
assert.equal(
|
|
600
|
+
personaMatches(legacy, { ...DEFAULT_CONFIG, style: 'roast', nickname: '换了个人' }),
|
|
601
|
+
true,
|
|
602
|
+
'残留的「关于你」不应影响匹配'
|
|
603
|
+
)
|
|
604
|
+
})
|
|
605
|
+
|
|
606
|
+
check('normalizePersonas 剥离历史预设里的「关于你」,保留人设字段与元数据', () => {
|
|
607
|
+
const out = normalizePersonas({
|
|
608
|
+
历史预设: {
|
|
609
|
+
nickname: '老昵称',
|
|
610
|
+
occupation: '老职业',
|
|
611
|
+
bio: '老简介',
|
|
612
|
+
style: 'roast',
|
|
613
|
+
headingLists: 'less',
|
|
614
|
+
updatedAt: 'T'
|
|
615
|
+
},
|
|
616
|
+
脏条目: 'not-an-object'
|
|
617
|
+
})
|
|
618
|
+
assert.deepEqual(Object.keys(out), ['历史预设'], '非对象条目应被剔除')
|
|
619
|
+
assert.deepEqual(Object.keys(out['历史预设']).sort(), ['headingLists', 'style', 'updatedAt'])
|
|
620
|
+
assert.equal('nickname' in out['历史预设'], false, '历史遗留的昵称应被剥离')
|
|
621
|
+
// 走真实读取路径(migrateConfig → normalizePersonas)同样生效
|
|
622
|
+
const migrated = migrateConfig({ personas: { 历史预设: { nickname: '老昵称', style: 'roast' } } })
|
|
623
|
+
assert.deepEqual(migrated.personas['历史预设'], { style: 'roast' })
|
|
624
|
+
})
|
|
625
|
+
|
|
626
|
+
check('预设的保存与应用都只遍历 PERSONA_FIELDS 这一份白名单', () => {
|
|
627
|
+
// 宿主侧唯一的两处取键:数据源必须是常量而不是各自硬编码一份字段表,
|
|
628
|
+
// 否则「预设范围」的增删会出现两套互相漂移的语义。
|
|
629
|
+
const snapshot = indexCode.match(/function personaSnapshotOf\(config\) \{([\s\S]*?)\n\}/)
|
|
630
|
+
assert.ok(snapshot, '缺少 personaSnapshotOf')
|
|
631
|
+
assert.ok(/for \(const key of PERSONA_FIELDS\)/.test(snapshot[1]), '快照必须按 PERSONA_FIELDS 取键')
|
|
632
|
+
const pick = indexCode.match(/function pickPersonaValues\(persona\) \{([\s\S]*?)\n\}/)
|
|
633
|
+
assert.ok(pick, '缺少 pickPersonaValues')
|
|
634
|
+
assert.ok(/for \(const key of PERSONA_FIELDS\)/.test(pick[1]), '应用必须按 PERSONA_FIELDS 取键')
|
|
635
|
+
// 反向:两处都不得出现硬编码的身份字段(否则会绕过白名单)
|
|
636
|
+
for (const key of PROFILE_FIELDS) {
|
|
637
|
+
assert.equal(snapshot[1].includes(key), false, `快照不应涉及 ${key}`)
|
|
638
|
+
assert.equal(pick[1].includes(key), false, `应用不应涉及 ${key}`)
|
|
639
|
+
}
|
|
640
|
+
})
|
|
641
|
+
|
|
642
|
+
check('内置人设的枚举值与文本长度合法,且都有自定义指令', () => {
|
|
643
|
+
const enums = {
|
|
644
|
+
style: STYLE_VALUES,
|
|
645
|
+
headingLists: TRAIT_VALUES,
|
|
646
|
+
emoji: TRAIT_VALUES,
|
|
647
|
+
tables: TRAIT_VALUES,
|
|
648
|
+
replyLength: REPLY_LENGTH_VALUES
|
|
649
|
+
}
|
|
650
|
+
for (const name of builtinPersonaNames()) {
|
|
651
|
+
const entry = BUILTIN_PERSONAS[name]
|
|
652
|
+
for (const [field, allowed] of Object.entries(enums)) {
|
|
653
|
+
if (!(field in entry)) continue
|
|
654
|
+
assert.ok(allowed.includes(entry[field]), `${name}.${field} 非法取值:${entry[field]}`)
|
|
655
|
+
}
|
|
656
|
+
// customInstructions 是人格差异的主要载体:枚举字段只能表达「更啰嗦 / 更简洁」
|
|
657
|
+
// 这类粗粒度倾向,所以内置预设必须有它。
|
|
658
|
+
assert.equal(typeof entry.customInstructions, 'string', `${name} 缺少 customInstructions`)
|
|
659
|
+
assert.ok(entry.customInstructions.length > 0, `${name} 的 customInstructions 为空`)
|
|
660
|
+
assert.ok(
|
|
661
|
+
entry.customInstructions.length <= FIELD_LIMITS.customInstructions,
|
|
662
|
+
`${name} 的 customInstructions 超长(${entry.customInstructions.length} > ${FIELD_LIMITS.customInstructions})`
|
|
663
|
+
)
|
|
664
|
+
}
|
|
665
|
+
})
|
|
666
|
+
|
|
667
|
+
check('内置人设都能通过 sanitizeConfig(数据本身合法)', () => {
|
|
668
|
+
for (const name of builtinPersonaNames()) {
|
|
669
|
+
const { errors } = sanitizeConfig(resolvePersona(name, {}))
|
|
670
|
+
assert.deepEqual(errors, {}, `${name} 未通过校验:${JSON.stringify(errors)}`)
|
|
671
|
+
}
|
|
672
|
+
})
|
|
673
|
+
|
|
674
|
+
check('mergePersonas:内置优先并带 builtin 标记,用户条目不受影响', () => {
|
|
675
|
+
const names = builtinPersonaNames()
|
|
676
|
+
const merged = mergePersonas({ '极简主义者': { style: 'roast' }, 我的预设: { style: 'casual' } })
|
|
677
|
+
assert.equal(Object.keys(merged).length, names.length + 1, '同名用户条目应被内置遮蔽而非并存')
|
|
678
|
+
assert.equal(merged['极简主义者'].style, BUILTIN_PERSONAS['极简主义者'].style, '同名时应取内置内容')
|
|
679
|
+
assert.equal(merged['极简主义者'].builtin, true)
|
|
680
|
+
assert.equal(merged['我的预设'].builtin, undefined, '用户预设不应带 builtin 标记')
|
|
681
|
+
assert.deepEqual(merged['我的预设'], { style: 'casual' })
|
|
682
|
+
for (const name of names) assert.equal(merged[name].builtin, true, `${name} 应带 builtin 标记`)
|
|
683
|
+
// 非对象输入不应抛错
|
|
684
|
+
assert.equal(Object.keys(mergePersonas(null)).length, names.length)
|
|
685
|
+
assert.equal(Object.keys(mergePersonas('x')).length, names.length)
|
|
686
|
+
})
|
|
687
|
+
|
|
688
|
+
check('isBuiltinPersona / resolvePersona 的判定与优先级', () => {
|
|
689
|
+
const names = builtinPersonaNames()
|
|
690
|
+
assert.equal(isBuiltinPersona(names[0]), true)
|
|
691
|
+
assert.equal(isBuiltinPersona('不存在的名字'), false)
|
|
692
|
+
assert.equal(isBuiltinPersona(123), false)
|
|
693
|
+
assert.equal(isBuiltinPersona(null), false)
|
|
694
|
+
assert.deepEqual(resolvePersona(names[0], {}), BUILTIN_PERSONAS[names[0]])
|
|
695
|
+
// 内置名即使磁盘上有同名条目也应解析到内置内容
|
|
696
|
+
assert.deepEqual(resolvePersona(names[0], { [names[0]]: { style: 'roast' } }), BUILTIN_PERSONAS[names[0]])
|
|
697
|
+
assert.deepEqual(resolvePersona('我的预设', { '我的预设': { style: 'casual' } }), { style: 'casual' })
|
|
698
|
+
assert.equal(resolvePersona('不存在', { '我的预设': { style: 'casual' } }), null)
|
|
699
|
+
assert.equal(resolvePersona(undefined, {}), null)
|
|
700
|
+
})
|
|
701
|
+
|
|
702
|
+
check('应用内置预设后能命中它自己(★ 标记闭环)', () => {
|
|
703
|
+
// 这条是「预设能用」的最小闭环:应用 → 表单回读 → 列表应显示 ★。
|
|
704
|
+
// 若匹配语义写成全字段相等,apply 会失败而这条会立刻报出来。
|
|
705
|
+
for (const name of builtinPersonaNames()) {
|
|
706
|
+
const entry = resolvePersona(name, {})
|
|
707
|
+
const { patch } = sanitizeConfig(entry)
|
|
708
|
+
const applied = { ...DEFAULT_CONFIG, ...patch }
|
|
709
|
+
assert.equal(personaMatches(entry, applied), true, `${name} 应用后匹配不上自己`)
|
|
710
|
+
}
|
|
711
|
+
})
|
|
712
|
+
|
|
713
|
+
check('匹配是部分覆盖语义:已声明字段敏感、未声明字段免疫', () => {
|
|
714
|
+
const entry = resolvePersona('极简主义者', {})
|
|
715
|
+
const applied = { ...DEFAULT_CONFIG, ...sanitizeConfig(entry).patch }
|
|
716
|
+
assert.equal(personaMatches(entry, DEFAULT_CONFIG), false, '未应用时不应匹配')
|
|
717
|
+
assert.equal(personaMatches(entry, { ...applied, style: 'casual' }), false, '改动已声明字段后不应匹配')
|
|
718
|
+
assert.equal(
|
|
719
|
+
personaMatches(entry, { ...applied, customInstructions: `${applied.customInstructions}x` }),
|
|
720
|
+
false,
|
|
721
|
+
'自定义指令变化后不应匹配'
|
|
722
|
+
)
|
|
723
|
+
assert.equal(personaMatches(entry, { ...applied, language: 'en' }), true, '预设未声明 language,语言变化不应影响匹配')
|
|
724
|
+
assert.equal(personaMatches(entry, { ...applied, nickname: '小明' }), true, '预设未声明 nickname,改昵称不应影响匹配')
|
|
725
|
+
})
|
|
726
|
+
|
|
727
|
+
check('未声明任何字段的条目不匹配(避免匹配一切)', () => {
|
|
728
|
+
assert.equal(personaMatches({}, DEFAULT_CONFIG), false)
|
|
729
|
+
assert.equal(personaMatches({ updatedAt: 'x', builtin: true }, DEFAULT_CONFIG), false)
|
|
730
|
+
assert.equal(personaMatches(null, DEFAULT_CONFIG), false)
|
|
731
|
+
assert.deepEqual(declaredPersonaKeys({ style: 'casual', updatedAt: 'x', builtin: true, extra: 1 }), ['style'])
|
|
732
|
+
})
|
|
733
|
+
|
|
734
|
+
// ==================== 前后端契约一致性 ====================
|
|
735
|
+
|
|
736
|
+
console.log('\n前后端契约一致性')
|
|
737
|
+
|
|
738
|
+
const clientSource = readFileSync(new URL('../client/index.mjs', import.meta.url), 'utf8')
|
|
739
|
+
const clientCode = stripComments(clientSource)
|
|
740
|
+
|
|
741
|
+
// 真实 @deepseek-ai/dsh-tools(若本机可解析):用于行为层校验 set_persona 的 schema。
|
|
742
|
+
// ESM 顶层 await;解析不到(CI / 未装 DSH)时为 null,行为层以 skipNote 报告。
|
|
743
|
+
//
|
|
744
|
+
// 解析不能用 createRequire(插件 package.json) 单点:dsh-tools 是 **peer 依赖**,不在
|
|
745
|
+
// 插件 node_modules 里,从插件目录解析注定 MODULE_NOT_FOUND(首次实现即踩此坑:
|
|
746
|
+
// skipNote 提示「未安装」却依然显示 ✓,靠 --strict 才暴露)。这里按 verify-compat 的
|
|
747
|
+
// anchors 思路显式发现:插件目录 → cwd → DSH profiles 下各 profile → 逐级向上。
|
|
748
|
+
let defineTool = null
|
|
749
|
+
try {
|
|
750
|
+
const here = fileURLToPath(new URL('..', import.meta.url))
|
|
751
|
+
const anchors = [here, process.cwd()]
|
|
752
|
+
const profiles = join(process.env.DSH_HOME || join(homedir(), '.dsh'), 'profiles')
|
|
753
|
+
if (existsSync(profiles)) {
|
|
754
|
+
for (const p of readdirSync(profiles)) anchors.push(join(profiles, p))
|
|
755
|
+
}
|
|
756
|
+
let resolved = null
|
|
757
|
+
for (const anchor of anchors) {
|
|
758
|
+
try {
|
|
759
|
+
resolved = createRequire(join(anchor, 'noop.js')).resolve('@deepseek-ai/dsh-tools')
|
|
760
|
+
break
|
|
761
|
+
} catch { /* 换下一个 anchor */ }
|
|
762
|
+
}
|
|
763
|
+
if (!resolved) throw new Error('not found')
|
|
764
|
+
;({ defineTool } = await import(pathToFileURL(resolved).href))
|
|
765
|
+
} catch { /* 未装 DSH:行为层跳过(skipNote 在用到它的 check 里报告) */ }
|
|
766
|
+
if (defineTool) console.log(' · 已定位真实 @deepseek-ai/dsh-tools,set_persona 将做行为层校验')
|
|
767
|
+
|
|
768
|
+
check('客户端摘要的默认值表与宿主 DEFAULT_CONFIG 逐项一致', () => {
|
|
769
|
+
const block = clientCode.match(/const META_FIELD_DEFAULTS = \{([\s\S]*?)\}/)
|
|
770
|
+
assert.ok(block, '客户端缺少 META_FIELD_DEFAULTS')
|
|
771
|
+
const pairs = [...block[1].matchAll(/(\w+):\s*'([^']*)'/g)].map((m) => [m[1], m[2]])
|
|
772
|
+
assert.ok(pairs.length >= 5, `默认值表条目过少:${pairs.length}`)
|
|
773
|
+
for (const [field, value] of pairs) {
|
|
774
|
+
assert.equal(
|
|
775
|
+
value,
|
|
776
|
+
String(DEFAULT_CONFIG[field]),
|
|
777
|
+
`客户端 ${field} 默认值 ${value} 与宿主 DEFAULT_CONFIG 的 ${DEFAULT_CONFIG[field]} 不一致(摘要会显示相反的结果)`
|
|
778
|
+
)
|
|
779
|
+
}
|
|
780
|
+
})
|
|
781
|
+
|
|
782
|
+
check('客户端摘要覆盖的字段集合固定', () => {
|
|
783
|
+
const block = clientCode.match(/const META_FIELD_VALUES = \{([\s\S]*?)\}/)
|
|
784
|
+
assert.ok(block, '客户端缺少 META_FIELD_VALUES')
|
|
785
|
+
const fields = [...block[1].matchAll(/(\w+):/g)].map((m) => m[1]).sort()
|
|
786
|
+
assert.deepEqual(fields, ['emoji', 'headingLists', 'language', 'replyLength', 'tables'])
|
|
787
|
+
})
|
|
788
|
+
|
|
789
|
+
check('客户端为内置预设提供中英双语文案键', () => {
|
|
790
|
+
const keys = [
|
|
791
|
+
'personas.builtin',
|
|
792
|
+
'personas.hint',
|
|
793
|
+
'meta.replyLength.concise',
|
|
794
|
+
'meta.replyLength.detailed',
|
|
795
|
+
'meta.headingLists.more',
|
|
796
|
+
'meta.headingLists.less',
|
|
797
|
+
'meta.emoji.more',
|
|
798
|
+
'meta.emoji.less',
|
|
799
|
+
'meta.tables.more',
|
|
800
|
+
'meta.tables.less',
|
|
801
|
+
'meta.language.zh',
|
|
802
|
+
'meta.language.en'
|
|
803
|
+
]
|
|
804
|
+
for (const key of keys) {
|
|
805
|
+
const hits = clientSource.split(`'${key}':`).length - 1
|
|
806
|
+
assert.equal(hits, 2, `${key} 应在 zh / en 各出现一次,实际 ${hits} 次`)
|
|
807
|
+
}
|
|
808
|
+
})
|
|
809
|
+
|
|
810
|
+
check('客户端内置预设行不渲染删除按钮,改为「内置」标记', () => {
|
|
811
|
+
// 徽标渲染在名称列(紧跟名称),不是操作区 —— 放操作区会把「使用」挤出竖列。
|
|
812
|
+
// 定界必须用括号配平(extractCall),不能用缩进/固定窗口:曾用 `\s{22}` 猜终点,
|
|
813
|
+
// 而真实闭合是 24 空格 ⇒ 捕获一路吞掉摘要列与操作区,「徽标在名称列」退化成
|
|
814
|
+
// 「徽标在这一行里」,把徽标挪到摘要列也能通过(变异实验实测,H4)。
|
|
815
|
+
const nameCell = extractCall(clientSource, "e('span', { className: 'soul-persona-name' }")
|
|
816
|
+
assert.ok(nameCell !== '', '找不到名称列渲染块')
|
|
817
|
+
assert.ok(
|
|
818
|
+
/soul-persona-badge/.test(nameCell),
|
|
819
|
+
'内置标记未渲染在名称列(放操作区会让两类行的「使用」不同列)'
|
|
820
|
+
)
|
|
821
|
+
// 名称列不得包含摘要/操作区内容 —— 防止定界再次失效而不自知
|
|
822
|
+
assert.ok(
|
|
823
|
+
!nameCell.includes('soul-persona-meta') && !nameCell.includes('soul-persona-actions'),
|
|
824
|
+
'名称列的捕获范围越界(定界用括号配平,不得吞入 meta/actions)'
|
|
825
|
+
)
|
|
826
|
+
// 徽标不得出现在操作区块里
|
|
827
|
+
const actionsCell = extractCall(clientSource, "e('span', { className: 'soul-persona-actions' }")
|
|
828
|
+
assert.ok(actionsCell !== '', '找不到操作区渲染块')
|
|
829
|
+
assert.equal(
|
|
830
|
+
/soul-persona-badge/.test(actionsCell),
|
|
831
|
+
false,
|
|
832
|
+
'徽标不应出现在操作区(会占掉「使用」的列位)'
|
|
833
|
+
)
|
|
834
|
+
// 徽标也不得出现在摘要列(把徽标挪进 meta 同样破坏对齐前提)
|
|
835
|
+
const metaCell = extractCall(clientSource, "e('span', { className: 'soul-persona-meta' }")
|
|
836
|
+
assert.ok(metaCell !== '', '找不到摘要列渲染块')
|
|
837
|
+
assert.equal(
|
|
838
|
+
/soul-persona-badge/.test(metaCell),
|
|
839
|
+
false,
|
|
840
|
+
'徽标不应出现在摘要列(对齐断言的前提是徽标独占名称列)'
|
|
841
|
+
)
|
|
842
|
+
// 内置行在操作区第 2 轨放空占位(span,非按钮),自建行才是删除按钮:
|
|
843
|
+
// 这正是两类行「使用」按钮能严格同列的原因。
|
|
844
|
+
assert.ok(
|
|
845
|
+
/builtin\s*\?\s*e\('span', \{ className: 'soul-persona-slot'/.test(clientCode),
|
|
846
|
+
'内置行缺少操作区空占位(会导致「使用」与自建行不同列)'
|
|
847
|
+
)
|
|
848
|
+
assert.ok(
|
|
849
|
+
/e\('button', \{ type: 'button', className: 'soul-prompt-link soul-persona-danger'/.test(stripComments(clientSource)),
|
|
850
|
+
'删除按钮未渲染'
|
|
851
|
+
)
|
|
852
|
+
// 空占位必须是 span 而不是 button —— 否则会造出一个点了没反应的控件
|
|
853
|
+
assert.equal(
|
|
854
|
+
/soul-persona-slot'[^)]*\}\s*,\s*\{[^}]*onClick/.test(clientCode),
|
|
855
|
+
false,
|
|
856
|
+
'空占位不应是可点控件'
|
|
857
|
+
)
|
|
858
|
+
})
|
|
859
|
+
|
|
860
|
+
check('客户端人设行用网格布局保证「使用」按钮跨行同列', () => {
|
|
861
|
+
const cssBlock = clientCode.match(/const css = \[([\s\S]*?)\]\.join/)?.[1] ?? ''
|
|
862
|
+
const row = cssBlock.match(/\.soul-persona-row\{([^}]*)\}/)?.[1] ?? ''
|
|
863
|
+
assert.ok(/display:grid/.test(row), '人设行应使用网格布局(flex 下按钮列会随内容错位)')
|
|
864
|
+
const actions = cssBlock.match(/\.soul-persona-actions\{([^}]*)\}/)?.[1] ?? ''
|
|
865
|
+
assert.ok(/display:grid/.test(actions), '操作区应是网格(两条固定轨道)')
|
|
866
|
+
// 轨道必须两端等宽:`auto`/`minmax()` 会按各自内容算宽,内置行第 2 轨为空 ⇒ 两行不同列
|
|
867
|
+
assert.ok(
|
|
868
|
+
/grid-template-columns:1fr 1fr/.test(actions),
|
|
869
|
+
'操作区两条轨道必须等宽固定,否则内置行(第 2 轨为空)与自建行的「使用」不同列'
|
|
870
|
+
)
|
|
871
|
+
assert.ok(/width:\s*\d/.test(actions), '操作区需要固定宽度,跨行几何才会一致')
|
|
872
|
+
})
|
|
873
|
+
|
|
874
|
+
check('客户端的动效尊重 prefers-reduced-motion(含提示条)', () => {
|
|
875
|
+
const cssBlock = clientCode.match(/const css = \[([\s\S]*?)\]\.join/)?.[1] ?? ''
|
|
876
|
+
const reduced = cssBlock.match(/@media \(prefers-reduced-motion:reduce\)\{([\s\S]*?)\}\}/g) ?? []
|
|
877
|
+
assert.ok(reduced.length > 0, '客户端没有任何 prefers-reduced-motion 规则')
|
|
878
|
+
const all = reduced.join('')
|
|
879
|
+
// 三处会动的元素都必须在 reduced-motion 下停:光轨层、快捷开关圆点、提示条。
|
|
880
|
+
// 提示条此前被漏掉 —— 它用的是**内联** animation,只查样式表容易漏看。
|
|
881
|
+
for (const [needle, why] of [
|
|
882
|
+
['.soul-trail-layer', '光轨流动'],
|
|
883
|
+
['.soul-quick-toggle', '快捷开关圆点的脉冲'],
|
|
884
|
+
['.soul-toast', '保存/失败提示条的淡出']
|
|
885
|
+
]) {
|
|
886
|
+
assert.ok(all.includes(needle), `prefers-reduced-motion 未覆盖 ${needle}(${why})`)
|
|
887
|
+
}
|
|
888
|
+
// 提示条那条要能压过内联声明(实测:普通声明也有效,但 !important 更稳)
|
|
889
|
+
const toastRule = (all.match(/\.soul-toast\{[^}]*\}/) ?? [''])[0]
|
|
890
|
+
assert.ok(/animation:none/.test(toastRule), '提示条在 reduced-motion 下必须 animation:none')
|
|
891
|
+
})
|
|
892
|
+
|
|
893
|
+
check('客户端预设行摘要不读「关于你」', () => {
|
|
894
|
+
// 行摘要显示「这个预设会改变什么」。身份字段已不属于预设,摘要再显示昵称就会
|
|
895
|
+
// 与「应用它不会动昵称」自相矛盾。
|
|
896
|
+
const block = clientCode.match(/const personaRowMeta = \(entry\) => \{([\s\S]*?)return parts\.join/)
|
|
897
|
+
assert.ok(block, '缺少 personaRowMeta')
|
|
898
|
+
for (const key of PROFILE_FIELDS) {
|
|
899
|
+
assert.equal(block[1].includes(`row.${key}`), false, `预设行摘要不应读取 ${key}`)
|
|
900
|
+
}
|
|
901
|
+
// 反向防空过:摘要仍要读人设维度,否则「不读身份字段」会因为整个函数被掏空而假通过
|
|
902
|
+
assert.ok(/row\.style/.test(block[1]), '预设行摘要仍应读 style')
|
|
903
|
+
})
|
|
904
|
+
|
|
905
|
+
check('服务端四条写路径都拒绝内置名,读取路径都走合并库', () => {
|
|
906
|
+
// HTTP 保存 / 删除各一处,/soul 命令的 save / del 各一处
|
|
907
|
+
const rejections = indexCode.split('isBuiltinPersona(personaName)').length - 1
|
|
908
|
+
assert.equal(rejections, 4, `内置名拒绝点应为 4 处(HTTP 与命令层各 2),实际 ${rejections} 处`)
|
|
909
|
+
assert.ok(indexCode.split('mergePersonas(').length - 1 >= 3, '预设库读取应统一走 mergePersonas')
|
|
910
|
+
assert.ok(/resolvePersona\(personaName, config\.personas\)/.test(indexCode), '命令层 use 未走 resolvePersona')
|
|
911
|
+
assert.ok(/resolvePersona\(personaName, current\.personas\)/.test(indexCode), 'HTTP use 未走 resolvePersona')
|
|
912
|
+
})
|
|
913
|
+
|
|
914
|
+
check('匹配逻辑只在纯模块实现一份(不留本地重复实现)', () => {
|
|
915
|
+
assert.equal(
|
|
916
|
+
indexCode.includes('function declaredPersonaKeys'),
|
|
917
|
+
false,
|
|
918
|
+
'index.mjs 不应自带 declaredPersonaKeys——已下沉到 lib/personas.mjs,两份实现会漂移'
|
|
919
|
+
)
|
|
920
|
+
assert.ok(/personaMatches\(/.test(indexCode), 'index.mjs 应调用 personaMatches 做 ★ 判定')
|
|
921
|
+
})
|
|
922
|
+
|
|
923
|
+
// ---------- 提示词预览(草稿编译)----------
|
|
924
|
+
|
|
925
|
+
// 抽出参与提示词编译的全部函数源码:compilePrompt 及其两个子构建器。
|
|
926
|
+
// 只扫部分函数会漏掉字段,故三者一起扫。
|
|
927
|
+
function promptCompilerSource() {
|
|
928
|
+
const chunks = []
|
|
929
|
+
for (const name of ['buildUserProfile', 'buildBehavior', 'compilePrompt']) {
|
|
930
|
+
const matched = indexCode.match(new RegExp(`function ${name}\\([\\s\\S]*?\\n\\}`))
|
|
931
|
+
assert.ok(matched, `index.mjs 中找不到函数 ${name}`)
|
|
932
|
+
chunks.push(matched[0])
|
|
933
|
+
}
|
|
934
|
+
return chunks.join('\n')
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
check('草稿预览端点复用保存路径的校验与编译,且不落盘、不注入', () => {
|
|
938
|
+
const matched = indexCode.match(/path: '\/api\/soul\/prompt\/preview'[\s\S]*?\n \}\)/)
|
|
939
|
+
assert.ok(matched, '缺少 /api/soul/prompt/preview 端点')
|
|
940
|
+
const body = matched[0]
|
|
941
|
+
assert.ok(/sanitizeConfig\(parsed\.body\)/.test(body), '草稿预览未复用 sanitizeConfig(未校验的草稿会编出误导性提示词)')
|
|
942
|
+
assert.ok(/compilePrompt\(draft\)/.test(body), '草稿预览未调用 compilePrompt')
|
|
943
|
+
assert.ok(/req\.method !== 'POST'/.test(body), '草稿预览应限制为 POST')
|
|
944
|
+
assert.equal(/saveConfig\(/.test(body), false, '草稿预览不得落盘(不应调用 saveConfig)')
|
|
945
|
+
assert.equal(/refreshPromptAndInject\(/.test(body), false, '草稿预览不得注入会话(不应调用 refreshPromptAndInject)')
|
|
946
|
+
})
|
|
947
|
+
|
|
948
|
+
check('客户端「参与编译的字段」与宿主 compilePrompt 实际读取的字段一致', () => {
|
|
949
|
+
const block = clientCode.match(/const PROMPT_FIELD_KEYS = \[([\s\S]*?)\]/)
|
|
950
|
+
assert.ok(block, '客户端缺少 PROMPT_FIELD_KEYS')
|
|
951
|
+
const clientKeys = [...block[1].matchAll(/'([^']+)'/g)].map((m) => m[1]).sort()
|
|
952
|
+
|
|
953
|
+
// 「参与编译的字段」由配置层单点定义(总开关 + 关于你 + 人设)。这里刻意**不**用
|
|
954
|
+
// PERSONA_FIELDS —— 预设能覆盖的字段与编译读取的字段是两回事,混用会在改预设范围
|
|
955
|
+
// 时把这条契约连带改错。
|
|
956
|
+
const driven = new Set(PROMPT_INPUT_FIELDS)
|
|
957
|
+
const hostKeys = [...new Set([...promptCompilerSource().matchAll(/config\.(\w+)/g)].map((m) => m[1]))]
|
|
958
|
+
.filter((key) => driven.has(key))
|
|
959
|
+
.sort()
|
|
960
|
+
|
|
961
|
+
assert.deepEqual(
|
|
962
|
+
clientKeys,
|
|
963
|
+
hostKeys,
|
|
964
|
+
'PROMPT_FIELD_KEYS 与实际参与编译的字段不一致:漏字段会让预览显示陈旧内容,多字段会触发无谓重编译'
|
|
965
|
+
)
|
|
966
|
+
assert.ok(clientKeys.length >= 5, `参与编译的字段过少:${clientKeys.length}`)
|
|
967
|
+
})
|
|
968
|
+
|
|
969
|
+
check('客户端复刻的枚举常量与宿主 lib/config.mjs 逐项一致', () => {
|
|
970
|
+
// 浏览器模块表不允许 client/index.mjs import lib/config.mjs,所以客户端必须**复刻**
|
|
971
|
+
// 一份常量;这些常量本身写着「与 lib/config.mjs 的合法值保持一致」,但那只是注释。
|
|
972
|
+
// 漂移后果各不相同且都不报错:
|
|
973
|
+
// - 少了取值:下拉框少一档,用户在界面上根本选不到该能力
|
|
974
|
+
// - 多了取值:界面能选、宿主 sanitizeConfig 拒绝 ⇒ 保存报 400
|
|
975
|
+
// - 换了取值:与宿主枚举错位,脏检查与保存载荷对不上
|
|
976
|
+
// 因此这里从两端各取一次真相再比对:宿主用 import 到的真实值,客户端按源文本解析。
|
|
977
|
+
const specs = [
|
|
978
|
+
['STYLE_VALUES', STYLE_VALUES],
|
|
979
|
+
['TRAIT_VALUES', TRAIT_VALUES],
|
|
980
|
+
['REPLY_LENGTH_VALUES', REPLY_LENGTH_VALUES],
|
|
981
|
+
['LANGUAGE_VALUES', LANGUAGE_VALUES]
|
|
982
|
+
]
|
|
983
|
+
for (const [name, hostValues] of specs) {
|
|
984
|
+
const m = clientCode.match(new RegExp(`const ${name} = \\[([^\\]]*)\\]`))
|
|
985
|
+
assert.ok(m, `客户端缺少 ${name}`)
|
|
986
|
+
const clientValues = [...m[1].matchAll(/'([^']*)'/g)].map((x) => x[1])
|
|
987
|
+
assert.ok(clientValues.length > 0, `客户端 ${name} 解析为空`)
|
|
988
|
+
assert.deepEqual(
|
|
989
|
+
clientValues,
|
|
990
|
+
hostValues,
|
|
991
|
+
`客户端 ${name} 与宿主不一致(客户端 ${JSON.stringify(clientValues)} / 宿主 ${JSON.stringify(hostValues)})`
|
|
992
|
+
)
|
|
993
|
+
}
|
|
994
|
+
})
|
|
995
|
+
|
|
996
|
+
check('客户端 FIELD_KEYS 覆盖宿主全部配置字段(dirty 门控不漏字段)', () => {
|
|
997
|
+
// FIELD_KEYS 驱动 dirty 判定与保存载荷。漏一个字段的后果是静默的:
|
|
998
|
+
// 改那个字段后「保存」按钮可能仍被禁用(看起来像界面卡住),或保存结果被判成「无变化」。
|
|
999
|
+
const m = clientCode.match(/const FIELD_KEYS = \[([^\]]*)\]/)
|
|
1000
|
+
assert.ok(m, '客户端缺少 FIELD_KEYS')
|
|
1001
|
+
const clientKeys = [...m[1].matchAll(/'([^']*)'/g)].map((x) => x[1])
|
|
1002
|
+
const hostFields = Object.keys(DEFAULT_CONFIG).sort()
|
|
1003
|
+
assert.deepEqual(
|
|
1004
|
+
[...clientKeys].sort(),
|
|
1005
|
+
hostFields,
|
|
1006
|
+
'FIELD_KEYS 必须与宿主 DEFAULT_CONFIG 的字段集合完全相同(漏字段会让 dirty 检测失效)'
|
|
1007
|
+
)
|
|
1008
|
+
// 顺序无关紧要但重复有害:同一字段出现两次会让 dirty 判定重复计算。
|
|
1009
|
+
assert.equal(new Set(clientKeys).size, clientKeys.length, 'FIELD_KEYS 含重复字段')
|
|
1010
|
+
})
|
|
1011
|
+
|
|
1012
|
+
check('预设应用只有一条实现路径,且非法字段必须回报(HTTP 与 /soul use 共用)', () => {
|
|
1013
|
+
// 两条应用路径(POST /api/soul/personas/use 与 /soul use)此前各自内联一份
|
|
1014
|
+
// `sanitizeConfig(pickPersonaValues(entry))`,且都只取 patch、把 errors 丢掉 ⇒
|
|
1015
|
+
// 「部分应用」被报成成功(实测:style / emoji 被静默丢弃,仍返回 ok:true)。
|
|
1016
|
+
// 现已收口到 applyPersonaEntry。这里钉三件事:
|
|
1017
|
+
// ① 该校验全仓只允许出现在收口函数里(再写一份必漏 errors);
|
|
1018
|
+
// ② 两条路径都真的走收口函数;
|
|
1019
|
+
// ③ 收口函数把 errors 换算成 invalid / applied 回报,且调用方真的消费。
|
|
1020
|
+
// 一律匹配**去注释后**的源码:注释里写着「applied 是…」就能让裸 includes 恒真
|
|
1021
|
+
// (本条初版正是这么写的,被自己的变异实验抓到 —— 与 M5 同一漏洞类别)。
|
|
1022
|
+
const bare = stripComments(indexSource)
|
|
1023
|
+
const inlined = [...bare.matchAll(/sanitizeConfig\(pickPersonaValues\(entry\)\)/g)].length
|
|
1024
|
+
assert.equal(inlined, 1, `预设取值校验只应有一处实现(收口在 applyPersonaEntry),实际 ${inlined} 处`)
|
|
1025
|
+
const uses = [...bare.matchAll(/await applyPersonaEntry\(ctx, entry\)/g)].length
|
|
1026
|
+
assert.equal(uses, 2, `HTTP 与 /soul use 都应走 applyPersonaEntry,实际 ${uses} 处`)
|
|
1027
|
+
const body = stripComments(extractObject(indexSource, 'async function applyPersonaEntry'))
|
|
1028
|
+
assert.ok(body !== '', '找不到 applyPersonaEntry')
|
|
1029
|
+
for (const token of ['invalid', 'applied', 'Object.keys(errors)']) {
|
|
1030
|
+
assert.ok(body.includes(token), `applyPersonaEntry 未回报 ${token}:非法字段会被静默丢弃`)
|
|
1031
|
+
}
|
|
1032
|
+
assert.ok(/invalid\.length > 0/.test(bare), '命令路径未按 invalid 分支提示(usePartial / useAllInvalid)')
|
|
1033
|
+
assert.ok(/invalid \}/.test(bare), 'HTTP 响应未带出 invalid')
|
|
1034
|
+
// 双语文案表必须提供这两个键,否则部分应用时界面上没有对应文案
|
|
1035
|
+
for (const lang of ['zh', 'en']) {
|
|
1036
|
+
const keys = tableLangKeys(indexSource, 'COMMAND_MESSAGES', lang)
|
|
1037
|
+
for (const key of ['usePartial', 'useAllInvalid']) {
|
|
1038
|
+
assert.ok(keys.has(key), `COMMAND_MESSAGES.${lang} 缺少 ${key}`)
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
})
|
|
1042
|
+
|
|
1043
|
+
check('命令层的三处行为契约(覆盖列 / 键名大小写 / 昵称兜底)', () => {
|
|
1044
|
+
// ① `/soul list` 不得再显示预设的「关于你」:v0.7.1 起预设不含 nickname,
|
|
1045
|
+
// normalizePersonas 也会剥离它 ⇒ 显示 `昵称=${entry.nickname || '-'}` 恒为
|
|
1046
|
+
// `昵称=-`,是一列永远没有信息的死数据。改为报告覆盖了几个维度。
|
|
1047
|
+
const listBody = extractObject(indexCode, "if (first === 'list')")
|
|
1048
|
+
assert.ok(listBody !== '', '找不到 /soul list 分支')
|
|
1049
|
+
assert.equal(/entry\.nickname/.test(listBody), false, '/soul list 不得再读 entry.nickname(预设已不含该字段,会恒显示「-」)')
|
|
1050
|
+
assert.ok(/listCovers/.test(listBody), '/soul list 应报告覆盖的维度数(listCovers)')
|
|
1051
|
+
// ② 键名忽略大小写,且必须**映射回真实字段名**:直接 toLowerCase 会把 headingLists
|
|
1052
|
+
// 压成 headinglists 反而匹配不上白名单,所以查表还原(STYLE → style)。
|
|
1053
|
+
assert.ok(/COMMAND_FIELD_BY_LOWER/.test(indexCode), '缺少字段名小写索引表')
|
|
1054
|
+
assert.ok(/normalizeSetKey\(rawKey\)/.test(indexCode), '/soul set 未对键名做大小写归一化')
|
|
1055
|
+
assert.ok(
|
|
1056
|
+
/new Map\(Object\.keys\(DEFAULT_CONFIG\)\.map/.test(indexCode),
|
|
1057
|
+
'字段名索引表应由 DEFAULT_CONFIG 生成(手写列表会漂移)'
|
|
1058
|
+
)
|
|
1059
|
+
// ③ 昵称兜底是**刻意的设计**:非关键字输入整体视为昵称(/soul help 会写昵称)。
|
|
1060
|
+
// 这条断言把设计钉住,避免有人顺手加 help/status 关键字而无声改变语义。
|
|
1061
|
+
assert.ok(
|
|
1062
|
+
/!COMMAND_KEYWORDS\.has\(first\)[\s\S]{0,400}?sanitizeConfig\(\{ nickname: raw \}\)/.test(indexCode),
|
|
1063
|
+
'非关键字输入应整体视为昵称(该行为是设计,改动需同步文档与断言)'
|
|
1064
|
+
)
|
|
1065
|
+
for (const key of ['help', 'status', 'version']) {
|
|
1066
|
+
assert.equal(
|
|
1067
|
+
new RegExp(`COMMAND_KEYWORDS = new Set\\(\\[[^\\]]*'${key}'`).test(indexCode),
|
|
1068
|
+
false,
|
|
1069
|
+
`COMMAND_KEYWORDS 不应包含 ${key}:那会改变「非关键字即昵称」的既有语义`
|
|
1070
|
+
)
|
|
1071
|
+
}
|
|
1072
|
+
})
|
|
1073
|
+
|
|
1074
|
+
check('枚举值 trim 与文本字段同规则', () => {
|
|
1075
|
+
// 文本字段本来就 trim(见上面的 TEXT_FIELDS 断言),枚举不 trim 会让
|
|
1076
|
+
// `/soul set style= humorous`(= 后带空格)被拒,而 `bio= 张三` 却能成功 ——
|
|
1077
|
+
// 同一命令里两套规则。这里直接对真实 sanitizeConfig 断言行为。
|
|
1078
|
+
assert.equal(sanitizeConfig({ style: ' humorous' }).patch.style, 'humorous')
|
|
1079
|
+
assert.equal(sanitizeConfig({ replyLength: ' concise ' }).patch.replyLength, 'concise')
|
|
1080
|
+
assert.equal(sanitizeConfig({ trailSpeed: ' fast' }).patch.trailSpeed, 'fast')
|
|
1081
|
+
// 防空过:trim 不能把非法值放进来
|
|
1082
|
+
assert.ok(sanitizeConfig({ style: ' humorous ' }).errors.style === undefined)
|
|
1083
|
+
assert.ok(sanitizeConfig({ style: 'bogus' }).errors.style !== undefined, '非法枚举仍须被拒')
|
|
1084
|
+
assert.ok(sanitizeConfig({ style: '' }).errors.style !== undefined, '空串仍须被拒')
|
|
1085
|
+
})
|
|
1086
|
+
|
|
1087
|
+
check('客户端标题徽标的版本号与 package.json 一致', () => {
|
|
1088
|
+
// 宿主端已由下一条断言钉住「唯一来源 package.json」;客户端因为拿不到该模块系统,
|
|
1089
|
+
// 只能复刻一个字面量显示在标题右上角。这处漂移此前**完全无人看管**(实测:把它改成
|
|
1090
|
+
// 9.9.9 后整套 verify 依然全绿),而症状是界面上写着错误的版本号。
|
|
1091
|
+
const manifest = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'))
|
|
1092
|
+
const m = clientCode.match(/const VERSION = '([^']+)'/)
|
|
1093
|
+
assert.ok(m, '客户端缺少 VERSION 常量')
|
|
1094
|
+
assert.equal(
|
|
1095
|
+
m[1],
|
|
1096
|
+
manifest.version,
|
|
1097
|
+
`客户端标题徽标显示 v${m[1]},而 package.json 是 ${manifest.version}(升版本时漏改这一处)`
|
|
1098
|
+
)
|
|
1099
|
+
})
|
|
1100
|
+
|
|
1101
|
+
check('客户端版本号是唯一需要手工同步的常量(不再新增同类副本)', () => {
|
|
1102
|
+
// VERSION 是历史遗留的复刻点(上面一条断言已保证它与 package.json 一致)。
|
|
1103
|
+
// 除此之外不应再出现别的版本常量,否则每加一处就多一处漏改风险。
|
|
1104
|
+
const hits = [...clientSource.matchAll(/const\s+(?:PLUGIN_)?VERSION\s*=/g)].length
|
|
1105
|
+
assert.equal(hits, 1, `客户端版本常量应恰好 1 处(VERSION),实际 ${hits} 处`)
|
|
1106
|
+
})
|
|
1107
|
+
|
|
1108
|
+
check('客户端在有未保存编辑时预览草稿,否则预览已生效提示词', () => {
|
|
1109
|
+
assert.ok(/async previewPrompt\(draft\)/.test(clientCode), '控制器缺少 previewPrompt')
|
|
1110
|
+
assert.ok(/await controller\.previewPrompt\(promptDraft\(\)\)/.test(clientCode), '未调用草稿预览端点')
|
|
1111
|
+
assert.ok(/const promptDirty = PROMPT_FIELD_KEYS\.some\(/.test(clientCode), 'promptDirty 应由 PROMPT_FIELD_KEYS 判定(改光轨不该让预览变成草稿)')
|
|
1112
|
+
// 只断言下面的三元语法不够:把 asDraft 写死成 false 也能让三元看起来「正确」,
|
|
1113
|
+
// 但预览就永远只看已生效内容了。必须把「草稿判定来源于 promptDirty」这一环钉住。
|
|
1114
|
+
assert.ok(/const asDraft = promptDirty\b/.test(clientCode), 'asDraft 必须取自 promptDirty,否则预览会永远停留在已生效内容')
|
|
1115
|
+
assert.ok(/asDraft\s*\?\s*await controller\.previewPrompt/.test(clientCode), '草稿分支未按 asDraft 选择端点')
|
|
1116
|
+
})
|
|
1117
|
+
|
|
1118
|
+
check('提示词预览的编译入口唯一(避免重复请求与过期闭包)', () => {
|
|
1119
|
+
assert.equal(
|
|
1120
|
+
clientCode.includes('if (showPrompt) await loadPrompt()'),
|
|
1121
|
+
false,
|
|
1122
|
+
'保存/重置/应用预设处理器内不应再直接编译预览——刷新统一由 effect 驱动,两处调用会重复请求并可能用到过期闭包'
|
|
1123
|
+
)
|
|
1124
|
+
const effect = clientCode.match(
|
|
1125
|
+
/const promptSettled = React\.useRef\(false\)[\s\S]*?\}, \[showPrompt, promptDraftKey, promptDirty\]\)/
|
|
1126
|
+
)
|
|
1127
|
+
assert.ok(effect, '缺少预览驱动 effect')
|
|
1128
|
+
assert.equal(
|
|
1129
|
+
effect[0].split('void loadPrompt()').length - 1,
|
|
1130
|
+
2,
|
|
1131
|
+
'effect 应有两个编译触发点:刚展开时立即一次、此后防抖一次'
|
|
1132
|
+
)
|
|
1133
|
+
})
|
|
1134
|
+
|
|
1135
|
+
check('提示词预览新增文案键中英各一份', () => {
|
|
1136
|
+
for (const key of ['prompt.titleDraft', 'prompt.summaryDraft', 'prompt.invalid']) {
|
|
1137
|
+
const hits = clientSource.split(`'${key}':`).length - 1
|
|
1138
|
+
assert.equal(hits, 2, `${key} 应在 zh / en 各出现一次,实际 ${hits} 次`)
|
|
1139
|
+
}
|
|
1140
|
+
})
|
|
1141
|
+
|
|
1142
|
+
// ---------- 系统提示词插值 ----------
|
|
1143
|
+
|
|
1144
|
+
check('系统提示词 section 显式关闭宿主插值', () => {
|
|
1145
|
+
// 用括号配平取 section(...) 的**整个选项对象**,而不是「anchor 起 1500 字符」的
|
|
1146
|
+
// 固定窗口:窗口式取法在注册点与选项之间多出约 1100 字符(注释或代码)后就会
|
|
1147
|
+
// 把 interpolate 推出窗口而假失败,且附近再出现一个 section 注册时可能锚错对象。
|
|
1148
|
+
const opts = stripComments(extractObject(indexSource, 'spCtx.systemPrompt.section('))
|
|
1149
|
+
assert.ok(opts !== '', '找不到 spCtx.systemPrompt.section(...) 调用')
|
|
1150
|
+
// 先确认锚对了对象(否则下面的断言可能对着别的 section 通过)
|
|
1151
|
+
assert.ok(/name:\s*'soul:persona'/.test(opts), 'section 选项对象里没有 soul:persona(锚点异常)')
|
|
1152
|
+
assert.ok(
|
|
1153
|
+
/interpolate:\s*false/.test(opts),
|
|
1154
|
+
'section 必须显式写 interpolate: false —— 宿主默认开启且是严格模式,' +
|
|
1155
|
+
'用户只要在「自定义指令」里写出一对完整的 {{...}} 就会让 renderPrompt 抛错;' +
|
|
1156
|
+
'该错位于 agent.step() 开头且无 try/catch ⇒ 该会话每一轮都失败'
|
|
1157
|
+
)
|
|
1158
|
+
assert.equal(/interpolate:\s*true/.test(opts), false, 'interpolate 不得为 true')
|
|
1159
|
+
})
|
|
1160
|
+
|
|
1161
|
+
// ---------- 配置持久化(原子写 / 损坏不覆盖)----------
|
|
1162
|
+
|
|
1163
|
+
const storeSource = readFileSync(new URL('../lib/store.mjs', import.meta.url), 'utf8')
|
|
1164
|
+
const storeCode = stripComments(storeSource)
|
|
1165
|
+
|
|
1166
|
+
check('配置读取区分「文件不存在」与「文件损坏」,失败时不缓存', () => {
|
|
1167
|
+
const matched = indexCode.match(/async function loadConfig\(\)[\s\S]*?\n\}/)
|
|
1168
|
+
assert.ok(matched, 'index.mjs 中找不到 loadConfig')
|
|
1169
|
+
const body = matched[0]
|
|
1170
|
+
assert.ok(/readConfigFile\(/.test(body), 'loadConfig 应经由 lib/store.mjs 读取(失败类型的判定在那里)')
|
|
1171
|
+
assert.ok(/configLoadFailure = failure/.test(body), 'loadConfig 必须记录失败,供写入路径拒绝保存')
|
|
1172
|
+
assert.ok(/return \{ \.\.\.DEFAULT_CONFIG \}/.test(body), '失败时回退默认值(保证插件与设置页仍可用)')
|
|
1173
|
+
|
|
1174
|
+
const failBlock = body.match(/if \(failure\) \{[\s\S]*?\n \}/)
|
|
1175
|
+
assert.ok(failBlock, '找不到 loadConfig 的失败分支')
|
|
1176
|
+
assert.equal(
|
|
1177
|
+
/configCache\s*=/.test(failBlock[0]),
|
|
1178
|
+
false,
|
|
1179
|
+
'失败分支不得写入 configCache:缓存了默认值,用户修好文件后不重启就恢复不了'
|
|
1180
|
+
)
|
|
1181
|
+
})
|
|
1182
|
+
|
|
1183
|
+
check('「是否允许写入」的判定只在 lib/store.mjs 实现一份', () => {
|
|
1184
|
+
assert.equal(
|
|
1185
|
+
/function configWriteRefusal|function describeConfigFailure/.test(indexCode),
|
|
1186
|
+
false,
|
|
1187
|
+
'index.mjs 不应自带失败说明 / 拒绝判定的实现——写入路径有四条,本地再写一份必然漂移'
|
|
1188
|
+
)
|
|
1189
|
+
assert.ok(
|
|
1190
|
+
/configWriteRefusal\(configLoadFailure, configPath\(\)\)/.test(indexCode),
|
|
1191
|
+
'saveConfig 应调用 configWriteRefusal 决定是否拒绝写入'
|
|
1192
|
+
)
|
|
1193
|
+
})
|
|
1194
|
+
|
|
1195
|
+
check('配置写入只有一条路径:lib/store.mjs 的「临时文件 + rename」', () => {
|
|
1196
|
+
assert.equal(
|
|
1197
|
+
/from 'node:fs\/promises'/.test(indexCode),
|
|
1198
|
+
false,
|
|
1199
|
+
'index.mjs 不应直接操作文件系统——原子写必须收口在 lib/store.mjs,否则会被绕过'
|
|
1200
|
+
)
|
|
1201
|
+
assert.ok(/await writeConfigFile\(configPath\(\), clean\)/.test(stripComments(indexSource)), 'saveConfig 应经由 writeConfigFile 写入(匹配去注释后的源码:注释里留字样不算)')
|
|
1202
|
+
assert.ok(
|
|
1203
|
+
/writeFile\(tmpPath[\s\S]*?rename\(tmpPath, filePath\)/.test(storeCode),
|
|
1204
|
+
'lib/store.mjs 的写入必须是「先写 .tmp、再 rename 覆盖」;直接 writeFile 会在中断时留下截断的 JSON'
|
|
1205
|
+
)
|
|
1206
|
+
})
|
|
1207
|
+
|
|
1208
|
+
check('set_persona 的输出 schema 能被真实 dsh-tools 的 DSL 接受(注册不失败)', () => {
|
|
1209
|
+
// 背景:DSH 的 schema DSL 与 JSON Schema 不同 —— 根级不支持 required(作者错误
|
|
1210
|
+
// "schema.required is not supported by the value schema DSL")、object 属性必须显式
|
|
1211
|
+
// 声明 additionalProperties。两者都曾让 defineTool 抛错,再被 .catch() 吞成一条 warn
|
|
1212
|
+
// ⇒ set_persona 静默不注册(Agent 无法改人设),而 196 项断言全绿 —— 因为全链
|
|
1213
|
+
// 只有 dsh-llm 桩、从未用真实 DSL 校验过 schema。本条就是那个缺口。
|
|
1214
|
+
const toolCall = extractCall(indexSource, 'defineTool(')
|
|
1215
|
+
assert.ok(toolCall !== '', 'index.mjs 应调用 defineTool 注册 set_persona')
|
|
1216
|
+
// 结构层(去注释后):不得出现根级 required;对象属性必须带 additionalProperties
|
|
1217
|
+
const outputSchema = toolCall.slice(toolCall.indexOf('output:'))
|
|
1218
|
+
assert.equal(
|
|
1219
|
+
/required:\s*\[/.test(outputSchema),
|
|
1220
|
+
false,
|
|
1221
|
+
'set_persona 输出 schema 不得使用根级 required(DSH DSL 不支持,defineTool 会抛错且被静默吞掉)'
|
|
1222
|
+
)
|
|
1223
|
+
assert.ok(
|
|
1224
|
+
/ok:\s*\{[^}]*required:\s*true/.test(outputSchema),
|
|
1225
|
+
'set_persona 输出的 ok 必须用属性级 required: true 声明必填(DSL 的唯一写法)'
|
|
1226
|
+
)
|
|
1227
|
+
assert.ok(
|
|
1228
|
+
/changes:\s*\{[^{}]*additionalProperties:\s*false/.test(outputSchema.replace(/\s+/g, ' ')),
|
|
1229
|
+
'set_persona 输出的 changes(object)必须显式声明 additionalProperties: false'
|
|
1230
|
+
)
|
|
1231
|
+
// 行为层:本机能解析到真实 @deepseek-ai/dsh-tools 时,用**真实 defineTool** 校验
|
|
1232
|
+
// schema —— 这是「结构对了但 DSL 仍拒收」这类回归的唯一可靠判据。
|
|
1233
|
+
// (顶层 await 已解析;本机未装 DSH 时为 null,CI 上走结构层即可。)
|
|
1234
|
+
if (defineTool) {
|
|
1235
|
+
// 从真实源码抽出 output.schema 的字面量并求值,保证测的就是插件里那份
|
|
1236
|
+
const literal = outputSchema.slice(outputSchema.indexOf('schema: {'))
|
|
1237
|
+
const open = literal.indexOf('{')
|
|
1238
|
+
let depth = 0
|
|
1239
|
+
let quote = null
|
|
1240
|
+
let end = -1
|
|
1241
|
+
for (let i = open; i < literal.length; i++) {
|
|
1242
|
+
const c = literal[i]
|
|
1243
|
+
if (quote) {
|
|
1244
|
+
if (c === '\\') { i++; continue }
|
|
1245
|
+
if (c === quote) quote = null
|
|
1246
|
+
continue
|
|
1247
|
+
}
|
|
1248
|
+
if (c === "'" || c === '"') { quote = c; continue }
|
|
1249
|
+
if (c === '{') depth++
|
|
1250
|
+
else if (c === '}') { depth--; if (depth === 0) { end = i + 1; break } }
|
|
1251
|
+
}
|
|
1252
|
+
assert.ok(end > 0, '无法从 index.mjs 抽出 output.schema 字面量')
|
|
1253
|
+
let schema
|
|
1254
|
+
try {
|
|
1255
|
+
schema = new Function(`return (${literal.slice(open, end)})`)()
|
|
1256
|
+
} catch (err) {
|
|
1257
|
+
assert.fail(`output.schema 字面量求值失败:${err.message}`)
|
|
1258
|
+
}
|
|
1259
|
+
assert.doesNotThrow(
|
|
1260
|
+
() => defineTool({
|
|
1261
|
+
name: 'set_persona',
|
|
1262
|
+
description: 'verify-config schema 契约探针',
|
|
1263
|
+
parameters: { nickname: { type: 'string', description: 'probe' } },
|
|
1264
|
+
output: { schema },
|
|
1265
|
+
async execute() { return { ok: true } }
|
|
1266
|
+
}),
|
|
1267
|
+
'真实 dsh-tools 的 defineTool 拒绝了 set_persona 的输出 schema(工具将静默注册失败)'
|
|
1268
|
+
)
|
|
1269
|
+
return
|
|
1270
|
+
}
|
|
1271
|
+
if (!skipNote('verify-config', 1, '本机未安装 @deepseek-ai/dsh-tools,set_persona 仅做结构层校验(行为层需真实 DSL)', '安装 DSH 后重跑即可覆盖行为层')) {
|
|
1272
|
+
failures.push({ name: 'set_persona schema 行为层(需真实 dsh-tools)', err: new Error('--strict 下跳过按失败处理') })
|
|
1273
|
+
}
|
|
1274
|
+
})
|
|
1275
|
+
|
|
1276
|
+
check('set_persona 的模型侧描述必须中英并列', () => {
|
|
1277
|
+
// 工具 schema 只在 apply 时注册一次,而宿主 registry 对同名重注册会抛
|
|
1278
|
+
// `tool "set_persona" is already registered`(实测 dsh-tools 源码)⇒ 语言切换时
|
|
1279
|
+
// **无法**原地换描述。所以描述必须中英并列,覆盖 zh / en 两种界面语言。
|
|
1280
|
+
//
|
|
1281
|
+
// 只检查 **parameters** 块里的描述:output.schema 的 description 是给宿主/渲染层
|
|
1282
|
+
// 用的结构化标注(如「实际发生变化的字段」),不是模型读的自然语言说明 —— 把它
|
|
1283
|
+
// 一并纳入会误报(本条初版就这么写的,被断言自己抓出来了)。
|
|
1284
|
+
const toolCall = stripComments(extractCall(indexSource, 'defineTool('))
|
|
1285
|
+
assert.ok(toolCall !== '', '找不到 defineTool 调用')
|
|
1286
|
+
const paramsStart = toolCall.indexOf('parameters:')
|
|
1287
|
+
const paramsEnd = toolCall.indexOf('output:', paramsStart)
|
|
1288
|
+
assert.ok(paramsStart > 0 && paramsEnd > paramsStart, '无法定位 parameters 块')
|
|
1289
|
+
const params = toolCall.slice(paramsStart, paramsEnd)
|
|
1290
|
+
const descriptions = [...params.matchAll(/description:\s*\n?\s*((?:'[^']*'\s*\+?\s*)+)/g)]
|
|
1291
|
+
.map((m) => [...m[1].matchAll(/'([^']*)'/g)].map((x) => x[1]).join(''))
|
|
1292
|
+
.filter((text) => text.length > 0)
|
|
1293
|
+
assert.ok(descriptions.length >= 8, `模型侧描述条目过少:${descriptions.length}`)
|
|
1294
|
+
for (const text of descriptions) {
|
|
1295
|
+
assert.ok(/[\u4e00-\u9fff]/.test(text), `工具描述缺少中文:${text.slice(0, 40)}`)
|
|
1296
|
+
assert.ok(/[A-Za-z]{3,}/.test(text), `工具描述缺少英文(zh/en 两种界面都要能读):${text.slice(0, 40)}`)
|
|
1297
|
+
}
|
|
1298
|
+
})
|
|
1299
|
+
|
|
1300
|
+
check('配置损坏时上报原因(设置页不必等一次失败的保存才知道)', () => {
|
|
1301
|
+
assert.ok(
|
|
1302
|
+
/configError: configLoadFailure \? describeConfigFailure\(configLoadFailure\) : null/.test(indexCode),
|
|
1303
|
+
'GET /api/soul/config 应带回 configError,否则用户只会看到「人设无故被重置」'
|
|
1304
|
+
)
|
|
1305
|
+
assert.ok(
|
|
1306
|
+
/s\.error = typeof payload\.configError === 'string'[\s\S]{0,120}?typeof payload\.deliveryWarning === 'string'/.test(clientCode),
|
|
1307
|
+
'客户端应同时展示 configError(读不出来)与 deliveryWarning(送不到会话)——两者症状相同、处理方式不同'
|
|
1308
|
+
)
|
|
1309
|
+
})
|
|
1310
|
+
|
|
1311
|
+
// ---------- 损坏配置的逃生口(重置)----------
|
|
1312
|
+
|
|
1313
|
+
check('移除损坏配置只有一条实现:lib/store.mjs 的 moveAsideConfigFile', () => {
|
|
1314
|
+
assert.ok(
|
|
1315
|
+
/export async function moveAsideConfigFile\(filePath\)[\s\S]*?await rename\(filePath, target\)/.test(storeCode),
|
|
1316
|
+
'必须用 rename 把损坏文件移开——只 copyFile 保留原文件的话,读取仍判定为损坏,重置依然被拒'
|
|
1317
|
+
)
|
|
1318
|
+
assert.equal(
|
|
1319
|
+
/rename\(configPath\(\)|unlink\(configPath\(\)/.test(indexCode),
|
|
1320
|
+
false,
|
|
1321
|
+
'index.mjs 不应自己搬移/删除配置文件,收口在 lib/store.mjs'
|
|
1322
|
+
)
|
|
1323
|
+
})
|
|
1324
|
+
|
|
1325
|
+
check('重置是配置损坏时的逃生口:先移开损坏文件,再复用同一条写入路径', () => {
|
|
1326
|
+
const matched = indexCode.match(/async function resetConfigWithEscape\(\)[\s\S]*?\n\}/)
|
|
1327
|
+
assert.ok(matched, 'index.mjs 中找不到 resetConfigWithEscape')
|
|
1328
|
+
const body = matched[0]
|
|
1329
|
+
assert.ok(/moveAsideConfigFile\(configPath\(\)\)/.test(body), '重置应先移开损坏文件(=备份到 .corrupt)')
|
|
1330
|
+
assert.ok(/configLoadFailure = null/.test(body), '移开之后必须清掉失败标记,否则 saveConfig 仍会拒绝')
|
|
1331
|
+
assert.ok(/commitConfig\(/.test(body), '重置应复用 commitConfig,而不是另写一份「读—改—写」')
|
|
1332
|
+
assert.ok(
|
|
1333
|
+
/catch[\s\S]*?configLoadFailure = failure[\s\S]*?throw/.test(body),
|
|
1334
|
+
'写入失败必须恢复失败标记,否则会把「文件仍然不可用」误报成正常'
|
|
1335
|
+
)
|
|
1336
|
+
})
|
|
1337
|
+
|
|
1338
|
+
check('两个重置入口都走逃生口(HTTP 与斜杠命令)', () => {
|
|
1339
|
+
const route = indexCode.match(/path: '\/api\/soul\/config\/reset'[\s\S]*?\n \}\)/)
|
|
1340
|
+
assert.ok(route, '找不到 /api/soul/config/reset 端点')
|
|
1341
|
+
assert.ok(/resetConfigWithEscape\(\)/.test(route[0]), 'HTTP 重置必须走逃生口')
|
|
1342
|
+
assert.ok(/backupPath/.test(route[0]), 'HTTP 重置应回传备份路径,供界面如实说明数据去哪了')
|
|
1343
|
+
|
|
1344
|
+
const resetBranch = indexCode.match(/if \(first === 'reset'\)[\s\S]*?\n \}/)
|
|
1345
|
+
assert.ok(resetBranch, "找不到 /soul reset 分支")
|
|
1346
|
+
assert.ok(/resetConfigWithEscape\(\)/.test(resetBranch[0]), '/soul reset 必须走逃生口')
|
|
1347
|
+
assert.ok(/backupPath/.test(resetBranch[0]), '/soul reset 应告知备份位置')
|
|
1348
|
+
|
|
1349
|
+
assert.equal(
|
|
1350
|
+
/commitConfig\(current => defaultConfigPreservingPersonas\(current\)\)/.test(indexCode),
|
|
1351
|
+
false,
|
|
1352
|
+
'不得再有绕过逃生口的重置路径——否则损坏状态下会走到一条必然失败的写'
|
|
1353
|
+
)
|
|
1354
|
+
})
|
|
1355
|
+
|
|
1356
|
+
check('重置失败不得被报成成功', () => {
|
|
1357
|
+
const handler = clientCode.match(/const handleReset = async \(\) => \{[\s\S]*?\n \}/)
|
|
1358
|
+
assert.ok(handler, '找不到客户端的 handleReset')
|
|
1359
|
+
assert.ok(/if \(!payload\)/.test(handler[0]), 'handleReset 必须区分失败(resetConfig 失败时返回 undefined)')
|
|
1360
|
+
assert.ok(/toast\.resetFailed/.test(handler[0]), '失败时应用失败提示,而不是无条件报「已重置」')
|
|
1361
|
+
assert.ok(/payload\.backupPath/.test(handler[0]), '有备份时应提示「已备份」,避免用户以为数据凭空消失')
|
|
1362
|
+
})
|
|
1363
|
+
|
|
1364
|
+
// ---------- 配置生效链路的可观测性 ----------
|
|
1365
|
+
|
|
1366
|
+
check('注入失败必须留下可观测记录,不得静默返回', () => {
|
|
1367
|
+
const matched = indexCode.match(/function injectPromptToAllAgents\(ctx, config\)[\s\S]*?\n\}/)
|
|
1368
|
+
assert.ok(matched, 'index.mjs 中找不到 injectPromptToAllAgents')
|
|
1369
|
+
const body = matched[0]
|
|
1370
|
+
assert.ok(/deliveryState\.injectionProblem = reason/.test(body), 'agents 不可用时必须记录原因')
|
|
1371
|
+
assert.equal(
|
|
1372
|
+
/if \(!agents \|\| typeof agents\.list !== 'function'\) \{\s*return\s*\}/.test(body),
|
|
1373
|
+
false,
|
|
1374
|
+
'agents 不可用不得直接裸 return——这正是「改了人设不生效却毫无线索」的来源'
|
|
1375
|
+
)
|
|
1376
|
+
assert.ok(/warnOnce\(/.test(body), '应经 logger 告警(同因只告一次,避免刷屏)')
|
|
1377
|
+
assert.ok(/delivered\+\+/.test(body) && /failed\+\+/.test(body), '应如实统计成功与失败数')
|
|
1378
|
+
assert.equal(/console\.(warn|error)\(/.test(body), false, '不要用被注释掉的 console——统一走 ctx.logger')
|
|
1379
|
+
})
|
|
1380
|
+
|
|
1381
|
+
check('诊断端点 /api/soul/status 暴露两条通道', () => {
|
|
1382
|
+
const matched = indexCode.match(/path: '\/api\/soul\/status'[\s\S]*?\n \}\)/)
|
|
1383
|
+
assert.ok(matched, '应注册 GET /api/soul/status')
|
|
1384
|
+
const body = matched[0]
|
|
1385
|
+
assert.ok(/channels:/.test(body), '应分别报告两条通道')
|
|
1386
|
+
assert.ok(/section:/.test(body) && /injection/.test(body), 'section 与 injection 都要有')
|
|
1387
|
+
assert.ok(/sectionRegisteredAt/.test(indexCode), 'section 的注册状态必须被记录')
|
|
1388
|
+
})
|
|
1389
|
+
|
|
1390
|
+
check('「送不到会话」与「读不出来」是两种问题,分开上报', () => {
|
|
1391
|
+
assert.ok(
|
|
1392
|
+
/deliveryWarning: deliveryProblemText\(config\)/.test(indexCode),
|
|
1393
|
+
'GET /api/soul/config 应带回 deliveryWarning'
|
|
1394
|
+
)
|
|
1395
|
+
assert.ok(
|
|
1396
|
+
/deliveryWarning: deliveryProblemText\(updated\)/.test(indexCode),
|
|
1397
|
+
'POST /api/soul/config 也应带回——保存成功但送不到会话时要让用户看见'
|
|
1398
|
+
)
|
|
1399
|
+
assert.ok(/deliveryProblemText\(config\)/.test(indexCode), '/soul show 应展示送达状态')
|
|
1400
|
+
assert.ok(/t\.deliveryLabel/.test(indexCode), '/soul show 应带「送达」标签行')
|
|
1401
|
+
// delivery* 文案键**只定义在 COMMAND_MESSAGES**(PROMPT_TEXT 里没有)。
|
|
1402
|
+
// deliveryProblemText 必须从 commandMessages 取表:此前误用 promptTextOf,
|
|
1403
|
+
// 'agents-service-unavailable' 分支返回 undefined(JSON 序列化丢字段,故障不可见)、
|
|
1404
|
+
// 'inject-failed' 分支抛 TypeError(保存已落盘却报 HTTP 500)—— 变异实验均实测复现。
|
|
1405
|
+
const dpt = stripComments(extractObject(indexSource, 'function deliveryProblemText'))
|
|
1406
|
+
assert.ok(dpt !== '', '找不到 deliveryProblemText 函数体')
|
|
1407
|
+
assert.ok(
|
|
1408
|
+
/commandMessages\(config\)/.test(dpt),
|
|
1409
|
+
'deliveryProblemText 必须用 commandMessages 取文案(delivery* 键只在 COMMAND_MESSAGES;用 promptTextOf 会拿到 undefined 并在 inject-failed 时抛 TypeError)'
|
|
1410
|
+
)
|
|
1411
|
+
assert.equal(
|
|
1412
|
+
/promptTextOf\(config\)/.test(dpt),
|
|
1413
|
+
false,
|
|
1414
|
+
'deliveryProblemText 不得再从 PROMPT_TEXT 取 delivery 文案(该表没有这些键)'
|
|
1415
|
+
)
|
|
1416
|
+
// 两张表必须真的提供这两个键(防「改了取表方向、键又缺失」的二次回归)
|
|
1417
|
+
for (const table of ['COMMAND_MESSAGES']) {
|
|
1418
|
+
for (const lang of ['zh', 'en']) {
|
|
1419
|
+
const keys = tableLangKeys(indexSource, table, lang)
|
|
1420
|
+
for (const key of ['deliveryNoAgentsService', 'deliveryInjectFailed']) {
|
|
1421
|
+
assert.ok(
|
|
1422
|
+
keys.has(key),
|
|
1423
|
+
`${table}.${lang} 缺少 ${key}:deliveryProblemText 的两个分支都会拿不到文案`
|
|
1424
|
+
)
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
})
|
|
1429
|
+
|
|
1430
|
+
check('版本号不新增需手工同步的常量(唯一来源 package.json)', () => {
|
|
1431
|
+
assert.ok(
|
|
1432
|
+
/createRequire\(import\.meta\.url\)\('\.\/package\.json'\)\.version/.test(indexCode),
|
|
1433
|
+
'host 端版本应直接取自 package.json'
|
|
1434
|
+
)
|
|
1435
|
+
assert.equal(
|
|
1436
|
+
/const\s+(PLUGIN_)?VERSION\s*=\s*'0\./.test(indexCode),
|
|
1437
|
+
false,
|
|
1438
|
+
'不要再写一个版本常量——需要手工同步的地方已经有三处'
|
|
1439
|
+
)
|
|
1440
|
+
})
|
|
1441
|
+
|
|
1442
|
+
// ---------- 双语文案与校验链完整性 ----------
|
|
1443
|
+
|
|
1444
|
+
check('host 双语文案表逐键对齐(PROMPT_TEXT / COMMAND_MESSAGES)', () => {
|
|
1445
|
+
for (const table of ['PROMPT_TEXT', 'COMMAND_MESSAGES']) {
|
|
1446
|
+
const zh = tableLangKeys(indexSource, table, 'zh')
|
|
1447
|
+
const en = tableLangKeys(indexSource, table, 'en')
|
|
1448
|
+
assert.ok(zh.size > 10, `${table} 的 zh 键数异常:${zh.size}`)
|
|
1449
|
+
assert.deepEqual(
|
|
1450
|
+
[...zh].sort(),
|
|
1451
|
+
[...en].sort(),
|
|
1452
|
+
`${table} 的 zh / en 必须逐键对齐(少一个键就是另一种语言下少一句话)`
|
|
1453
|
+
)
|
|
1454
|
+
}
|
|
1455
|
+
})
|
|
1456
|
+
|
|
1457
|
+
check('client 双语文案表逐键对齐', () => {
|
|
1458
|
+
const zh = objectKeys(extractObject(clientSource, 'const zh = {'))
|
|
1459
|
+
const en = objectKeys(extractObject(clientSource, 'const en = {'))
|
|
1460
|
+
assert.ok(zh.size > 50, `client zh 键数异常:${zh.size}`)
|
|
1461
|
+
assert.deepEqual([...zh].sort(), [...en].sort(), 'client i18n 的 zh / en 必须逐键对齐')
|
|
1462
|
+
})
|
|
1463
|
+
|
|
1464
|
+
check('校验链与 --strict 链覆盖同一组脚本,且严格语义真在各脚本里实现', () => {
|
|
1465
|
+
const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'))
|
|
1466
|
+
const entries = (cmd) => [...String(cmd).matchAll(/scripts\/([\w.-]+\.mjs)(\s+--strict)?/g)]
|
|
1467
|
+
.map((m) => ({ file: m[1], strict: Boolean(m[2]) }))
|
|
1468
|
+
const chain = entries(pkg.scripts['verify'])
|
|
1469
|
+
const strict = entries(pkg.scripts['verify:strict'])
|
|
1470
|
+
assert.ok(chain.length >= 5, `verify 链应覆盖全部脚本,实际 ${chain.length}`)
|
|
1471
|
+
assert.deepEqual(
|
|
1472
|
+
strict.map((e) => e.file),
|
|
1473
|
+
chain.map((e) => e.file),
|
|
1474
|
+
'verify:strict 必须与 verify 逐项同序,否则 --strict 会漏掉某些脚本'
|
|
1475
|
+
)
|
|
1476
|
+
assert.ok(
|
|
1477
|
+
strict.every((e) => e.strict),
|
|
1478
|
+
'verify:strict 的每一项都必须带 --strict,否则那一项仍会「跳过即通过」'
|
|
1479
|
+
)
|
|
1480
|
+
// 链覆盖必须有下限且与 scripts 目录对账:只比对「两条链互相同序」时,对称地删掉
|
|
1481
|
+
// 一个脚本(两条链一起删)两条链依然相等、CI 照样绿 —— 覆盖声明没有任何机器保证。
|
|
1482
|
+
const onDisk = readdirSync(new URL('../scripts', import.meta.url))
|
|
1483
|
+
.filter((f) => /^verify-[\w-]+\.mjs$/.test(f))
|
|
1484
|
+
.sort()
|
|
1485
|
+
assert.deepEqual(
|
|
1486
|
+
[...new Set(chain.map((e) => e.file))].sort(),
|
|
1487
|
+
onDisk,
|
|
1488
|
+
'verify 链必须覆盖 scripts 目录下的全部 verify-*.mjs(对称删脚本不应被静默放过)'
|
|
1489
|
+
)
|
|
1490
|
+
for (const { file } of [...chain, ...strict]) {
|
|
1491
|
+
const url = new URL(`../scripts/${file}`, import.meta.url)
|
|
1492
|
+
assert.ok(existsSync(url), `package.json 引用了不存在的脚本:${file}`)
|
|
1493
|
+
// 去注释后再匹配:把真实调用注释掉、在注释里留下同样字样,不应被视为满足。
|
|
1494
|
+
// (M5 类漏洞:正断言只查「字样出现」时,注释里的字样同样命中。)
|
|
1495
|
+
const src = stripComments(readFileSync(url, 'utf8'))
|
|
1496
|
+
// 光在 package.json 里写上 `--strict` 是不够的 —— 脚本必须真的实现那套语义:
|
|
1497
|
+
// ① 引入 skip-report(跳过与断言数的唯一实现);
|
|
1498
|
+
// ② 把「本脚本有多少断言」交给它,并**消费其结果**:跑完时
|
|
1499
|
+
// `if (!assertCount(...))`,或像 verify-compat 那样把 nominal(随环境浮动的
|
|
1500
|
+
// 检查数)交给 `skipExit(..., NOMINAL_ASSERTIONS, ...)`。两者取其一 ——
|
|
1501
|
+
// compat 的检查数取决于本机装了什么,精确相等不适用。
|
|
1502
|
+
// 只检测「字样出现」是不够的:调用被注释掉、或 assertCount 返回值被丢弃
|
|
1503
|
+
// (打印了不一致却仍 return 0),都曾实测绕过 —— 所以必须匹配**消费形态**。
|
|
1504
|
+
assert.ok(
|
|
1505
|
+
// 必须是一条**真正的 import 语句**,不能只在注释里出现这段路径 —— 否则把 import
|
|
1506
|
+
// 注掉、留下一行注释,这条断言照样通过(判断力对照实测过这个漏法)。
|
|
1507
|
+
/^\s*import\b[^\n]*from '\.\/lib\/skip-report\.mjs'/m.test(src),
|
|
1508
|
+
`${file} 必须真正 import ./lib/skip-report.mjs,否则跳过语义与断言数都无人守`
|
|
1509
|
+
)
|
|
1510
|
+
const consumesCount = /!\s*assertCount\(/.test(src)
|
|
1511
|
+
const skipExitCall = extractCall(src, 'skipExit(')
|
|
1512
|
+
const reportsNominal = skipExitCall !== '' && /NOMINAL_ASSERTIONS/.test(skipExitCall)
|
|
1513
|
+
assert.ok(
|
|
1514
|
+
consumesCount || reportsNominal,
|
|
1515
|
+
`${file} 必须把断言数交给 skip-report 并消费其结果(if (!assertCount(...)) 或 skipExit(..., NOMINAL_ASSERTIONS, ...));返回值被丢弃或只剩注释均不算`
|
|
1516
|
+
)
|
|
1517
|
+
if (reportsNominal && !consumesCount) {
|
|
1518
|
+
assert.ok(
|
|
1519
|
+
/const\s+NOMINAL_ASSERTIONS\s*=/.test(src),
|
|
1520
|
+
`${file} 引用 NOMINAL_ASSERTIONS 但未声明该常量`
|
|
1521
|
+
)
|
|
1522
|
+
}
|
|
1523
|
+
assert.equal(
|
|
1524
|
+
/process\.argv\.includes\('--strict'\)/.test(src),
|
|
1525
|
+
false,
|
|
1526
|
+
`${file} 不得自己解析 --strict(必须走 skip-report 的 strictMode(),否则两处判定会漂移)`
|
|
1527
|
+
)
|
|
1528
|
+
}
|
|
1529
|
+
})
|
|
1530
|
+
|
|
1531
|
+
// 「断言数一致」与「每一项真的通过」是两回事:前者只防脚本与声明漂移,
|
|
1532
|
+
// 后者才是校验本身。check() 里捕获的任何异常都必须让整体失败 —— 否则失败的
|
|
1533
|
+
// check 只打印一行 ✗ 却仍退出 0,这正是「看起来绿、实际没守住」的形态。
|
|
1534
|
+
if (failures.length > 0) {
|
|
1535
|
+
console.log(`\n结果:失败(${failures.length} 项未通过)。`)
|
|
1536
|
+
process.exit(1)
|
|
1537
|
+
}
|
|
1538
|
+
if (!assertCount('verify-config', passed, EXPECTED_ASSERTIONS)) process.exit(1)
|
|
1539
|
+
|
|
235
1540
|
console.log(`\n全部通过:${passed} 项检查`)
|