using System.Net; using System.Text; using Moli.Desktop.Llm; namespace Moli.Desktop.Tests.Llm; public class OpenAICompatibleAdapterTests { [Fact] public async Task StreamAsync_IncludesProviderErrorMessageWhenRequestFails() { using var httpClient = new HttpClient(new StaticResponseHandler( 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(async () => { await adapter.StreamAsync(CreateRequest()).ToListAsync(); }); Assert.Equal(LlmErrorCodes.EndpointUnavailable, exception.Code); Assert.Contains("LLM provider returned 400", exception.Message); Assert.Contains("Invalid max_tokens value", exception.Message); } private static LlmProviderRequest CreateRequest() => new() { RequestId = "req-1", Model = new LlmModelConfig { Id = "deepseek-v4-flash", Label = "DeepSeek", Provider = "deepseek", Endpoint = "https://api.deepseek.com/chat/completions", ApiModel = "deepseek-v4-flash", Enabled = true }, Messages = [new LlmMessage { Role = "user", Content = "ping" }], Options = new LlmRequestOptions(), ApiKey = "test-key" }; private sealed class StaticResponseHandler(HttpStatusCode statusCode, string content) : HttpMessageHandler { protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) => Task.FromResult(new HttpResponseMessage(statusCode) { Content = new StringContent(content, Encoding.UTF8, "application/json") }); } }