|
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 { Modules, TransactionState } from "@medusajs/utils" |
|
10
|
import { |
|
11
|
adminHeaders, |
|
12
|
createAdminUser, |
|
13
|
} from "../../../../helpers/create-admin-user" |
|
14
|
import { setTimeout } from "timers/promises" |
|
15
|
import { IWorkflowEngineService } from "@medusajs/framework/types" |
|
16
|
|
|
17
|
jest.setTimeout(300000) |
|
18
|
|
|
19
|
medusaIntegrationTestRunner({ |
|
20
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
21
|
let container |
|
22
|
let workflowOrcModule: IWorkflowEngineService |
|
23
|
|
|
24
|
beforeAll(async () => { |
|
25
|
container = getContainer() |
|
26
|
await createAdminUser(dbConnection, adminHeaders, container) |
|
27
|
workflowOrcModule = container.resolve(Modules.WORKFLOW_ENGINE) |
|
28
|
|
|
29
|
await dbUtils.snapshot() |
|
30
|
}) |
|
31
|
|
|
32
|
describe("GET /admin/workflow-executions", () => { |
|
33
|
it("should filter using q", async () => { |
|
34
|
const step1 = createStep( |
|
35
|
{ |
|
36
|
name: "my-step", |
|
37
|
}, |
|
38
|
async (_) => { |
|
39
|
return new StepResponse({ result: "success" }) |
|
40
|
} |
|
41
|
) |
|
42
|
|
|
43
|
const workflowName = "workflow-admin/workflow-executions" |
|
44
|
createWorkflow( |
|
45
|
{ |
|
46
|
name: workflowName, |
|
47
|
retentionTime: 50, |
|
48
|
}, |
|
49
|
function (input: WorkflowData<{ initial: string }>) { |
|
50
|
const stepRes = step1() |
|
51
|
|
|
52
|
return new WorkflowResponse(stepRes) |
|
53
|
} |
|
54
|
) |
|
55
|
|
|
56
|
const engine = container.resolve(Modules.WORKFLOW_ENGINE) |
|
57
|
|
|
58
|
const transactionId = "test-transaction-id" |
|
59
|
await engine.run(workflowName, { |
|
60
|
transactionId, |
|
61
|
input: { |
|
62
|
initial: "test", |
|
63
|
}, |
|
64
|
}) |
|
65
|
|
|
66
|
const transactionId2 = "unknown" |
|
67
|
await engine.run(workflowName, { |
|
68
|
transactionId: transactionId2, |
|
69
|
input: { |
|
70
|
initial: "test", |
|
71
|
}, |
|
72
|
}) |
|
73
|
|
|
74
|
const q = "transaction-id" |
|
75
|
const response = await api.get( |
|
76
|
`/admin/workflows-executions?q=${q}`, |
|
77
|
adminHeaders |
|
78
|
) |
|
79
|
|
|
80
|
expect(response.status).toEqual(200) |
|
81
|
expect(response.data.workflow_executions.length).toEqual(1) |
|
82
|
expect(response.data.workflow_executions[0].transaction_id).toEqual( |
|
83
|
transactionId |
|
84
|
) |
|
85
|
|
|
86
|
const q2 = "known" |
|
87
|
const response2 = await api.get( |
|
88
|
`/admin/workflows-executions?q=${q2}`, |
|
89
|
adminHeaders |
|
90
|
) |
|
91
|
|
|
92
|
expect(response2.status).toEqual(200) |
|
93
|
expect(response2.data.workflow_executions.length).toEqual(1) |
|
94
|
expect(response2.data.workflow_executions[0].transaction_id).toEqual( |
|
95
|
transactionId2 |
|
96
|
) |
|
97
|
}) |
|
98
|
}) |
|
99
|
|
|
100
|
describe("POST /admin/workflows-executions/[workflow_id]/steps/failure", function () { |
|
101
|
it("should set step as failed", async () => { |
|
102
|
const stepId = "test-step" |
|
103
|
const step = createStep( |
|
104
|
{ |
|
105
|
name: stepId, |
|
106
|
async: true, |
|
107
|
}, |
|
108
|
() => {} |
|
109
|
) |
|
110
|
|
|
111
|
const workflowId = |
|
112
|
"workflow-f" + Math.random().toString(36).substring(2, 15) |
|
113
|
createWorkflow( |
|
114
|
{ |
|
115
|
name: workflowId, |
|
116
|
retentionTime: 60, |
|
117
|
}, |
|
118
|
() => { |
|
119
|
step() |
|
120
|
return new WorkflowResponse(void 0) |
|
121
|
} |
|
122
|
) |
|
123
|
|
|
124
|
const transactionId = |
|
125
|
"trx_123_f" + Math.random().toString(36).substring(2, 15) |
|
126
|
const engine = container.resolve( |
|
127
|
Modules.WORKFLOW_ENGINE |
|
128
|
) as IWorkflowEngineService |
|
129
|
await engine.run(workflowId, { |
|
130
|
transactionId, |
|
131
|
}) |
|
132
|
let workflowDetail = ( |
|
133
|
await api.get( |
|
134
|
`/admin/workflows-executions/${workflowId}/${transactionId}`, |
|
135
|
adminHeaders |
|
136
|
) |
|
137
|
).data.workflow_execution |
|
138
|
|
|
139
|
expect(workflowDetail.state).toBe(TransactionState.INVOKING) |
|
140
|
|
|
141
|
const setFailureResponse = await api.post( |
|
142
|
`/admin/workflows-executions/${workflowId}/steps/failure`, |
|
143
|
{ |
|
144
|
transaction_id: transactionId, |
|
145
|
step_id: stepId, |
|
146
|
}, |
|
147
|
adminHeaders |
|
148
|
) |
|
149
|
|
|
150
|
expect(setFailureResponse.status).toBe(200) |
|
151
|
expect(setFailureResponse.data).toEqual( |
|
152
|
expect.objectContaining({ |
|
153
|
success: true, |
|
154
|
}) |
|
155
|
) |
|
156
|
|
|
157
|
workflowDetail = ( |
|
158
|
await api.get( |
|
159
|
`/admin/workflows-executions/${workflowId}/${transactionId}`, |
|
160
|
adminHeaders |
|
161
|
) |
|
162
|
).data.workflow_execution |
|
163
|
|
|
164
|
expect(workflowDetail).toEqual( |
|
165
|
expect.objectContaining({ |
|
166
|
state: TransactionState.REVERTED, |
|
167
|
}) |
|
168
|
) |
|
169
|
}) |
|
170
|
}) |
|
171
|
|
|
172
|
describe("POST /admin/workflows-executions/[workflow_id]/steps/success", function () { |
|
173
|
it("should set step as successful", async () => { |
|
174
|
const stepId = "test-step" |
|
175
|
const step = createStep( |
|
176
|
{ |
|
177
|
name: stepId, |
|
178
|
async: true, |
|
179
|
}, |
|
180
|
() => {} |
|
181
|
) |
|
182
|
|
|
183
|
const workflowId = |
|
184
|
"workflow_s" + Math.random().toString(36).substring(2, 15) |
|
185
|
createWorkflow( |
|
186
|
{ |
|
187
|
name: workflowId, |
|
188
|
retentionTime: 60, |
|
189
|
}, |
|
190
|
() => { |
|
191
|
step() |
|
192
|
return new WorkflowResponse(void 0) |
|
193
|
} |
|
194
|
) |
|
195
|
|
|
196
|
const transactionId = |
|
197
|
"trx_123_s" + Math.random().toString(36).substring(2, 15) |
|
198
|
const engine = container.resolve( |
|
199
|
Modules.WORKFLOW_ENGINE |
|
200
|
) as IWorkflowEngineService |
|
201
|
await engine.run(workflowId, { |
|
202
|
transactionId, |
|
203
|
}) |
|
204
|
let workflowDetail = ( |
|
205
|
await api.get( |
|
206
|
`/admin/workflows-executions/${workflowId}/${transactionId}`, |
|
207
|
adminHeaders |
|
208
|
) |
|
209
|
).data.workflow_execution |
|
210
|
|
|
211
|
expect(workflowDetail.state).toBe(TransactionState.INVOKING) |
|
212
|
|
|
213
|
const setSuccessResponse = await api.post( |
|
214
|
`/admin/workflows-executions/${workflowId}/steps/success`, |
|
215
|
{ |
|
216
|
transaction_id: transactionId, |
|
217
|
step_id: stepId, |
|
218
|
}, |
|
219
|
adminHeaders |
|
220
|
) |
|
221
|
|
|
222
|
expect(setSuccessResponse.status).toBe(200) |
|
223
|
expect(setSuccessResponse.data).toEqual( |
|
224
|
expect.objectContaining({ |
|
225
|
success: true, |
|
226
|
}) |
|
227
|
) |
|
228
|
|
|
229
|
workflowDetail = ( |
|
230
|
await api.get( |
|
231
|
`/admin/workflows-executions/${workflowId}/${transactionId}`, |
|
232
|
adminHeaders |
|
233
|
) |
|
234
|
).data.workflow_execution |
|
235
|
|
|
236
|
expect(workflowDetail).toEqual( |
|
237
|
expect.objectContaining({ |
|
238
|
state: TransactionState.DONE, |
|
239
|
}) |
|
240
|
) |
|
241
|
}) |
|
242
|
}) |
|
243
|
|
|
244
|
describe("Workflow Orchestrator module subscribe", function () { |
|
245
|
it("should subscribe to a workflow and receive the response when it finishes (1)", async () => { |
|
246
|
const step1 = createStep({ name: "step1" }, async () => { |
|
247
|
return new StepResponse("step1") |
|
248
|
}) |
|
249
|
const step2 = createStep({ name: "step2" }, async () => { |
|
250
|
await setTimeout(1000) |
|
251
|
return new StepResponse("step2") |
|
252
|
}) |
|
253
|
|
|
254
|
const workflowId = |
|
255
|
"workflow" + Math.random().toString(36).substring(2, 15) |
|
256
|
createWorkflow(workflowId, function (input) { |
|
257
|
step1() |
|
258
|
step2().config({ |
|
259
|
async: true, |
|
260
|
}) |
|
261
|
return new WorkflowResponse("workflow") |
|
262
|
}) |
|
263
|
|
|
264
|
const step1_1 = createStep({ name: "step1_1" }, async () => { |
|
265
|
return new StepResponse("step1_1") |
|
266
|
}) |
|
267
|
const step2_1 = createStep({ name: "step2_1" }, async () => { |
|
268
|
await setTimeout(1000) |
|
269
|
return new StepResponse("step2_1") |
|
270
|
}) |
|
271
|
|
|
272
|
const workflow2Id = |
|
273
|
"workflow_2" + Math.random().toString(36).substring(2, 15) |
|
274
|
createWorkflow(workflow2Id, function (input) { |
|
275
|
step1_1() |
|
276
|
step2_1().config({ |
|
277
|
async: true, |
|
278
|
}) |
|
279
|
return new WorkflowResponse("workflow_2") |
|
280
|
}) |
|
281
|
|
|
282
|
const transactionId = |
|
283
|
"trx_123" + Math.random().toString(36).substring(2, 15) |
|
284
|
const transactionId2 = |
|
285
|
"trx_124" + Math.random().toString(36).substring(2, 15) |
|
286
|
|
|
287
|
const onWorkflowFinishSpy = jest.fn() |
|
288
|
|
|
289
|
const onWorkflowFinishPromise = new Promise<void>((resolve) => { |
|
290
|
void workflowOrcModule.subscribe({ |
|
291
|
workflowId: workflowId, |
|
292
|
transactionId, |
|
293
|
subscriber: (event) => { |
|
294
|
console.log("event", event) |
|
295
|
if (event.eventType === "onFinish") { |
|
296
|
onWorkflowFinishSpy() |
|
297
|
workflowOrcModule.run(workflow2Id, { |
|
298
|
transactionId: transactionId2, |
|
299
|
}) |
|
300
|
resolve() |
|
301
|
} |
|
302
|
}, |
|
303
|
}) |
|
304
|
}) |
|
305
|
|
|
306
|
const onWorkflow2FinishSpy = jest.fn() |
|
307
|
|
|
308
|
const workflow2FinishPromise = new Promise<void>((resolve) => { |
|
309
|
void workflowOrcModule.subscribe({ |
|
310
|
workflowId: workflow2Id, |
|
311
|
subscriber: (event) => { |
|
312
|
console.log("event", event) |
|
313
|
if (event.eventType === "onFinish") { |
|
314
|
onWorkflow2FinishSpy() |
|
315
|
resolve() |
|
316
|
} |
|
317
|
}, |
|
318
|
}) |
|
319
|
}) |
|
320
|
|
|
321
|
workflowOrcModule.run(workflowId, { |
|
322
|
transactionId, |
|
323
|
}) |
|
324
|
|
|
325
|
await onWorkflowFinishPromise |
|
326
|
await workflow2FinishPromise |
|
327
|
|
|
328
|
expect(onWorkflowFinishSpy).toHaveBeenCalledTimes(1) |
|
329
|
expect(onWorkflow2FinishSpy).toHaveBeenCalledTimes(1) |
|
330
|
}) |
|
331
|
}) |
|
332
|
}, |
|
333
|
}) |