notification.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { describe, expect, test } from "bun:test"
  2. import { buildNotificationIndex } from "./notification-index"
  3. type Notification = {
  4. type: "turn-complete" | "error"
  5. session: string
  6. directory: string
  7. viewed: boolean
  8. time: number
  9. }
  10. const turn = (session: string, directory: string, viewed = false): Notification => ({
  11. type: "turn-complete",
  12. session,
  13. directory,
  14. viewed,
  15. time: 1,
  16. })
  17. const error = (session: string, directory: string, viewed = false): Notification => ({
  18. type: "error",
  19. session,
  20. directory,
  21. viewed,
  22. time: 1,
  23. })
  24. describe("buildNotificationIndex", () => {
  25. test("builds unseen counts and unseen error flags", () => {
  26. const list = [
  27. turn("s1", "d1", false),
  28. error("s1", "d1", false),
  29. turn("s1", "d1", true),
  30. turn("s2", "d1", false),
  31. error("s3", "d2", true),
  32. ]
  33. const index = buildNotificationIndex(list)
  34. expect(index.session.all.get("s1")?.length).toBe(3)
  35. expect(index.session.unseen.get("s1")?.length).toBe(2)
  36. expect(index.session.unseenCount.get("s1")).toBe(2)
  37. expect(index.session.unseenHasError.get("s1")).toBe(true)
  38. expect(index.session.unseenCount.get("s2")).toBe(1)
  39. expect(index.session.unseenHasError.get("s2") ?? false).toBe(false)
  40. expect(index.session.unseenCount.get("s3") ?? 0).toBe(0)
  41. expect(index.session.unseenHasError.get("s3") ?? false).toBe(false)
  42. expect(index.project.unseenCount.get("d1")).toBe(3)
  43. expect(index.project.unseenHasError.get("d1")).toBe(true)
  44. expect(index.project.unseenCount.get("d2") ?? 0).toBe(0)
  45. expect(index.project.unseenHasError.get("d2") ?? false).toBe(false)
  46. })
  47. test("updates selectors after viewed transitions", () => {
  48. const list = [turn("s1", "d1", false), error("s1", "d1", false), turn("s2", "d1", false)]
  49. const next = list.map((item) => (item.session === "s1" ? { ...item, viewed: true } : item))
  50. const before = buildNotificationIndex(list)
  51. const after = buildNotificationIndex(next)
  52. expect(before.session.unseenCount.get("s1")).toBe(2)
  53. expect(before.session.unseenHasError.get("s1")).toBe(true)
  54. expect(before.project.unseenCount.get("d1")).toBe(3)
  55. expect(before.project.unseenHasError.get("d1")).toBe(true)
  56. expect(after.session.unseenCount.get("s1") ?? 0).toBe(0)
  57. expect(after.session.unseenHasError.get("s1") ?? false).toBe(false)
  58. expect(after.project.unseenCount.get("d1")).toBe(1)
  59. expect(after.project.unseenHasError.get("d1") ?? false).toBe(false)
  60. })
  61. })