quickPickConversationNavigation.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { describe, expect, it } from "vitest";
  2. import type { ZCodeTaskMeta } from "@zcode/shared";
  3. import { buildTaskEntityKey, buildTaskListCacheDescriptor } from "@/lib/taskQueryCache.js";
  4. import {
  5. resolveQuickPickConversationNavigation,
  6. selectQuickPickConversationTaskIds,
  7. } from "@/lib/quickPickConversationNavigation.js";
  8. function createTask(taskId: string, updatedAt: number, workspaceIdentity?: string): ZCodeTaskMeta {
  9. return {
  10. taskId,
  11. traceId: `trace-${taskId}`,
  12. title: taskId,
  13. workspacePath: "/workspace",
  14. ...(workspaceIdentity ? { workspaceIdentity } : {}),
  15. createdAt: updatedAt,
  16. updatedAt,
  17. mode: "default",
  18. };
  19. }
  20. describe("quickPickConversationNavigation", () => {
  21. it("resolves previous and next from the ordered task ids", () => {
  22. expect(
  23. resolveQuickPickConversationNavigation({
  24. taskIds: ["newest", "middle", "oldest"],
  25. activeTaskId: "middle",
  26. }),
  27. ).toMatchObject({
  28. canSelectPreviousConversation: true,
  29. canSelectNextConversation: true,
  30. previousTaskId: "newest",
  31. nextTaskId: "oldest",
  32. });
  33. });
  34. it("uses edge availability when the active task is at the list boundary", () => {
  35. expect(
  36. resolveQuickPickConversationNavigation({
  37. taskIds: ["newest", "oldest"],
  38. activeTaskId: "newest",
  39. }),
  40. ).toMatchObject({
  41. canSelectPreviousConversation: false,
  42. canSelectNextConversation: true,
  43. previousTaskId: null,
  44. nextTaskId: "oldest",
  45. });
  46. });
  47. it("prefers workspace task query cache over stale fallback ids", () => {
  48. const tasks = [createTask("cache-new", 3), createTask("cache-old", 1)];
  49. const descriptor = buildTaskListCacheDescriptor({
  50. kind: "workspace",
  51. workspaceScopes: [{ workspacePath: "/workspace" }],
  52. sortBy: "updated",
  53. search: "",
  54. expanded: true,
  55. });
  56. const taskMetaByEntityKey = Object.fromEntries(
  57. tasks.map((task) => [buildTaskEntityKey(task), task]),
  58. );
  59. const taskIds = selectQuickPickConversationTaskIds({
  60. workspacePath: "/workspace",
  61. resultsByQueryKey: {
  62. workspace: {
  63. taskKeys: tasks.map(buildTaskEntityKey),
  64. total: tasks.length,
  65. hasMore: false,
  66. invalidationVersion: 0,
  67. fetchedAt: 1,
  68. stale: false,
  69. partial: false,
  70. loadingShardKeys: [],
  71. failedShardKeys: [],
  72. descriptor,
  73. },
  74. },
  75. taskMetaByEntityKey,
  76. fallbackTaskIds: ["fallback-only"],
  77. });
  78. expect(taskIds).toEqual(["cache-new", "cache-old"]);
  79. });
  80. it("falls back to old task ids before task query cache is ready", () => {
  81. expect(
  82. selectQuickPickConversationTaskIds({
  83. workspacePath: "/workspace",
  84. resultsByQueryKey: {},
  85. taskMetaByEntityKey: {},
  86. fallbackTaskIds: ["fallback-new", "fallback-old"],
  87. }),
  88. ).toEqual(["fallback-new", "fallback-old"]);
  89. });
  90. });