返回 DeepSeek-Reasonix
request_error_diagnostic_test.go
根目录 / internal / provider / request_error_diagnostic_test.go
1 package provider
2
3 import (
4 "context"
5 "crypto/tls"
6 "encoding/json"
7 "errors"
8 "io"
9 "net/http/httptrace"
10 "net/url"
11 "os"
12 "strings"
13 "testing"
14 "time"
15
16 "golang.org/x/net/http2"
17 )
18
19 func TestTransportDiagnosticReasonsAreTypedAndSafe(t *testing.T) {
20 for _, tt := range []struct {
21 name string
22 err error
23 code, kind string
24 }{
25 {"eof", io.EOF, "eof", "transport_eof"},
26 {"truncated", io.ErrUnexpectedEOF, "unexpected_eof", "transport_eof"},
27 {"reset", closedConnectionErrnos[0], "connection_reset", "transport_reset"},
28 {"timeout", os.ErrDeadlineExceeded, "timeout", "transport_timeout"},
29 {"h2", http2.ConnectionError(http2.ErrCodeInternal), "http2_protocol", "transport_protocol"},
30 {"untyped", errors.New("EOF TLS timeout ?key=private-key"), "unknown", "unknown"},
31 {"send", &RequestSendTimeoutError{Budget: requestSendTimeout}, "request_send_timeout", "request_send_timeout"},
32 } {
33 t.Run(tt.name, func(t *testing.T) {
34 err := &url.Error{Op: "POST", URL: "https://user:private-key@provider.test/?key=private-key", Err: tt.err}
35 if got := transportErrorCode(err); got != tt.code {
36 t.Fatalf("code=%s", got)
37 }
38 d := DiagnoseFailure(&RequestFailure{Identity: RequestIdentity{Provider: "deepseek"}, Err: err})
39 if d.Kind != tt.kind {
40 t.Fatalf("diagnostic=%+v", d)
41 }
42 var last RequestObservation
43 ctx, s := observeRequest(WithRequestObserver(t.Context(), func(v RequestObservation) { last = v }))
44 trace := httptrace.ContextClientTrace(ctx)
45 trace.TLSHandshakeStart()
46 trace.TLSHandshakeDone(tls.ConnectionState{}, err)
47 s.finish(err, "request_error")
48 if last.ErrorCode != tt.code || last.ErrorPhase != "tls" || last.LastPhase != "tls_failed" {
49 t.Fatalf("observation=%+v", last)
50 }
51 raw, _ := json.Marshal(struct {
52 Failure *FailureDiagnostic
53 Request RequestObservation
54 }{d, last})
55 if strings.Contains(string(raw), "private-key") || strings.Contains(string(raw), "https://") {
56 t.Fatalf("unsafe diagnostic=%s", raw)
57 }
58 })
59 }
60 }
61
62 func TestTransportObservationTimeoutSource(t *testing.T) {
63 for _, parentDeadline := range []bool{false, true} {
64 ctx := t.Context()
65 want := "unknown"
66 if parentDeadline {
67 var cancel context.CancelFunc
68 ctx, cancel = context.WithDeadline(ctx, time.Now().Add(-time.Second))
69 defer cancel()
70 want = "parent_context"
71 }
72 var last RequestObservation
73 _, s := observeRequest(WithRequestObserver(ctx, func(v RequestObservation) { last = v }))
74 s.finish(context.DeadlineExceeded, "request_error")
75 if last.TimeoutSource != want || last.ErrorPhase != "unknown" {
76 t.Fatalf("timeout source=%+v, want=%s", last, want)
77 }
78 }
79 }
80
81 func TestTransportObservationParentTimeoutAndLateWrite(t *testing.T) {
82 var last RequestObservation
83 parent, cancel := context.WithCancel(t.Context())
84 ctx, s := observeRequest(WithRequestObserver(parent, func(v RequestObservation) { last = v }))
85 trace := httptrace.ContextClientTrace(ctx)
86 trace.WroteRequest(httptrace.WroteRequestInfo{Err: io.ErrUnexpectedEOF})
87 cancel()
88 s.finish(context.Canceled, "request_error")
89 if last.Phase != "canceled" || last.ErrorCode != "cancelled" {
90 t.Fatalf("cancellation=%+v", last)
91 }
92 _, s = observeRequest(WithRequestObserver(t.Context(), func(v RequestObservation) { last = v }))
93 s.update("headers_received", func(v *RequestObservation) { v.HeadersAt = v.ObservedAt })
94 s.transportFailure(io.ErrUnexpectedEOF, "request_write_failed")
95 if last.Phase != "headers_received" || last.ErrorCode != "" {
96 t.Fatalf("late write overwrote response=%+v", last)
97 }
98 }
99
100 func TestTransportObservationParentWinsConcurrentBodyFailure(t *testing.T) {
101 for _, deadline := range []bool{false, true} {
102 ctx, cancel := context.WithCancel(t.Context())
103 wantPhase, wantCode, wantSource := "canceled", "cancelled", ""
104 if deadline {
105 cancel()
106 ctx, cancel = context.WithDeadline(t.Context(), time.Now().Add(-time.Second))
107 wantPhase, wantCode, wantSource = "deadline_exceeded", "timeout", "parent_context"
108 }
109 var last RequestObservation
110 _, s := observeRequest(WithRequestObserver(ctx, func(v RequestObservation) { last = v }))
111 s.update("body_received", nil)
112 cancel()
113 s.finish(io.EOF, "body_eof")
114 if last.Phase != wantPhase || last.ErrorCode != wantCode || last.TimeoutSource != wantSource || last.ErrorPhase != "response_body" {
115 t.Fatalf("caller terminal cause lost: %+v", last)
116 }
117 }
118 }
119
120 func TestFailureDiagnosticStrictPreviousReader(t *testing.T) {
121 // This is the complete persisted diagnostic field set in v1.39.8/v1.39.9.
122 // Required recovery events use DisallowUnknownFields, unlike optional events.
123 type previous struct {
124 Kind string `json:"kind"`
125 Status int `json:"status,omitempty"`
126 TraceID string `json:"traceId,omitempty"`
127 ProviderID string `json:"providerId,omitempty"`
128 ProviderDisplayName string `json:"providerDisplayName,omitempty"`
129 Protocol string `json:"protocol,omitempty"`
130 RequestPath string `json:"requestPath,omitempty"`
131 TransportCode string `json:"transportCode,omitempty"`
132 }
133 for _, kind := range []string{"request_send_timeout", "transport_eof", "transport_reset", "transport_timeout", "transport_dns", FailureKindImageInput} {
134 raw, _ := json.Marshal(FailureDiagnostic{Kind: kind, ProviderID: "deepseek", Protocol: "openai"})
135 var old previous
136 decoder := json.NewDecoder(strings.NewReader(string(raw)))
137 decoder.DisallowUnknownFields()
138 if err := decoder.Decode(&old); err != nil || old.Kind != kind {
139 t.Fatalf("previous reader: %+v %v", old, err)
140 }
141 }
142 }
143
143 lines GO