dsh-knit 0.12.0 → 0.13.0
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 +276 -0
- package/README.en.md +5 -3
- package/README.md +7 -4
- package/package.json +1 -1
- package/src/client/client.js +380 -147
- package/test/client.test.mjs +267 -93
- package/test/harness.mjs +4 -2
- package/test/i18n.test.mjs +123 -3
- package/test/tier1.test.mjs +97 -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 () => {
|
package/test/tier1.test.mjs
CHANGED
|
@@ -295,6 +295,103 @@ test('键盘:↑ 到顶停住,不循环', async () => {
|
|
|
295
295
|
assert.ok(String(byClass(nodes, 'knit-doc')[0].props.className).includes('cursor'), '↑ 到顶应停住')
|
|
296
296
|
})
|
|
297
297
|
|
|
298
|
+
/* ── 1.5 多列键盘导航(P0 修复的守卫) ───────────────
|
|
299
|
+
真实列数要靠 ResizeObserver 量,测试环境量不到 → 永远是 1 列。
|
|
300
|
+
所以这里**直接测纯函数**,让多列分支真的被跑到(否则又是一盏空转的绿灯)。 */
|
|
301
|
+
|
|
302
|
+
test('键盘:nextIndexFor —— 多列时 ←→ 跨行,且到行首/行尾停住不回绕', () => {
|
|
303
|
+
const { nextIndexFor } = loadClientModule().exports.__test
|
|
304
|
+
const at = (i, key, cols) => nextIndexFor(i, key, 5, cols)
|
|
305
|
+
|
|
306
|
+
// 单列:只认 ↑↓(←→ 不该悄悄改变行为,返回 -1 表示「这个键我不处理」)
|
|
307
|
+
assert.equal(at(0, 'ArrowDown', 1), 1)
|
|
308
|
+
assert.equal(at(0, 'ArrowUp', 1), 0, '↑ 到顶停住')
|
|
309
|
+
assert.equal(at(4, 'ArrowDown', 1), 4, '↓ 到底停住')
|
|
310
|
+
assert.equal(at(2, 'ArrowLeft', 1), -1, '单列不认 ←(避免挡掉宿主/输入框)')
|
|
311
|
+
assert.equal(at(2, 'ArrowRight', 1), -1, '单列不认 →')
|
|
312
|
+
assert.equal(at(2, 'a', 1), -1, '无关的键一律不处理')
|
|
313
|
+
|
|
314
|
+
// 2 列:0 1 / 2 3 / 4。←→ 只在本行内移动
|
|
315
|
+
assert.equal(at(0, 'ArrowRight', 2), 1, '0 → 1')
|
|
316
|
+
assert.equal(at(1, 'ArrowRight', 2), 1, '行尾再按 → 停住(不回绕到下一行)')
|
|
317
|
+
assert.equal(at(1, 'ArrowLeft', 2), 0, '1 → 0')
|
|
318
|
+
assert.equal(at(0, 'ArrowLeft', 2), 0, '行首再按 ← 停住(不跳到上一行末尾)')
|
|
319
|
+
assert.equal(at(2, 'ArrowRight', 2), 3)
|
|
320
|
+
assert.equal(at(3, 'ArrowRight', 2), 3, '末行不满时 → 停在最后一个')
|
|
321
|
+
assert.equal(at(4, 'ArrowRight', 2), 4, '末行只有一项')
|
|
322
|
+
assert.equal(at(4, 'ArrowLeft', 2), 4, '末行只有一项时 ← 也停住')
|
|
323
|
+
// ↑↓ 是「相邻项」= 视觉上横向走(DOM 顺序即阅读顺序)
|
|
324
|
+
assert.equal(at(0, 'ArrowDown', 2), 1, '2 列时 ↓ 视觉上是往右走')
|
|
325
|
+
assert.equal(at(1, 'ArrowDown', 2), 2, '再 ↓ 落到下一行行首')
|
|
326
|
+
|
|
327
|
+
// Home / End 与列数无关
|
|
328
|
+
assert.equal(at(3, 'Home', 2), 0)
|
|
329
|
+
assert.equal(at(1, 'End', 2), 4)
|
|
330
|
+
|
|
331
|
+
// 没有光标(index < 0)时按第 0 项算;空列表返回 -1
|
|
332
|
+
assert.equal(nextIndexFor(-1, 'ArrowDown', 5, 2), 1)
|
|
333
|
+
assert.equal(nextIndexFor(-1, 'ArrowUp', 5, 2), 0)
|
|
334
|
+
assert.equal(nextIndexFor(0, 'ArrowDown', 0, 2), -1, '空列表不处理')
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
test('可访问性(listbox):容器指向 activedescendant,选项带 role/aria-selected', async () => {
|
|
338
|
+
const docCalls = []
|
|
339
|
+
installFetch((url) => {
|
|
340
|
+
if (url.includes('/api/doc')) {
|
|
341
|
+
docCalls.push(decodeURIComponent(url.split('rel=')[1]))
|
|
342
|
+
return { ok: true, rel: 'x', title: 'x', text: '# x', truncated: false }
|
|
343
|
+
}
|
|
344
|
+
return listPayload()
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
const { KnitBody } = loadClientModule().exports.__test
|
|
348
|
+
let { nodes, renderAgain } = await mount(KnitBody, { sessionId: 's1' }, ['', 'time'])
|
|
349
|
+
nodes = await renderAgain(true)
|
|
350
|
+
|
|
351
|
+
const list = listNode(nodes)
|
|
352
|
+
assert.equal(list.props.role, 'listbox')
|
|
353
|
+
const active = list.props['aria-activedescendant']
|
|
354
|
+
assert.ok(active, '容器必须给出 aria-activedescendant')
|
|
355
|
+
|
|
356
|
+
const docs = byClass(nodes, 'knit-doc')
|
|
357
|
+
assert.equal(docs.length, 3)
|
|
358
|
+
for (const doc of docs) {
|
|
359
|
+
assert.equal(doc.props.role, 'option', '每个选项都要 role="option"(否则读不出「N 项中的第 i 项」)')
|
|
360
|
+
assert.ok(doc.props.id, '每个选项要有 id,才能被 aria-activedescendant 指到')
|
|
361
|
+
}
|
|
362
|
+
// 指向的 id 必须真的在列表里(不然辅助技术指到一个不存在的节点)
|
|
363
|
+
const ids = docs.map((d) => d.props.id)
|
|
364
|
+
assert.ok(ids.includes(active), `aria-activedescendant=${active} 必须是真实存在的选项 id`)
|
|
365
|
+
// 光标项 = aria-activedescendant 指向的那一项
|
|
366
|
+
const cursorNode = docs.find((d) => String(d.props.className).includes('cursor'))
|
|
367
|
+
assert.ok(cursorNode, '初次渲染 cursor 应落在第一项')
|
|
368
|
+
assert.equal(cursorNode.props.id, active, 'activedescendant 必须指向光标那一项')
|
|
369
|
+
|
|
370
|
+
// aria-selected 表达的是**正在预览**(不是光标):没预览时全为 false
|
|
371
|
+
assert.equal(docs.filter((d) => d.props['aria-selected'] === 'true').length, 0,
|
|
372
|
+
'没打开预览时不该有 aria-selected=true')
|
|
373
|
+
|
|
374
|
+
// 按 Enter 打开预览 → 那一项变成 aria-selected=true,且仍然只有一项
|
|
375
|
+
list.props.onKeyDown(keyEvent('Enter'))
|
|
376
|
+
nodes = await renderAgain(true)
|
|
377
|
+
const after = byClass(nodes, 'knit-doc')
|
|
378
|
+
const selected = after.filter((d) => d.props['aria-selected'] === 'true')
|
|
379
|
+
assert.equal(selected.length, 1, '打开预览后恰好一项 aria-selected=true')
|
|
380
|
+
assert.equal(selected[0].props.id, cursorNode.props.id, '选中的就是当前那一项')
|
|
381
|
+
})
|
|
382
|
+
|
|
383
|
+
test('可访问性:选项 id 由 rel 稳定推导(同一篇跨渲染不变,中文/空格也合法)', () => {
|
|
384
|
+
const { docOptionId } = loadClientModule().exports.__test
|
|
385
|
+
const a = { rel: '01_ Knit PRD/Knit_SDD-v0.12-链接解析.md' }
|
|
386
|
+
assert.equal(docOptionId(a), docOptionId(a), '同一篇必须每次都得到同一个 id')
|
|
387
|
+
assert.notEqual(docOptionId(a), docOptionId({ rel: '01_ Knit PRD/别的.md' }))
|
|
388
|
+
assert.match(docOptionId(a), /^[A-Za-z0-9_-]+$/, 'id 只能是合法字符(会进 HTML 属性与选择器)')
|
|
389
|
+
assert.ok(docOptionId(a).length <= 120, 'id 不该过长')
|
|
390
|
+
// 折叠后可能撞车的两篇,靠哈希区分
|
|
391
|
+
assert.notEqual(docOptionId({ rel: 'a b/c.md' }), docOptionId({ rel: 'a_b/c.md' }),
|
|
392
|
+
'折叠字符相同的两篇不能拿到同一个 id')
|
|
393
|
+
})
|
|
394
|
+
|
|
298
395
|
test('键盘:Esc 收起预览', async () => {
|
|
299
396
|
installFetch((url) => (url.includes('/api/doc')
|
|
300
397
|
? { ok: true, rel: 'x', title: 'x', text: '# x', truncated: false }
|