|
| 1 | +/** |
| 2 | + * Integration tests for schedule status API route |
| 3 | + * |
| 4 | + * @vitest-environment node |
| 5 | + */ |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import { createMockRequest, mockScheduleStatusDb } from '@/app/api/__test-utils__/utils' |
| 8 | + |
| 9 | +// Common mocks |
| 10 | +const mockSchedule = { |
| 11 | + id: 'schedule-id', |
| 12 | + workflowId: 'workflow-id', |
| 13 | + status: 'active', |
| 14 | + failedCount: 0, |
| 15 | + lastRanAt: new Date('2024-01-01T00:00:00.000Z'), |
| 16 | + lastFailedAt: null, |
| 17 | + nextRunAt: new Date('2024-01-02T00:00:00.000Z'), |
| 18 | +} |
| 19 | + |
| 20 | +beforeEach(() => { |
| 21 | + vi.resetModules() |
| 22 | + |
| 23 | + vi.doMock('@/lib/logs/console-logger', () => ({ |
| 24 | + createLogger: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }), |
| 25 | + })) |
| 26 | + |
| 27 | + vi.doMock('crypto', () => ({ |
| 28 | + randomUUID: vi.fn(() => 'test-uuid'), |
| 29 | + default: { randomUUID: vi.fn(() => 'test-uuid') }, |
| 30 | + })) |
| 31 | +}) |
| 32 | + |
| 33 | +afterEach(() => { |
| 34 | + vi.clearAllMocks() |
| 35 | +}) |
| 36 | + |
| 37 | +describe('Schedule Status API Route', () => { |
| 38 | + it('returns schedule status successfully', async () => { |
| 39 | + mockScheduleStatusDb({}) // default mocks |
| 40 | + |
| 41 | + vi.doMock('@/lib/auth', () => ({ |
| 42 | + getSession: vi.fn().mockResolvedValue({ user: { id: 'user-id' } }), |
| 43 | + })) |
| 44 | + |
| 45 | + const req = createMockRequest('GET') |
| 46 | + |
| 47 | + const { GET } = await import('./route') |
| 48 | + |
| 49 | + const res = await GET(req, { params: Promise.resolve({ id: 'schedule-id' }) }) |
| 50 | + |
| 51 | + expect(res.status).toBe(200) |
| 52 | + const data = await res.json() |
| 53 | + |
| 54 | + expect(data).toMatchObject({ |
| 55 | + status: 'active', |
| 56 | + failedCount: 0, |
| 57 | + nextRunAt: mockSchedule.nextRunAt.toISOString(), |
| 58 | + isDisabled: false, |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + it('marks disabled schedules with isDisabled = true', async () => { |
| 63 | + mockScheduleStatusDb({ schedule: [{ ...mockSchedule, status: 'disabled' }] }) |
| 64 | + |
| 65 | + vi.doMock('@/lib/auth', () => ({ |
| 66 | + getSession: vi.fn().mockResolvedValue({ user: { id: 'user-id' } }), |
| 67 | + })) |
| 68 | + |
| 69 | + const req = createMockRequest('GET') |
| 70 | + const { GET } = await import('./route') |
| 71 | + const res = await GET(req, { params: Promise.resolve({ id: 'schedule-id' }) }) |
| 72 | + |
| 73 | + expect(res.status).toBe(200) |
| 74 | + const data = await res.json() |
| 75 | + expect(data).toHaveProperty('status', 'disabled') |
| 76 | + expect(data).toHaveProperty('isDisabled', true) |
| 77 | + expect(data).toHaveProperty('lastFailedAt') |
| 78 | + }) |
| 79 | + |
| 80 | + it('returns 404 if schedule not found', async () => { |
| 81 | + mockScheduleStatusDb({ schedule: [] }) |
| 82 | + |
| 83 | + vi.doMock('@/lib/auth', () => ({ |
| 84 | + getSession: vi.fn().mockResolvedValue({ user: { id: 'user-id' } }), |
| 85 | + })) |
| 86 | + |
| 87 | + const req = createMockRequest('GET') |
| 88 | + const { GET } = await import('./route') |
| 89 | + const res = await GET(req, { params: Promise.resolve({ id: 'missing-id' }) }) |
| 90 | + |
| 91 | + expect(res.status).toBe(404) |
| 92 | + const data = await res.json() |
| 93 | + expect(data).toHaveProperty('error', 'Schedule not found') |
| 94 | + }) |
| 95 | + |
| 96 | + it('returns 404 if related workflow not found', async () => { |
| 97 | + mockScheduleStatusDb({ workflow: [] }) |
| 98 | + |
| 99 | + vi.doMock('@/lib/auth', () => ({ |
| 100 | + getSession: vi.fn().mockResolvedValue({ user: { id: 'user-id' } }), |
| 101 | + })) |
| 102 | + |
| 103 | + const req = createMockRequest('GET') |
| 104 | + const { GET } = await import('./route') |
| 105 | + const res = await GET(req, { params: Promise.resolve({ id: 'schedule-id' }) }) |
| 106 | + |
| 107 | + expect(res.status).toBe(404) |
| 108 | + const data = await res.json() |
| 109 | + expect(data).toHaveProperty('error', 'Workflow not found') |
| 110 | + }) |
| 111 | + |
| 112 | + it('returns 403 when user is not owner of workflow', async () => { |
| 113 | + mockScheduleStatusDb({ workflow: [{ userId: 'another-user' }] }) |
| 114 | + |
| 115 | + vi.doMock('@/lib/auth', () => ({ |
| 116 | + getSession: vi.fn().mockResolvedValue({ user: { id: 'user-id' } }), |
| 117 | + })) |
| 118 | + |
| 119 | + const req = createMockRequest('GET') |
| 120 | + const { GET } = await import('./route') |
| 121 | + const res = await GET(req, { params: Promise.resolve({ id: 'schedule-id' }) }) |
| 122 | + |
| 123 | + expect(res.status).toBe(403) |
| 124 | + const data = await res.json() |
| 125 | + expect(data).toHaveProperty('error', 'Not authorized to view this schedule') |
| 126 | + }) |
| 127 | + |
| 128 | + it('returns 401 when user is not authenticated', async () => { |
| 129 | + mockScheduleStatusDb({}) |
| 130 | + |
| 131 | + vi.doMock('@/lib/auth', () => ({ |
| 132 | + getSession: vi.fn().mockResolvedValue(null), |
| 133 | + })) |
| 134 | + |
| 135 | + const req = createMockRequest('GET') |
| 136 | + const { GET } = await import('./route') |
| 137 | + const res = await GET(req, { params: Promise.resolve({ id: 'schedule-id' }) }) |
| 138 | + |
| 139 | + expect(res.status).toBe(401) |
| 140 | + const data = await res.json() |
| 141 | + expect(data).toHaveProperty('error', 'Unauthorized') |
| 142 | + }) |
| 143 | +}) |
0 commit comments