1
+ import { serve } from "@upstash/workflow/nextjs" ;
2
+ import { BASE_URL , CI_RANDOM_ID_HEADER , CI_ROUTE_HEADER , TEST_ROUTE_PREFIX } from "app/ci/constants" ;
3
+ import { testServe , expect } from "app/ci/utils" ;
4
+ import { FailureFunctionPayload , WorkflowContext } from "@upstash/workflow" ;
5
+ import { saveResult } from "app/ci/upstash/redis" ;
6
+
7
+ const header = `test-header-foo`
8
+ const headerValue = `header-bar`
9
+ const authentication = `Bearer test-auth-super-secret`
10
+ const payload = "my-payload"
11
+
12
+ const thirdPartyEndpoint = `${ TEST_ROUTE_PREFIX } /auth/custom/target`
13
+
14
+ const makeCall = async (
15
+ context : WorkflowContext ,
16
+ stepName : string ,
17
+ method : "GET" | "POST" ,
18
+ expectedStatus : number ,
19
+ expectedBody : unknown
20
+ ) => {
21
+ const randomId = context . headers . get ( CI_RANDOM_ID_HEADER )
22
+ const route = context . headers . get ( CI_ROUTE_HEADER )
23
+
24
+ if ( ! randomId || ! route ) {
25
+ throw new Error ( "randomId or route not found" )
26
+ }
27
+
28
+ const { status, body } = await context . call < FailureFunctionPayload > ( stepName , {
29
+ url : thirdPartyEndpoint ,
30
+ body :
31
+ {
32
+ status : 200 ,
33
+ header : "" ,
34
+ body : "" ,
35
+ url : "" ,
36
+ sourceHeader : {
37
+ [ CI_ROUTE_HEADER ] : [ route ] ,
38
+ [ CI_RANDOM_ID_HEADER ] : [ randomId ]
39
+ } ,
40
+ sourceBody : "" ,
41
+ workflowRunId : "" ,
42
+ sourceMessageId : "" ,
43
+ } ,
44
+ method,
45
+ headers : {
46
+ [ CI_RANDOM_ID_HEADER ] : randomId ,
47
+ [ CI_ROUTE_HEADER ] : route ,
48
+ "Upstash-Workflow-Is-Failure" : "true"
49
+ }
50
+ } )
51
+
52
+ expect ( status , expectedStatus )
53
+
54
+ expect ( typeof body , typeof expectedBody )
55
+ expect ( JSON . stringify ( body ) , JSON . stringify ( expectedBody ) )
56
+ }
57
+
58
+ export const { POST , GET } = testServe (
59
+ serve < string > (
60
+ async ( context ) => {
61
+
62
+ expect ( context . headers . get ( header ) ! , headerValue )
63
+
64
+ await makeCall (
65
+ context ,
66
+ "regular call should fail" ,
67
+ "POST" ,
68
+ 500 ,
69
+ {
70
+ error : "WorkflowError" ,
71
+ message : "Not authorized to run the failure function."
72
+ }
73
+ )
74
+
75
+ const input = context . requestPayload ;
76
+ expect ( input , payload ) ;
77
+
78
+ await saveResult (
79
+ context ,
80
+ "not authorized for failure"
81
+ )
82
+ } , {
83
+ baseUrl : BASE_URL ,
84
+ retries : 0 ,
85
+ }
86
+ ) , {
87
+ expectedCallCount : 4 ,
88
+ expectedResult : "not authorized for failure" ,
89
+ payload,
90
+ headers : {
91
+ [ header ] : headerValue ,
92
+ "authentication" : authentication
93
+ }
94
+ }
95
+ )
0 commit comments