quickPickCommands.test.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import { describe, expect, it, vi } from "vitest";
  2. import enUS from "@/i18n/locales/en-US.js";
  3. import zhCN from "@/i18n/locales/zh-CN.js";
  4. import {
  5. createQuickPickCommands,
  6. QUICK_PICK_SECTION_ORDER,
  7. } from "@/quickpick/quickPickCommands.js";
  8. function createHandlers() {
  9. return {
  10. createTask: vi.fn(),
  11. openWorkspace: vi.fn(),
  12. openSettings: vi.fn(),
  13. openSkillsSettings: vi.fn(),
  14. openMcpSettings: vi.fn(),
  15. switchTheme: vi.fn(),
  16. openFeedback: vi.fn(),
  17. openCommunity: vi.fn(),
  18. openProductDocs: vi.fn(),
  19. login: vi.fn(),
  20. logout: vi.fn(),
  21. toggleSidebar: vi.fn(),
  22. toggleTerminal: vi.fn(),
  23. togglePreview: vi.fn(),
  24. openTerminalTab: vi.fn(),
  25. openBrowserTab: vi.fn(),
  26. openReviewTab: vi.fn(),
  27. };
  28. }
  29. const shortcuts = {
  30. newTask: "⌘ N",
  31. openWorkspace: "⌘ O",
  32. toggleSidebar: "⌘ B",
  33. toggleTerminal: "⌘ J",
  34. };
  35. describe("createQuickPickCommands", () => {
  36. it("通用模式过滤技术面板命令并保留浏览器和普通命令", () => {
  37. const commands = createQuickPickCommands({
  38. allowOpenWorkspace: true,
  39. canOpenCommunity: true,
  40. isSidebarVisible: true,
  41. isLoggedIn: false,
  42. supportsTerminal: false,
  43. supportsReview: false,
  44. themeTarget: "dark",
  45. shortcuts,
  46. handlers: createHandlers(),
  47. });
  48. const ids = commands.map((command) => command.id);
  49. expect(ids).not.toContain("toggle-terminal");
  50. expect(ids).not.toContain("add-terminal-tab");
  51. expect(ids).not.toContain("add-review-tab");
  52. expect(ids).toContain("add-browser-tab");
  53. expect(ids).toContain("new-task");
  54. expect(ids).toContain("settings");
  55. });
  56. it("creates the starter command registry in stable sections", () => {
  57. const commands = createQuickPickCommands({
  58. allowOpenWorkspace: true,
  59. canOpenCommunity: true,
  60. isSidebarVisible: true,
  61. isLoggedIn: false,
  62. themeTarget: "dark",
  63. shortcuts,
  64. handlers: createHandlers(),
  65. });
  66. expect(commands.map((command) => command.id)).toEqual([
  67. "new-task",
  68. "open-workspace",
  69. "suggested-settings",
  70. "toggle-sidebar",
  71. "toggle-terminal",
  72. "toggle-preview",
  73. "add-terminal-tab",
  74. "add-browser-tab",
  75. "add-review-tab",
  76. "settings",
  77. "switch-theme",
  78. "skills-settings",
  79. "mcp-settings",
  80. "feedback",
  81. "community",
  82. "product-docs",
  83. "login",
  84. ]);
  85. expect(commands.find((command) => command.id === "open-workspace")).toMatchObject({
  86. sectionId: "suggested",
  87. shortcut: "⌘ O",
  88. disabled: false,
  89. });
  90. expect(commands.find((command) => command.id === "new-task")).toMatchObject({
  91. sectionId: "suggested",
  92. });
  93. expect(commands.find((command) => command.id === "suggested-settings")).toMatchObject({
  94. sectionId: "suggested",
  95. titleId: "quickPick.command.settings",
  96. icon: "settings",
  97. });
  98. expect(commands.find((command) => command.id === "settings")).toMatchObject({
  99. sectionId: "configure",
  100. });
  101. expect(commands.find((command) => command.id === "switch-theme")).toMatchObject({
  102. sectionId: "configure",
  103. titleId: "quickPick.command.switchThemeToDark",
  104. icon: "themeDark",
  105. });
  106. expect(commands.find((command) => command.id === "skills-settings")).toMatchObject({
  107. sectionId: "configure",
  108. titleId: "quickPick.command.skills",
  109. icon: "skills",
  110. });
  111. expect(commands.find((command) => command.id === "mcp-settings")).toMatchObject({
  112. sectionId: "configure",
  113. titleId: "quickPick.command.mcpServers",
  114. });
  115. expect(commands.find((command) => command.id === "toggle-sidebar")).toMatchObject({
  116. sectionId: "panels",
  117. shortcut: "⌘ B",
  118. icon: "sidebarClose",
  119. });
  120. expect(commands.find((command) => command.id === "toggle-terminal")).toMatchObject({
  121. sectionId: "panels",
  122. });
  123. expect(commands.find((command) => command.id === "toggle-preview")).toMatchObject({
  124. sectionId: "panels",
  125. titleId: "quickPick.command.togglePreview",
  126. icon: "browser",
  127. });
  128. expect(commands.find((command) => command.id === "add-terminal-tab")).toMatchObject({
  129. sectionId: "panels",
  130. titleId: "quickPick.command.addTerminalTab",
  131. icon: "terminal",
  132. });
  133. expect(commands.find((command) => command.id === "add-browser-tab")).toMatchObject({
  134. sectionId: "panels",
  135. titleId: "quickPick.command.addBrowserTab",
  136. icon: "browser",
  137. });
  138. expect(commands.find((command) => command.id === "add-review-tab")).toMatchObject({
  139. sectionId: "panels",
  140. titleId: "quickPick.command.addReviewTab",
  141. icon: "diff",
  142. });
  143. expect(commands.find((command) => command.id === "product-docs")).toMatchObject({
  144. sectionId: "app",
  145. titleId: "quickPick.command.productDocs",
  146. icon: "book",
  147. });
  148. });
  149. it("keeps unavailable workspace opening visible but disabled", () => {
  150. const commands = createQuickPickCommands({
  151. allowOpenWorkspace: false,
  152. canOpenCommunity: true,
  153. isSidebarVisible: true,
  154. isLoggedIn: false,
  155. themeTarget: "dark",
  156. shortcuts,
  157. handlers: createHandlers(),
  158. });
  159. expect(commands.find((command) => command.id === "open-workspace")).toMatchObject({
  160. disabled: true,
  161. });
  162. });
  163. it("matches the sidebar toggle icon to the current sidebar state", () => {
  164. const closedSidebarCommands = createQuickPickCommands({
  165. allowOpenWorkspace: true,
  166. canOpenCommunity: true,
  167. isSidebarVisible: false,
  168. isLoggedIn: false,
  169. themeTarget: "dark",
  170. shortcuts,
  171. handlers: createHandlers(),
  172. });
  173. expect(closedSidebarCommands.find((command) => command.id === "toggle-sidebar")).toMatchObject({
  174. icon: "sidebarOpen",
  175. });
  176. });
  177. it("omits embedded browser commands when the shell does not support browser tabs", () => {
  178. const commands = createQuickPickCommands({
  179. allowOpenWorkspace: true,
  180. canOpenCommunity: true,
  181. isSidebarVisible: true,
  182. isLoggedIn: false,
  183. supportsEmbeddedBrowser: false,
  184. themeTarget: "dark",
  185. shortcuts,
  186. handlers: createHandlers(),
  187. });
  188. expect(commands.map((command) => command.id)).not.toContain("toggle-preview");
  189. expect(commands.map((command) => command.id)).not.toContain("add-browser-tab");
  190. });
  191. it("runs the registered panel handler", () => {
  192. const handlers = createHandlers();
  193. const command = createQuickPickCommands({
  194. allowOpenWorkspace: true,
  195. canOpenCommunity: true,
  196. isSidebarVisible: true,
  197. isLoggedIn: false,
  198. themeTarget: "dark",
  199. shortcuts,
  200. handlers,
  201. }).find((item) => item.id === "add-browser-tab");
  202. command?.run();
  203. expect(handlers.openBrowserTab).toHaveBeenCalledTimes(1);
  204. });
  205. it("runs configure and app handlers", () => {
  206. const handlers = createHandlers();
  207. const commands = createQuickPickCommands({
  208. allowOpenWorkspace: true,
  209. canOpenCommunity: true,
  210. isSidebarVisible: true,
  211. isLoggedIn: false,
  212. themeTarget: "dark",
  213. shortcuts,
  214. handlers,
  215. });
  216. commands.find((item) => item.id === "skills-settings")?.run();
  217. commands.find((item) => item.id === "product-docs")?.run();
  218. expect(handlers.openSkillsSettings).toHaveBeenCalledTimes(1);
  219. expect(handlers.openProductDocs).toHaveBeenCalledTimes(1);
  220. });
  221. it("has locale messages for every visible command label", () => {
  222. const commands = createQuickPickCommands({
  223. allowOpenWorkspace: true,
  224. canOpenCommunity: true,
  225. isSidebarVisible: true,
  226. isLoggedIn: false,
  227. themeTarget: "light",
  228. shortcuts,
  229. handlers: createHandlers(),
  230. });
  231. expect(commands.find((command) => command.id === "switch-theme")).toMatchObject({
  232. titleId: "quickPick.command.switchThemeToLight",
  233. icon: "themeLight",
  234. });
  235. const messageIds = new Set([
  236. "quickPick.title",
  237. "quickPick.description",
  238. "quickPick.placeholder",
  239. "quickPick.empty",
  240. "quickPick.commandFailed",
  241. "commandCenter.scopeTabs",
  242. "commandCenter.scope.all",
  243. "commandCenter.scope.commands",
  244. "commandCenter.scope.conversations",
  245. "commandCenter.scope.files",
  246. "commandCenter.section.recentChanges",
  247. "commandCenter.section.recentTasks",
  248. "commandCenter.empty.recentChanges",
  249. "commandCenter.empty.recentTasks",
  250. ...commands.map((command) => command.titleId),
  251. ...QUICK_PICK_SECTION_ORDER.map((sectionId) => `quickPick.section.${sectionId}`),
  252. ]);
  253. for (const id of messageIds) {
  254. expect(zhCN[id], `zh-CN missing ${id}`).toBeTruthy();
  255. expect(enUS[id], `en-US missing ${id}`).toBeTruthy();
  256. }
  257. });
  258. it("keeps the command section taxonomy stable", () => {
  259. expect(QUICK_PICK_SECTION_ORDER).toEqual([
  260. "suggested",
  261. "chat",
  262. "navigation",
  263. "panels",
  264. "configure",
  265. "app",
  266. ]);
  267. });
  268. it("uses logout instead of login for authenticated users", () => {
  269. const commands = createQuickPickCommands({
  270. allowOpenWorkspace: true,
  271. canOpenCommunity: false,
  272. isSidebarVisible: true,
  273. isLoggedIn: true,
  274. themeTarget: "dark",
  275. shortcuts,
  276. handlers: createHandlers(),
  277. });
  278. expect(commands.some((command) => command.id === "login")).toBe(false);
  279. expect(commands.find((command) => command.id === "logout")).toMatchObject({
  280. sectionId: "app",
  281. titleId: "quickPick.command.logout",
  282. });
  283. expect(commands.some((command) => command.id === "community")).toBe(false);
  284. });
  285. it("uses connection wording for app auth commands", () => {
  286. expect(zhCN["quickPick.command.login"]).toBe("连接");
  287. expect(zhCN["quickPick.command.logout"]).toBe("断开连接");
  288. expect(enUS["quickPick.command.login"]).toBe("Connect");
  289. expect(enUS["quickPick.command.logout"]).toBe("Disconnect");
  290. });
  291. });