dsh-knit 0.4.0 → 0.5.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/README.en.md +18 -7
- package/README.md +16 -7
- package/package.json +9 -4
- package/src/client/client.js +793 -53
- package/src/host/index.js +232 -47
- package/test/client.test.mjs +491 -1
- package/test/fixture.mjs +48 -0
- package/test/harness.mjs +30 -6
- package/test/host-http.test.mjs +119 -0
- package/test/host.test.mjs +141 -3
- package/test/path.test.mjs +208 -0
- package/test/peek.test.mjs +338 -0
package/test/client.test.mjs
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import test from 'node:test'
|
|
7
7
|
import assert from 'node:assert/strict'
|
|
8
8
|
|
|
9
|
-
import { loadClientModule, createHarness, byClass, textOf, makeLocale } from './harness.mjs'
|
|
9
|
+
import { loadClientModule, createHarness, byClass, byExactClass, textOf, makeLocale } from './harness.mjs'
|
|
10
10
|
|
|
11
11
|
const harness = createHarness()
|
|
12
12
|
const React = globalThis.__knitReact
|
|
@@ -498,3 +498,493 @@ test('渲染:宿主返回失败时把错误码翻成当前语言的文案', as
|
|
|
498
498
|
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
499
499
|
assert.match(byClass(nodes, 'knit-msg').map(textOf).join(''), /路径越出工作区/)
|
|
500
500
|
})
|
|
501
|
+
|
|
502
|
+
/* ── 图片与视频 ─────────────────────────────────────── */
|
|
503
|
+
|
|
504
|
+
/** 造一份媒体载荷(1 图 + 1 视频,图片在前)。 */
|
|
505
|
+
function mediaPayload(overrides = {}) {
|
|
506
|
+
const now = Date.now()
|
|
507
|
+
return {
|
|
508
|
+
ok: true, root: '/p', total: 2, mode: 'time', topic: '', kind: 'media',
|
|
509
|
+
docs: [
|
|
510
|
+
{ path: '/p/a.png', rel: 'a.png', name: 'a.png', title: 'a', kind: 'image', summary: '', size: 1024, mtimeMs: now - 60000, score: null },
|
|
511
|
+
{ path: '/p/demo.mp4', rel: 'demo.mp4', name: 'demo.mp4', title: 'demo', kind: 'video', summary: '', size: 99999, mtimeMs: now - 9e7, score: null },
|
|
512
|
+
],
|
|
513
|
+
...overrides,
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** 造一份文档 + 图片 + 视频混排载荷(全部视图)。 */
|
|
518
|
+
function mixedPayload(overrides = {}) {
|
|
519
|
+
const now = Date.now()
|
|
520
|
+
return {
|
|
521
|
+
ok: true, root: '/p', total: 3, mode: 'time', topic: '', kind: 'all',
|
|
522
|
+
docs: [
|
|
523
|
+
{ path: '/p/方案.md', rel: '方案.md', name: '方案.md', title: '方案', kind: 'md', summary: '文档摘要', size: 500, mtimeMs: now - 30000, score: null },
|
|
524
|
+
{ path: '/p/a.png', rel: 'a.png', name: 'a.png', title: 'a', kind: 'image', summary: '', size: 1024, mtimeMs: now - 60000, score: null },
|
|
525
|
+
{ path: '/p/demo.mp4', rel: 'demo.mp4', name: 'demo.mp4', title: 'demo', kind: 'video', summary: '', size: 99999, mtimeMs: now - 9e7, score: null },
|
|
526
|
+
],
|
|
527
|
+
...overrides,
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/** 按请求里的 kind 参数返回对应载荷。 */
|
|
532
|
+
function kindResponder(map) {
|
|
533
|
+
return (url) => {
|
|
534
|
+
if (url.includes('/api/doc')) {
|
|
535
|
+
return { ok: true, rel: 'x.md', title: 'x', text: '正文', truncated: false }
|
|
536
|
+
}
|
|
537
|
+
const m = url.match(/[?&]kind=([a-z]+)/)
|
|
538
|
+
return (map && map[m ? m[1] : 'doc']) || listPayload()
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/** 造一份超出「全部」两个分区上限的载荷:7 篇文档 + 5 图 + 4 视频 = 16 项。 */
|
|
543
|
+
function bigMixedPayload() {
|
|
544
|
+
const now = Date.now()
|
|
545
|
+
const docs = []
|
|
546
|
+
for (let i = 1; i <= 7; i += 1) {
|
|
547
|
+
docs.push({
|
|
548
|
+
path: `/p/d${i}.md`, rel: `d${i}.md`, name: `d${i}.md`, title: `文档${i}`,
|
|
549
|
+
kind: 'md', summary: '摘要', size: 500, mtimeMs: now - i * 1000, score: null,
|
|
550
|
+
})
|
|
551
|
+
}
|
|
552
|
+
for (let i = 1; i <= 5; i += 1) {
|
|
553
|
+
docs.push({
|
|
554
|
+
path: `/p/i${i}.png`, rel: `i${i}.png`, name: `i${i}.png`, title: `图${i}`,
|
|
555
|
+
kind: 'image', summary: '', size: 1024, mtimeMs: now - i * 1000, score: null,
|
|
556
|
+
})
|
|
557
|
+
}
|
|
558
|
+
for (let i = 1; i <= 4; i += 1) {
|
|
559
|
+
docs.push({
|
|
560
|
+
path: `/p/v${i}.mp4`, rel: `v${i}.mp4`, name: `v${i}.mp4`, title: `视频${i}`,
|
|
561
|
+
kind: 'video', summary: '', size: 99999, mtimeMs: now - i * 1000, score: null,
|
|
562
|
+
})
|
|
563
|
+
}
|
|
564
|
+
return {
|
|
565
|
+
ok: true, root: '/p', total: docs.length, mode: 'time', topic: '', kind: 'all', docs,
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* 按 class **词**取节点。
|
|
571
|
+
*
|
|
572
|
+
* 一个节点的 className 常同时带 active / cursor / fresh,`byExactClass` 的整段相等
|
|
573
|
+
* 匹配用不了,`byClass` 的子串匹配又会误伤 —— 计数类断言用这个。
|
|
574
|
+
*
|
|
575
|
+
* @param {object[]} nodes - 扁平节点
|
|
576
|
+
* @param {string} token - 单个 class 名
|
|
577
|
+
* @returns {object[]} 命中的节点
|
|
578
|
+
*/
|
|
579
|
+
function byToken(nodes, token) {
|
|
580
|
+
return nodes.filter((n) => String(n.props.className || '').split(/\s+/).includes(token))
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
test('媒体:fmtDuration 格式化 m:ss / h:mm:ss,非法值回落', () => {
|
|
584
|
+
const { fmtDuration } = loadClientModule().exports.__test
|
|
585
|
+
assert.equal(fmtDuration(0), '0:00')
|
|
586
|
+
assert.equal(fmtDuration(5), '0:05')
|
|
587
|
+
assert.equal(fmtDuration(65), '1:05')
|
|
588
|
+
assert.equal(fmtDuration(3725), '1:02:05')
|
|
589
|
+
assert.equal(fmtDuration(NaN), '0:00')
|
|
590
|
+
})
|
|
591
|
+
|
|
592
|
+
test('媒体:mediaUrl 拼同源 raw 地址并编码相对路径', () => {
|
|
593
|
+
const { mediaUrl } = loadClientModule().exports.__test
|
|
594
|
+
const u = mediaUrl('s1', 'sub/a b.png')
|
|
595
|
+
assert.match(u, /^\/knit\/api\/raw\?/)
|
|
596
|
+
assert.ok(u.includes('sessionId=s1'))
|
|
597
|
+
assert.ok(u.includes('rel=sub%2Fa%20b.png'), `实际: ${u}`)
|
|
598
|
+
})
|
|
599
|
+
|
|
600
|
+
test('媒体:isMedia 只认 image/video', () => {
|
|
601
|
+
const { isMedia } = loadClientModule().exports.__test
|
|
602
|
+
assert.equal(isMedia({ kind: 'image' }), true)
|
|
603
|
+
assert.equal(isMedia({ kind: 'video' }), true)
|
|
604
|
+
assert.equal(isMedia({ kind: 'md' }), false)
|
|
605
|
+
assert.equal(isMedia(null), false)
|
|
606
|
+
})
|
|
607
|
+
|
|
608
|
+
test('媒体:类型偏好默认 doc,合法值保留,非法值回落', () => {
|
|
609
|
+
const { readKindPref } = loadClientModule().exports.__test
|
|
610
|
+
assert.equal(readKindPref(), 'doc')
|
|
611
|
+
globalThis.window.localStorage.setItem('dsh-knit:kind', 'media')
|
|
612
|
+
assert.equal(readKindPref(), 'media')
|
|
613
|
+
globalThis.window.localStorage.setItem('dsh-knit:kind', 'all')
|
|
614
|
+
assert.equal(readKindPref(), 'all')
|
|
615
|
+
globalThis.window.localStorage.setItem('dsh-knit:kind', 'garbage')
|
|
616
|
+
assert.equal(readKindPref(), 'doc')
|
|
617
|
+
})
|
|
618
|
+
|
|
619
|
+
test('媒体:默认文档视图有三个类型按钮,但不出现媒体网格,计数仍按文档', async () => {
|
|
620
|
+
installFetch(kindResponder({ doc: listPayload() }))
|
|
621
|
+
const { KnitBody } = loadClientModule().exports.__test
|
|
622
|
+
harness.reset()
|
|
623
|
+
harness.seed(['', 'time'])
|
|
624
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
625
|
+
await harness.flush()
|
|
626
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
627
|
+
|
|
628
|
+
const btns = byClass(nodes, 'knit-type-btn')
|
|
629
|
+
assert.equal(btns.length, 3)
|
|
630
|
+
assert.deepEqual(btns.map(textOf), ['文档', '图片与视频', '全部'])
|
|
631
|
+
assert.equal(btns.find((b) => textOf(b) === '文档').props['aria-selected'], true)
|
|
632
|
+
assert.equal(byClass(nodes, 'knit-media-grid').length, 0)
|
|
633
|
+
assert.equal(byClass(nodes, 'knit-doc').length, 2)
|
|
634
|
+
assert.match(byClass(nodes, 'knit-count').map(textOf).join(''), /2 篇/)
|
|
635
|
+
})
|
|
636
|
+
|
|
637
|
+
test('媒体:媒体视图渲染方形网格,图片出 img、视频出首帧 video 与播放三角', async () => {
|
|
638
|
+
const { calls } = installFetch(kindResponder({ media: mediaPayload() }))
|
|
639
|
+
const { KnitBody } = loadClientModule().exports.__test
|
|
640
|
+
harness.reset()
|
|
641
|
+
harness.seed(['', 'time', 'media'])
|
|
642
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
643
|
+
await harness.flush()
|
|
644
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
645
|
+
|
|
646
|
+
assert.ok(calls[0].includes('kind=media'), `实际 URL: ${calls[0]}`)
|
|
647
|
+
assert.equal(byClass(nodes, 'knit-media-grid').length, 1)
|
|
648
|
+
assert.equal(byClass(nodes, 'knit-media-card').length, 2)
|
|
649
|
+
assert.equal(byClass(nodes, 'knit-doc').length, 0, '媒体视图不该有文档行')
|
|
650
|
+
assert.equal(nodes.filter((n) => n.type === 'img').length, 1, '一张图片缩略图')
|
|
651
|
+
const videos = nodes.filter((n) => n.type === 'video')
|
|
652
|
+
assert.equal(videos.length, 1, '一个视频首帧')
|
|
653
|
+
assert.equal(videos[0].props.preload, 'metadata')
|
|
654
|
+
assert.ok(String(videos[0].props.src).includes('#t=0.5'), '用 #t=0.5 取首帧')
|
|
655
|
+
assert.equal(byClass(nodes, 'knit-thumb-play').length, 1, '视频有中央播放三角')
|
|
656
|
+
assert.match(byClass(nodes, 'knit-count').map(textOf).join(''), /2 个媒体/)
|
|
657
|
+
})
|
|
658
|
+
|
|
659
|
+
test('媒体:点图片卡片就地预览大图,且不请求 /api/doc', async () => {
|
|
660
|
+
const { calls } = installFetch(kindResponder({ media: mediaPayload() }))
|
|
661
|
+
const { KnitBody } = loadClientModule().exports.__test
|
|
662
|
+
harness.reset()
|
|
663
|
+
harness.seed(['', 'time', 'media'])
|
|
664
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
665
|
+
await harness.flush()
|
|
666
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
667
|
+
|
|
668
|
+
byClass(nodes, 'knit-media-card')[0].props.onClick() // 第一张是图片
|
|
669
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
670
|
+
await harness.flush()
|
|
671
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
672
|
+
|
|
673
|
+
const pm = byClass(nodes, 'knit-preview-media')
|
|
674
|
+
assert.equal(pm.length, 1)
|
|
675
|
+
assert.equal(pm[0].type, 'img')
|
|
676
|
+
assert.ok(String(pm[0].props.src).includes('/knit/api/raw'))
|
|
677
|
+
assert.ok(!calls.some((u) => u.includes('/api/doc')), '图片不应触发正文请求')
|
|
678
|
+
})
|
|
679
|
+
|
|
680
|
+
test('媒体:点视频卡片就地预览,播放器 controls 且静音自动播放', async () => {
|
|
681
|
+
const { calls } = installFetch(kindResponder({ media: mediaPayload() }))
|
|
682
|
+
const { KnitBody } = loadClientModule().exports.__test
|
|
683
|
+
harness.reset()
|
|
684
|
+
harness.seed(['', 'time', 'media'])
|
|
685
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
686
|
+
await harness.flush()
|
|
687
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
688
|
+
|
|
689
|
+
byClass(nodes, 'knit-media-card')[1].props.onClick() // 第二张是视频
|
|
690
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
691
|
+
await harness.flush()
|
|
692
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
693
|
+
|
|
694
|
+
const pm = byClass(nodes, 'knit-preview-media')
|
|
695
|
+
assert.equal(pm.length, 1)
|
|
696
|
+
assert.equal(pm[0].type, 'video')
|
|
697
|
+
assert.equal(pm[0].props.controls, true)
|
|
698
|
+
assert.equal(pm[0].props.autoPlay, true)
|
|
699
|
+
assert.equal(pm[0].props.muted, true, '自动播放必须配静音以满足浏览器策略')
|
|
700
|
+
assert.equal(pm[0].props.playsInline, true)
|
|
701
|
+
assert.ok(!calls.some((u) => u.includes('/api/doc')), '视频不应触发正文请求')
|
|
702
|
+
})
|
|
703
|
+
|
|
704
|
+
test('媒体:「全部」分上下两区,文档上限 4、媒体不截断(超 8 个才等比缩小)', async () => {
|
|
705
|
+
installFetch(kindResponder({ all: bigMixedPayload() }))
|
|
706
|
+
const { KnitBody } = loadClientModule().exports.__test
|
|
707
|
+
harness.reset()
|
|
708
|
+
harness.seed(['', 'time', 'all'])
|
|
709
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
710
|
+
await harness.flush()
|
|
711
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
712
|
+
|
|
713
|
+
assert.equal(byExactClass(nodes, 'knit-section').length, 2, '文档区 + 媒体区,上下两区')
|
|
714
|
+
assert.deepEqual(
|
|
715
|
+
byExactClass(nodes, 'knit-section-title').map(textOf),
|
|
716
|
+
['文档', '图片与视频'],
|
|
717
|
+
)
|
|
718
|
+
assert.equal(byToken(nodes, 'knit-doc').length, 4, '文档区最多 4 条')
|
|
719
|
+
// 载荷里 5 图 + 4 视频 = 9 个媒体;一屏基准是 8,超出的那个不隐藏,改成整块等比缩小
|
|
720
|
+
assert.equal(byToken(nodes, 'knit-media-card').length, 9, '媒体区不截断,9 个全出')
|
|
721
|
+
assert.equal(byExactClass(nodes, 'knit-media-grid in-section').length, 1, '媒体区用分区网格')
|
|
722
|
+
assert.match(byClass(nodes, 'knit-count').map(textOf).join(''), /16 项/, '顶行仍报总数')
|
|
723
|
+
|
|
724
|
+
const grid = byExactClass(nodes, 'knit-media-grid in-section')[0]
|
|
725
|
+
// 测试环境没有 ResizeObserver → 量不到宽度 → 走默认面板几何(632px)
|
|
726
|
+
assert.equal(grid.props.style['--knit-media-cell'], '118px', '默认面板下 9 个媒体 → 5 列 118px')
|
|
727
|
+
assert.equal(grid.props.style['--knit-media-label'], 'none', '超 8 个 → 卡片文字让位给缩略图')
|
|
728
|
+
assert.equal(grid.props.style.maxHeight, '246px', '两行 + 一个 gap,超出的交给滚动')
|
|
729
|
+
})
|
|
730
|
+
|
|
731
|
+
test('媒体:不超过 8 个时卡片正常带文件名与时间,不进入等比缩小态', async () => {
|
|
732
|
+
// 1 篇文档 + 5 媒体(正好一屏内)
|
|
733
|
+
const docs = bigMixedPayload().docs.slice(0, 1).concat(
|
|
734
|
+
bigMixedPayload().docs.filter((d) => d.kind !== 'md').slice(0, 5))
|
|
735
|
+
installFetch(kindResponder({ all: mixedPayload({ total: docs.length, docs }) }))
|
|
736
|
+
const { KnitBody } = loadClientModule().exports.__test
|
|
737
|
+
harness.reset()
|
|
738
|
+
harness.seed(['', 'time', 'all'])
|
|
739
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
740
|
+
await harness.flush()
|
|
741
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
742
|
+
|
|
743
|
+
const grid = byExactClass(nodes, 'knit-media-grid in-section')[0]
|
|
744
|
+
assert.equal(grid.props.style['--knit-media-label'], undefined, '不缩小就保留文件名/时间')
|
|
745
|
+
assert.equal(byExactClass(nodes, 'knit-media-meta').length, 5, '5 个媒体都有文字区')
|
|
746
|
+
})
|
|
747
|
+
|
|
748
|
+
test('媒体:「全部」的文档区被截断时给「已显示 / 总数」与「查看全部」,媒体区不截断', async () => {
|
|
749
|
+
installFetch(kindResponder({ all: bigMixedPayload() }))
|
|
750
|
+
const { KnitBody } = loadClientModule().exports.__test
|
|
751
|
+
harness.reset()
|
|
752
|
+
harness.seed(['', 'time', 'all'])
|
|
753
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
754
|
+
await harness.flush()
|
|
755
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
756
|
+
|
|
757
|
+
// 7 篇文档里只显示 4 条;媒体 9 个全显示,所以媒体区没有「已显示 / 总数」
|
|
758
|
+
assert.deepEqual(byExactClass(nodes, 'knit-section-count').map(textOf), ['4 / 7', '9'])
|
|
759
|
+
assert.equal(byExactClass(nodes, 'knit-section-more').length, 1, '只有文档区被截断')
|
|
760
|
+
})
|
|
761
|
+
|
|
762
|
+
test('媒体:「全部」没被截断时不出现「查看全部」,空的一区整个不渲染', async () => {
|
|
763
|
+
// 1 篇文档 + 1 图:都低于上限
|
|
764
|
+
installFetch(kindResponder({ all: mixedPayload({ total: 2, docs: mixedPayload().docs.slice(0, 2) }) }))
|
|
765
|
+
const { KnitBody } = loadClientModule().exports.__test
|
|
766
|
+
harness.reset()
|
|
767
|
+
harness.seed(['', 'time', 'all'])
|
|
768
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
769
|
+
await harness.flush()
|
|
770
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
771
|
+
|
|
772
|
+
assert.equal(byExactClass(nodes, 'knit-section').length, 2)
|
|
773
|
+
assert.equal(byExactClass(nodes, 'knit-section-more').length, 0, '没截断就没有「查看全部」')
|
|
774
|
+
assert.deepEqual(byExactClass(nodes, 'knit-section-count').map(textOf), ['1 / 1', '1'])
|
|
775
|
+
})
|
|
776
|
+
|
|
777
|
+
test('媒体:只有文档时「全部」只出一区,不留空标题', async () => {
|
|
778
|
+
installFetch(kindResponder({ all: mixedPayload({ total: 1, docs: mixedPayload().docs.slice(0, 1) }) }))
|
|
779
|
+
const { KnitBody } = loadClientModule().exports.__test
|
|
780
|
+
harness.reset()
|
|
781
|
+
harness.seed(['', 'time', 'all'])
|
|
782
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
783
|
+
await harness.flush()
|
|
784
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
785
|
+
|
|
786
|
+
assert.equal(byExactClass(nodes, 'knit-section').length, 1)
|
|
787
|
+
assert.deepEqual(byExactClass(nodes, 'knit-section-title').map(textOf), ['文档'])
|
|
788
|
+
assert.equal(byExactClass(nodes, 'knit-media-grid in-section').length, 0)
|
|
789
|
+
})
|
|
790
|
+
|
|
791
|
+
test('媒体:「全部」文档区的「查看全部」点了切到文档分类', async () => {
|
|
792
|
+
const { calls } = installFetch(kindResponder({
|
|
793
|
+
all: bigMixedPayload(), doc: listPayload(), media: mediaPayload(),
|
|
794
|
+
}))
|
|
795
|
+
const { KnitBody } = loadClientModule().exports.__test
|
|
796
|
+
harness.reset()
|
|
797
|
+
harness.seed(['', 'time', 'all'])
|
|
798
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
799
|
+
await harness.flush()
|
|
800
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
801
|
+
|
|
802
|
+
// 媒体区不截断,所以「全部」里只剩文档区那一个「查看全部」
|
|
803
|
+
byExactClass(nodes, 'knit-section-more')[0].props.onClick()
|
|
804
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
805
|
+
await harness.flush()
|
|
806
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
807
|
+
|
|
808
|
+
assert.ok(calls.some((u) => u.includes('kind=doc')), `实际: ${calls.join(', ')}`)
|
|
809
|
+
assert.equal(globalThis.window.localStorage.getItem('dsh-knit:kind'), 'doc')
|
|
810
|
+
assert.equal(byExactClass(nodes, 'knit-section').length, 0, '已离开「全部」,不再有分区')
|
|
811
|
+
assert.equal(byExactClass(nodes, 'knit-media-grid').length, 0, '文档视图里没有媒体网格')
|
|
812
|
+
})
|
|
813
|
+
|
|
814
|
+
test('媒体:只有媒体时「全部」的媒体区不截断、也不给「查看全部」', async () => {
|
|
815
|
+
const now = Date.now()
|
|
816
|
+
const docs = Array.from({ length: 9 }, (_, i) => ({
|
|
817
|
+
path: `/p/i${i}.png`, rel: `i${i}.png`, name: `i${i}.png`, title: `图${i}`,
|
|
818
|
+
kind: 'image', summary: '', size: 1024, mtimeMs: now - i * 1000, score: null,
|
|
819
|
+
}))
|
|
820
|
+
installFetch(kindResponder({ all: mixedPayload({ total: docs.length, docs }) }))
|
|
821
|
+
const { KnitBody } = loadClientModule().exports.__test
|
|
822
|
+
harness.reset()
|
|
823
|
+
harness.seed(['', 'time', 'all'])
|
|
824
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
825
|
+
await harness.flush()
|
|
826
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
827
|
+
|
|
828
|
+
assert.equal(byExactClass(nodes, 'knit-section').length, 1, '只有媒体区')
|
|
829
|
+
assert.deepEqual(byExactClass(nodes, 'knit-section-title').map(textOf), ['图片与视频'])
|
|
830
|
+
assert.equal(byExactClass(nodes, 'knit-section-more').length, 0, '媒体区不截断,没有「查看全部」')
|
|
831
|
+
assert.equal(byToken(nodes, 'knit-media-card').length, 9)
|
|
832
|
+
})
|
|
833
|
+
|
|
834
|
+
test('媒体:「全部」的键盘导航只走到分区上限内,不落到被截断的条目', async () => {
|
|
835
|
+
installFetch(kindResponder({ all: bigMixedPayload() }))
|
|
836
|
+
const { KnitBody } = loadClientModule().exports.__test
|
|
837
|
+
harness.reset()
|
|
838
|
+
harness.seed(['', 'time', 'all'])
|
|
839
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
840
|
+
await harness.flush()
|
|
841
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
842
|
+
|
|
843
|
+
const list = byExactClass(nodes, 'knit-list')[0]
|
|
844
|
+
// 上限内共 4 + 6 = 10 条;连按 20 次也不能走到第 11 条
|
|
845
|
+
for (let i = 0; i < 20; i += 1) {
|
|
846
|
+
list.props.onKeyDown({ key: 'ArrowDown', preventDefault() {} })
|
|
847
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
848
|
+
await harness.flush()
|
|
849
|
+
}
|
|
850
|
+
const withCursor = byToken(nodes, 'cursor')
|
|
851
|
+
assert.equal(withCursor.length, 1, '任何时候只有一个条目是 cursor')
|
|
852
|
+
const reached = String(withCursor[0].props['data-knit-rel'] || '')
|
|
853
|
+
assert.ok(!/^d[5-7]\.md$/.test(reached), `不该落到被截断的文档:${reached}`)
|
|
854
|
+
})
|
|
855
|
+
|
|
856
|
+
test('媒体:mediaLayoutFor —— 最少 3 列、一屏 8 个、超过就等比缩小', () => {
|
|
857
|
+
const { mediaLayoutFor, MEDIA_MIN_PX, MEDIA_MAX_ITEMS } = loadClientModule().exports.__test
|
|
858
|
+
|
|
859
|
+
assert.equal(MEDIA_MIN_PX, 64, '格子下限是用户定的 64×64')
|
|
860
|
+
assert.equal(MEDIA_MAX_ITEMS, 8, '一屏基准 8 个(4 列 × 2 行)')
|
|
861
|
+
|
|
862
|
+
// 一开始的 bug 是「量不到宽度就回落 3 列」,于是默认面板下格子被撑到 200px+
|
|
863
|
+
assert.deepEqual(mediaLayoutFor(0, 4), { columns: 4, cell: 150, scaled: false, maxHeight: null },
|
|
864
|
+
'量不到宽度按默认面板算,不是回落 3 列')
|
|
865
|
+
|
|
866
|
+
// 不超一屏:宽度够就固定 4 列满宽;媒体少于 4 个也不缩列,免得一个缩略图独占半屏
|
|
867
|
+
assert.deepEqual(mediaLayoutFor(632, 1), { columns: 4, cell: 150, scaled: false, maxHeight: null },
|
|
868
|
+
'1 个也占 4 列网格里的一格(旧的 3 列回落会让它撑到 204px)')
|
|
869
|
+
assert.equal(mediaLayoutFor(632, 3).columns, 4)
|
|
870
|
+
assert.equal(mediaLayoutFor(632, 4).columns, 4)
|
|
871
|
+
assert.equal(mediaLayoutFor(632, 4).cell, 150, '默认右侧栏 4 列时约 150px,远小于旧的 200px+')
|
|
872
|
+
assert.equal(mediaLayoutFor(632, 8).columns, 4, '8 个仍是 4 列 × 2 行')
|
|
873
|
+
|
|
874
|
+
// 超过一屏:不再隐藏,而是加列 + 缩小,两行装下
|
|
875
|
+
const nine = mediaLayoutFor(632, 9)
|
|
876
|
+
assert.equal(nine.columns, 5, '9 个 → 5 列 × 2 行')
|
|
877
|
+
assert.ok(nine.cell < mediaLayoutFor(632, 8).cell, '格子必须比 8 个时更小')
|
|
878
|
+
assert.equal(nine.scaled, true, '进入等比缩小态(卡片文字让位)')
|
|
879
|
+
assert.equal(nine.maxHeight, nine.cell * 2 + 10, '两行 + 一个 gap')
|
|
880
|
+
|
|
881
|
+
// 面板再窄也不掉到 1~2 列;够宽时网格绝不横向溢出
|
|
882
|
+
for (const width of [240, 280, 375, 500, 900, 1400]) {
|
|
883
|
+
for (const count of [1, 3, 8, 9, 24]) {
|
|
884
|
+
const layout = mediaLayoutFor(width, count)
|
|
885
|
+
assert.ok(layout.columns >= 3,
|
|
886
|
+
`${width}px / ${count} 个:列数不该低于 3(实际 ${layout.columns})`)
|
|
887
|
+
assert.ok(layout.columns * layout.cell + (layout.columns - 1) * 10 <= width,
|
|
888
|
+
`${width}px / ${count} 个:网格不能横向溢出`)
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
// 极窄面板(连 3 个 64px 都放不下)时守住 3 列、允许轻微横溢 —— 用户明确要「最少 3 个」
|
|
892
|
+
const tiny = mediaLayoutFor(120, 8)
|
|
893
|
+
assert.equal(tiny.columns, 3, '极窄也保 3 列')
|
|
894
|
+
assert.equal(tiny.cell, 64, '格子不再继续缩,守住 64px 可读下限')
|
|
895
|
+
})
|
|
896
|
+
|
|
897
|
+
test('样式:类型切换的选中态不再用品牌色描边,与列表行同一套中性灰填充', async () => {
|
|
898
|
+
const { readFileSync } = await import('node:fs')
|
|
899
|
+
const { fileURLToPath } = await import('node:url')
|
|
900
|
+
const source = readFileSync(fileURLToPath(new URL('../src/client/client.js', import.meta.url)), 'utf8')
|
|
901
|
+
|
|
902
|
+
// 用户原话:「选中的时候不用加绿色、蓝色的描边,就跟下面列表一样,选中填充背景灰就可以」
|
|
903
|
+
const rule = source.match(/\.knit-type-btn\.active\{([^}]*)\}/)
|
|
904
|
+
assert.ok(rule, '必须还有 .knit-type-btn.active 这条规则')
|
|
905
|
+
assert.ok(!rule[1].includes('--knit-accent'), `选中态不许再出现品牌色:${rule[1]}`)
|
|
906
|
+
assert.match(rule[1], /background:var\(--dsw-alias-interactive-bg-active/,
|
|
907
|
+
'选中态用 DSH 的中性选中底色,与列表行同一套')
|
|
908
|
+
})
|
|
909
|
+
|
|
910
|
+
test('样式:媒体网格靠 CSS 变量收列数与格子边长,下限与 JS 常量同源', async () => {
|
|
911
|
+
const { readFileSync } = await import('node:fs')
|
|
912
|
+
const { fileURLToPath } = await import('node:url')
|
|
913
|
+
const source = readFileSync(fileURLToPath(new URL('../src/client/client.js', import.meta.url)), 'utf8')
|
|
914
|
+
|
|
915
|
+
assert.ok(!source.includes('grid-template-columns:1fr 1fr'), '不能再写死两列')
|
|
916
|
+
assert.ok(!source.includes('minmax(160px,1fr)'), '160px 下限已按用户要求改成 64px')
|
|
917
|
+
// 列数由 JS 算好传进来;前面的 repeat(3,1fr) 是旧内核的兜底,保证「最少 3 列」
|
|
918
|
+
assert.match(source, /grid-template-columns:repeat\(3,1fr\);\s*\n\s*grid-template-columns:repeat\(var\(--knit-media-cols/)
|
|
919
|
+
assert.match(source, /height:var\(--knit-media-cell/)
|
|
920
|
+
assert.match(source, /const MEDIA_MIN_PX = 64/)
|
|
921
|
+
assert.match(source, /const MEDIA_GAP_PX = 10/)
|
|
922
|
+
assert.match(source, /const MEDIA_MAX_ITEMS = 8/)
|
|
923
|
+
})
|
|
924
|
+
|
|
925
|
+
test('样式:交互强调色走 DSH 品牌令牌,源码里不再有硬编码的紫/蓝强调色', async () => {
|
|
926
|
+
const { readFileSync } = await import('node:fs')
|
|
927
|
+
const { fileURLToPath } = await import('node:url')
|
|
928
|
+
const source = readFileSync(fileURLToPath(new URL('../src/client/client.js', import.meta.url)), 'utf8')
|
|
929
|
+
|
|
930
|
+
assert.ok(!source.includes('rgba(124,108,240'), '紫色强调色必须清掉')
|
|
931
|
+
assert.ok(!source.includes('rgba(79,140,255'), '另一套硬编码蓝也要清掉')
|
|
932
|
+
assert.match(source, /--knit-accent:var\(--dsw-alias-brand-primary-new-colorprimary-new-color/)
|
|
933
|
+
// 品牌色只留给「正在预览」那一处表达;选中态一律走中性灰(见类型切换那条测试)
|
|
934
|
+
assert.match(source, /\.knit-media-card\.active \.knit-media-thumbbox\{\s*\n\s*border-color:var\(--knit-accent\)/)
|
|
935
|
+
})
|
|
936
|
+
|
|
937
|
+
test('样式:每个 --knit-* 引用都必须有定义或 fallback', async () => {
|
|
938
|
+
const { readFileSync } = await import('node:fs')
|
|
939
|
+
const { fileURLToPath } = await import('node:url')
|
|
940
|
+
const source = readFileSync(fileURLToPath(new URL('../src/client/client.js', import.meta.url)), 'utf8')
|
|
941
|
+
|
|
942
|
+
const start = source.indexOf('const CSS = `') + 'const CSS = `'.length
|
|
943
|
+
const css = source.slice(start, source.indexOf('`', start))
|
|
944
|
+
assert.ok(css.length > 1000, 'CSS 块没取到')
|
|
945
|
+
|
|
946
|
+
// 定义过的变量(`--knit-x:` 形式)
|
|
947
|
+
const defined = new Set([...css.matchAll(/(--knit-[a-z-]+)\s*:/g)].map((m) => m[1]))
|
|
948
|
+
|
|
949
|
+
// 引用时没写 fallback 的,必须自己定义过;
|
|
950
|
+
// 否则 var() 失效 → 整条声明作废(invalid at computed-value time),
|
|
951
|
+
// 界面会静默少一块样式 —— 曾经就这么丢过 `.knit-doc.active` 的背景填充。
|
|
952
|
+
const missing = []
|
|
953
|
+
for (const m of css.matchAll(/var\((--knit-[a-z-]+)\s*(,)?/g)) {
|
|
954
|
+
if (!m[2] && !defined.has(m[1])) missing.push(m[1])
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
assert.deepEqual([...new Set(missing)], [],
|
|
958
|
+
'这些变量没定义又没 fallback,会让整条声明失效')
|
|
959
|
+
assert.ok(defined.has('--knit-accent'), '品牌色变量必须定义在 .knit-root 上')
|
|
960
|
+
})
|
|
961
|
+
|
|
962
|
+
test('媒体:点类型按钮切到「图片与视频」会带 kind=media 重拉并记住偏好', async () => {
|
|
963
|
+
const { calls } = installFetch(kindResponder({ doc: listPayload(), media: mediaPayload() }))
|
|
964
|
+
const { KnitBody } = loadClientModule().exports.__test
|
|
965
|
+
harness.reset()
|
|
966
|
+
harness.seed(['', 'time'])
|
|
967
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
968
|
+
await harness.flush()
|
|
969
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
970
|
+
|
|
971
|
+
byClass(nodes, 'knit-type-btn').find((b) => textOf(b) === '图片与视频').props.onClick()
|
|
972
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
973
|
+
await harness.flush()
|
|
974
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
975
|
+
|
|
976
|
+
assert.ok(calls.some((u) => u.includes('kind=media')), '切换后应带 kind=media 重拉')
|
|
977
|
+
assert.equal(globalThis.window.localStorage.getItem('dsh-knit:kind'), 'media')
|
|
978
|
+
assert.equal(byClass(nodes, 'knit-media-grid').length, 1)
|
|
979
|
+
})
|
|
980
|
+
|
|
981
|
+
test('媒体:媒体视图一个媒体都没有时给出专属空态', async () => {
|
|
982
|
+
installFetch(kindResponder({ media: mediaPayload({ total: 0, docs: [] }) }))
|
|
983
|
+
const { KnitBody } = loadClientModule().exports.__test
|
|
984
|
+
harness.reset()
|
|
985
|
+
harness.seed(['', 'time', 'media'])
|
|
986
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
987
|
+
await harness.flush()
|
|
988
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
989
|
+
assert.match(byClass(nodes, 'knit-msg').map(textOf).join(''), /还没有图片或视频/)
|
|
990
|
+
})
|
package/test/fixture.mjs
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 测试用样本工作区:把「至少 3 篇 Markdown + 一张图 + 一个非媒体文件」
|
|
3
|
+
* 造成一个临时目录,测试因此不再依赖仓库/包的目录布局。
|
|
4
|
+
*
|
|
5
|
+
* 为什么需要它:测试原先用 `new URL('../../')` 当样本工作区 ——
|
|
6
|
+
* 作者本机那是 08_Knit/(恰好有样本),但别人 clone 公开仓库时那是 clone 的父目录,
|
|
7
|
+
* 装进 npm 包时那是 node_modules/,两处都没有样本,测试就会误报红。
|
|
8
|
+
*/
|
|
9
|
+
import { mkdtempSync, mkdirSync, writeFileSync, utimesSync, rmSync } from 'node:fs'
|
|
10
|
+
import { tmpdir } from 'node:os'
|
|
11
|
+
import { join } from 'node:path'
|
|
12
|
+
|
|
13
|
+
/** 1×1 透明 PNG 的真实字节,尾部补 0 到 2048 —— 够测 Range 后缀,且仍是可解码的 PNG。 */
|
|
14
|
+
const PNG_1X1 = Buffer.from(
|
|
15
|
+
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFAAH/q842iQAAAABJRU5ErkJggg==',
|
|
16
|
+
'base64',
|
|
17
|
+
)
|
|
18
|
+
const PADDED_PNG = Buffer.concat([PNG_1X1, Buffer.alloc(2048 - PNG_1X1.length)])
|
|
19
|
+
|
|
20
|
+
/** 假视频:宿主不解析容器,只按扩展名放行并支持 Range。 */
|
|
21
|
+
const FAKE_MP4 = Buffer.alloc(4096, 0x21)
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 建样本工作区。文件名刻意与旧断言一致(README.md / package.json / docs/screenshot.png),
|
|
25
|
+
* 断言语义不用改;mtime 用 utimes 定死,排序断言不再看运气。
|
|
26
|
+
* @returns {string} 工作区绝对路径;进程退出时自动清理
|
|
27
|
+
*/
|
|
28
|
+
export function makeWorkspace() {
|
|
29
|
+
const dir = mkdtempSync(join(tmpdir(), 'knit-fixture-'))
|
|
30
|
+
const base = Date.now() - 120_000
|
|
31
|
+
const files = [
|
|
32
|
+
['README.md', '# 样本 README\n\nKnit 样本工作区主文档,含关键词:相关性排序、sidebar。\n'],
|
|
33
|
+
['docs/notes.md', '# 样本笔记\n\n第二篇,用来凑够三篇。\n'],
|
|
34
|
+
['CHANGELOG.md', '# 样本变更\n\n第三篇。\n'],
|
|
35
|
+
['package.json', '{\n "name": "sample"\n}\n'],
|
|
36
|
+
['docs/screenshot.png', PADDED_PNG],
|
|
37
|
+
['docs/clip.mp4', FAKE_MP4],
|
|
38
|
+
]
|
|
39
|
+
files.forEach(([rel, content], i) => {
|
|
40
|
+
const abs = join(dir, rel)
|
|
41
|
+
mkdirSync(join(abs, '..'), { recursive: true })
|
|
42
|
+
writeFileSync(abs, content)
|
|
43
|
+
const t = (base + i * 1000) / 1000
|
|
44
|
+
utimesSync(abs, t, t)
|
|
45
|
+
})
|
|
46
|
+
process.on('exit', () => rmSync(dir, { recursive: true, force: true }))
|
|
47
|
+
return dir
|
|
48
|
+
}
|
package/test/harness.mjs
CHANGED
|
@@ -59,8 +59,9 @@ export function loadClientModule(options = {}) {
|
|
|
59
59
|
"module.exports.inject = ['slots', 'locale']",
|
|
60
60
|
'module.exports.__test = { KnitBody, OfficialTabBody, BetterSidebarTabBody, '
|
|
61
61
|
+ 'sessionFileAddress, relTime, resolveRelative, pathImagesFor, clampRatio, '
|
|
62
|
-
+ 'openKnitPanel, makeEntryButton, KnitGlyph, KnitTitle, KNIT_ICON_PATH, '
|
|
63
|
-
+ '
|
|
62
|
+
+ 'openKnitPanel, makeEntryButton, KnitGlyph, KnitTitle, KNIT_ICON_PATH, openLocalPath, '
|
|
63
|
+
+ 'requestPreview, fmtDuration, readKindPref, mediaUrl, isMedia, mediaLayoutFor, '
|
|
64
|
+
+ 'MIN_RATIO, MAX_RATIO, ALL_DOC_CAP, MEDIA_MAX_ITEMS, MEDIA_MIN_PX, MEDIA_VIEW_ROWS }\n'
|
|
64
65
|
+ " module.exports.inject = ['slots', 'locale']",
|
|
65
66
|
)
|
|
66
67
|
|
|
@@ -100,15 +101,24 @@ export function createHarness() {
|
|
|
100
101
|
useEffect: (fn) => { pendingEffects.push(fn) },
|
|
101
102
|
// 测试里不做记忆化:每次渲染重算即可,行为与真实 useMemo 等价
|
|
102
103
|
useMemo: (fn) => fn(),
|
|
103
|
-
// 真实 useRef
|
|
104
|
-
useRef
|
|
104
|
+
// 真实 useRef 跨渲染稳定(与 useState 共用槽位空间,同 React)
|
|
105
|
+
useRef(init) {
|
|
106
|
+
const i = hookIdx++
|
|
107
|
+
if (!(i in hookStates)) hookStates[i] = { current: init === undefined ? null : init }
|
|
108
|
+
return hookStates[i]
|
|
109
|
+
},
|
|
105
110
|
// 照抄 React.memo 的真实形态:返回对象而不是函数
|
|
106
111
|
memo: (fn) => ({ $$typeof: Symbol.for('react.memo'), type: fn, compare: null }),
|
|
107
112
|
}
|
|
108
113
|
|
|
109
114
|
globalThis.__knitReact = React
|
|
110
|
-
|
|
111
|
-
|
|
115
|
+
|
|
116
|
+
// 可控定时器:默认什么也不做(否则轮询会拖住进程),
|
|
117
|
+
// 需要时用 tick() 显式跑一轮。
|
|
118
|
+
let timers = new Map()
|
|
119
|
+
let timerSeq = 0
|
|
120
|
+
globalThis.setTimeout = (fn) => { const id = ++timerSeq; timers.set(id, fn); return id }
|
|
121
|
+
globalThis.clearTimeout = (id) => { timers.delete(id) }
|
|
112
122
|
|
|
113
123
|
return {
|
|
114
124
|
/** 重置 hook 槽位,供每个用例独立开始。 */
|
|
@@ -138,6 +148,20 @@ export function createHarness() {
|
|
|
138
148
|
}
|
|
139
149
|
return walk(element)
|
|
140
150
|
},
|
|
151
|
+
/**
|
|
152
|
+
* 跑一轮当前挂起的定时器(快照后清空,新排的不算)。
|
|
153
|
+
*
|
|
154
|
+
* 用来测「悬停延迟显示」这类依赖 setTimeout 的行为。
|
|
155
|
+
* @returns {number} 执行了几个
|
|
156
|
+
*/
|
|
157
|
+
tick() {
|
|
158
|
+
const pending = [...timers.values()]
|
|
159
|
+
timers.clear()
|
|
160
|
+
pending.forEach((fn) => fn())
|
|
161
|
+
return pending.length
|
|
162
|
+
},
|
|
163
|
+
/** 当前挂起几个定时器。 */
|
|
164
|
+
pendingTimers() { return timers.size },
|
|
141
165
|
/** 跑一遍已登记的 useEffect(并给微任务一个 tick)。 */
|
|
142
166
|
async flush() {
|
|
143
167
|
const fx = pendingEffects.slice()
|