Files
apes-sidebar/tests/manifest-consistency.spec.ts
T
Menghuan1918 162c16ec32 feat(file-icon-theme): 可选彩色图标主题接线(懒加载 chunk + 设置开关 + i18n)
- 新 chunk `file-icons`(`lib/client-file-icons.js`,255 kB / gzip 90 kB):把数据表
  注册成 `registerFileIcon` 描述符——按 glyph+color 归并,563 条规则 → 约 250 条注册;
  目录规则只走 `folderNames`(只认领列出的目录,未列出的仍用内置 glyph)。
- `src/client/file-icon-theme.ts`:pref → loadChunk → 注册/注销,generation 守卫
  丢弃「切换中途落地」的加载;chunk 拉取失败只 log,保持内置 glyph。
- pref `fileIconTheme`(默认 `'builtin'`,未知值按 builtin 处理)+ 设置页
  「文件 → 文件图标」下拉(内置单色 / 彩色品牌图标)。
- chunk 名同步四处(chunk-loader / bundle-route / tsdown / manifest-consistency)+
  `bundle-route.spec`;tsdown 为该 chunk 加 react-icons ESM alias——否则整包打进
  6.4 MB,alias 后 255 kB。
- i18n:6 个新 key 镜像到全部 19 份词典(zh/en 之外),locale 键集守护通过。
- 皮肤契约:指南 §12 增豁免条款(品牌色是内容非 chrome,仅限该懒加载 chunk),
  AGENTS §5 同步;`tests/theme.spec.ts` 新增边界用例(核心图标模块零颜色字面量、
  核心不静态 import 该 chunk)。
- 测试:`tests/file-icon-theme.spec.tsx` 7 例(loader 六态 + 真实数据三表注册);
  prefs 新增默认值/解析用例;builtins / prefs / plugin-shape / smoke 夹具补字段。
- 全量 `pnpm test` 失败集与 main 基线逐条一致(5 文件 / 26 例预存环境失败),
  新增用例全绿;typecheck / lint / build / check:consumer-types 全通过。
2026-09-08 21:43:28 +08:00

128 lines
5.4 KiB
TypeScript

