基于WebView2实现的Win11桌面框架,利用插件自定义
chunhualin1
2026-08-14 09d4e2f1a1bc206c80d8d3287d070c70ec7fd63d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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);
        var model = Assert.Single(document.Models);
        Assert.Equal("http://127.0.0.1:11434/v1", model.BaseUrl);
        Assert.Null(model.Endpoint);
        Assert.True(File.Exists(Path.Combine(paths.ConfigDirectory, LlmModelRegistry.FileName)));
        var saved = await File.ReadAllTextAsync(Path.Combine(paths.ConfigDirectory, LlmModelRegistry.FileName));
        Assert.Contains("baseUrl", saved, StringComparison.Ordinal);
        Assert.DoesNotContain("chat/completions", saved, StringComparison.Ordinal);
    }
 
    [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_AcceptsBaseUrlAndLegacyChatEndpoint()
    {
        var (registry, paths, _) = CreateRegistry();
        Directory.CreateDirectory(paths.ConfigDirectory);
        await File.WriteAllTextAsync(Path.Combine(paths.ConfigDirectory, LlmModelRegistry.FileName), """
        {
          "schemaVersion": 1,
          "defaultModel": "from-base",
          "models": [
            {
              "id": "from-base",
              "label": "Base",
              "provider": "openai-compatible",
              "baseUrl": "https://api.deepseek.com",
              "apiModel": "deepseek-v4-flash",
              "enabled": true
            },
            {
              "id": "from-endpoint",
              "label": "Legacy",
              "provider": "openai-compatible",
              "endpoint": "http://127.0.0.1:11434/v1/chat/completions",
              "apiModel": "qwen2.5:14b",
              "enabled": true
            }
          ]
        }
        """);
 
        var document = await registry.LoadAsync();
 
        Assert.Equal(2, document.Models.Count);
        Assert.Equal("https://api.deepseek.com", document.Models[0].BaseUrl);
        Assert.Equal("http://127.0.0.1:11434/v1", document.Models[1].BaseUrl);
        Assert.All(document.Models, model => Assert.Null(model.Endpoint));
    }
 
    [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);
    }
}