global-sync.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. let fallback = 0
  31. const result = await loadRootSessionsWithFallback({
  32. directory: "dir",
  33. limit: 10,
  34. list: async (query) => {
  35. calls.push(query)
  36. return { data: [] }
  37. },
  38. onFallback: () => {
  39. fallback += 1
  40. },
  41. })
  42. expect(result.data).toEqual([])
  43. expect(result.limited).toBe(true)
  44. expect(calls).toEqual([{ directory: "dir", roots: true, limit: 10 }])
  45. expect(fallback).toBe(0)
  46. })
  47. test("falls back to full roots query on limited-query failure", async () => {
  48. const calls: Array<{ directory: string; roots: true; limit?: number }> = []
  49. let fallback = 0
  50. const result = await loadRootSessionsWithFallback({
  51. directory: "dir",
  52. limit: 25,
  53. list: async (query) => {
  54. calls.push(query)
  55. if (query.limit) throw new Error("unsupported")
  56. return { data: [] }
  57. },
  58. onFallback: () => {
  59. fallback += 1
  60. },
  61. })
  62. expect(result.data).toEqual([])
  63. expect(result.limited).toBe(false)
  64. expect(calls).toEqual([
  65. { directory: "dir", roots: true, limit: 25 },
  66. { directory: "dir", roots: true },
  67. ])
  68. expect(fallback).toBe(1)
  69. })
  70. })
  71. describe("estimateRootSessionTotal", () => {
  72. test("keeps exact total for full fetches", () => {
  73. expect(estimateRootSessionTotal({ count: 42, limit: 10, limited: false })).toBe(42)
  74. })
  75. test("marks has-more for full-limit limited fetches", () => {
  76. expect(estimateRootSessionTotal({ count: 10, limit: 10, limited: true })).toBe(11)
  77. })
  78. test("keeps exact total when limited fetch is under limit", () => {
  79. expect(estimateRootSessionTotal({ count: 9, limit: 10, limited: true })).toBe(9)
  80. })
  81. })
  82. describe("canDisposeDirectory", () => {
  83. test("rejects pinned or inflight directories", () => {
  84. expect(
  85. canDisposeDirectory({
  86. directory: "dir",
  87. hasStore: true,
  88. pinned: true,
  89. booting: false,
  90. loadingSessions: false,
  91. }),
  92. ).toBe(false)
  93. expect(
  94. canDisposeDirectory({
  95. directory: "dir",
  96. hasStore: true,
  97. pinned: false,
  98. booting: true,
  99. loadingSessions: false,
  100. }),
  101. ).toBe(false)
  102. expect(
  103. canDisposeDirectory({
  104. directory: "dir",
  105. hasStore: true,
  106. pinned: false,
  107. booting: false,
  108. loadingSessions: true,
  109. }),
  110. ).toBe(false)
  111. })
  112. test("accepts idle unpinned directory store", () => {
  113. expect(
  114. canDisposeDirectory({
  115. directory: "dir",
  116. hasStore: true,
  117. pinned: false,
  118. booting: false,
  119. loadingSessions: false,
  120. }),
  121. ).toBe(true)
  122. })
  123. })