-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathwebhook-execution.test.ts
More file actions
351 lines (303 loc) · 10.6 KB
/
Copy pathwebhook-execution.test.ts
File metadata and controls
351 lines (303 loc) · 10.6 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* @vitest-environment node
*/
import {
dbChainMockFns,
executionPreprocessingMock,
executionPreprocessingMockFns,
loggingSessionMock,
loggingSessionMockFns,
} from '@sim/testing'
import { beforeEach, describe, expect, it, vi } from 'vitest'
const {
mockResolveWebhookRecordProviderConfig,
mockExecuteWorkflowCore,
mockWasExecutionFinalizedByCore,
mockRecordException,
mockGetActiveSpan,
mockExecuteWithIdempotency,
mockReleaseExecutionSlot,
mockLoadDeploymentVersionState,
} = vi.hoisted(() => ({
mockResolveWebhookRecordProviderConfig: vi.fn(),
mockExecuteWorkflowCore: vi.fn(),
mockWasExecutionFinalizedByCore: vi.fn(),
mockRecordException: vi.fn(),
mockGetActiveSpan: vi.fn(),
mockExecuteWithIdempotency: vi.fn(),
mockReleaseExecutionSlot: vi.fn(),
mockLoadDeploymentVersionState: vi.fn(
async (_workflowId: string, deploymentVersionId: string) => ({
blocks: {},
edges: [],
loops: {},
parallels: {},
deploymentVersionId,
})
),
}))
vi.mock('@opentelemetry/api', () => ({
trace: { getActiveSpan: mockGetActiveSpan },
}))
vi.mock('@/lib/execution/preprocessing', () => executionPreprocessingMock)
vi.mock('@/lib/logs/execution/logging-session', () => loggingSessionMock)
vi.mock('@/lib/webhooks/env-resolver', () => ({
resolveWebhookRecordProviderConfig: mockResolveWebhookRecordProviderConfig,
}))
vi.mock('@/lib/workflows/executor/execution-core', () => ({
executeWorkflowCore: mockExecuteWorkflowCore,
wasExecutionFinalizedByCore: mockWasExecutionFinalizedByCore,
}))
vi.mock('@/lib/billing/calculations/usage-reservation', () => ({
releaseExecutionSlot: mockReleaseExecutionSlot,
}))
vi.mock('@/lib/core/idempotency', () => ({
IdempotencyService: { createWebhookIdempotencyKey: vi.fn(() => 'idempotency-key') },
webhookIdempotency: {
executeWithIdempotency: mockExecuteWithIdempotency,
},
}))
vi.mock('@/lib/workflows/persistence/utils', () => ({
loadDeployedWorkflowState: vi.fn(async () => ({
blocks: {},
edges: [],
loops: {},
parallels: {},
deploymentVersionId: 'deployment-1',
})),
loadWorkflowDeploymentVersionState: mockLoadDeploymentVersionState,
}))
vi.mock('@/lib/webhooks/providers', () => ({
getProviderHandler: vi.fn(() => ({})),
}))
vi.mock('@/lib/logs/execution/trace-spans/trace-spans', () => ({
buildTraceSpans: vi.fn(() => ({ traceSpans: [] })),
}))
vi.mock('@/lib/core/execution-limits', () => ({
createTimeoutAbortController: vi.fn(() => ({
signal: new AbortController().signal,
cleanup: vi.fn(),
isTimedOut: () => false,
timeoutMs: 120_000,
})),
getTimeoutErrorMessage: vi.fn(() => 'timed out'),
}))
vi.mock('@/lib/workflows/executor/pause-persistence', () => ({
handlePostExecutionPauseState: vi.fn(),
}))
vi.mock('@/lib/webhooks/attachment-processor', () => ({
WebhookAttachmentProcessor: class {},
}))
vi.mock('@/app/api/auth/oauth/utils', () => ({
resolveOAuthAccountId: vi.fn(),
}))
vi.mock('@/executor/execution/snapshot', () => ({
ExecutionSnapshot: class {},
}))
vi.mock('@/tools/safe-assign', () => ({ safeAssign: vi.fn() }))
vi.mock('@/blocks', () => ({ getBlock: vi.fn(() => null) }))
vi.mock('@/triggers', () => ({
getTrigger: vi.fn(),
isTriggerValid: vi.fn(() => false),
}))
import {
executeWebhookJob,
resolveWebhookExecutionProviderConfig,
type WebhookExecutionPayload,
} from './webhook-execution'
describe('resolveWebhookExecutionProviderConfig', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('returns the resolved webhook record when provider config resolution succeeds', async () => {
const webhookRecord = {
id: 'webhook-1',
providerConfig: {
botToken: '{{SLACK_BOT_TOKEN}}',
},
}
const resolvedWebhookRecord = {
...webhookRecord,
providerConfig: {
botToken: 'xoxb-resolved',
},
}
mockResolveWebhookRecordProviderConfig.mockResolvedValue(resolvedWebhookRecord)
await expect(
resolveWebhookExecutionProviderConfig(webhookRecord, 'slack', 'user-1', 'workspace-1')
).resolves.toEqual(resolvedWebhookRecord)
expect(mockResolveWebhookRecordProviderConfig).toHaveBeenCalledWith(
webhookRecord,
'user-1',
'workspace-1'
)
})
it('throws a contextual error when provider config resolution fails', async () => {
mockResolveWebhookRecordProviderConfig.mockRejectedValue(new Error('env lookup failed'))
await expect(
resolveWebhookExecutionProviderConfig(
{
id: 'webhook-1',
providerConfig: {
botToken: '{{SLACK_BOT_TOKEN}}',
},
},
'slack',
'user-1',
'workspace-1'
)
).rejects.toThrow(
'Failed to resolve webhook provider config for slack webhook webhook-1: env lookup failed'
)
})
})
describe('executeWebhookJob fault vs error handling', () => {
const billingAttribution = {
actorUserId: 'user-1',
workspaceId: 'workspace-1',
organizationId: null,
billedAccountUserId: 'user-1',
billingEntity: { type: 'user' as const, id: 'user-1' },
billingPeriod: {
start: '2026-07-01T00:00:00.000Z',
end: '2026-08-01T00:00:00.000Z',
},
payerSubscription: null,
}
const payload: WebhookExecutionPayload = {
webhookId: 'webhook-1',
workflowId: 'workflow-1',
userId: 'user-1',
billingAttribution,
executionId: 'execution-1',
requestId: 'request-1',
provider: 'gmail',
body: { message: 'hello' },
headers: {},
path: '/webhook',
workspaceId: 'workspace-1',
}
beforeEach(() => {
vi.clearAllMocks()
mockExecuteWithIdempotency.mockImplementation(
(_provider: string, _key: string, operation: () => Promise<unknown>) => operation()
)
executionPreprocessingMockFns.mockPreprocessExecution.mockResolvedValue({
success: true,
actorUserId: 'user-1',
billingAttribution,
workflowRecord: {
workspaceId: 'workspace-1',
userId: 'user-1',
variables: {},
isDeployed: true,
archivedAt: null,
},
executionTimeout: { async: 120_000 },
})
mockResolveWebhookRecordProviderConfig.mockImplementation(async (record) => record)
dbChainMockFns.limit.mockResolvedValue([{ id: 'webhook-1' }])
mockGetActiveSpan.mockReturnValue({ recordException: mockRecordException })
})
it('completes the run (does not throw) when the failure was finalized by core', async () => {
mockExecuteWorkflowCore.mockRejectedValue(
new Error('Gmail 2 is missing required fields: Label')
)
mockWasExecutionFinalizedByCore.mockReturnValue(true)
const result = await executeWebhookJob(payload)
expect(result).toMatchObject({
success: false,
workflowId: 'workflow-1',
executionId: 'execution-1',
provider: 'gmail',
})
expect(loggingSessionMockFns.mockWaitForPostExecution).toHaveBeenCalled()
// User/workflow errors are already recorded by core — the catch must not re-log them.
expect(loggingSessionMockFns.mockSafeCompleteWithError).not.toHaveBeenCalled()
// The error is still recorded on the run span so it stays visible in traces.
expect(mockRecordException).toHaveBeenCalledWith(
expect.objectContaining({ message: 'Gmail 2 is missing required fields: Label' })
)
})
it('faults the run (re-throws) when the failure was not finalized by core', async () => {
mockExecuteWorkflowCore.mockRejectedValue(new Error('Workflow state not found'))
mockWasExecutionFinalizedByCore.mockReturnValue(false)
await expect(executeWebhookJob(payload)).rejects.toThrow('Workflow state not found')
// waitForPostExecution must run on every path so the finalized-by-core signal is always reliable.
expect(loggingSessionMockFns.mockWaitForPostExecution).toHaveBeenCalled()
// Pipeline/infra errors are recorded here before re-throwing to fault the trigger.dev run.
expect(loggingSessionMockFns.mockSafeCompleteWithError).toHaveBeenCalled()
})
it('executes against the deployment version admitted by webhook ingress', async () => {
mockExecuteWorkflowCore.mockResolvedValue({
success: true,
status: 'completed',
output: {},
logs: [],
executionState: {
blockStates: {},
executedBlocks: [],
blockLogs: [],
decisions: {},
completedLoops: [],
activeExecutionPath: [],
},
})
await executeWebhookJob({
...payload,
deploymentVersionId: 'deployment-admitted',
})
expect(mockLoadDeploymentVersionState).toHaveBeenCalledWith(
'workflow-1',
'deployment-admitted',
'workspace-1'
)
})
it('acknowledges and skips queued webhook work after the workflow is undeployed', async () => {
executionPreprocessingMockFns.mockPreprocessExecution.mockResolvedValueOnce({
success: true,
actorUserId: 'user-1',
billingAttribution,
workflowRecord: {
workspaceId: 'workspace-1',
userId: 'user-1',
variables: {},
isDeployed: false,
archivedAt: null,
},
executionTimeout: { async: 120_000 },
})
const result = await executeWebhookJob(payload)
expect(result).toMatchObject({ skipped: true, success: false, workflowId: 'workflow-1' })
expect(mockExecuteWorkflowCore).not.toHaveBeenCalled()
expect(mockReleaseExecutionSlot).toHaveBeenCalled()
})
it('releases the reservation when idempotency returns a cached result', async () => {
const cachedResult = {
success: true,
workflowId: 'workflow-1',
executionId: 'original-execution',
}
mockExecuteWithIdempotency.mockResolvedValueOnce(cachedResult)
await expect(executeWebhookJob(payload)).resolves.toBe(cachedResult)
expect(executionPreprocessingMockFns.mockPreprocessExecution).not.toHaveBeenCalled()
expect(mockReleaseExecutionSlot).toHaveBeenCalledWith('execution-1')
})
it('releases the reservation when background preprocessing fails', async () => {
executionPreprocessingMockFns.mockPreprocessExecution.mockResolvedValueOnce({
success: false,
error: { message: 'workflow archived', statusCode: 404 },
})
await expect(executeWebhookJob(payload)).rejects.toThrow('workflow archived')
expect(mockReleaseExecutionSlot).toHaveBeenCalledWith('execution-1')
})
it('rejects queued webhook work without an immutable attribution snapshot', async () => {
await expect(
executeWebhookJob({
...payload,
billingAttribution: undefined,
} as unknown as WebhookExecutionPayload)
).rejects.toThrow('Billing attribution snapshot must be an object')
expect(executionPreprocessingMockFns.mockPreprocessExecution).not.toHaveBeenCalled()
})
})