subagentsMentionProvider.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import type { AgentSummary } from "@zcode/shared";
  5. const mockSubagentsState = vi.hoisted(() => ({
  6. agents: [] as AgentSummary[],
  7. loading: false,
  8. error: null as string | null,
  9. }));
  10. vi.mock("@/hooks/useSubagents.js", () => ({
  11. useSubagents: () => mockSubagentsState,
  12. }));
  13. import {
  14. mapSubagentsToMentionItemsForTest,
  15. useSubagentsMentionProvider,
  16. } from "../src/mentions/providers/subagentsMentionProvider.js";
  17. function createAgent(name: string): AgentSummary {
  18. return {
  19. id: `user:${name}`,
  20. name,
  21. description: `${name} description`,
  22. systemPrompt: `${name} prompt`,
  23. path: `/tmp/agents/${name}.md`,
  24. scope: "user",
  25. source: "user",
  26. enabled: true,
  27. };
  28. }
  29. function SubagentMentionProbe({ query }: { query: string }) {
  30. const result = useSubagentsMentionProvider(
  31. "/repo",
  32. "glm",
  33. query,
  34. true,
  35. "No subagents",
  36. "Subagents",
  37. );
  38. return createElement("output", null, result.items.map((item) => item.label).join(","));
  39. }
  40. describe("subagents mention provider", () => {
  41. it("only includes enabled subagents", () => {
  42. const items = mapSubagentsToMentionItemsForTest([
  43. {
  44. id: "agent-1",
  45. name: "code-review",
  46. description: "review code",
  47. path: "/repo/.claude/agents/code-review.md",
  48. scope: "workspace",
  49. source: "user",
  50. enabled: true,
  51. modelSelection: { providerId: "anthropic", modelId: "claude-sonnet" },
  52. },
  53. {
  54. id: "agent-2",
  55. name: "disabled-agent",
  56. description: "not available",
  57. path: "/home/user/.claude/agents/disabled.md",
  58. scope: "user",
  59. source: "user",
  60. enabled: false,
  61. },
  62. ]);
  63. expect(items).toHaveLength(1);
  64. expect(items[0]).toMatchObject({
  65. label: "code-review",
  66. value: "code-review",
  67. });
  68. });
  69. it("dedupes same-name subagents preferring workspace > user > plugin", () => {
  70. const items = mapSubagentsToMentionItemsForTest([
  71. {
  72. id: "agent-plugin",
  73. name: "reviewer",
  74. description: "plugin reviewer",
  75. path: "/plugins/reviewer/agent.md",
  76. scope: "user",
  77. source: "plugin",
  78. enabled: true,
  79. },
  80. {
  81. id: "agent-user",
  82. name: "reviewer",
  83. description: "user reviewer",
  84. path: "/home/user/.claude/agents/reviewer.md",
  85. scope: "user",
  86. source: "user",
  87. enabled: true,
  88. },
  89. {
  90. id: "agent-workspace",
  91. name: "reviewer",
  92. description: "workspace reviewer",
  93. path: "/repo/.claude/agents/reviewer.md",
  94. scope: "workspace",
  95. source: "user",
  96. enabled: true,
  97. },
  98. ]);
  99. expect(items).toHaveLength(1);
  100. expect(items[0]).toMatchObject({
  101. label: "reviewer",
  102. description: "Workspace · workspace reviewer",
  103. data: {
  104. scope: "workspace",
  105. path: "/repo/.claude/agents/reviewer.md",
  106. },
  107. });
  108. });
  109. it("builds description with source label and agent description", () => {
  110. const items = mapSubagentsToMentionItemsForTest([
  111. {
  112. id: "agent-1",
  113. name: "browser",
  114. description: "browse the web",
  115. path: "/home/user/.claude/agents/browser.md",
  116. scope: "user",
  117. source: "user",
  118. enabled: true,
  119. modelSelection: { providerId: "anthropic", modelId: "claude-sonnet" },
  120. },
  121. ]);
  122. expect(items[0].description).toBe("User · browse the web");
  123. });
  124. it("uses source label alone when description is empty", () => {
  125. const items = mapSubagentsToMentionItemsForTest([
  126. {
  127. id: "agent-1",
  128. name: "browser",
  129. description: "",
  130. path: "/plugins/browser/agent.md",
  131. scope: "user",
  132. source: "plugin",
  133. enabled: true,
  134. },
  135. ]);
  136. expect(items[0].description).toBe("Plugin");
  137. });
  138. it("includes name, description, scope, source, model, and path as keywords", () => {
  139. const items = mapSubagentsToMentionItemsForTest([
  140. {
  141. id: "agent-1",
  142. name: "code-review",
  143. description: "review code quality",
  144. path: "/repo/.claude/agents/review.md",
  145. scope: "workspace",
  146. source: "user",
  147. enabled: true,
  148. modelSelection: { providerId: "zai", modelId: "glm-5.2" },
  149. },
  150. ]);
  151. expect(items[0].keywords).toContain("code-review");
  152. expect(items[0].keywords).toContain("review code quality");
  153. expect(items[0].keywords).toContain("workspace");
  154. expect(items[0].keywords).toContain("user");
  155. expect(items[0].keywords).toContain("Workspace");
  156. expect(items[0].keywords).toContain("zai/glm-5.2");
  157. expect(items[0].keywords).toContain("/repo/.claude/agents/review.md");
  158. });
  159. it("generates markdown as @agent-name", () => {
  160. const items = mapSubagentsToMentionItemsForTest([
  161. {
  162. id: "agent-1",
  163. name: "code-review",
  164. description: "review code",
  165. path: "/repo/.claude/agents/review.md",
  166. scope: "workspace",
  167. source: "user",
  168. enabled: true,
  169. },
  170. ]);
  171. expect(items[0].markdown).toBe("@code-review");
  172. expect(items[0].value).toBe("code-review");
  173. });
  174. it("skips agents with empty names", () => {
  175. const items = mapSubagentsToMentionItemsForTest([
  176. {
  177. id: "agent-1",
  178. name: " ",
  179. description: "empty name",
  180. path: "/repo/.claude/agents/empty.md",
  181. scope: "workspace",
  182. source: "user",
  183. enabled: true,
  184. },
  185. ]);
  186. expect(items).toHaveLength(0);
  187. });
  188. it("shows all enabled subagents before the user types a search query", () => {
  189. mockSubagentsState.agents = [
  190. createAgent("agent-one"),
  191. createAgent("agent-two"),
  192. createAgent("agent-three"),
  193. createAgent("agent-four"),
  194. createAgent("agent-five"),
  195. ];
  196. mockSubagentsState.loading = false;
  197. mockSubagentsState.error = null;
  198. const html = renderToStaticMarkup(createElement(SubagentMentionProbe, { query: "" }));
  199. expect(html).toContain("agent-one,agent-two,agent-three,agent-four,agent-five");
  200. });
  201. });