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