| | |
| | | public class OpenAICompatibleAdapterTests |
| | | { |
| | | [Fact] |
| | | public async Task StreamAsync_PostsToBaseUrlChatCompletions() |
| | | { |
| | | var handler = new RecordingHandler(HttpStatusCode.OK, "data: {\"choices\":[{\"delta\":{\"content\":\"ok\"}}]}\n\ndata: [DONE]\n"); |
| | | using var httpClient = new HttpClient(handler); |
| | | var adapter = new OpenAICompatibleAdapter(httpClient); |
| | | |
| | | var chunks = await adapter.StreamAsync(CreateRequest(baseUrl: "https://api.deepseek.com")).ToListAsync(); |
| | | |
| | | Assert.Equal("https://api.deepseek.com/chat/completions", handler.LastRequest?.RequestUri?.ToString()); |
| | | Assert.Contains(chunks, chunk => chunk.Delta == "ok"); |
| | | } |
| | | |
| | | [Fact] |
| | | public async Task StreamAsync_AcceptsLegacyChatCompletionsEndpoint() |
| | | { |
| | | var handler = new RecordingHandler(HttpStatusCode.OK, "data: {\"choices\":[{\"delta\":{\"content\":\"ok\"}}]}\n\ndata: [DONE]\n"); |
| | | using var httpClient = new HttpClient(handler); |
| | | var adapter = new OpenAICompatibleAdapter(httpClient); |
| | | |
| | | await adapter.StreamAsync(CreateRequest(endpoint: "https://api.deepseek.com/chat/completions")).ToListAsync(); |
| | | |
| | | Assert.Equal("https://api.deepseek.com/chat/completions", handler.LastRequest?.RequestUri?.ToString()); |
| | | } |
| | | |
| | | [Fact] |
| | | public async Task StreamAsync_IncludesProviderErrorMessageWhenRequestFails() |
| | | { |
| | | using var httpClient = new HttpClient(new StaticResponseHandler( |
| | | using var httpClient = new HttpClient(new RecordingHandler( |
| | | HttpStatusCode.BadRequest, |
| | | """{"error":{"message":"Invalid max_tokens value, the valid range of max_tokens is [1, 393216]"}}""")); |
| | | var adapter = new OpenAICompatibleAdapter(httpClient); |
| | | |
| | | var exception = await Assert.ThrowsAsync<LlmProviderException>(async () => |
| | | { |
| | | await adapter.StreamAsync(CreateRequest()).ToListAsync(); |
| | | await adapter.StreamAsync(CreateRequest(baseUrl: "https://api.deepseek.com")).ToListAsync(); |
| | | }); |
| | | |
| | | Assert.Equal(LlmErrorCodes.EndpointUnavailable, exception.Code); |
| | |
| | | Assert.Contains("Invalid max_tokens value", exception.Message); |
| | | } |
| | | |
| | | private static LlmProviderRequest CreateRequest() |
| | | [Fact] |
| | | public async Task ListModelIdsAsync_ReadsOpenAiDataIds() |
| | | { |
| | | var handler = new RecordingHandler( |
| | | HttpStatusCode.OK, |
| | | """{"data":[{"id":"deepseek-v4-flash"},{"id":"deepseek-chat"}]}"""); |
| | | using var httpClient = new HttpClient(handler); |
| | | var adapter = new OpenAICompatibleAdapter(httpClient); |
| | | |
| | | var ids = await adapter.ListModelIdsAsync("https://api.deepseek.com", "secret-key"); |
| | | |
| | | Assert.Equal("https://api.deepseek.com/models", handler.LastRequest?.RequestUri?.ToString()); |
| | | Assert.Equal("secret-key", handler.LastRequest?.Headers.Authorization?.Parameter); |
| | | Assert.Equal(["deepseek-v4-flash", "deepseek-chat"], ids); |
| | | } |
| | | |
| | | private static LlmProviderRequest CreateRequest(string? baseUrl = null, string? endpoint = null) |
| | | => new() |
| | | { |
| | | RequestId = "req-1", |
| | |
| | | Id = "deepseek-v4-flash", |
| | | Label = "DeepSeek", |
| | | Provider = "deepseek", |
| | | Endpoint = "https://api.deepseek.com/chat/completions", |
| | | BaseUrl = baseUrl, |
| | | Endpoint = endpoint, |
| | | ApiModel = "deepseek-v4-flash", |
| | | Enabled = true |
| | | }, |
| | |
| | | ApiKey = "test-key" |
| | | }; |
| | | |
| | | private sealed class StaticResponseHandler(HttpStatusCode statusCode, string content) : HttpMessageHandler |
| | | private sealed class RecordingHandler(HttpStatusCode statusCode, string content) : HttpMessageHandler |
| | | { |
| | | public HttpRequestMessage? LastRequest { get; private set; } |
| | | |
| | | protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| | | => Task.FromResult(new HttpResponseMessage(statusCode) |
| | | { |
| | | LastRequest = request; |
| | | return Task.FromResult(new HttpResponseMessage(statusCode) |
| | | { |
| | | Content = new StringContent(content, Encoding.UTF8, "application/json") |
| | | }); |
| | | } |
| | | } |
| | | } |