taskListChangeSummary.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. import { createElement, type ReactElement } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import type { ZCodeTaskMeta } from "@zcode/shared";
  5. import { MemoTaskItem as TaskItem } from "@/TaskListItem.js";
  6. import { TooltipProvider } from "@/components/ui/tooltip.js";
  7. const { workspaceStateByPath } = vi.hoisted(() => ({
  8. workspaceStateByPath: new Map<string, unknown>(),
  9. }));
  10. vi.mock("@/store/zcodeSessionStore.js", async () => {
  11. const actual = await vi.importActual<typeof import("@/store/zcodeSessionStore.js")>(
  12. "@/store/zcodeSessionStore.js",
  13. );
  14. return {
  15. ...actual,
  16. useZCodeSessionStore: (selector: (state: { workspaces: Record<string, unknown> }) => unknown) =>
  17. selector({ workspaces: Object.fromEntries(workspaceStateByPath) }),
  18. };
  19. });
  20. vi.mock("@/useTaskListItemContextActions.js", () => ({
  21. useTaskListItemContextActions: () => ({
  22. taskProvider: "codex",
  23. taskSessionFile: { loading: false, path: null, exists: false },
  24. taskNativeSessionLogFile: { loading: false, path: null, exists: false },
  25. providerConfigFile: { loading: false, path: null, exists: false },
  26. fileManagerLabel: "Open in Finder",
  27. handleCopyText: async () => {},
  28. handleOpenTaskPathInFileManager: async () => {},
  29. handleOpenProviderConfig: async () => {},
  30. }),
  31. }));
  32. beforeEach(() => {
  33. workspaceStateByPath.clear();
  34. });
  35. function createTaskMeta(overrides: Partial<ZCodeTaskMeta> = {}): ZCodeTaskMeta {
  36. return {
  37. taskId: "task-change-summary",
  38. traceId: "trace-change-summary",
  39. title: "汇总任务文件改动",
  40. workspacePath: "/repo/workspace",
  41. createdAt: 1,
  42. updatedAt: 1,
  43. mode: "default",
  44. provider: "codex",
  45. ...overrides,
  46. };
  47. }
  48. function createIntl() {
  49. return {
  50. formatMessage: ({ id }: { id: string }, values?: Record<string, string>) => {
  51. const messages: Record<string, string> = {
  52. "taskList.permissionTag": "等待确认",
  53. "taskList.stopCountdown": "停止计时",
  54. "taskList.untitled": "新对话",
  55. "taskList.justNow": "刚刚",
  56. "taskList.minutesAgo": "{minutes}分",
  57. "taskList.hoursAgo": "{hours}小时",
  58. "taskList.daysAgo": "{days}天",
  59. "taskList.archive": "归档任务",
  60. "taskList.changeStats": "+{added} -{removed}",
  61. "common.confirm": "确认",
  62. };
  63. let message = messages[id] ?? id;
  64. if (values) {
  65. Object.entries(values).forEach(([key, value]) => {
  66. message = message.replaceAll(`{${key}}`, value);
  67. });
  68. }
  69. return message;
  70. },
  71. };
  72. }
  73. function renderWithTooltipProvider(element: ReactElement) {
  74. return renderToStaticMarkup(createElement(TooltipProvider, null, element));
  75. }
  76. function renderTaskItem(
  77. task: ZCodeTaskMeta = createTaskMeta({
  78. changeSummary: {
  79. fileCount: 2,
  80. added: 8,
  81. removed: 3,
  82. files: [
  83. {
  84. path: "/repo/workspace/packages/ui/src/TaskList.tsx",
  85. added: 5,
  86. removed: 2,
  87. writeCount: 2,
  88. lastTurnIndex: 1,
  89. },
  90. {
  91. path: "/repo/workspace/packages/ui/src/App.tsx",
  92. added: 3,
  93. removed: 1,
  94. writeCount: 1,
  95. lastTurnIndex: 1,
  96. },
  97. ],
  98. },
  99. }),
  100. props: Partial<Parameters<typeof TaskItem>[0]> = {},
  101. ) {
  102. workspaceStateByPath.set(task.workspacePath, {
  103. taskRuntimeByTaskId: {
  104. [task.taskId]: {
  105. status: "completed",
  106. error: null,
  107. usage: null,
  108. },
  109. },
  110. taskUiByTaskId: {
  111. [task.taskId]: {
  112. permissionRequest: null,
  113. pendingPermissionRequests: [],
  114. },
  115. },
  116. taskUnreadByTaskId: {},
  117. });
  118. return renderWithTooltipProvider(
  119. createElement(TaskItem, {
  120. workspacePath: task.workspacePath,
  121. task,
  122. isActive: false,
  123. onSelectTask: () => {},
  124. onArchiveTaskInline: () => {},
  125. onCancelArchiveConfirm: () => {},
  126. isArchiveConfirming: false,
  127. intl: createIntl(),
  128. ...props,
  129. }),
  130. );
  131. }
  132. describe("TaskList change summary", () => {
  133. it("任务标题右侧只显示总增删,不再显示文件数量和文件列表", () => {
  134. const html = renderTaskItem();
  135. expect(html).toContain("汇总任务文件改动");
  136. expect(html).toContain(">+8</span>");
  137. expect(html).toContain(">-3</span>");
  138. expect(html).not.toContain("2 个文件");
  139. expect(html).not.toContain("packages/ui/src/TaskList.tsx +5 -2");
  140. expect(html).not.toContain("packages/ui/src/App.tsx +3 -1");
  141. });
  142. it("timeline variant 把 workspace 和改动统计放到第二行", () => {
  143. const html = renderTaskItem(undefined, { variant: "timeline" });
  144. expect(html).toContain("汇总任务文件改动");
  145. expect(html).toContain("workspace");
  146. expect(html).toContain("+8");
  147. expect(html).toContain("-3");
  148. });
  149. it("timeline variant 在闲置状态显示灰点", () => {
  150. const html = renderTaskItem(undefined, { variant: "timeline" });
  151. const defaultHtml = renderTaskItem();
  152. expect(html).toContain('data-idle-indicator="true"');
  153. expect(defaultHtml).not.toContain('data-idle-indicator="true"');
  154. });
  155. it("workspace 和 timeline 标题交给运行时溢出测量,不默认挂载渐隐", () => {
  156. const defaultHtml = renderTaskItem();
  157. const timelineHtml = renderTaskItem(undefined, { variant: "timeline" });
  158. expect(defaultHtml).not.toContain("mask-image:linear-gradient");
  159. expect(timelineHtml).not.toContain("mask-image:linear-gradient");
  160. expect(defaultHtml).not.toContain("truncate text-ui-base text-foreground");
  161. expect(timelineHtml).not.toContain("truncate text-ui-base text-foreground");
  162. });
  163. });