|
1
|
import { |
|
2
|
createStep, |
|
3
|
createWorkflow, |
|
4
|
StepResponse, |
|
5
|
WorkflowData, |
|
6
|
WorkflowResponse, |
|
7
|
} from "@medusajs/framework/workflows-sdk" |
|
8
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
9
|
import { createAdminUser } from "../../../../helpers/create-admin-user" |
|
10
|
import { setTimeout } from "timers/promises" |
|
11
|
|
|
12
|
jest.setTimeout(100000) |
|
13
|
|
|
14
|
/** |
|
15
|
* finish the workflow to prevent the runner from waiting for the end of the execution permanently |
|
16
|
*/ |
|
17
|
async function finishWorkflows( |
|
18
|
api: any, |
|
19
|
{ |
|
20
|
stepId, |
|
21
|
response, |
|
22
|
executions, |
|
23
|
}: { |
|
24
|
stepId: string |
|
25
|
response?: any |
|
26
|
executions: { transaction_id: string }[] |
|
27
|
} |
|
28
|
) { |
|
29
|
const promises: any[] = [] |
|
30
|
for (const execution of executions) { |
|
31
|
promises.push( |
|
32
|
api.post( |
|
33
|
`/admin/workflows-executions/my-workflow-name/steps/success`, |
|
34
|
{ |
|
35
|
transaction_id: execution.transaction_id, |
|
36
|
step_id: stepId, |
|
37
|
response: response || { |
|
38
|
all: "good", |
|
39
|
}, |
|
40
|
}, |
|
41
|
adminHeaders |
|
42
|
) |
|
43
|
) |
|
44
|
} |
|
45
|
|
|
46
|
await Promise.all(promises) |
|
47
|
} |
|
48
|
|
|
49
|
const adminHeaders = { |
|
50
|
headers: { |
|
51
|
"x-medusa-access-token": "test_token", |
|
52
|
}, |
|
53
|
} |
|
54
|
|
|
55
|
medusaIntegrationTestRunner({ |
|
56
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
57
|
describe("Workflow Engine API", () => { |
|
58
|
let medusaContainer |
|
59
|
|
|
60
|
beforeAll(() => { |
|
61
|
medusaContainer = getContainer() |
|
62
|
|
|
63
|
const step1 = createStep( |
|
64
|
{ |
|
65
|
name: "my-step", |
|
66
|
}, |
|
67
|
async (input: { initial: string }) => { |
|
68
|
return new StepResponse({ |
|
69
|
result: input.initial, |
|
70
|
}) |
|
71
|
} |
|
72
|
) |
|
73
|
const step2 = createStep( |
|
74
|
{ |
|
75
|
name: "my-step-async", |
|
76
|
async: true, |
|
77
|
}, |
|
78
|
async () => {} |
|
79
|
) |
|
80
|
|
|
81
|
createWorkflow( |
|
82
|
{ |
|
83
|
name: "my-workflow-name", |
|
84
|
retentionTime: 1000, |
|
85
|
}, |
|
86
|
function (input: WorkflowData<{ initial: string }>) { |
|
87
|
step1(input) |
|
88
|
const res = step2() |
|
89
|
return new WorkflowResponse(res) |
|
90
|
} |
|
91
|
) |
|
92
|
}) |
|
93
|
|
|
94
|
beforeAll(async () => { |
|
95
|
await createAdminUser(dbConnection, adminHeaders, medusaContainer) |
|
96
|
|
|
97
|
await dbUtils.snapshot() |
|
98
|
}) |
|
99
|
|
|
100
|
it("Should list all workflows in execution or completed and retrieve them by id", async () => { |
|
101
|
for (let i = 3; i--; ) { |
|
102
|
await api.post( |
|
103
|
`/admin/workflows-executions/my-workflow-name/run`, |
|
104
|
{ |
|
105
|
input: { |
|
106
|
initial: "abc", |
|
107
|
}, |
|
108
|
}, |
|
109
|
adminHeaders |
|
110
|
) |
|
111
|
} |
|
112
|
|
|
113
|
const executions = await api.get( |
|
114
|
`/admin/workflows-executions`, |
|
115
|
adminHeaders |
|
116
|
) |
|
117
|
|
|
118
|
expect(executions.data.count).toEqual(3) |
|
119
|
expect(executions.data.workflow_executions.length).toEqual(3) |
|
120
|
expect(executions.data.workflow_executions[0]).toEqual({ |
|
121
|
workflow_id: "my-workflow-name", |
|
122
|
transaction_id: expect.any(String), |
|
123
|
id: expect.any(String), |
|
124
|
state: "invoking", |
|
125
|
execution: expect.anything(), |
|
126
|
created_at: expect.any(String), |
|
127
|
updated_at: expect.any(String), |
|
128
|
deleted_at: null, |
|
129
|
}) |
|
130
|
|
|
131
|
const retrivedById = await api.get( |
|
132
|
`/admin/workflows-executions/` + |
|
133
|
executions.data.workflow_executions[0].id, |
|
134
|
adminHeaders |
|
135
|
) |
|
136
|
|
|
137
|
expect(retrivedById.data.workflow_execution).toEqual( |
|
138
|
expect.objectContaining(executions.data.workflow_executions[0]) |
|
139
|
) |
|
140
|
|
|
141
|
await finishWorkflows(api, { |
|
142
|
stepId: "my-step-async", |
|
143
|
executions: executions.data.workflow_executions, |
|
144
|
}) |
|
145
|
}) |
|
146
|
|
|
147
|
it("Should list all workflows matching the filters", async () => { |
|
148
|
const promises: any[] = [] |
|
149
|
const transactions: string[] = [] |
|
150
|
|
|
151
|
for (let i = 3; i--; ) { |
|
152
|
const transactionId = "transaction_" + (i + 1) |
|
153
|
transactions.push(transactionId) |
|
154
|
promises.push( |
|
155
|
api.post( |
|
156
|
`/admin/workflows-executions/my-workflow-name/run`, |
|
157
|
{ |
|
158
|
input: { |
|
159
|
initial: "abc", |
|
160
|
}, |
|
161
|
transaction_id: transactionId, |
|
162
|
}, |
|
163
|
adminHeaders |
|
164
|
) |
|
165
|
) |
|
166
|
} |
|
167
|
|
|
168
|
await Promise.all(promises) |
|
169
|
|
|
170
|
const executions = await api.get( |
|
171
|
`/admin/workflows-executions?transaction_id[]=transaction_1&transaction_id[]=transaction_2`, |
|
172
|
adminHeaders |
|
173
|
) |
|
174
|
|
|
175
|
expect(executions.data.count).toEqual(2) |
|
176
|
expect(executions.data.workflow_executions.length).toEqual(2) |
|
177
|
expect(executions.data.workflow_executions[0]).toEqual({ |
|
178
|
workflow_id: "my-workflow-name", |
|
179
|
transaction_id: expect.stringMatching( |
|
180
|
new RegExp(transactions.join("|")) |
|
181
|
), |
|
182
|
id: expect.any(String), |
|
183
|
state: "invoking", |
|
184
|
execution: expect.anything(), |
|
185
|
created_at: expect.any(String), |
|
186
|
updated_at: expect.any(String), |
|
187
|
deleted_at: null, |
|
188
|
}) |
|
189
|
expect(executions.data.workflow_executions[1]).toEqual({ |
|
190
|
workflow_id: "my-workflow-name", |
|
191
|
transaction_id: expect.stringMatching( |
|
192
|
new RegExp(transactions.join("|")) |
|
193
|
), |
|
194
|
id: expect.any(String), |
|
195
|
state: "invoking", |
|
196
|
execution: expect.anything(), |
|
197
|
created_at: expect.any(String), |
|
198
|
updated_at: expect.any(String), |
|
199
|
deleted_at: null, |
|
200
|
}) |
|
201
|
|
|
202
|
await finishWorkflows(api, { |
|
203
|
stepId: "my-step-async", |
|
204
|
executions: transactions.map((transactionId) => ({ |
|
205
|
transaction_id: transactionId, |
|
206
|
})), |
|
207
|
}) |
|
208
|
|
|
209
|
console.log("finished") |
|
210
|
}) |
|
211
|
|
|
212
|
it("Should execute a workflow and retrieve its execution while running and when it is completed", async () => { |
|
213
|
const wf = await api.post( |
|
214
|
`/admin/workflows-executions/my-workflow-name/run`, |
|
215
|
{ |
|
216
|
input: { |
|
217
|
initial: "abc", |
|
218
|
}, |
|
219
|
transaction_id: "trx_123", |
|
220
|
}, |
|
221
|
adminHeaders |
|
222
|
) |
|
223
|
|
|
224
|
expect(wf.data).toEqual({ |
|
225
|
acknowledgement: { |
|
226
|
transactionId: "trx_123", |
|
227
|
workflowId: "my-workflow-name", |
|
228
|
hasFailed: false, |
|
229
|
hasFinished: false, |
|
230
|
}, |
|
231
|
}) |
|
232
|
|
|
233
|
await setTimeout(2000) |
|
234
|
|
|
235
|
const execution = await api.get( |
|
236
|
`/admin/workflows-executions/my-workflow-name/${wf.data.acknowledgement.transactionId}`, |
|
237
|
adminHeaders |
|
238
|
) |
|
239
|
|
|
240
|
expect(execution.data).toEqual({ |
|
241
|
workflow_execution: expect.objectContaining({ |
|
242
|
workflow_id: "my-workflow-name", |
|
243
|
transaction_id: "trx_123", |
|
244
|
id: expect.any(String), |
|
245
|
state: "invoking", |
|
246
|
execution: expect.objectContaining({ |
|
247
|
hasAsyncSteps: true, |
|
248
|
hasFailedSteps: false, |
|
249
|
hasSkippedSteps: false, |
|
250
|
hasWaitingSteps: true, |
|
251
|
hasRevertedSteps: false, |
|
252
|
}), |
|
253
|
context: expect.objectContaining({ |
|
254
|
data: expect.objectContaining({ |
|
255
|
invoke: { |
|
256
|
"my-step": { |
|
257
|
__type: "Symbol(WorkflowWorkflowData)", |
|
258
|
output: { |
|
259
|
__type: "Symbol(WorkflowStepResponse)", |
|
260
|
output: { |
|
261
|
result: "abc", |
|
262
|
}, |
|
263
|
compensateInput: { |
|
264
|
result: "abc", |
|
265
|
}, |
|
266
|
}, |
|
267
|
}, |
|
268
|
}, |
|
269
|
payload: { |
|
270
|
initial: "abc", |
|
271
|
}, |
|
272
|
}), |
|
273
|
}), |
|
274
|
}), |
|
275
|
}) |
|
276
|
|
|
277
|
const respondAsync = await api.post( |
|
278
|
`/admin/workflows-executions/my-workflow-name/steps/success`, |
|
279
|
{ |
|
280
|
transaction_id: "trx_123", |
|
281
|
step_id: "my-step-async", |
|
282
|
response: { |
|
283
|
all: "good", |
|
284
|
}, |
|
285
|
}, |
|
286
|
adminHeaders |
|
287
|
) |
|
288
|
|
|
289
|
expect(respondAsync.data.success).toEqual(true) |
|
290
|
|
|
291
|
const completed = await api.get( |
|
292
|
`/admin/workflows-executions/my-workflow-name/trx_123`, |
|
293
|
adminHeaders |
|
294
|
) |
|
295
|
|
|
296
|
expect(completed.data).toEqual({ |
|
297
|
workflow_execution: expect.objectContaining({ |
|
298
|
workflow_id: "my-workflow-name", |
|
299
|
transaction_id: "trx_123", |
|
300
|
state: "done", |
|
301
|
execution: expect.objectContaining({ |
|
302
|
hasAsyncSteps: true, |
|
303
|
hasFailedSteps: false, |
|
304
|
hasSkippedSteps: false, |
|
305
|
hasWaitingSteps: false, |
|
306
|
hasRevertedSteps: false, |
|
307
|
}), |
|
308
|
context: expect.objectContaining({ |
|
309
|
data: expect.objectContaining({ |
|
310
|
invoke: expect.objectContaining({ |
|
311
|
"my-step-async": { |
|
312
|
__type: "Symbol(WorkflowStepResponse)", |
|
313
|
output: { |
|
314
|
all: "good", |
|
315
|
}, |
|
316
|
compensateInput: { |
|
317
|
all: "good", |
|
318
|
}, |
|
319
|
}, |
|
320
|
}), |
|
321
|
}), |
|
322
|
}), |
|
323
|
}), |
|
324
|
}) |
|
325
|
}) |
|
326
|
}) |
|
327
|
}, |
|
328
|
}) |