taskListMetaSync.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { beforeEach, describe, expect, it } from "vitest";
  2. import type { ZCodeTaskMeta } from "@zcode/shared";
  3. import { insertTaskIntoTaskCaches, syncTaskMetaToTaskCaches } from "@/lib/taskListMetaSync.js";
  4. import { buildTaskListCacheDescriptor, buildTaskWorkspaceKey } from "@/lib/taskQueryCache.js";
  5. import { getTaskMeta, useZCodeSessionStore } from "@/store/zcodeSessionStore.js";
  6. import {
  7. upsertTaskQueryCacheTaskMeta,
  8. useTaskQueryCacheStore,
  9. } from "@/store/taskQueryCacheStore.js";
  10. const WORKSPACE_PATH = "/repo/goal-clear-cache";
  11. const TASK_ID = "task-goal-clear-cache";
  12. function createTaskMeta(overrides: Partial<ZCodeTaskMeta> = {}): ZCodeTaskMeta {
  13. return {
  14. createdAt: 1,
  15. mode: "default",
  16. provider: "glm",
  17. taskId: TASK_ID,
  18. title: "目标清理任务",
  19. traceId: "trace-goal-clear-cache",
  20. updatedAt: 1,
  21. workspacePath: WORKSPACE_PATH,
  22. ...overrides,
  23. };
  24. }
  25. beforeEach(() => {
  26. useZCodeSessionStore.setState((state) => ({
  27. ...state,
  28. workspaces: {},
  29. }));
  30. useTaskQueryCacheStore.getState().clearAll();
  31. });
  32. describe("syncTaskMetaToTaskCaches", () => {
  33. it("首发 optimistic meta 已存在时仍按新成员增加折叠 workspace 列表 total", () => {
  34. const existingTasks = Array.from({ length: 5 }, (_, index) =>
  35. createTaskMeta({
  36. taskId: `existing-${index}`,
  37. title: `已有任务 ${index}`,
  38. updatedAt: 50 - index,
  39. }),
  40. );
  41. const firstSendTask = createTaskMeta({
  42. taskId: TASK_ID,
  43. title: "第六个首发任务",
  44. updatedAt: 100,
  45. });
  46. const descriptor = buildTaskListCacheDescriptor({
  47. kind: "workspace",
  48. workspaceScopes: [{ workspacePath: WORKSPACE_PATH }],
  49. sortBy: "updated",
  50. expanded: false,
  51. visibleLimit: 5,
  52. });
  53. const queryKey = "collapsed-workspace-query";
  54. const store = useZCodeSessionStore.getState();
  55. store.setTaskListCache(WORKSPACE_PATH, existingTasks);
  56. for (const task of existingTasks) {
  57. store.upsertOptimisticTaskListItem(WORKSPACE_PATH, task);
  58. }
  59. useTaskQueryCacheStore.getState().setQueryResult({
  60. queryKey,
  61. descriptor,
  62. items: existingTasks,
  63. total: 5,
  64. hasMore: false,
  65. });
  66. store.upsertOptimisticTaskListItem(WORKSPACE_PATH, firstSendTask);
  67. insertTaskIntoTaskCaches({
  68. workspacePath: WORKSPACE_PATH,
  69. task: firstSendTask,
  70. membership: { pinned: false, archived: false },
  71. });
  72. const cacheState = useTaskQueryCacheStore.getState();
  73. const result = cacheState.resultsByQueryKey[queryKey];
  74. const taskIds = result?.taskKeys.map(
  75. (taskKey) => cacheState.taskMetaByEntityKey[taskKey]?.taskId,
  76. );
  77. expect(taskIds).toEqual([TASK_ID, "existing-0", "existing-1", "existing-2", "existing-3"]);
  78. expect(result?.total).toBe(6);
  79. expect(result?.hasMore).toBe(true);
  80. expect(result?.stale).toBe(true);
  81. });
  82. it("不会让旧快照覆盖首发乐观任务的 updatedAt 和标题", () => {
  83. const optimisticTask = createTaskMeta({
  84. title: "首发问题标题",
  85. updatedAt: 100,
  86. });
  87. const staleSnapshotTask = createTaskMeta({
  88. title: "New session",
  89. updatedAt: 10,
  90. });
  91. const store = useZCodeSessionStore.getState();
  92. store.setTaskListCache(WORKSPACE_PATH, [optimisticTask]);
  93. store.upsertOptimisticTaskListItem(WORKSPACE_PATH, optimisticTask);
  94. upsertTaskQueryCacheTaskMeta(optimisticTask);
  95. syncTaskMetaToTaskCaches({
  96. workspacePath: WORKSPACE_PATH,
  97. task: staleSnapshotTask,
  98. membership: { pinned: false, archived: false },
  99. });
  100. const workspaceState = useZCodeSessionStore.getState().getWorkspaceState(WORKSPACE_PATH);
  101. const entityKey = `${buildTaskWorkspaceKey(WORKSPACE_PATH)}::${TASK_ID}`;
  102. expect(getTaskMeta(workspaceState, TASK_ID)?.title).toBe("首发问题标题");
  103. expect(getTaskMeta(workspaceState, TASK_ID)?.updatedAt).toBe(100);
  104. expect(workspaceState.taskListCache?.[0]?.title).toBe("首发问题标题");
  105. expect(useTaskQueryCacheStore.getState().taskMetaByEntityKey[entityKey]?.updatedAt).toBe(100);
  106. });
  107. it("workspace cache 为空时也不会让 raw snapshot 覆盖 query cache 的手动标题", () => {
  108. const indexedRenamedTask = createTaskMeta({
  109. title: "airline-1",
  110. titleOverridden: true,
  111. updatedAt: 20,
  112. });
  113. const rawSnapshotTask = createTaskMeta({
  114. title: "Analyze airline import flow",
  115. status: "running",
  116. updatedAt: 30,
  117. });
  118. const entityKey = `${buildTaskWorkspaceKey(WORKSPACE_PATH)}::${TASK_ID}`;
  119. upsertTaskQueryCacheTaskMeta(indexedRenamedTask);
  120. syncTaskMetaToTaskCaches({
  121. workspacePath: WORKSPACE_PATH,
  122. task: rawSnapshotTask,
  123. });
  124. const workspaceState = useZCodeSessionStore.getState().getWorkspaceState(WORKSPACE_PATH);
  125. const workspaceTask = getTaskMeta(workspaceState, TASK_ID);
  126. const queryTask = useTaskQueryCacheStore.getState().taskMetaByEntityKey[entityKey];
  127. expect(workspaceTask?.title).toBe("airline-1");
  128. expect(workspaceTask?.titleOverridden).toBe(true);
  129. expect(workspaceTask?.status).toBe("running");
  130. expect(workspaceTask?.updatedAt).toBe(30);
  131. expect(queryTask?.title).toBe("airline-1");
  132. expect(queryTask?.titleOverridden).toBe(true);
  133. expect(queryTask?.status).toBe("running");
  134. });
  135. });