useWorkspaceActiveTaskState.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { act, createElement } from "react";
  2. import { createRoot, type Root } from "react-dom/client";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import type { ZCodeTaskMeta } from "@zcode/shared";
  5. import { useWorkspaceActiveTaskState } from "@/hooks/useWorkspaceActiveTaskState.js";
  6. const { activeWorkspaceState } = vi.hoisted(() => ({
  7. activeWorkspaceState: {
  8. current: {
  9. optimisticTaskListByTaskId: {} as Record<string, ZCodeTaskMeta>,
  10. taskListCache: [] as ZCodeTaskMeta[],
  11. taskMessagesByTaskId: {} as Record<string, unknown[]>,
  12. },
  13. },
  14. }));
  15. vi.mock("@/store/zcodeSessionStore.js", () => ({
  16. getTaskMessages: (
  17. workspaceState: { taskMessagesByTaskId?: Record<string, unknown[]> },
  18. taskId: string,
  19. ) => workspaceState.taskMessagesByTaskId?.[taskId] ?? [],
  20. getTaskMeta: (
  21. workspaceState: {
  22. optimisticTaskListByTaskId?: Record<string, ZCodeTaskMeta>;
  23. taskListCache?: ZCodeTaskMeta[];
  24. },
  25. taskId: string,
  26. ) =>
  27. workspaceState.optimisticTaskListByTaskId?.[taskId] ??
  28. workspaceState.taskListCache?.find((task) => task.taskId === taskId) ??
  29. null,
  30. selectWorkspaceZCodeState: () => activeWorkspaceState.current,
  31. useZCodeSessionStore: (selector: (state: Record<string, unknown>) => unknown) =>
  32. selector({}),
  33. }));
  34. vi.mock("@/store/taskQueryCacheStore.js", () => ({
  35. useTaskQueryCacheStore: (selector: (state: { taskMetaByEntityKey: {} }) => unknown) =>
  36. selector({ taskMetaByEntityKey: {} }),
  37. }));
  38. vi.mock("@/hooks/useActiveTaskSnapshotMeta.js", () => ({
  39. useActiveTaskSnapshotMeta: () => null,
  40. }));
  41. vi.mock("@/hooks/useTaskNativeSessionLogFile.js", () => ({
  42. useTaskNativeSessionLogFile: () => ({
  43. exists: false,
  44. loading: false,
  45. path: null,
  46. provider: null,
  47. }),
  48. }));
  49. vi.mock("@/hooks/useTaskSessionFilePath.js", () => ({
  50. useTaskSessionFilePath: () => ({
  51. exists: false,
  52. loading: false,
  53. path: null,
  54. }),
  55. }));
  56. function createTaskMeta(overrides: Partial<ZCodeTaskMeta> = {}): ZCodeTaskMeta {
  57. return {
  58. createdAt: 1,
  59. mode: "default",
  60. provider: "codex",
  61. taskId: "task-1",
  62. title: "Task 1",
  63. traceId: "trace-1",
  64. updatedAt: 2,
  65. workspacePath: "/workspace",
  66. ...overrides,
  67. };
  68. }
  69. function createMinimalElement(ownerDocument: Document, tagName = "div") {
  70. const element = {
  71. addEventListener: () => {},
  72. appendChild: (child: { parentNode?: unknown }) => {
  73. child.parentNode = element;
  74. return child;
  75. },
  76. childNodes: [] as unknown[],
  77. getAttribute: () => null,
  78. insertBefore: (child: { parentNode?: unknown }) => {
  79. child.parentNode = element;
  80. return child;
  81. },
  82. nodeName: tagName.toUpperCase(),
  83. nodeType: 1,
  84. ownerDocument,
  85. parentNode: null as unknown,
  86. removeAttribute: () => {},
  87. removeChild: (child: { parentNode?: unknown }) => {
  88. child.parentNode = null;
  89. return child;
  90. },
  91. removeEventListener: () => {},
  92. setAttribute: () => {},
  93. style: {},
  94. tagName: tagName.toUpperCase(),
  95. };
  96. return element as unknown as Element;
  97. }
  98. function installMinimalDom() {
  99. const documentMock = {
  100. addEventListener: () => {},
  101. createElement: (tagName: string) =>
  102. createMinimalElement(documentMock as unknown as Document, tagName),
  103. createTextNode: (nodeValue: string) => ({
  104. nodeType: 3,
  105. nodeValue,
  106. ownerDocument: documentMock,
  107. parentNode: null,
  108. }),
  109. nodeType: 9,
  110. removeEventListener: () => {},
  111. } as unknown as Document;
  112. const windowMock = {
  113. addEventListener: () => {},
  114. document: documentMock,
  115. HTMLIFrameElement: function HTMLIFrameElement() {},
  116. HTMLElement: function HTMLElement() {},
  117. Node: function Node() {},
  118. removeEventListener: () => {},
  119. };
  120. Object.defineProperty(globalThis, "document", {
  121. configurable: true,
  122. value: documentMock,
  123. });
  124. Object.defineProperty(globalThis, "window", {
  125. configurable: true,
  126. value: windowMock,
  127. });
  128. Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
  129. configurable: true,
  130. value: true,
  131. });
  132. return createMinimalElement(documentMock);
  133. }
  134. describe("useWorkspaceActiveTaskState", () => {
  135. afterEach(() => {
  136. activeWorkspaceState.current = {
  137. optimisticTaskListByTaskId: {},
  138. taskListCache: [],
  139. taskMessagesByTaskId: {},
  140. };
  141. delete (globalThis as { document?: unknown }).document;
  142. delete (globalThis as { window?: unknown }).window;
  143. delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown }).IS_REACT_ACT_ENVIRONMENT;
  144. });
  145. it("reuses resolvedActiveTaskMeta reference when stream updates keep meta fields equal", () => {
  146. const observedMetas: Array<ZCodeTaskMeta | null | undefined> = [];
  147. const root: Root = createRoot(installMinimalDom());
  148. function Probe({ renderId }: { renderId: number }) {
  149. const result = useWorkspaceActiveTaskState({
  150. activeTaskId: "task-1",
  151. intl: {
  152. formatMessage: ({ id }: { id: string }) => id,
  153. },
  154. selectedProvider: "codex",
  155. workspaceAbsPath: "/workspace",
  156. });
  157. observedMetas.push(result.resolvedActiveTaskMeta);
  158. return createElement("span", null, renderId);
  159. }
  160. activeWorkspaceState.current = {
  161. optimisticTaskListByTaskId: {},
  162. taskListCache: [createTaskMeta()],
  163. taskMessagesByTaskId: {},
  164. };
  165. act(() => {
  166. root.render(createElement(Probe, { renderId: 1 }));
  167. });
  168. activeWorkspaceState.current = {
  169. optimisticTaskListByTaskId: {},
  170. taskListCache: [createTaskMeta()],
  171. taskMessagesByTaskId: {
  172. "task-1": [{ id: "stream-update" }],
  173. },
  174. };
  175. act(() => {
  176. root.render(createElement(Probe, { renderId: 2 }));
  177. });
  178. expect(observedMetas).toHaveLength(2);
  179. expect(observedMetas[1]).toBe(observedMetas[0]);
  180. act(() => {
  181. root.unmount();
  182. });
  183. });
  184. });