This commit is contained in:
lpf
2026-02-17 17:34:25 +08:00
parent 607b059066
commit 8586f66932
3 changed files with 83 additions and 2 deletions

View File

@@ -62,6 +62,35 @@ func TestCallLLMWithModelFallback_RetriesOnUnknownProvider(t *testing.T) {
}
}
func TestCallLLMWithModelFallback_RetriesOnGateway502(t *testing.T) {
p := &fallbackTestProvider{
byModel: map[string]fallbackResult{
"gemini-3-flash": {err: fmt.Errorf("API error (status 502, content-type \"text/html\"): <html>bad gateway</html>")},
"gpt-4o-mini": {resp: &providers.LLMResponse{Content: "ok"}},
},
}
al := &AgentLoop{
provider: p,
model: "gemini-3-flash",
modelFallbacks: []string{"gpt-4o-mini"},
}
resp, err := al.callLLMWithModelFallback(context.Background(), nil, nil, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp == nil || resp.Content != "ok" {
t.Fatalf("unexpected response: %+v", resp)
}
if len(p.called) != 2 {
t.Fatalf("expected 2 model attempts, got %d (%v)", len(p.called), p.called)
}
if p.called[0] != "gemini-3-flash" || p.called[1] != "gpt-4o-mini" {
t.Fatalf("unexpected model order: %v", p.called)
}
}
func TestCallLLMWithModelFallback_NoRetryOnNonRetryableError(t *testing.T) {
p := &fallbackTestProvider{
byModel: map[string]fallbackResult{
@@ -90,3 +119,10 @@ func TestShouldRetryWithFallbackModel_UnknownProviderError(t *testing.T) {
t.Fatalf("expected unknown provider error to trigger fallback retry")
}
}
func TestShouldRetryWithFallbackModel_HTMLUnmarshalError(t *testing.T) {
err := fmt.Errorf("failed to unmarshal response: invalid character '<' looking for beginning of value")
if !shouldRetryWithFallbackModel(err) {
t.Fatalf("expected HTML parse error to trigger fallback retry")
}
}