global-sync.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { describe, expect, test } from "bun:test"
  2. import { canDisposeDirectory, pickDirectoriesToEvict } from "./global-sync/eviction"
  3. import { estimateRootSessionTotal, loadRootSessionsWithFallback } from "./global-sync/session-load"
  4. describe("pickDirectoriesToEvict", () => {
  5. test("keeps pinned stores and evicts idle stores", () => {
  6. const now = 5_000
  7. const picks = pickDirectoriesToEvict({
  8. stores: ["a", "b", "c", "d"],
  9. state: new Map([
  10. ["a", { lastAccessAt: 1_000 }],
  11. ["b", { lastAccessAt: 4_900 }],
  12. ["c", { lastAccessAt: 4_800 }],
  13. ["d", { lastAccessAt: 3_000 }],
  14. ]),
  15. pins: new Set(["a"]),
  16. max: 2,
  17. ttl: 1_500,
  18. now,
  19. })
  20. expect(picks).toEqual(["d", "c"])
  21. })
  22. })
  23. describe("loadRootSessionsWithFallback", () => {
  24. test("uses limited roots query when supported", async () => {
  25. const calls: Array<{ directory: string; roots: true; limit?: number }> = []
  26. const result = await loadRootSessionsWithFallback({
  27. directory: "dir",
  28. limit: 10,
  29. list: async (query) => {
  30. calls.push(query)
  31. return { data: [] }
  32. },
  33. })
  34. expect(result.data).toEqual([])
  35. expect(result.limited).toBe(true)
  36. expect(calls).toEqual([{ directory: "dir", roots: true, limit: 10 }])
  37. })
  38. test("falls back to full roots query on limited-query failure", async () => {
  39. const calls: Array<{ directory: string; roots: true; limit?: number }> = []
  40. const result = await loadRootSessionsWithFallback({
  41. directory: "dir",
  42. limit: 25,
  43. list: async (query) => {
  44. calls.push(query)
  45. if (query.limit) throw new Error("unsupported")
  46. return { data: [] }
  47. },
  48. })
  49. expect(result.data).toEqual([])
  50. expect(result.limited).toBe(false)
  51. expect(calls).toEqual([
  52. { directory: "dir", roots: true, limit: 25 },
  53. { directory: "dir", roots: true },
  54. ])
  55. })
  56. })
  57. describe("estimateRootSessionTotal", () => {
  58. test("keeps exact total for full fetches", () => {
  59. expect(estimateRootSessionTotal({ count: 42, limit: 10, limited: false })).toBe(42)
  60. })
  61. test("marks has-more for full-limit limited fetches", () => {
  62. expect(estimateRootSessionTotal({ count: 10, limit: 10, limited: true })).toBe(11)
  63. })
  64. test("keeps exact total when limited fetch is under limit", () => {
  65. expect(estimateRootSessionTotal({ count: 9, limit: 10, limited: true })).toBe(9)
  66. })
  67. })
  68. describe("canDisposeDirectory", () => {
  69. test("rejects pinned or inflight directories", () => {
  70. expect(
  71. canDisposeDirectory({
  72. directory: "dir",
  73. hasStore: true,
  74. pinned: true,
  75. booting: false,
  76. loadingSessions: false,
  77. }),
  78. ).toBe(false)
  79. expect(
  80. canDisposeDirectory({
  81. directory: "dir",
  82. hasStore: true,
  83. pinned: false,
  84. booting: true,
  85. loadingSessions: false,
  86. }),
  87. ).toBe(false)
  88. expect(
  89. canDisposeDirectory({
  90. directory: "dir",
  91. hasStore: true,
  92. pinned: false,
  93. booting: false,
  94. loadingSessions: true,
  95. }),
  96. ).toBe(false)
  97. })
  98. test("accepts idle unpinned directory store", () => {
  99. expect(
  100. canDisposeDirectory({
  101. directory: "dir",
  102. hasStore: true,
  103. pinned: false,
  104. booting: false,
  105. loadingSessions: false,
  106. }),
  107. ).toBe(true)
  108. })
  109. })