fix(tests): update model lookup references and enhance Claude executor tests

This commit is contained in:
hkfires
2026-04-17 08:32:07 +08:00
parent 5dcca69e8c
commit d9a3b3e5f3
3 changed files with 58 additions and 23 deletions

View File

@@ -136,13 +136,13 @@ func TestGetAvailableModelsReturnsClonedSupportedParameters(t *testing.T) {
}
func TestLookupModelInfoReturnsCloneForStaticDefinitions(t *testing.T) {
first := LookupModelInfo("glm-4.6")
first := LookupModelInfo("claude-sonnet-4-6")
if first == nil || first.Thinking == nil || len(first.Thinking.Levels) == 0 {
t.Fatalf("expected static model with thinking levels, got %+v", first)
}
first.Thinking.Levels[0] = "mutated"
second := LookupModelInfo("glm-4.6")
second := LookupModelInfo("claude-sonnet-4-6")
if second == nil || second.Thinking == nil || len(second.Thinking.Levels) == 0 || second.Thinking.Levels[0] == "mutated" {
t.Fatalf("expected static lookup clone, got %+v", second)
}

View File

@@ -1714,7 +1714,27 @@ func TestClaudeExecutor_ExecuteStream_AcceptEncodingOverrideCannotBypassIdentity
}
}
// Test case 1: String system prompt is preserved and converted to a content block
func expectedClaudeCodeStaticPrompt() string {
return strings.Join([]string{
helps.ClaudeCodeIntro,
helps.ClaudeCodeSystem,
helps.ClaudeCodeDoingTasks,
helps.ClaudeCodeToneAndStyle,
helps.ClaudeCodeOutputEfficiency,
}, "\n\n")
}
func expectedForwardedSystemReminder(text string) string {
return fmt.Sprintf(`<system-reminder>
As you answer the user's questions, you can use the following context from the system:
%s
IMPORTANT: this context may or may not be relevant to your tasks. You should not respond to this context unless it is highly relevant to your task.
</system-reminder>
`, text)
}
// Test case 1: String system prompt is preserved by forwarding it to the first user message
func TestCheckSystemInstructionsWithMode_StringSystemPreserved(t *testing.T) {
payload := []byte(`{"system":"You are a helpful assistant.","messages":[{"role":"user","content":"hi"}]}`)
@@ -1733,42 +1753,52 @@ func TestCheckSystemInstructionsWithMode_StringSystemPreserved(t *testing.T) {
if !strings.HasPrefix(blocks[0].Get("text").String(), "x-anthropic-billing-header:") {
t.Fatalf("blocks[0] should be billing header, got %q", blocks[0].Get("text").String())
}
if blocks[1].Get("text").String() != "You are a Claude agent, built on Anthropic's Claude Agent SDK." {
if blocks[1].Get("text").String() != "You are Claude Code, Anthropic's official CLI for Claude." {
t.Fatalf("blocks[1] should be agent block, got %q", blocks[1].Get("text").String())
}
if blocks[2].Get("text").String() != "You are a helpful assistant." {
t.Fatalf("blocks[2] should be user system prompt, got %q", blocks[2].Get("text").String())
if blocks[2].Get("text").String() != expectedClaudeCodeStaticPrompt() {
t.Fatalf("blocks[2] should be static Claude Code prompt, got %q", blocks[2].Get("text").String())
}
if blocks[2].Get("cache_control.type").String() != "ephemeral" {
t.Fatalf("blocks[2] should have cache_control.type=ephemeral")
if blocks[2].Get("cache_control").Exists() {
t.Fatalf("blocks[2] should not have cache_control, got %s", blocks[2].Get("cache_control").Raw)
}
if got := gjson.GetBytes(out, "messages.0.content").String(); got != expectedForwardedSystemReminder("You are a helpful assistant.")+"hi" {
t.Fatalf("messages[0].content should include forwarded system prompt, got %q", got)
}
}
// Test case 2: Strict mode drops the string system prompt
// Test case 2: Strict mode keeps only the injected Claude Code system blocks
func TestCheckSystemInstructionsWithMode_StringSystemStrict(t *testing.T) {
payload := []byte(`{"system":"You are a helpful assistant.","messages":[{"role":"user","content":"hi"}]}`)
out := checkSystemInstructionsWithMode(payload, true)
blocks := gjson.GetBytes(out, "system").Array()
if len(blocks) != 2 {
t.Fatalf("strict mode should produce 2 blocks, got %d", len(blocks))
if len(blocks) != 3 {
t.Fatalf("strict mode should produce 3 injected blocks, got %d", len(blocks))
}
if got := gjson.GetBytes(out, "messages.0.content").String(); got != "hi" {
t.Fatalf("strict mode should not forward system prompt into messages, got %q", got)
}
}
// Test case 3: Empty string system prompt does not produce a spurious block
// Test case 3: Empty string system prompt does not alter the first user message
func TestCheckSystemInstructionsWithMode_EmptyStringSystemIgnored(t *testing.T) {
payload := []byte(`{"system":"","messages":[{"role":"user","content":"hi"}]}`)
out := checkSystemInstructionsWithMode(payload, false)
blocks := gjson.GetBytes(out, "system").Array()
if len(blocks) != 2 {
t.Fatalf("empty string system should produce 2 blocks, got %d", len(blocks))
if len(blocks) != 3 {
t.Fatalf("empty string system should still produce 3 injected blocks, got %d", len(blocks))
}
if got := gjson.GetBytes(out, "messages.0.content").String(); got != "hi" {
t.Fatalf("empty string system should not alter messages, got %q", got)
}
}
// Test case 4: Array system prompt is unaffected by the string handling
// Test case 4: Array system prompt is forwarded to the first user message
func TestCheckSystemInstructionsWithMode_ArraySystemStillWorks(t *testing.T) {
payload := []byte(`{"system":[{"type":"text","text":"Be concise."}],"messages":[{"role":"user","content":"hi"}]}`)
@@ -1778,12 +1808,15 @@ func TestCheckSystemInstructionsWithMode_ArraySystemStillWorks(t *testing.T) {
if len(blocks) != 3 {
t.Fatalf("expected 3 system blocks, got %d", len(blocks))
}
if blocks[2].Get("text").String() != "Be concise." {
t.Fatalf("blocks[2] should be user system prompt, got %q", blocks[2].Get("text").String())
if blocks[2].Get("text").String() != expectedClaudeCodeStaticPrompt() {
t.Fatalf("blocks[2] should be static Claude Code prompt, got %q", blocks[2].Get("text").String())
}
if got := gjson.GetBytes(out, "messages.0.content").String(); got != expectedForwardedSystemReminder("Be concise.")+"hi" {
t.Fatalf("messages[0].content should include forwarded array system prompt, got %q", got)
}
}
// Test case 5: Special characters in string system prompt survive conversion
// Test case 5: Special characters in string system prompt survive forwarding
func TestCheckSystemInstructionsWithMode_StringWithSpecialChars(t *testing.T) {
payload := []byte(`{"system":"Use <xml> tags & \"quotes\" in output.","messages":[{"role":"user","content":"hi"}]}`)
@@ -1793,8 +1826,8 @@ func TestCheckSystemInstructionsWithMode_StringWithSpecialChars(t *testing.T) {
if len(blocks) != 3 {
t.Fatalf("expected 3 system blocks, got %d", len(blocks))
}
if blocks[2].Get("text").String() != `Use <xml> tags & "quotes" in output.` {
t.Fatalf("blocks[2] text mangled, got %q", blocks[2].Get("text").String())
if got := gjson.GetBytes(out, "messages.0.content").String(); got != expectedForwardedSystemReminder(`Use <xml> tags & "quotes" in output.`)+"hi" {
t.Fatalf("forwarded system prompt text mangled, got %q", got)
}
}
@@ -1902,8 +1935,11 @@ func TestApplyCloaking_PreservesConfiguredStrictModeAndSensitiveWordsWhenModeOmi
out := applyCloaking(context.Background(), cfg, auth, payload, "claude-3-5-sonnet-20241022", "key-123")
blocks := gjson.GetBytes(out, "system").Array()
if len(blocks) != 2 {
t.Fatalf("expected strict mode to keep only injected system blocks, got %d", len(blocks))
if len(blocks) != 3 {
t.Fatalf("expected strict mode to keep the 3 injected Claude Code system blocks, got %d", len(blocks))
}
if got := gjson.GetBytes(out, "messages.0.content.#").Int(); got != 1 {
t.Fatalf("strict mode should not prepend a forwarded system reminder block, got %d content blocks", got)
}
if got := gjson.GetBytes(out, "messages.0.content.0.text").String(); !strings.Contains(got, "\u200B") {
t.Fatalf("expected configured sensitive word obfuscation to apply, got %q", got)

View File

@@ -2,7 +2,6 @@ package test
import (
"fmt"
"strings"
"testing"
"time"