| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "sync/atomic" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | func TestSyntheticProbeIsBoundedAndDoesNotFollowRedirects(t *testing.T) { |
| 15 | for _, size := range []int{1024, 3_248_572} { |
| 16 | body := syntheticBody("test-model", size) |
| 17 | var payload map[string]any |
| 18 | if len(body) != size || json.Unmarshal(body, &payload) != nil || payload["max_tokens"] != float64(1) || payload["tools"] != nil { |
| 19 | t.Fatalf("invalid synthetic request length=%d", len(body)) |
| 20 | } |
| 21 | } |
| 22 | var calls atomic.Int32 |
| 23 | s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 24 | calls.Add(1) |
| 25 | io.Copy(io.Discard, r.Body) |
| 26 | http.Redirect(w, r, "/redirect", http.StatusTemporaryRedirect) |
| 27 | })) |
| 28 | defer s.Close() |
| 29 | t.Setenv("REASONIX_PROBE_API_KEY", "local-only-key") |
| 30 | if err := run(s.URL, "test-model", "h1", 1024, false, time.Second); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | if calls.Load() != 1 { |
| 34 | t.Fatalf("calls=%d", calls.Load()) |
| 35 | } |
| 36 | tr := &singleTransport{base: http.DefaultTransport} |
| 37 | tr.used.Store(true) |
| 38 | req, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, s.URL, nil) |
| 39 | if _, err := tr.RoundTrip(req); err == nil || calls.Load() != 1 { |
| 40 | t.Fatal("probe allowed a retry") |
| 41 | } |
| 42 | } |
| 43 |