@@ -155,13 +155,31 @@ fn chat_json_without_key_is_auth_error() {
155155 timeout_secs : 5 ,
156156 max_retries : 0 ,
157157 max_tokens : 20000 ,
158+ is_local : false ,
158159 } ) ;
159160 match client. chat_json ( "sys" , "usr" ) {
160161 Err ( AiClientError :: Auth ( _) ) => { }
161162 other => panic ! ( "期望 Auth 错误,实际 {:?}" , other. map( |_| ( ) ) ) ,
162163 }
163164}
164165
166+ #[ test]
167+ fn chat_json_without_key_skips_auth_when_local ( ) {
168+ // Why: Ollama 本地端点无需密钥——is_local=true 时空密钥不触发 Auth 拒绝
169+ let client = AiClient :: new ( AiClientConfig {
170+ base_url : "http://127.0.0.1:11434/v1" . to_string ( ) ,
171+ api_key : "" . to_string ( ) ,
172+ model : "llama3" . to_string ( ) ,
173+ timeout_secs : 5 ,
174+ max_retries : 0 ,
175+ max_tokens : 20000 ,
176+ is_local : true ,
177+ } ) ;
178+ // is_local 时空密钥应跳过 Auth 检查,实际会触发网络/传输错误(非 Auth)
179+ let result = client. chat_json ( "sys" , "usr" ) ;
180+ assert ! ( !matches!( result, Err ( AiClientError :: Auth ( _) ) ) , "is_local 不应返回 Auth 错误" ) ;
181+ }
182+
165183// ---- v0.11.6 M1:Provider 化 + 降级链(Task 3 纯函数层)----
166184
167185#[ test]
@@ -172,6 +190,18 @@ fn from_provider_builds_config_from_provider() {
172190 assert_eq ! ( client. config. base_url, p. base_url) ;
173191 assert_eq ! ( client. config. model, p. default_model) ;
174192 assert_eq ! ( client. config. api_key, "sk-p1" ) ;
193+ // Why: Ollama 预设 is_local=true;云端预设(SiliconFlow)is_local=false
194+ assert ! ( !client. config. is_local, "云端 Provider is_local=false" ) ;
195+ }
196+
197+ #[ test]
198+ fn from_provider_sets_is_local_for_ollama ( ) {
199+ // Why: Ollama 本地端点无需密钥——is_local=true 确保空密钥不触发 Auth 拒绝
200+ let ollama_preset = preset_templates ( ) . into_iter ( ) . find ( |p| p. kind == crate :: ai_provider:: ProviderKind :: Ollama )
201+ . expect ( "Ollama 预设存在" ) ;
202+ let client = AiClient :: from_provider ( & ollama_preset, None ) ;
203+ assert ! ( client. config. is_local, "Ollama Provider is_local=true" ) ;
204+ assert ! ( client. config. api_key. is_empty( ) , "Ollama 空密钥预期" ) ;
175205}
176206
177207#[ test]
0 commit comments