@@ -11,7 +11,7 @@ import {
11
11
import { nanoid } from "../utils" ;
12
12
import { WORKFLOW_INVOKE_COUNT_HEADER } from "../constants" ;
13
13
import { Telemetry } from "../types" ;
14
- import { invokeWorkflow } from "./serve-many" ;
14
+ import { getNewUrlFromWorkflowId , invokeWorkflow } from "./serve-many" ;
15
15
16
16
describe ( "serveMany" , ( ) => {
17
17
describe ( "invokeWorkflow" , ( ) => {
@@ -148,14 +148,36 @@ describe("serveMany", () => {
148
148
149
149
const workflowTwo = createWorkflow (
150
150
async ( context : WorkflowContext < string > ) => {
151
- await context . invoke ( "invoke step two" , {
151
+ const result = await context . invoke ( "invoke step two" , {
152
152
workflow : workflowOne ,
153
153
body : 2 ,
154
154
flowControl : {
155
155
key : "customFlowControl" ,
156
156
parallelism : 4 ,
157
157
} ,
158
158
} ) ;
159
+
160
+ const _body = result . body ;
161
+ const _isCanceled = result . isCanceled ;
162
+ const _isFailed = result . isFailed ;
163
+
164
+ console . log ( _body , _isCanceled , _isFailed ) ;
165
+
166
+ // just checking the type. code won't reach here.
167
+ const secondResult = await context . invoke ( "invoke step two" , {
168
+ workflow : workflowOne ,
169
+ body : 2 ,
170
+ flowControl : {
171
+ key : "customFlowControl" ,
172
+ parallelism : 4 ,
173
+ } ,
174
+ } ) ;
175
+
176
+ const _secondBody = secondResult . body ;
177
+ const _secondIsCanceled = secondResult . isCanceled ;
178
+ const _secondIsFailed = secondResult . isFailed ;
179
+
180
+ console . log ( _secondBody , _secondIsCanceled , _secondIsFailed ) ;
159
181
} ,
160
182
{
161
183
flowControl : {
@@ -166,10 +188,33 @@ describe("serveMany", () => {
166
188
}
167
189
) ;
168
190
191
+ const workflowThree = createWorkflow (
192
+ async ( context : WorkflowContext < string > ) => {
193
+ const result = await context . call ( "call other workflow" , {
194
+ workflow : workflowOne ,
195
+ body : 2 ,
196
+ } ) ;
197
+
198
+ const _body = result . body ;
199
+ const _header = result . header ;
200
+ const _status = result . status ;
201
+
202
+ console . log ( _body , _header , _status ) ;
203
+ } ,
204
+ {
205
+ flowControl : {
206
+ key : "workflowThreeFlowControl" ,
207
+ parallelism : 4 ,
208
+ ratePerSecond : 6 ,
209
+ } ,
210
+ }
211
+ ) ;
212
+
169
213
const { POST : handler } = serveMany (
170
214
{
171
215
"workflow-one" : workflowOne ,
172
216
"workflow-two" : workflowTwo ,
217
+ "workflow-three" : workflowThree ,
173
218
} ,
174
219
{
175
220
qstashClient,
@@ -187,7 +232,8 @@ describe("serveMany", () => {
187
232
188
233
await mockQStashServer ( {
189
234
execute : async ( ) => {
190
- await handler ( request ) ;
235
+ const response = await handler ( request ) ;
236
+ expect ( response . status ) . toBe ( 200 ) ;
191
237
} ,
192
238
responseFields : { body : "msgId" , status : 200 } ,
193
239
receivesRequest : {
@@ -243,7 +289,8 @@ describe("serveMany", () => {
243
289
244
290
await mockQStashServer ( {
245
291
execute : async ( ) => {
246
- await handler ( request ) ;
292
+ const response = await handler ( request ) ;
293
+ expect ( response . status ) . toBe ( 200 ) ;
247
294
} ,
248
295
responseFields : { body : "msgId" , status : 200 } ,
249
296
receivesRequest : {
@@ -287,5 +334,93 @@ describe("serveMany", () => {
287
334
} ,
288
335
} ) ;
289
336
} ) ;
337
+
338
+ test ( "should make context.call request with workflow" , async ( ) => {
339
+ const request = getRequest (
340
+ `${ WORKFLOW_ENDPOINT } /workflow-three` ,
341
+ "wfr_id" ,
342
+ "initial-payload" ,
343
+ [ ]
344
+ ) ;
345
+ request . headers . set ( WORKFLOW_INVOKE_COUNT_HEADER , "1" ) ;
346
+
347
+ await mockQStashServer ( {
348
+ execute : async ( ) => {
349
+ const response = await handler ( request ) ;
350
+ expect ( response . status ) . toBe ( 200 ) ;
351
+ } ,
352
+ responseFields : { body : "msgId" , status : 200 } ,
353
+ receivesRequest : {
354
+ method : "POST" ,
355
+ url : `${ MOCK_QSTASH_SERVER_URL } /v2/batch` ,
356
+ token,
357
+ body : [
358
+ {
359
+ body : "2" ,
360
+ destination : "https://requestcatcher.com/api/workflow-one" ,
361
+ headers : {
362
+ "content-type" : "application/json" ,
363
+ "upstash-callback" : "https://requestcatcher.com/api/workflow-three" ,
364
+ "upstash-callback-feature-set" : "LazyFetch,InitialBody" ,
365
+ "upstash-callback-flow-control-key" : "workflowThreeFlowControl" ,
366
+ "upstash-callback-flow-control-value" : "parallelism=4, rate=6" ,
367
+ "upstash-flow-control-key" : "workflowOneFlowControl" ,
368
+ "upstash-flow-control-value" : "parallelism=2, rate=10" ,
369
+ "upstash-callback-forward-upstash-workflow-callback" : "true" ,
370
+ "upstash-callback-forward-upstash-workflow-concurrent" : "1" ,
371
+ "upstash-callback-forward-upstash-workflow-contenttype" : "application/json" ,
372
+ "upstash-callback-forward-upstash-workflow-invoke-count" : "1" ,
373
+ "upstash-callback-forward-upstash-workflow-stepid" : "1" ,
374
+ "upstash-callback-forward-upstash-workflow-stepname" : "call other workflow" ,
375
+ "upstash-callback-forward-upstash-workflow-steptype" : "Call" ,
376
+ "upstash-callback-retries" : "3" ,
377
+ "upstash-callback-workflow-calltype" : "fromCallback" ,
378
+ "upstash-callback-workflow-init" : "false" ,
379
+ "upstash-callback-workflow-runid" : "wfr_id" ,
380
+ "upstash-callback-workflow-url" : "https://requestcatcher.com/api/workflow-three" ,
381
+ "upstash-failure-callback-retries" : "3" ,
382
+ "upstash-feature-set" : "WF_NoDelete,InitialBody" ,
383
+ "upstash-method" : "POST" ,
384
+ "upstash-retries" : "0" ,
385
+ "upstash-telemetry-framework" : "nextjs" ,
386
+ "upstash-telemetry-runtime" :
"[email protected] " ,
387
+ "upstash-telemetry-sdk" :
"@upstash/[email protected] " ,
388
+ "upstash-workflow-calltype" : "toCallback" ,
389
+ "upstash-workflow-init" : "false" ,
390
+ "upstash-workflow-runid" : "wfr_id" ,
391
+ "upstash-workflow-sdk-version" : "1" ,
392
+ "upstash-workflow-url" : "https://requestcatcher.com/api/workflow-three" ,
393
+ } ,
394
+ } ,
395
+ ] ,
396
+ } ,
397
+ } ) ;
398
+ } ) ;
399
+ } ) ;
400
+
401
+ describe ( "getNewUrlFromWorkflowId" , ( ) => {
402
+ test ( "should return new url" , ( ) => {
403
+ const url = "https://requestcatcher.com/api/original_workflow" ;
404
+ const workflowId = "workflowId" ;
405
+ const newUrl = getNewUrlFromWorkflowId ( url , workflowId ) ;
406
+
407
+ expect ( newUrl ) . toBe ( "https://requestcatcher.com/api/workflowId" ) ;
408
+ } ) ;
409
+
410
+ test ( "should ignore query parameters" , ( ) => {
411
+ const url = "https://requestcatcher.com/api/original_workflow?query=param" ;
412
+ const workflowId = "workflowId" ;
413
+ const newUrl = getNewUrlFromWorkflowId ( url , workflowId ) ;
414
+
415
+ expect ( newUrl ) . toBe ( "https://requestcatcher.com/api/workflowId" ) ;
416
+ } ) ;
417
+
418
+ test ( "shuold ignore hash parameters" , ( ) => {
419
+ const url = "https://requestcatcher.com/api/original_workflow#hash" ;
420
+ const workflowId = "workflowId" ;
421
+ const newUrl = getNewUrlFromWorkflowId ( url , workflowId ) ;
422
+
423
+ expect ( newUrl ) . toBe ( "https://requestcatcher.com/api/workflowId" ) ;
424
+ } ) ;
290
425
} ) ;
291
426
} ) ;
0 commit comments