global-sync.test.ts 3.3 KB

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