|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createMockRequest } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ |
| 8 | + enqueueSweep: vi.fn(), |
| 9 | + verifyCronAuth: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@/lib/auth/internal', () => ({ verifyCronAuth: mocks.verifyCronAuth })) |
| 13 | +vi.mock('@/lib/knowledge/projection/enqueue', () => ({ |
| 14 | + enqueueKnowledgeProjectionSweep: mocks.enqueueSweep, |
| 15 | +})) |
| 16 | + |
| 17 | +import { GET } from '@/app/api/cron/knowledge-projection/route' |
| 18 | + |
| 19 | +function request() { |
| 20 | + return createMockRequest( |
| 21 | + 'GET', |
| 22 | + undefined, |
| 23 | + {}, |
| 24 | + 'http://localhost:3000/api/cron/knowledge-projection' |
| 25 | + ) |
| 26 | +} |
| 27 | + |
| 28 | +describe('knowledge projection sweep route', () => { |
| 29 | + beforeEach(() => { |
| 30 | + vi.clearAllMocks() |
| 31 | + mocks.verifyCronAuth.mockReturnValue(null) |
| 32 | + }) |
| 33 | + |
| 34 | + it('returns as soon as Trigger.dev accepts the pass', async () => { |
| 35 | + mocks.enqueueSweep.mockResolvedValue({ |
| 36 | + triggered: true, |
| 37 | + backend: 'trigger-dev', |
| 38 | + jobId: 'run-1', |
| 39 | + }) |
| 40 | + |
| 41 | + const response = await GET(request()) |
| 42 | + |
| 43 | + expect(response.status).toBe(202) |
| 44 | + await expect(response.json()).resolves.toEqual({ |
| 45 | + success: true, |
| 46 | + triggered: true, |
| 47 | + backend: 'trigger-dev', |
| 48 | + jobId: 'run-1', |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + it('answers 200 without a pass when the projector has nothing to do', async () => { |
| 53 | + mocks.enqueueSweep.mockResolvedValue({ triggered: false, backend: null, jobId: null }) |
| 54 | + |
| 55 | + const response = await GET(request()) |
| 56 | + |
| 57 | + expect(response.status).toBe(200) |
| 58 | + await expect(response.json()).resolves.toEqual({ |
| 59 | + success: true, |
| 60 | + triggered: false, |
| 61 | + backend: null, |
| 62 | + jobId: null, |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + it('returns the cron auth refusal without enqueueing', async () => { |
| 67 | + mocks.verifyCronAuth.mockReturnValue(new Response(null, { status: 401 })) |
| 68 | + |
| 69 | + const response = await GET(request()) |
| 70 | + |
| 71 | + expect(response.status).toBe(401) |
| 72 | + expect(mocks.enqueueSweep).not.toHaveBeenCalled() |
| 73 | + }) |
| 74 | + |
| 75 | + it('fails closed when Trigger.dev does not accept the pass', async () => { |
| 76 | + mocks.enqueueSweep.mockRejectedValue(new Error('trigger unavailable')) |
| 77 | + |
| 78 | + const response = await GET(request()) |
| 79 | + |
| 80 | + expect(response.status).toBe(500) |
| 81 | + await expect(response.json()).resolves.toEqual({ |
| 82 | + success: false, |
| 83 | + error: 'Sweep enqueue failed', |
| 84 | + }) |
| 85 | + }) |
| 86 | +}) |
0 commit comments