using Moli.Desktop.Diagnostics;
|
using Moli.Desktop.Host;
|
using Moli.Desktop.Llm;
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
namespace Moli.Desktop.Tests.Llm;
|
|
public class LlmGatewayServiceTests
|
{
|
[Fact]
|
public async Task StreamChatAsync_RoutesSelectedModelToMatchingAdapter()
|
{
|
var adapter = new CapturingAdapter("openai-compatible");
|
var gateway = CreateGateway(adapter, new LlmModelConfig
|
{
|
Id = "model-a",
|
Label = "Model A",
|
Provider = "openai-compatible",
|
Endpoint = "http://127.0.0.1:11434/v1/chat/completions",
|
ApiModel = "actual-model",
|
Enabled = true
|
});
|
|
var events = await gateway.StreamChatAsync(new LlmChatRequest
|
{
|
RequestId = "req-1",
|
PluginId = "sample.llm-command-palette",
|
InstanceId = "host-1",
|
ModelId = "model-a",
|
Messages = [new LlmMessage { Role = "user", Content = "hello" }]
|
}).ToListAsync();
|
|
Assert.Equal("http://127.0.0.1:11434/v1/chat/completions", adapter.LastRequest?.Model.Endpoint);
|
Assert.Contains(events, item => item is LlmDeltaEvent { Delta: "ok" });
|
Assert.Contains(events, item => item is LlmCompletedEvent);
|
}
|
|
[Fact]
|
public async Task StreamChatAsync_ReturnsModelNotFoundForDisabledModel()
|
{
|
var gateway = CreateGateway(new CapturingAdapter("openai-compatible"), new LlmModelConfig
|
{
|
Id = "model-a",
|
Label = "Model A",
|
Provider = "openai-compatible",
|
Endpoint = "http://127.0.0.1:11434/v1/chat/completions",
|
ApiModel = "actual-model",
|
Enabled = false
|
});
|
|
var events = await gateway.StreamChatAsync(new LlmChatRequest
|
{
|
RequestId = "req-2",
|
PluginId = "sample.llm-command-palette",
|
InstanceId = "host-1",
|
ModelId = "model-a",
|
Messages = [new LlmMessage { Role = "user", Content = "hello" }]
|
}).ToListAsync();
|
|
var error = Assert.IsType<LlmErrorEvent>(Assert.Single(events));
|
Assert.Equal(LlmErrorCodes.ModelNotFound, error.Code);
|
}
|
|
[Fact]
|
public async Task StreamChatAsync_UsesOpenAICompatibleAdapterForDeepSeekAlias()
|
{
|
var adapter = new CapturingAdapter("openai-compatible");
|
var gateway = CreateGateway(adapter, new LlmModelConfig
|
{
|
Id = "deepseek-chat",
|
Label = "DeepSeek",
|
Provider = "deepseek",
|
Endpoint = "https://api.deepseek.com/chat/completions",
|
ApiModel = "deepseek-chat",
|
Enabled = true
|
});
|
|
var events = await gateway.StreamChatAsync(new LlmChatRequest
|
{
|
RequestId = "req-3",
|
PluginId = "sample.llm-command-palette",
|
InstanceId = "host-1",
|
ModelId = "deepseek-chat",
|
Messages = [new LlmMessage { Role = "user", Content = "hello" }]
|
}).ToListAsync();
|
|
Assert.Equal("deepseek", adapter.LastRequest?.Model.Provider);
|
Assert.Contains(events, item => item is LlmDeltaEvent { Delta: "ok" });
|
}
|
|
private static LlmGatewayService CreateGateway(CapturingAdapter adapter, params LlmModelConfig[] models)
|
{
|
var root = Path.Combine(Path.GetTempPath(), "Moli.Desktop.Tests", Guid.NewGuid().ToString("N"));
|
var paths = new DesktopPaths(Path.Combine(root, "app"), Path.Combine(root, "local"));
|
var diagnostics = new DiagnosticsService(paths, NullLogger<DiagnosticsService>.Instance);
|
return new LlmGatewayService(
|
new InMemoryRegistry(models),
|
new EnvironmentCredentialStore(),
|
new LlmProviderAdapterFactory([adapter]),
|
diagnostics);
|
}
|
|
private sealed class InMemoryRegistry(IEnumerable<LlmModelConfig> models) : ILlmModelRegistry
|
{
|
private readonly List<LlmModelConfig> _models = [.. models];
|
|
public Task<LlmSettingsDocument> LoadAsync(CancellationToken cancellationToken = default)
|
=> Task.FromResult(new LlmSettingsDocument { Models = _models });
|
|
public Task<LlmModelConfig?> GetEnabledModelAsync(string modelId, CancellationToken cancellationToken = default)
|
=> Task.FromResult(_models.FirstOrDefault(model => model.Enabled && model.Id == modelId));
|
}
|
|
private sealed class CapturingAdapter(string provider) : ILlmProviderAdapter
|
{
|
public string Provider { get; } = provider;
|
public LlmProviderRequest? LastRequest { get; private set; }
|
|
public async IAsyncEnumerable<LlmProviderChunk> StreamAsync(
|
LlmProviderRequest request,
|
[System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
|
{
|
await Task.Yield();
|
LastRequest = request;
|
yield return new LlmProviderChunk { Delta = "ok" };
|
yield return new LlmProviderChunk { IsCompleted = true };
|
}
|
}
|
}
|
|
internal static class AsyncEnumerableTestExtensions
|
{
|
public static async Task<List<T>> ToListAsync<T>(this IAsyncEnumerable<T> source)
|
{
|
var results = new List<T>();
|
await foreach (var item in source)
|
{
|
results.Add(item);
|
}
|
|
return results;
|
}
|
}
|