|
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.ClientModel; |
5 | 6 | using System.ClientModel.Primitives; |
6 | 7 | using System.Linq; |
7 | 8 | using Microsoft.ClientModel.TestFramework; |
@@ -166,4 +167,43 @@ public void TestResponseCreationOptionsExtensions() |
166 | 167 | Assert.That(options.StructuredInputs.Keys, Has.Count.EqualTo(2)); |
167 | 168 | Assert.That(options.StructuredInputs["direct_value_2"].ToString(), Is.EqualTo("42")); |
168 | 169 | } |
| 170 | + |
| 171 | + [Test] |
| 172 | + public void ResponsesEndpointsSetCorrectly() |
| 173 | + { |
| 174 | + Uri mockProjectEndpoint = new("https://microsoft.com/mock/endpoint"); |
| 175 | + Uri mockOpenAIEndpoint = new($"{mockProjectEndpoint.AbsoluteUri}/openai"); |
| 176 | + AuthenticationTokenProvider mockCredential = new MockCredential(); |
| 177 | + AuthenticationPolicy mockAuthPolicy = new BearerTokenPolicy(mockCredential, "https://ai.azure.com/.default"); |
| 178 | + |
| 179 | + ProjectOpenAIClientOptions GetOptions(Uri endpoint = null) => new() { Endpoint = endpoint }; |
| 180 | + |
| 181 | + // Not specifying options should use the constructed /openai endpoint from the project Uri |
| 182 | + ProjectOpenAIClient client = new(mockProjectEndpoint, new MockCredential()); |
| 183 | + Assert.That(client.Endpoint?.AbsoluteUri, Is.EqualTo(mockOpenAIEndpoint)); |
| 184 | + client = new(new Uri(mockProjectEndpoint.AbsoluteUri + "/"), mockCredential); |
| 185 | + Assert.That(client.Endpoint?.AbsoluteUri, Is.EqualTo(mockOpenAIEndpoint)); |
| 186 | + |
| 187 | + // Providing no endpoint anywhere should throw |
| 188 | + Assert.Throws<ArgumentNullException>(() => client = new(mockAuthPolicy, GetOptions())); |
| 189 | + |
| 190 | + // Supplying in options should use the literal value with no construction |
| 191 | + client = new(mockAuthPolicy, GetOptions(mockProjectEndpoint)); |
| 192 | + Assert.That(client.Endpoint?.AbsoluteUri, Is.EqualTo(mockProjectEndpoint.AbsoluteUri)); |
| 193 | + |
| 194 | + // Supplying in both should be OK if they match correctly |
| 195 | + client = new(mockProjectEndpoint, mockCredential, GetOptions(mockOpenAIEndpoint)); |
| 196 | + Assert.That(client.Endpoint?.AbsoluteUri, Is.EqualTo(mockOpenAIEndpoint)); |
| 197 | + |
| 198 | + // Supplying in both should throw if they don't match |
| 199 | + Assert.Throws<InvalidOperationException>(() => client = new(mockProjectEndpoint, mockCredential, GetOptions(mockProjectEndpoint))); |
| 200 | + |
| 201 | + // Clients retrieved from ProjectOpenAIClient should handle construction |
| 202 | + ProjectOpenAIClient openAIClient = new(mockProjectEndpoint, mockCredential); |
| 203 | + Assert.That(openAIClient.Responses.Endpoint, Is.EqualTo(mockOpenAIEndpoint)); |
| 204 | + Assert.That(openAIClient.GetOpenAIResponseClient("model").Endpoint, Is.EqualTo(mockOpenAIEndpoint)); |
| 205 | + Assert.That(openAIClient.GetProjectResponsesClient().Endpoint, Is.EqualTo(mockOpenAIEndpoint)); |
| 206 | + Assert.That(openAIClient.GetProjectResponsesClientForModel("model").Endpoint, Is.EqualTo(mockOpenAIEndpoint)); |
| 207 | + Assert.That(openAIClient.GetProjectResponsesClientForAgent(new AgentReference("agent")).Endpoint, Is.EqualTo(mockOpenAIEndpoint)); |
| 208 | + } |
169 | 209 | } |
0 commit comments