| 1 | package openai |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "reasonix/internal/provider" |
| 7 | ) |
| 8 | |
| 9 | func TestNewPrefersExactRequestURLOverLegacyChatURL(t *testing.T) { |
| 10 | p, err := New(provider.Config{ |
| 11 | BaseURL: "https://base.example.com/v1", |
| 12 | Model: "model-a", |
| 13 | Extra: map[string]any{ |
| 14 | "chat_url": "https://legacy.example.com/chat/completions/", |
| 15 | "request_url": "https://exact.example.com/custom/?token=1", |
| 16 | }, |
| 17 | }) |
| 18 | if err != nil { |
| 19 | t.Fatalf("New: %v", err) |
| 20 | } |
| 21 | if got := p.(*client).chatURL; got != "https://exact.example.com/custom/?token=1" { |
| 22 | t.Fatalf("chatURL = %q, want exact request_url", got) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | func TestRequestURLEqualToBaseURLIsIgnored(t *testing.T) { |
| 27 | p, err := New(provider.Config{ |
| 28 | BaseURL: "https://base.example.com/v1", |
| 29 | Model: "model-a", |
| 30 | Extra: map[string]any{"request_url": "https://base.example.com/v1"}, |
| 31 | }) |
| 32 | if err != nil { |
| 33 | t.Fatalf("New: %v", err) |
| 34 | } |
| 35 | if got := p.(*client).chatURL; got != "https://base.example.com/v1/chat/completions" { |
| 36 | t.Fatalf("chatURL = %q, want base-derived chat/completions endpoint", got) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestEndpointOverrideRepeatingBaseResolvesCanonicalChatURL(t *testing.T) { |
| 41 | const base = "http://localhost:8000/v1" |
| 42 | for name, extra := range map[string]map[string]any{ |
| 43 | "request_url": {"request_url": base}, |
| 44 | "request_url slash": {"request_url": base + "/"}, |
| 45 | "request_url upper host": {"request_url": "HTTP://LOCALHOST:8000/v1"}, |
| 46 | "chat_url": {"chat_url": base}, |
| 47 | "chat_url upper host": {"chat_url": "http://LocalHost:8000/v1/"}, |
| 48 | "both repeat base": {"request_url": base, "chat_url": base}, |
| 49 | } { |
| 50 | t.Run(name, func(t *testing.T) { |
| 51 | if got := resolveOpenAIChatURL(base, extra); got != base+"/chat/completions" { |
| 52 | t.Fatalf("chat URL = %q, want %q", got, base+"/chat/completions") |
| 53 | } |
| 54 | }) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestEndpointOverrideDistinctFromBaseStaysVerbatim(t *testing.T) { |
| 59 | const base = "http://127.0.0.1:8000/v1" |
| 60 | for _, override := range []string{ |
| 61 | base + "?token=1", |
| 62 | base + "#debug", |
| 63 | "http://127.0.0.1:8000/V1", |
| 64 | "https://127.0.0.1:8000/v1", |
| 65 | "http://127.0.0.1:8000/custom/chat/completions", |
| 66 | } { |
| 67 | if got := resolveOpenAIChatURL(base, map[string]any{"request_url": override}); got != override { |
| 68 | t.Errorf("request_url %q resolved to %q, want it verbatim", override, got) |
| 69 | } |
| 70 | if got := resolveOpenAIChatURL(base, map[string]any{"chat_url": override}); got != override { |
| 71 | t.Errorf("chat_url %q resolved to %q, want it verbatim", override, got) |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 |