@@ -32,8 +32,13 @@ import { FULL_ENDPOINT, MINIMAL_V2_ENDPOINT, FULL_OPTIONS, FULL_TRIGGER } from "
32
32
import { onInit } from "../../../src/v2/core" ;
33
33
import { Handler } from "express" ;
34
34
35
- function request ( args : { data ?: any , auth ?: Record < string , string > , headers ?: Record < string , string > , method ?: MockRequest [ "method" ] } ) : any {
36
- let headers : Record < string , string > = { }
35
+ function request ( args : {
36
+ data ?: any ;
37
+ auth ?: Record < string , string > ;
38
+ headers ?: Record < string , string > ;
39
+ method ?: MockRequest [ "method" ] ;
40
+ } ) : any {
41
+ let headers : Record < string , string > = { } ;
37
42
if ( args . method !== "POST" ) {
38
43
headers [ "content-type" ] = "application/json" ;
39
44
}
@@ -191,7 +196,7 @@ describe("onRequest", () => {
191
196
res . send ( "Works" ) ;
192
197
} ) ;
193
198
194
- const req = request ( { headers : { origin : "example.com" } } ) ;
199
+ const req = request ( { headers : { origin : "example.com" } } ) ;
195
200
const resp = await runHandler ( func , req ) ;
196
201
expect ( resp . body ) . to . equal ( "Works" ) ;
197
202
} ) ;
@@ -208,9 +213,9 @@ describe("onRequest", () => {
208
213
origin : "example.com" ,
209
214
} ,
210
215
method : "OPTIONS" ,
211
- } )
216
+ } ) ;
212
217
213
- const resp = await runHandler ( func , req as any ) ;
218
+ const resp = await runHandler ( func , req ) ;
214
219
expect ( resp . status ) . to . equal ( 204 ) ;
215
220
expect ( resp . body ) . to . be . undefined ;
216
221
expect ( resp . headers ) . to . deep . equal ( {
@@ -237,7 +242,7 @@ describe("onRequest", () => {
237
242
method : "OPTIONS" ,
238
243
} ) ;
239
244
240
- const resp = await runHandler ( func , req as any ) ;
245
+ const resp = await runHandler ( func , req ) ;
241
246
expect ( resp . status ) . to . equal ( 204 ) ;
242
247
expect ( resp . body ) . to . be . undefined ;
243
248
expect ( resp . headers ) . to . deep . equal ( {
@@ -266,7 +271,7 @@ describe("onRequest", () => {
266
271
method : "OPTIONS" ,
267
272
} ) ;
268
273
269
- const resp = await runHandler ( func , req as any ) ;
274
+ const resp = await runHandler ( func , req ) ;
270
275
expect ( resp . status ) . to . equal ( 200 ) ;
271
276
expect ( resp . body ) . to . be . equal ( "Good" ) ;
272
277
expect ( resp . headers ) . to . deep . equal ( { } ) ;
@@ -289,7 +294,7 @@ describe("onRequest", () => {
289
294
let hello ;
290
295
onInit ( ( ) => ( hello = "world" ) ) ;
291
296
expect ( hello ) . to . be . undefined ;
292
- await runHandler ( func , req as any ) ;
297
+ await runHandler ( func , req ) ;
293
298
expect ( hello ) . to . equal ( "world" ) ;
294
299
} ) ;
295
300
} ) ;
@@ -404,9 +409,9 @@ describe("onCall", () => {
404
409
it ( "should be an express handler" , async ( ) => {
405
410
const func = https . onCall ( ( ) => 42 ) ;
406
411
407
- const req = request ( { headers : { origin : "example.com" } } ) ;
412
+ const req = request ( { headers : { origin : "example.com" } } ) ;
408
413
409
- const resp = await runHandler ( func , req as any ) ;
414
+ const resp = await runHandler ( func , req ) ;
410
415
expect ( resp . body ) . to . deep . equal ( JSON . stringify ( { result : 42 } ) ) ;
411
416
} ) ;
412
417
@@ -424,7 +429,7 @@ describe("onCall", () => {
424
429
method : "OPTIONS" ,
425
430
} ) ;
426
431
427
- const resp = await runHandler ( func , req as any ) ;
432
+ const resp = await runHandler ( func , req ) ;
428
433
expect ( resp . status ) . to . equal ( 204 ) ;
429
434
expect ( resp . body ) . to . be . undefined ;
430
435
expect ( resp . headers ) . to . deep . equal ( {
@@ -450,7 +455,7 @@ describe("onCall", () => {
450
455
method : "OPTIONS" ,
451
456
} ) ;
452
457
453
- const response = await runHandler ( func , req as any ) ;
458
+ const response = await runHandler ( func , req ) ;
454
459
455
460
expect ( response . status ) . to . equal ( 204 ) ;
456
461
expect ( response . body ) . to . be . undefined ;
@@ -466,7 +471,7 @@ describe("onCall", () => {
466
471
467
472
it ( "adds CORS headers" , async ( ) => {
468
473
const func = https . onCall ( ( ) => 42 ) ;
469
- const req = request ( { headers : { origin : "example.com" } } )
474
+ const req = request ( { headers : { origin : "example.com" } } ) ;
470
475
const response = await runHandler ( func , req ) ;
471
476
472
477
expect ( response . status ) . to . equal ( 200 ) ;
@@ -488,16 +493,15 @@ describe("onCall", () => {
488
493
it ( "calls init function" , async ( ) => {
489
494
const func = https . onCall ( ( ) => 42 ) ;
490
495
491
- const req = request ( { headers : { origin : "example.com" } } ) ;
496
+ const req = request ( { headers : { origin : "example.com" } } ) ;
492
497
let hello ;
493
498
onInit ( ( ) => ( hello = "world" ) ) ;
494
499
expect ( hello ) . to . be . undefined ;
495
- await runHandler ( func , req as any ) ;
500
+ await runHandler ( func , req ) ;
496
501
expect ( hello ) . to . equal ( "world" ) ;
497
502
} ) ;
498
503
499
504
describe ( "authPolicy" , ( ) => {
500
-
501
505
before ( ( ) => {
502
506
sinon . stub ( debug , "isDebugFeatureEnabled" ) . withArgs ( "skipTokenVerification" ) . returns ( true ) ;
503
507
} ) ;
@@ -514,7 +518,7 @@ describe("onCall", () => {
514
518
( ) => 42
515
519
) ;
516
520
517
- const authResp = await runHandler ( func , request ( { auth : { sub : "inlined" } } ) ) ;
521
+ const authResp = await runHandler ( func , request ( { auth : { sub : "inlined" } } ) ) ;
518
522
expect ( authResp . status ) . to . equal ( 200 ) ;
519
523
520
524
const anonResp = await runHandler ( func , request ( { } ) ) ;
@@ -543,7 +547,7 @@ describe("onCall", () => {
543
547
{ fn : specificValue , auth : { meaning : "42" } , status : 200 } ,
544
548
{ fn : specificValue , auth : { meaning : "43" } , status : 403 } ,
545
549
{ fn : specificValue , auth : { order : "66" } , status : 403 } ,
546
- { fn : specificValue , status : 403 } ,
550
+ { fn : specificValue , status : 403 } ,
547
551
] ;
548
552
for ( const test of cases ) {
549
553
const resp = await runHandler ( test . fn , request ( { auth : test . auth } ) ) ;
@@ -579,14 +583,13 @@ describe("onCallGenkit", () => {
579
583
flow . run . withArgs ( "answer" ) . returns ( 42 ) ;
580
584
flow . stream . onCall ( 0 ) . throws ( "Unexpected stream" ) ;
581
585
582
- const f = https . onCallGenkit ( flow ) ;
586
+ const f = https . onCallGenkit ( flow ) ;
583
587
584
588
const req = request ( { data : "answer" } ) ;
585
589
const res = await runHandler ( f , req ) ;
586
- expect ( JSON . parse ( res . body ) ) . to . deep . equal ( { " result" : 42 } ) ;
590
+ expect ( JSON . parse ( res . body ) ) . to . deep . equal ( { result : 42 } ) ;
587
591
} ) ;
588
592
589
-
590
593
it ( "Streams with SSE requests" , async ( ) => {
591
594
const flow = {
592
595
__action : {
@@ -597,8 +600,10 @@ describe("onCallGenkit", () => {
597
600
} ;
598
601
flow . run . onFirstCall ( ) . throws ( ) ;
599
602
flow . stream . withArgs ( "answer" ) . returns ( {
600
- stream : ( async function * ( ) {
603
+ stream : ( async function * ( ) {
604
+ await Promise . resolve ( ) ;
601
605
yield 1 ;
606
+ await Promise . resolve ( ) ;
602
607
yield 2 ;
603
608
} ) ( ) ,
604
609
output : Promise . resolve ( 42 ) ,
@@ -607,12 +612,13 @@ describe("onCallGenkit", () => {
607
612
608
613
const f = https . onCallGenkit ( flow ) ;
609
614
610
- const req = request ( { data : "answer" , headers : { accept : "text/event-stream" } } ) ;
615
+ const req = request ( { data : "answer" , headers : { accept : "text/event-stream" } } ) ;
611
616
const res = await runHandler ( f , req ) ;
612
- expect ( res . body ) . to . equal ( [ 'data: {"message":1}' , 'data: {"message":2}' , 'data: {"result":42}' , '' ] . join ( "\n" ) ) ;
617
+ expect ( res . body ) . to . equal (
618
+ [ 'data: {"message":1}' , 'data: {"message":2}' , 'data: {"result":42}' , "" ] . join ( "\n" )
619
+ ) ;
613
620
} ) ;
614
621
615
-
616
622
// TODO: Once genkit 1.0.0-rc.4 is released, add a type checking test that succeeds by compiling correctly.
617
623
// It should create a genkit resource and then call onCallGenkit with it. If the compiler fails, our typing
618
624
// needs to be updated.
0 commit comments