slashCommandHelpers.test.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import { describe, expect, it } from "vitest";
  2. import type { ZCodeSlashCommand } from "@zcode/shared";
  3. import {
  4. buildAppSlashCommandSuggestions,
  5. buildSkillSuggestions,
  6. buildSubagentSuggestions,
  7. buildSlashSuggestions,
  8. isAppSlashCommandSuggestion,
  9. replaceActiveSlashToken,
  10. shouldOfferSideSlashCommand,
  11. } from "../src/slashCommandHelpers.js";
  12. describe("slashCommandHelpers", () => {
  13. it("buildSlashSuggestions 逐项展示 CLI catalog,不在 UI 维护内建白名单", () => {
  14. const commands: ZCodeSlashCommand[] = [
  15. {
  16. name: "review",
  17. description: "run review",
  18. inputHint: "/review [scope]",
  19. source: "custom",
  20. },
  21. {
  22. name: "goal",
  23. description: "set goal",
  24. source: "builtin",
  25. },
  26. {
  27. name: "compact",
  28. description: "compact context",
  29. source: "builtin",
  30. },
  31. {
  32. name: "init",
  33. description: "create AGENTS.md",
  34. inputHint: "/init [notes]",
  35. source: "builtin",
  36. },
  37. {
  38. name: "model",
  39. description: "switch model",
  40. source: "builtin",
  41. },
  42. ];
  43. const result = buildSlashSuggestions(commands);
  44. expect(result).toEqual([
  45. {
  46. id: "slash:review",
  47. trigger: "/",
  48. value: "review",
  49. label: "/review [scope]",
  50. description: "run review",
  51. keywords: ["review", "run review", "/review [scope]"],
  52. },
  53. {
  54. id: "slash:goal",
  55. trigger: "/",
  56. value: "goal",
  57. label: "/goal",
  58. description: "set goal",
  59. keywords: ["goal", "set goal", ""],
  60. },
  61. {
  62. id: "slash:compact",
  63. trigger: "/",
  64. value: "compact",
  65. label: "/compact",
  66. description: "compact context",
  67. keywords: ["compact", "compact context", ""],
  68. },
  69. {
  70. id: "slash:init",
  71. trigger: "/",
  72. value: "init",
  73. label: "/init [notes]",
  74. description: "create AGENTS.md",
  75. keywords: ["init", "create AGENTS.md", "/init [notes]"],
  76. },
  77. {
  78. id: "slash:model",
  79. trigger: "/",
  80. value: "model",
  81. label: "/model",
  82. description: "switch model",
  83. keywords: ["model", "switch model", ""],
  84. },
  85. ]);
  86. });
  87. it("buildSlashSuggestions 不按旧协议 source 字段过滤 CLI catalog", () => {
  88. const result = buildSlashSuggestions([
  89. {
  90. name: "review",
  91. description: "legacy custom without source",
  92. },
  93. {
  94. name: "compact",
  95. description: "Compact context",
  96. },
  97. ]);
  98. expect(result.map((item) => item.value)).toEqual(["review", "compact"]);
  99. });
  100. it("buildSlashSuggestions 会规范化已带 / 的 ZCode Agent 命令名", () => {
  101. const result = buildSlashSuggestions([
  102. {
  103. name: "/compact",
  104. description: "Compact context",
  105. },
  106. ]);
  107. expect(result).toEqual([
  108. {
  109. id: "slash:compact",
  110. trigger: "/",
  111. value: "compact",
  112. label: "/compact",
  113. description: "Compact context",
  114. keywords: ["compact", "/compact", "Compact context", ""],
  115. },
  116. ]);
  117. });
  118. it("buildSlashSuggestions 不为缺失的 CLI catalog 补任何命令", () => {
  119. expect(buildSlashSuggestions([])).toEqual([]);
  120. });
  121. it("buildSubagentSuggestions 只包含 enabled subagents", () => {
  122. const result = buildSubagentSuggestions([
  123. {
  124. id: "a1",
  125. name: "reviewer",
  126. description: "review code",
  127. path: "./agents/reviewer.md",
  128. scope: "workspace",
  129. source: "user",
  130. enabled: true,
  131. },
  132. {
  133. id: "a2",
  134. name: "disabled",
  135. description: "hidden",
  136. path: "./agents/disabled.md",
  137. scope: "user",
  138. source: "user",
  139. enabled: false,
  140. },
  141. ]);
  142. expect(result).toEqual([
  143. expect.objectContaining({
  144. id: "subagent:a1",
  145. trigger: "/",
  146. value: "reviewer",
  147. }),
  148. ]);
  149. });
  150. it("buildSubagentSuggestions 复用 mention provider 的同名折叠优先级", () => {
  151. const result = buildSubagentSuggestions([
  152. {
  153. id: "agent-plugin",
  154. name: "reviewer",
  155. description: "plugin reviewer",
  156. path: "/plugins/reviewer/agent.md",
  157. scope: "user",
  158. source: "plugin",
  159. enabled: true,
  160. },
  161. {
  162. id: "agent-workspace",
  163. name: "reviewer",
  164. description: "workspace reviewer",
  165. path: "/repo/.claude/agents/reviewer.md",
  166. scope: "workspace",
  167. source: "user",
  168. enabled: true,
  169. },
  170. ]);
  171. expect(result).toHaveLength(1);
  172. expect(result[0]).toMatchObject({
  173. id: "subagent:agent-workspace",
  174. value: "reviewer",
  175. description: "Workspace · workspace reviewer",
  176. data: {
  177. path: "/repo/.claude/agents/reviewer.md",
  178. scope: "workspace",
  179. },
  180. });
  181. });
  182. it("buildSkillSuggestions 让 / 面板复用 $ 技能展示语义", () => {
  183. const result = buildSkillSuggestions(
  184. [
  185. {
  186. id: "glm:workspace:code-review",
  187. name: "code-review",
  188. description: "review code",
  189. path: "/repo/.zcode/skills/code-review/SKILL.md",
  190. scope: "workspace",
  191. pluginName: undefined,
  192. },
  193. ],
  194. "en-US",
  195. );
  196. expect(result).toEqual([
  197. {
  198. id: "skill:glm:workspace:code-review",
  199. trigger: "/",
  200. value: "code-review",
  201. label: "$code-review",
  202. description: "Workspace · review code",
  203. keywords: ["code-review", "review code", "workspace", "skill", "skills"],
  204. data: {
  205. path: "/repo/.zcode/skills/code-review/SKILL.md",
  206. scope: "workspace",
  207. },
  208. },
  209. ]);
  210. });
  211. it("replaceActiveSlashToken 支持把 /token 替换成 $skill", () => {
  212. expect(replaceActiveSlashToken("请执行 /cod", "e-review 这里", "$code-review")).toEqual({
  213. cursorOffset: 17,
  214. text: "请执行 $code-review 这里",
  215. });
  216. });
  217. it("replaceActiveSlashToken 不把完整 token 后面的正文当 tail 删除", () => {
  218. expect(replaceActiveSlashToken("请执行 /goal", "修复这个问题", "/goal")).toEqual({
  219. cursorOffset: 10,
  220. text: "请执行 /goal 修复这个问题",
  221. });
  222. });
  223. // SSC28:App 层 `/side` 命令由渲染层注入,不写入 CLI catalog。
  224. it("buildAppSlashCommandSuggestions 生成 App 层命令建议,关键词含中英文别名", () => {
  225. const noop = () => {};
  226. const result = buildAppSlashCommandSuggestions([
  227. {
  228. value: "side",
  229. description: "新建并打开一个辅助对话",
  230. keywords: ["side", "辅助对话", "辅助"],
  231. run: noop,
  232. },
  233. ]);
  234. expect(result).toEqual([
  235. {
  236. id: "app-slash:side",
  237. trigger: "/",
  238. value: "side",
  239. label: "/side",
  240. description: "新建并打开一个辅助对话",
  241. keywords: ["side", "新建并打开一个辅助对话", "辅助对话", "辅助"],
  242. },
  243. ]);
  244. expect(isAppSlashCommandSuggestion(result[0]!)).toBe(true);
  245. });
  246. it("App 层命令与 CLI 命令的建议 id 前缀互不冲突", () => {
  247. const cli = buildSlashSuggestions([{ name: "side", description: "cli side" }]);
  248. expect(isAppSlashCommandSuggestion(cli[0]!)).toBe(false);
  249. });
  250. // SSC28:/btw 是 /side 的等价别名,面板中各自独立展示。
  251. it("buildAppSlashCommandSuggestions 支持 /btw 等价别名独立展示", () => {
  252. const run = () => {};
  253. const shared = { description: "新建并打开一个辅助对话", keywords: ["side", "btw"], run };
  254. const result = buildAppSlashCommandSuggestions([
  255. { value: "side", ...shared },
  256. { value: "btw", ...shared },
  257. ]);
  258. expect(result.map((item) => ({ id: item.id, label: item.label }))).toEqual([
  259. { id: "app-slash:side", label: "/side" },
  260. { id: "app-slash:btw", label: "/btw" },
  261. ]);
  262. expect(result.every((item) => isAppSlashCommandSuggestion(item))).toBe(true);
  263. });
  264. // SSC29:草稿态、辅助对话自身、只读与手机 viewport 不提供 `/side`。
  265. it("shouldOfferSideSlashCommand 门禁只在主会话可用", () => {
  266. const base = {
  267. isDraft: false,
  268. selectionSideChat: false,
  269. readOnly: false,
  270. isMobileViewport: false,
  271. };
  272. expect(shouldOfferSideSlashCommand(base)).toBe(true);
  273. expect(shouldOfferSideSlashCommand({ ...base, isDraft: true })).toBe(false);
  274. expect(shouldOfferSideSlashCommand({ ...base, selectionSideChat: true })).toBe(false);
  275. expect(shouldOfferSideSlashCommand({ ...base, readOnly: true })).toBe(false);
  276. expect(shouldOfferSideSlashCommand({ ...base, isMobileViewport: true })).toBe(false);
  277. });
  278. });