quickPickCommands.test.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. });
  96. expect(commands.find((command) => command.id === "settings")).toMatchObject({
  97. sectionId: "configure",
  98. });
  99. expect(commands.find((command) => command.id === "switch-theme")).toMatchObject({
  100. sectionId: "configure",
  101. titleId: "quickPick.command.switchThemeToDark",
  102. icon: "themeDark",
  103. });
  104. expect(commands.find((command) => command.id === "skills-settings")).toMatchObject({
  105. sectionId: "configure",
  106. });
  107. expect(commands.find((command) => command.id === "mcp-settings")).toMatchObject({
  108. sectionId: "configure",
  109. });
  110. expect(commands.find((command) => command.id === "toggle-sidebar")).toMatchObject({
  111. sectionId: "panels",
  112. shortcut: "⌘ B",
  113. icon: "sidebarClose",
  114. });
  115. expect(commands.find((command) => command.id === "toggle-terminal")).toMatchObject({
  116. sectionId: "panels",
  117. });
  118. expect(commands.find((command) => command.id === "toggle-preview")).toMatchObject({
  119. sectionId: "panels",
  120. });
  121. expect(commands.find((command) => command.id === "add-terminal-tab")).toMatchObject({
  122. sectionId: "panels",
  123. });
  124. expect(commands.find((command) => command.id === "add-browser-tab")).toMatchObject({
  125. sectionId: "panels",
  126. });
  127. expect(commands.find((command) => command.id === "add-review-tab")).toMatchObject({
  128. sectionId: "panels",
  129. });
  130. expect(commands.find((command) => command.id === "product-docs")).toMatchObject({
  131. sectionId: "app",
  132. });
  133. });
  134. it("keeps unavailable workspace opening visible but disabled", () => {
  135. const commands = createQuickPickCommands({
  136. allowOpenWorkspace: false,
  137. canOpenCommunity: true,
  138. isSidebarVisible: true,
  139. isLoggedIn: false,
  140. themeTarget: "dark",
  141. shortcuts,
  142. handlers: createHandlers(),
  143. });
  144. expect(commands.find((command) => command.id === "open-workspace")).toMatchObject({
  145. disabled: true,
  146. });
  147. });
  148. it("matches the sidebar toggle icon to the current sidebar state", () => {
  149. const closedSidebarCommands = createQuickPickCommands({
  150. allowOpenWorkspace: true,
  151. canOpenCommunity: true,
  152. isSidebarVisible: false,
  153. isLoggedIn: false,
  154. themeTarget: "dark",
  155. shortcuts,
  156. handlers: createHandlers(),
  157. });
  158. expect(closedSidebarCommands.find((command) => command.id === "toggle-sidebar")).toMatchObject({
  159. icon: "sidebarOpen",
  160. });
  161. });
  162. it("omits embedded browser commands when the shell does not support browser tabs", () => {
  163. const commands = createQuickPickCommands({
  164. allowOpenWorkspace: true,
  165. canOpenCommunity: true,
  166. isSidebarVisible: true,
  167. isLoggedIn: false,
  168. supportsEmbeddedBrowser: false,
  169. themeTarget: "dark",
  170. shortcuts,
  171. handlers: createHandlers(),
  172. });
  173. expect(commands.map((command) => command.id)).not.toContain("toggle-preview");
  174. expect(commands.map((command) => command.id)).not.toContain("add-browser-tab");
  175. });
  176. it("runs the registered panel handler", () => {
  177. const handlers = createHandlers();
  178. const command = createQuickPickCommands({
  179. allowOpenWorkspace: true,
  180. canOpenCommunity: true,
  181. isSidebarVisible: true,
  182. isLoggedIn: false,
  183. themeTarget: "dark",
  184. shortcuts,
  185. handlers,
  186. }).find((item) => item.id === "add-browser-tab");
  187. command?.run();
  188. expect(handlers.openBrowserTab).toHaveBeenCalledTimes(1);
  189. });
  190. it("runs configure and app handlers", () => {
  191. const handlers = createHandlers();
  192. const commands = createQuickPickCommands({
  193. allowOpenWorkspace: true,
  194. canOpenCommunity: true,
  195. isSidebarVisible: true,
  196. isLoggedIn: false,
  197. themeTarget: "dark",
  198. shortcuts,
  199. handlers,
  200. });
  201. commands.find((item) => item.id === "skills-settings")?.run();
  202. commands.find((item) => item.id === "product-docs")?.run();
  203. expect(handlers.openSkillsSettings).toHaveBeenCalledTimes(1);
  204. expect(handlers.openProductDocs).toHaveBeenCalledTimes(1);
  205. });
  206. it("has locale messages for every visible command label", () => {
  207. const commands = createQuickPickCommands({
  208. allowOpenWorkspace: true,
  209. canOpenCommunity: true,
  210. isSidebarVisible: true,
  211. isLoggedIn: false,
  212. themeTarget: "light",
  213. shortcuts,
  214. handlers: createHandlers(),
  215. });
  216. expect(commands.find((command) => command.id === "switch-theme")).toMatchObject({
  217. titleId: "quickPick.command.switchThemeToLight",
  218. icon: "themeLight",
  219. });
  220. const messageIds = new Set([
  221. "quickPick.title",
  222. "quickPick.description",
  223. "quickPick.placeholder",
  224. "quickPick.empty",
  225. "quickPick.commandFailed",
  226. "commandCenter.scopeTabs",
  227. "commandCenter.scope.all",
  228. "commandCenter.scope.commands",
  229. "commandCenter.scope.conversations",
  230. "commandCenter.scope.files",
  231. "commandCenter.section.recentChanges",
  232. "commandCenter.section.recentTasks",
  233. "commandCenter.empty.recentChanges",
  234. "commandCenter.empty.recentTasks",
  235. ...commands.map((command) => command.titleId),
  236. ...QUICK_PICK_SECTION_ORDER.map((sectionId) => `quickPick.section.${sectionId}`),
  237. ]);
  238. for (const id of messageIds) {
  239. expect(zhCN[id], `zh-CN missing ${id}`).toBeTruthy();
  240. expect(enUS[id], `en-US missing ${id}`).toBeTruthy();
  241. }
  242. });
  243. it("uses logout instead of login for authenticated users", () => {
  244. const commands = createQuickPickCommands({
  245. allowOpenWorkspace: true,
  246. canOpenCommunity: false,
  247. isSidebarVisible: true,
  248. isLoggedIn: true,
  249. themeTarget: "dark",
  250. shortcuts,
  251. handlers: createHandlers(),
  252. });
  253. expect(commands.some((command) => command.id === "login")).toBe(false);
  254. expect(commands.find((command) => command.id === "logout")).toMatchObject({
  255. sectionId: "app",
  256. titleId: "quickPick.command.logout",
  257. });
  258. expect(commands.some((command) => command.id === "community")).toBe(false);
  259. });
  260. });