|
| 1 | +import { db } from '@sim/db' |
| 2 | +import { idempotencyKey } from '@sim/db/schema' |
| 3 | +import { asyncJobsRegionMock } from '@sim/testing/mocks/async-jobs-region.mock' |
| 4 | +import { triggerSdkMock, triggerSdkMockFns } from '@sim/testing/mocks/trigger-sdk.mock' |
| 5 | +import { generateId } from '@sim/utils/id' |
| 6 | +import { inArray } from 'drizzle-orm' |
| 7 | +import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' |
| 8 | +import { TriggerDevJobQueue } from '@/lib/core/async-jobs/backends/trigger-dev' |
| 9 | + |
| 10 | +vi.mock('@trigger.dev/sdk', () => triggerSdkMock) |
| 11 | +vi.mock('@/lib/core/async-jobs/region', () => asyncJobsRegionMock) |
| 12 | + |
| 13 | +const receipts: string[] = [] |
| 14 | +const runs = new Map< |
| 15 | + string, |
| 16 | + { id: string; taskIdentifier: string; status: string; payload: unknown; createdAt: Date } |
| 17 | +>() |
| 18 | + |
| 19 | +beforeEach(() => { |
| 20 | + triggerSdkMockFns.mockRunsList.mockReset() |
| 21 | + runs.clear() |
| 22 | + triggerSdkMockFns.mockTasksTrigger.mockImplementation(async (type, payload) => { |
| 23 | + const id = `run_${generateId()}` |
| 24 | + runs.set(id, { id, taskIdentifier: type, status: 'QUEUED', payload, createdAt: new Date() }) |
| 25 | + return { id } |
| 26 | + }) |
| 27 | + triggerSdkMockFns.mockRunsRetrieve.mockImplementation(async (id) => { |
| 28 | + const run = runs.get(id) |
| 29 | + if (!run) throw new Error('Run not found') |
| 30 | + return run |
| 31 | + }) |
| 32 | + triggerSdkMockFns.mockRunsCancel.mockImplementation(async (id) => { |
| 33 | + const run = runs.get(id) |
| 34 | + if (!run) throw new Error('Run not found') |
| 35 | + run.status = 'CANCELED' |
| 36 | + return run |
| 37 | + }) |
| 38 | +}) |
| 39 | + |
| 40 | +afterAll(async () => { |
| 41 | + if (receipts.length) await db.delete(idempotencyKey).where(inArray(idempotencyKey.key, receipts)) |
| 42 | +}) |
| 43 | + |
| 44 | +async function enqueue() { |
| 45 | + const executionId = generateId() |
| 46 | + const workflowId = generateId() |
| 47 | + const rootJobId = `workflow-execution:${executionId}` |
| 48 | + receipts.push(`trigger-job:${rootJobId}`) |
| 49 | + const id = await new TriggerDevJobQueue().enqueue( |
| 50 | + 'workflow-execution', |
| 51 | + { executionId, workflowId }, |
| 52 | + { jobId: rootJobId } |
| 53 | + ) |
| 54 | + return { id, binding: { workflowId, executionId, rootJobId } } |
| 55 | +} |
| 56 | + |
| 57 | +describe('accepted Trigger runs before tag indexing', () => { |
| 58 | + it('persists a receipt and lets another queue instance read the accepted run', async () => { |
| 59 | + const { id, binding } = await enqueue() |
| 60 | + const reader = new TriggerDevJobQueue() |
| 61 | + expect(await reader.getJob(binding.rootJobId)).toMatchObject({ |
| 62 | + id, |
| 63 | + status: 'pending', |
| 64 | + metadata: { workflowId: binding.workflowId }, |
| 65 | + }) |
| 66 | + const stored = await db |
| 67 | + .select() |
| 68 | + .from(idempotencyKey) |
| 69 | + .where(inArray(idempotencyKey.key, receipts)) |
| 70 | + expect( |
| 71 | + stored.some( |
| 72 | + (row) => |
| 73 | + row.key === `trigger-job:${binding.rootJobId}` && |
| 74 | + (row.result as { runId: string }).runId === id |
| 75 | + ) |
| 76 | + ).toBe(true) |
| 77 | + }) |
| 78 | + |
| 79 | + it('cancels an accepted run while every tag search is still empty', async () => { |
| 80 | + const { id, binding } = await enqueue() |
| 81 | + expect(await new TriggerDevJobQueue().cancelByExecution(binding, 'standalone')).toBe(1) |
| 82 | + expect(runs.get(id)?.status).toBe('CANCELED') |
| 83 | + }) |
| 84 | + |
| 85 | + it('keeps a retry discoverable and cancels a run only once when its tags catch up', async () => { |
| 86 | + const { id, binding } = await enqueue() |
| 87 | + triggerSdkMockFns.mockTasksTrigger.mockResolvedValueOnce({ id }) |
| 88 | + await new TriggerDevJobQueue().enqueue( |
| 89 | + 'workflow-execution', |
| 90 | + { |
| 91 | + workflowId: binding.workflowId, |
| 92 | + executionId: binding.executionId, |
| 93 | + }, |
| 94 | + { jobId: binding.rootJobId } |
| 95 | + ) |
| 96 | + triggerSdkMockFns.mockRunsList.mockImplementation(() => ({ |
| 97 | + async *[Symbol.asyncIterator]() { |
| 98 | + yield { |
| 99 | + id, |
| 100 | + taskIdentifier: 'workflow-execution', |
| 101 | + tags: [`workflowId:${binding.workflowId}`, `executionId:${binding.executionId}`], |
| 102 | + } |
| 103 | + }, |
| 104 | + })) |
| 105 | + const queue = new TriggerDevJobQueue() |
| 106 | + expect(await queue.getJob(binding.rootJobId)).toMatchObject({ id }) |
| 107 | + expect(await queue.cancelByExecution(binding, 'standalone')).toBe(1) |
| 108 | + expect(runs.get(id)?.status).toBe('CANCELED') |
| 109 | + }) |
| 110 | + |
| 111 | + it('does not cancel a receipt belonging to another workflow or cancellation scope', async () => { |
| 112 | + const { id, binding } = await enqueue() |
| 113 | + const queue = new TriggerDevJobQueue() |
| 114 | + expect( |
| 115 | + await queue.cancelByExecution({ ...binding, workflowId: generateId() }, 'standalone') |
| 116 | + ).toBe(0) |
| 117 | + expect( |
| 118 | + await queue.cancelByExecution({ ...binding, executionId: generateId() }, 'standalone') |
| 119 | + ).toBe(0) |
| 120 | + expect(await queue.cancelByExecution(binding, 'resume')).toBe(0) |
| 121 | + expect(runs.get(id)?.status).toBe('QUEUED') |
| 122 | + }) |
| 123 | + |
| 124 | + it('does not report a completed receipt as a successful cancellation', async () => { |
| 125 | + const { id, binding } = await enqueue() |
| 126 | + runs.get(id)!.status = 'COMPLETED' |
| 127 | + expect(await new TriggerDevJobQueue().cancelByExecution(binding, 'standalone')).toBe(0) |
| 128 | + expect(runs.get(id)?.status).toBe('COMPLETED') |
| 129 | + }) |
| 130 | +}) |
0 commit comments