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.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.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"), "Clock"); } } }