/**
* Registry manifest consistency spec: guards the plugin-registry release form
* (`dsh.plugin.json`) against the build output and package.json, so the repo
* cannot drift into an uninstallable / uneable state:
* - `id` passes the registry's strict two-segment validation (native form),
* - `version` stays in sync with package.json,
* - the manifest entry files exist in the build output,
* - the registry client bundle registers exactly the manifest id (the
* browser-side `arrive()` contract), and the official client bundle keeps
* the package-name id (the client-modules compose contract) — the two
* bundles must never be swapped.
*
* Reads the built lib/ output, so run `pnpm build` first (the registry
* install validates main/client.main existence the same way). A missing
* lib/ (fresh clone before the first build) skips the whole suite instead
* of crashing on ENOENT.
*/
import { describe, expect, it } from 'vitest'
import { existsSync, readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const ROOT = resolve(fileURLToPath(new URL('..', import.meta.url)))
/** The build output is present (tsdown emits the whole lib/ in one run). */
const libBuilt = existsSync(resolve(ROOT, 'lib/client.js'))
if (!libBuilt) {
console.warn('[manifest-consistency] lib/ build output missing — run `pnpm build` first; skipping this suite')
}
/** The registry manifest face this spec touches (mirror of the registry's PluginManifest). */
interface PluginManifest {
id: string
version: string
main: string
description?: string
engines?: { dsh?: string }
contributes?: { tools?: string[]; skills?: string[] }
client?: { main?: string; inject?: string[]; immediately?: boolean }
}
interface PackageJson {
name: string
version: string
}
const manifest = JSON.parse(readFileSync(resolve(ROOT, 'dsh.plugin.json'), 'utf8')) as PluginManifest
const pkg = JSON.parse(readFileSync(resolve(ROOT, 'package.json'), 'utf8')) as PackageJson
/** The registry's strict id validation (manifest.ts / client-modules registerExternal): exactly two lowercase slash-separated segments. */
const ID_PATTERN = /^(?!node_modules\/)(?:@[a-z0-9][a-z0-9-.]*\/[a-z0-9][a-z0-9-.]*|[a-z0-9][a-z0-9-.]*\/[a-z0-9][a-z0-9-.]*)$/
/** Literal require() specifiers the frozen browser module table can answer. */
const CLIENT_REQUIRE_ALLOWED = new Set([
'react',
'react/jsx-runtime',
'react-dom',
'react-dom/client',
'cordis',
'@deepseek-ai/dsh-client-ui-slots',
'@deepseek-ai/dsh-client-ui-primitives',
])
/** The registered `__ModuleLoader__.load({ id })` value of a built client bundle. */
function bundleId(file: string): string {
const source = readFileSync(resolve(ROOT, file), 'utf8')
const match = /load\(\{\s*id:\s*"([^"]+)"/.exec(source)
if (match === null) throw new Error(`${file} registers no __ModuleLoader__.load id — run \`pnpm build\` first`)
return match[1]!
}
/** The lazy chunk bundle names (mirror of src/bundle-route.ts CHUNK_NAMES). */
const CHUNK_FILES = ['terminal', 'editor', 'mermaid', 'file-icons'].map(name => `lib/client-${name}.js`)
/** The global registry slot a built chunk script assigns (its factory key). */
function chunkSlot(file: string): string {
const source = readFileSync(resolve(ROOT, file), 'utf8')
const match = /globalThis\.__dshChunks__\["([a-z0-9-]+)"\]/.exec(source)
if (match === null) throw new Error(`${file} assigns no globalThis.__dshChunks__ slot — run \`pnpm build\` first`)
return match[1]!
}
describe.skipIf(!libBuilt)('registry manifest consistency (dsh.plugin.json)', () => {
it('id is a valid two-segment registry id (native form)', () => {
expect(manifest.id).toMatch(ID_PATTERN)
})
it('version matches package.json', () => {
expect(manifest.version).toBe(pkg.version)
})
it('main and client.main exist in the build output', () => {
expect(existsSync(resolve(ROOT, manifest.main))).toBe(true)
expect(manifest.client?.main).toBeDefined()
expect(existsSync(resolve(ROOT, manifest.client!.main!))).toBe(true)
})
it('the registry bundle registers exactly the manifest id and the official bundle the package name (no swap)', () => {
const registryId = bundleId(manifest.client!.main!)
expect(registryId).toBe(manifest.id)
expect(bundleId('lib/client.js')).toBe(pkg.name)
expect(registryId).not.toBe(pkg.name)
})
it('the lazy chunk bundles exist and assign their global registry slots (served by /sidebar/bundle)', () => {
for (const file of CHUNK_FILES) {
expect(existsSync(resolve(ROOT, file)), file).toBe(true)
expect(chunkSlot(file), file).toBe(file.slice('lib/client-'.length, -'.js'.length))
}
})
it('client bundles require only frozen module-table entries', () => {
for (const file of ['lib/client.js', manifest.client!.main!, ...CHUNK_FILES]) {
const source = readFileSync(resolve(ROOT, file), 'utf8')
// Exclude method calls such as `freeModule.require("util")`; only the
// loader factory's lexical require() is constrained by the module table.
const required = [...source.matchAll(/(^|[^.$\w])require\("([^"]+)"\)/gm)].map(match => match[2]!)
expect([...new Set(required)].filter(id => !CLIENT_REQUIRE_ALLOWED.has(id)), file).toEqual([])
}
})
it('contributes declares no tools or skills (the host half registers none)', () => {
expect(manifest.contributes?.tools).toEqual([])
expect(manifest.contributes?.skills).toEqual([])
})
})