|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createMockRequest } from '@sim/testing' |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { mockEnqueue, mockGetJobQueue, mockVerifyCronAuth } = vi.hoisted(() => ({ |
| 8 | + mockEnqueue: vi.fn(), |
| 9 | + mockGetJobQueue: vi.fn(), |
| 10 | + mockVerifyCronAuth: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('@/lib/auth/internal', () => ({ verifyCronAuth: mockVerifyCronAuth })) |
| 14 | +vi.mock('@/lib/core/async-jobs', () => ({ getJobQueue: mockGetJobQueue })) |
| 15 | + |
| 16 | +import { GET } from '@/app/api/cron/cleanup-table-row-ttl/route' |
| 17 | + |
| 18 | +describe('table row TTL cleanup route', () => { |
| 19 | + beforeEach(() => { |
| 20 | + vi.clearAllMocks() |
| 21 | + vi.useFakeTimers() |
| 22 | + vi.setSystemTime(new Date('2026-08-22T17:12:00Z')) |
| 23 | + mockVerifyCronAuth.mockReturnValue(null) |
| 24 | + mockEnqueue.mockResolvedValue('job-ttl-1') |
| 25 | + mockGetJobQueue.mockResolvedValue({ enqueue: mockEnqueue }) |
| 26 | + }) |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + vi.useRealTimers() |
| 30 | + }) |
| 31 | + |
| 32 | + it('enqueues one serialized cleanup job', async () => { |
| 33 | + const response = await GET( |
| 34 | + createMockRequest( |
| 35 | + 'GET', |
| 36 | + undefined, |
| 37 | + {}, |
| 38 | + 'http://localhost:3000/api/cron/cleanup-table-row-ttl' |
| 39 | + ) |
| 40 | + ) |
| 41 | + |
| 42 | + expect(response.status).toBe(200) |
| 43 | + await expect(response.json()).resolves.toEqual({ triggered: true, jobId: 'job-ttl-1' }) |
| 44 | + expect(mockEnqueue).toHaveBeenCalledWith( |
| 45 | + 'cleanup-table-row-ttl', |
| 46 | + {}, |
| 47 | + expect.objectContaining({ |
| 48 | + maxAttempts: 1, |
| 49 | + jobId: 'cleanup-table-row-ttl:5958062', |
| 50 | + concurrencyKey: 'cleanup:table-row-ttl', |
| 51 | + concurrencyLimit: 1, |
| 52 | + runner: expect.any(Function), |
| 53 | + }) |
| 54 | + ) |
| 55 | + }) |
| 56 | + |
| 57 | + it('deduplicates retries within the same five-minute schedule window', async () => { |
| 58 | + const request = () => |
| 59 | + createMockRequest( |
| 60 | + 'GET', |
| 61 | + undefined, |
| 62 | + {}, |
| 63 | + 'http://localhost:3000/api/cron/cleanup-table-row-ttl' |
| 64 | + ) |
| 65 | + |
| 66 | + await GET(request()) |
| 67 | + vi.advanceTimersByTime(2 * 60 * 1000) |
| 68 | + await GET(request()) |
| 69 | + |
| 70 | + expect(mockEnqueue.mock.calls[0]?.[2]?.jobId).toBe(mockEnqueue.mock.calls[1]?.[2]?.jobId) |
| 71 | + }) |
| 72 | + |
| 73 | + it('returns the cron auth refusal without touching the queue', async () => { |
| 74 | + mockVerifyCronAuth.mockReturnValue(new Response(null, { status: 401 })) |
| 75 | + |
| 76 | + const response = await GET( |
| 77 | + createMockRequest( |
| 78 | + 'GET', |
| 79 | + undefined, |
| 80 | + {}, |
| 81 | + 'http://localhost:3000/api/cron/cleanup-table-row-ttl' |
| 82 | + ) |
| 83 | + ) |
| 84 | + |
| 85 | + expect(response.status).toBe(401) |
| 86 | + expect(mockGetJobQueue).not.toHaveBeenCalled() |
| 87 | + }) |
| 88 | +}) |
0 commit comments