package engine import "testing" // TestQuerySource_IsForeground 测试前台/后台判断 func TestQuerySource_IsForeground(t *testing.T) { tests := []struct { source QuerySource want bool }{ {SourceMainThread, true}, {SourceSubAgent, true}, {SourceCompact, true}, {SourceSummary, false}, {SourceDream, false}, {SourceClassifier, false}, {SourceBackground, false}, } for _, tt := range tests { got := tt.source.IsForeground() if got != tt.want { t.Errorf("QuerySource(%q).IsForeground() = %v, want %v", tt.source, got, tt.want) } } } // TestShouldRetryOverloaded 测试 529 重试策略 func TestShouldRetryOverloaded(t *testing.T) { // 前台请求应该重试 529 if !ShouldRetryOverloaded(SourceMainThread) { t.Error("SourceMainThread should retry on 529") } if !ShouldRetryOverloaded(SourceSubAgent) { t.Error("SourceSubAgent should retry on 529") } if !ShouldRetryOverloaded(SourceCompact) { t.Error("SourceCompact should retry on 529") } // 后台请求不应该重试 529 if ShouldRetryOverloaded(SourceSummary) { t.Error("SourceSummary should NOT retry on 529") } if ShouldRetryOverloaded(SourceDream) { t.Error("SourceDream should NOT retry on 529") } if ShouldRetryOverloaded(SourceBackground) { t.Error("SourceBackground should NOT retry on 529") } } // TestIsRetryableErrorForSource 测试带来源的重试判断 func TestIsRetryableErrorForSource(t *testing.T) { // 429 对任何来源都重试 if !isRetryableErrorForSource("api: HTTP 429: rate limited", SourceMainThread) { t.Error("429 should be retryable for MainThread") } if !isRetryableErrorForSource("api: HTTP 429: rate limited", SourceBackground) { t.Error("429 should be retryable for Background") } // 529 只对前台来源重试 if !isRetryableErrorForSource("api: HTTP 529: overloaded", SourceMainThread) { t.Error("529 should be retryable for MainThread") } if isRetryableErrorForSource("api: HTTP 529: overloaded", SourceBackground) { t.Error("529 should NOT be retryable for Background") } if isRetryableErrorForSource("api: HTTP 529: overloaded", SourceSummary) { t.Error("529 should NOT be retryable for Summary") } // 其他错误不重试 if isRetryableErrorForSource("api: HTTP 500: internal error", SourceMainThread) { t.Error("500 should NOT be retryable") } } // TestIsOverloadedError 测试 529 错误检测 func TestIsOverloadedError(t *testing.T) { tests := []struct { errStr string want bool }{ {"api: HTTP 529: overloaded", true}, {"HTTP 529", true}, {"api: HTTP 429: rate limited", false}, {"api: HTTP 500: internal error", false}, {"", false}, {"short", false}, } for _, tt := range tests { got := isOverloadedError(tt.errStr) if got != tt.want { t.Errorf("isOverloadedError(%q) = %v, want %v", tt.errStr, got, tt.want) } } } // TestQuerySource_String 测试字符串表示 func TestQuerySource_String(t *testing.T) { if SourceMainThread.String() != "main_thread" { t.Error("SourceMainThread.String() should be 'main_thread'") } if SourceBackground.String() != "background" { t.Error("SourceBackground.String() should be 'background'") } }