taskListMobileActiveIndicator.test.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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: "mobile-active-task",
  38. traceId: "trace-mobile-active-task",
  39. title: "手机正在查看的任务",
  40. workspacePath: "/tmp/workspace-mobile-active",
  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.mobileActive": "手机正在操作此任务",
  53. "taskList.permissionTag": "等待确认",
  54. "taskList.stopCountdown": "停止计时",
  55. "taskList.untitled": "新对话",
  56. "taskList.justNow": "刚刚",
  57. "taskList.minutesAgo": "{minutes}分",
  58. "taskList.hoursAgo": "{hours}小时",
  59. "taskList.daysAgo": "{days}天",
  60. "taskList.archive": "归档任务",
  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 = createTaskMeta(),
  78. isPinned = false,
  79. isMobileActive = true,
  80. legacyUnread = Boolean(task.unreadAt),
  81. }: {
  82. task?: ZCodeTaskMeta;
  83. isPinned?: boolean;
  84. isMobileActive?: boolean;
  85. legacyUnread?: boolean;
  86. } = {}) {
  87. workspaceStateByPath.set(task.workspacePath, {
  88. taskRuntimeByTaskId: {
  89. [task.taskId]: {
  90. status: "completed",
  91. error: null,
  92. usage: null,
  93. },
  94. },
  95. taskUiByTaskId: {
  96. [task.taskId]: {
  97. permissionRequest: null,
  98. pendingPermissionRequests: [],
  99. },
  100. },
  101. taskUnreadByTaskId: legacyUnread ? { [task.taskId]: true } : {},
  102. });
  103. return renderWithTooltipProvider(
  104. createElement(TaskItem, {
  105. workspacePath: task.workspacePath,
  106. task,
  107. isPinned,
  108. isActive: false,
  109. isMobileActive,
  110. onSelectTask: () => {},
  111. onArchiveTaskInline: () => {},
  112. onCancelArchiveConfirm: () => {},
  113. isArchiveConfirming: false,
  114. intl: createIntl(),
  115. }),
  116. );
  117. }
  118. describe("TaskList mobile active indicator", () => {
  119. it("shows a phone marker on the desktop task row that is open on mobile", () => {
  120. const html = renderTaskItem();
  121. expect(html).toContain('data-mobile-active-task="true"');
  122. expect(html).toContain("手机正在操作此任务");
  123. });
  124. it("renders unread dot with sky colors", () => {
  125. const html = renderTaskItem({
  126. task: createTaskMeta({ unreadAt: 100 }),
  127. isMobileActive: false,
  128. legacyUnread: false,
  129. });
  130. expect(html).toContain('data-unread-indicator="true"');
  131. expect(html).toContain("bg-sky-500");
  132. expect(html).toContain("dark:bg-sky-400");
  133. expect(html).not.toContain("bg-brand");
  134. });
  135. it("renders error dot from task meta status", () => {
  136. const html = renderTaskItem({
  137. task: createTaskMeta({ status: "error", unreadAt: 100 }),
  138. isMobileActive: false,
  139. legacyUnread: false,
  140. });
  141. expect(html).toContain('data-error-indicator="true"');
  142. expect(html).toContain("bg-destructive");
  143. expect(html).not.toContain('data-unread-indicator="true"');
  144. });
  145. it("places the phone marker before the task title text", () => {
  146. const html = renderTaskItem();
  147. expect(html.indexOf('aria-label="手机正在操作此任务"')).toBeLessThan(
  148. html.indexOf('data-task-title-copy="original"'),
  149. );
  150. expect(html).not.toContain('title="手机正在查看的任务"');
  151. });
  152. it("keeps the phone marker out of the title flex flow", () => {
  153. const html = renderTaskItem();
  154. expect(html).toMatch(
  155. /<span data-mobile-active-task="true" class="[^"]*absolute[^"]*" aria-label="手机正在操作此任务" data-state="closed" data-slot="tooltip-trigger">/,
  156. );
  157. });
  158. it("keeps the phone marker mounted until React interaction state replaces it", () => {
  159. const html = renderTaskItem();
  160. expect(html).toMatch(
  161. /<span data-mobile-active-task="true"[^>]*aria-label="手机正在操作此任务"[^>]*data-slot="tooltip-trigger">/,
  162. );
  163. expect(html).not.toContain("group-hover/task-item:hidden");
  164. expect(html).not.toContain('aria-label="taskList.pin"');
  165. });
  166. it("does not mount unpin until interaction when the phone marker owns the leading slot", () => {
  167. const html = renderTaskItem({ isPinned: true });
  168. expect(html).toMatch(
  169. /<span data-mobile-active-task="true"[^>]*aria-label="手机正在操作此任务"[^>]*data-slot="tooltip-trigger">/,
  170. );
  171. expect(html).not.toContain('aria-label="taskList.unpin"');
  172. });
  173. });