workspaceHeaderTitleSection.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. import { createElement, type ReactNode } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import { TID_WORKSPACE_MORE_BUTTON, TID_WORKSPACE_PATH, TID_WORKSPACE_TITLE } from "@zcode/shared";
  5. const capturedGitBranchSwitcherProps = vi.hoisted(() => [] as Array<Record<string, unknown>>);
  6. vi.mock("lucide-react", async (importOriginal) => {
  7. const actual = await importOriginal<typeof import("lucide-react")>();
  8. const createIcon = (name: string) => (props: Record<string, unknown>) =>
  9. createElement("svg", { "data-icon": name, ...props });
  10. return {
  11. ...actual,
  12. Cloud: createIcon("cloud"),
  13. Ellipsis: createIcon("ellipsis"),
  14. Folder: createIcon("folder"),
  15. GitBranch: createIcon("git-branch"),
  16. LoaderIcon: createIcon("loader"),
  17. UploadCloud: createIcon("upload-cloud"),
  18. };
  19. });
  20. vi.mock("@/ControlHintTooltip.js", () => ({
  21. ControlHintTooltip: ({ children }: { children: ReactNode }) => children,
  22. }));
  23. vi.mock("@/components/ui/button.js", () => ({
  24. Button: ({ children, ...props }: { children: ReactNode }) =>
  25. createElement("button", props, children),
  26. }));
  27. vi.mock("@/components/ui/dialog.js", () => ({
  28. Dialog: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  29. DialogContent: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  30. DialogFooter: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  31. DialogHeader: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  32. DialogTitle: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  33. }));
  34. vi.mock("@/components/ui/dropdown-menu.js", () => ({
  35. DropdownMenu: ({ children }: { children: ReactNode }) =>
  36. createElement("div", { "data-testid": "dropdown-menu" }, children),
  37. DropdownMenuContent: ({ children }: { children: ReactNode }) =>
  38. createElement("div", { "data-testid": "dropdown-content" }, children),
  39. DropdownMenuItem: ({ children }: { children: ReactNode }) =>
  40. createElement("div", { "data-testid": "dropdown-item" }, children),
  41. DropdownMenuSeparator: () => createElement("hr"),
  42. DropdownMenuTrigger: ({ children }: { children: ReactNode }) => children,
  43. }));
  44. vi.mock("@/components/ui/input.js", () => ({
  45. Input: (props: Record<string, unknown>) => createElement("input", props),
  46. }));
  47. vi.mock("@/components/ui/toast.js", () => ({
  48. toast: vi.fn(),
  49. }));
  50. vi.mock("@/GitBranchSwitcher.js", () => ({
  51. GitBranchSwitcher: (props: Record<string, unknown>) => {
  52. capturedGitBranchSwitcherProps.push(props);
  53. return createElement("span", {
  54. "data-testid": "git-branch",
  55. "data-trigger-class-name": String(props.triggerClassName ?? ""),
  56. });
  57. },
  58. }));
  59. vi.mock("@/TaskActionMenuContent.js", () => ({
  60. TaskActionMenuContent: () => createElement("div", { "data-testid": "task-action-menu" }),
  61. }));
  62. vi.mock("@/settings/RemoteSkillSyncDialog.js", () => ({
  63. RemoteSkillSyncDialog: () => createElement("div", { "data-testid": "remote-skill-sync-dialog" }),
  64. }));
  65. vi.mock("@/settings/RemoteMcpSyncDialog.js", () => ({
  66. RemoteMcpSyncDialog: () => createElement("div", { "data-testid": "remote-mcp-sync-dialog" }),
  67. }));
  68. vi.mock("@/settings/RemotePluginSyncDialog.js", () => ({
  69. RemotePluginSyncDialog: () =>
  70. createElement("div", { "data-testid": "remote-plugin-sync-dialog" }),
  71. }));
  72. vi.mock("@/hooks/useConfirmDialog.js", () => ({
  73. useConfirmDialog: () => vi.fn(async () => true),
  74. }));
  75. vi.mock("@/hooks/useGlobalTaskList.js", () => ({
  76. useGlobalTaskList: () => ({ items: [], loading: false }),
  77. }));
  78. vi.mock("@/hooks/usePlatform.js", () => ({
  79. usePlatform: () => ({}),
  80. }));
  81. vi.mock("@/hooks/useWorkspaceServices.js", () => ({
  82. // Bugfix: Header 现在同时读取 workspace services 和 base services;
  83. // 测试 mock 需要覆盖同模块全部被用到的 hook,避免新增导出后静态渲染直接崩溃。
  84. useBaseWorkspaceServices: () => ({
  85. skillSyncService: {},
  86. mcpSyncService: {},
  87. pluginSyncService: {},
  88. }),
  89. useWorkspaceServices: () => ({
  90. skillSyncService: {},
  91. mcpSyncService: {},
  92. pluginSyncService: {},
  93. zcodeTaskService: {
  94. archiveTask: vi.fn(),
  95. renameTask: vi.fn(),
  96. setTaskPinned: vi.fn(),
  97. setTaskUnread: vi.fn(),
  98. },
  99. }),
  100. }));
  101. vi.mock("@/useTaskListItemContextActions.js", () => ({
  102. useTaskListItemContextActions: () => ({
  103. taskSessionFile: { path: "/tmp/session.json", exists: true },
  104. taskNativeSessionLogFile: { path: "/tmp/session.log", exists: true },
  105. providerConfigFile: { path: "/tmp/provider.json", exists: true },
  106. fileManagerLabel: "Finder",
  107. handleCopyText: vi.fn(),
  108. handleOpenTaskPathInFileManager: vi.fn(),
  109. handleOpenProviderConfig: vi.fn(),
  110. }),
  111. }));
  112. vi.mock("@/i18n/IntlProvider.js", () => ({
  113. useZCodeIntl: () => ({
  114. intl: {
  115. formatMessage: ({ id }: { id: string }) => id,
  116. },
  117. }),
  118. }));
  119. vi.mock("@/feedback/feedbackStore.js", () => ({
  120. useFeedbackStore: (selector: (state: { openSubmit: () => void }) => unknown) =>
  121. selector({ openSubmit: vi.fn() }),
  122. }));
  123. vi.mock("@/store/taskQueryCacheStore.js", () => ({
  124. applyTaskQueryCacheMutation: vi.fn(),
  125. }));
  126. vi.mock("@/store/zcodeSessionStore.js", () => ({
  127. useZCodeSessionStore: (selector: (state: Record<string, unknown>) => unknown) =>
  128. selector({
  129. removeTaskState: vi.fn(),
  130. removeOptimisticTaskListItem: vi.fn(),
  131. setTaskUnreadIndicator: vi.fn(),
  132. upsertOptimisticTaskListItem: vi.fn(),
  133. }),
  134. }));
  135. vi.mock("@/store/remotePinnedTaskStore.js", () => ({
  136. useRemotePinnedTaskStore: {
  137. getState: () => ({
  138. removeTask: vi.fn(),
  139. upsertTask: vi.fn(),
  140. }),
  141. },
  142. }));
  143. vi.mock("@/store/remoteTimelineTaskStore.js", () => ({
  144. useRemoteTimelineTaskStore: {
  145. getState: () => ({
  146. removeTask: vi.fn(),
  147. upsertTask: vi.fn(),
  148. }),
  149. },
  150. }));
  151. vi.mock("@/store/modelTrajectoryStore.js", () => ({
  152. useModelTrajectoryStore: {
  153. getState: () => ({
  154. requestOpen: vi.fn(),
  155. }),
  156. },
  157. }));
  158. describe("WorkspaceHeaderTitleSection", () => {
  159. it("keeps the title but hides task-only context and menus in the draft variant", async () => {
  160. capturedGitBranchSwitcherProps.length = 0;
  161. const { WorkspaceHeaderTitleSection } = await import("@/WorkspaceHeaderSections.js");
  162. const html = renderToStaticMarkup(
  163. createElement(WorkspaceHeaderTitleSection, {
  164. variant: "draft",
  165. workspaceAbsPath: "/workspace/zcode-cli",
  166. projectName: "zcode-cli",
  167. activeTaskTitle: "New task",
  168. activeTaskId: null,
  169. activeTraceId: null,
  170. activeSessionId: null,
  171. activeTaskProvider: null,
  172. gitSummary: { isRepository: true, branchName: "main" },
  173. gitDirtyFileCount: 0,
  174. sessionLogPath: null,
  175. nativeSessionLogProvider: null,
  176. nativeSessionLogPath: null,
  177. nativeSessionLogExists: false,
  178. nativeSessionLogLoading: false,
  179. providerWorkspaceConfigPath: null,
  180. providerWorkspaceConfigExists: false,
  181. providerWorkspaceConfigLoading: false,
  182. workspaceHeaderState: { selectedProvider: "codex" },
  183. selectedEditor: null,
  184. onRefreshGit: vi.fn(),
  185. }),
  186. );
  187. expect(html).toContain(`data-testid="${TID_WORKSPACE_TITLE}"`);
  188. expect(html).not.toContain(`data-testid="${TID_WORKSPACE_PATH}"`);
  189. expect(html).not.toContain(`data-testid="${TID_WORKSPACE_MORE_BUTTON}"`);
  190. expect(capturedGitBranchSwitcherProps).toHaveLength(0);
  191. });
  192. it("places the workspace icon before the title and removes the inline branch in mobile mode", async () => {
  193. capturedGitBranchSwitcherProps.length = 0;
  194. const { WorkspaceHeaderTitleSection } = await import("@/WorkspaceHeaderSections.js");
  195. const html = renderToStaticMarkup(
  196. createElement(WorkspaceHeaderTitleSection, {
  197. workspaceAbsPath: "/workspace/zcode-cli",
  198. projectName: "zcode-cli",
  199. activeTaskTitle: "hello",
  200. activeTaskId: "task-1",
  201. activeTraceId: "trace-1",
  202. activeSessionId: "session-1",
  203. activeTaskProvider: "glm",
  204. gitSummary: {
  205. isGitAvailable: true,
  206. isRepository: true,
  207. branchName: "main",
  208. headRefType: "branch",
  209. },
  210. gitDirtyFileCount: 0,
  211. sessionLogPath: null,
  212. nativeSessionLogProvider: null,
  213. nativeSessionLogPath: null,
  214. nativeSessionLogExists: false,
  215. nativeSessionLogLoading: false,
  216. providerWorkspaceConfigPath: null,
  217. providerWorkspaceConfigExists: false,
  218. providerWorkspaceConfigLoading: false,
  219. workspaceHeaderState: { selectedProvider: "glm" },
  220. selectedEditor: null,
  221. simplifyForNarrowRemote: true,
  222. onRefreshGit: vi.fn(),
  223. }),
  224. );
  225. expect(capturedGitBranchSwitcherProps).toHaveLength(0);
  226. expect(html).toContain('data-icon="folder"');
  227. expect(html).toContain('aria-label="zcode-cli · main"');
  228. expect(html.indexOf(`data-testid="${TID_WORKSPACE_PATH}"`)).toBeLessThan(
  229. html.indexOf(`data-testid="${TID_WORKSPACE_TITLE}"`),
  230. );
  231. });
  232. it("uses the SSH alias suffix format for remote workspace context", async () => {
  233. const { WorkspaceHeaderTitleSection } = await import("@/WorkspaceHeaderSections.js");
  234. const html = renderToStaticMarkup(
  235. createElement(WorkspaceHeaderTitleSection, {
  236. workspaceAbsPath: "/root",
  237. remoteSessionId: "remote-session-1",
  238. workspaceIdentity: "remote:ssh:localhost:2223:root:/root",
  239. remoteTarget: {
  240. kind: "ssh",
  241. host: "localhost",
  242. port: 2223,
  243. username: "root",
  244. sshConfigAlias: "linux-arm64",
  245. },
  246. projectName: "root",
  247. activeTaskTitle: "hello",
  248. activeTaskId: "task-1",
  249. activeTraceId: "trace-1",
  250. activeSessionId: "session-1",
  251. activeTaskProvider: "glm",
  252. gitSummary: { isRepository: false },
  253. gitDirtyFileCount: 0,
  254. sessionLogPath: null,
  255. nativeSessionLogProvider: null,
  256. nativeSessionLogPath: null,
  257. nativeSessionLogExists: false,
  258. nativeSessionLogLoading: false,
  259. providerWorkspaceConfigPath: null,
  260. providerWorkspaceConfigExists: false,
  261. providerWorkspaceConfigLoading: false,
  262. workspaceHeaderState: { selectedProvider: "glm" },
  263. selectedEditor: null,
  264. onRefreshGit: vi.fn(),
  265. }),
  266. );
  267. expect(html).toContain("root [SSH: linux-arm64]");
  268. expect(html).toContain('data-icon="cloud"');
  269. expect(html).not.toContain(">root</span><span");
  270. expect(html).not.toContain(">localhost:2223</span>");
  271. });
  272. it("shows Skills and MCP sync actions for connected SSH workspace header menu", async () => {
  273. const { WorkspaceHeaderTitleSection } = await import("@/WorkspaceHeaderSections.js");
  274. const html = renderToStaticMarkup(
  275. createElement(WorkspaceHeaderTitleSection, {
  276. workspaceAbsPath: "/root/folder1",
  277. remoteSessionId: "remote-session-1",
  278. workspaceIdentity: "remote:ssh:localhost:2223:root:/root/folder1",
  279. remoteTarget: {
  280. kind: "ssh",
  281. host: "localhost",
  282. port: 2223,
  283. username: "root",
  284. sshConfigAlias: "linux-arm64",
  285. },
  286. projectName: "folder1",
  287. activeTaskTitle: "hello",
  288. activeTaskId: "task-1",
  289. activeTraceId: "trace-1",
  290. activeSessionId: "session-1",
  291. activeTaskProvider: "glm",
  292. gitSummary: { isRepository: false },
  293. gitDirtyFileCount: 0,
  294. sessionLogPath: null,
  295. nativeSessionLogProvider: null,
  296. nativeSessionLogPath: null,
  297. nativeSessionLogExists: false,
  298. nativeSessionLogLoading: false,
  299. providerWorkspaceConfigPath: null,
  300. providerWorkspaceConfigExists: false,
  301. providerWorkspaceConfigLoading: false,
  302. workspaceHeaderState: { selectedProvider: "glm" },
  303. selectedEditor: null,
  304. onRefreshGit: vi.fn(),
  305. }),
  306. );
  307. expect(html).toContain("settings.skills.remoteSync.open");
  308. expect(html).toContain("settings.mcp.remoteSync.open");
  309. });
  310. });