fix(api): 缺失输出能力配置时透传请求

This commit is contained in:
2026-07-23 11:07:31 +08:00
parent 8d86c4b3b3
commit 46cdb1a288
2 changed files with 123 additions and 42 deletions
@@ -72,32 +72,90 @@ func TestVolcesOutputProcessorPreservesExplicitLimits(t *testing.T) {
}
}
func TestVolcesOutputCandidateFilterSupportsFailover(t *testing.T) {
func TestVolcesOutputCandidateFilterPreservesMissingCapabilityOrder(t *testing.T) {
missing := volcTextCandidate(nil)
missing.PlatformID = "missing"
valid := volcTextCandidate(32768)
valid.PlatformID = "valid"
filtered, summary, err := filterRuntimeCandidatesByOutputTokens("chat.completions", "demo", "text_generate", map[string]any{"messages": []any{}}, []store.RuntimeModelCandidate{missing, valid})
if err != nil || len(filtered) != 1 || filtered[0].PlatformID != "valid" {
t.Fatalf("expected missing capability candidate to be skipped: filtered=%+v summary=%+v err=%v", filtered, summary, err)
if err != nil || len(filtered) != 2 || filtered[0].PlatformID != "missing" || filtered[1].PlatformID != "valid" {
t.Fatalf("expected missing capability candidate order to be preserved: filtered=%+v summary=%+v err=%v", filtered, summary, err)
}
result := preprocessRequestWithLog("chat.completions", map[string]any{"messages": []any{}}, filtered[0])
if summary["missingCapabilityCandidateCount"] != 1 || summary["passthroughMissingCapability"] != true {
t.Fatalf("expected missing capability audit summary, got %+v", summary)
}
missingResult := preprocessRequestWithLog("chat.completions", map[string]any{"messages": []any{}}, filtered[0])
if missingResult.Err != nil || len(missingResult.Log.Changes) != 0 {
t.Fatalf("missing capability candidate must pass through unchanged: %+v", missingResult)
}
if _, ok := missingResult.Body["max_tokens"]; ok {
t.Fatalf("missing capability candidate must not inject max_tokens: %+v", missingResult.Body)
}
result := preprocessRequestWithLog("chat.completions", map[string]any{"messages": []any{}}, filtered[1])
if result.Body["max_tokens"] != 10922 {
t.Fatalf("failover candidate must recalculate its own default, got %+v", result.Body)
t.Fatalf("configured candidate must calculate its own default, got %+v", result.Body)
}
}
func TestVolcesOutputCandidateFilterRejectsMissingAndExceededCapabilities(t *testing.T) {
_, _, err := filterRuntimeCandidatesByOutputTokens("responses", "demo", "text_generate", map[string]any{"input": "hello"}, []store.RuntimeModelCandidate{volcTextCandidate(nil)})
if store.ModelCandidateErrorCode(err) != volcesOutputCapabilityErrorCode {
t.Fatalf("expected capability configuration error, got %v", err)
func TestVolcesOutputCandidateFilterPassesThroughMissingCapability(t *testing.T) {
filtered, summary, err := filterRuntimeCandidatesByOutputTokens("responses", "demo", "text_generate", map[string]any{"input": "hello"}, []store.RuntimeModelCandidate{volcTextCandidate(nil)})
if err != nil || len(filtered) != 1 {
t.Fatalf("expected missing capability candidate to remain available: filtered=%+v summary=%+v err=%v", filtered, summary, err)
}
_, _, err = filterRuntimeCandidatesByOutputTokens("chat.completions", "demo", "text_generate", map[string]any{"messages": []any{}, "max_tokens": 32769}, []store.RuntimeModelCandidate{volcTextCandidate(32768)})
result := preprocessRequestWithLog("responses", map[string]any{"input": "hello"}, filtered[0])
if result.Err != nil {
t.Fatalf("missing capability preprocessing must pass through: %v", result.Err)
}
if _, ok := result.Body["max_output_tokens"]; ok {
t.Fatalf("missing capability preprocessing must not inject max_output_tokens: %+v", result.Body)
}
}
func TestVolcesOutputCandidateFilterRejectsExceededKnownCapability(t *testing.T) {
_, _, err := filterRuntimeCandidatesByOutputTokens("chat.completions", "demo", "text_generate", map[string]any{"messages": []any{}, "max_tokens": 32769}, []store.RuntimeModelCandidate{volcTextCandidate(32768)})
if store.ModelCandidateErrorCode(err) != volcesOutputInvalidParameterCode {
t.Fatalf("expected invalid_parameter for explicit limit, got %v", err)
}
}
func TestVolcesOutputCandidateFilterAllowsUnknownCapabilityForExplicitLimit(t *testing.T) {
missing := volcTextCandidate(nil)
missing.PlatformID = "missing"
configured := volcTextCandidate(32768)
configured.PlatformID = "configured"
filtered, summary, err := filterRuntimeCandidatesByOutputTokens(
"chat.completions",
"demo",
"text_generate",
map[string]any{"messages": []any{}, "max_tokens": 65536},
[]store.RuntimeModelCandidate{missing, configured},
)
if err != nil || len(filtered) != 1 || filtered[0].PlatformID != "missing" {
t.Fatalf("expected unknown capability candidate to remain available: filtered=%+v summary=%+v err=%v", filtered, summary, err)
}
}
func TestVolcesOutputProcessorValidatesExplicitLimitWithoutCapability(t *testing.T) {
valid := preprocessRequestWithLog("chat.completions", map[string]any{"messages": []any{}, "max_tokens": 2048}, volcTextCandidate(nil))
if valid.Err != nil || valid.Body["max_tokens"] != 2048 {
t.Fatalf("valid explicit limit must pass through: %+v", valid)
}
invalid := preprocessRequestWithLog("chat.completions", map[string]any{"messages": []any{}, "max_tokens": 0}, volcTextCandidate(nil))
if invalid.Err == nil {
t.Fatalf("invalid explicit limit must still be rejected: %+v", invalid)
}
_, _, err := filterRuntimeCandidatesByOutputTokens(
"chat.completions",
"demo",
"text_generate",
map[string]any{"messages": []any{}, "max_tokens": 0},
[]store.RuntimeModelCandidate{volcTextCandidate(nil)},
)
if store.ModelCandidateErrorCode(err) != volcesOutputInvalidParameterCode {
t.Fatalf("candidate filter must reject an invalid explicit limit, got %v", err)
}
}
func TestNonVolcesOutputProcessorDoesNotInject(t *testing.T) {
candidate := volcTextCandidate(131072)
candidate.Provider = "openai"