taskListRenderStability.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import { act, createElement, type ReactNode } from "react";
  2. import { createRoot, type Root } from "react-dom/client";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import type { ZCodeTaskMeta } from "@zcode/shared";
  6. const {
  7. memoTaskItemCalls,
  8. taskListItemContextMenuContentCalls,
  9. taskRenameDialogCalls,
  10. stableIntl,
  11. stableStartDraft,
  12. stableOpenSettingsTab,
  13. } = vi.hoisted(() => ({
  14. memoTaskItemCalls: [] as Array<Record<string, unknown>>,
  15. taskListItemContextMenuContentCalls: [] as Array<Record<string, unknown>>,
  16. taskRenameDialogCalls: [] as Array<Record<string, unknown>>,
  17. stableIntl: {
  18. formatMessage: ({ id }: { id: string }) => id,
  19. },
  20. stableOpenSettingsTab: vi.fn(),
  21. stableStartDraft: vi.fn(),
  22. }));
  23. vi.mock("lucide-react", () => ({
  24. Settings2: (props: Record<string, unknown>) =>
  25. createElement("svg", { "data-icon": "settings", ...props }),
  26. }));
  27. vi.mock("@/components/ui/button.js", () => ({
  28. Button: ({ children, ...props }: { children?: ReactNode }) =>
  29. createElement("button", props, children),
  30. }));
  31. vi.mock("@/components/ui/context-menu.js", () => ({
  32. ContextMenu: ({ children }: { children?: ReactNode }) => createElement("div", null, children),
  33. ContextMenuTrigger: ({ children }: { children?: ReactNode }) => children,
  34. }));
  35. vi.mock("@/components/ui/toast.js", () => ({
  36. toast: () => {},
  37. }));
  38. vi.mock("@/i18n/IntlProvider.js", () => ({
  39. useZCodeIntl: () => ({ intl: stableIntl }),
  40. }));
  41. vi.mock("@/store/zcodeSessionStore.js", () => ({
  42. useZCodeSessionStore: (selector: (state: { startDraft: () => void }) => unknown) =>
  43. selector({ startDraft: stableStartDraft }),
  44. }));
  45. vi.mock("@/store/TabStoreProvider.js", () => ({
  46. useTabStore: (selector: (state: { openSettingsTab: () => void }) => unknown) =>
  47. selector({ openSettingsTab: stableOpenSettingsTab }),
  48. }));
  49. vi.mock("@/NewTaskButtonGroup.js", () => ({
  50. NewTaskButtonGroup: () => null,
  51. }));
  52. vi.mock("@/TaskRenameDialog.js", () => ({
  53. TaskRenameDialog: (props: Record<string, unknown>) => {
  54. taskRenameDialogCalls.push(props);
  55. return null;
  56. },
  57. }));
  58. vi.mock("@/TaskListLoadingHint.js", () => ({
  59. TaskListLoadingHint: () => null,
  60. }));
  61. vi.mock("@/TaskListItem.js", () => ({
  62. MemoTaskItem: (props: Record<string, unknown>) => {
  63. memoTaskItemCalls.push(props);
  64. return createElement("li", null, (props.task as ZCodeTaskMeta).title);
  65. },
  66. TaskListItemContextMenuContent: (props: Record<string, unknown>) => {
  67. taskListItemContextMenuContentCalls.push(props);
  68. return null;
  69. },
  70. }));
  71. function createTask(overrides: Partial<ZCodeTaskMeta> = {}): ZCodeTaskMeta {
  72. return {
  73. createdAt: 1,
  74. mode: "default",
  75. provider: "codex",
  76. taskId: "task-1",
  77. title: "Task 1",
  78. traceId: "trace-1",
  79. updatedAt: 2,
  80. workspacePath: "/workspace",
  81. ...overrides,
  82. };
  83. }
  84. function createMinimalElement(ownerDocument: Document, tagName = "div") {
  85. const element = {
  86. addEventListener: () => {},
  87. appendChild: (child: { parentNode?: unknown }) => {
  88. child.parentNode = element;
  89. return child;
  90. },
  91. childNodes: [] as unknown[],
  92. getAttribute: () => null,
  93. insertBefore: (child: { parentNode?: unknown }) => {
  94. child.parentNode = element;
  95. return child;
  96. },
  97. nodeName: tagName.toUpperCase(),
  98. nodeType: 1,
  99. ownerDocument,
  100. parentNode: null as unknown,
  101. removeAttribute: () => {},
  102. removeChild: (child: { parentNode?: unknown }) => {
  103. child.parentNode = null;
  104. return child;
  105. },
  106. removeEventListener: () => {},
  107. setAttribute: () => {},
  108. style: {},
  109. tagName: tagName.toUpperCase(),
  110. };
  111. return element as unknown as Element;
  112. }
  113. function installMinimalDom() {
  114. const documentMock = {
  115. addEventListener: () => {},
  116. createElement: (tagName: string) =>
  117. createMinimalElement(documentMock as unknown as Document, tagName),
  118. createElementNS: (_namespace: string, tagName: string) =>
  119. createMinimalElement(documentMock as unknown as Document, tagName),
  120. createTextNode: (nodeValue: string) => ({
  121. nodeType: 3,
  122. nodeValue,
  123. ownerDocument: documentMock,
  124. parentNode: null,
  125. }),
  126. nodeType: 9,
  127. removeEventListener: () => {},
  128. } as unknown as Document;
  129. const windowMock = {
  130. addEventListener: () => {},
  131. document: documentMock,
  132. HTMLIFrameElement: function HTMLIFrameElement() {},
  133. HTMLElement: function HTMLElement() {},
  134. Node: function Node() {},
  135. removeEventListener: () => {},
  136. };
  137. Object.defineProperty(globalThis, "document", {
  138. configurable: true,
  139. value: documentMock,
  140. });
  141. Object.defineProperty(globalThis, "window", {
  142. configurable: true,
  143. value: windowMock,
  144. });
  145. Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
  146. configurable: true,
  147. value: true,
  148. });
  149. return createMinimalElement(documentMock);
  150. }
  151. describe("TaskList render stability", () => {
  152. afterEach(() => {
  153. memoTaskItemCalls.length = 0;
  154. taskListItemContextMenuContentCalls.length = 0;
  155. taskRenameDialogCalls.length = 0;
  156. stableOpenSettingsTab.mockReset();
  157. stableStartDraft.mockReset();
  158. delete (globalThis as { document?: unknown }).document;
  159. delete (globalThis as { window?: unknown }).window;
  160. delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown }).IS_REACT_ACT_ENVIRONMENT;
  161. });
  162. it("does not mount the rename dialog while no row is being renamed", async () => {
  163. const { TaskList } = await import("@/TaskList.js");
  164. const root: Root = createRoot(installMinimalDom());
  165. act(() => {
  166. root.render(
  167. createElement(TaskList, {
  168. activeTaskId: null,
  169. isWorkspaceActive: true,
  170. onArchiveTask: vi.fn(async () => null),
  171. onRenameTask: vi.fn(async () => null),
  172. onSelectTask: vi.fn(),
  173. onSetTaskPinned: vi.fn(async () => null),
  174. onSetTaskUnread: vi.fn(async () => null),
  175. showCreateButton: false,
  176. showFooter: false,
  177. tasks: [createTask()],
  178. workspacePath: "/workspace",
  179. }),
  180. );
  181. });
  182. expect(taskRenameDialogCalls).toHaveLength(0);
  183. expect(taskListItemContextMenuContentCalls).toHaveLength(0);
  184. act(() => {
  185. root.unmount();
  186. });
  187. });
  188. it("shows only the one-way show-more entry while another page exists", async () => {
  189. const { TaskList } = await import("@/TaskList.js");
  190. const html = renderToStaticMarkup(
  191. createElement(TaskList, {
  192. activeTaskId: null,
  193. hasMore: true,
  194. onArchiveTask: vi.fn(async () => null),
  195. onRenameTask: vi.fn(async () => null),
  196. onSelectTask: vi.fn(),
  197. onSetTaskPinned: vi.fn(async () => null),
  198. onSetTaskUnread: vi.fn(async () => null),
  199. onShowMore: vi.fn(),
  200. showCreateButton: false,
  201. showFooter: false,
  202. tasks: [createTask()],
  203. workspacePath: "/workspace",
  204. }),
  205. );
  206. expect(html).toContain("taskList.showMore");
  207. expect(html).not.toContain("taskList.showLess");
  208. });
  209. it("hides the show-more entry after the final page", async () => {
  210. const { TaskList } = await import("@/TaskList.js");
  211. const html = renderToStaticMarkup(
  212. createElement(TaskList, {
  213. activeTaskId: null,
  214. hasMore: false,
  215. onArchiveTask: vi.fn(async () => null),
  216. onRenameTask: vi.fn(async () => null),
  217. onSelectTask: vi.fn(),
  218. onSetTaskPinned: vi.fn(async () => null),
  219. onSetTaskUnread: vi.fn(async () => null),
  220. onShowMore: vi.fn(),
  221. showCreateButton: false,
  222. showFooter: false,
  223. tasks: [createTask()],
  224. workspacePath: "/workspace",
  225. }),
  226. );
  227. expect(html).not.toContain("taskList.showMore");
  228. });
  229. it("does not re-render unchanged memo task rows when parent rerenders with identical list props", async () => {
  230. const { TaskList } = await import("@/TaskList.js");
  231. const root: Root = createRoot(installMinimalDom());
  232. const tasks = [createTask()];
  233. const onSelectTask = vi.fn();
  234. const onRenameTask = vi.fn(async () => null);
  235. const onSetTaskPinned = vi.fn(async () => null);
  236. const onArchiveTask = vi.fn(async () => null);
  237. const onSetTaskUnread = vi.fn(async () => null);
  238. function Parent({ tick }: { tick: number }) {
  239. return createElement(
  240. "section",
  241. null,
  242. createElement("span", null, tick),
  243. createElement(TaskList, {
  244. activeTaskId: null,
  245. isWorkspaceActive: true,
  246. onArchiveTask,
  247. onRenameTask,
  248. onSelectTask,
  249. onSetTaskPinned,
  250. onSetTaskUnread,
  251. showCreateButton: false,
  252. showFooter: false,
  253. tasks,
  254. workspacePath: "/workspace",
  255. }),
  256. );
  257. }
  258. act(() => {
  259. root.render(createElement(Parent, { tick: 1 }));
  260. });
  261. act(() => {
  262. root.render(createElement(Parent, { tick: 2 }));
  263. });
  264. expect(memoTaskItemCalls).toHaveLength(1);
  265. act(() => {
  266. root.unmount();
  267. });
  268. });
  269. it("keeps unchanged row action props stable when archive confirmation state changes", async () => {
  270. const { TaskList } = await import("@/TaskList.js");
  271. const root: Root = createRoot(installMinimalDom());
  272. const tasks = [
  273. createTask(),
  274. createTask({
  275. taskId: "task-2",
  276. title: "Task 2",
  277. traceId: "trace-2",
  278. updatedAt: 3,
  279. }),
  280. ];
  281. const onSelectTask = vi.fn();
  282. const onRenameTask = vi.fn(async () => null);
  283. const onSetTaskPinned = vi.fn(async () => null);
  284. const onArchiveTask = vi.fn(async () => null);
  285. const onSetTaskUnread = vi.fn(async () => null);
  286. act(() => {
  287. root.render(
  288. createElement(TaskList, {
  289. activeTaskId: null,
  290. isWorkspaceActive: true,
  291. onArchiveTask,
  292. onRenameTask,
  293. onSelectTask,
  294. onSetTaskPinned,
  295. onSetTaskUnread,
  296. showCreateButton: false,
  297. showFooter: false,
  298. tasks,
  299. workspacePath: "/workspace",
  300. }),
  301. );
  302. });
  303. const untouchedRowInitialProps = memoTaskItemCalls.find(
  304. (props) => (props.task as ZCodeTaskMeta).taskId === "task-2",
  305. );
  306. expect(untouchedRowInitialProps).toBeDefined();
  307. const archiveInline = memoTaskItemCalls[0]?.onArchiveTaskInline;
  308. expect(archiveInline).toBeDefined();
  309. await act(async () => {
  310. await archiveInline?.({ stopPropagation: vi.fn() }, "task-1");
  311. });
  312. const untouchedRowUpdatedProps = memoTaskItemCalls
  313. .slice(2)
  314. .find((props) => (props.task as ZCodeTaskMeta).taskId === "task-2");
  315. expect(untouchedRowUpdatedProps).toBeDefined();
  316. expect(untouchedRowUpdatedProps?.onSelectTask).toBe(untouchedRowInitialProps?.onSelectTask);
  317. expect(untouchedRowUpdatedProps?.onArchiveTaskInline).toBe(
  318. untouchedRowInitialProps?.onArchiveTaskInline,
  319. );
  320. expect(untouchedRowUpdatedProps?.onTogglePinTask).toBe(
  321. untouchedRowInitialProps?.onTogglePinTask,
  322. );
  323. expect(untouchedRowUpdatedProps?.onStartRenameTask).toBe(
  324. untouchedRowInitialProps?.onStartRenameTask,
  325. );
  326. expect(untouchedRowUpdatedProps?.onArchiveTask).toBe(untouchedRowInitialProps?.onArchiveTask);
  327. expect(untouchedRowUpdatedProps?.onMarkTaskAsUnread).toBe(
  328. untouchedRowInitialProps?.onMarkTaskAsUnread,
  329. );
  330. act(() => {
  331. root.unmount();
  332. });
  333. });
  334. });