@@ -90,3 +90,103 @@ describe("envvars.update outside a task context", () => {
9090 expect ( requests ) . toHaveLength ( 0 ) ;
9191 } ) ;
9292} ) ;
93+
94+ describe ( "envvars.update inside a task context" , ( ) => {
95+ let server : Server | undefined ;
96+ let requests : { method ?: string ; url ?: string ; body : string } [ ] ;
97+ let previousApiUrl : string | undefined ;
98+ let previousSecretKey : string | undefined ;
99+ let previousAccessToken : string | undefined ;
100+
101+ beforeEach ( async ( ) => {
102+ requests = [ ] ;
103+ previousApiUrl = process . env . TRIGGER_API_URL ;
104+ previousSecretKey = process . env . TRIGGER_SECRET_KEY ;
105+ previousAccessToken = process . env . TRIGGER_ACCESS_TOKEN ;
106+
107+ delete process . env . TRIGGER_SECRET_KEY ;
108+
109+ server = createServer ( ( req , res ) => {
110+ let body = "" ;
111+ req . on ( "data" , ( chunk ) => {
112+ body += chunk ;
113+ } ) ;
114+ req . on ( "end" , ( ) => {
115+ requests . push ( { method : req . method , url : req . url , body } ) ;
116+ res . writeHead ( 200 , { "content-type" : "application/json" } ) ;
117+ res . end ( JSON . stringify ( { success : true } ) ) ;
118+ } ) ;
119+ } ) ;
120+
121+ await new Promise < void > ( ( resolve ) => server ! . listen ( 0 , "127.0.0.1" , resolve ) ) ;
122+
123+ const { port } = server ! . address ( ) as AddressInfo ;
124+ process . env . TRIGGER_API_URL = `http://127.0.0.1:${ port } ` ;
125+ process . env . TRIGGER_ACCESS_TOKEN = "tr_test_token" ;
126+
127+ taskContext . setGlobalLocation ( {
128+ ctx : {
129+ project : { id : "proj_ctx_id" , ref : "proj_ctx_ref" , name : "Project Ctx" } ,
130+ environment : { id : "env_ctx_id" , slug : "dev" , type : "DEVELOPMENT" } ,
131+ organization : { id : "org_ctx_id" , slug : "org_ctx" , title : "Org Ctx" } ,
132+ run : { id : "run_ctx_id" , isTest : false } ,
133+ task : { id : "task_ctx_id" , filePath : "task.ts" , exportName : "task" } ,
134+
135+ } ,
136+ } ) ;
137+ } ) ;
138+
139+ afterEach ( async ( ) => {
140+ taskContext . clear ( ) ;
141+
142+ if ( previousApiUrl === undefined ) {
143+ delete process . env . TRIGGER_API_URL ;
144+ } else {
145+ process . env . TRIGGER_API_URL = previousApiUrl ;
146+ }
147+
148+ if ( previousSecretKey === undefined ) {
149+ delete process . env . TRIGGER_SECRET_KEY ;
150+ } else {
151+ process . env . TRIGGER_SECRET_KEY = previousSecretKey ;
152+ }
153+
154+ if ( previousAccessToken === undefined ) {
155+ delete process . env . TRIGGER_ACCESS_TOKEN ;
156+ } else {
157+ process . env . TRIGGER_ACCESS_TOKEN = previousAccessToken ;
158+ }
159+
160+ const running = server ;
161+ server = undefined ;
162+
163+ if ( running ) {
164+ await new Promise < void > ( ( resolve , reject ) =>
165+ running . close ( ( error ) => ( error ? reject ( error ) : resolve ( ) ) )
166+ ) ;
167+ }
168+ } ) ;
169+
170+ it ( "correctly parses explicit projectRef, slug, name parameters when taskContext exists" , async ( ) => {
171+ await expect ( update ( "proj_explicit" , "staging" , "MY_VAR" , { value : "hello" } ) ) . resolves . toEqual ( {
172+ success : true ,
173+ } ) ;
174+
175+ expect ( requests ) . toHaveLength ( 1 ) ;
176+ expect ( requests [ 0 ] ?. method ) . toBe ( "PUT" ) ;
177+ expect ( requests [ 0 ] ?. url ) . toBe ( "/api/v1/projects/proj_explicit/envvars/staging/MY_VAR" ) ;
178+ expect ( JSON . parse ( requests [ 0 ] ?. body ?? "" ) ) . toEqual ( { value : "hello" } ) ;
179+ } ) ;
180+
181+ it ( "correctly uses taskContext defaults when only name and params are provided" , async ( ) => {
182+ await expect ( update ( "MY_VAR" , { value : "hello_context" } ) ) . resolves . toEqual ( {
183+ success : true ,
184+ } ) ;
185+
186+ expect ( requests ) . toHaveLength ( 1 ) ;
187+ expect ( requests [ 0 ] ?. method ) . toBe ( "PUT" ) ;
188+ expect ( requests [ 0 ] ?. url ) . toBe ( "/api/v1/projects/proj_ctx_ref/envvars/dev/MY_VAR" ) ;
189+ expect ( JSON . parse ( requests [ 0 ] ?. body ?? "" ) ) . toEqual ( { value : "hello_context" } ) ;
190+ } ) ;
191+ } ) ;
192+
0 commit comments