messageChangeSummaryPanel.test.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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("+1");
  154. expect(html).toContain("-1");
  155. expect(html).toContain('aria-label="展开已更改文件"');
  156. expect(html).toContain('aria-expanded="false"');
  157. expect(html).toContain('data-state="closed"');
  158. expect(html).toContain('data-slot="collapsible-content"');
  159. expect(html).toContain('hidden=""');
  160. // Bugfix: 摘要默认闭合时不挂载文件行,避免多文件变更把最新消息顶走。
  161. expect(html).not.toContain("src/MessageChangeSummaryPanel.tsx");
  162. expect(html).not.toContain("打开");
  163. expect(html).not.toContain("选择打开方式");
  164. });
  165. it("优先显示当前轮次的摘要,不把整个 session 的汇总混进来", () => {
  166. const html = renderPanel({
  167. summary: {
  168. fileCount: 2,
  169. added: 8,
  170. removed: 3,
  171. files: [
  172. {
  173. path: "/workspace/src/MessageChangeSummaryPanel.tsx",
  174. added: 2,
  175. removed: 1,
  176. writeCount: 1,
  177. lastTurnIndex: 1,
  178. },
  179. {
  180. path: "/workspace/src/App.tsx",
  181. added: 6,
  182. removed: 2,
  183. writeCount: 3,
  184. lastTurnIndex: 0,
  185. },
  186. ],
  187. },
  188. changeDetails: {
  189. turnIndex: 1,
  190. snapshots: [
  191. {
  192. path: "/workspace/src/MessageChangeSummaryPanel.tsx",
  193. beforeContent: "const next = 1;\n",
  194. afterContent: "const next = 2;\n",
  195. writeCount: 1,
  196. },
  197. ],
  198. },
  199. });
  200. expect(html).toContain("1 个文件已更改");
  201. expect(html).toContain("+1");
  202. expect(html).not.toContain("2 个文件已更改");
  203. expect(html).not.toContain("src/App.tsx");
  204. expect(html).not.toContain("+8");
  205. });
  206. it("为打开按钮构建 MultiFileDiff code-viewer source", () => {
  207. expect(
  208. buildChangeSummaryDiffViewerSource({
  209. relativePath: "src/App.tsx",
  210. snapshot: {
  211. path: "/workspace/src/App.tsx",
  212. beforeContent: "const value = 1;\n",
  213. afterContent: "const value = 2;\n",
  214. writeCount: 1,
  215. },
  216. }),
  217. ).toEqual({
  218. type: "multi-file-diff",
  219. title: "src/App.tsx",
  220. path: "/workspace/src/App.tsx",
  221. beforeContent: "const value = 1;\n",
  222. afterContent: "const value = 2;\n",
  223. });
  224. });
  225. it("为打开按钮构建普通文件 preview source", () => {
  226. expect(
  227. buildChangeSummaryFilePreviewSource({
  228. path: "/workspace/src/App.tsx",
  229. relativePath: "src/App.tsx",
  230. workspacePath: "/workspace",
  231. workspaceIdentity: "remote:ssh:host-a:/workspace",
  232. workspaceRemoteSessionId: "session-a",
  233. }),
  234. ).toEqual({
  235. type: "file",
  236. title: "src/App.tsx",
  237. path: "/workspace/src/App.tsx",
  238. workspacePath: "/workspace",
  239. workspaceIdentity: "remote:ssh:host-a:/workspace",
  240. workspaceRemoteSessionId: "session-a",
  241. });
  242. });
  243. it("为工具聚合的 readonly patch 构建 patch preview source", () => {
  244. expect(
  245. buildChangeSummaryDiffViewerSource({
  246. relativePath: "src/App.tsx",
  247. path: "/workspace/src/App.tsx",
  248. snapshot: undefined,
  249. patch: "--- a/App.tsx\n+++ b/App.tsx\n@@ -1 +1 @@\n-old\n+new",
  250. }),
  251. ).toEqual({
  252. type: "patch",
  253. title: "src/App.tsx",
  254. path: "/workspace/src/App.tsx",
  255. patch: "--- a/App.tsx\n+++ b/App.tsx\n@@ -1 +1 @@\n-old\n+new",
  256. });
  257. });
  258. it("有 source 和打开回调时委托一次,缺少任一时不调用", () => {
  259. const source = buildChangeSummaryDiffViewerSource({
  260. relativePath: "src/App.tsx",
  261. path: "/workspace/src/App.tsx",
  262. snapshot: undefined,
  263. patch: "--- a/App.tsx\n+++ b/App.tsx\n@@ -1 +1 @@\n-old\n+new",
  264. });
  265. const onOpenCodeViewer = vi.fn();
  266. expect(openChangeSummaryDiffViewer(source, onOpenCodeViewer)).toBe(true);
  267. expect(onOpenCodeViewer).toHaveBeenCalledWith(source);
  268. onOpenCodeViewer.mockClear();
  269. expect(openChangeSummaryDiffViewer(undefined, onOpenCodeViewer)).toBe(false);
  270. expect(openChangeSummaryDiffViewer(source, undefined)).toBe(false);
  271. expect(onOpenCodeViewer).not.toHaveBeenCalled();
  272. });
  273. it("工具聚合摘要没有真实 checkpoint 时不展示撤销入口", () => {
  274. const html = renderPanel({
  275. changeDetails: null,
  276. onToggleFiles: vi.fn(),
  277. readonlyDiffPatchesByPath: new Map([
  278. [
  279. "/workspace/src/MessageChangeSummaryPanel.tsx",
  280. "--- a/MessageChangeSummaryPanel.tsx\n+++ b/MessageChangeSummaryPanel.tsx\n@@ -1 +1 @@\n-old\n+new",
  281. ],
  282. ]),
  283. });
  284. expect(html).toContain("1 个文件已更改");
  285. expect(html).not.toContain("撤销");
  286. });
  287. });