dsh-knit 0.11.0 → 0.12.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/CHANGELOG.md +578 -0
- package/README.en.md +6 -4
- package/README.md +4 -3
- package/package.json +4 -3
- package/src/client/client.js +228 -10
- package/src/host/index.js +18 -1
- package/src/host/links.js +304 -0
- package/test/client.test.mjs +180 -0
- package/test/fixture.mjs +6 -1
- package/test/host-http.test.mjs +80 -2
- package/test/i18n.test.mjs +123 -3
- package/test/links.test.mjs +271 -0
package/test/i18n.test.mjs
CHANGED
|
@@ -17,9 +17,10 @@ const h = React.createElement
|
|
|
17
17
|
/**
|
|
18
18
|
* 造一个假上下文,用于把模块 apply 起来(真实运行时由 DSH 调用)。
|
|
19
19
|
* @param {string} locale - 活动语言
|
|
20
|
+
* @param {{betterSidebar?: boolean}} [extra] - 额外提供哪些可选服务
|
|
20
21
|
* @returns {{ctx: object, log: object, registered: object}} 上下文与记录
|
|
21
22
|
*/
|
|
22
|
-
function contextFor(locale) {
|
|
23
|
+
function contextFor(locale, extra = {}) {
|
|
23
24
|
const log = { tabs: [], bsTabs: [], slots: [], effects: [] }
|
|
24
25
|
const { locale: localeService, registered } = makeLocale({ active: locale })
|
|
25
26
|
|
|
@@ -30,6 +31,9 @@ function contextFor(locale) {
|
|
|
30
31
|
|
|
31
32
|
const services = { slots, locale: localeService }
|
|
32
33
|
services.sidebarRightTabs = { register(def) { log.tabs.push(def); return () => {} } }
|
|
34
|
+
if (extra.betterSidebar) {
|
|
35
|
+
services.betterSidebar = { registerTab(def) { log.bsTabs.push(def); return () => {} } }
|
|
36
|
+
}
|
|
33
37
|
|
|
34
38
|
const ctx = {
|
|
35
39
|
slots,
|
|
@@ -51,11 +55,12 @@ function contextFor(locale) {
|
|
|
51
55
|
* 装载模块并 apply 到指定语言。
|
|
52
56
|
* @param {string} locale - 活动语言
|
|
53
57
|
* @param {object} [options] - 传给 loadClientModule
|
|
58
|
+
* @param {{betterSidebar?: boolean}} [extra] - 额外提供哪些可选服务
|
|
54
59
|
* @returns {{exports: object, log: object, registered: object}} 结果
|
|
55
60
|
*/
|
|
56
|
-
function boot(locale, options) {
|
|
61
|
+
function boot(locale, options, extra) {
|
|
57
62
|
const loaded = loadClientModule(options)
|
|
58
|
-
const { ctx, log, registered } = contextFor(locale)
|
|
63
|
+
const { ctx, log, registered } = contextFor(locale, extra)
|
|
59
64
|
loaded.exports.apply(ctx)
|
|
60
65
|
return { exports: loaded.exports, log, registered, markdownCalls: loaded.markdownCalls }
|
|
61
66
|
}
|
|
@@ -342,6 +347,121 @@ test('冒烟:中英环境下渲染同一份数据都不抛错,且文案确
|
|
|
342
347
|
assert.ok(!/刷新|按/.test(enText), `英文环境混进了中文:${enText}`)
|
|
343
348
|
})
|
|
344
349
|
|
|
350
|
+
/* ── 硬编码中文的守卫(2026-09-19 加的)──────────────────
|
|
351
|
+
*
|
|
352
|
+
* 起因:真机截图暴露「英文界面 + 中文『3 分钟前』」——`relTime()` 把那六条时间文案
|
|
353
|
+
* 硬编码在函数里,没进词典。同一批还查出两处:文档列表的 `aria-label`、
|
|
354
|
+
* better-sidebar 的 tab 标题。
|
|
355
|
+
*
|
|
356
|
+
* 上面那条冒烟用例**当时没抓住它**,因为它只硬编码地查了 `刷新|按` 两个词 ——
|
|
357
|
+
* 写死几个词等于给未来留了个洞。所以这里换成**通用判据**:
|
|
358
|
+
* 英文环境渲染出的整棵树里,文字与无障碍属性都不许出现汉字。
|
|
359
|
+
*
|
|
360
|
+
* ⚠️ 语料必须保持 ASCII(`listPayload()` 的标题是 'A' / 'B'):
|
|
361
|
+
* 文档自带的中文标题会把这条用例误报成失败,而那是**数据**、不是**文案**。
|
|
362
|
+
*/
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* 收集渲染树里所有**直接**文本子节点(不走子树,避免重复)。
|
|
366
|
+
* @param {object[]} nodes - 扁平节点
|
|
367
|
+
* @returns {string[]} 文本片段
|
|
368
|
+
*/
|
|
369
|
+
function ownTexts(nodes) {
|
|
370
|
+
return nodes.flatMap((n) => (n.children || []).filter((c) => typeof c === 'string'))
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* 挑出含汉字的片段,失败信息里就能直接看出漏了哪一条。
|
|
375
|
+
* @param {string[]} texts - 文本片段
|
|
376
|
+
* @returns {string[]} 含汉字的片段
|
|
377
|
+
*/
|
|
378
|
+
function cjkIn(texts) {
|
|
379
|
+
return texts.filter((s) => /[\u3400-\u9fff]/.test(s))
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const MIN = 60 * 1000
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* 造一份**把相对时间六个分支全走一遍**的列表载荷。
|
|
386
|
+
*
|
|
387
|
+
* ⚠️ 这份语料不能省。第一版守卫用的是 `listPayload()`,它两篇文档只落在
|
|
388
|
+
* `justNow` 与 `yesterday` 两个分支上 —— 于是「把 `分钟前` 硬编码回去」
|
|
389
|
+
* 这种回归**测不出来**(我实测过:补丁打上去,守卫照样全绿)。
|
|
390
|
+
* 真机截图里漏出来的恰恰就是 `分钟前` 那一条。
|
|
391
|
+
*
|
|
392
|
+
* @returns {object} 载荷
|
|
393
|
+
*/
|
|
394
|
+
function timePayload() {
|
|
395
|
+
const now = Date.now()
|
|
396
|
+
const ages = [
|
|
397
|
+
['now.md', 0],
|
|
398
|
+
['min.md', 5 * MIN],
|
|
399
|
+
['hour.md', 5 * 60 * MIN],
|
|
400
|
+
['yest.md', 25 * 60 * MIN],
|
|
401
|
+
['days.md', 3 * 24 * 60 * MIN],
|
|
402
|
+
['month.md', 40 * 24 * 60 * MIN],
|
|
403
|
+
]
|
|
404
|
+
return {
|
|
405
|
+
ok: true,
|
|
406
|
+
root: '/p',
|
|
407
|
+
total: ages.length,
|
|
408
|
+
mode: 'time',
|
|
409
|
+
topic: '',
|
|
410
|
+
docs: ages.map(([rel, age]) => ({
|
|
411
|
+
path: `/p/${rel}`, rel, name: rel, title: rel, summary: 's', mtimeMs: now - age, score: null,
|
|
412
|
+
})),
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
test('守卫:英文环境渲染出的整棵树里一个汉字都没有', async () => {
|
|
417
|
+
// 用 timePayload:六个时间分支都要真的渲染出来,否则守卫是「没覆盖的空转绿灯」
|
|
418
|
+
const { nodes } = await renderPanel('en', () => timePayload())
|
|
419
|
+
const bad = cjkIn(ownTexts(nodes))
|
|
420
|
+
assert.deepEqual(bad, [], `英文渲染里混进了中文(说明有文案没走 t()):${bad.join(' / ')}`)
|
|
421
|
+
})
|
|
422
|
+
|
|
423
|
+
test('守卫:英文环境下无障碍与提示属性里也没有汉字', async () => {
|
|
424
|
+
const { nodes } = await renderPanel('en', () => timePayload())
|
|
425
|
+
const attrs = ['aria-label', 'title', 'placeholder', 'alt']
|
|
426
|
+
const bad = []
|
|
427
|
+
for (const n of nodes) {
|
|
428
|
+
for (const a of attrs) {
|
|
429
|
+
const v = n.props[a]
|
|
430
|
+
if (typeof v === 'string' && /[\u3400-\u9fff]/.test(v)) bad.push(`${a}="${v}"`)
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
assert.deepEqual(bad, [], `英文环境的属性里混进了中文:${bad.join(' / ')}`)
|
|
434
|
+
})
|
|
435
|
+
|
|
436
|
+
test('守卫:相对时间的六个分支都随语言(原来是硬编码中文)', async () => {
|
|
437
|
+
const en = await renderPanel('en', () => timePayload())
|
|
438
|
+
const enTimes = byClass(en.nodes, 'knit-time').map(textOf)
|
|
439
|
+
assert.equal(enTimes.length, 6, `六个分支都该渲染出来,实际只有 ${enTimes.length} 条`)
|
|
440
|
+
assert.deepEqual(
|
|
441
|
+
enTimes.slice(0, 5),
|
|
442
|
+
['just now', '5 min ago', '5 h ago', 'yesterday', '3 d ago'],
|
|
443
|
+
)
|
|
444
|
+
// 第六档是「月/日」,具体日期随当天变,只校验形状
|
|
445
|
+
assert.match(enTimes[5], /^\d{1,2}\/\d{1,2}$/, `英文的月日应形如 8/10,实际是 ${enTimes[5]}`)
|
|
446
|
+
|
|
447
|
+
const zh = await renderPanel('zh', () => timePayload())
|
|
448
|
+
const zhTimes = byClass(zh.nodes, 'knit-time').map(textOf)
|
|
449
|
+
assert.deepEqual(
|
|
450
|
+
zhTimes.slice(0, 5),
|
|
451
|
+
['刚刚', '5分钟前', '5小时前', '昨天', '3天前'],
|
|
452
|
+
)
|
|
453
|
+
assert.match(zhTimes[5], /^\d{1,2}月\d{1,2}日$/, `中文的月日应形如 8月10日,实际是 ${zhTimes[5]}`)
|
|
454
|
+
})
|
|
455
|
+
|
|
456
|
+
test('守卫:better-sidebar 的 tab 标题随语言(复用 guide.title,不再写死)', () => {
|
|
457
|
+
const en = boot('en', undefined, { betterSidebar: true })
|
|
458
|
+
assert.equal(en.log.bsTabs.length, 1, '应注册一个 better-sidebar tab')
|
|
459
|
+
assert.equal(en.log.bsTabs[0].title(), 'Knit — recent documents')
|
|
460
|
+
|
|
461
|
+
const zh = boot('zh', undefined, { betterSidebar: true })
|
|
462
|
+
assert.equal(zh.log.bsTabs[0].title(), 'Knit 最近文档')
|
|
463
|
+
})
|
|
464
|
+
|
|
345
465
|
/* ── 文案洁癖:词典里不出现表情装饰 ───────────────────── */
|
|
346
466
|
|
|
347
467
|
test('词典:不出现 🤖 之类的表情字符(用户明确要求删掉)', async () => {
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Knit · **引用关系解析**(v0.12)的测试
|
|
3
|
+
*
|
|
4
|
+
* 这一组测试的靶心是 **误报**,不是召回。
|
|
5
|
+
* 依据是真实语料的实测:`SKILL.md` 有 20 个、`01_公众号正文.md` 有 12 个,
|
|
6
|
+
* 那些「只写了 basename」的提及多半在讲**文件名约定**,不是在指某一篇 ——
|
|
7
|
+
* 宽松解析会让 13% 的边是错的(见 `Knit_SDD-v0.12` §1.4)。
|
|
8
|
+
*
|
|
9
|
+
* 所以有一条专门的回归用例:**同名文件存在时,光写 basename 必须不产生边**。
|
|
10
|
+
*/
|
|
11
|
+
import test from 'node:test'
|
|
12
|
+
import assert from 'node:assert/strict'
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
extractRefs,
|
|
16
|
+
createIndex,
|
|
17
|
+
resolveRef,
|
|
18
|
+
buildLinkGraph,
|
|
19
|
+
linksOf,
|
|
20
|
+
resetLinkCache,
|
|
21
|
+
} from '../src/host/links.js'
|
|
22
|
+
|
|
23
|
+
/* ── extractRefs:三种真实写法 ───────────────────────── */
|
|
24
|
+
|
|
25
|
+
test('extractRefs:认反引号路径(真实语料里的主力写法,946 次)', () => {
|
|
26
|
+
assert.deepEqual(extractRefs('见 `docs/A.md` 那篇'), ['docs/A.md'])
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test('extractRefs:反引号路径里的空格要保留(`00_ Knit PRD/x.md` 真实存在)', () => {
|
|
30
|
+
assert.deepEqual(extractRefs('见 `00_ Knit PRD/Knit_PRD-v0.12.md`'), ['00_ Knit PRD/Knit_PRD-v0.12.md'])
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('extractRefs:认标准 Markdown 链接,并忽略锚点', () => {
|
|
34
|
+
assert.deepEqual(extractRefs('[看这里](docs/A.md#三)'), ['docs/A.md'])
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
test('extractRefs:认裸路径提及(前面是空白或中文括号)', () => {
|
|
38
|
+
assert.deepEqual(extractRefs('见 docs/A.md 那篇'), ['docs/A.md'])
|
|
39
|
+
assert.deepEqual(extractRefs('(docs/A.md)'), ['docs/A.md'])
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('extractRefs:去重,保持出现顺序', () => {
|
|
43
|
+
assert.deepEqual(extractRefs('`b.md` 再 `a.md` 又 `b.md`'), ['b.md', 'a.md'])
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('extractRefs:外链不算(不是本工作区的文档)', () => {
|
|
47
|
+
assert.deepEqual(extractRefs('[x](https://ex.com/a.md)'), [])
|
|
48
|
+
assert.deepEqual(extractRefs('`ftp://h/a.md`'), [])
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('extractRefs:非 .md 不算', () => {
|
|
52
|
+
assert.deepEqual(extractRefs('`a.png` `b.js` `c`'), [])
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('extractRefs:不咬 `a.md.bak` 这种更长但不是 .md 的串', () => {
|
|
56
|
+
assert.deepEqual(extractRefs('备份在 a.md.bak'), [])
|
|
57
|
+
assert.deepEqual(extractRefs('`a.md.bak`'), [])
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('extractRefs:不做 `[[wikilink]]`(真实语料 0 次使用,SDD §7.1 已否决)', () => {
|
|
61
|
+
assert.deepEqual(extractRefs('见 [[docs/A]] 那篇'), [])
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test('extractRefs:空/非字符串输入返回空数组,不抛', () => {
|
|
65
|
+
assert.deepEqual(extractRefs(''), [])
|
|
66
|
+
assert.deepEqual(extractRefs(null), [])
|
|
67
|
+
assert.deepEqual(extractRefs(undefined), [])
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
/* ── resolveRef:四级优先 + 歧义不算 ─────────────────── */
|
|
71
|
+
|
|
72
|
+
const IDX = createIndex([
|
|
73
|
+
'README.md',
|
|
74
|
+
'docs/README.md',
|
|
75
|
+
'docs/LoreFlow-Copilot-PRD-v0.6.md',
|
|
76
|
+
'docs/LoreFlow-Copilot-SDD-v0.6.md',
|
|
77
|
+
'a/SKILL.md',
|
|
78
|
+
'b/SKILL.md',
|
|
79
|
+
])
|
|
80
|
+
|
|
81
|
+
test('resolveRef:① 引用方所在目录优先(这才是相对路径的语义)', () => {
|
|
82
|
+
assert.equal(resolveRef('README.md', IDX, 'docs/B.md'), 'docs/README.md')
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('resolveRef:① 同目录没有才退到工作区根', () => {
|
|
86
|
+
assert.equal(resolveRef('README.md', IDX, 'other/B.md'), 'README.md')
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test('resolveRef:② 完整相对路径直接命中', () => {
|
|
90
|
+
assert.equal(resolveRef('docs/LoreFlow-Copilot-PRD-v0.6.md', IDX, 'x/y.md'), 'docs/LoreFlow-Copilot-PRD-v0.6.md')
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
test('resolveRef:③ 缩写后缀 `-SDD-v0.6.md`(真实语料里存在)', () => {
|
|
94
|
+
assert.equal(resolveRef('-SDD-v0.6.md', IDX, 'docs/LoreFlow-Copilot-PRD-v0.6.md'),
|
|
95
|
+
'docs/LoreFlow-Copilot-SDD-v0.6.md')
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('resolveRef:③ 缩写后缀命中多个就不算(宁可少不可错)', () => {
|
|
99
|
+
// `-6.md` 能匹配 PRD-v0.6 与 SDD-v0.6,无法确定
|
|
100
|
+
assert.equal(resolveRef('-6.md', IDX, 'docs/x.md'), null)
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
test('resolveRef:④ basename 唯一时可以命中', () => {
|
|
104
|
+
assert.equal(resolveRef('LoreFlow-Copilot-PRD-v0.6.md', IDX, 'x/y.md'),
|
|
105
|
+
'docs/LoreFlow-Copilot-PRD-v0.6.md')
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
test('🔴 误报回归:同名文件存在时,光写 basename 必须不产生边', () => {
|
|
109
|
+
// 这条是 v0.12 最重要的回归 —— 真实语料里 SKILL.md 有 20 个。
|
|
110
|
+
// 宽松解析(取第一个)会让 `s5-illustration/SKILL.md` 的入度虚高到 15。
|
|
111
|
+
assert.equal(resolveRef('SKILL.md', IDX, 'z/w.md'), null)
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
test('resolveRef:⑤ 工作区里没有这个文件 → null,不编造', () => {
|
|
115
|
+
assert.equal(resolveRef('docs/不存在.md', IDX, 'x.md'), null)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
test('resolveRef:`./` 前缀会被剥掉', () => {
|
|
119
|
+
assert.equal(resolveRef('./docs/LoreFlow-Copilot-PRD-v0.6.md', IDX, 'x.md'),
|
|
120
|
+
'docs/LoreFlow-Copilot-PRD-v0.6.md')
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
test('resolveRef:非法输入 → null,不抛', () => {
|
|
124
|
+
assert.equal(resolveRef('', IDX, 'x.md'), null)
|
|
125
|
+
assert.equal(resolveRef('https://e.com/a.md', IDX, 'x.md'), null)
|
|
126
|
+
assert.equal(resolveRef('a.md', null, 'x.md'), null)
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
/* ── buildLinkGraph:注入 list/read,缓存按签名失效 ───── */
|
|
130
|
+
|
|
131
|
+
/** 造一个可控的假 io:`files` 是 rel → 正文。 */
|
|
132
|
+
function fakeIo(files) {
|
|
133
|
+
const reads = { count: 0, byRel: new Map() }
|
|
134
|
+
const docs = Object.keys(files).map((rel, i) => ({
|
|
135
|
+
rel, title: rel.replace(/\.md$/, ''), mtimeMs: 1000 + i,
|
|
136
|
+
}))
|
|
137
|
+
return {
|
|
138
|
+
reads,
|
|
139
|
+
list: async () => ({ docs, truncated: false }),
|
|
140
|
+
read: async (_root, rel) => {
|
|
141
|
+
reads.count += 1
|
|
142
|
+
reads.byRel.set(rel, (reads.byRel.get(rel) || 0) + 1)
|
|
143
|
+
return { ok: true, rel, text: files[rel] }
|
|
144
|
+
},
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
test('buildLinkGraph:正向边与反向边一致(反向边由正向推出来)', async () => {
|
|
149
|
+
resetLinkCache()
|
|
150
|
+
const io = fakeIo({
|
|
151
|
+
'A.md': '见 `B.md` 与 C.md',
|
|
152
|
+
'B.md': '正文没有引用',
|
|
153
|
+
'C.md': '见 `A.md`',
|
|
154
|
+
})
|
|
155
|
+
const g = await buildLinkGraph('/fake-1', io)
|
|
156
|
+
assert.deepEqual([...g.out.get('A.md')].sort(), ['B.md', 'C.md'])
|
|
157
|
+
assert.deepEqual([...g.in.get('B.md')], ['A.md'])
|
|
158
|
+
assert.deepEqual([...g.in.get('A.md')], ['C.md'])
|
|
159
|
+
assert.deepEqual([...g.in.get('C.md')], ['A.md'])
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('buildLinkGraph:自引用不算边', async () => {
|
|
163
|
+
resetLinkCache()
|
|
164
|
+
const io = fakeIo({ 'A.md': '见 `A.md`' })
|
|
165
|
+
const g = await buildLinkGraph('/fake-2', io)
|
|
166
|
+
assert.equal(g.out.get('A.md').size, 0)
|
|
167
|
+
assert.equal(g.in.get('A.md').size, 0)
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
test('buildLinkGraph:歧义 basename 不产生边(误报回归,图这一层)', async () => {
|
|
171
|
+
resetLinkCache()
|
|
172
|
+
const io = fakeIo({
|
|
173
|
+
'x/SKILL.md': '内容',
|
|
174
|
+
'y/SKILL.md': '内容',
|
|
175
|
+
'doc.md': '见 `SKILL.md`',
|
|
176
|
+
})
|
|
177
|
+
const g = await buildLinkGraph('/fake-3', io)
|
|
178
|
+
assert.equal(g.out.get('doc.md').size, 0, '歧义 basename 不许产生边')
|
|
179
|
+
assert.equal(g.in.get('x/SKILL.md').size, 0)
|
|
180
|
+
assert.equal(g.in.get('y/SKILL.md').size, 0)
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
test('buildLinkGraph:同一签名不重复读盘', async () => {
|
|
184
|
+
resetLinkCache()
|
|
185
|
+
const io = fakeIo({ 'A.md': '见 `B.md`', 'B.md': 'x' })
|
|
186
|
+
await buildLinkGraph('/fake-4', io)
|
|
187
|
+
const first = io.reads.count
|
|
188
|
+
await buildLinkGraph('/fake-4', io)
|
|
189
|
+
assert.equal(io.reads.count, first, '签名没变就不该再读一遍')
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
test('buildLinkGraph:mtime 变了要重建', async () => {
|
|
193
|
+
resetLinkCache()
|
|
194
|
+
const files = { 'A.md': '见 `B.md`', 'B.md': 'x' }
|
|
195
|
+
let bump = 0
|
|
196
|
+
const reads = { count: 0 }
|
|
197
|
+
const docs = () => Object.keys(files).map((rel, i) => ({
|
|
198
|
+
rel, title: rel, mtimeMs: 1000 + i + bump,
|
|
199
|
+
}))
|
|
200
|
+
const io = {
|
|
201
|
+
list: async () => ({ docs: docs(), truncated: false }),
|
|
202
|
+
read: async (_r, rel) => { reads.count += 1; return { ok: true, rel, text: files[rel] } },
|
|
203
|
+
}
|
|
204
|
+
await buildLinkGraph('/fake-5', io)
|
|
205
|
+
const first = reads.count
|
|
206
|
+
bump = 500
|
|
207
|
+
await buildLinkGraph('/fake-5', io)
|
|
208
|
+
assert.ok(reads.count > first, 'mtime 变了必须重建')
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
test('buildLinkGraph:读失败的篇跳过,不影响其它篇(skipped 计数)', async () => {
|
|
212
|
+
resetLinkCache()
|
|
213
|
+
const io = fakeIo({ 'A.md': '见 `B.md`', 'B.md': 'x' })
|
|
214
|
+
const realRead = io.read
|
|
215
|
+
io.read = async (root, rel) => (rel === 'B.md' ? { ok: false, code: 'knit/read-failed' } : realRead(root, rel))
|
|
216
|
+
const g = await buildLinkGraph('/fake-6', io)
|
|
217
|
+
assert.equal(g.parsed, 1)
|
|
218
|
+
assert.equal(g.skipped, 1)
|
|
219
|
+
assert.deepEqual([...g.out.get('A.md')], ['B.md'], 'A 的解析不受 B 读失败影响')
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
/* ── linksOf:形状、排序、计数与截断 ─────────────────── */
|
|
223
|
+
|
|
224
|
+
test('linksOf:未知 rel → not-found(宿主只返回错误码,不返回文案)', () => {
|
|
225
|
+
const g = { titles: new Map([['A.md', 'A']]), in: new Map(), out: new Map(), limited: false }
|
|
226
|
+
assert.deepEqual(linksOf(g, 'nope.md'), { ok: false, code: 'knit/not-found' })
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
test('linksOf:只返回 rel + title,不泄漏内部结构', async () => {
|
|
230
|
+
resetLinkCache()
|
|
231
|
+
const io = fakeIo({ 'A.md': '见 `B.md`', 'B.md': 'x' })
|
|
232
|
+
const g = await buildLinkGraph('/fake-7', io)
|
|
233
|
+
const v = linksOf(g, 'B.md')
|
|
234
|
+
assert.equal(v.ok, true)
|
|
235
|
+
assert.deepEqual(Object.keys(v.incoming[0]).sort(), ['rel', 'title'])
|
|
236
|
+
assert.ok(!('out' in v) && !('titles' in v) && !('signature' in v))
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
test('linksOf:按「对方入度降序」排,平手按 rel 字典序(顺序必须稳定)', async () => {
|
|
240
|
+
resetLinkCache()
|
|
241
|
+
const io = fakeIo({
|
|
242
|
+
// hub 被两篇引用,leaf 没人引用;两者都引用了 T
|
|
243
|
+
'hub.md': '见 `T.md`',
|
|
244
|
+
'x.md': '见 `hub.md`',
|
|
245
|
+
'y.md': '见 `hub.md`',
|
|
246
|
+
'a-leaf.md': '见 `T.md`',
|
|
247
|
+
'T.md': 'target',
|
|
248
|
+
})
|
|
249
|
+
const g = await buildLinkGraph('/fake-8', io)
|
|
250
|
+
const v = linksOf(g, 'T.md')
|
|
251
|
+
assert.deepEqual(v.incoming.map((d) => d.rel), ['hub.md', 'a-leaf.md'],
|
|
252
|
+
'入度高的引用方排前面')
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
test('linksOf:计数给全量,列表可截断', async () => {
|
|
256
|
+
resetLinkCache()
|
|
257
|
+
const files = { 'T.md': 'target' }
|
|
258
|
+
for (let i = 0; i < 5; i += 1) files[`p${i}.md`] = '见 `T.md`'
|
|
259
|
+
const g = await buildLinkGraph('/fake-9', fakeIo(files))
|
|
260
|
+
const v = linksOf(g, 'T.md', 2)
|
|
261
|
+
assert.equal(v.incoming.length, 2)
|
|
262
|
+
assert.equal(v.incomingTotal, 5, '计数必须是全量,不能是截断后的长度')
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
test('linksOf:limited 透传(扫描被上限截断时面板要能提示「结果不完整」)', async () => {
|
|
266
|
+
resetLinkCache()
|
|
267
|
+
const io = fakeIo({ 'A.md': 'x' })
|
|
268
|
+
io.list = async () => ({ docs: [{ rel: 'A.md', title: 'A', mtimeMs: 1 }], truncated: true })
|
|
269
|
+
const g = await buildLinkGraph('/fake-10', io)
|
|
270
|
+
assert.equal(linksOf(g, 'A.md').limited, true)
|
|
271
|
+
})
|