chatEmptyStateRemoteEntry.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. import { createElement, type ComponentType, type ReactNode } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. vi.mock("@/components/ui/button.js", () => ({
  5. Button: ({
  6. children,
  7. ...props
  8. }: {
  9. children: ReactNode;
  10. [key: string]: unknown;
  11. }) => createElement("button", props, children),
  12. }));
  13. vi.mock("@/components/ui/dropdown-menu.js", () => ({
  14. DropdownMenu: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  15. DropdownMenuTrigger: ({ children }: { children: ReactNode }) =>
  16. createElement("div", null, children),
  17. DropdownMenuContent: ({ children }: { children: ReactNode }) =>
  18. createElement("div", null, children),
  19. DropdownMenuSub: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  20. DropdownMenuSubTrigger: ({
  21. children,
  22. ...props
  23. }: {
  24. children: ReactNode;
  25. [key: string]: unknown;
  26. }) => createElement("div", props, children),
  27. DropdownMenuSubContent: ({ children }: { children: ReactNode }) =>
  28. createElement("div", null, children),
  29. DropdownMenuItem: ({
  30. children,
  31. ...props
  32. }: {
  33. children: ReactNode;
  34. [key: string]: unknown;
  35. }) => createElement("div", props, children),
  36. DropdownMenuCheckboxItem: ({
  37. children,
  38. ...props
  39. }: {
  40. children: ReactNode;
  41. [key: string]: unknown;
  42. }) => createElement("div", props, children),
  43. DropdownMenuSeparator: () => createElement("hr"),
  44. }));
  45. vi.mock("@/components/ui/dialog.js", () => ({
  46. Dialog: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  47. DialogContent: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  48. DialogFooter: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  49. DialogHeader: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  50. DialogTitle: ({ children }: { children: ReactNode }) => createElement("h2", null, children),
  51. }));
  52. vi.mock("@/components/ui/input.js", () => ({
  53. Input: (props: Record<string, unknown>) => createElement("input", props),
  54. }));
  55. vi.mock("@/i18n/IntlProvider.js", () => ({
  56. useZCodeIntl: () => ({
  57. intl: {
  58. formatMessage: ({ id }: { id: string }) => id,
  59. },
  60. }),
  61. }));
  62. vi.mock("@/logger.js", () => ({
  63. logger: {
  64. info: vi.fn(),
  65. warn: vi.fn(),
  66. error: vi.fn(),
  67. },
  68. }));
  69. vi.mock("@/SSHDialog.js", () => ({
  70. SSHDialog: () => createElement("div", { "data-testid": "ssh-dialog" }),
  71. }));
  72. async function renderMenu(
  73. showRemoteConnectionEntry: boolean,
  74. allowRemoteWorkspace = true,
  75. ) {
  76. vi.resetModules();
  77. vi.doMock("@/hooks/useRemoteConnectionEntryVisibility.js", () => ({
  78. useRemoteConnectionEntryVisibility: () => showRemoteConnectionEntry,
  79. }));
  80. vi.doMock("@zcode/shared", async () => {
  81. const actual = await vi.importActual<typeof import("@zcode/shared")>(
  82. "@zcode/shared"
  83. );
  84. return {
  85. ...actual,
  86. };
  87. });
  88. const { ChatEmptyWorkspacePreviewMenu } = await import("../src/ChatEmptyState.js");
  89. const Menu = ChatEmptyWorkspacePreviewMenu as unknown as ComponentType<
  90. Record<string, unknown>
  91. >;
  92. return renderToStaticMarkup(
  93. createElement(Menu, {
  94. workspacePath: "/Users/tester/project",
  95. workspaceTabs: [
  96. {
  97. workspacePath: "/Users/tester/project",
  98. label: "project",
  99. },
  100. ],
  101. onSelectWorkspace: vi.fn(),
  102. onSelectConversationWorkspace: vi.fn(),
  103. onOpenFolder: vi.fn(),
  104. allowRemoteWorkspace,
  105. onConnectRemote: async () => "session-id",
  106. onSelectRemoteProject: async () => {},
  107. onCancelRemoteProject: async () => {},
  108. }),
  109. );
  110. }
  111. afterEach(() => {
  112. vi.resetModules();
  113. });
  114. describe("ChatEmptyWorkspacePreviewMenu", () => {
  115. it("同路径远程项目按 workspaceIdentity 判定唯一选中项", async () => {
  116. const { isWorkspaceMenuTabSelected } = await import("../src/ChatEmptyState.js");
  117. const remoteA = {
  118. workspacePath: "/repo",
  119. workspaceIdentity: "remote:ssh:a:/repo",
  120. label: "remote-a",
  121. };
  122. const remoteB = {
  123. workspacePath: "/repo",
  124. workspaceIdentity: "remote:ssh:b:/repo",
  125. label: "remote-b",
  126. };
  127. expect(
  128. isWorkspaceMenuTabSelected(remoteA, {
  129. workspacePath: "/repo",
  130. workspaceIdentity: remoteB.workspaceIdentity,
  131. }),
  132. ).toBe(false);
  133. expect(
  134. isWorkspaceMenuTabSelected(remoteB, {
  135. workspacePath: "/repo",
  136. workspaceIdentity: remoteB.workspaceIdentity,
  137. }),
  138. ).toBe(true);
  139. });
  140. it("工作区搜索最多保留前 5 条且保持原顺序", async () => {
  141. const {
  142. filterVisibleWorkspaceMenuTabs,
  143. } = await import("../src/ChatEmptyState.js");
  144. const tabs = [
  145. "/workspace/alpha",
  146. "/workspace/beta",
  147. "/workspace/gamma",
  148. "/workspace/delta",
  149. "/workspace/epsilon",
  150. "/workspace/zeta",
  151. ].map((workspacePath) => ({
  152. workspacePath,
  153. label: workspacePath.split("/").pop()!,
  154. }));
  155. expect(
  156. filterVisibleWorkspaceMenuTabs({
  157. workspaceTabs: tabs,
  158. homeWorkspaceLabel: "Home",
  159. searchQuery: "",
  160. }).map((tab) => tab.label),
  161. ).toEqual(["alpha", "beta", "gamma", "delta", "epsilon"]);
  162. expect(
  163. filterVisibleWorkspaceMenuTabs({
  164. workspaceTabs: tabs,
  165. homeWorkspaceLabel: "Home",
  166. searchQuery: "workspace",
  167. }).map((tab) => tab.label),
  168. ).toEqual(["alpha", "beta", "gamma", "delta", "epsilon"]);
  169. });
  170. it("工作区搜索匹配名称、路径和远程目标信息", async () => {
  171. const {
  172. filterVisibleWorkspaceMenuTabs,
  173. } = await import("../src/ChatEmptyState.js");
  174. const tabs = [
  175. {
  176. workspacePath: "/repo/alpha",
  177. label: "Alpha",
  178. },
  179. {
  180. workspacePath: "/remote/project",
  181. label: "Remote Project",
  182. remoteSessionId: "remote-1",
  183. remoteTarget: {
  184. kind: "ssh",
  185. host: "dev.example.com",
  186. port: 2222,
  187. username: "alice",
  188. },
  189. },
  190. {
  191. workspacePath: "/repo/by-path",
  192. label: "Path Match",
  193. },
  194. ] as const;
  195. expect(
  196. filterVisibleWorkspaceMenuTabs({
  197. workspaceTabs: tabs,
  198. homeWorkspaceLabel: "Home",
  199. searchQuery: "alpha",
  200. }).map((tab) => tab.label),
  201. ).toEqual(["Alpha"]);
  202. expect(
  203. filterVisibleWorkspaceMenuTabs({
  204. workspaceTabs: tabs,
  205. homeWorkspaceLabel: "Home",
  206. searchQuery: "by-path",
  207. }).map((tab) => tab.label),
  208. ).toEqual(["Path Match"]);
  209. expect(
  210. filterVisibleWorkspaceMenuTabs({
  211. workspaceTabs: tabs,
  212. homeWorkspaceLabel: "Home",
  213. searchQuery: "dev.example.com",
  214. }).map((tab) => tab.label),
  215. ).toEqual(["Remote Project"]);
  216. });
  217. it("工作区搜索没有匹配项时保留新增工作区入口", async () => {
  218. const {
  219. filterVisibleWorkspaceMenuTabs,
  220. ChatEmptyWorkspacePreviewMenu,
  221. } = await import("../src/ChatEmptyState.js");
  222. const tabs = [
  223. {
  224. workspacePath: "/repo/alpha",
  225. label: "Alpha",
  226. },
  227. ];
  228. expect(
  229. filterVisibleWorkspaceMenuTabs({
  230. workspaceTabs: tabs,
  231. homeWorkspaceLabel: "Home",
  232. searchQuery: "missing",
  233. }),
  234. ).toEqual([]);
  235. const Menu = ChatEmptyWorkspacePreviewMenu as unknown as ComponentType<
  236. Record<string, unknown>
  237. >;
  238. const html = renderToStaticMarkup(
  239. createElement(Menu, {
  240. workspacePath: "/Users/tester/project",
  241. workspaceTabs: [],
  242. onSelectWorkspace: vi.fn(),
  243. onSelectConversationWorkspace: vi.fn(),
  244. onOpenFolder: vi.fn(),
  245. allowRemoteWorkspace: false,
  246. onConnectRemote: async () => "session-id",
  247. onSelectRemoteProject: async () => {},
  248. onCancelRemoteProject: async () => {},
  249. }),
  250. );
  251. expect(html).toContain("chat.empty.workspaceSearchEmpty");
  252. expect(html).toContain("workspace.openFolder");
  253. });
  254. it("隐藏 Home 快捷项并展示 Open folder 入口", async () => {
  255. const html = await renderMenu(false);
  256. expect(html).not.toContain("chat.empty.home");
  257. expect(html).not.toContain("workspace.openWorkspace");
  258. expect(html).toContain("workspace.openFolder");
  259. });
  260. it("Start from scratch 默认路径为空名称后缀,非法名称有校验", async () => {
  261. const {
  262. getScratchWorkspaceLocationHint,
  263. getScratchWorkspaceNameErrorKind,
  264. } = await import("../src/ChatEmptyState.js");
  265. expect(getScratchWorkspaceLocationHint("")).toBe("~/ZCodeProject/");
  266. expect(getScratchWorkspaceLocationHint("demo")).toBe("~/ZCodeProject/demo");
  267. expect(getScratchWorkspaceNameErrorKind("")).toBe("required");
  268. expect(getScratchWorkspaceNameErrorKind("bad/name")).toBe("separator");
  269. expect(getScratchWorkspaceNameErrorKind("bad\\name")).toBe("separator");
  270. expect(getScratchWorkspaceNameErrorKind("demo")).toBeNull();
  271. });
  272. it("在允许远程入口时展示远程连接项", async () => {
  273. const html = await renderMenu(true);
  274. expect(html).toContain("remote.trigger");
  275. });
  276. it("workspace 菜单提供不在项目中工作入口", async () => {
  277. const html = await renderMenu(false);
  278. expect(html).toContain("chat.empty.workOutsideProject");
  279. });
  280. it("对话工作区显示选择项目且不展示关闭按钮", async () => {
  281. const { ChatEmptyWorkspacePreviewMenu } =
  282. await import("../src/ChatEmptyState.js");
  283. const Menu = ChatEmptyWorkspacePreviewMenu as unknown as ComponentType<
  284. Record<string, unknown>
  285. >;
  286. const html = renderToStaticMarkup(
  287. createElement(Menu, {
  288. workspacePath: "/Users/tester/.zcode/workspace/default",
  289. workspaceTabs: [
  290. {
  291. workspacePath: "/Users/tester/.zcode/workspace/default",
  292. label: "default",
  293. workspacePurpose: "conversation",
  294. },
  295. ],
  296. onSelectWorkspace: vi.fn(),
  297. onSelectConversationWorkspace: vi.fn(),
  298. onOpenFolder: vi.fn(),
  299. allowRemoteWorkspace: false,
  300. onConnectRemote: async () => "session-id",
  301. onSelectRemoteProject: async () => {},
  302. onCancelRemoteProject: async () => {},
  303. }),
  304. );
  305. expect(html).toContain("chat.empty.selectProject");
  306. expect(html).not.toContain("chat.empty.detachProject");
  307. });
  308. it("可单独保留不在项目中工作菜单项并关闭项目 chip 的脱离按钮", async () => {
  309. const { ChatEmptyWorkspacePreviewMenu } =
  310. await import("../src/ChatEmptyState.js");
  311. const Menu = ChatEmptyWorkspacePreviewMenu as unknown as ComponentType<
  312. Record<string, unknown>
  313. >;
  314. const html = renderToStaticMarkup(
  315. createElement(Menu, {
  316. workspacePath: "/Users/tester/project",
  317. workspaceTabs: [
  318. {
  319. workspacePath: "/Users/tester/.zcode/workspace/default",
  320. label: "default",
  321. workspacePurpose: "conversation",
  322. },
  323. {
  324. workspacePath: "/Users/tester/project",
  325. label: "project",
  326. },
  327. ],
  328. allowConversationWorkspaceSelection: true,
  329. allowConversationWorkspaceDetach: false,
  330. onSelectWorkspace: vi.fn(),
  331. onSelectConversationWorkspace: vi.fn(),
  332. onOpenFolder: vi.fn(),
  333. allowOpenWorkspace: false,
  334. allowRemoteWorkspace: false,
  335. onConnectRemote: async () => "session-id",
  336. onSelectRemoteProject: async () => {},
  337. onCancelRemoteProject: async () => {},
  338. }),
  339. );
  340. expect(html).toContain("chat.empty.workOutsideProject");
  341. expect(html).toContain("lucide-message-circle");
  342. expect(html).not.toContain(">default<");
  343. expect(html).not.toContain("chat.empty.detachProject");
  344. expect(html).not.toContain("group-hover/workspace-chip:opacity-0");
  345. });
  346. it("禁用非项目工作区能力时同时隐藏脱离按钮和菜单项", async () => {
  347. const { ChatEmptyWorkspacePreviewMenu } =
  348. await import("../src/ChatEmptyState.js");
  349. const Menu = ChatEmptyWorkspacePreviewMenu as unknown as ComponentType<
  350. Record<string, unknown>
  351. >;
  352. const props = {
  353. workspacePath: "/Users/tester/project",
  354. workspaceTabs: [
  355. {
  356. workspacePath: "/Users/tester/project",
  357. label: "project",
  358. },
  359. ],
  360. onSelectWorkspace: vi.fn(),
  361. onSelectConversationWorkspace: vi.fn(),
  362. onOpenFolder: vi.fn(),
  363. allowRemoteWorkspace: false,
  364. onConnectRemote: async () => "session-id",
  365. onSelectRemoteProject: async () => {},
  366. onCancelRemoteProject: async () => {},
  367. };
  368. const defaultHtml = renderToStaticMarkup(createElement(Menu, props));
  369. const hiddenHtml = renderToStaticMarkup(
  370. createElement(Menu, {
  371. ...props,
  372. allowConversationWorkspaceSelection: false,
  373. }),
  374. );
  375. expect(defaultHtml).toContain("chat.empty.detachProject");
  376. expect(defaultHtml).toContain("chat.empty.workOutsideProject");
  377. expect(defaultHtml).toContain("group-hover/workspace-chip:opacity-0");
  378. expect(defaultHtml).toContain("group-focus-within/workspace-chip:opacity-0");
  379. expect(hiddenHtml).not.toContain("chat.empty.detachProject");
  380. expect(hiddenHtml).not.toContain("chat.empty.workOutsideProject");
  381. expect(hiddenHtml).not.toContain("group-hover/workspace-chip:opacity-0");
  382. expect(hiddenHtml).not.toContain("group-focus-within/workspace-chip:opacity-0");
  383. });
  384. it("普通会话 hover workspace trigger 时隐藏项目图标供关闭按钮替换", async () => {
  385. const html = await renderMenu(false);
  386. expect(html).toContain("group-hover/workspace-chip:opacity-0");
  387. expect(html).toContain("group-focus-within/workspace-chip:opacity-0");
  388. });
  389. it("在不允许远程入口时隐藏远程连接项", async () => {
  390. const html = await renderMenu(false);
  391. expect(html).not.toContain("remote.trigger");
  392. });
  393. it("在 Web 本地模式关闭远程能力时隐藏远程连接项", async () => {
  394. const html = await renderMenu(true, false);
  395. expect(html).not.toContain("remote.trigger");
  396. expect(html).not.toContain("ssh-dialog");
  397. });
  398. });