summaryrefslogtreecommitdiff
path: root/internal/provider/forwarder_test.go
blob: 2e9b4a1a3ef6d0fafc367afa79d4475bb9f0af37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package provider

import (
	"encoding/json"
	"testing"

	"aigw/internal/domain"
)

func TestRewriteRequestForcesOpenAIStreamUsage(t *testing.T) {
	result, err := rewriteRequest([]byte(`{"model":"public/model","stream":true,"stream_options":{"other":true}}`), "upstream/model", domain.ProtocolOpenAI)
	if err != nil {
		t.Fatal(err)
	}
	var body struct {
		Model         string         `json:"model"`
		StreamOptions map[string]any `json:"stream_options"`
	}
	if err := json.Unmarshal(result, &body); err != nil {
		t.Fatal(err)
	}
	if body.Model != "upstream/model" || body.StreamOptions["include_usage"] != true || body.StreamOptions["other"] != true {
		t.Fatalf("unexpected rewritten body: %s", result)
	}
}

func TestRewriteRequestDoesNotAddStreamOptionsToAnthropic(t *testing.T) {
	result, err := rewriteRequest([]byte(`{"model":"public/model","stream":true}`), "upstream/model", domain.ProtocolAnthropic)
	if err != nil {
		t.Fatal(err)
	}
	var body map[string]json.RawMessage
	if err := json.Unmarshal(result, &body); err != nil {
		t.Fatal(err)
	}
	if _, exists := body["stream_options"]; exists {
		t.Fatalf("unexpected OpenAI stream options in Anthropic request: %s", result)
	}
}