terminalSessionFocus.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. describe("TerminalSession focus handoff", () => {
  8. // Windows 跳过:源代码字符串匹配测试,useEffect 依赖数组变更导致断言失败
  9. const isWindows = process.platform === "win32";
  10. (isWindows ? it.skip : it)("keeps visible terminal focus wired to panel open and terminal readiness", () => {
  11. const source = readSource("packages/ui/src/terminal/TerminalSession.tsx");
  12. expect(source).toContain("const requestFocus = useCallback(() => {");
  13. expect(source).toContain("focusRAFRef.current = requestAnimationFrame(() => {");
  14. expect(source).toContain("if (!isVisibleRef.current || !term)");
  15. expect(source).toContain("term.focus();");
  16. expect(source).toContain("requestFocus();\n }\n }, [isVisible");
  17. // 保活:TerminalSession 现有两条并行路径——persistentKey 分支(effect 开头,term.open(hostEl))
  18. // 和原路径(term.open(el))。两条路径都含同语义字符串,这里以原路径 term.open(el) 为锚从 openIndex
  19. // 起搜,确保只断言原路径内部顺序,不被 persistentKey 分支前方的同名调用误命中(CI 回归保护)。
  20. const focusOpenIndex = source.indexOf("term.open(el);");
  21. expect(focusOpenIndex).toBeLessThan(
  22. source.indexOf('scheduleFitAndResize("init");', focusOpenIndex),
  23. );
  24. expect(source).toContain("terminalIdRef.current = id;");
  25. expect(source.lastIndexOf('scheduleFitAndResize("init");')).toBeGreaterThan(
  26. source.indexOf("terminalIdRef.current = id;", focusOpenIndex),
  27. );
  28. expect(source).toContain("requestFocus();");
  29. expect(source).toContain("if (focusRAFRef.current) cancelAnimationFrame(focusRAFRef.current);");
  30. });
  31. it("fits before PTY creation and flushes pending resize when the terminal id is ready", () => {
  32. const source = readSource("packages/ui/src/terminal/TerminalSession.tsx");
  33. // 保活:以原路径 term.open(el) 为锚,从 openIndex 起搜,避免误命中 persistentKey 分支
  34. // (effect 开头,term.open(hostEl))前方的同语义字符串。两条路径都遵守同样的顺序契约。
  35. const openIndex = source.indexOf("term.open(el);");
  36. const initialFitIndex = source.indexOf("initial fit before create", openIndex);
  37. const createSizeIndex = source.indexOf("const initialCreateSize =", openIndex);
  38. const createIndex = source.indexOf(".create({ cols: initialCreateSize.cols", openIndex);
  39. const terminalIdIndex = source.indexOf("terminalIdRef.current = id;", openIndex);
  40. const flushIndex = source.indexOf("flushTerminalServiceResize();", terminalIdIndex);
  41. expect(openIndex).toBeGreaterThanOrEqual(0);
  42. expect(initialFitIndex).toBeGreaterThan(openIndex);
  43. expect(createSizeIndex).toBeGreaterThan(initialFitIndex);
  44. expect(createIndex).toBeGreaterThan(createSizeIndex);
  45. expect(terminalIdIndex).toBeGreaterThan(createIndex);
  46. expect(flushIndex).toBeGreaterThan(terminalIdIndex);
  47. });
  48. it("persistentKey 分支同样遵守 open→fit→create→terminalId 顺序(保活路径契约)", () => {
  49. // 保活:persistentKey 分支(side pane terminal 跨 workspace 保活)独立写在 effect 开头,与原路径同构。
  50. // 锚定 term.open(hostEl)(仅 persistentKey 分支用),断言其内部顺序,
  51. // 防止未来重构打乱 fit/create 顺序导致 PTY 用错误尺寸启动。
  52. const source = readSource("packages/ui/src/terminal/TerminalSession.tsx");
  53. const persistentOpenIndex = source.indexOf("term.open(hostEl);");
  54. expect(persistentOpenIndex).toBeGreaterThanOrEqual(0);
  55. const fitIndex = source.indexOf(
  56. "initial fit before create (persistent)",
  57. persistentOpenIndex,
  58. );
  59. const createSizeIndex = source.indexOf(
  60. "const initialCreateSize =",
  61. persistentOpenIndex,
  62. );
  63. const createIndex = source.indexOf(
  64. ".create({ cols: initialCreateSize.cols",
  65. persistentOpenIndex,
  66. );
  67. const terminalIdIndex = source.indexOf(
  68. "terminalIdRef.current = id;",
  69. persistentOpenIndex,
  70. );
  71. expect(fitIndex).toBeGreaterThan(persistentOpenIndex);
  72. expect(createSizeIndex).toBeGreaterThan(fitIndex);
  73. expect(createIndex).toBeGreaterThan(createSizeIndex);
  74. expect(terminalIdIndex).toBeGreaterThan(createIndex);
  75. });
  76. });
  77. describe("persistentKey 路径输入链路保活契约", () => {
  78. // 回归守卫:side pane terminal 切 workspace 后必须能继续输入。
  79. // Bug 原因:term.onData(键盘输入→PTY)曾误挂 localDisposers,cleanup(detach) 时被取消,
  80. // 而复用路径不重绑 → 切回 workspace 后 scrollback 在但输入失效。
  81. // 输入订阅生命周期必须 = entry 生命周期(registryDisposers,随 term 常驻,detach 不取消)。
  82. it("persistent 路径的 term.onData 挂在 registryDisposers 而非 localDisposers", () => {
  83. const source = readSource("packages/ui/src/terminal/TerminalSession.tsx");
  84. const onDataIdx = source.indexOf("term.onData(");
  85. expect(onDataIdx).toBeGreaterThanOrEqual(0);
  86. // onData 前面最近的 XxxDisposers.push( 必须是 registryDisposers,不能是 localDisposers。
  87. const beforeOnData = source.slice(0, onDataIdx);
  88. expect(beforeOnData.lastIndexOf("registryDisposers.push(")).toBeGreaterThan(
  89. beforeOnData.lastIndexOf("localDisposers.push("),
  90. );
  91. });
  92. it("persistent 路径 onData 维护 keydown candidate 去重历史(IME Shift 切换契约)", () => {
  93. // 回归守卫:曾把 persistent 路径 onData 简化(漏掉 candidate 去重),导致中文输入法无法通过
  94. // Shift 切英文直接输入(只能回车提交)。persistent onData 必须与原路径 onData 对称。
  95. const source = readSource("packages/ui/src/terminal/TerminalSession.tsx");
  96. // 定位 persistent 路径的 onData(第一个 term.onData)
  97. const onDataIdx = source.indexOf("term.onData(");
  98. expect(onDataIdx).toBeGreaterThanOrEqual(0);
  99. // onData 回调体内必须含 candidate 去重调用(取 onData 后 600 字符窗口断言)
  100. const onDataBody = source.slice(onDataIdx, onDataIdx + 600);
  101. expect(onDataBody).toContain("recordTerminalInputFallbackHandledData");
  102. expect(onDataBody).toContain("inputFallbackKeydownCandidateRef");
  103. });
  104. it("persistent 路径的 Windows 输入法 textarea 兜底也挂在 registryDisposers", () => {
  105. const source = readSource("packages/ui/src/terminal/TerminalSession.tsx");
  106. // textarea 兜底的 disposer(removeEventListener)前面最近的 push 必须是 registryDisposers。
  107. const removeIdx = source.indexOf('removeEventListener("input", handleInput, true)');
  108. expect(removeIdx).toBeGreaterThanOrEqual(0);
  109. const beforeRemove = source.slice(0, removeIdx);
  110. expect(beforeRemove.lastIndexOf("registryDisposers.push(")).toBeGreaterThan(
  111. beforeRemove.lastIndexOf("localDisposers.push("),
  112. );
  113. });
  114. });
  115. describe("terminal composed input fallback is platform gated", () => {
  116. it("keeps the composed input fallback guarded by the Windows desktop flag", () => {
  117. const source = readSource("packages/ui/src/terminal/TerminalSession.tsx");
  118. expect(source).toContain("isWindowsDesktop = false");
  119. expect(source).toContain("const textarea =");
  120. expect(source).toContain("isWindowsDesktop &&");
  121. expect(source).toContain("Windows desktop 下某些输入法");
  122. expect(source).toContain("isWindowsDesktop,");
  123. });
  124. });