51 lines
1.8 KiB
TypeScript
51 lines
1.8 KiB
TypeScript
import { test } from 'vitest'
|
||
import assert from 'node:assert/strict'
|
||
import { readFileSync } from 'node:fs'
|
||
import vm from 'node:vm'
|
||
|
||
test('lib/client.js registers the merged package factory with the module loader', () => {
|
||
const code = readFileSync(new URL('../../lib/client.js', import.meta.url), 'utf8')
|
||
assert.ok(!/^export /m.test(code), 'loader file must not contain ESM export syntax')
|
||
|
||
let registered: any
|
||
const window = {
|
||
__ModuleLoader__: {
|
||
load: (entry: any) => { registered = entry },
|
||
},
|
||
}
|
||
vm.runInNewContext(code, {
|
||
window,
|
||
document: undefined,
|
||
console,
|
||
setInterval,
|
||
clearInterval,
|
||
setTimeout,
|
||
clearTimeout,
|
||
})
|
||
assert.ok(registered, 'expected a __ModuleLoader__.load call')
|
||
assert.equal(registered.id, 'dsh-better-chat')
|
||
assert.equal(typeof registered.factory, 'function', 'factory must be a function')
|
||
|
||
// 合并后:一个 client 产物同时带阅读视图与聊天增强。
|
||
assert.match(code, /data-approval-key/, 'approval card routing must be bundled')
|
||
assert.match(code, /data-better-chat-update/, 'store update buttons must be bundled')
|
||
assert.match(code, /conversation\.view/, 'reader view slot registration must be bundled')
|
||
|
||
// 只允许共享模块表里的 require,避免打包进第二份 React/Cordis。
|
||
const requires = [...code.matchAll(/require\("([^"]+)"\)/g)].map((match) => match[1])
|
||
const allowed = new Set([
|
||
'react',
|
||
'react/jsx-runtime',
|
||
'react-dom',
|
||
'react-dom/client',
|
||
'@deepseek-ai/cordis',
|
||
'zod',
|
||
'@tanstack/react-virtual',
|
||
'@deepseek-ai/dsh-client-store',
|
||
'@deepseek-ai/dsh-client-ui-slots',
|
||
'@deepseek-ai/dsh-client-ui-primitives',
|
||
'@deepseek-ai/dsh-client-ui-dockkit',
|
||
])
|
||
for (const id of requires) assert.ok(allowed.has(id), `unexpected runtime require: ${id}`)
|
||
})
|