session-prefetch.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { describe, expect, test } from "bun:test"
  2. import {
  3. clearSessionPrefetch,
  4. clearSessionPrefetchDirectory,
  5. getSessionPrefetch,
  6. runSessionPrefetch,
  7. setSessionPrefetch,
  8. shouldSkipSessionPrefetch,
  9. } from "./session-prefetch"
  10. describe("session prefetch", () => {
  11. test("stores and clears message metadata by directory", () => {
  12. clearSessionPrefetch("/tmp/a", ["ses_1"])
  13. clearSessionPrefetch("/tmp/b", ["ses_1"])
  14. setSessionPrefetch({
  15. directory: "/tmp/a",
  16. sessionID: "ses_1",
  17. limit: 200,
  18. cursor: "abc",
  19. complete: false,
  20. at: 123,
  21. })
  22. expect(getSessionPrefetch("/tmp/a", "ses_1")).toEqual({ limit: 200, cursor: "abc", complete: false, at: 123 })
  23. expect(getSessionPrefetch("/tmp/b", "ses_1")).toBeUndefined()
  24. clearSessionPrefetch("/tmp/a", ["ses_1"])
  25. expect(getSessionPrefetch("/tmp/a", "ses_1")).toBeUndefined()
  26. })
  27. test("dedupes inflight work", async () => {
  28. clearSessionPrefetch("/tmp/c", ["ses_2"])
  29. let calls = 0
  30. const run = () =>
  31. runSessionPrefetch({
  32. directory: "/tmp/c",
  33. sessionID: "ses_2",
  34. task: async () => {
  35. calls += 1
  36. return { limit: 100, cursor: "next", complete: true, at: 456 }
  37. },
  38. })
  39. const [a, b] = await Promise.all([run(), run()])
  40. expect(calls).toBe(1)
  41. expect(a).toEqual({ limit: 100, cursor: "next", complete: true, at: 456 })
  42. expect(b).toEqual({ limit: 100, cursor: "next", complete: true, at: 456 })
  43. })
  44. test("clears a whole directory", () => {
  45. setSessionPrefetch({ directory: "/tmp/d", sessionID: "ses_1", limit: 10, cursor: "a", complete: true, at: 1 })
  46. setSessionPrefetch({ directory: "/tmp/d", sessionID: "ses_2", limit: 20, cursor: "b", complete: false, at: 2 })
  47. setSessionPrefetch({ directory: "/tmp/e", sessionID: "ses_1", limit: 30, cursor: "c", complete: true, at: 3 })
  48. clearSessionPrefetchDirectory("/tmp/d")
  49. expect(getSessionPrefetch("/tmp/d", "ses_1")).toBeUndefined()
  50. expect(getSessionPrefetch("/tmp/d", "ses_2")).toBeUndefined()
  51. expect(getSessionPrefetch("/tmp/e", "ses_1")).toEqual({ limit: 30, cursor: "c", complete: true, at: 3 })
  52. })
  53. test("refreshes stale first-page prefetched history", () => {
  54. expect(
  55. shouldSkipSessionPrefetch({
  56. message: true,
  57. info: { limit: 200, cursor: "x", complete: false, at: 1 },
  58. chunk: 200,
  59. now: 1 + 15_001,
  60. }),
  61. ).toBe(false)
  62. })
  63. test("keeps deeper or complete history cached", () => {
  64. expect(
  65. shouldSkipSessionPrefetch({
  66. message: true,
  67. info: { limit: 400, cursor: "x", complete: false, at: 1 },
  68. chunk: 200,
  69. now: 1 + 15_001,
  70. }),
  71. ).toBe(true)
  72. expect(
  73. shouldSkipSessionPrefetch({
  74. message: true,
  75. info: { limit: 120, complete: true, at: 1 },
  76. chunk: 200,
  77. now: 1 + 15_001,
  78. }),
  79. ).toBe(true)
  80. })
  81. })