tui-ime-cursor.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import { McodeInteractiveRenderer } from "../../src/tui/renderer/interactive-renderer.js";
  3. import { TuiComposer } from "../../src/tui/shell/composer.js";
  4. import { Editor } from "../../src/tui/widgets/editor/editor.js";
  5. import { type Component } from "../../src/tui/engine/public.js";
  6. import { VirtualTerminal } from "../pi-084-upstream/virtual-terminal.js";
  7. const platformDescriptor = Object.getOwnPropertyDescriptor(
  8. process,
  9. "platform",
  10. )!;
  11. const identity = (text: string) => text;
  12. const theme = {
  13. borderColor: identity,
  14. selectList: {
  15. selectedPrefix: identity,
  16. selectedText: identity,
  17. description: identity,
  18. scrollInfo: identity,
  19. noMatch: identity,
  20. },
  21. };
  22. class RecordingTerminal extends VirtualTerminal {
  23. writes: string[] = [];
  24. override write(data: string): void {
  25. this.writes.push(data);
  26. super.write(data);
  27. }
  28. override hideCursor(): void {
  29. this.write("\x1b[?25l");
  30. }
  31. override showCursor(): void {
  32. this.write("\x1b[?25h");
  33. }
  34. }
  35. function setup(
  36. mode: "regular" | "fullscreen",
  37. showHardwareCursor?: boolean,
  38. columns = 40,
  39. ) {
  40. const terminal = new RecordingTerminal(columns, 16);
  41. const renderer = new McodeInteractiveRenderer({
  42. terminal,
  43. initialMode: mode,
  44. showHardwareCursor,
  45. });
  46. const editor = new Editor(renderer.ui, theme, { paddingX: 1 });
  47. const composer = new TuiComposer(
  48. editor,
  49. {
  50. mode: "message",
  51. surface: "conversation",
  52. placeholder: "Ask a side question…",
  53. },
  54. { showTips: false },
  55. );
  56. const trailing: Component & { lines: string[] } = {
  57. lines: ["status", "extra"],
  58. render() {
  59. return this.lines;
  60. },
  61. invalidate() {},
  62. };
  63. renderer.ui.addChild(composer);
  64. renderer.ui.addChild(trailing);
  65. renderer.ui.setFocus(editor);
  66. return { terminal, renderer, editor, trailing };
  67. }
  68. afterEach(() => {
  69. Object.defineProperty(process, "platform", platformDescriptor);
  70. vi.unstubAllEnvs();
  71. });
  72. describe("IME cursor through the product renderer", () => {
  73. it.each([
  74. ["regular", 8],
  75. ["regular", 40],
  76. ["regular", 80],
  77. ["fullscreen", 8],
  78. ["fullscreen", 40],
  79. ["fullscreen", 80],
  80. ] as const)(
  81. "publishes a visible Windows input cursor in %s mode at width %s and after switching",
  82. async (mode, columns) => {
  83. Object.defineProperty(process, "platform", { value: "win32" });
  84. vi.stubEnv("PI_HARDWARE_CURSOR", undefined);
  85. const { terminal, renderer, editor } = setup(mode, undefined, columns);
  86. try {
  87. renderer.start();
  88. await terminal.waitForRender();
  89. expect(renderer.ui.getShowHardwareCursor()).toBe(true);
  90. expect(terminal.writes.join("")).toContain("\x1b[?25h");
  91. expect(terminal.getCursorPosition()).toEqual({ x: 3, y: 2 });
  92. editor.setText("中文abc");
  93. renderer.ui.renderNow();
  94. await terminal.flush();
  95. expect(terminal.getCursorPosition()).toEqual(
  96. columns === 8 ? { x: 6, y: 3 } : { x: 10, y: 2 },
  97. );
  98. renderer.switchMode(mode === "regular" ? "fullscreen" : "regular");
  99. renderer.ui.renderNow();
  100. await terminal.flush();
  101. expect(renderer.ui.getShowHardwareCursor()).toBe(true);
  102. expect(terminal.getCursorPosition()).toEqual(
  103. columns === 8 ? { x: 6, y: 3 } : { x: 10, y: 2 },
  104. );
  105. renderer.ui.setFocus(null);
  106. terminal.writes = [];
  107. renderer.ui.renderNow();
  108. await terminal.flush();
  109. expect(terminal.writes.join("")).toContain("\x1b[?25l");
  110. expect(terminal.writes.join("")).not.toContain("\x1b[?25h");
  111. } finally {
  112. renderer.dispose();
  113. }
  114. },
  115. );
  116. it.each([
  117. ["win32", undefined, undefined, true],
  118. ["win32", "0", undefined, false],
  119. ["win32", "1", undefined, true],
  120. ["win32", undefined, false, false],
  121. ["darwin", undefined, undefined, false],
  122. ["linux", undefined, undefined, false],
  123. ["linux", "1", undefined, true],
  124. ["linux", "0", true, true],
  125. ] as const)(
  126. "respects cursor policy on %s, env=%s, option=%s",
  127. (platform, env, option, expected) => {
  128. Object.defineProperty(process, "platform", { value: platform });
  129. vi.stubEnv("PI_HARDWARE_CURSOR", env);
  130. const { renderer } = setup("regular", option);
  131. try {
  132. expect(renderer.ui.getShowHardwareCursor()).toBe(expected);
  133. } finally {
  134. renderer.dispose();
  135. }
  136. },
  137. );
  138. it.each([false, true])(
  139. "settles the cursor before each regular-mode frame is presented (visible=%s)",
  140. async (visible) => {
  141. const { terminal, renderer, editor, trailing } = setup(
  142. "regular",
  143. visible,
  144. );
  145. const replay = new VirtualTerminal(40, 16);
  146. let cursorVisible = false;
  147. const assertFrame = async (expected: { x: number; y: number }) => {
  148. renderer.ui.renderNow();
  149. await terminal.flush();
  150. const output = terminal.writes.splice(0).join("");
  151. let boundaries = 0;
  152. // Replay at synchronized-output boundaries, not just after the final write:
  153. // the latter would miss the old frame presenting the last painted column.
  154. for (const part of output.split(/(\x1b\[\?2026l)/u)) {
  155. replay.write(part);
  156. for (const match of part.matchAll(/\x1b\[\?25([hl])/gu))
  157. cursorVisible = match[1] === "h";
  158. if (part === "\x1b[?2026l") {
  159. await replay.flush();
  160. expect(replay.getCursorPosition()).toEqual(expected);
  161. expect(cursorVisible).toBe(visible);
  162. boundaries++;
  163. }
  164. }
  165. await replay.flush();
  166. expect(replay.getCursorPosition()).toEqual(expected);
  167. return boundaries;
  168. };
  169. try {
  170. renderer.start();
  171. expect(await assertFrame({ x: 3, y: 2 })).toBeGreaterThan(0);
  172. editor.setText("中文abc");
  173. expect(await assertFrame({ x: 10, y: 2 })).toBeGreaterThan(0);
  174. editor.handleInput("\x1b[D");
  175. await assertFrame({ x: 9, y: 2 });
  176. trailing.lines = ["status"]; // deleted-lines-only branch
  177. expect(await assertFrame({ x: 9, y: 2 })).toBeGreaterThan(0);
  178. editor.setText("中文\nabc");
  179. expect(await assertFrame({ x: 6, y: 3 })).toBeGreaterThan(0);
  180. editor.setText(""); // differential shrink and placeholder restoration
  181. expect(await assertFrame({ x: 3, y: 2 })).toBeGreaterThan(0);
  182. await assertFrame({ x: 3, y: 2 }); // unchanged frame
  183. terminal.resize(32, 16);
  184. replay.resize(32, 16);
  185. expect(await assertFrame({ x: 3, y: 2 })).toBeGreaterThan(0);
  186. renderer.ui.requestRender(true); // forced full repaint
  187. expect(await assertFrame({ x: 3, y: 2 })).toBeGreaterThan(0);
  188. } finally {
  189. renderer.dispose();
  190. }
  191. },
  192. );
  193. });