1- import { mkdtemp , rm } from "node:fs/promises" ;
1+ import { mkdtemp , rm , writeFile } from "node:fs/promises" ;
22import { tmpdir } from "node:os" ;
33import { join } from "node:path" ;
44import { fileURLToPath } from "node:url" ;
@@ -9,7 +9,19 @@ const fixtureRoot = join(fileURLToPath(import.meta.url), "..", "..", "fixtures",
99let configDir : string ;
1010
1111function env ( ) : NodeJS . ProcessEnv {
12- return { BAILIAN_CONFIG_DIR : configDir , DO_NOT_TRACK : "1" } ;
12+ return {
13+ BAILIAN_CONFIG_DIR : configDir ,
14+ DO_NOT_TRACK : "1" ,
15+ DASHSCOPE_API_KEY : "" ,
16+ DASHSCOPE_BASE_URL : "" ,
17+ } ;
18+ }
19+
20+ function parseStderrJsonDiagnostics ( stderr : string ) : unknown [ ] {
21+ return stderr
22+ . trim ( )
23+ . split ( / \n \s * \n / )
24+ . map ( ( diagnostic ) => JSON . parse ( diagnostic ) as unknown ) ;
1325}
1426
1527describe ( "e2e: Command Pack" , ( ) => {
@@ -71,6 +83,68 @@ describe("e2e: Command Pack", () => {
7183 expect ( executed . exitCode , executed . stderr ) . toBe ( 0 ) ;
7284 expect ( executed . stdout ) . toContain ( "command-pack:hello" ) ;
7385
86+ await writeFile (
87+ join ( configDir , "config.json" ) ,
88+ JSON . stringify (
89+ {
90+ base_url : "https://default.example.com" ,
91+ active_config : "company-plan" ,
92+ "company-plan" : {
93+ api_key : "sk-plan" ,
94+ base_url : "https://plan.example.com" ,
95+ api_key_capabilities : [ ] ,
96+ } ,
97+ } ,
98+ null ,
99+ 2 ,
100+ ) + "\n" ,
101+ ) ;
102+ const fallbackFailure = await runCli ( [ "agent" , "credential" , "--output" , "json" ] , env ( ) ) ;
103+ expect ( fallbackFailure . exitCode ) . toBe ( 3 ) ;
104+ expect ( fallbackFailure . stderr ) . toMatch ( / ^ \{ \n { 2 } " w a r n i n g " : \{ / ) ;
105+ expect ( fallbackFailure . stderr ) . toContain ( "\n\n{\n" ) ;
106+ expect ( parseStderrJsonDiagnostics ( fallbackFailure . stderr ) ) . toEqual ( [
107+ {
108+ warning : {
109+ code : "PROFILE_API_KEY_FALLBACK" ,
110+ message :
111+ 'Profile "company-plan" does not support command "agent credential"; API Key settings will be read from Profile "default" for this run.' ,
112+ profile : "company-plan" ,
113+ command_path : [ "agent" , "credential" ] ,
114+ fallback_profile : "default" ,
115+ credential_fields : [ "api_key" , "base_url" ] ,
116+ } ,
117+ } ,
118+ {
119+ error : expect . objectContaining ( {
120+ code : 3 ,
121+ message : "No API key found." ,
122+ } ) ,
123+ } ,
124+ ] ) ;
125+
126+ await writeFile (
127+ join ( configDir , "config.json" ) ,
128+ JSON . stringify (
129+ {
130+ api_key : "sk-default" ,
131+ base_url : "https://default.example.com" ,
132+ active_config : "company-plan" ,
133+ "company-plan" : {
134+ api_key : "sk-plan" ,
135+ base_url : "https://plan.example.com" ,
136+ api_key_capabilities : [ ] ,
137+ } ,
138+ } ,
139+ null ,
140+ 2 ,
141+ ) + "\n" ,
142+ ) ;
143+ const fallbackCredential = await runCli ( [ "agent" , "credential" ] , env ( ) ) ;
144+ expect ( fallbackCredential . exitCode , fallbackCredential . stderr ) . toBe ( 0 ) ;
145+ expect ( fallbackCredential . stderr ) . toContain ( 'command "agent credential"' ) ;
146+ expect ( fallbackCredential . stdout ) . toContain ( "credential-base-url:https://default.example.com" ) ;
147+
74148 const credential = await runCli ( [ "agent" , "credential" , "--api-key" , "fixture-key" ] , env ( ) ) ;
75149 expect ( credential . exitCode , credential . stderr ) . toBe ( 0 ) ;
76150 expect ( credential . stdout ) . toContain ( "credential-source:flag" ) ;
0 commit comments