@@ -111,6 +111,112 @@ describe("e2e: named config", () => {
111111 } ) ;
112112 } ) ;
113113
114+ test . each ( [ "config show" , "auth status" ] ) (
115+ "前置和后置 --config 为 %s 选择同一份非激活 Profile,且不修改配置" ,
116+ async ( commandPath ) => {
117+ await withTempConfigDir ( async ( configDir ) => {
118+ const originalConfig = {
119+ active_config : "default" ,
120+ api_key : "sk-default-review" ,
121+ base_url : "https://default.example.test" ,
122+ work : {
123+ api_key : "sk-work-review" ,
124+ base_url : "https://work.example.test" ,
125+ access_token : "console-work-review" ,
126+ console_site : "international" ,
127+ console_region : "ap-southeast-1" ,
128+ workspace_id : "work-space-review" ,
129+ output : "json" ,
130+ } ,
131+ } ;
132+ writeConfig ( configDir , originalConfig ) ;
133+ const env = {
134+ BAILIAN_CONFIG_DIR : configDir ,
135+ DASHSCOPE_API_KEY : "" ,
136+ DASHSCOPE_BASE_URL : "" ,
137+ DASHSCOPE_OUTPUT : "" ,
138+ ALIBABA_CLOUD_ACCESS_KEY_ID : "" ,
139+ ALIBABA_CLOUD_ACCESS_KEY_SECRET : "" ,
140+ } ;
141+ const leading = await runCli ( [ "--config" , "work" , ...commandPath . split ( " " ) ] , env ) ;
142+ const trailing = await runCli ( [ ...commandPath . split ( " " ) , "--config=work" ] , env ) ;
143+ expect ( leading . exitCode , leading . stderr ) . toBe ( 0 ) ;
144+ expect ( trailing . exitCode , trailing . stderr ) . toBe ( 0 ) ;
145+ const leadingData = parseStdoutJson < Record < string , unknown > > ( leading . stdout ) ;
146+ expect ( leadingData ) . toEqual ( parseStdoutJson ( trailing . stdout ) ) ;
147+ expect ( leadingData . config ) . toBe ( "work" ) ;
148+ if ( commandPath === "auth status" ) {
149+ expect ( leadingData ) . toMatchObject ( {
150+ authenticated : true ,
151+ api_key : { source : "config" , base_url : "https://work.example.test" } ,
152+ console : { source : "config" , site : "international" , region : "ap-southeast-1" } ,
153+ } ) ;
154+ } else {
155+ expect ( leadingData . base_url ) . toBe ( "https://work.example.test" ) ;
156+ expect ( leadingData . workspace_id ) . toBe ( "work-space-review" ) ;
157+ }
158+ expect ( JSON . parse ( readFileSync ( join ( configDir , "config.json" ) , "utf8" ) ) ) . toEqual (
159+ originalConfig ,
160+ ) ;
161+ } ) ;
162+ } ,
163+ ) ;
164+
165+ test . each ( [
166+ { language : "en-US" , expectedMessage : / b e f o r e t h e c o m m a n d p a t h / , expectedHint : / a f t e r / } ,
167+ { language : "zh-CN" , expectedMessage : / 命 令 路 径 之 前 / , expectedHint : / 之 后 / } ,
168+ ] ) (
169+ "前置非全局参数的 JSON 错误遵循选中 Profile 的语言 $language" ,
170+ async ( { language, expectedMessage, expectedHint } ) => {
171+ await withTempConfigDir ( async ( configDir ) => {
172+ writeConfig ( configDir , { language : "en-US" , work : { language } } ) ;
173+ const result = await runCli (
174+ [ "--config=work" , "--output=json" , "--prompt" , "hi" , "text" , "chat" ] ,
175+ { BAILIAN_CONFIG_DIR : configDir } ,
176+ ) ;
177+ expect ( result . exitCode ) . toBe ( 2 ) ;
178+ expect ( result . stdout ) . toBe ( "" ) ;
179+ const { error } = JSON . parse ( result . stderr ) as {
180+ error : { code : number ; message : string ; hint : string } ;
181+ } ;
182+ expect ( error . code ) . toBe ( 2 ) ;
183+ expect ( error . message ) . toMatch ( expectedMessage ) ;
184+ expect ( error . hint ) . toMatch ( expectedHint ) ;
185+ } ) ;
186+ } ,
187+ ) ;
188+
189+ test . each ( [
190+ { argv : [ "--config=work" , "auth" ] , help : / U s a g e : b l a u t h < c o m m a n d > / } ,
191+ { argv : [ "--config=work" ] , help : / U s a g e : b l < r e s o u r c e > < c o m m a n d > / } ,
192+ ] ) ( "合法前置参数保留相应帮助页: $argv" , async ( { argv, help } ) => {
193+ await withTempConfigDir ( async ( configDir ) => {
194+ writeConfig ( configDir , { work : { language : "en-US" } } ) ;
195+ const result = await runCli ( argv , { BAILIAN_CONFIG_DIR : configDir } ) ;
196+ expect ( result . exitCode , result . stderr ) . toBe ( 0 ) ;
197+ expect ( result . stderr ) . toMatch ( help ) ;
198+ expect ( result . stdout ) . toBe ( "" ) ;
199+ } ) ;
200+ } ) ;
201+
202+ test . each ( [
203+ [ "auth" , "--output=json" , "--timeout" ] ,
204+ [ "--output=json" , "--timeout" , "--help" ] ,
205+ [ "--config=work" , "--output=json" , "--timeout" , "--version" ] ,
206+ ] ) ( "中文帮助分支缺少参数值时返回结构化用法错误: %j" , async ( ...argv ) => {
207+ await withTempConfigDir ( async ( configDir ) => {
208+ writeConfig ( configDir , { language : "zh-CN" , work : { language : "zh-CN" } } ) ;
209+ const result = await runCli ( argv , {
210+ BAILIAN_CONFIG_DIR : configDir ,
211+ } ) ;
212+ expect ( result . exitCode ) . toBe ( 2 ) ;
213+ expect ( result . stdout ) . toBe ( "" ) ;
214+ expect ( JSON . parse ( result . stderr ) ) . toMatchObject ( {
215+ error : { code : 2 , message : expect . stringMatching ( / - - t i m e o u t .* 需 要 .* 值 / ) } ,
216+ } ) ;
217+ } ) ;
218+ } ) ;
219+
114220 test ( "--config default 等价默认配置" , async ( ) => {
115221 await withTempConfigDir ( async ( dir ) => {
116222 writeConfig ( dir , {
0 commit comments