sidePaneTerminalPersistentProfileTheme.test.ts 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { readFileSync } from "node:fs";
  2. import { resolve } from "node:path";
  3. import { describe, expect, it } from "vitest";
  4. function readSource(path: string) {
  5. return readFileSync(resolve(process.cwd(), path), "utf8");
  6. }
  7. /**
  8. * side pane terminal persistent profile theme 跨重挂保活契约(CR-01 Critical)。
  9. *
  10. * Bug 原因:
  11. * terminalService.create() 返回的 profile theme(用户终端配置的颜色)只写入
  12. * 组件局部 terminalProfileThemeRef(TerminalSession.tsx:565)。registry entry 不保存它。
  13. * 组件卸载后 ref 销毁;重挂时新组件 ref 被重置为 undefined(line 340),复用路径
  14. * reuseThemeObserver 用 mergeTerminalTheme(terminalProfileThemeRef.current) =
  15. * mergeTerminalTheme(undefined) = 基础主题,丢掉 profile theme。
  16. * 触发条件:终端服务返回自定义 theme + 用户切到其他 workspace 再切回 + 之后切换
  17. * 浅色/深色主题(documentElement.class 变化触发 observer)。
  18. * 后果:终端颜色与用户配置不一致,前景/背景/ANSI 配色可读性下降,持续到关闭重建 tab。
  19. *
  20. * 修复(CR-01 建议):profile theme 所有权上移到 registry entry(与 term/PTY 同一常驻所有者)。
  21. * entry 承载 profileTheme,首次创建写入 entry.profileTheme,重挂 observer 从 entry 读取。
  22. * 与 CR-01 的精神一致:本 MR 核心承诺是同一个 xterm/PTY 跨组件生命周期复用,
  23. * 与终端实例关联的 profile theme 也必须由同一常驻所有者保存。
  24. */
  25. describe("side pane terminal persistent profile theme 跨重挂保活契约", () => {
  26. it("registry entry 必须承载 profileTheme 字段", () => {
  27. const source = readSource("packages/ui/src/terminal/sidePaneTerminalSessionRegistry.ts");
  28. // entry 接口含 profileTheme,否则 profile theme 只活在组件局部 ref,重挂丢失。
  29. expect(source).toContain("profileTheme");
  30. });
  31. it("首次创建时 create 返回的 theme 必须写入 entry.profileTheme", () => {
  32. const source = readSource("packages/ui/src/terminal/TerminalSession.tsx");
  33. const idx = source.indexOf("terminalProfileThemeRef.current = theme");
  34. expect(idx).toBeGreaterThan(-1);
  35. const block = source.slice(idx, idx + 400);
  36. // 必须同步写入 entry.profileTheme,让 profile theme 所有权上移到 registry entry。
  37. expect(block).toContain("entry.profileTheme");
  38. });
  39. it("复用路径 observer 必须从 entry.profileTheme 读取,不能用组件局部 ref", () => {
  40. const source = readSource("packages/ui/src/terminal/TerminalSession.tsx");
  41. const idx = source.indexOf("reuseThemeObserver");
  42. expect(idx).toBeGreaterThan(-1);
  43. // 取 observer 回调块(到闭合 });)
  44. const blockEnd = source.indexOf("});", idx);
  45. expect(blockEnd).toBeGreaterThan(idx);
  46. const block = source.slice(idx, blockEnd);
  47. // 必须用 existingEntry.profileTheme;用 terminalProfileThemeRef.current 会在重挂后丢 profile theme。
  48. expect(block).toContain("existingEntry.profileTheme");
  49. expect(block).not.toContain("terminalProfileThemeRef.current");
  50. });
  51. it("首次创建路径 observer 也必须从 entry.profileTheme 读取(统一常驻所有者)", () => {
  52. const source = readSource("packages/ui/src/terminal/TerminalSession.tsx");
  53. // 定位首次创建路径的 themeObserver(区别于复用路径的 reuseThemeObserver)
  54. const idx = source.indexOf("const themeObserver = new MutationObserver");
  55. expect(idx).toBeGreaterThan(-1);
  56. const blockEnd = source.indexOf("});", idx);
  57. expect(blockEnd).toBeGreaterThan(idx);
  58. const block = source.slice(idx, blockEnd);
  59. // 首次创建 observer 也应从 entry 读取,统一 profile theme 的单一数据源。
  60. expect(block).toContain("entry.profileTheme");
  61. });
  62. });