202 lines
6.7 KiB
JavaScript
202 lines
6.7 KiB
JavaScript
import assert from 'node:assert/strict'
|
||
import { readFile } from 'node:fs/promises'
|
||
import { test } from 'node:test'
|
||
|
||
/**
|
||
* 在 Node 里加载浏览器端插件:伪造 __ModuleLoader__、React 与最小 DOM,
|
||
* 验证 1) 注册了右栏页签与三个插槽,2) 无数据时渲染空态提示,3) 头部按钮能打开页签。
|
||
*/
|
||
|
||
function reactStub() {
|
||
const createElement = (type, props, ...children) => {
|
||
const list = children.length === 0 ? undefined : children.length === 1 ? children[0] : children
|
||
return { type, props: { ...(props ?? {}), ...(list === undefined ? {} : { children: list }) } }
|
||
}
|
||
const useState = initial => [typeof initial === 'function' ? initial() : initial, () => {}]
|
||
const useEffect = () => {}
|
||
return {
|
||
createElement,
|
||
useState,
|
||
useEffect,
|
||
useMemo: fn => fn(),
|
||
useCallback: fn => fn,
|
||
useRef: value => ({ current: value }),
|
||
useLayoutEffect: useEffect,
|
||
Fragment: 'Fragment',
|
||
}
|
||
}
|
||
|
||
function domStub() {
|
||
const styles = new Map()
|
||
const document = {
|
||
querySelector(selector) {
|
||
return styles.get(selector) ?? null
|
||
},
|
||
createElement() {
|
||
const node = { attrs: {}, textContent: '' }
|
||
node.setAttribute = (key, value) => { node.attrs[key] = value }
|
||
return node
|
||
},
|
||
head: {
|
||
appendChild(node) {
|
||
styles.set('style[data-plugin-css=' + JSON.stringify(node.attrs['data-plugin-css']) + ']', node)
|
||
},
|
||
},
|
||
}
|
||
return { document, styles }
|
||
}
|
||
|
||
async function loadClient() {
|
||
const source = await readFile(new URL('../client.js', import.meta.url), 'utf8')
|
||
const { document, styles } = domStub()
|
||
let captured
|
||
const window = {
|
||
__ModuleLoader__: {
|
||
load(options) {
|
||
captured = options
|
||
return options.factory((name) => {
|
||
if (name === 'react') return reactStub()
|
||
throw new Error(`未知模块 ${name}`)
|
||
})
|
||
},
|
||
},
|
||
localStorage: {
|
||
store: new Map(),
|
||
getItem(key) { return this.store.has(key) ? this.store.get(key) : null },
|
||
setItem(key, value) { this.store.set(key, String(value)) },
|
||
},
|
||
}
|
||
const run = new Function('window', 'document', 'fetch', 'nextTick', source)
|
||
run(window, document, () => Promise.reject(new Error('测试里不联网')), () => {})
|
||
return { module: captured.factory((name) => (name === 'react' ? reactStub() : (() => { throw new Error(name) })())), captured, document, styles, window }
|
||
}
|
||
|
||
function fakeCtx({ better } = {}) {
|
||
const tabs = []
|
||
const slots = []
|
||
const services = {
|
||
sidebarRightTabs: { register(options) { tabs.push(options); return () => {} } },
|
||
sidebarRight: { openTabIn(sessionId, kind) { services.sidebarRight.opened = [sessionId, kind] } },
|
||
}
|
||
if (better) services.betterSidebar = { openTab(options, target) { services.betterSidebar.opened = [options, target] } }
|
||
return {
|
||
effect(fn) { return fn() },
|
||
sidebarRightTabs: services.sidebarRightTabs,
|
||
get(name, fallback) { return services[name] ?? fallback },
|
||
slots: {
|
||
inject(name, callback) {
|
||
const result = callback()
|
||
slots.push({ name, ...result })
|
||
return () => {}
|
||
},
|
||
register(spec, component) {
|
||
return { spec, component }
|
||
},
|
||
},
|
||
tabs,
|
||
slotsList: slots,
|
||
services,
|
||
}
|
||
}
|
||
|
||
function textOf(node) {
|
||
if (node === null || node === undefined || node === false) return ''
|
||
if (typeof node === 'string' || typeof node === 'number') return String(node)
|
||
if (Array.isArray(node)) return node.map(textOf).join(' ')
|
||
const children = node.props?.children
|
||
return [node.type, textOf(children)].join(' ')
|
||
}
|
||
|
||
test('客户端模块导出页签常量与依赖注入', async () => {
|
||
const { module, captured } = await loadClient()
|
||
assert.equal(captured.id, 'dsh-sansheng-sansheng')
|
||
assert.equal(typeof module.apply, 'function')
|
||
assert.deepEqual(module.inject, ['slots', 'sidebarRightTabs', 'sidebarRight'])
|
||
assert.equal(module.TAB_ID, 'sansheng-roles')
|
||
assert.equal(module.TAB_KIND, 'sansheng-roles')
|
||
})
|
||
|
||
test('apply 注册右栏页签与三个插槽,并只注入一次样式', async () => {
|
||
const { module, styles } = await loadClient()
|
||
const ctx = fakeCtx()
|
||
module.apply(ctx)
|
||
|
||
assert.equal(ctx.tabs.length, 1)
|
||
const tab = ctx.tabs[0]
|
||
assert.equal(tab.id, 'sansheng-roles')
|
||
assert.equal(tab.kind, 'sansheng-roles')
|
||
assert.equal(tab.keepMounted, true)
|
||
assert.equal(tab.priority, 'builtin')
|
||
assert.equal(tab.title(), '三省六部')
|
||
|
||
assert.deepEqual(ctx.slotsList.map(item => item.name), [
|
||
'sidebar.right.pane.tab',
|
||
'sidebar.right.pane.tab.title',
|
||
'conversation.session.header.actions',
|
||
])
|
||
const pane = ctx.slotsList[0].spec
|
||
assert.equal(pane.key, 'sansheng-roles')
|
||
const header = ctx.slotsList[2].spec
|
||
assert.equal(header.id, 'sansheng-roles:open')
|
||
assert.equal(header.order, 60)
|
||
|
||
assert.equal(styles.size, 1, '样式只注入一次')
|
||
module.apply(fakeCtx())
|
||
assert.equal(styles.size, 1, '重复 apply 不重复注入样式')
|
||
})
|
||
|
||
test('右栏列在没有项目时渲染引导文案', async () => {
|
||
const { module } = await loadClient()
|
||
const ctx = fakeCtx()
|
||
module.apply(ctx)
|
||
const wrap = ctx.slotsList[0].component
|
||
const element = wrap({ sessionId: 'sess-1' })
|
||
const tree = element.type(element.props)
|
||
const text = textOf(tree)
|
||
assert.match(text, /还没有三省六部项目/)
|
||
assert.match(text, /sansheng_provision/)
|
||
})
|
||
|
||
test('页签标题渲染三省与角色字样', async () => {
|
||
const { module } = await loadClient()
|
||
const ctx = fakeCtx()
|
||
module.apply(ctx)
|
||
const title = ctx.slotsList[1].component
|
||
const text = textOf(title())
|
||
assert.match(text, /角色/)
|
||
})
|
||
|
||
test('头部按钮在当前会话打开右栏页签', async () => {
|
||
const { module } = await loadClient()
|
||
const ctx = fakeCtx()
|
||
module.apply(ctx)
|
||
const wrap = ctx.slotsList[2].component
|
||
const element = wrap({ sessionId: 'sess-42' })
|
||
const tree = element.type(element.props)
|
||
assert.match(textOf(tree), /角色/)
|
||
tree.props.onClick()
|
||
assert.deepEqual(ctx.services.sidebarRight.opened, ['sess-42', 'sansheng-roles'])
|
||
})
|
||
|
||
test('装了 better-sidebar 时优先走它的 openTab', async () => {
|
||
const { module } = await loadClient()
|
||
const ctx = fakeCtx({ better: true })
|
||
module.apply(ctx)
|
||
const wrap = ctx.slotsList[2].component
|
||
const element = wrap({ sessionId: 'sess-7' })
|
||
const tree = element.type(element.props)
|
||
tree.props.onClick()
|
||
assert.deepEqual(ctx.services.betterSidebar.opened, [{ type: 'sansheng-roles' }, { sessionId: 'sess-7' }])
|
||
})
|
||
|
||
test('没有当前会话时提示页签是会话级的', async () => {
|
||
const { module } = await loadClient()
|
||
const ctx = fakeCtx()
|
||
module.apply(ctx)
|
||
const wrap = ctx.slotsList[2].component
|
||
const element = wrap({})
|
||
const tree = element.type(element.props)
|
||
tree.props.onClick()
|
||
assert.equal(ctx.services.sidebarRight.opened, undefined)
|
||
})
|