Files
apes-sidebar/tests/chunk-artifact.spec.ts
T
Menghuan1918 1ff8fa3514 feat(terminal): 底部工作台终端 tab terminal-bottom(#774)
底部 + 菜单里开得出 shell,刷新后认回同一个 shell,且**不**变成宿主
右列终端的第二个入口。

实现分三层:

1. 宿主能力层(src/client/terminal-client.ts,核心 bundle 安全):结构化
   探测 `ctx.webTerminals`(view/close/recover 三个函数都在才算数)+
   身份助手。**不写进 dsh.client.inject**:注入一个不存在的服务会把整行
   拖成 pending,插件在无终端控制器的部署上会整体不加载;也**不**
   value-import 任何 @deepseek-ai/*(纯度门),类型面手工声明。
2. chunk 层(src/client/TerminalView.tsx + terminal.module.css +
   chunks/terminal.tsx + terminal-lazy.tsx):xterm 与它的样式表整份打进
   lib/client-terminal.js(750 KB),核心包一处都没有;chunk 名在 9 处
   镜像(tsdown / bundle-route / chunk-loader / package.json.files /
   package-registry / bundle-route.spec),另两处 spec 从 CHUNK_NAMES 派生。
3. 描述符层(builtins/tabs.tsx):第 6 个内置类型,`bottomOnly: true`
   (见上一个 commit)、`single`、`available` 读同一份探测、`onClose`
   结束进程。

三条恢复语义的由来:

- **contentId 恒定,terminalId 不写进 tab.meta**:宿主 view() 显式传
  terminalId 会关掉「缺了就建」分支,一个过期 id 会把页签永久卡在
  missingTerminal;宿主自己的 (sessionId, contentId) → 终端 id 持久绑定
  本来就能在刷新后认回同一个 shell。
- **「新建终端」换代号**:DSH 重启后 PTY 没了而 localStorage 绑定还在,
  恒定 contentId 会永远认领死绑定。tab.meta.terminalRun(默认 0)+1 换
  一代 contentId 让宿主另铸进程;key 与 contentId 一起换(views 按
  (sessionId, key) 记忆化)。
- **关页签才结束进程**:宿主的契约是「视图跨 DOM 卸载存活」,这正是刷新
  能认回同一个 shell 的原因,所以卸载不 close;但不 close 会让每个关掉
  的终端永久占一个 per-session 终端槽位(terminalLimit),因此 descriptor
  的 onClose → webTerminals.close() 是唯一且必须存在的回收路径。

测试(判别性证据见交付报告):
- tests/terminal-tab.spec.tsx 14 条:view() 调用形状与 contentId 恒定性、
  mount/detach、write/resize 转发与上限夹紧、snapshot 重放 + acknowledge、
  无服务降级、missingTerminal → 换代号、onClose → close()。
- tests/builtins.spec.ts 内置清单 5 → 6,并新增「terminal-bottom 是
  bottomOnly、其余内置都不是」。
- tests/chunk-artifact.spec.ts 新增产物包含性断言:chunk 里必须有
  xterm(xterm-char-measure-element / @xterm/xterm/lib/xterm.js),
  lib/client.js 与 lib/client-registry.js 里一处都不许有。
- tests/e2e/mount.e2e.ts 第三条真实浏览器用例:先开原生右列(guide 基线
  = 宿主 terminal 恰好 1 条)→ 展开底部工作台(data-active + 布局推挤
  > 0)→ 点空面板的 Terminal 卡片 → 断言 tab 挂载、.xterm 在、chunk 有
  响应、状态不停在 unavailable/loading、宿主 terminal 条目仍为 1 且
  terminal-bottom 条目为 0、无 pageerror / 插件 console 错误。

依赖:@xterm/xterm@^6.0.0 + @xterm/addon-fit@^0.11.0(与宿主自用版本一致,
安装期零脚本,不触市场拒绝名单)。lockfile 只手工拼接这 2 个包的 4 个块:
pnpm add 会顺手重解析无关 caret 范围,产出 99+/69- 的噪声 diff。
2026-10-05 23:42:45 +08:00

87 lines
4.1 KiB
TypeScript

/**
* Built chunk artifact contract: each lib/client-<name>.js must, when
* executed as a classic script, assign its factory to the plugin-owned
* global registry (globalThis.__dshChunks__[<name>]), and the factory must
* be callable with a require that resolves the platform externals — the
* exact shape the loader (src/client/chunk-loader.ts) depends on. Reads the
* built lib/ output, so run `pnpm build` first (like manifest-consistency).
* A missing lib/ (fresh clone before the first build) skips the whole suite
* instead of crashing on ENOENT.
*/
import { existsSync, readFileSync } from 'node:fs'
import { describe, expect, it } from 'vitest'
// Browser globals first: chunk bodies probe `self`/`document` at evaluation
// (CodeMirror's UA probe).
import './browser-globals.ts'
import { CHUNK_EXTERNALS } from '../src/client/chunk-loader.ts'
import { CHUNK_NAMES } from '../src/bundle-route.ts'
const g = globalThis as Record<string, unknown>
/**
* The chunk set, DERIVED from the host route's allowlist — the same registry
* `/sidebar/bundle` serves from (src/bundle-route.ts). Never hand-mirrored:
* the hand-written list that used to live here drifted (`locale` was
* missing), so the locale artifact went unguarded.
*/
const CHUNKS = CHUNK_NAMES
/** All chunk artifacts present (tsdown emits the whole lib/ in one run). */
const chunksBuilt = CHUNKS.every(name => existsSync(`lib/client-${name}.js`))
if (!chunksBuilt) {
console.warn('[chunk-artifact] lib/ chunk artifacts missing — run `pnpm build` first; skipping this suite')
}
describe.skipIf(!chunksBuilt)('built chunk artifacts', () => {
it('each chunk assigns its global registry slot when executed as a script', () => {
g.window = g // classic-script globals
// mermaid's core hooks window.addEventListener('load') at module scope
// (its startOnLoad wiring); the Node global lacks the API, so stub it
// exactly like browser-globals.ts does for its window stub.
if (typeof g.addEventListener !== 'function') g.addEventListener = () => {}
if (typeof g.removeEventListener !== 'function') g.removeEventListener = () => {}
for (const name of CHUNKS) {
const code = readFileSync(`lib/client-${name}.js`, 'utf8')
expect(() => new Function(code)(), name).not.toThrow()
const registry = g.__dshChunks__ as Record<string, unknown>
expect(typeof registry[name], name).toBe('function')
}
})
it('each chunk factory materializes through a require over the platform externals', () => {
const registry = g.__dshChunks__ as Record<string, unknown>
const table = new Map<string, unknown>(CHUNK_EXTERNALS.map(spec => [spec, { spec }]))
for (const name of CHUNKS) {
const factory = registry[name] as (require: (spec: string) => unknown) => Record<string, unknown>
expect(() => factory((spec) => {
if (!table.has(spec)) throw new Error(`require("${spec}") missed the module table`)
return table.get(spec)
}), name).not.toThrow()
}
})
/**
* The emulator's own DOM contract, present in xterm's runtime source AND in
* its stylesheet — either half landing in the core bundle means the lazy
* split broke. `@xterm/xterm` alone would be too weak a probe: the package
* NAME also shows up in doc comments and locale copy.
*/
const XTERM_MARKER = 'xterm-char-measure-element'
it('the terminal chunk carries xterm and the core bundles carry none of it', () => {
// #774's whole build requirement: several hundred KB of emulator must not
// reach startup. The core bundle is what the plugin ships to every page
// load, so a static import of src/client/TerminalView.tsx (or of the
// xterm stylesheet) from a core module would show up here.
const chunk = readFileSync('lib/client-terminal.js', 'utf8')
expect(chunk).toContain(XTERM_MARKER)
expect(chunk).toContain('@xterm/xterm/lib/xterm.js')
for (const file of ['lib/client.js', 'lib/client-registry.js']) {
const source = readFileSync(file, 'utf8')
expect(source, `${file} must not bundle xterm`).not.toContain(XTERM_MARKER)
expect(source, `${file} must not resolve @xterm/xterm`).not.toContain('@xterm/xterm')
}
})
})