titlebar-history.test.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { describe, expect, test } from "bun:test"
  2. import { applyPath, backPath, forwardPath, type TitlebarHistory } from "./titlebar-history"
  3. function history(): TitlebarHistory {
  4. return { stack: [], index: 0, action: undefined }
  5. }
  6. describe("titlebar history", () => {
  7. test("append and trim keeps max bounded", () => {
  8. let state = history()
  9. state = applyPath(state, "/", 3)
  10. state = applyPath(state, "/a", 3)
  11. state = applyPath(state, "/b", 3)
  12. state = applyPath(state, "/c", 3)
  13. expect(state.stack).toEqual(["/a", "/b", "/c"])
  14. expect(state.stack.length).toBe(3)
  15. expect(state.index).toBe(2)
  16. })
  17. test("back and forward indexes stay correct after trimming", () => {
  18. let state = history()
  19. state = applyPath(state, "/", 3)
  20. state = applyPath(state, "/a", 3)
  21. state = applyPath(state, "/b", 3)
  22. state = applyPath(state, "/c", 3)
  23. expect(state.stack).toEqual(["/a", "/b", "/c"])
  24. expect(state.index).toBe(2)
  25. const back = backPath(state)
  26. expect(back?.to).toBe("/b")
  27. expect(back?.state.index).toBe(1)
  28. const afterBack = applyPath(back!.state, back!.to, 3)
  29. expect(afterBack.stack).toEqual(["/a", "/b", "/c"])
  30. expect(afterBack.index).toBe(1)
  31. const forward = forwardPath(afterBack)
  32. expect(forward?.to).toBe("/c")
  33. expect(forward?.state.index).toBe(2)
  34. const afterForward = applyPath(forward!.state, forward!.to, 3)
  35. expect(afterForward.stack).toEqual(["/a", "/b", "/c"])
  36. expect(afterForward.index).toBe(2)
  37. })
  38. test("action-driven navigation does not push duplicate history entries", () => {
  39. const state: TitlebarHistory = {
  40. stack: ["/", "/a", "/b"],
  41. index: 2,
  42. action: undefined,
  43. }
  44. const back = backPath(state)
  45. expect(back?.to).toBe("/a")
  46. const next = applyPath(back!.state, back!.to, 10)
  47. expect(next.stack).toEqual(["/", "/a", "/b"])
  48. expect(next.index).toBe(1)
  49. expect(next.action).toBeUndefined()
  50. })
  51. })