sub2api/backend/internal/pkg/windsurf/tool_bridge_test.go
win 21325afb33
Some checks failed
CI / test (push) Failing after 10s
CI / frontend (push) Failing after 8s
CI / golangci-lint (push) Failing after 5s
Security Scan / backend-security (push) Failing after 5s
Security Scan / frontend-security (push) Failing after 4s
feat(windsurf): 补全ops日志记录与endpoint派生,对齐其他平台
- windsurf_gateway_service: 添加上游延迟/TTFT/错误上下文记录
- endpoint: DeriveUpstreamEndpoint 添加 PlatformWindsurf 分支
- ops_error_logger: guessPlatformFromPath 添加 /windsurf/ 识别
2026-04-23 20:46:27 +08:00

160 lines
4.2 KiB
Go

package windsurf
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
)
func TestBuildToolPreambleForProtoCanonicalizesToolsAndChoice(t *testing.T) {
tools := []OpenAITool{
{
Type: "function",
Function: OpenAIFunction{
Name: "list_files",
Description: "List files in the repository",
Parameters: json.RawMessage(`{"type":"object"}`),
},
},
{
Type: "function",
Function: OpenAIFunction{
Name: "glob",
Description: "Duplicate alias should be deduped",
Parameters: json.RawMessage(`{"type":"object"}`),
},
},
}
got := BuildToolPreambleForProto(tools, map[string]any{
"type": "tool",
"name": "search_files",
})
if strings.Contains(got, "### list_files") {
t.Fatalf("preamble should not expose alias tool names: %s", got)
}
if count := strings.Count(got, "### glob"); count != 1 {
t.Fatalf("expected exactly one canonical glob tool, got %d in %s", count, got)
}
if !strings.Contains(got, `You MUST call the function "grep"`) {
t.Fatalf("forced tool choice should be canonicalized to grep: %s", got)
}
}
func TestNormalizeMessagesForCascadePreservesStructuredToolResultPayload(t *testing.T) {
messages := []AnthropicMessage{
{
Role: "tool",
ToolCallID: "call-1",
Content: json.RawMessage(`[
{"type":"text","text":"partial listing"},
{"type":"json","value":{"entries":["a.go","b.go"]}}
]`),
},
}
got := NormalizeMessagesForCascade(messages, nil)
if len(got) != 1 {
t.Fatalf("NormalizeMessagesForCascade() returned %d messages, want 1", len(got))
}
if !strings.Contains(got[0].Content, `"type":"json"`) {
t.Fatalf("structured tool_result payload should be preserved, got %q", got[0].Content)
}
}
func TestParseToolCallsFromTextNormalizesAliases(t *testing.T) {
text := strings.Join([]string{
`<tool_call>{"name":"list_files","arguments":{"path":"."}}</tool_call>`,
`{"name":"search_files","arguments":{"pattern":"TODO"}}`,
`{"tool_code":"apply_patch(\"*** Begin Patch\")"}`,
}, "\n")
got := ParseToolCallsFromText(text)
if len(got.ToolCalls) != 3 {
t.Fatalf("ParseToolCallsFromText() returned %d tool calls, want 3", len(got.ToolCalls))
}
wantNames := []string{"glob", "grep", "edit"}
for i, want := range wantNames {
if got.ToolCalls[i].Name != want {
t.Fatalf("tool call %d name = %q, want %q", i, got.ToolCalls[i].Name, want)
}
}
}
func TestSanitizePathMarksUnmountedWorkspace(t *testing.T) {
got := SanitizePath("/tmp/windsurf-workspace/pkg/main.go")
if got != "[unmounted-workspace]/pkg/main.go" {
t.Fatalf("SanitizePath() = %q, want %q", got, "[unmounted-workspace]/pkg/main.go")
}
}
func TestWarmupCascadeSkipsTrackedWorkspaceByDefault(t *testing.T) {
var mu sync.Mutex
var paths []string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
paths = append(paths, r.URL.Path)
mu.Unlock()
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
client := NewLocalLSClient(42099, "csrf")
client.BaseURL = server.URL
client.HTTP = server.Client()
if err := client.WarmupCascade(context.Background(), "token"); err != nil {
t.Fatalf("WarmupCascade() error = %v", err)
}
mu.Lock()
defer mu.Unlock()
for _, path := range paths {
if path == AddTrackedWorkspaceRPC {
t.Fatalf("WarmupCascade() unexpectedly called AddTrackedWorkspaceRPC: %v", paths)
}
}
}
func TestWarmupCascadeAddsConfiguredWorkspace(t *testing.T) {
var mu sync.Mutex
var paths []string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
paths = append(paths, r.URL.Path)
mu.Unlock()
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
client := NewLocalLSClient(42099, "csrf")
client.BaseURL = server.URL
client.HTTP = server.Client()
client.TrackedWorkspace = "/repo"
if err := client.WarmupCascade(context.Background(), "token"); err != nil {
t.Fatalf("WarmupCascade() error = %v", err)
}
mu.Lock()
defer mu.Unlock()
found := false
for _, path := range paths {
if path == AddTrackedWorkspaceRPC {
found = true
break
}
}
if !found {
t.Fatalf("WarmupCascade() should call AddTrackedWorkspaceRPC when TrackedWorkspace is configured: %v", paths)
}
}