| 1 | package responses |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/provider" |
| 9 | ) |
| 10 | |
| 11 | func TestStreamUsesConfiguredRequestURLExactly(t *testing.T) { |
| 12 | server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 13 | if r.URL.RequestURI() != "/custom/responses/?token=1" { |
| 14 | t.Errorf("request URI = %q, want /custom/responses/?token=1", r.URL.RequestURI()) |
| 15 | } |
| 16 | writeEvents(w, `{"type":"response.completed","response":{"id":"resp_1","usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}}}`) |
| 17 | })) |
| 18 | defer server.Close() |
| 19 | |
| 20 | collect(t, New(Config{ |
| 21 | Name: "custom-responses", APIKey: "key", BaseURL: server.URL + "/base", RequestURL: server.URL + "/custom/responses/?token=1", Model: "m", Mode: "stateless", |
| 22 | }), provider.Request{Messages: []provider.Message{{Role: provider.RoleUser, Content: "hi"}}}) |
| 23 | } |
| 24 | |
| 25 | func TestFactoryUsesRequestURLAndIgnoresLegacyChatURL(t *testing.T) { |
| 26 | p, err := newFromConfig(provider.Config{ |
| 27 | BaseURL: "https://base.example.com/v1", |
| 28 | Model: "m", |
| 29 | Extra: map[string]any{ |
| 30 | "chat_url": "https://stale.example.com/chat/completions", |
| 31 | "request_url": "https://exact.example.com/custom/responses/?token=1", |
| 32 | }, |
| 33 | }) |
| 34 | if err != nil { |
| 35 | t.Fatalf("newFromConfig: %v", err) |
| 36 | } |
| 37 | if got := p.(*client).requestURL; got != "https://exact.example.com/custom/responses/?token=1" { |
| 38 | t.Fatalf("requestURL = %q, want exact request_url", got) |
| 39 | } |
| 40 | |
| 41 | legacy, err := newFromConfig(provider.Config{ |
| 42 | BaseURL: "https://base.example.com/v1", |
| 43 | Model: "m", |
| 44 | Extra: map[string]any{"chat_url": "https://stale.example.com/chat/completions"}, |
| 45 | }) |
| 46 | if err != nil { |
| 47 | t.Fatalf("newFromConfig legacy: %v", err) |
| 48 | } |
| 49 | if got := legacy.(*client).requestURL; got != "https://base.example.com/v1/responses" { |
| 50 | t.Fatalf("legacy requestURL = %q, want base-derived endpoint", got) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestRequestURLEqualToBaseURLIsIgnored(t *testing.T) { |
| 55 | p, err := newFromConfig(provider.Config{ |
| 56 | BaseURL: "https://base.example.com/v1", |
| 57 | Model: "m", |
| 58 | Extra: map[string]any{"request_url": "https://base.example.com/v1"}, |
| 59 | }) |
| 60 | if err != nil { |
| 61 | t.Fatalf("newFromConfig: %v", err) |
| 62 | } |
| 63 | if got := p.(*client).requestURL; got != "https://base.example.com/v1/responses" { |
| 64 | t.Fatalf("requestURL = %q, want base-derived /responses endpoint", got) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestRequestURLRepeatingBaseResolution(t *testing.T) { |
| 69 | const base = "https://base.example.com/v1" |
| 70 | for override, want := range map[string]string{ |
| 71 | "HTTPS://BASE.Example.com/v1/": base + "/responses", |
| 72 | "https://base.example.com/v1?token=1": "https://base.example.com/v1?token=1", |
| 73 | "https://base.example.com/v1#debug": "https://base.example.com/v1#debug", |
| 74 | "https://base.example.com/V1": "https://base.example.com/V1", |
| 75 | "https://base.example.com/v1/response": "https://base.example.com/v1/response", |
| 76 | } { |
| 77 | p, err := newFromConfig(provider.Config{BaseURL: base, Model: "m", Extra: map[string]any{"request_url": override}}) |
| 78 | if err != nil { |
| 79 | t.Fatalf("newFromConfig: %v", err) |
| 80 | } |
| 81 | if got := p.(*client).requestURL; got != want { |
| 82 | t.Errorf("request_url %q resolved to %q, want %q", override, got, want) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 |