| 1 | package anthropic |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/provider" |
| 11 | ) |
| 12 | |
| 13 | func TestStreamUsesConfiguredRequestURLExactly(t *testing.T) { |
| 14 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 15 | if r.URL.RequestURI() != "/custom/messages/?token=1" { |
| 16 | t.Errorf("request URI = %q, want /custom/messages/?token=1", r.URL.RequestURI()) |
| 17 | } |
| 18 | w.Header().Set("Content-Type", "text/event-stream") |
| 19 | _, _ = io.WriteString(w, `event: message_start |
| 20 | data: {"type":"message_start","message":{"usage":{"input_tokens":2,"output_tokens":0}}} |
| 21 | |
| 22 | event: message_delta |
| 23 | data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}} |
| 24 | |
| 25 | event: message_stop |
| 26 | data: {"type":"message_stop"} |
| 27 | |
| 28 | `) |
| 29 | })) |
| 30 | defer srv.Close() |
| 31 | |
| 32 | p, err := New(provider.Config{ |
| 33 | Name: "custom-anthropic", |
| 34 | BaseURL: srv.URL + "/base", |
| 35 | Model: "model", |
| 36 | APIKey: "key", |
| 37 | Extra: map[string]any{"request_url": srv.URL + "/custom/messages/?token=1"}, |
| 38 | }) |
| 39 | if err != nil { |
| 40 | t.Fatalf("New: %v", err) |
| 41 | } |
| 42 | stream, err := p.Stream(context.Background(), provider.Request{Messages: []provider.Message{{Role: provider.RoleUser, Content: "hi"}}}) |
| 43 | if err != nil { |
| 44 | t.Fatalf("Stream: %v", err) |
| 45 | } |
| 46 | for chunk := range stream { |
| 47 | if chunk.Type == provider.ChunkError { |
| 48 | t.Fatalf("stream error: %v", chunk.Err) |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestLegacyChatURLRemainsIgnored(t *testing.T) { |
| 54 | p, err := New(provider.Config{ |
| 55 | BaseURL: "https://base.example.com/v1", |
| 56 | Model: "model", |
| 57 | Extra: map[string]any{"chat_url": "https://stale.example.com/chat/completions"}, |
| 58 | }) |
| 59 | if err != nil { |
| 60 | t.Fatalf("New: %v", err) |
| 61 | } |
| 62 | if got := p.(*client).requestURL; got != "https://base.example.com/v1/messages" { |
| 63 | t.Fatalf("requestURL = %q, want legacy base-derived endpoint", got) |
| 64 | } |
| 65 | } |
| 66 |