@@ -256,6 +256,7 @@ try {
256256 "Invalid arguments. Run 'firstdraft compilation status --help' for usage." ,
257257 } ) ;
258258
259+ exercisePackedEnvironmentSelection ( temporaryDirectory ) ;
259260 await exercisePackedCompilation ( projectDirectory ) ;
260261} finally {
261262 rmSync ( temporaryDirectory , { recursive : true , force : true } ) ;
@@ -291,12 +292,19 @@ function spawnNpm(arguments_, cwd = process.cwd()) {
291292/**
292293 * @param {string[] } arguments_
293294 * @param {string } [cwd]
295+ * @param {Record<string, string> } [environment]
294296 */
295- function spawnPackedCli ( arguments_ , cwd = process . cwd ( ) ) {
297+ function spawnPackedCli ( arguments_ , cwd = process . cwd ( ) , environment = { } ) {
296298 return spawnSync ( process . execPath , [ packedExecutable , ...arguments_ ] , {
297299 cwd,
298300 encoding : "utf8" ,
299- env : { ...process . env , FIRSTDRAFT_API_TOKEN : apiToken } ,
301+ env : {
302+ ...process . env ,
303+ FIRSTDRAFT_API_URL : undefined ,
304+ FIRSTDRAFT_API_TOKEN : apiToken ,
305+ FIRSTDRAFT_STAGING_API_TOKEN : "" ,
306+ ...environment ,
307+ } ,
300308 } ) ;
301309}
302310
@@ -307,7 +315,12 @@ function spawnPackedCli(arguments_, cwd = process.cwd()) {
307315async function spawnPackedCliAsync ( arguments_ , cwd ) {
308316 const child = spawn ( process . execPath , [ packedExecutable , ...arguments_ ] , {
309317 cwd,
310- env : { ...process . env , FIRSTDRAFT_API_TOKEN : apiToken } ,
318+ env : {
319+ ...process . env ,
320+ FIRSTDRAFT_API_URL : undefined ,
321+ FIRSTDRAFT_API_TOKEN : apiToken ,
322+ FIRSTDRAFT_STAGING_API_TOKEN : "" ,
323+ } ,
311324 stdio : [ "ignore" , "pipe" , "pipe" ] ,
312325 } ) ;
313326 let stdout = "" ;
@@ -325,6 +338,41 @@ async function spawnPackedCliAsync(arguments_, cwd) {
325338 return { status, stdout, stderr } ;
326339}
327340
341+ /** @param {string } temporaryDirectory */
342+ function exercisePackedEnvironmentSelection ( temporaryDirectory ) {
343+ const cwd = path . join ( temporaryDirectory , "staging-project" ) ;
344+ mkdirSync ( cwd ) ;
345+ const initialized = spawnPackedCli (
346+ [ "plan" , "init" , "--name" , "Staging Project" ] ,
347+ cwd ,
348+ ) ;
349+ assert . equal ( initialized . status , 0 ) ;
350+
351+ const missing = spawnPackedCli ( [ "--staging" , "plan" , "push" ] , cwd ) ;
352+ assert . equal ( missing . status , 1 ) ;
353+ assert . equal ( JSON . parse ( missing . stderr ) . error , "authentication_required" ) ;
354+ assert . match ( missing . stderr , / F I R S T D R A F T _ S T A G I N G _ A P I _ T O K E N / ) ;
355+
356+ const conflict = spawnPackedCli ( [ "plan" , "push" , "--staging" ] , cwd , {
357+ FIRSTDRAFT_API_URL : "http://127.0.0.1:1" ,
358+ FIRSTDRAFT_STAGING_API_TOKEN : "canary-staging-token" ,
359+ } ) ;
360+ assert . equal ( conflict . status , 2 ) ;
361+ assert . equal ( JSON . parse ( conflict . stderr ) . error , "invalid_configuration" ) ;
362+ assert . match ( conflict . stderr , / - - s t a g i n g c o n f l i c t s w i t h F I R S T D R A F T _ A P I _ U R L / ) ;
363+ assert . doesNotMatch ( conflict . stderr , / c a n a r y / ) ;
364+
365+ const statePath = path . join ( cwd , ".firstdraft" , "state.json" ) ;
366+ const state = JSON . parse ( readFileSync ( statePath , "utf8" ) ) ;
367+ state . api_url = "https://staging.firstdraft.com" ;
368+ state . foundation_plan_etag = '"retained-staging-head"' ;
369+ writeFileSync ( statePath , `${ JSON . stringify ( state ) } \n` ) ;
370+ const retained = spawnPackedCli ( [ "plan" , "status" ] , cwd ) ;
371+ assert . equal ( retained . status , 1 ) ;
372+ assert . equal ( JSON . parse ( retained . stderr ) . error , "authentication_required" ) ;
373+ assert . match ( retained . stderr , / F I R S T D R A F T _ S T A G I N G _ A P I _ T O K E N / ) ;
374+ }
375+
328376/** @param {string } projectDirectory */
329377async function exercisePackedCompilation ( projectDirectory ) {
330378 const projectId = "01900000-0000-7000-8000-000000000901" ;
0 commit comments