tui-mode-suggestions.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import { completeModeCommand } from "../src/app-keyboard.js";
  4. import {
  5. filterModeOptions,
  6. modeCommandQuery,
  7. selectedModeOption,
  8. } from "../src/app-mode-command.js";
  9. import { ModeSuggestionPanel, visibleModeOptionWindow } from "../src/app-mode-suggestion-panel.js";
  10. import { palette } from "../src/app-model.js";
  11. import type { TuiModeOption } from "../src/types.js";
  12. import { TUI_SWITCHABLE_MODES } from "../src/app-mode.js";
  13. const modes: TuiModeOption[] = [
  14. {
  15. description: "Inspect the code and present a plan before editing.",
  16. id: "plan",
  17. label: "Plan",
  18. },
  19. {
  20. description: "Ask before each file changes.",
  21. id: "build",
  22. label: "Build",
  23. },
  24. {
  25. description: "Full access with single-use confirmation for explicit dangerous commands.",
  26. id: "guarded",
  27. label: "Guarded",
  28. },
  29. {
  30. description: "Edit and run commands with fewer confirmations.",
  31. id: "yolo",
  32. label: "Yolo",
  33. },
  34. ];
  35. test("mode command popup filters mode choices from the composer draft", () => {
  36. assert.deepEqual(TUI_SWITCHABLE_MODES, ["plan", "build", "guarded", "yolo"]);
  37. assert.deepEqual(filterModeOptions("/mode").map((mode) => mode.id), TUI_SWITCHABLE_MODES);
  38. assert.equal(filterModeOptions("/mode edit").some((mode) => mode.id === "edit"), false);
  39. assert.equal(modeCommandQuery("/mode"), "");
  40. assert.equal(modeCommandQuery("/mode yo"), "yo");
  41. assert.equal(modeCommandQuery("/modebuild"), "build");
  42. assert.equal(modeCommandQuery("/model"), undefined);
  43. assert.deepEqual(
  44. filterModeOptions("/mode build").map((mode) => mode.id),
  45. ["build"],
  46. );
  47. assert.deepEqual(
  48. filterModeOptions("/mode guarded").map((mode) => mode.id),
  49. ["guarded"],
  50. );
  51. assert.deepEqual(
  52. filterModeOptions("/mode fewer").map((mode) => mode.id),
  53. ["yolo"],
  54. );
  55. });
  56. test("mode command popup selection completes and submits explicit mode commands", () => {
  57. let draft = "";
  58. assert.equal(
  59. completeModeCommand(modes, { selectedIndex: 3 }, (value) => {
  60. draft = value;
  61. }),
  62. true,
  63. );
  64. assert.equal(draft, "/mode yolo");
  65. assert.equal(
  66. completeModeCommand(modes, { selectedIndex: 2 }, (value) => {
  67. draft = value;
  68. }),
  69. true,
  70. );
  71. assert.equal(draft, "/mode guarded");
  72. assert.equal(
  73. selectedModeOption("/mode", { selectedIndex: 1 }, filterModeOptions("/mode"))?.id,
  74. "build",
  75. );
  76. });
  77. test("keeps selected mode option inside the visible window", () => {
  78. const legacyModes = modes.filter((mode) => mode.id !== "guarded");
  79. assert.deepEqual(visibleModeOptionWindow(legacyModes, 99, 2), {
  80. modes: legacyModes.slice(1, 3),
  81. selectedIndex: 1,
  82. startIndex: 1,
  83. });
  84. assert.deepEqual(visibleModeOptionWindow(modes, 99, 2), {
  85. modes: modes.slice(2, 4),
  86. selectedIndex: 1,
  87. startIndex: 2,
  88. });
  89. });
  90. test("renders mode suggestions with mode labels and muted descriptions", () => {
  91. const panel = ModeSuggestionPanel({
  92. contentWidth: 96,
  93. currentMode: "build",
  94. modes,
  95. selectedIndex: 1,
  96. });
  97. assert.ok(panel);
  98. const lines = collectTextNodes(panel);
  99. assert.ok(lines.some((line) => line.text === "> "));
  100. assert.ok(lines.some((line) => line.text === "Build"));
  101. assert.ok(lines.some((line) => line.text.includes("current")));
  102. assert.equal(lines.find((line) => line.text === "Build")?.style.fg, palette.accent);
  103. assert.equal(lines.find((line) => line.text.includes("current"))?.style.fg, palette.muted);
  104. });
  105. function collectTextNodes(node: unknown): Array<{ style: Record<string, unknown>; text: string }> {
  106. const lines: ReturnType<typeof collectTextNodes> = [];
  107. const visit = (value: unknown) => {
  108. if (Array.isArray(value)) {
  109. for (const child of value) visit(child);
  110. return;
  111. }
  112. if (typeof value !== "object" || value === null || !("props" in value)) return;
  113. const element = value as {
  114. props?: {
  115. children?: unknown;
  116. style?: Record<string, unknown>;
  117. };
  118. type?: unknown;
  119. };
  120. if (typeof element.type === "function") {
  121. visit(element.type(element.props));
  122. return;
  123. }
  124. if (element.type === "text") {
  125. lines.push({
  126. style: element.props?.style ?? {},
  127. text: collectTextLines(element.props?.children).join(""),
  128. });
  129. }
  130. visit(element.props?.children);
  131. };
  132. visit(node);
  133. return lines;
  134. }
  135. function collectTextLines(node: unknown): string[] {
  136. const lines: string[] = [];
  137. const visit = (value: unknown) => {
  138. if (typeof value === "string") {
  139. lines.push(value);
  140. return;
  141. }
  142. if (Array.isArray(value)) {
  143. for (const child of value) visit(child);
  144. return;
  145. }
  146. if (typeof value === "object" && value !== null && "props" in value) {
  147. visit((value as { props?: { children?: unknown } }).props?.children);
  148. }
  149. };
  150. visit(node);
  151. return lines;
  152. }