-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.test.ts
More file actions
138 lines (129 loc) · 3.76 KB
/
background.test.ts
File metadata and controls
138 lines (129 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { handleCommentEvent, openSpots } from '../src/entrypoints/background'
import type { CommentEvent, CommentSpot } from '../src/lib/enhancer'
const mockSender = {
tab: {
id: 123,
windowId: 456,
},
}
const mockSpot = {
type: 'TEST_SPOT',
unique_key: 'test-key',
}
describe('Background Event Handler', () => {
beforeEach(() => {
vi.useFakeTimers()
vi.setSystemTime(new Date('2025-09-19T10:00:00.000Z'))
openSpots.clear()
})
afterEach(() => {
vi.useRealTimers()
})
describe('ENHANCED Event', () => {
it('should create new comment state when textarea is enhanced', () => {
handleCommentEvent(
{
spot: mockSpot,
type: 'ENHANCED',
},
mockSender,
)
expect(Array.from(openSpots)).toMatchInlineSnapshot(`
[
[
"test-key",
{
"drafts": [
[
1758276000000,
"",
],
],
"sentOn": null,
"spot": {
"type": "TEST_SPOT",
"unique_key": "test-key",
},
"tab": {
"tabId": 123,
"windowId": 456,
},
"trashedOn": null,
},
],
]
`)
})
it('should not handle ENHANCED event without tab info', () => {
const senderWithoutTab = { tab: null }
handleCommentEvent(
{
spot: mockSpot,
type: 'ENHANCED',
},
senderWithoutTab,
)
expect(openSpots.size).toBe(0)
})
})
describe('DESTROYED Event', () => {
it('should remove comment state when textarea is destroyed', () => {
// First create a state using the actual handler
const enhanceMessage: CommentEvent = {
spot: mockSpot,
type: 'ENHANCED',
}
handleCommentEvent(enhanceMessage, mockSender)
expect(openSpots.size).toBe(1)
// Then destroy it
const destroyMessage: CommentEvent = {
spot: mockSpot,
type: 'DESTROYED',
}
handleCommentEvent(destroyMessage, mockSender)
expect(openSpots.size).toBe(0)
})
it('should handle DESTROYED event for non-existent state gracefully', () => {
const message: CommentEvent = {
spot: mockSpot,
type: 'DESTROYED',
}
// Should not throw error
handleCommentEvent(message, mockSender)
expect(openSpots.size).toBe(0)
})
})
describe('Invalid Events', () => {
it('should ignore events with unsupported type', () => {
const message: CommentEvent = {
spot: mockSpot,
type: 'LOST_FOCUS',
}
handleCommentEvent(message, mockSender)
expect(openSpots.size).toBe(0)
})
})
describe('State Management', () => {
it('should handle multiple enhanced textareas from different tabs', () => {
const spot1: CommentSpot = { type: 'SPOT1', unique_key: 'key1' }
const spot2: CommentSpot = { type: 'SPOT2', unique_key: 'key2' }
const sender1 = { tab: { id: 123, windowId: 456 } }
const sender2 = { tab: { id: 789, windowId: 456 } }
handleCommentEvent({ spot: spot1, type: 'ENHANCED' }, sender1)
handleCommentEvent({ spot: spot2, type: 'ENHANCED' }, sender2)
expect(openSpots.size).toBe(2)
})
it('should handle same spot from same tab (overwrite)', () => {
const message: CommentEvent = {
spot: mockSpot,
type: 'ENHANCED',
}
// Enhance same spot twice
handleCommentEvent(message, mockSender)
handleCommentEvent(message, mockSender)
// Should still be 1 entry (overwritten)
expect(openSpots.size).toBe(1)
})
})
})