slashCommandHelpers.test.ts 9.0 KB

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