| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestFailureDiagnosticOpaqueAndSafe(t *testing.T) { |
| 11 | for _, body := range []string{"", `{}`, `{"model":"anything"}`} { |
| 12 | d := DiagnoseFailure(&APIError{Status: 400, Body: body, TraceID: "trace_123"}) |
| 13 | if d.Kind != "upstream_reason_missing" || d.Status != 400 || d.TraceID != "trace_123" { |
| 14 | t.Fatalf("diagnostic=%+v", d) |
| 15 | } |
| 16 | } |
| 17 | e := &APIError{Status: 400, Body: `{"error":{"message":"invalid temperature"},"request":{"reasoning":"private"}}`, TraceID: "https://billing.invalid/private"} |
| 18 | d := DiagnoseFailure(e) |
| 19 | b, _ := json.Marshal(d) |
| 20 | if d.Kind != "request" || strings.Contains(string(b), "private") || strings.Contains(string(b), "billing") { |
| 21 | t.Fatalf("unsafe diagnostic: %s", b) |
| 22 | } |
| 23 | if DiagnoseFailure(nil) != nil { |
| 24 | t.Fatal("nil error diagnostic") |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func TestFailureDiagnosticKeepsDisplayAndStableIdentitySeparate(t *testing.T) { |
| 29 | err := &APIError{Provider: "deepseek-anthropic", ProviderDisplayName: "Deepseek2", Protocol: "openai", Status: 404, RequestPath: "/anthropic/v1/chat/completions"} |
| 30 | d := DiagnoseFailure(err) |
| 31 | if d.ProviderID != "deepseek-anthropic" || d.ProviderDisplayName != "Deepseek2" || d.Protocol != "openai" || d.Status != 404 || d.RequestPath != "/anthropic/v1/chat/completions" { |
| 32 | t.Fatalf("diagnostic = %+v", d) |
| 33 | } |
| 34 | if got := err.Error(); got != "Deepseek2 · Chat Completions: status 404" { |
| 35 | t.Fatalf("display error = %q", got) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestRequestFailureKeepsDisplayAndStableIdentitySeparate(t *testing.T) { |
| 40 | cause := errors.New("invalid request URL") |
| 41 | err := &RequestFailure{ |
| 42 | Identity: RequestIdentity{Provider: "deepseek-anthropic", DisplayName: "Deepseek2", Protocol: "openai"}, |
| 43 | Operation: "build request", |
| 44 | Err: cause, |
| 45 | } |
| 46 | d := DiagnoseFailure(err) |
| 47 | if d.ProviderID != "deepseek-anthropic" || d.ProviderDisplayName != "Deepseek2" || d.Protocol != "openai" { |
| 48 | t.Fatalf("diagnostic identity = %+v", d) |
| 49 | } |
| 50 | if !errors.Is(err, cause) || strings.Contains(err.Error(), "deepseek-anthropic") || !strings.Contains(err.Error(), "Deepseek2 · Chat Completions") { |
| 51 | t.Fatalf("request error did not preserve cause and display identity: %v", err) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestFailureDiagnosticDetailUsesOnlySafeOperatorFields(t *testing.T) { |
| 56 | diagnostic := &FailureDiagnostic{ |
| 57 | ProviderID: "deepseek-anthropic", |
| 58 | ProviderDisplayName: "Deepseek2", |
| 59 | Protocol: "openai", |
| 60 | RequestPath: "/anthropic/v1/chat/completions", |
| 61 | TraceID: "trace-secret", |
| 62 | } |
| 63 | if got, want := FailureDiagnosticDetail(diagnostic), "Connection ID: deepseek-anthropic\nRequest path: /anthropic/v1/chat/completions"; got != want { |
| 64 | t.Fatalf("FailureDiagnosticDetail() = %q, want %q", got, want) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestInterruptedTurnRecoveryOptionalFieldsRemainBackwardCompatible(t *testing.T) { |
| 69 | type legacyRecovery struct { |
| 70 | Pending bool `json:"pending,omitempty"` |
| 71 | InterruptedTools []string `json:"interrupted_tools,omitempty"` |
| 72 | } |
| 73 | current := InterruptedTurnRecovery{ |
| 74 | TerminalStatus: "failed", FailureDiagnostic: &FailureDiagnostic{Kind: "request", Status: 404}, |
| 75 | Pending: true, InterruptedTools: []string{"bash"}, |
| 76 | } |
| 77 | raw, err := json.Marshal(current) |
| 78 | if err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | var legacy legacyRecovery |
| 82 | if err := json.Unmarshal(raw, &legacy); err != nil { |
| 83 | t.Fatalf("legacy reader rejected optional fields: %v", err) |
| 84 | } |
| 85 | if !legacy.Pending || len(legacy.InterruptedTools) != 1 || legacy.InterruptedTools[0] != "bash" { |
| 86 | t.Fatalf("legacy fields lost: %+v", legacy) |
| 87 | } |
| 88 | var old InterruptedTurnRecovery |
| 89 | if err := json.Unmarshal([]byte(`{"pending":true,"interrupted_tools":["bash"]}`), &old); err != nil { |
| 90 | t.Fatalf("current reader rejected legacy record: %v", err) |
| 91 | } |
| 92 | if old.TerminalStatus != "" || old.FailureDiagnostic != nil || !old.Pending { |
| 93 | t.Fatalf("legacy defaults changed: %+v", old) |
| 94 | } |
| 95 | } |
| 96 | func TestSearchStatusStaysOutsideReplay(t *testing.T) { |
| 97 | raw := json.RawMessage(`{"type":"web_search_call","id":"s","status":"completed","opaque":"proof"}`) |
| 98 | call := ServerSearchCall{ID: "s", Raw: raw} |
| 99 | merged := MergeServerSearch(nil, call) |
| 100 | if merged[0].SourcesStatus != SourcesNotProvided { |
| 101 | t.Fatal(merged) |
| 102 | } |
| 103 | original := []Message{{Role: RoleAssistant, ServerSearch: merged}} |
| 104 | model := ModelMessages(original) |
| 105 | if model[0].ServerSearch[0].SourcesStatus != "" || string(model[0].ServerSearch[0].Raw) != string(raw) { |
| 106 | t.Fatal("presentation contaminated replay") |
| 107 | } |
| 108 | if original[0].ServerSearch[0].SourcesStatus != SourcesNotProvided || ProjectionMessages(original)[0].ServerSearch[0].SourcesStatus != SourcesNotProvided { |
| 109 | t.Fatal("lost stored display status") |
| 110 | } |
| 111 | if ServerSearchSourcesStatus(ServerSearchCall{}) != "" { |
| 112 | t.Fatal("inferred missing from legacy record") |
| 113 | } |
| 114 | if ServerSearchSourcesStatus(ServerSearchCall{Raw: json.RawMessage(`{"type":"web_search_tool_result_error"}`)}) != "" { |
| 115 | t.Fatal("error classified as search success") |
| 116 | } |
| 117 | if ServerSearchSourcesStatus(ServerSearchCall{Results: []ServerSearchHit{{URL: "https://example.com"}}}) != SourcesAvailable { |
| 118 | t.Fatal("valid source missing") |
| 119 | } |
| 120 | } |
| 121 |