taskQueryCacheStore.test.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import type { ZCodeTaskMeta } from "@zcode/shared";
  3. import {
  4. buildTaskListCacheDescriptor,
  5. buildTaskListCacheKeyFromDescriptor,
  6. buildTaskWorkspaceKey,
  7. } from "@/lib/taskQueryCache.js";
  8. import {
  9. applyTaskQueryCacheMutation,
  10. removeTaskFromTaskQueryCaches,
  11. updateTaskQueryCacheTaskMetaPreservingMembership,
  12. useTaskQueryCacheStore,
  13. } from "@/store/taskQueryCacheStore.js";
  14. import { subscribeTaskLifecycle } from "@/lib/taskLifecycleEvents.js";
  15. import { attachTaskListRowActivity, getTaskListRowActivity } from "@/v4/taskListRowActivity.js";
  16. const WORKSPACE_PATH = "/repo/app";
  17. function createTaskMeta(overrides: Partial<ZCodeTaskMeta>): ZCodeTaskMeta {
  18. return {
  19. taskId: "task-default",
  20. traceId: "trace-default",
  21. title: "默认任务",
  22. workspacePath: WORKSPACE_PATH,
  23. createdAt: 1,
  24. updatedAt: 1,
  25. mode: "default",
  26. provider: "codex",
  27. ...overrides,
  28. };
  29. }
  30. function createDescriptor(params: {
  31. kind: "workspace" | "pinned" | "archived" | "timeline" | "active";
  32. visibleLimit?: number | null;
  33. search?: string;
  34. }) {
  35. return buildTaskListCacheDescriptor({
  36. kind: params.kind,
  37. workspaceScopes: [{ workspacePath: WORKSPACE_PATH }],
  38. sortBy: "updated",
  39. search: params.search,
  40. expanded: params.visibleLimit === null,
  41. visibleLimit: params.visibleLimit,
  42. });
  43. }
  44. function taskIdsFor(queryKey: string): string[] {
  45. const state = useTaskQueryCacheStore.getState();
  46. const result = state.resultsByQueryKey[queryKey];
  47. if (!result) {
  48. return [];
  49. }
  50. return result.taskKeys.map((taskKey) => state.taskMetaByEntityKey[taskKey]?.taskId ?? "");
  51. }
  52. afterEach(() => {
  53. useTaskQueryCacheStore.getState().clearAll();
  54. });
  55. describe("taskQueryCache key", () => {
  56. it("区分折叠列表的不同可见数量", () => {
  57. const firstPageKey = buildTaskListCacheKeyFromDescriptor(
  58. createDescriptor({ kind: "timeline", visibleLimit: 20 }),
  59. );
  60. const expandedPageKey = buildTaskListCacheKeyFromDescriptor(
  61. createDescriptor({ kind: "timeline", visibleLimit: 40 }),
  62. );
  63. expect(firstPageKey).not.toBe(expandedPageKey);
  64. expect(expandedPageKey).toContain("limit=40");
  65. });
  66. });
  67. describe("taskQueryCacheStore clearAll", () => {
  68. it("清空 query result 和 task meta", () => {
  69. const queryKey = "workspace-clear-all";
  70. const task = createTaskMeta({ taskId: "task-clear-all", updatedAt: 10 });
  71. useTaskQueryCacheStore.getState().setQueryResult({
  72. queryKey,
  73. descriptor: createDescriptor({ kind: "workspace", visibleLimit: null }),
  74. items: [task],
  75. total: 1,
  76. hasMore: false,
  77. });
  78. useTaskQueryCacheStore.getState().clearAll();
  79. expect(useTaskQueryCacheStore.getState().resultsByQueryKey).toEqual({});
  80. expect(useTaskQueryCacheStore.getState().taskMetaByEntityKey).toEqual({});
  81. });
  82. });
  83. describe("taskQueryCacheStore invalidation generation", () => {
  84. it("连续失效会递增代次,并拒绝旧异步结果清除 stale", () => {
  85. const queryKey = "workspace-invalidation-generation";
  86. const descriptor = createDescriptor({ kind: "workspace", visibleLimit: null });
  87. const completed = createTaskMeta({
  88. taskId: "task-generation",
  89. title: "completed",
  90. status: "completed",
  91. updatedAt: 10,
  92. });
  93. const staleRunning = createTaskMeta({
  94. taskId: "task-generation",
  95. title: "stale-running",
  96. status: "running",
  97. updatedAt: 20,
  98. });
  99. useTaskQueryCacheStore.getState().setQueryResult({
  100. queryKey,
  101. descriptor,
  102. items: [completed],
  103. total: 1,
  104. hasMore: false,
  105. });
  106. useTaskQueryCacheStore.getState().markWorkspaceKeysStale([WORKSPACE_PATH]);
  107. useTaskQueryCacheStore.getState().markWorkspaceKeysStale([WORKSPACE_PATH]);
  108. const invalidated = useTaskQueryCacheStore.getState().resultsByQueryKey[queryKey];
  109. expect(invalidated?.stale).toBe(true);
  110. expect(invalidated?.invalidationVersion).toBe(2);
  111. useTaskQueryCacheStore.getState().setQueryResults([
  112. {
  113. queryKey,
  114. descriptor,
  115. items: [staleRunning],
  116. total: 1,
  117. hasMore: false,
  118. expectedInvalidationVersion: 0,
  119. },
  120. ]);
  121. const afterStaleResponse = useTaskQueryCacheStore.getState();
  122. expect(afterStaleResponse.resultsByQueryKey[queryKey]?.stale).toBe(true);
  123. expect(afterStaleResponse.resultsByQueryKey[queryKey]?.invalidationVersion).toBe(2);
  124. expect(taskIdsFor(queryKey)).toEqual(["task-generation"]);
  125. expect(
  126. afterStaleResponse.taskMetaByEntityKey[`${WORKSPACE_PATH}::task-generation`]?.title,
  127. ).toBe("completed");
  128. useTaskQueryCacheStore.getState().setQueryResults([
  129. {
  130. queryKey,
  131. descriptor,
  132. items: [staleRunning],
  133. total: 1,
  134. hasMore: false,
  135. expectedInvalidationVersion: 2,
  136. },
  137. ]);
  138. const resolved = useTaskQueryCacheStore.getState();
  139. expect(resolved.resultsByQueryKey[queryKey]?.stale).toBe(false);
  140. expect(resolved.resultsByQueryKey[queryKey]?.invalidationVersion).toBe(2);
  141. expect(resolved.taskMetaByEntityKey[`${WORKSPACE_PATH}::task-generation`]?.title).toBe(
  142. "stale-running",
  143. );
  144. });
  145. });
  146. describe("task lifecycle events", () => {
  147. it("归档和删除时发布父任务生命周期事件", () => {
  148. const listener = vi.fn();
  149. const unsubscribe = subscribeTaskLifecycle(listener);
  150. const task = createTaskMeta({ taskId: "task-lifecycle" });
  151. applyTaskQueryCacheMutation({
  152. previousTask: task,
  153. nextTask: task,
  154. previousState: { pinned: false, archived: false },
  155. nextState: { pinned: false, archived: true },
  156. });
  157. removeTaskFromTaskQueryCaches(task);
  158. unsubscribe();
  159. expect(listener).toHaveBeenNthCalledWith(1, {
  160. type: "archived",
  161. taskId: task.taskId,
  162. workspaceKey: WORKSPACE_PATH,
  163. });
  164. expect(listener).toHaveBeenNthCalledWith(2, {
  165. type: "deleted",
  166. taskId: task.taskId,
  167. workspaceKey: WORKSPACE_PATH,
  168. });
  169. });
  170. });
  171. describe("taskQueryCacheStore sessions activity", () => {
  172. it("搜索结果更新 meta 时保留已缓存的 sessions-index activity", () => {
  173. const task = attachTaskListRowActivity(
  174. createTaskMeta({ taskId: "search-running", updatedAt: 10 }),
  175. { phase: "running", lastActivityAt: 10, hasBackgroundWork: false },
  176. );
  177. useTaskQueryCacheStore.getState().setQueryResult({
  178. queryKey: "workspace-before-search",
  179. descriptor: createDescriptor({ kind: "workspace", visibleLimit: null }),
  180. items: [task],
  181. total: 1,
  182. hasMore: false,
  183. });
  184. useTaskQueryCacheStore.getState().setQueryResult({
  185. queryKey: "workspace-search",
  186. descriptor: createDescriptor({
  187. kind: "workspace",
  188. visibleLimit: null,
  189. search: "running",
  190. }),
  191. items: [
  192. {
  193. ...createTaskMeta({
  194. taskId: "search-running",
  195. title: "running result",
  196. updatedAt: 99,
  197. }),
  198. searchSnippet: "matched body",
  199. },
  200. ],
  201. total: 1,
  202. hasMore: false,
  203. });
  204. const entityKey = `${buildTaskWorkspaceKey(WORKSPACE_PATH)}::search-running`;
  205. const cached = useTaskQueryCacheStore.getState().taskMetaByEntityKey[entityKey];
  206. expect(cached?.title).toBe("running result");
  207. expect(cached?.updatedAt).toBe(10);
  208. expect(cached ? getTaskListRowActivity(cached)?.phase : null).toBe("running");
  209. });
  210. });
  211. describe("taskQueryCacheStore applyTaskMutation", () => {
  212. it("更新 membership/meta 但不使用 tasks-index updatedAt 重排 activity", () => {
  213. const queryKey = "workspace-updated";
  214. const olderTask = attachTaskListRowActivity(
  215. createTaskMeta({ taskId: "older", updatedAt: 10 }),
  216. {
  217. phase: "completedSuccess",
  218. lastActivityAt: 10,
  219. hasBackgroundWork: false,
  220. },
  221. );
  222. const targetTask = attachTaskListRowActivity(
  223. createTaskMeta({ taskId: "target", updatedAt: 5 }),
  224. {
  225. phase: "completedSuccess",
  226. lastActivityAt: 5,
  227. hasBackgroundWork: false,
  228. },
  229. );
  230. const updatedTargetTask = {
  231. ...targetTask,
  232. title: "更新后的任务",
  233. updatedAt: 30,
  234. };
  235. useTaskQueryCacheStore.getState().setQueryResult({
  236. queryKey,
  237. descriptor: createDescriptor({ kind: "workspace", visibleLimit: null }),
  238. items: [olderTask, targetTask],
  239. total: 2,
  240. hasMore: false,
  241. });
  242. useTaskQueryCacheStore.getState().applyTaskMutation({
  243. previousTask: targetTask,
  244. nextTask: updatedTargetTask,
  245. previousState: { pinned: false, archived: false },
  246. nextState: { pinned: false, archived: false },
  247. });
  248. const result = useTaskQueryCacheStore.getState().resultsByQueryKey[queryKey];
  249. const targetEntityKey = `${buildTaskWorkspaceKey(WORKSPACE_PATH)}::target`;
  250. expect(taskIdsFor(queryKey)).toEqual(["older", "target"]);
  251. expect(result?.total).toBe(2);
  252. expect(result?.hasMore).toBe(false);
  253. expect(useTaskQueryCacheStore.getState().taskMetaByEntityKey[targetEntityKey]?.title).toBe(
  254. "更新后的任务",
  255. );
  256. expect(useTaskQueryCacheStore.getState().taskMetaByEntityKey[targetEntityKey]?.updatedAt).toBe(
  257. 5,
  258. );
  259. expect(
  260. getTaskListRowActivity(
  261. useTaskQueryCacheStore.getState().taskMetaByEntityKey[targetEntityKey]!,
  262. )?.phase,
  263. ).toBe("completedSuccess");
  264. });
  265. it("未读乐观 overlay 会挡住在途的旧 membership 回包", () => {
  266. const queryKey = "workspace-unread-overlay";
  267. const descriptor = createDescriptor({
  268. kind: "workspace",
  269. visibleLimit: null,
  270. });
  271. const unreadTask = attachTaskListRowActivity(
  272. createTaskMeta({ taskId: "unread", unreadAt: 100, updatedAt: 10 }),
  273. {
  274. phase: "completedSuccess",
  275. lastActivityAt: 10,
  276. hasBackgroundWork: false,
  277. },
  278. );
  279. const target = {
  280. taskId: unreadTask.taskId,
  281. workspacePath: unreadTask.workspacePath,
  282. };
  283. useTaskQueryCacheStore.getState().setQueryResult({
  284. queryKey,
  285. descriptor,
  286. items: [unreadTask],
  287. total: 1,
  288. hasMore: false,
  289. });
  290. useTaskQueryCacheStore.getState().setTaskUnreadOverlay(target, undefined);
  291. useTaskQueryCacheStore.getState().setQueryResult({
  292. queryKey,
  293. descriptor,
  294. items: [unreadTask],
  295. total: 1,
  296. hasMore: false,
  297. });
  298. const entityKey = `${buildTaskWorkspaceKey(WORKSPACE_PATH)}::unread`;
  299. expect(
  300. useTaskQueryCacheStore.getState().taskMetaByEntityKey[entityKey]?.unreadAt,
  301. ).toBeUndefined();
  302. useTaskQueryCacheStore.getState().reconcileTaskUnread(target, undefined);
  303. expect(
  304. useTaskQueryCacheStore.getState().taskMetaByEntityKey[entityKey]?.unreadAt,
  305. ).toBeUndefined();
  306. expect(useTaskQueryCacheStore.getState().taskUnreadOverlayByEntityKey[entityKey]).toBeNull();
  307. useTaskQueryCacheStore.getState().setQueryResult({
  308. queryKey,
  309. descriptor,
  310. items: [{ ...unreadTask, unreadAt: undefined }],
  311. total: 1,
  312. hasMore: false,
  313. });
  314. expect(useTaskQueryCacheStore.getState().taskUnreadOverlayByEntityKey).toEqual({});
  315. });
  316. it("服务端确认未读后继续阻挡迟到旧回包,直到 membership 发布同值", () => {
  317. const queryKey = "workspace-unread-confirmed-overlay";
  318. const descriptor = createDescriptor({ kind: "workspace", visibleLimit: null });
  319. const task = createTaskMeta({ taskId: "confirmed-unread", updatedAt: 10 });
  320. const target = { taskId: task.taskId, workspacePath: task.workspacePath };
  321. const entityKey = `${buildTaskWorkspaceKey(WORKSPACE_PATH)}::confirmed-unread`;
  322. useTaskQueryCacheStore.getState().setQueryResult({
  323. queryKey,
  324. descriptor,
  325. items: [task],
  326. total: 1,
  327. hasMore: false,
  328. });
  329. useTaskQueryCacheStore.getState().setTaskUnreadOverlay(target, 50);
  330. useTaskQueryCacheStore.getState().reconcileTaskUnread(target, 100);
  331. useTaskQueryCacheStore.getState().setQueryResult({
  332. queryKey,
  333. descriptor,
  334. items: [task],
  335. total: 1,
  336. hasMore: false,
  337. });
  338. expect(useTaskQueryCacheStore.getState().taskMetaByEntityKey[entityKey]?.unreadAt).toBe(100);
  339. expect(useTaskQueryCacheStore.getState().taskUnreadOverlayByEntityKey[entityKey]).toBe(100);
  340. useTaskQueryCacheStore.getState().setQueryResults([
  341. {
  342. queryKey,
  343. descriptor,
  344. items: [{ ...task, unreadAt: 100 }],
  345. total: 1,
  346. hasMore: false,
  347. },
  348. ]);
  349. expect(useTaskQueryCacheStore.getState().taskUnreadOverlayByEntityKey).toEqual({});
  350. });
  351. it("任务从 workspace 列表移动到 pinned 列表时同步更新两个缓存", () => {
  352. const workspaceQueryKey = "workspace-list";
  353. const pinnedQueryKey = "pinned-list";
  354. const task = createTaskMeta({ taskId: "task-to-pin", updatedAt: 10 });
  355. const pinnedTask = { ...task, pinned: true };
  356. useTaskQueryCacheStore.getState().setQueryResult({
  357. queryKey: workspaceQueryKey,
  358. descriptor: createDescriptor({ kind: "workspace", visibleLimit: null }),
  359. items: [task],
  360. total: 1,
  361. hasMore: false,
  362. });
  363. useTaskQueryCacheStore.getState().setQueryResult({
  364. queryKey: pinnedQueryKey,
  365. descriptor: createDescriptor({ kind: "pinned", visibleLimit: null }),
  366. items: [],
  367. total: 0,
  368. hasMore: false,
  369. });
  370. useTaskQueryCacheStore.getState().applyTaskMutation({
  371. previousTask: task,
  372. nextTask: pinnedTask,
  373. previousState: { pinned: false, archived: false },
  374. nextState: { pinned: true, archived: false },
  375. });
  376. expect(taskIdsFor(workspaceQueryKey)).toEqual([]);
  377. expect(useTaskQueryCacheStore.getState().resultsByQueryKey[workspaceQueryKey]?.total).toBe(0);
  378. expect(taskIdsFor(pinnedQueryKey)).toEqual(["task-to-pin"]);
  379. expect(useTaskQueryCacheStore.getState().resultsByQueryKey[pinnedQueryKey]?.total).toBe(1);
  380. });
  381. it("只更新任务元数据时保留原有 pinned 列表成员关系", () => {
  382. const workspaceQueryKey = "workspace-list";
  383. const pinnedQueryKey = "pinned-list";
  384. const pinnedTask = createTaskMeta({
  385. taskId: "active-pinned",
  386. title: "置顶任务",
  387. updatedAt: 10,
  388. });
  389. const updatedPinnedTask = {
  390. ...pinnedTask,
  391. title: "置顶任务有新回复",
  392. updatedAt: 30,
  393. };
  394. useTaskQueryCacheStore.getState().setQueryResult({
  395. queryKey: workspaceQueryKey,
  396. descriptor: createDescriptor({ kind: "workspace", visibleLimit: null }),
  397. items: [],
  398. total: 0,
  399. hasMore: false,
  400. });
  401. useTaskQueryCacheStore.getState().setQueryResult({
  402. queryKey: pinnedQueryKey,
  403. descriptor: createDescriptor({ kind: "pinned", visibleLimit: null }),
  404. items: [pinnedTask],
  405. total: 1,
  406. hasMore: false,
  407. });
  408. updateTaskQueryCacheTaskMetaPreservingMembership(updatedPinnedTask);
  409. const entityKey = `${buildTaskWorkspaceKey(WORKSPACE_PATH)}::active-pinned`;
  410. expect(taskIdsFor(pinnedQueryKey)).toEqual(["active-pinned"]);
  411. expect(taskIdsFor(workspaceQueryKey)).toEqual([]);
  412. expect(useTaskQueryCacheStore.getState().taskMetaByEntityKey[entityKey]?.title).toBe(
  413. "置顶任务有新回复",
  414. );
  415. });
  416. it("任务进入折叠列表时按排序进入可见区,并标记后台刷新补齐截断结果", () => {
  417. const queryKey = "collapsed-pinned-list";
  418. const visiblePinnedTask = createTaskMeta({
  419. taskId: "visible-pinned",
  420. updatedAt: 20,
  421. });
  422. const task = createTaskMeta({ taskId: "newly-pinned", updatedAt: 30 });
  423. useTaskQueryCacheStore.getState().setQueryResult({
  424. queryKey,
  425. descriptor: createDescriptor({ kind: "pinned", visibleLimit: 1 }),
  426. items: [visiblePinnedTask],
  427. total: 1,
  428. hasMore: false,
  429. });
  430. useTaskQueryCacheStore.getState().applyTaskMutation({
  431. previousTask: task,
  432. nextTask: { ...task, pinned: true },
  433. previousState: { pinned: false, archived: false },
  434. nextState: { pinned: true, archived: false },
  435. });
  436. const result = useTaskQueryCacheStore.getState().resultsByQueryKey[queryKey];
  437. expect(taskIdsFor(queryKey)).toEqual(["newly-pinned"]);
  438. expect(result?.total).toBe(2);
  439. expect(result?.hasMore).toBe(true);
  440. expect(result?.stale).toBe(true);
  441. });
  442. it("带搜索词的列表变更时只标记 stale,避免用标题规则误删正文命中结果", () => {
  443. const queryKey = "content-search-list";
  444. const task = createTaskMeta({
  445. taskId: "content-hit",
  446. title: "标题不包含关键词",
  447. updatedAt: 10,
  448. });
  449. const updatedTask = {
  450. ...task,
  451. updatedAt: 20,
  452. };
  453. useTaskQueryCacheStore.getState().setQueryResult({
  454. queryKey,
  455. descriptor: createDescriptor({
  456. kind: "workspace",
  457. visibleLimit: null,
  458. search: "body-keyword",
  459. }),
  460. items: [task],
  461. total: 1,
  462. hasMore: false,
  463. });
  464. useTaskQueryCacheStore.getState().applyTaskMutation({
  465. previousTask: task,
  466. nextTask: updatedTask,
  467. previousState: { pinned: false, archived: false },
  468. nextState: { pinned: false, archived: false },
  469. });
  470. const result = useTaskQueryCacheStore.getState().resultsByQueryKey[queryKey];
  471. expect(taskIdsFor(queryKey)).toEqual(["content-hit"]);
  472. expect(result?.total).toBe(1);
  473. expect(result?.stale).toBe(true);
  474. });
  475. it("普通列表刷新不会抹掉搜索结果项上的正文摘要", () => {
  476. const searchQueryKey = "content-search-result";
  477. const workspaceQueryKey = "workspace-result";
  478. const task = createTaskMeta({
  479. taskId: "content-hit",
  480. title: "标题不包含关键词",
  481. updatedAt: 10,
  482. });
  483. useTaskQueryCacheStore.getState().setQueryResult({
  484. queryKey: searchQueryKey,
  485. descriptor: createDescriptor({
  486. kind: "workspace",
  487. visibleLimit: null,
  488. search: "body-keyword",
  489. }),
  490. items: [{ ...task, searchSnippet: "这里命中了 body-keyword" }],
  491. total: 1,
  492. hasMore: false,
  493. });
  494. useTaskQueryCacheStore.getState().setQueryResult({
  495. queryKey: workspaceQueryKey,
  496. descriptor: createDescriptor({ kind: "workspace", visibleLimit: null }),
  497. items: [task],
  498. total: 1,
  499. hasMore: false,
  500. });
  501. const entityKey = `${buildTaskWorkspaceKey(WORKSPACE_PATH)}::content-hit`;
  502. expect(useTaskQueryCacheStore.getState().taskMetaByEntityKey[entityKey]?.searchSnippet).toBe(
  503. "这里命中了 body-keyword",
  504. );
  505. expect(
  506. useTaskQueryCacheStore.getState().resultsByQueryKey[searchQueryKey]
  507. ?.searchSnippetsByTaskKey?.[entityKey],
  508. ).toBe("这里命中了 body-keyword");
  509. });
  510. it("旧 sqlite 刷新不会把刚本地插入的新任务从可见缓存抹掉", () => {
  511. const queryKey = "workspace-first-send-race";
  512. const descriptor = createDescriptor({ kind: "workspace", visibleLimit: 2 });
  513. const olderTask = createTaskMeta({ taskId: "older", updatedAt: 20 });
  514. const anotherTask = createTaskMeta({ taskId: "another", updatedAt: 10 });
  515. useTaskQueryCacheStore.getState().setQueryResult({
  516. queryKey,
  517. descriptor,
  518. items: [olderTask, anotherTask],
  519. total: 2,
  520. hasMore: false,
  521. });
  522. const previousFetchedAt =
  523. useTaskQueryCacheStore.getState().resultsByQueryKey[queryKey]?.fetchedAt ?? Date.now();
  524. const newTask = createTaskMeta({
  525. taskId: "new-first-send",
  526. title: "首发问题",
  527. updatedAt: previousFetchedAt + 10,
  528. });
  529. useTaskQueryCacheStore.getState().applyTaskMutation({
  530. previousTask: newTask,
  531. nextTask: newTask,
  532. previousState: { pinned: true, archived: true },
  533. nextState: { pinned: false, archived: false },
  534. });
  535. useTaskQueryCacheStore.getState().setQueryResults([
  536. {
  537. queryKey,
  538. descriptor,
  539. // 模拟 sqlite 同步慢半拍,刷新结果仍然只返回旧任务。
  540. items: [olderTask, anotherTask],
  541. total: 2,
  542. hasMore: false,
  543. },
  544. ]);
  545. const result = useTaskQueryCacheStore.getState().resultsByQueryKey[queryKey];
  546. const entityKey = `${buildTaskWorkspaceKey(WORKSPACE_PATH)}::new-first-send`;
  547. expect(taskIdsFor(queryKey)).toEqual(["new-first-send", "older"]);
  548. expect(result?.total).toBe(3);
  549. expect(result?.hasMore).toBe(true);
  550. expect(useTaskQueryCacheStore.getState().taskMetaByEntityKey[entityKey]?.title).toBe(
  551. "首发问题",
  552. );
  553. });
  554. });
  555. describe("taskQueryCacheStore removeTask", () => {
  556. it("从所有命中的 query cache 中移除目标 task 并更新 total", () => {
  557. const workspaceQueryKey = "workspace-delete";
  558. const archivedQueryKey = "archived-delete";
  559. const targetTask = createTaskMeta({ taskId: "delete-me", updatedAt: 20 });
  560. const otherTask = createTaskMeta({ taskId: "keep-me", updatedAt: 10 });
  561. useTaskQueryCacheStore.getState().setQueryResult({
  562. queryKey: workspaceQueryKey,
  563. descriptor: createDescriptor({ kind: "workspace", visibleLimit: null }),
  564. items: [targetTask, otherTask],
  565. total: 2,
  566. hasMore: false,
  567. });
  568. useTaskQueryCacheStore.getState().setQueryResult({
  569. queryKey: archivedQueryKey,
  570. descriptor: createDescriptor({ kind: "archived", visibleLimit: null }),
  571. items: [targetTask],
  572. total: 1,
  573. hasMore: false,
  574. });
  575. const removed = useTaskQueryCacheStore.getState().removeTask({
  576. workspacePath: WORKSPACE_PATH,
  577. taskId: "delete-me",
  578. });
  579. const entityKey = `${buildTaskWorkspaceKey(WORKSPACE_PATH)}::delete-me`;
  580. expect(removed).toBe(true);
  581. expect(taskIdsFor(workspaceQueryKey)).toEqual(["keep-me"]);
  582. expect(useTaskQueryCacheStore.getState().resultsByQueryKey[workspaceQueryKey]?.total).toBe(1);
  583. expect(taskIdsFor(archivedQueryKey)).toEqual([]);
  584. expect(useTaskQueryCacheStore.getState().resultsByQueryKey[archivedQueryKey]?.total).toBe(0);
  585. expect(useTaskQueryCacheStore.getState().taskMetaByEntityKey[entityKey]).toBeUndefined();
  586. });
  587. it("删除隐藏项未命中可见缓存时返回 false,等待调用方刷新事实源", () => {
  588. const queryKey = "active-delete-hidden";
  589. const visibleTask = createTaskMeta({
  590. taskId: "visible-task",
  591. updatedAt: 20,
  592. });
  593. useTaskQueryCacheStore.getState().setQueryResult({
  594. queryKey,
  595. descriptor: createDescriptor({ kind: "active", visibleLimit: 1 }),
  596. items: [visibleTask],
  597. total: 2,
  598. hasMore: true,
  599. });
  600. const removed = useTaskQueryCacheStore.getState().removeTask({
  601. workspacePath: WORKSPACE_PATH,
  602. taskId: "hidden-task",
  603. });
  604. const result = useTaskQueryCacheStore.getState().resultsByQueryKey[queryKey];
  605. expect(removed).toBe(false);
  606. expect(taskIdsFor(queryKey)).toEqual(["visible-task"]);
  607. expect(result?.total).toBe(2);
  608. expect(result?.hasMore).toBe(true);
  609. });
  610. });