-
Notifications
You must be signed in to change notification settings - Fork 673
Expand file tree
/
Copy pathuseQueueNotificationBanners.test.ts
More file actions
349 lines (298 loc) · 8.22 KB
/
Copy pathuseQueueNotificationBanners.test.ts
File metadata and controls
349 lines (298 loc) · 8.22 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import { render } from '@testing-library/vue'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { nextTick, reactive } from 'vue'
import { useQueueNotificationBanners } from '@/composables/queue/useQueueNotificationBanners'
import { useExecutionStore } from '@/stores/executionStore'
import { useQueueStore } from '@/stores/queueStore'
const mockApi = vi.hoisted(() => new EventTarget())
vi.mock('@/scripts/api', () => ({
api: mockApi
}))
type MockTask = {
displayStatus: 'Completed' | 'Failed' | 'Cancelled' | 'Running' | 'Pending'
executionEndTimestamp?: number
previewOutput?: {
isImage: boolean
urlWithTimestamp: string
}
}
vi.mock('@/stores/queueStore', () => {
const state = reactive({
pendingTasks: [] as MockTask[],
runningTasks: [] as MockTask[],
historyTasks: [] as MockTask[]
})
return {
useQueueStore: () => state
}
})
vi.mock('@/stores/executionStore', () => {
const state = reactive({
isIdle: true
})
return {
useExecutionStore: () => state
}
})
const mountComposable = () => {
let composable: ReturnType<typeof useQueueNotificationBanners>
const result = render({
template: '<div />',
setup() {
composable = useQueueNotificationBanners()
return {}
}
})
return { ...result, composable: composable! }
}
describe(useQueueNotificationBanners, () => {
const queueStore = () =>
useQueueStore() as {
pendingTasks: MockTask[]
runningTasks: MockTask[]
historyTasks: MockTask[]
}
const executionStore = () => useExecutionStore() as { isIdle: boolean }
const resetState = () => {
queueStore().pendingTasks = []
queueStore().runningTasks = []
queueStore().historyTasks = []
executionStore().isIdle = true
}
const createTask = (
options: {
state?: MockTask['displayStatus']
ts?: number
previewUrl?: string
isImage?: boolean
} = {}
): MockTask => {
const {
state = 'Completed',
ts = Date.now(),
previewUrl,
isImage = true
} = options
const task: MockTask = {
displayStatus: state,
executionEndTimestamp: ts
}
if (previewUrl) {
task.previewOutput = {
isImage,
urlWithTimestamp: previewUrl
}
}
return task
}
const runBatch = async (options: {
start: number
finish: number
tasks: MockTask[]
}) => {
const { start, finish, tasks } = options
vi.setSystemTime(start)
executionStore().isIdle = false
await nextTick()
vi.setSystemTime(finish)
queueStore().historyTasks = tasks
executionStore().isIdle = true
await nextTick()
}
beforeEach(() => {
resetState()
})
afterEach(() => {
vi.runOnlyPendingTimers()
resetState()
})
it('shows queued notifications from promptQueued events', async () => {
const { unmount, composable } = mountComposable()
try {
mockApi.dispatchEvent(
new CustomEvent('promptQueued', { detail: { batchCount: 4 } })
)
await nextTick()
expect(composable.currentNotification.value).toEqual({
type: 'queued',
count: 4
})
await vi.advanceTimersByTimeAsync(4000)
await nextTick()
expect(composable.currentNotification.value).toBeNull()
} finally {
unmount()
}
})
it('shows queued pending then queued confirmation', async () => {
const { unmount, composable } = mountComposable()
try {
mockApi.dispatchEvent(
new CustomEvent('promptQueueing', {
detail: { requestId: 1, batchCount: 2 }
})
)
await nextTick()
expect(composable.currentNotification.value).toEqual({
type: 'queuedPending',
count: 2,
requestId: 1
})
mockApi.dispatchEvent(
new CustomEvent('promptQueued', {
detail: { requestId: 1, batchCount: 2 }
})
)
await nextTick()
expect(composable.currentNotification.value).toEqual({
type: 'queued',
count: 2,
requestId: 1
})
} finally {
unmount()
}
})
it('falls back to 1 when queued batch count is invalid', async () => {
const { unmount, composable } = mountComposable()
try {
mockApi.dispatchEvent(
new CustomEvent('promptQueued', { detail: { batchCount: 0 } })
)
await nextTick()
expect(composable.currentNotification.value).toEqual({
type: 'queued',
count: 1
})
} finally {
unmount()
}
})
it('shows a completed notification from a finished batch', async () => {
const { unmount, composable } = mountComposable()
const now = Date.now()
try {
await runBatch({
start: now + 1_000,
finish: now + 1_200,
tasks: [
createTask({
ts: now + 1_050,
previewUrl: 'https://example.com/preview.png'
})
]
})
expect(composable.currentNotification.value).toEqual({
type: 'completed',
count: 1,
thumbnailUrls: ['https://example.com/preview.png']
})
} finally {
unmount()
}
})
it('shows one completion notification when history updates after queue becomes idle', async () => {
const { unmount, composable } = mountComposable()
const now = Date.now()
try {
vi.setSystemTime(now + 4_000)
executionStore().isIdle = false
await nextTick()
vi.setSystemTime(now + 4_100)
executionStore().isIdle = true
queueStore().historyTasks = []
await nextTick()
expect(composable.currentNotification.value).toBeNull()
queueStore().historyTasks = [
createTask({
ts: now + 4_050,
previewUrl: 'https://example.com/race-preview.png'
})
]
await nextTick()
expect(composable.currentNotification.value).toEqual({
type: 'completed',
count: 1,
thumbnailUrls: ['https://example.com/race-preview.png']
})
await vi.advanceTimersByTimeAsync(4000)
await nextTick()
expect(composable.currentNotification.value).toBeNull()
await vi.advanceTimersByTimeAsync(4000)
await nextTick()
expect(composable.currentNotification.value).toBeNull()
} finally {
unmount()
}
})
it('queues both completed and failed notifications for mixed batches', async () => {
const { unmount, composable } = mountComposable()
const now = Date.now()
try {
await runBatch({
start: now + 2_000,
finish: now + 2_200,
tasks: [
createTask({
ts: now + 2_050,
previewUrl: 'https://example.com/result.png'
}),
createTask({ ts: now + 2_060 }),
createTask({ ts: now + 2_070 }),
createTask({ state: 'Failed', ts: now + 2_080 })
]
})
expect(composable.currentNotification.value).toEqual({
type: 'completed',
count: 3,
thumbnailUrls: ['https://example.com/result.png']
})
await vi.advanceTimersByTimeAsync(4000)
await nextTick()
expect(composable.currentNotification.value).toEqual({
type: 'failed',
count: 1
})
} finally {
unmount()
}
})
it('uses up to two completion thumbnails for notification icon previews', async () => {
const { unmount, composable } = mountComposable()
const now = Date.now()
try {
await runBatch({
start: now + 3_000,
finish: now + 3_300,
tasks: [
createTask({
ts: now + 3_050,
previewUrl: 'https://example.com/preview-1.png'
}),
createTask({
ts: now + 3_060,
previewUrl: 'https://example.com/preview-2.png'
}),
createTask({
ts: now + 3_070,
previewUrl: 'https://example.com/preview-3.png'
}),
createTask({
ts: now + 3_080,
previewUrl: 'https://example.com/preview-4.png'
})
]
})
expect(composable.currentNotification.value).toEqual({
type: 'completed',
count: 4,
thumbnailUrls: [
'https://example.com/preview-1.png',
'https://example.com/preview-2.png'
]
})
} finally {
unmount()
}
})
})