tui-focus.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import React, { act } from "react";
  4. import { testRender } from "@mbears/opentui-react/test-utils";
  5. import { getZCodeCopy } from "@zcode/i18n";
  6. import { completeSlashCommand } from "../src/app-keyboard.js";
  7. import { AppView } from "../src/app-view.js";
  8. import { Sidebar } from "../src/app-sidebar.js";
  9. import type { SidebarState } from "../src/app-model.js";
  10. import type { PromptInputEditor } from "../src/app-input-pane.js";
  11. import type { TuiSlashCommandSuggestion } from "../src/types.js";
  12. test("ordinary tab does not change prompt text or expose transcript focus", () => {
  13. let nextDraft: string | undefined;
  14. assert.equal(
  15. completeSlashCommand([], undefined, (value) => {
  16. nextDraft = value;
  17. }),
  18. false,
  19. );
  20. assert.equal(nextDraft, undefined);
  21. const sidebarLines = collectTextLines(Sidebar(baseSidebarState));
  22. assert.equal(
  23. sidebarLines.some((line) => line.includes("Tab focus")),
  24. false,
  25. );
  26. assert.equal(
  27. sidebarLines.some((line) => line.startsWith("Focus ")),
  28. false,
  29. );
  30. });
  31. test("tab still completes an active slash command candidate", () => {
  32. const commands: TuiSlashCommandSuggestion[] = [
  33. {
  34. name: "compact",
  35. summary: "Compact the session",
  36. usage: "/compact",
  37. },
  38. ];
  39. let nextDraft = "";
  40. assert.equal(
  41. completeSlashCommand(commands, { selectedIndex: 0 }, (value) => {
  42. nextDraft = value;
  43. }),
  44. true,
  45. );
  46. assert.equal(nextDraft, "/compact ");
  47. });
  48. test("mouse clicks outside the prompt return focus to the prompt input", async () => {
  49. const editorRef = { current: null } as React.MutableRefObject<PromptInputEditor | null>;
  50. let copySelectionCount = 0;
  51. let view: Awaited<ReturnType<typeof testRender>> | undefined;
  52. await act(async () => {
  53. view = await testRender(
  54. React.createElement(AppView, {
  55. approvalQueue: [],
  56. busy: false,
  57. contextUsage: {},
  58. copy: getZCodeCopy("en-US").tui,
  59. copyCurrentSelection: () => {
  60. copySelectionCount += 1;
  61. return false;
  62. },
  63. draft: "",
  64. editorRef,
  65. inputCursorToEndVersion: 0,
  66. lastEvent: "idle",
  67. liveModelText: "",
  68. loginRequired: false,
  69. messages: [{ content: "clickable transcript line", role: "agent" }],
  70. mode: "build",
  71. model: "openai/gpt-test",
  72. modifiedFiles: [],
  73. networkRequests: [],
  74. options: {
  75. noColor: true,
  76. stderr: process.stderr,
  77. stdin: process.stdin,
  78. stdout: process.stdout,
  79. submitPrompt: async () => ({ response: "done" }),
  80. },
  81. setDraftValue: () => undefined,
  82. sidebarLayout: {
  83. overlay: false,
  84. reservedWidth: 0,
  85. visible: false,
  86. wide: false,
  87. },
  88. slashCommands: [],
  89. status: "Ready.",
  90. statusDetails: [],
  91. submitValue: () => undefined,
  92. thoughtLevel: "medium",
  93. todos: [],
  94. terminalWidth: 100,
  95. }),
  96. {
  97. height: 24,
  98. kittyKeyboard: true,
  99. otherModifiersMode: true,
  100. width: 100,
  101. },
  102. );
  103. await view.renderOnce();
  104. });
  105. try {
  106. assert.ok(view);
  107. assert.equal(editorRef.current?.focused, true);
  108. await act(async () => {
  109. await view?.mockMouse.click(5, 5);
  110. await view?.renderOnce();
  111. });
  112. assert.equal(copySelectionCount, 1);
  113. assert.equal(editorRef.current?.focused, true);
  114. } finally {
  115. await act(async () => {
  116. view?.renderer.destroy();
  117. });
  118. }
  119. });
  120. const baseSidebarState: SidebarState = {
  121. busy: false,
  122. contextUsage: {},
  123. draft: "",
  124. lastEvent: "idle",
  125. messageCount: 1,
  126. mcpStatus: {
  127. loading: false,
  128. servers: {},
  129. },
  130. mode: "build",
  131. model: "openai/gpt-test",
  132. modifiedFiles: [],
  133. networkRequests: [],
  134. status: "Ready.",
  135. statusDetails: [],
  136. thoughtLevel: "medium",
  137. todos: [],
  138. };
  139. function collectTextLines(node: unknown): string[] {
  140. const lines: string[] = [];
  141. const visit = (value: unknown) => {
  142. if (typeof value === "string") {
  143. lines.push(value);
  144. return;
  145. }
  146. if (Array.isArray(value)) {
  147. for (const child of value) visit(child);
  148. return;
  149. }
  150. if (typeof value === "object" && value !== null && "props" in value) {
  151. const element = value as { props?: { children?: unknown } };
  152. visit(element.props?.children);
  153. }
  154. };
  155. visit(node);
  156. return lines;
  157. }