tui-file-mentions.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import { getZCodeCopy } from "@zcode/i18n";
  4. import { toPromptInput } from "../src/app-input.js";
  5. import { FileMentionPanel } from "../src/app-file-mention-panel.js";
  6. import {
  7. promptEditorCursorOffset,
  8. resolveActiveFileMention,
  9. visibleFileMentionWindow,
  10. type FileMentionState,
  11. } from "../src/app-file-mentions.js";
  12. import { palette } from "../src/app-model.js";
  13. test("resolves active file mention tokens at the prompt cursor", () => {
  14. assert.deepEqual(resolveActiveFileMention("@pu"), {
  15. endOffset: 3,
  16. token: "pu",
  17. triggerIndex: 0,
  18. });
  19. assert.deepEqual(resolveActiveFileMention("open @public/ap"), {
  20. endOffset: 15,
  21. token: "public/ap",
  22. triggerIndex: 5,
  23. });
  24. assert.equal(resolveActiveFileMention("email me@work"), undefined);
  25. assert.equal(resolveActiveFileMention("open @public/app now"), undefined);
  26. });
  27. test("keeps selected file mention inside the visible window", () => {
  28. const items = Array.from({ length: 10 }, (_, index) => ({
  29. kind: "file" as const,
  30. path: `src/file-${index}.ts`,
  31. }));
  32. assert.deepEqual(visibleFileMentionWindow(items, 7, 8), {
  33. items: items.slice(0, 8),
  34. selectedIndex: 7,
  35. startIndex: 0,
  36. });
  37. assert.deepEqual(visibleFileMentionWindow(items, 9, 8), {
  38. items: items.slice(2, 10),
  39. selectedIndex: 7,
  40. startIndex: 2,
  41. });
  42. });
  43. test("falls back to the draft end when the prompt editor is stale", () => {
  44. const staleEditor = {
  45. get cursorOffset(): number {
  46. throw new Error("EditorView is destroyed");
  47. },
  48. };
  49. assert.equal(promptEditorCursorOffset(staleEditor, 12), 12);
  50. });
  51. test("submits only file mention attachments that remain in the draft", () => {
  52. assert.deepEqual(
  53. toPromptInput("read @src/app.ts", [
  54. {
  55. id: 1,
  56. path: "src/app.ts",
  57. placeholder: "@src/app.ts",
  58. type: "file",
  59. },
  60. {
  61. id: 2,
  62. path: "src/removed.ts",
  63. placeholder: "@src/removed.ts",
  64. type: "file",
  65. },
  66. ]),
  67. {
  68. attachments: [{ path: "src/app.ts", type: "file" }],
  69. text: "read @src/app.ts",
  70. },
  71. );
  72. });
  73. test("renders file mention suggestions with wrapped rows", () => {
  74. const panel = FileMentionPanel({
  75. contentWidth: 28,
  76. copy: getZCodeCopy("en-US").tui,
  77. state: {
  78. endOffset: 3,
  79. items: [
  80. { kind: "directory", path: "public/" },
  81. { kind: "file", path: "public/very-long-file-name-that-wraps.ts" },
  82. ],
  83. loading: false,
  84. selectedIndex: 1,
  85. token: "pu",
  86. triggerIndex: 0,
  87. truncated: false,
  88. },
  89. });
  90. assert.ok(panel);
  91. const textNodes = collectTextNodes(panel);
  92. assert.equal(textNodes[0]?.text, " public/");
  93. assert.equal(textNodes[1]?.text, "> public/very-long-file-name-that-wraps.ts");
  94. assert.equal(textNodes[0]?.style.fg, palette.text);
  95. assert.equal(textNodes[1]?.style.fg, palette.accent);
  96. assert.equal((textNodes[1]?.style.height as number) > 1, true);
  97. assert.equal(textNodes[1]?.style.wrapMode, "word");
  98. });
  99. test("renders a loading file mention state", () => {
  100. const panel = FileMentionPanel({
  101. copy: getZCodeCopy("en-US").tui,
  102. state: emptyMentionState({ loading: true }),
  103. });
  104. assert.ok(collectTextNodes(panel).some((node) => node.text === "Loading workspace paths..."));
  105. });
  106. function emptyMentionState(input: { loading: boolean }): FileMentionState {
  107. return {
  108. endOffset: 1,
  109. items: [],
  110. loading: input.loading,
  111. selectedIndex: 0,
  112. token: "",
  113. triggerIndex: 0,
  114. truncated: false,
  115. };
  116. }
  117. function collectTextNodes(node: unknown): Array<{ style: Record<string, unknown>; text: string }> {
  118. const lines: ReturnType<typeof collectTextNodes> = [];
  119. const visit = (value: unknown) => {
  120. if (Array.isArray(value)) {
  121. for (const child of value) visit(child);
  122. return;
  123. }
  124. if (typeof value !== "object" || value === null || !("props" in value)) return;
  125. const element = value as {
  126. props?: {
  127. children?: unknown;
  128. style?: Record<string, unknown>;
  129. };
  130. type?: unknown;
  131. };
  132. if (element.type === "text") {
  133. const text = collectTextLines(element.props?.children).join("");
  134. lines.push({ style: element.props?.style ?? {}, text });
  135. }
  136. visit(element.props?.children);
  137. };
  138. visit(node);
  139. return lines;
  140. }
  141. function collectTextLines(node: unknown): string[] {
  142. const lines: string[] = [];
  143. const visit = (value: unknown) => {
  144. if (typeof value === "string") {
  145. lines.push(value);
  146. return;
  147. }
  148. if (Array.isArray(value)) {
  149. for (const child of value) visit(child);
  150. return;
  151. }
  152. if (typeof value === "object" && value !== null && "props" in value) {
  153. const element = value as { props?: { children?: unknown } };
  154. visit(element.props?.children);
  155. }
  156. };
  157. visit(node);
  158. return lines;
  159. }