From 4df6165fa93c7caeea86385312415e312de4d11f Mon Sep 17 00:00:00 2001
From: Moli <13026959681@163.com>
Date: 星期二, 05 五月 2026 01:10:39 +0800
Subject: [PATCH] 暂存

---
 src/Moli.Desktop.Tests/Llm/LlmModelRegistryTests.cs |   87 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 87 insertions(+), 0 deletions(-)

diff --git a/src/Moli.Desktop.Tests/Llm/LlmModelRegistryTests.cs b/src/Moli.Desktop.Tests/Llm/LlmModelRegistryTests.cs
new file mode 100644
index 0000000..03c53b1
--- /dev/null
+++ b/src/Moli.Desktop.Tests/Llm/LlmModelRegistryTests.cs
@@ -0,0 +1,87 @@
+using Microsoft.Extensions.Logging.Abstractions;
+using Moli.Desktop.Diagnostics;
+using Moli.Desktop.Host;
+using Moli.Desktop.Llm;
+
+namespace Moli.Desktop.Tests.Llm;
+
+public class LlmModelRegistryTests
+{
+    [Fact]
+    public async Task LoadAsync_CreatesDefaultLlmModelDocument()
+    {
+        var (registry, paths, _) = CreateRegistry();
+
+        var document = await registry.LoadAsync();
+
+        Assert.Equal(1, document.SchemaVersion);
+        Assert.True(File.Exists(Path.Combine(paths.ConfigDirectory, LlmModelRegistry.FileName)));
+    }
+
+    [Fact]
+    public async Task LoadAsync_FiltersInvalidAndDuplicateModels()
+    {
+        var (registry, paths, diagnostics) = CreateRegistry();
+        Directory.CreateDirectory(paths.ConfigDirectory);
+        await File.WriteAllTextAsync(Path.Combine(paths.ConfigDirectory, LlmModelRegistry.FileName), """
+        {
+          "schemaVersion": 1,
+          "defaultModel": "valid",
+          "models": [
+            {
+              "id": "valid",
+              "label": "Valid",
+              "provider": "openai-compatible",
+              "endpoint": "http://127.0.0.1:11434/v1/chat/completions",
+              "apiModel": "qwen",
+              "enabled": true
+            },
+            {
+              "id": "valid",
+              "label": "Duplicate",
+              "provider": "openai-compatible",
+              "endpoint": "http://127.0.0.1:11434/v1/chat/completions",
+              "apiModel": "qwen",
+              "enabled": true
+            },
+            {
+              "id": "bad-url",
+              "label": "Bad",
+              "provider": "openai-compatible",
+              "endpoint": "file:///tmp/model",
+              "apiModel": "qwen",
+              "enabled": true
+            }
+          ]
+        }
+        """);
+
+        var document = await registry.LoadAsync();
+
+        Assert.Single(document.Models);
+        Assert.Equal("valid", document.DefaultModel);
+        Assert.Contains(diagnostics.Snapshot(), entry => entry.Code == "LlmModelInvalid");
+    }
+
+    [Fact]
+    public async Task LoadAsync_BacksUpInvalidJsonAndRecovers()
+    {
+        var (registry, paths, diagnostics) = CreateRegistry();
+        Directory.CreateDirectory(paths.ConfigDirectory);
+        await File.WriteAllTextAsync(Path.Combine(paths.ConfigDirectory, LlmModelRegistry.FileName), "{ broken");
+
+        var document = await registry.LoadAsync();
+
+        Assert.Equal(1, document.SchemaVersion);
+        Assert.NotEmpty(Directory.GetFiles(paths.ConfigDirectory, "llm-models.invalid.*.json"));
+        Assert.Contains(diagnostics.Snapshot(), entry => entry.Code == DiagnosticCodes.ConfigurationRecovered);
+    }
+
+    private static (LlmModelRegistry Registry, DesktopPaths Paths, DiagnosticsService Diagnostics) CreateRegistry()
+    {
+        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 LlmModelRegistry(paths, diagnostics), paths, diagnostics);
+    }
+}

--
Gitblit v1.9.1