tui-mode-switch.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import assert from "node:assert/strict";
  2. import { test } from "node:test";
  3. import React from "react";
  4. import { createTestRenderer } from "@mbears/opentui-core/testing";
  5. import { runTuiWithRenderer } from "../../tui/src/tui.js";
  6. import { TUI_SWITCHABLE_MODES } from "../../tui/src/app-mode.js";
  7. import type { TuiOptions } from "../../tui/src/types.js";
  8. import { resolveExecutionState } from "@zcode/shared";
  9. import { createTuiSubmitPrompt } from "../src/tui-prompt-handler.js";
  10. import { createCliModeState, currentCliMode } from "../src/tui-command-state.js";
  11. import type { RunDependencies } from "../src/cli-types.js";
  12. async function fixture(overrides: Partial<TuiOptions> = {}) {
  13. const environment = globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean };
  14. const previous = environment.IS_REACT_ACT_ENVIRONMENT;
  15. environment.IS_REACT_ACT_ENVIRONMENT = true;
  16. const terminal = await createTestRenderer({
  17. width: 110,
  18. height: 32,
  19. targetFps: 30,
  20. screenMode: "alternate-screen",
  21. useThread: false,
  22. });
  23. terminal.renderer.waitForThemeMode = async () => "dark";
  24. const modes: string[] = [];
  25. let outcome!: Promise<number>;
  26. await React.act(async () => {
  27. outcome = runTuiWithRenderer(
  28. {
  29. noColor: true,
  30. locale: "en-US",
  31. stdin: process.stdin,
  32. stdout: process.stdout,
  33. stderr: process.stderr,
  34. initialMode: "yolo",
  35. workspaceDirectory: "/test/workspace",
  36. submitPrompt: async () => ({ response: "" }),
  37. setMode: async (mode) => {
  38. modes.push(mode);
  39. return { mode };
  40. },
  41. ...overrides,
  42. },
  43. terminal.renderer,
  44. );
  45. });
  46. return {
  47. ...terminal,
  48. modes,
  49. async action(work: () => void | Promise<void>) {
  50. await React.act(async () => {
  51. await work();
  52. });
  53. await React.act(async () => {
  54. await terminal.renderOnce();
  55. });
  56. },
  57. async close() {
  58. await React.act(async () => terminal.renderer.destroy());
  59. await outcome;
  60. environment.IS_REACT_ACT_ENVIRONMENT = previous;
  61. },
  62. };
  63. }
  64. test("terminal Shift+Tab cycles modes with an empty or populated composer", async () => {
  65. const terminal = await fixture();
  66. try {
  67. await terminal.action(() => terminal.renderOnce());
  68. // 循环顺序以 TUI_SWITCHABLE_MODES 为准:staging 新增 guarded 并把 edit 移出 Shift+Tab 循环。
  69. for (const mode of TUI_SWITCHABLE_MODES) {
  70. await terminal.action(() => terminal.mockInput.pressTab({ shift: true }));
  71. assert.equal(terminal.modes.at(-1), mode);
  72. assert.match(
  73. terminal.captureCharFrame(),
  74. new RegExp(` ${mode[0].toUpperCase()}${mode.slice(1)} `),
  75. );
  76. }
  77. await terminal.action(() => terminal.mockInput.typeText("keep this draft"));
  78. await terminal.action(() => terminal.mockInput.pressTab({ shift: true }));
  79. assert.deepEqual(terminal.modes, [...TUI_SWITCHABLE_MODES, "plan"]);
  80. assert.match(terminal.captureCharFrame(), /keep this draft/);
  81. } finally {
  82. await terminal.close();
  83. }
  84. });
  85. test("Shift+Tab still reaches the app after the startup screen is replaced", async () => {
  86. const terminal = await fixture({
  87. loadStartupOptions: async () => ({ initialMode: "yolo" }),
  88. });
  89. try {
  90. await terminal.action(() => terminal.renderOnce());
  91. await terminal.action(() => terminal.mockInput.pressTab({ shift: true }));
  92. assert.deepEqual(terminal.modes, ["plan"]);
  93. } finally {
  94. await terminal.close();
  95. }
  96. });
  97. test("Shift+Tab switches mode while file suggestions are open; Tab still completes the file", async () => {
  98. const terminal = await fixture({
  99. listWorkspacePathSuggestions: async () => ({
  100. items: [{ path: "README.md", kind: "file" }],
  101. truncated: false,
  102. }),
  103. });
  104. try {
  105. await terminal.action(() => terminal.mockInput.typeText("@READ"));
  106. assert.match(terminal.captureCharFrame(), /README.md/);
  107. await terminal.action(() => terminal.mockInput.pressTab({ shift: true }));
  108. assert.deepEqual(terminal.modes, ["plan"]);
  109. assert.match(terminal.captureCharFrame(), /@READ\s/);
  110. assert.doesNotMatch(terminal.captureCharFrame(), /@README.md/);
  111. await terminal.action(() => terminal.mockInput.pressTab());
  112. assert.match(terminal.captureCharFrame(), /@README.md/);
  113. assert.deepEqual(terminal.modes, ["plan"]);
  114. } finally {
  115. await terminal.close();
  116. }
  117. });
  118. test("runtime Plan state survives Shift+Tab responses and metadata refreshes", async () => {
  119. let execution = resolveExecutionState({ mode: "yolo" });
  120. const state = createCliModeState();
  121. const app = {
  122. getMode: () => execution.mode,
  123. runtime: { getPlanEnabled: () => execution.planEnabled },
  124. setMode: async (mode: string) => {
  125. execution = resolveExecutionState({ mode }, execution);
  126. // The real facade returns the permission mode, independently of Plan.
  127. return { mode: execution.mode };
  128. },
  129. listModels: () => [],
  130. close: async () => {},
  131. };
  132. const handler = createTuiSubmitPrompt(
  133. {
  134. env: {},
  135. cwd: () => process.cwd(),
  136. skipUserConfig: true,
  137. loadDotenv: () => ({ keys: [], loaded: false, path: "" }),
  138. createZCodeApp: async () => app,
  139. startProcessProviderRegistryRuntime: async () => ({
  140. runtime: { registryService: {} },
  141. dispose() {},
  142. }),
  143. } as unknown as RunDependencies,
  144. state,
  145. "test",
  146. );
  147. await handler.getSessionMetadata!();
  148. const terminal = await fixture({ initialMode: currentCliMode(state), setMode: handler.setMode });
  149. try {
  150. await terminal.action(() => terminal.mockInput.pressTab({ shift: true }));
  151. assert.deepEqual(execution, { mode: "yolo", planEnabled: true });
  152. assert.match(terminal.captureCharFrame(), / Plan /);
  153. await handler.getSessionMetadata!();
  154. assert.equal(currentCliMode(state), "plan");
  155. await terminal.action(() => terminal.mockInput.pressTab({ shift: true }));
  156. assert.deepEqual(execution, { mode: "build", planEnabled: false });
  157. assert.match(terminal.captureCharFrame(), / Build /);
  158. } finally {
  159. await terminal.close();
  160. await handler.close!();
  161. }
  162. });