1- package aibridge
1+ package aibridge_test
22
33import (
44 "net/http"
@@ -7,16 +7,22 @@ import (
77
88 "github.com/stretchr/testify/assert"
99 "github.com/stretchr/testify/require"
10+ "go.opentelemetry.io/otel"
1011
1112 "cdr.dev/slog/v3/sloggers/slogtest"
13+ "github.com/coder/aibridge"
1214 "github.com/coder/aibridge/config"
1315 "github.com/coder/aibridge/internal/testutil"
1416 "github.com/coder/aibridge/provider"
1517)
1618
17- func TestValidateProvider_Names (t * testing.T ) {
19+ var bridgeTestTracer = otel .Tracer ("bridge_test" )
20+
21+ func TestValidateProviders (t * testing.T ) {
1822 t .Parallel ()
1923
24+ logger := slogtest .Make (t , nil )
25+
2026 tests := []struct {
2127 name string
2228 providers []provider.Provider
@@ -25,94 +31,69 @@ func TestValidateProvider_Names(t *testing.T) {
2531 {
2632 name : "all_supported_providers" ,
2733 providers : []provider.Provider {
28- NewOpenAIProvider (config.OpenAI {Name : "openai" , BaseURL : "https://api.openai.com/v1/" }),
29- NewAnthropicProvider (config.Anthropic {Name : "anthropic" , BaseURL : "https://api.anthropic.com/" }, nil ),
30- NewCopilotProvider (config.Copilot {Name : "copilot" , BaseURL : "https://api.individual.githubcopilot.com" }),
31- NewCopilotProvider (config.Copilot {Name : "copilot-business" , BaseURL : "https://api.business.githubcopilot.com" }),
32- NewCopilotProvider (config.Copilot {Name : "copilot-enterprise" , BaseURL : "https://api.enterprise.githubcopilot.com" }),
34+ aibridge . NewOpenAIProvider (config.OpenAI {Name : "openai" , BaseURL : "https://api.openai.com/v1/" }),
35+ aibridge . NewAnthropicProvider (config.Anthropic {Name : "anthropic" , BaseURL : "https://api.anthropic.com/" }, nil ),
36+ aibridge . NewCopilotProvider (config.Copilot {Name : "copilot" , BaseURL : "https://api.individual.githubcopilot.com" }),
37+ aibridge . NewCopilotProvider (config.Copilot {Name : "copilot-business" , BaseURL : "https://api.business.githubcopilot.com" }),
38+ aibridge . NewCopilotProvider (config.Copilot {Name : "copilot-enterprise" , BaseURL : "https://api.enterprise.githubcopilot.com" }),
3339 },
3440 },
3541 {
3642 name : "default_names_and_base_urls" ,
3743 providers : []provider.Provider {
38- NewOpenAIProvider (config.OpenAI {}),
39- NewAnthropicProvider (config.Anthropic {}, nil ),
40- NewCopilotProvider (config.Copilot {}),
44+ aibridge . NewOpenAIProvider (config.OpenAI {}),
45+ aibridge . NewAnthropicProvider (config.Anthropic {}, nil ),
46+ aibridge . NewCopilotProvider (config.Copilot {}),
4147 },
4248 },
4349 {
4450 name : "multiple_copilot_instances" ,
4551 providers : []provider.Provider {
46- NewCopilotProvider (config.Copilot {}),
47- NewCopilotProvider (config.Copilot {Name : "copilot-business" , BaseURL : "https://api.business.githubcopilot.com" }),
48- NewCopilotProvider (config.Copilot {Name : "copilot-enterprise" , BaseURL : "https://api.enterprise.githubcopilot.com" }),
52+ aibridge . NewCopilotProvider (config.Copilot {}),
53+ aibridge . NewCopilotProvider (config.Copilot {Name : "copilot-business" , BaseURL : "https://api.business.githubcopilot.com" }),
54+ aibridge . NewCopilotProvider (config.Copilot {Name : "copilot-enterprise" , BaseURL : "https://api.enterprise.githubcopilot.com" }),
4955 },
5056 },
5157 {
5258 name : "name_with_slashes" ,
5359 providers : []provider.Provider {
54- NewCopilotProvider (config.Copilot {Name : "copilot/business" , BaseURL : "https://api.business.githubcopilot.com" }),
60+ aibridge . NewCopilotProvider (config.Copilot {Name : "copilot/business" , BaseURL : "https://api.business.githubcopilot.com" }),
5561 },
5662 expectErr : "invalid provider name" ,
5763 },
5864 {
5965 name : "name_with_spaces" ,
6066 providers : []provider.Provider {
61- NewCopilotProvider (config.Copilot {Name : "copilot business" , BaseURL : "https://api.business.githubcopilot.com" }),
67+ aibridge . NewCopilotProvider (config.Copilot {Name : "copilot business" , BaseURL : "https://api.business.githubcopilot.com" }),
6268 },
6369 expectErr : "invalid provider name" ,
6470 },
6571 {
6672 name : "name_with_uppercase" ,
6773 providers : []provider.Provider {
68- NewCopilotProvider (config.Copilot {Name : "Copilot" , BaseURL : "https://api.business.githubcopilot.com" }),
74+ aibridge . NewCopilotProvider (config.Copilot {Name : "Copilot" , BaseURL : "https://api.business.githubcopilot.com" }),
6975 },
7076 expectErr : "invalid provider name" ,
7177 },
72- }
73-
74- for _ , tc := range tests {
75- t .Run (tc .name , func (t * testing.T ) {
76- t .Parallel ()
77-
78- err := validateProviders (tc .providers )
79- if tc .expectErr != "" {
80- require .Error (t , err )
81- assert .Contains (t , err .Error (), tc .expectErr )
82- } else {
83- require .NoError (t , err )
84- }
85- })
86- }
87- }
88-
89- func TestValidateProvider_DuplicateNames (t * testing.T ) {
90- t .Parallel ()
91-
92- tests := []struct {
93- name string
94- providers []provider.Provider
95- expectErr string
96- }{
9778 {
9879 name : "unique_names" ,
9980 providers : []provider.Provider {
100- NewCopilotProvider (config.Copilot {Name : "copilot" , BaseURL : "https://api.individual.githubcopilot.com" }),
101- NewCopilotProvider (config.Copilot {Name : "copilot-business" , BaseURL : "https://api.business.githubcopilot.com" }),
81+ aibridge . NewCopilotProvider (config.Copilot {Name : "copilot" , BaseURL : "https://api.individual.githubcopilot.com" }),
82+ aibridge . NewCopilotProvider (config.Copilot {Name : "copilot-business" , BaseURL : "https://api.business.githubcopilot.com" }),
10283 },
10384 },
10485 {
10586 name : "duplicate_base_url_different_names" ,
10687 providers : []provider.Provider {
107- NewCopilotProvider (config.Copilot {Name : "copilot" , BaseURL : "https://api.individual.githubcopilot.com" }),
108- NewCopilotProvider (config.Copilot {Name : "copilot-business" , BaseURL : "https://api.individual.githubcopilot.com" }),
88+ aibridge . NewCopilotProvider (config.Copilot {Name : "copilot" , BaseURL : "https://api.individual.githubcopilot.com" }),
89+ aibridge . NewCopilotProvider (config.Copilot {Name : "copilot-business" , BaseURL : "https://api.individual.githubcopilot.com" }),
10990 },
11091 },
11192 {
11293 name : "duplicate_name" ,
11394 providers : []provider.Provider {
114- NewCopilotProvider (config.Copilot {Name : "copilot" , BaseURL : "https://api.individual.githubcopilot.com" }),
115- NewCopilotProvider (config.Copilot {Name : "copilot" , BaseURL : "https://api.business.githubcopilot.com" }),
95+ aibridge . NewCopilotProvider (config.Copilot {Name : "copilot" , BaseURL : "https://api.individual.githubcopilot.com" }),
96+ aibridge . NewCopilotProvider (config.Copilot {Name : "copilot" , BaseURL : "https://api.business.githubcopilot.com" }),
11697 },
11798 expectErr : "duplicate provider name" ,
11899 },
@@ -122,7 +103,7 @@ func TestValidateProvider_DuplicateNames(t *testing.T) {
122103 t .Run (tc .name , func (t * testing.T ) {
123104 t .Parallel ()
124105
125- err := validateProviders ( tc .providers )
106+ _ , err := aibridge . NewRequestBridge ( t . Context (), tc .providers , nil , nil , logger , nil , bridgeTestTracer )
126107 if tc .expectErr != "" {
127108 require .Error (t , err )
128109 assert .Contains (t , err .Error (), tc .expectErr )
@@ -148,7 +129,7 @@ func TestPassthroughRoutesForProviders(t *testing.T) {
148129 name : "openAI_no_base_path" ,
149130 requestPath : "/openai/v1/conversations" ,
150131 provider : func (baseURL string ) provider.Provider {
151- return NewOpenAIProvider (config.OpenAI {BaseURL : baseURL })
132+ return aibridge . NewOpenAIProvider (config.OpenAI {BaseURL : baseURL })
152133 },
153134 expectPath : "/conversations" ,
154135 },
@@ -157,15 +138,15 @@ func TestPassthroughRoutesForProviders(t *testing.T) {
157138 baseURLPath : "/v1" ,
158139 requestPath : "/openai/v1/conversations" ,
159140 provider : func (baseURL string ) provider.Provider {
160- return NewOpenAIProvider (config.OpenAI {BaseURL : baseURL })
141+ return aibridge . NewOpenAIProvider (config.OpenAI {BaseURL : baseURL })
161142 },
162143 expectPath : "/v1/conversations" ,
163144 },
164145 {
165146 name : "anthropic_no_base_path" ,
166147 requestPath : "/anthropic/v1/models" ,
167148 provider : func (baseURL string ) provider.Provider {
168- return NewAnthropicProvider (config.Anthropic {BaseURL : baseURL }, nil )
149+ return aibridge . NewAnthropicProvider (config.Anthropic {BaseURL : baseURL }, nil )
169150 },
170151 expectPath : "/v1/models" ,
171152 },
@@ -174,15 +155,15 @@ func TestPassthroughRoutesForProviders(t *testing.T) {
174155 baseURLPath : "/v1" ,
175156 requestPath : "/anthropic/v1/models" ,
176157 provider : func (baseURL string ) provider.Provider {
177- return NewAnthropicProvider (config.Anthropic {BaseURL : baseURL }, nil )
158+ return aibridge . NewAnthropicProvider (config.Anthropic {BaseURL : baseURL }, nil )
178159 },
179160 expectPath : "/v1/v1/models" ,
180161 },
181162 {
182163 name : "copilot_no_base_path" ,
183164 requestPath : "/copilot/models" ,
184165 provider : func (baseURL string ) provider.Provider {
185- return NewCopilotProvider (config.Copilot {BaseURL : baseURL })
166+ return aibridge . NewCopilotProvider (config.Copilot {BaseURL : baseURL })
186167 },
187168 expectPath : "/models" ,
188169 },
@@ -191,7 +172,7 @@ func TestPassthroughRoutesForProviders(t *testing.T) {
191172 baseURLPath : "/v1" ,
192173 requestPath : "/copilot/models" ,
193174 provider : func (baseURL string ) provider.Provider {
194- return NewCopilotProvider (config.Copilot {BaseURL : baseURL })
175+ return aibridge . NewCopilotProvider (config.Copilot {BaseURL : baseURL })
195176 },
196177 expectPath : "/v1/models" ,
197178 },
@@ -210,14 +191,14 @@ func TestPassthroughRoutesForProviders(t *testing.T) {
210191 }))
211192 t .Cleanup (upstream .Close )
212193
213- recorder := testutil.MockRecorder {}
194+ rec := testutil.MockRecorder {}
214195 prov := tc .provider (upstream .URL + tc .baseURLPath )
215- bridge , err := NewRequestBridge (t .Context (), []provider.Provider {prov }, & recorder , nil , logger , nil , testTracer )
196+ bridge , err := aibridge . NewRequestBridge (t .Context (), []provider.Provider {prov }, & rec , nil , logger , nil , bridgeTestTracer )
216197 require .NoError (t , err )
217198
218199 req := httptest .NewRequest ("" , tc .requestPath , nil )
219200 resp := httptest .NewRecorder ()
220- bridge .mux . ServeHTTP (resp , req )
201+ bridge .ServeHTTP (resp , req )
221202
222203 assert .Equal (t , http .StatusOK , resp .Code )
223204 assert .Contains (t , resp .Body .String (), upstreamRespBody )
0 commit comments