| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- import type { IBroadcastService } from "@zcode/services";
- import type { IPlatformService } from "@zcode/shared";
- import { createElement } from "react";
- import { renderToStaticMarkup } from "react-dom/server";
- import { beforeEach, describe, expect, it, vi } from "vitest";
- import {
- buildChangeSummaryDiffViewerSource,
- buildChangeSummaryFilePreviewSource,
- MessageChangeSummaryPanel,
- openChangeSummaryDiffViewer,
- } from "@/MessageChangeSummaryPanel.js";
- import { PlatformProvider } from "@/hooks/usePlatform.js";
- import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
- import { StoreProvider } from "@/store/StoreProvider.js";
- const localStorageState = new Map<string, string>();
- Object.defineProperty(globalThis, "localStorage", {
- configurable: true,
- value: {
- getItem: (key: string) => localStorageState.get(key) ?? null,
- setItem: (key: string, value: string) => {
- localStorageState.set(key, value);
- },
- removeItem: (key: string) => {
- localStorageState.delete(key);
- },
- clear: () => {
- localStorageState.clear();
- },
- },
- });
- Object.defineProperty(globalThis, "document", {
- configurable: true,
- value: {
- documentElement: {
- classList: {
- contains: () => false,
- toggle: () => {},
- },
- },
- },
- });
- const mockBroadcastService: IBroadcastService = {
- send: async () => {},
- onMessage: () => ({ dispose: () => {} }),
- };
- const mockPlatform = {
- selectDirectory: async () => null,
- selectFile: async () => null,
- onRemoteConnectionLog: () => () => {},
- onRemoteSessionClosed: () => () => {},
- activateOrSetWorkspace: async () => ({ activated: false }),
- connectRemote: async () => ({ success: true }),
- startWebRemoteControl: async () => ({
- status: "running" as const,
- sessionId: "session-1",
- qrUrl: "qr://session-1",
- connectUrl: "https://example.com/connect",
- workspacePath: "/workspace",
- }),
- stopWebRemoteControl: async () => {},
- getWebRemoteControlStatus: async () => ({ status: "idle" as const }),
- disposeRemoteSession: async () => {},
- isDockerAvailable: async () => false,
- listWSLDistros: async () => [],
- listDockerContainers: async () => [],
- listSSHConfigAliases: async () => [],
- openExternal: () => {},
- openFeedback: async () => {},
- openCommunity: async () => {},
- canOpenCommunity: async () => false,
- openInFileManager: async () => ({ success: true }),
- openInEditor: async () => ({ success: true }),
- registerOAuthState: () => {},
- onOAuthCallback: () => () => {},
- notifyRendererReady: () => {},
- showTaskNotification: () => {},
- reportTelemetryEvent: async () => {},
- syncWindowTabs: () => {},
- syncWindowUnreadCount: () => {},
- onFocusTab: () => () => {},
- onNewTab: () => () => {},
- onNewTask: () => () => {},
- onOpenWorkspace: () => () => {},
- onWindowFullscreenChanged: () => () => {},
- onTaskNotificationClick: () => () => {},
- exportLogs: async () => ({ success: true }),
- onUpdateReady: () => () => {},
- onUpdateCheckResult: () => () => {},
- onPostUpdateReleaseNotes: () => () => {},
- acknowledgePostUpdateReleaseNotes: async () => {},
- quitAndInstallUpdate: async () => {},
- getInstalledEditors: async () => [],
- executeDesktopCommand: async () => {},
- setApplicationLocale: async () => {},
- setTitleBarTheme: async () => {},
- } as unknown as IPlatformService;
- type PanelProps = Parameters<typeof MessageChangeSummaryPanel>[0];
- function renderPanel(overrides: Partial<PanelProps> = {}) {
- const defaultProps: PanelProps = {
- summary: {
- fileCount: 1,
- added: 2,
- removed: 1,
- files: [
- {
- path: "/workspace/src/MessageChangeSummaryPanel.tsx",
- added: 2,
- removed: 1,
- writeCount: 1,
- lastTurnIndex: 0,
- },
- ],
- },
- workspacePath: "/workspace",
- changeDetails: {
- turnIndex: 0,
- snapshots: [
- {
- path: "/workspace/src/MessageChangeSummaryPanel.tsx",
- beforeContent: "const next = 1;\nconsole.log(next);\n",
- afterContent: "const next = 2;\nconsole.log(next);\n",
- writeCount: 1,
- },
- ],
- },
- };
- return renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(
- PlatformProvider,
- { platform: mockPlatform },
- createElement(
- StoreProvider,
- { broadcastService: mockBroadcastService },
- createElement(MessageChangeSummaryPanel, {
- ...defaultProps,
- ...overrides,
- }),
- ),
- ),
- ),
- );
- }
- describe("MessageChangeSummaryPanel", () => {
- beforeEach(() => {
- localStorageState.clear();
- });
- it("默认闭合变更文件列表,并且先隐藏撤销按钮", () => {
- const html = renderPanel();
- expect(html).toContain("1 个文件已更改");
- expect(html).toContain('data-zcode-stream-animate="true"');
- expect(html).toContain("+1");
- expect(html).toContain("-1");
- expect(html).toContain('aria-label="展开已更改文件"');
- expect(html).toContain('aria-expanded="false"');
- expect(html).toContain('data-state="closed"');
- expect(html).toContain('data-slot="collapsible-content"');
- expect(html).toContain('hidden=""');
- // Bugfix: 摘要默认闭合时不挂载文件行,避免多文件变更把最新消息顶走。
- expect(html).not.toContain("src/MessageChangeSummaryPanel.tsx");
- expect(html).not.toContain("打开");
- expect(html).not.toContain("选择打开方式");
- expect(html).not.toContain("group/file flex w-full");
- expect(html).not.toContain('aria-label="展开工具详情"');
- expect(html).not.toContain("暂时无法预览这份 Diff。");
- });
- it("优先显示当前轮次的摘要,不把整个 session 的汇总混进来", () => {
- const html = renderPanel({
- summary: {
- fileCount: 2,
- added: 8,
- removed: 3,
- files: [
- {
- path: "/workspace/src/MessageChangeSummaryPanel.tsx",
- added: 2,
- removed: 1,
- writeCount: 1,
- lastTurnIndex: 1,
- },
- {
- path: "/workspace/src/App.tsx",
- added: 6,
- removed: 2,
- writeCount: 3,
- lastTurnIndex: 0,
- },
- ],
- },
- changeDetails: {
- turnIndex: 1,
- snapshots: [
- {
- path: "/workspace/src/MessageChangeSummaryPanel.tsx",
- beforeContent: "const next = 1;\n",
- afterContent: "const next = 2;\n",
- writeCount: 1,
- },
- ],
- },
- });
- expect(html).toContain("1 个文件已更改");
- expect(html).toContain("+1");
- expect(html).not.toContain("2 个文件已更改");
- expect(html).not.toContain("src/App.tsx");
- expect(html).not.toContain("+8");
- });
- it("为打开按钮构建 MultiFileDiff code-viewer source", () => {
- expect(
- buildChangeSummaryDiffViewerSource({
- relativePath: "src/App.tsx",
- snapshot: {
- path: "/workspace/src/App.tsx",
- beforeContent: "const value = 1;\n",
- afterContent: "const value = 2;\n",
- writeCount: 1,
- },
- }),
- ).toEqual({
- type: "multi-file-diff",
- title: "src/App.tsx",
- path: "/workspace/src/App.tsx",
- beforeContent: "const value = 1;\n",
- afterContent: "const value = 2;\n",
- });
- });
- it("为打开按钮构建普通文件 preview source", () => {
- expect(
- buildChangeSummaryFilePreviewSource({
- path: "/workspace/src/App.tsx",
- relativePath: "src/App.tsx",
- workspacePath: "/workspace",
- workspaceIdentity: "remote:ssh:host-a:/workspace",
- workspaceRemoteSessionId: "session-a",
- }),
- ).toEqual({
- type: "file",
- title: "src/App.tsx",
- path: "/workspace/src/App.tsx",
- workspacePath: "/workspace",
- workspaceIdentity: "remote:ssh:host-a:/workspace",
- workspaceRemoteSessionId: "session-a",
- });
- });
- it("为工具聚合的 readonly patch 构建 patch preview source", () => {
- expect(
- buildChangeSummaryDiffViewerSource({
- relativePath: "src/App.tsx",
- path: "/workspace/src/App.tsx",
- snapshot: undefined,
- patch: "--- a/App.tsx\n+++ b/App.tsx\n@@ -1 +1 @@\n-old\n+new",
- }),
- ).toEqual({
- type: "patch",
- title: "src/App.tsx",
- path: "/workspace/src/App.tsx",
- patch: "--- a/App.tsx\n+++ b/App.tsx\n@@ -1 +1 @@\n-old\n+new",
- });
- });
- it("文件行和 Review 按钮复用同一个 diff 打开事件", () => {
- const source = buildChangeSummaryDiffViewerSource({
- relativePath: "src/App.tsx",
- path: "/workspace/src/App.tsx",
- snapshot: undefined,
- patch: "--- a/App.tsx\n+++ b/App.tsx\n@@ -1 +1 @@\n-old\n+new",
- });
- const onOpenCodeViewer = vi.fn();
- expect(openChangeSummaryDiffViewer(source, onOpenCodeViewer)).toBe(true);
- expect(onOpenCodeViewer).toHaveBeenCalledWith(source);
- onOpenCodeViewer.mockClear();
- expect(openChangeSummaryDiffViewer(undefined, onOpenCodeViewer)).toBe(false);
- expect(openChangeSummaryDiffViewer(source, undefined)).toBe(false);
- expect(onOpenCodeViewer).not.toHaveBeenCalled();
- });
- it("工具聚合摘要没有真实 checkpoint 时不展示撤销入口", () => {
- const html = renderPanel({
- changeDetails: null,
- onToggleFiles: vi.fn(),
- readonlyDiffPatchesByPath: new Map([
- [
- "/workspace/src/MessageChangeSummaryPanel.tsx",
- "--- a/MessageChangeSummaryPanel.tsx\n+++ b/MessageChangeSummaryPanel.tsx\n@@ -1 +1 @@\n-old\n+new",
- ],
- ]),
- });
- expect(html).toContain("1 个文件已更改");
- expect(html).not.toContain("撤销");
- });
- });
|