workspaceTaskPagination.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import { describe, expect, it } from "vitest";
  2. import type { ZCodeTaskMeta } from "@zcode/shared";
  3. import {
  4. buildTaskEntityKey,
  5. buildTaskListCacheDescriptor,
  6. buildTaskWorkspaceKey,
  7. type CachedTaskListResult,
  8. } from "@/lib/taskQueryCache.js";
  9. import {
  10. increaseWorkspaceTaskVisibleLimit,
  11. resolveVisibleWorkspaceTaskKeys,
  12. resolveWorkspaceTaskVisibleLimit,
  13. retainWorkspaceTaskVisibleLimits,
  14. } from "@/lib/workspaceTaskPagination.js";
  15. import {
  16. buildWorkspaceTaskListDisplayGroups,
  17. type WorkspaceTaskListGroup,
  18. } from "@/hooks/workspaceTaskListDisplayGroups.js";
  19. const WORKSPACE_PATH = "/workspace";
  20. const WORKSPACE_KEY = buildTaskWorkspaceKey(WORKSPACE_PATH);
  21. function createTask(index: number): ZCodeTaskMeta {
  22. return {
  23. createdAt: 100 - index,
  24. mode: "default",
  25. provider: "codex",
  26. taskId: `task-${index + 1}`,
  27. title: `Task ${index + 1}`,
  28. traceId: `trace-${index + 1}`,
  29. updatedAt: 100 - index,
  30. workspacePath: WORKSPACE_PATH,
  31. };
  32. }
  33. function createCachedResult(
  34. tasks: ZCodeTaskMeta[],
  35. total: number,
  36. allTasks: ZCodeTaskMeta[],
  37. ): CachedTaskListResult {
  38. const descriptor = buildTaskListCacheDescriptor({
  39. kind: "workspace",
  40. workspaceScopes: [{ workspacePath: WORKSPACE_PATH }],
  41. sortBy: "updated",
  42. search: "",
  43. expanded: false,
  44. visibleLimit: Math.max(5, tasks.length),
  45. });
  46. return {
  47. descriptor,
  48. failedShardKeys: [],
  49. fetchedAt: 1,
  50. hasMore: total > tasks.length,
  51. invalidationVersion: 0,
  52. loadingShardKeys: [],
  53. partial: false,
  54. stale: false,
  55. taskKeys: tasks.map(buildTaskEntityKey),
  56. unreadTaskKeys: allTasks
  57. .filter((task) => typeof task.unreadAt === "number")
  58. .map(buildTaskEntityKey),
  59. total,
  60. };
  61. }
  62. function buildDisplayGroup(params: {
  63. allTasks: ZCodeTaskMeta[];
  64. cachedTasks?: ZCodeTaskMeta[];
  65. previousGroup?: WorkspaceTaskListGroup;
  66. taskUnreadOverlayByEntityKey?: Record<string, number | null>;
  67. visibleLimit: number;
  68. }): WorkspaceTaskListGroup {
  69. const queryKey = `workspace-limit-${params.visibleLimit}`;
  70. const taskMetaByEntityKey = Object.fromEntries(
  71. params.allTasks.map((task) => [buildTaskEntityKey(task), task]),
  72. );
  73. const result = buildWorkspaceTaskListDisplayGroups({
  74. queryConfigs: [
  75. {
  76. queryKey,
  77. scope: { workspacePath: WORKSPACE_PATH },
  78. visibleLimit: params.visibleLimit,
  79. workspaceKey: WORKSPACE_KEY,
  80. },
  81. ],
  82. resultsByQueryKey: params.cachedTasks
  83. ? {
  84. [queryKey]: createCachedResult(
  85. params.cachedTasks,
  86. params.allTasks.length,
  87. params.allTasks,
  88. ),
  89. }
  90. : {},
  91. taskMetaByEntityKey,
  92. taskUnreadOverlayByEntityKey: params.taskUnreadOverlayByEntityKey ?? {},
  93. optimisticTaskOverlayByWorkspaceKey: new Map(),
  94. previousGroupsByWorkspaceKey: params.previousGroup
  95. ? new Map([[WORKSPACE_KEY, params.previousGroup]])
  96. : new Map(),
  97. sortBy: "updated",
  98. });
  99. return result.groups[0]!;
  100. }
  101. describe("workspace task visible limit", () => {
  102. it("uses 5 as the default and increases only the selected workspace by 5", () => {
  103. const remoteWorkspaceKey = "ssh:host:/workspace";
  104. const initial = {};
  105. const afterFirstClick = increaseWorkspaceTaskVisibleLimit(initial, WORKSPACE_KEY);
  106. const afterSecondClick = increaseWorkspaceTaskVisibleLimit(afterFirstClick, WORKSPACE_KEY);
  107. expect(resolveWorkspaceTaskVisibleLimit(initial, WORKSPACE_KEY)).toBe(5);
  108. expect(resolveWorkspaceTaskVisibleLimit(afterFirstClick, WORKSPACE_KEY)).toBe(10);
  109. expect(resolveWorkspaceTaskVisibleLimit(afterSecondClick, WORKSPACE_KEY)).toBe(15);
  110. expect(resolveWorkspaceTaskVisibleLimit(afterSecondClick, remoteWorkspaceKey)).toBe(5);
  111. });
  112. it("drops pagination progress for collapsed or removed workspace keys", () => {
  113. const limits = { [WORKSPACE_KEY]: 15, other: 10 };
  114. const retained = retainWorkspaceTaskVisibleLimits(limits, new Set([WORKSPACE_KEY]));
  115. expect(retained).toEqual({ [WORKSPACE_KEY]: 15 });
  116. expect(retainWorkspaceTaskVisibleLimits(retained, new Set())).toEqual({});
  117. expect(retainWorkspaceTaskVisibleLimits(retained, new Set([WORKSPACE_KEY]))).toBe(retained);
  118. });
  119. it("keeps only expanded workspace identities while the nested workspace view is visible", () => {
  120. const remoteIdentity = "ssh:host:/workspace";
  121. const workspaces = [
  122. { workspacePath: WORKSPACE_PATH },
  123. { workspacePath: "/remote", workspaceIdentity: remoteIdentity },
  124. ];
  125. const visibleKeys = resolveVisibleWorkspaceTaskKeys({
  126. enabled: true,
  127. expandedWorkspacePaths: new Set(["/remote"]),
  128. workspaces,
  129. });
  130. expect([...visibleKeys]).toEqual([remoteIdentity]);
  131. expect(
  132. resolveVisibleWorkspaceTaskKeys({
  133. enabled: false,
  134. expandedWorkspacePaths: new Set([WORKSPACE_PATH, "/remote"]),
  135. workspaces,
  136. }),
  137. ).toEqual(new Set());
  138. });
  139. });
  140. describe("workspace task display pagination", () => {
  141. const tasks = Array.from({ length: 12 }, (_, index) => createTask(index));
  142. it("renders cached pages as 5, 10, then the final 12 and hides the entry", () => {
  143. const firstPage = buildDisplayGroup({
  144. allTasks: tasks,
  145. cachedTasks: tasks.slice(0, 5),
  146. visibleLimit: 5,
  147. });
  148. const secondPage = buildDisplayGroup({
  149. allTasks: tasks,
  150. cachedTasks: tasks.slice(0, 10),
  151. visibleLimit: 10,
  152. });
  153. const finalPage = buildDisplayGroup({
  154. allTasks: tasks,
  155. cachedTasks: tasks,
  156. visibleLimit: 15,
  157. });
  158. expect(firstPage.items).toHaveLength(5);
  159. expect(firstPage.hasMore).toBe(true);
  160. expect(secondPage.items).toHaveLength(10);
  161. expect(secondPage.hasMore).toBe(true);
  162. expect(finalPage.items).toHaveLength(12);
  163. expect(finalPage.hasMore).toBe(false);
  164. });
  165. it("keeps the previous page while a larger cache key is pending", () => {
  166. const previousGroup = buildDisplayGroup({
  167. allTasks: tasks,
  168. cachedTasks: tasks.slice(0, 5),
  169. visibleLimit: 5,
  170. });
  171. const pendingGroup = buildDisplayGroup({
  172. allTasks: tasks,
  173. previousGroup,
  174. visibleLimit: 10,
  175. });
  176. expect(pendingGroup.items).toHaveLength(5);
  177. expect(pendingGroup.hasMore).toBe(true);
  178. });
  179. it("trims a previous larger page immediately when reset to 5", () => {
  180. const previousGroup = buildDisplayGroup({
  181. allTasks: tasks,
  182. cachedTasks: tasks.slice(0, 10),
  183. visibleLimit: 10,
  184. });
  185. const resetGroup = buildDisplayGroup({
  186. allTasks: tasks,
  187. previousGroup,
  188. visibleLimit: 5,
  189. });
  190. expect(resetGroup.items).toHaveLength(5);
  191. expect(resetGroup.hasMore).toBe(true);
  192. });
  193. it("aggregates unread tasks outside the visible page and respects optimistic clear", () => {
  194. const hiddenUnreadTask = { ...tasks[11]!, unreadAt: 123 };
  195. const tasksWithHiddenUnread = [...tasks.slice(0, 11), hiddenUnreadTask];
  196. const initialGroup = buildDisplayGroup({
  197. allTasks: tasksWithHiddenUnread,
  198. cachedTasks: tasksWithHiddenUnread.slice(0, 5),
  199. visibleLimit: 5,
  200. });
  201. const clearedGroup = buildDisplayGroup({
  202. allTasks: tasksWithHiddenUnread,
  203. cachedTasks: tasksWithHiddenUnread.slice(0, 5),
  204. taskUnreadOverlayByEntityKey: { [buildTaskEntityKey(hiddenUnreadTask)]: null },
  205. visibleLimit: 5,
  206. });
  207. expect(initialGroup.items).toHaveLength(5);
  208. expect(initialGroup.hasUnread).toBe(true);
  209. expect(clearedGroup.hasUnread).toBe(false);
  210. });
  211. it("keeps the aggregate until the final unread task is cleared", () => {
  212. const firstUnreadTask = { ...tasks[10]!, unreadAt: 111 };
  213. const secondUnreadTask = { ...tasks[11]!, unreadAt: 222 };
  214. const tasksWithUnread = [...tasks.slice(0, 10), firstUnreadTask, secondUnreadTask];
  215. const group = buildDisplayGroup({
  216. allTasks: tasksWithUnread,
  217. cachedTasks: tasksWithUnread.slice(0, 5),
  218. taskUnreadOverlayByEntityKey: { [buildTaskEntityKey(firstUnreadTask)]: null },
  219. visibleLimit: 5,
  220. });
  221. expect(group.hasUnread).toBe(true);
  222. });
  223. });