breadcrumbs.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { beforeEach, describe, expect, it } from 'vitest'
  2. import {
  3. getMirroredBreadcrumbs,
  4. getOwnershipOfBreadcrumbSnapshot,
  5. MIRRORED_BREADCRUMBS,
  6. takeBreadcrumbSnapshot,
  7. } from './breadcrumbs'
  8. describe('breadcrumbs', () => {
  9. beforeEach(() => {
  10. // Clear the ring buffer by popping all items
  11. while (MIRRORED_BREADCRUMBS.length > 0) {
  12. MIRRORED_BREADCRUMBS.popFront()
  13. }
  14. })
  15. describe('getMirroredBreadcrumbs', () => {
  16. it('should return an array of breadcrumbs from the ring buffer', () => {
  17. const result = getMirroredBreadcrumbs()
  18. expect(Array.isArray(result)).toBe(true)
  19. })
  20. it('should return empty array when no breadcrumbs exist', () => {
  21. const result = getMirroredBreadcrumbs()
  22. expect(result).toHaveLength(0)
  23. })
  24. it('should return breadcrumbs after they are added to ring buffer', () => {
  25. const mockBreadcrumb = { message: 'test', timestamp: Date.now() } as any
  26. MIRRORED_BREADCRUMBS.pushBack(mockBreadcrumb)
  27. const result = getMirroredBreadcrumbs()
  28. expect(result).toHaveLength(1)
  29. expect(result[0]).toEqual(mockBreadcrumb)
  30. })
  31. })
  32. describe('takeBreadcrumbSnapshot', () => {
  33. it('should capture current breadcrumbs into a snapshot', () => {
  34. const mockBreadcrumb = { message: 'test', timestamp: Date.now() } as any
  35. MIRRORED_BREADCRUMBS.pushBack(mockBreadcrumb)
  36. takeBreadcrumbSnapshot()
  37. const snapshot = getOwnershipOfBreadcrumbSnapshot()
  38. expect(snapshot).toHaveLength(1)
  39. expect(snapshot?.[0]).toEqual(mockBreadcrumb)
  40. })
  41. it('should update snapshot when called multiple times', () => {
  42. const mockBreadcrumb1 = { message: 'first', timestamp: Date.now() } as any
  43. const mockBreadcrumb2 = { message: 'second', timestamp: Date.now() } as any
  44. MIRRORED_BREADCRUMBS.pushBack(mockBreadcrumb1)
  45. takeBreadcrumbSnapshot()
  46. MIRRORED_BREADCRUMBS.pushBack(mockBreadcrumb2)
  47. takeBreadcrumbSnapshot()
  48. const snapshot = getOwnershipOfBreadcrumbSnapshot()
  49. expect(snapshot).toHaveLength(2)
  50. })
  51. })
  52. describe('getOwnershipOfBreadcrumbSnapshot', () => {
  53. it('should return null initially when no snapshot has been taken', () => {
  54. const snapshot = getOwnershipOfBreadcrumbSnapshot()
  55. expect(snapshot).toBeNull()
  56. })
  57. it('should return the snapshot after takeBreadcrumbSnapshot is called', () => {
  58. const mockBreadcrumb = { message: 'test', timestamp: Date.now() } as any
  59. MIRRORED_BREADCRUMBS.pushBack(mockBreadcrumb)
  60. takeBreadcrumbSnapshot()
  61. const snapshot = getOwnershipOfBreadcrumbSnapshot()
  62. expect(snapshot).not.toBeNull()
  63. expect(snapshot).toHaveLength(1)
  64. })
  65. it('should clear the snapshot after returning it', () => {
  66. const mockBreadcrumb = { message: 'test', timestamp: Date.now() } as any
  67. MIRRORED_BREADCRUMBS.pushBack(mockBreadcrumb)
  68. takeBreadcrumbSnapshot()
  69. const firstSnapshot = getOwnershipOfBreadcrumbSnapshot()
  70. const secondSnapshot = getOwnershipOfBreadcrumbSnapshot()
  71. expect(firstSnapshot).not.toBeNull()
  72. expect(secondSnapshot).toBeNull()
  73. })
  74. it('should take ownership and prevent subsequent calls from getting the same snapshot', () => {
  75. takeBreadcrumbSnapshot()
  76. const snapshot1 = getOwnershipOfBreadcrumbSnapshot()
  77. const snapshot2 = getOwnershipOfBreadcrumbSnapshot()
  78. expect(snapshot1).not.toBe(snapshot2)
  79. expect(snapshot2).toBeNull()
  80. })
  81. })
  82. })