mentionPanel.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { describe, expect, it } from "vitest";
  2. import { createElement } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import enUS from "../src/i18n/locales/en-US.js";
  5. import zhCN from "../src/i18n/locales/zh-CN.js";
  6. import {
  7. buildVirtualRows,
  8. MentionPanel,
  9. SECTION_HEADER_CLASS_NAME,
  10. SECTION_HEADER_ROW_HEIGHT,
  11. type MentionPanelSection,
  12. } from "../src/mentions/components/MentionPanel.js";
  13. import { resolveVerticalScrollMaskState } from "../src/mentions/components/scrollMask.js";
  14. describe("mention search hint copy", () => {
  15. it("只提示插件、文件和对话,不再提及画板", () => {
  16. expect(zhCN["chat.mention.searchHint"]).toBe(
  17. "输入内容以搜索插件、文件或对话",
  18. );
  19. expect(enUS["chat.mention.searchHint"]).toBe(
  20. "Type to search plugins, files, or conversations",
  21. );
  22. });
  23. });
  24. describe("mention section header typography", () => {
  25. it("uses the base UI font size", () => {
  26. expect(SECTION_HEADER_CLASS_NAME).toContain("text-ui-base");
  27. expect(SECTION_HEADER_CLASS_NAME).not.toContain("text-ui-xs");
  28. });
  29. it("uses the secondary foreground color", () => {
  30. expect(SECTION_HEADER_CLASS_NAME).toContain("text-foreground-subtle");
  31. expect(SECTION_HEADER_CLASS_NAME).not.toContain("text-foreground-subtlest");
  32. });
  33. it("matches the command option row height without overflowing padding", () => {
  34. expect(SECTION_HEADER_ROW_HEIGHT).toBe(34);
  35. expect(SECTION_HEADER_CLASS_NAME).toContain("h-8");
  36. expect(SECTION_HEADER_CLASS_NAME).toContain("items-center");
  37. expect(SECTION_HEADER_CLASS_NAME).not.toMatch(/\b(?:pt|pb)-/);
  38. });
  39. });
  40. function makeOptions(count: number) {
  41. return Array.from({ length: count }, (_, i) => ({
  42. id: `opt-${i}`,
  43. label: `Option ${i}`,
  44. description: `Description ${i}`,
  45. }));
  46. }
  47. describe("buildVirtualRows", () => {
  48. it("returns empty array for empty sections", () => {
  49. expect(buildVirtualRows([])).toEqual([]);
  50. });
  51. it("produces one option row per option with correct flatOptionIndex", () => {
  52. const sections: MentionPanelSection[] = [
  53. { id: "files", title: "Files", options: makeOptions(3), emptyText: "No files" },
  54. ];
  55. const rows = buildVirtualRows(sections);
  56. expect(rows).toHaveLength(3);
  57. expect(rows.every((r) => r.kind === "option")).toBe(true);
  58. const indices = rows
  59. .filter((r): r is Extract<typeof r, { kind: "option" }> => r.kind === "option")
  60. .map((r) => r.flatOptionIndex);
  61. expect(indices).toEqual([0, 1, 2]);
  62. });
  63. it("shows loading status row when section is loading", () => {
  64. const sections: MentionPanelSection[] = [
  65. { id: "files", title: "Files", options: [], emptyText: "No files", loading: true, loadingText: "Loading..." },
  66. ];
  67. const rows = buildVirtualRows(sections);
  68. expect(rows).toHaveLength(1);
  69. expect(rows[0]).toMatchObject({ kind: "status", content: "loading", text: "Loading..." });
  70. });
  71. it("shows error status row when section has error", () => {
  72. const sections: MentionPanelSection[] = [
  73. { id: "files", title: "Files", options: [], emptyText: "No files", errorText: "Failed to load" },
  74. ];
  75. const rows = buildVirtualRows(sections);
  76. expect(rows).toHaveLength(1);
  77. expect(rows[0]).toMatchObject({ kind: "status", content: "error", text: "Failed to load" });
  78. });
  79. it("shows empty status row when section has no options and is not loading", () => {
  80. const sections: MentionPanelSection[] = [
  81. { id: "files", title: "Files", options: [], emptyText: "No files found" },
  82. ];
  83. const rows = buildVirtualRows(sections);
  84. expect(rows).toHaveLength(1);
  85. expect(rows[0]).toMatchObject({ kind: "status", content: "empty", text: "No files found" });
  86. });
  87. it("assigns continuous flatOptionIndex across multiple sections", () => {
  88. const sections: MentionPanelSection[] = [
  89. { id: "files", title: "Files", options: makeOptions(2), emptyText: "" },
  90. { id: "symbols", title: "Symbols", options: makeOptions(3), emptyText: "" },
  91. ];
  92. const rows = buildVirtualRows(sections);
  93. const optionRows = rows.filter(
  94. (r): r is Extract<typeof r, { kind: "option" }> => r.kind === "option",
  95. );
  96. expect(optionRows.map((r) => r.flatOptionIndex)).toEqual([0, 1, 2, 3, 4]);
  97. });
  98. it("handles large option sets efficiently", () => {
  99. const sections: MentionPanelSection[] = [
  100. { id: "files", title: "Files", options: makeOptions(1000), emptyText: "" },
  101. ];
  102. const start = performance.now();
  103. const rows = buildVirtualRows(sections);
  104. const elapsed = performance.now() - start;
  105. expect(rows).toHaveLength(1000);
  106. expect(elapsed).toBeLessThan(50);
  107. });
  108. it("renders the optional description after the virtualized list", () => {
  109. const html = renderToStaticMarkup(
  110. createElement(MentionPanel, {
  111. title: "Mentions",
  112. description: "Type to search",
  113. trigger: "@",
  114. sections: [
  115. {
  116. id: "files",
  117. title: "Files",
  118. options: makeOptions(1),
  119. emptyText: "No files",
  120. },
  121. ],
  122. emptyText: "",
  123. selectedIndex: 0,
  124. hasActiveQuery: false,
  125. onSelect: () => undefined,
  126. }),
  127. );
  128. expect(html.indexOf('role="listbox"')).toBeGreaterThanOrEqual(0);
  129. expect(html.indexOf("Type to search")).toBeGreaterThanOrEqual(0);
  130. expect(html.indexOf('role="listbox"')).toBeLessThan(
  131. html.indexOf("Type to search"),
  132. );
  133. });
  134. });
  135. describe("resolveVerticalScrollMaskState", () => {
  136. it("does not show masks when content does not overflow", () => {
  137. expect(
  138. resolveVerticalScrollMaskState({
  139. clientHeight: 100,
  140. scrollHeight: 100,
  141. scrollTop: 0,
  142. }),
  143. ).toEqual({ showBottom: false, showTop: false });
  144. });
  145. it("shows only the bottom mask at the top", () => {
  146. expect(
  147. resolveVerticalScrollMaskState({
  148. clientHeight: 100,
  149. scrollHeight: 240,
  150. scrollTop: 0,
  151. }),
  152. ).toEqual({ showBottom: true, showTop: false });
  153. });
  154. it("shows both masks in the middle", () => {
  155. expect(
  156. resolveVerticalScrollMaskState({
  157. clientHeight: 100,
  158. scrollHeight: 240,
  159. scrollTop: 64,
  160. }),
  161. ).toEqual({ showBottom: true, showTop: true });
  162. });
  163. it("shows only the top mask at the bottom", () => {
  164. expect(
  165. resolveVerticalScrollMaskState({
  166. clientHeight: 100,
  167. scrollHeight: 240,
  168. scrollTop: 140,
  169. }),
  170. ).toEqual({ showBottom: false, showTop: true });
  171. });
  172. });