// 工作台主题桥回归: // 1) 纯函数(前缀判定 / 取值清洗)行为正确 // 2) 注入幂等、有 head 插 head、无 head 退 body、再退前置;支持 external 模式 // 3) 浏览器脚本含关键接线(宿主镜像、深色标记、变化跟随、兜底轮询) // 4) 站点路由 / 模板路由 / dshell.css / dshell.html 的接线必须都在(防"改了桥但没接上") import assert from 'node:assert/strict' import { test } from 'node:test' import { createRequire } from 'node:module' import { runInNewContext } from 'node:vm' import { readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' const require = createRequire(new URL('../01_content/package.json', import.meta.url)) const { buildSync } = require('esbuild') const code = buildSync({ entryPoints: [fileURLToPath(new URL('../01_content/src/theme.ts', import.meta.url))], bundle: true, write: false, platform: 'node', format: 'cjs', }).outputFiles[0].text const module = { exports: {} } runInNewContext(code, { module, exports: module.exports, setInterval: () => 0, setTimeout: () => 0, clearTimeout: () => {} }) const { THEME_VAR_PREFIXES, THEME_BRIDGE_ID, THEME_JS_PATH, shouldMirrorVar, pickThemeVars, injectThemeBridge, THEME_BRIDGE_JS } = module.exports const read = (rel) => readFileSync(fileURLToPath(new URL(rel, import.meta.url)), 'utf8') const indexSrc = read('../01_content/src/index.ts') const cssSrc = read('../01_content/template/dshell.css') const htmlSrc = read('../01_content/template/dshell.html') test('shouldMirrorVar 只认主题 token 前缀', () => { assert.deepEqual([...THEME_VAR_PREFIXES], ['--dsw-', '--dsh-', '--ds-']) for (const ok of ['--dsw-alias-bg-base', '--dsh-content-font-size', '--ds-font-family-code']) { assert.ok(shouldMirrorVar(ok), '应镜像:' + ok) } for (const no of ['color', '--x', '--dsw', 'dsw-alias-bg-base', '', null, undefined]) { assert.ok(!shouldMirrorVar(no), '不应镜像:' + String(no)) } }) test('pickThemeVars 去空值、去非法名、保留顺序', () => { const got = pickThemeVars([ ['--dsw-alias-bg-base', ' #fff '], ['color', '#000'], ['--dsh-content-font-size', ''], ['--ds-font-family-code', 'Menlo, monospace'], ['--dsw-alias-label-primary', ' '], ]) // 跨 vm 上下文,数组原型不同,逐项按字符串比较 assert.equal(got.length, 2) assert.deepEqual([String(got[0][0]), String(got[0][1])], ['--dsw-alias-bg-base', '#fff']) assert.deepEqual([String(got[1][0]), String(got[1][1])], ['--ds-font-family-code', 'Menlo, monospace']) assert.equal(pickThemeVars(null).length, 0) assert.equal(pickThemeVars([null, [], ['--dsw-a']]).length, 0) }) test('injectThemeBridge:插 head、幂等、退化到 body/前置、external 模式', () => { const page = '