基于WebView2实现的Win11桌面框架,利用插件自定义
chunhualin1
2026-08-17 9b5711d3124622b1f1b18500d442c76492da877a
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
using Microsoft.Extensions.Logging.Abstractions;
using Moli.Desktop.Configuration;
using Moli.Desktop.Diagnostics;
using Moli.Desktop.Host;
using Moli.Desktop.Plugins;
 
namespace Moli.Desktop.Tests.Plugins;
 
public class PluginManagerTests
{
    [Fact]
    public async Task ScanAsync_RecognizesValidPluginAndPersistsEnabledState()
    {
        var (manager, paths, _) = CreateManager();
        await CreatePluginAsync(paths, "sample.clock", entryExists: true);
 
        var plugins = await manager.ScanAsync();
 
        var plugin = Assert.Single(plugins);
        Assert.Equal(new PluginId("sample.clock"), plugin.Id);
        Assert.Equal(PluginState.Enabled, plugin.State);
        Assert.Equal(Moli.Desktop.Layout.DisplayLayer.Normal, plugin.Manifest.DefaultLayout.DisplayLayer);
 
        var registry = await CreateConfiguration(paths).LoadPluginRegistryAsync();
        Assert.Contains(registry.Plugins, item => item.Id == new PluginId("sample.clock") && item.State == PluginState.Enabled);
    }
 
    [Fact]
    public async Task ScanAsync_RejectsMissingEntryAndRecordsDiagnostic()
    {
        var (manager, paths, diagnostics) = CreateManager();
        await CreatePluginAsync(paths, "sample.clock", entryExists: false);
 
        var plugins = await manager.ScanAsync();
 
        var plugin = Assert.Single(plugins);
        Assert.Equal(PluginState.Invalid, plugin.State);
        Assert.Contains(diagnostics.Snapshot(), entry => entry.Code == DiagnosticCodes.PluginEntryMissing);
    }
 
    [Fact]
    public void StateMachine_RejectsInvalidTransitionWithoutThrowing()
    {
        var plugin = new PluginDescriptor
        {
            Id = new PluginId("sample.clock"),
            Manifest = new PluginManifest
            {
                Id = "sample.clock",
                Name = "Clock",
                Version = "1.0.0",
                Type = PluginType.Widget,
                Entry = "index.html"
            },
            Type = PluginType.Widget,
            State = PluginState.Enabled,
            PluginDirectory = "C:\\plugins\\sample.clock",
            EntryUri = new Uri("https://example.test/index.html")
        };
 
        var result = new PluginStateMachine().Transition(plugin, PluginLifecycleEvent.Resume);
 
        Assert.False(result.Succeeded);
        Assert.Equal("PluginStateTransitionInvalid", result.ErrorCode);
    }
 
    private static (PluginManager Manager, DesktopPaths Paths, DiagnosticsService Diagnostics) CreateManager()
    {
        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);
        var configuration = new JsonConfigurationService(paths, diagnostics);
        var manager = new PluginManager(paths, configuration, new PluginManifestValidator(), diagnostics, new PluginStateMachine());
        return (manager, paths, diagnostics);
    }
 
    private static JsonConfigurationService CreateConfiguration(DesktopPaths paths)
    {
        var diagnostics = new DiagnosticsService(paths, NullLogger<DiagnosticsService>.Instance);
        return new JsonConfigurationService(paths, diagnostics);
    }
 
    private static async Task CreatePluginAsync(DesktopPaths paths, string id, bool entryExists)
    {
        var directory = Path.Combine(paths.PluginDirectory, id);
        Directory.CreateDirectory(directory);
        await File.WriteAllTextAsync(
            Path.Combine(directory, "plugin.json"),
            """
            {
              "id": "sample.clock",
              "name": "Clock",
              "version": "1.0.0",
              "type": "widget",
              "entry": "index.html",
              "defaultLayout": {
                "x": 100,
                "y": 100,
                "width": 320,
                "height": 160,
                "locked": false,
                "clickThrough": false,
                "opacity": 1.0,
                "displayLayer": "normal"
              }
            }
            """);
 
        if (entryExists)
        {
            await File.WriteAllTextAsync(Path.Combine(directory, "index.html"), "<!doctype html><title>Clock</title>");
        }
    }
}