messageChangeSummaryPanel.test.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import type { IBroadcastService } from "@zcode/services";
  2. import type { IPlatformService } from "@zcode/shared";
  3. import { createElement } from "react";
  4. import { renderToStaticMarkup } from "react-dom/server";
  5. import { beforeEach, describe, expect, it, vi } from "vitest";
  6. import {
  7. buildChangeSummaryDiffViewerSource,
  8. buildChangeSummaryFilePreviewSource,
  9. MessageChangeSummaryPanel,
  10. openChangeSummaryDiffViewer,
  11. } from "@/MessageChangeSummaryPanel.js";
  12. import { PlatformProvider } from "@/hooks/usePlatform.js";
  13. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  14. import { StoreProvider } from "@/store/StoreProvider.js";
  15. const localStorageState = new Map<string, string>();
  16. Object.defineProperty(globalThis, "localStorage", {
  17. configurable: true,
  18. value: {
  19. getItem: (key: string) => localStorageState.get(key) ?? null,
  20. setItem: (key: string, value: string) => {
  21. localStorageState.set(key, value);
  22. },
  23. removeItem: (key: string) => {
  24. localStorageState.delete(key);
  25. },
  26. clear: () => {
  27. localStorageState.clear();
  28. },
  29. },
  30. });
  31. Object.defineProperty(globalThis, "document", {
  32. configurable: true,
  33. value: {
  34. documentElement: {
  35. classList: {
  36. contains: () => false,
  37. toggle: () => {},
  38. },
  39. },
  40. },
  41. });
  42. const mockBroadcastService: IBroadcastService = {
  43. send: async () => {},
  44. onMessage: () => ({ dispose: () => {} }),
  45. };
  46. const mockPlatform = {
  47. selectDirectory: async () => null,
  48. selectFile: async () => null,
  49. onRemoteConnectionLog: () => () => {},
  50. onRemoteSessionClosed: () => () => {},
  51. activateOrSetWorkspace: async () => ({ activated: false }),
  52. connectRemote: async () => ({ success: true }),
  53. startWebRemoteControl: async () => ({
  54. status: "running" as const,
  55. sessionId: "session-1",
  56. qrUrl: "qr://session-1",
  57. connectUrl: "https://example.com/connect",
  58. workspacePath: "/workspace",
  59. }),
  60. stopWebRemoteControl: async () => {},
  61. getWebRemoteControlStatus: async () => ({ status: "idle" as const }),
  62. disposeRemoteSession: async () => {},
  63. isDockerAvailable: async () => false,
  64. listWSLDistros: async () => [],
  65. listDockerContainers: async () => [],
  66. listSSHConfigAliases: async () => [],
  67. openExternal: () => {},
  68. openFeedback: async () => {},
  69. openCommunity: async () => {},
  70. canOpenCommunity: async () => false,
  71. openInFileManager: async () => ({ success: true }),
  72. openInEditor: async () => ({ success: true }),
  73. registerOAuthState: () => {},
  74. onOAuthCallback: () => () => {},
  75. notifyRendererReady: () => {},
  76. showTaskNotification: () => {},
  77. reportTelemetryEvent: async () => {},
  78. syncWindowTabs: () => {},
  79. syncWindowUnreadCount: () => {},
  80. onFocusTab: () => () => {},
  81. onNewTab: () => () => {},
  82. onNewTask: () => () => {},
  83. onOpenWorkspace: () => () => {},
  84. onWindowFullscreenChanged: () => () => {},
  85. onTaskNotificationClick: () => () => {},
  86. exportLogs: async () => ({ success: true }),
  87. onUpdateReady: () => () => {},
  88. onUpdateCheckResult: () => () => {},
  89. onPostUpdateReleaseNotes: () => () => {},
  90. acknowledgePostUpdateReleaseNotes: async () => {},
  91. quitAndInstallUpdate: async () => {},
  92. getInstalledEditors: async () => [],
  93. executeDesktopCommand: async () => {},
  94. setApplicationLocale: async () => {},
  95. setTitleBarTheme: async () => {},
  96. } as unknown as IPlatformService;
  97. type PanelProps = Parameters<typeof MessageChangeSummaryPanel>[0];
  98. function renderPanel(overrides: Partial<PanelProps> = {}) {
  99. const defaultProps: PanelProps = {
  100. summary: {
  101. fileCount: 1,
  102. added: 2,
  103. removed: 1,
  104. files: [
  105. {
  106. path: "/workspace/src/MessageChangeSummaryPanel.tsx",
  107. added: 2,
  108. removed: 1,
  109. writeCount: 1,
  110. lastTurnIndex: 0,
  111. },
  112. ],
  113. },
  114. workspacePath: "/workspace",
  115. changeDetails: {
  116. turnIndex: 0,
  117. snapshots: [
  118. {
  119. path: "/workspace/src/MessageChangeSummaryPanel.tsx",
  120. beforeContent: "const next = 1;\nconsole.log(next);\n",
  121. afterContent: "const next = 2;\nconsole.log(next);\n",
  122. writeCount: 1,
  123. },
  124. ],
  125. },
  126. };
  127. return renderToStaticMarkup(
  128. createElement(
  129. ZCodeIntlProvider,
  130. { initialLocale: "zh-CN" },
  131. createElement(
  132. PlatformProvider,
  133. { platform: mockPlatform },
  134. createElement(
  135. StoreProvider,
  136. { broadcastService: mockBroadcastService },
  137. createElement(MessageChangeSummaryPanel, {
  138. ...defaultProps,
  139. ...overrides,
  140. }),
  141. ),
  142. ),
  143. ),
  144. );
  145. }
  146. describe("MessageChangeSummaryPanel", () => {
  147. beforeEach(() => {
  148. localStorageState.clear();
  149. });
  150. it("默认闭合变更文件列表,并且先隐藏撤销按钮", () => {
  151. const html = renderPanel();
  152. expect(html).toContain("1 个文件已更改");
  153. expect(html).toContain('data-zcode-stream-animate="true"');
  154. expect(html).toContain("+1");
  155. expect(html).toContain("-1");
  156. expect(html).toContain('aria-label="展开已更改文件"');
  157. expect(html).toContain('aria-expanded="false"');
  158. expect(html).toContain('data-state="closed"');
  159. expect(html).toContain('data-slot="collapsible-content"');
  160. expect(html).toContain('hidden=""');
  161. // Bugfix: 摘要默认闭合时不挂载文件行,避免多文件变更把最新消息顶走。
  162. expect(html).not.toContain("src/MessageChangeSummaryPanel.tsx");
  163. expect(html).not.toContain("打开");
  164. expect(html).not.toContain("选择打开方式");
  165. expect(html).not.toContain("group/file flex w-full");
  166. expect(html).not.toContain('aria-label="展开工具详情"');
  167. expect(html).not.toContain("暂时无法预览这份 Diff。");
  168. });
  169. it("优先显示当前轮次的摘要,不把整个 session 的汇总混进来", () => {
  170. const html = renderPanel({
  171. summary: {
  172. fileCount: 2,
  173. added: 8,
  174. removed: 3,
  175. files: [
  176. {
  177. path: "/workspace/src/MessageChangeSummaryPanel.tsx",
  178. added: 2,
  179. removed: 1,
  180. writeCount: 1,
  181. lastTurnIndex: 1,
  182. },
  183. {
  184. path: "/workspace/src/App.tsx",
  185. added: 6,
  186. removed: 2,
  187. writeCount: 3,
  188. lastTurnIndex: 0,
  189. },
  190. ],
  191. },
  192. changeDetails: {
  193. turnIndex: 1,
  194. snapshots: [
  195. {
  196. path: "/workspace/src/MessageChangeSummaryPanel.tsx",
  197. beforeContent: "const next = 1;\n",
  198. afterContent: "const next = 2;\n",
  199. writeCount: 1,
  200. },
  201. ],
  202. },
  203. });
  204. expect(html).toContain("1 个文件已更改");
  205. expect(html).toContain("+1");
  206. expect(html).not.toContain("2 个文件已更改");
  207. expect(html).not.toContain("src/App.tsx");
  208. expect(html).not.toContain("+8");
  209. });
  210. it("为打开按钮构建 MultiFileDiff code-viewer source", () => {
  211. expect(
  212. buildChangeSummaryDiffViewerSource({
  213. relativePath: "src/App.tsx",
  214. snapshot: {
  215. path: "/workspace/src/App.tsx",
  216. beforeContent: "const value = 1;\n",
  217. afterContent: "const value = 2;\n",
  218. writeCount: 1,
  219. },
  220. }),
  221. ).toEqual({
  222. type: "multi-file-diff",
  223. title: "src/App.tsx",
  224. path: "/workspace/src/App.tsx",
  225. beforeContent: "const value = 1;\n",
  226. afterContent: "const value = 2;\n",
  227. });
  228. });
  229. it("为打开按钮构建普通文件 preview source", () => {
  230. expect(
  231. buildChangeSummaryFilePreviewSource({
  232. path: "/workspace/src/App.tsx",
  233. relativePath: "src/App.tsx",
  234. workspacePath: "/workspace",
  235. workspaceIdentity: "remote:ssh:host-a:/workspace",
  236. workspaceRemoteSessionId: "session-a",
  237. }),
  238. ).toEqual({
  239. type: "file",
  240. title: "src/App.tsx",
  241. path: "/workspace/src/App.tsx",
  242. workspacePath: "/workspace",
  243. workspaceIdentity: "remote:ssh:host-a:/workspace",
  244. workspaceRemoteSessionId: "session-a",
  245. });
  246. });
  247. it("为工具聚合的 readonly patch 构建 patch preview source", () => {
  248. expect(
  249. buildChangeSummaryDiffViewerSource({
  250. relativePath: "src/App.tsx",
  251. path: "/workspace/src/App.tsx",
  252. snapshot: undefined,
  253. patch: "--- a/App.tsx\n+++ b/App.tsx\n@@ -1 +1 @@\n-old\n+new",
  254. }),
  255. ).toEqual({
  256. type: "patch",
  257. title: "src/App.tsx",
  258. path: "/workspace/src/App.tsx",
  259. patch: "--- a/App.tsx\n+++ b/App.tsx\n@@ -1 +1 @@\n-old\n+new",
  260. });
  261. });
  262. it("文件行和 Review 按钮复用同一个 diff 打开事件", () => {
  263. const source = buildChangeSummaryDiffViewerSource({
  264. relativePath: "src/App.tsx",
  265. path: "/workspace/src/App.tsx",
  266. snapshot: undefined,
  267. patch: "--- a/App.tsx\n+++ b/App.tsx\n@@ -1 +1 @@\n-old\n+new",
  268. });
  269. const onOpenCodeViewer = vi.fn();
  270. expect(openChangeSummaryDiffViewer(source, onOpenCodeViewer)).toBe(true);
  271. expect(onOpenCodeViewer).toHaveBeenCalledWith(source);
  272. onOpenCodeViewer.mockClear();
  273. expect(openChangeSummaryDiffViewer(undefined, onOpenCodeViewer)).toBe(false);
  274. expect(openChangeSummaryDiffViewer(source, undefined)).toBe(false);
  275. expect(onOpenCodeViewer).not.toHaveBeenCalled();
  276. });
  277. it("工具聚合摘要没有真实 checkpoint 时不展示撤销入口", () => {
  278. const html = renderPanel({
  279. changeDetails: null,
  280. onToggleFiles: vi.fn(),
  281. readonlyDiffPatchesByPath: new Map([
  282. [
  283. "/workspace/src/MessageChangeSummaryPanel.tsx",
  284. "--- a/MessageChangeSummaryPanel.tsx\n+++ b/MessageChangeSummaryPanel.tsx\n@@ -1 +1 @@\n-old\n+new",
  285. ],
  286. ]),
  287. });
  288. expect(html).toContain("1 个文件已更改");
  289. expect(html).not.toContain("撤销");
  290. });
  291. });