dsh-knit 0.4.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/LICENSE +21 -0
- package/README.en.md +178 -0
- package/README.md +167 -0
- package/assets/knit-icon-24-mono-4px.svg +1 -0
- package/cordis.patch.yml +8 -0
- package/package.json +63 -0
- package/src/client/client.js +1170 -0
- package/src/host/index.js +653 -0
- package/src/host/relevance.js +236 -0
- package/test/client.test.mjs +500 -0
- package/test/entry.test.mjs +232 -0
- package/test/harness.mjs +235 -0
- package/test/host.test.mjs +249 -0
- package/test/i18n.test.mjs +344 -0
- package/test/icon.test.mjs +153 -0
- package/test/tier1.test.mjs +443 -0
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 浏览器半边测试:两个宿主的注册、排序切换、相关度渲染、预览、降级。
|
|
3
|
+
*
|
|
4
|
+
* 跑法:node --test test/
|
|
5
|
+
*/
|
|
6
|
+
import test from 'node:test'
|
|
7
|
+
import assert from 'node:assert/strict'
|
|
8
|
+
|
|
9
|
+
import { loadClientModule, createHarness, byClass, textOf, makeLocale } from './harness.mjs'
|
|
10
|
+
|
|
11
|
+
const harness = createHarness()
|
|
12
|
+
const React = globalThis.__knitReact
|
|
13
|
+
const h = React.createElement
|
|
14
|
+
|
|
15
|
+
/* ── 假宿主 ─────────────────────────────────────────── */
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 造一个假的 cordis 上下文,只实现插件用到的 inject / effect 语义。
|
|
19
|
+
* 服务缺失时**不调用**回调 —— 这正是 cordis 子 fiber 等待的行为。
|
|
20
|
+
*
|
|
21
|
+
* @param {{sidebarRightTabs?: boolean, betterSidebar?: boolean}} available - 哪些服务可用
|
|
22
|
+
* @returns {{ctx: object, log: object}} 假上下文与调用记录
|
|
23
|
+
*/
|
|
24
|
+
function fakeCtx(available = {}) {
|
|
25
|
+
const log = { tabs: [], bsTabs: [], slots: [], effects: [] }
|
|
26
|
+
const { locale, registered } = makeLocale({ active: available.locale || 'zh' })
|
|
27
|
+
|
|
28
|
+
const slots = {
|
|
29
|
+
inject(name, cb) { cb(); return () => {} },
|
|
30
|
+
register(opts, component) { log.slots.push({ opts, component }); return () => {} },
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const services = { slots, locale }
|
|
34
|
+
if (available.sidebarRightTabs) {
|
|
35
|
+
services.sidebarRightTabs = { register(definition) { log.tabs.push(definition); return () => {} } }
|
|
36
|
+
}
|
|
37
|
+
if (available.betterSidebar) {
|
|
38
|
+
services.betterSidebar = { registerTab(descriptor) { log.bsTabs.push(descriptor); return () => {} } }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const ctx = {
|
|
42
|
+
// 模块级 inject: ['slots','locale'] 让它们直接挂在根 ctx 上(真实 cordis 行为)
|
|
43
|
+
slots,
|
|
44
|
+
locale,
|
|
45
|
+
effect(fn, label) { log.effects.push(label); return fn() },
|
|
46
|
+
get(name) { return services[name] },
|
|
47
|
+
inject(deps, callback) {
|
|
48
|
+
// 真实 cordis:派生上下文保留父级全部服务,再叠加注入的那几个
|
|
49
|
+
const face = { ...services }
|
|
50
|
+
for (const dep of deps) {
|
|
51
|
+
if (!services[dep]) return // 服务缺失 → 子 fiber 保持等待
|
|
52
|
+
}
|
|
53
|
+
face.effect = (fn, label) => { log.effects.push(label); fn() }
|
|
54
|
+
callback(face)
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return { ctx, log, registered }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 造一份文档列表载荷。
|
|
63
|
+
* @param {object} overrides - 覆盖字段
|
|
64
|
+
* @returns {object} 载荷
|
|
65
|
+
*/
|
|
66
|
+
function listPayload(overrides = {}) {
|
|
67
|
+
return {
|
|
68
|
+
ok: true,
|
|
69
|
+
root: '/p',
|
|
70
|
+
total: 2,
|
|
71
|
+
mode: 'time',
|
|
72
|
+
topic: '',
|
|
73
|
+
docs: [
|
|
74
|
+
{ path: '/p/技术方案.md', rel: '技术方案.md', name: '技术方案.md', title: '技术方案', summary: '摘要一', mtimeMs: Date.now() - 60000, score: null },
|
|
75
|
+
{ path: '/p/sub/需求 文档.md', rel: 'sub/需求 文档.md', name: '需求 文档.md', title: '需求', summary: '摘要二', mtimeMs: Date.now() - 9e7, score: null },
|
|
76
|
+
],
|
|
77
|
+
...overrides,
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 装一个记录调用的 fetch 替身。
|
|
83
|
+
* @param {Function} responder - 按 url 返回载荷
|
|
84
|
+
* @returns {{calls: string[]}} 调用记录
|
|
85
|
+
*/
|
|
86
|
+
function installFetch(responder) {
|
|
87
|
+
const calls = []
|
|
88
|
+
globalThis.fetch = async (url) => {
|
|
89
|
+
calls.push(url)
|
|
90
|
+
return { json: async () => responder(url) }
|
|
91
|
+
}
|
|
92
|
+
return { calls }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/* ── 注册:官方右侧栏 ───────────────────────────────── */
|
|
96
|
+
|
|
97
|
+
test('注册:有 sidebarRightTabs 时注册类型、body、标题三件套', () => {
|
|
98
|
+
const { ctx, log, services } = fakeCtx({ sidebarRightTabs: true })
|
|
99
|
+
const { exports } = loadClientModule()
|
|
100
|
+
exports.apply(ctx)
|
|
101
|
+
|
|
102
|
+
// 替换成带记录能力的实现还没生效,这里直接断言真实注册走的路径
|
|
103
|
+
assert.equal(log.tabs.length, 1)
|
|
104
|
+
assert.equal(log.tabs[0].kind, 'knit')
|
|
105
|
+
assert.equal(log.tabs[0].priority, 'extension')
|
|
106
|
+
assert.equal(log.tabs[0].patterns, undefined, 'page 类型不应声明 patterns')
|
|
107
|
+
assert.equal(log.tabs[0].guide.length, 1)
|
|
108
|
+
assert.equal(log.tabs[0].guide[0].order, 30)
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
test('注册:没有 sidebarRightTabs 时静默跳过,不抛错', () => {
|
|
112
|
+
const { ctx, log } = fakeCtx({})
|
|
113
|
+
const { exports } = loadClientModule()
|
|
114
|
+
assert.doesNotThrow(() => exports.apply(ctx))
|
|
115
|
+
assert.equal(log.tabs.length, 0)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
/* ── 注册:better-sidebar ───────────────────────────── */
|
|
119
|
+
|
|
120
|
+
test('注册:有 betterSidebar 时注册一个 tab descriptor', () => {
|
|
121
|
+
const { ctx, log } = fakeCtx({ betterSidebar: true })
|
|
122
|
+
const { exports } = loadClientModule()
|
|
123
|
+
exports.apply(ctx)
|
|
124
|
+
|
|
125
|
+
assert.equal(log.bsTabs.length, 1)
|
|
126
|
+
const d = log.bsTabs[0]
|
|
127
|
+
assert.equal(d.id, 'knit:recent')
|
|
128
|
+
assert.equal(d.single, true, '同类型只开一个')
|
|
129
|
+
assert.equal(typeof d.component, 'function')
|
|
130
|
+
assert.equal(d.title(), 'Knit 最近文档')
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('注册:两个宿主都在时互不影响,各注册一份', () => {
|
|
134
|
+
const { ctx, log } = fakeCtx({ sidebarRightTabs: true, betterSidebar: true })
|
|
135
|
+
const { exports } = loadClientModule()
|
|
136
|
+
exports.apply(ctx)
|
|
137
|
+
|
|
138
|
+
assert.equal(log.tabs.length, 1, '官方右侧栏注册一份')
|
|
139
|
+
assert.equal(log.bsTabs.length, 1, 'better-sidebar 注册一份')
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
test('注册:better-sidebar 缺失时官方右侧栏照常注册', () => {
|
|
143
|
+
const { ctx, log } = fakeCtx({ sidebarRightTabs: true })
|
|
144
|
+
const { exports } = loadClientModule()
|
|
145
|
+
exports.apply(ctx)
|
|
146
|
+
|
|
147
|
+
assert.equal(log.tabs.length, 1)
|
|
148
|
+
assert.equal(log.bsTabs.length, 0)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
/* ── 渲染:排序模式 ─────────────────────────────────── */
|
|
152
|
+
|
|
153
|
+
test('渲染:relevance 模式不做任何相关度可视化,只解释排序依据', async () => {
|
|
154
|
+
installFetch(() => listPayload({
|
|
155
|
+
mode: 'relevance',
|
|
156
|
+
topic: '相关性、sidebar',
|
|
157
|
+
docs: [
|
|
158
|
+
{ path: '/p/a.md', rel: 'a.md', name: 'a.md', title: 'A 文档', summary: 's', mtimeMs: Date.now(), score: 96 },
|
|
159
|
+
{ path: '/p/b.md', rel: 'b.md', name: 'b.md', title: 'B 文档', summary: 's', mtimeMs: Date.now(), score: 12 },
|
|
160
|
+
],
|
|
161
|
+
}))
|
|
162
|
+
|
|
163
|
+
const { exports } = loadClientModule()
|
|
164
|
+
const { KnitBody } = exports.__test
|
|
165
|
+
harness.reset()
|
|
166
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
167
|
+
await harness.flush()
|
|
168
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
169
|
+
|
|
170
|
+
// 相关度可视化已经全部移除:排序本身就是答案,名次即相关度。
|
|
171
|
+
// 数字会被误读成绝对概率;长条对「找到那篇文档」没有帮助。
|
|
172
|
+
assert.equal(byClass(nodes, 'knit-rel-num').length, 0, '不该有百分比数字')
|
|
173
|
+
assert.equal(byClass(nodes, 'knit-rel-bar').length, 0, '不该有长条')
|
|
174
|
+
assert.equal(byClass(nodes, 'knit-rel-fill').length, 0, '不该有长条填充')
|
|
175
|
+
|
|
176
|
+
const docs = byClass(nodes, 'knit-doc')
|
|
177
|
+
assert.ok(!String(docs[0].props.className).includes('hot'), '不该有相关度高亮边框')
|
|
178
|
+
assert.ok(!String(docs[0].props.className).includes('dim'), '不该有变暗档')
|
|
179
|
+
|
|
180
|
+
assert.equal(byClass(nodes, 'knit-time').length, 2, '时间对所有行都一样显示')
|
|
181
|
+
|
|
182
|
+
const topic = byClass(nodes, 'knit-topic').map(textOf)[0]
|
|
183
|
+
assert.ok(topic.includes('相关性、sidebar'), '应说明按什么话题排的')
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
test('渲染:相关度分数只影响顺序,不影响任何一行的渲染', async () => {
|
|
187
|
+
const make = (scores) => listPayload({
|
|
188
|
+
mode: 'relevance',
|
|
189
|
+
topic: 'x',
|
|
190
|
+
docs: scores.map((score, i) => ({
|
|
191
|
+
path: `/p/${i}.md`, rel: `${i}.md`, name: `${i}.md`, title: `文档 ${i}`,
|
|
192
|
+
summary: 's', mtimeMs: Date.now(), score,
|
|
193
|
+
})),
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 渲染一份分数表,返回每一行的 class 与文本。
|
|
198
|
+
* @param {number[]} scores - 分数
|
|
199
|
+
* @returns {Promise<Array<{cls:string, text:string}>>} 行信息
|
|
200
|
+
*/
|
|
201
|
+
async function rowsFor(scores) {
|
|
202
|
+
installFetch(() => make(scores))
|
|
203
|
+
const { exports } = loadClientModule()
|
|
204
|
+
const { KnitBody } = exports.__test
|
|
205
|
+
harness.reset()
|
|
206
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
207
|
+
await harness.flush()
|
|
208
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
209
|
+
return byClass(nodes, 'knit-doc').map((n) => ({ cls: String(n.props.className), text: textOf(n) }))
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const high = await rowsFor([96, 12])
|
|
213
|
+
const zero = await rowsFor([80, 0])
|
|
214
|
+
|
|
215
|
+
// 分数不同,但同样的行数、同样的 class、同样的文案结构
|
|
216
|
+
assert.equal(high.length, zero.length)
|
|
217
|
+
assert.deepEqual(
|
|
218
|
+
high.map((r) => r.cls),
|
|
219
|
+
zero.map((r) => r.cls),
|
|
220
|
+
'分数高低不该改变行的 class',
|
|
221
|
+
)
|
|
222
|
+
assert.ok(
|
|
223
|
+
high.every((r) => !/hot|dim|rel/.test(r.cls)),
|
|
224
|
+
`行上不该挂任何相关度 class:${high.map((r) => r.cls).join(' / ')}`,
|
|
225
|
+
)
|
|
226
|
+
assert.ok(high.every((r) => !/\d+%/.test(r.text)), `行内不该出现百分比:${high.map((r) => r.text).join(' | ')}`)
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
test('渲染:分数为 0 的文档与高分文档渲染完全一致(0 不代表"没用")', async () => {
|
|
230
|
+
installFetch(() => listPayload({
|
|
231
|
+
mode: 'relevance',
|
|
232
|
+
topic: 'x',
|
|
233
|
+
docs: [
|
|
234
|
+
{ path: '/p/a.md', rel: 'a.md', name: 'a.md', title: 'A', summary: 's', mtimeMs: Date.now(), score: 80 },
|
|
235
|
+
{ path: '/p/b.md', rel: 'b.md', name: 'b.md', title: 'B', summary: 's', mtimeMs: Date.now(), score: 0 },
|
|
236
|
+
],
|
|
237
|
+
}))
|
|
238
|
+
|
|
239
|
+
const { exports } = loadClientModule()
|
|
240
|
+
const { KnitBody } = exports.__test
|
|
241
|
+
harness.reset()
|
|
242
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
243
|
+
await harness.flush()
|
|
244
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
245
|
+
|
|
246
|
+
const docs = byClass(nodes, 'knit-doc')
|
|
247
|
+
assert.equal(docs.length, 2)
|
|
248
|
+
assert.equal(docs[0].props.className, docs[1].props.className, '两行 class 必须一致')
|
|
249
|
+
assert.equal(byClass(nodes, 'knit-time').length, 2, '两行都显示时间')
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
test('渲染:time 模式不显示分数,显示相对时间', async () => {
|
|
253
|
+
installFetch(() => listPayload())
|
|
254
|
+
|
|
255
|
+
const { exports } = loadClientModule()
|
|
256
|
+
const { KnitBody } = exports.__test
|
|
257
|
+
harness.reset()
|
|
258
|
+
harness.seed(['', 'time']) // notice='', sort='time'
|
|
259
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
260
|
+
await harness.flush()
|
|
261
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
262
|
+
|
|
263
|
+
assert.equal(byClass(nodes, 'knit-rel-num').length, 0, '时间模式不应有分数')
|
|
264
|
+
assert.equal(byClass(nodes, 'knit-time').length, 2, '应显示相对时间')
|
|
265
|
+
assert.ok(byClass(nodes, 'knit-topic').map(textOf)[0].includes('修改时间'))
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
test('渲染:请求 URL 带上当前排序方式', async () => {
|
|
269
|
+
const { calls } = installFetch(() => listPayload())
|
|
270
|
+
const { exports } = loadClientModule()
|
|
271
|
+
const { KnitBody } = exports.__test
|
|
272
|
+
harness.reset()
|
|
273
|
+
harness.seed(['', 'time'])
|
|
274
|
+
harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
275
|
+
await harness.flush()
|
|
276
|
+
|
|
277
|
+
assert.ok(calls[0].includes('sort=time'), `实际 URL: ${calls[0]}`)
|
|
278
|
+
assert.ok(calls[0].includes('sessionId=s1'))
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
test('渲染:点「最新」会记住偏好并重新拉取', async () => {
|
|
282
|
+
const { calls } = installFetch(() => listPayload())
|
|
283
|
+
const { exports } = loadClientModule()
|
|
284
|
+
const { KnitBody } = exports.__test
|
|
285
|
+
harness.reset()
|
|
286
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
287
|
+
await harness.flush()
|
|
288
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
289
|
+
|
|
290
|
+
const timeBtn = byClass(nodes, 'knit-seg-btn').find((n) => textOf(n) === '最新')
|
|
291
|
+
assert.ok(timeBtn, '应有一个「最新」按钮')
|
|
292
|
+
timeBtn.props.onClick()
|
|
293
|
+
|
|
294
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
295
|
+
await harness.flush()
|
|
296
|
+
assert.ok(calls.some((u) => u.includes('sort=time')), '切换后应带 sort=time 重新请求')
|
|
297
|
+
assert.equal(globalThis.window.localStorage.getItem('dsh-knit:sort'), 'time', '偏好应被记住')
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
/* ── 渲染:就地预览 ─────────────────────────────────── */
|
|
301
|
+
|
|
302
|
+
test('渲染:单击展开预览,再点收起', async () => {
|
|
303
|
+
installFetch((url) => (url.includes('/api/doc')
|
|
304
|
+
? { ok: true, rel: '技术方案.md', title: '技术方案', text: '# 技术方案\n\n正文。', truncated: false }
|
|
305
|
+
: listPayload()))
|
|
306
|
+
|
|
307
|
+
const { exports, markdownCalls } = loadClientModule()
|
|
308
|
+
const { KnitBody } = exports.__test
|
|
309
|
+
harness.reset()
|
|
310
|
+
harness.seed(['', 'time'])
|
|
311
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
312
|
+
await harness.flush()
|
|
313
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
314
|
+
|
|
315
|
+
const panelOf = (ns) => ns.find((n) => String(n.props.className) === 'knit-preview')
|
|
316
|
+
assert.equal(panelOf(nodes), undefined, '初始不应有预览')
|
|
317
|
+
|
|
318
|
+
byClass(nodes, 'knit-doc')[0].props.onClick()
|
|
319
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
320
|
+
assert.ok(panelOf(nodes), '单击后应出现预览面板')
|
|
321
|
+
|
|
322
|
+
await harness.flush()
|
|
323
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
324
|
+
assert.ok(markdownCalls.length >= 1, '应用原生 MarkdownText 渲染')
|
|
325
|
+
assert.match(markdownCalls.at(-1).text, /正文/)
|
|
326
|
+
|
|
327
|
+
byClass(nodes, 'knit-doc')[0].props.onClick()
|
|
328
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
329
|
+
assert.equal(panelOf(nodes), undefined, '再点同一条应收起')
|
|
330
|
+
})
|
|
331
|
+
|
|
332
|
+
test('渲染:拿不到 ui-primitives 时降级为 pre 并**显式说明**', async () => {
|
|
333
|
+
installFetch((url) => (url.includes('/api/doc')
|
|
334
|
+
? { ok: true, rel: '技术方案.md', title: '技术方案', text: '纯文本正文', truncated: false }
|
|
335
|
+
: listPayload()))
|
|
336
|
+
|
|
337
|
+
const { exports, markdownCalls } = loadClientModule({ primitives: false })
|
|
338
|
+
const { KnitBody } = exports.__test
|
|
339
|
+
harness.reset()
|
|
340
|
+
harness.seed(['', 'time'])
|
|
341
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
342
|
+
await harness.flush()
|
|
343
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
344
|
+
byClass(nodes, 'knit-doc')[0].props.onClick()
|
|
345
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
346
|
+
await harness.flush()
|
|
347
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
348
|
+
|
|
349
|
+
assert.equal(markdownCalls.length, 0, '不应调用 MarkdownText')
|
|
350
|
+
assert.equal(byClass(nodes, 'knit-raw').length, 1, '应退回 <pre>')
|
|
351
|
+
assert.match(byClass(nodes, 'knit-preview-note').map(textOf).join(''), /原生 Markdown 渲染不可用/, '降级必须显式说明')
|
|
352
|
+
})
|
|
353
|
+
|
|
354
|
+
test('渲染:MarkdownText 以 React.memo 形态(对象而非函数)提供时也必须用上', async () => {
|
|
355
|
+
// 真实形态:ui-primitives 的 MarkdownText = React.memo(...) → 对象,不是函数。
|
|
356
|
+
// 曾经用 typeof === 'function' 判断,把可用的渲染器误判成不可用,一路静默降级成纯文本。
|
|
357
|
+
installFetch((url) => (url.includes('/api/doc')
|
|
358
|
+
? { ok: true, rel: '技术方案.md', title: '技术方案', text: '# 标题\n\n正文', truncated: false }
|
|
359
|
+
: listPayload()))
|
|
360
|
+
|
|
361
|
+
const { exports, markdownCalls } = loadClientModule()
|
|
362
|
+
const { KnitBody } = exports.__test
|
|
363
|
+
harness.reset()
|
|
364
|
+
harness.seed(['', 'time'])
|
|
365
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
366
|
+
await harness.flush()
|
|
367
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
368
|
+
byClass(nodes, 'knit-doc')[0].props.onClick()
|
|
369
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
370
|
+
await harness.flush()
|
|
371
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
372
|
+
|
|
373
|
+
assert.equal(markdownCalls.length, 1, 'memo 形态的 MarkdownText 必须被用上')
|
|
374
|
+
assert.equal(byClass(nodes, 'knit-raw').length, 0, '不该走纯文本降级')
|
|
375
|
+
})
|
|
376
|
+
|
|
377
|
+
/* ── 宿主适配层 ─────────────────────────────────────── */
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* 把适配层渲染到可以双击为止。
|
|
381
|
+
* @param {object} adapter - 适配层组件
|
|
382
|
+
* @param {object} props - 适配层入参
|
|
383
|
+
* @returns {Promise<{nodes: object[], openFirst: Function}>} 节点与「双击第一条」的动作
|
|
384
|
+
*/
|
|
385
|
+
async function renderAdapter(adapter, props) {
|
|
386
|
+
harness.reset()
|
|
387
|
+
let nodes = harness.render(h(adapter, props))
|
|
388
|
+
await harness.flush()
|
|
389
|
+
nodes = harness.render(h(adapter, props))
|
|
390
|
+
const cards = byClass(nodes, 'knit-doc')
|
|
391
|
+
return {
|
|
392
|
+
nodes,
|
|
393
|
+
openFirst: async () => {
|
|
394
|
+
cards[0].props.onDoubleClick()
|
|
395
|
+
let next = harness.render(h(adapter, props))
|
|
396
|
+
await harness.flush()
|
|
397
|
+
next = harness.render(h(adapter, props))
|
|
398
|
+
return next
|
|
399
|
+
},
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
test('适配:官方宿主双击走 tab.actions.openResource,地址编码正确', async () => {
|
|
404
|
+
const opened = []
|
|
405
|
+
const { exports } = loadClientModule()
|
|
406
|
+
const { OfficialTabBody } = exports.__test
|
|
407
|
+
installFetch(() => listPayload())
|
|
408
|
+
|
|
409
|
+
const props = {
|
|
410
|
+
sessionId: 's-1',
|
|
411
|
+
useTabInfo: () => ({
|
|
412
|
+
tab: { id: 't', visible: true, actions: { openResource: (a) => opened.push(a) } },
|
|
413
|
+
}),
|
|
414
|
+
}
|
|
415
|
+
const { openFirst } = await renderAdapter(OfficialTabBody, props)
|
|
416
|
+
await openFirst()
|
|
417
|
+
|
|
418
|
+
assert.equal(opened.length, 1, '应调用一次 openResource')
|
|
419
|
+
assert.equal(opened[0], 'dsh-resource://file/session/s-1/%E6%8A%80%E6%9C%AF%E6%96%B9%E6%A1%88.md')
|
|
420
|
+
})
|
|
421
|
+
|
|
422
|
+
test('适配:官方宿主缺 tab.actions 时给出可见提示,不静默失败', async () => {
|
|
423
|
+
const { exports } = loadClientModule()
|
|
424
|
+
const { OfficialTabBody } = exports.__test
|
|
425
|
+
installFetch(() => listPayload())
|
|
426
|
+
|
|
427
|
+
const props = { sessionId: 's-1', useTabInfo: () => ({ tab: { id: 't', visible: true } }) }
|
|
428
|
+
const { openFirst } = await renderAdapter(OfficialTabBody, props)
|
|
429
|
+
const nodes = await openFirst()
|
|
430
|
+
|
|
431
|
+
assert.match(byClass(nodes, 'knit-notice').map(textOf).join(''), /openResource/)
|
|
432
|
+
})
|
|
433
|
+
|
|
434
|
+
test('适配:better-sidebar 双击走 ctx.betterSidebar.openTab,传绝对路径', async () => {
|
|
435
|
+
const opened = []
|
|
436
|
+
const fakeBs = { openTab: (seed) => opened.push(seed), isTabEnabled: () => true }
|
|
437
|
+
const { exports } = loadClientModule()
|
|
438
|
+
const { BetterSidebarTabBody } = exports.__test
|
|
439
|
+
installFetch(() => listPayload())
|
|
440
|
+
|
|
441
|
+
const props = { ctx: { get: () => fakeBs }, scope: { sessionId: 's-9' } }
|
|
442
|
+
const { openFirst } = await renderAdapter(BetterSidebarTabBody, props)
|
|
443
|
+
await openFirst()
|
|
444
|
+
|
|
445
|
+
assert.equal(opened.length, 1, '应调用一次 openTab')
|
|
446
|
+
assert.equal(opened[0].type, 'editor')
|
|
447
|
+
assert.equal(opened[0].path, '/p/技术方案.md', '要传绝对路径,better-sidebar 按它读文件')
|
|
448
|
+
assert.equal(opened[0].title, '技术方案.md')
|
|
449
|
+
})
|
|
450
|
+
|
|
451
|
+
test('适配:better-sidebar 的编辑器 tab 被禁用时说明原因,不硬闯', async () => {
|
|
452
|
+
const opened = []
|
|
453
|
+
const fakeBs = { openTab: (seed) => opened.push(seed), isTabEnabled: () => false }
|
|
454
|
+
const { exports } = loadClientModule()
|
|
455
|
+
const { BetterSidebarTabBody } = exports.__test
|
|
456
|
+
installFetch(() => listPayload())
|
|
457
|
+
|
|
458
|
+
const props = { ctx: { get: () => fakeBs }, scope: { sessionId: 's-9' } }
|
|
459
|
+
const { openFirst } = await renderAdapter(BetterSidebarTabBody, props)
|
|
460
|
+
const nodes = await openFirst()
|
|
461
|
+
|
|
462
|
+
assert.equal(opened.length, 0, '禁用时不该硬闯')
|
|
463
|
+
assert.match(byClass(nodes, 'knit-notice').map(textOf).join(''), /禁用/)
|
|
464
|
+
})
|
|
465
|
+
|
|
466
|
+
test('适配:better-sidebar 服务不可用时不崩', async () => {
|
|
467
|
+
const { exports } = loadClientModule()
|
|
468
|
+
const { BetterSidebarTabBody } = exports.__test
|
|
469
|
+
installFetch(() => listPayload())
|
|
470
|
+
|
|
471
|
+
const props = { ctx: { get: () => undefined }, scope: { sessionId: 's-9' } }
|
|
472
|
+
const { openFirst } = await renderAdapter(BetterSidebarTabBody, props)
|
|
473
|
+
const nodes = await openFirst()
|
|
474
|
+
assert.match(byClass(nodes, 'knit-notice').map(textOf).join(''), /不可用/)
|
|
475
|
+
})
|
|
476
|
+
|
|
477
|
+
/* ── 降级与错误态 ───────────────────────────────────── */
|
|
478
|
+
|
|
479
|
+
test('渲染:没有 sessionId 时给出明确错误而不是空白', async () => {
|
|
480
|
+
installFetch(() => listPayload())
|
|
481
|
+
const { exports } = loadClientModule()
|
|
482
|
+
const { KnitBody } = exports.__test
|
|
483
|
+
harness.reset()
|
|
484
|
+
let nodes = harness.render(h(KnitBody, {}))
|
|
485
|
+
await harness.flush()
|
|
486
|
+
nodes = harness.render(h(KnitBody, {}))
|
|
487
|
+
const msg = byClass(nodes, 'knit-msg').map(textOf).join('')
|
|
488
|
+
assert.match(msg, /sessionId/)
|
|
489
|
+
})
|
|
490
|
+
|
|
491
|
+
test('渲染:宿主返回失败时把错误码翻成当前语言的文案', async () => {
|
|
492
|
+
installFetch(() => ({ ok: false, code: 'knit/outside-workspace' }))
|
|
493
|
+
const { exports } = loadClientModule()
|
|
494
|
+
const { KnitBody } = exports.__test
|
|
495
|
+
harness.reset()
|
|
496
|
+
let nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
497
|
+
await harness.flush()
|
|
498
|
+
nodes = harness.render(h(KnitBody, { sessionId: 's1' }))
|
|
499
|
+
assert.match(byClass(nodes, 'knit-msg').map(textOf).join(''), /路径越出工作区/)
|
|
500
|
+
})
|