-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathinternal-scheduler.test.ts
More file actions
91 lines (74 loc) · 2.47 KB
/
internal-scheduler.test.ts
File metadata and controls
91 lines (74 loc) · 2.47 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
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
vi.mock('@/lib/core/config/env', () => ({
env: {
ENABLE_INTERNAL_SCHEDULER: 'true',
CRON_SECRET: 'test-secret',
NEXT_PUBLIC_APP_URL: 'http://localhost:3000',
INTERNAL_SCHEDULER_INTERVAL_MS: '1000',
},
}))
vi.mock('@/lib/logs/console/logger', () => ({
createLogger: vi.fn().mockReturnValue({
info: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
debug: vi.fn(),
}),
}))
const mockFetch = vi.fn()
global.fetch = mockFetch
describe('Internal Scheduler', () => {
beforeEach(() => {
vi.clearAllMocks()
mockFetch.mockResolvedValue({
ok: true,
json: async () => ({ executedCount: 0 }),
})
})
afterEach(() => {
vi.clearAllMocks()
})
it('should poll schedules endpoint with correct authentication', async () => {
const { startInternalScheduler, stopInternalScheduler } = await import('./internal-scheduler')
startInternalScheduler()
// Wait for the initial poll to complete
await new Promise((resolve) => setTimeout(resolve, 100))
expect(mockFetch).toHaveBeenCalledWith(
'http://localhost:3000/api/schedules/execute',
expect.objectContaining({
method: 'GET',
headers: expect.objectContaining({
Authorization: 'Bearer test-secret',
'User-Agent': 'sim-studio-internal-scheduler/1.0',
}),
})
)
stopInternalScheduler()
})
it('should handle fetch errors gracefully', async () => {
mockFetch.mockRejectedValueOnce(new Error('Network error'))
const { startInternalScheduler, stopInternalScheduler } = await import('./internal-scheduler')
// Should not throw
startInternalScheduler()
await new Promise((resolve) => setTimeout(resolve, 100))
stopInternalScheduler()
})
it('should handle non-ok responses', async () => {
mockFetch.mockResolvedValueOnce({
ok: false,
status: 401,
text: async () => 'Unauthorized',
})
const { startInternalScheduler, stopInternalScheduler } = await import('./internal-scheduler')
// Should not throw
startInternalScheduler()
await new Promise((resolve) => setTimeout(resolve, 100))
stopInternalScheduler()
})
})
describe('shouldEnableInternalScheduler', () => {
it('should return true when ENABLE_INTERNAL_SCHEDULER is true', async () => {
const { shouldEnableInternalScheduler } = await import('./internal-scheduler')
expect(shouldEnableInternalScheduler()).toBe(true)
})
})