using System.Runtime.CompilerServices;
|
using Microsoft.Extensions.Logging.Abstractions;
|
using Moli.Desktop.Configuration;
|
using Moli.Desktop.Diagnostics;
|
using Moli.Desktop.Host;
|
using Moli.Desktop.Interop;
|
using Moli.Desktop.Layout;
|
using Moli.Desktop.Llm;
|
using Moli.Desktop.Plugins;
|
using Moli.Desktop.Runtime;
|
using Moli.Desktop.Tests.Interop;
|
|
namespace Moli.Desktop.Tests.Runtime;
|
|
public class DesktopRuntimeCoordinatorTests
|
{
|
[Fact]
|
public async Task SetEditModeAsync_PersistsSettingAndBroadcastsStatus()
|
{
|
var fixture = await RuntimeFixture.CreateAsync();
|
var events = new List<RuntimeEvent>();
|
using var subscription = fixture.Events.Subscribe(runtimeEvent =>
|
{
|
events.Add(runtimeEvent);
|
return Task.CompletedTask;
|
});
|
await fixture.Runtime.StartAsync();
|
|
var result = await fixture.Runtime.SetEditModeAsync(true);
|
|
Assert.True(result.Succeeded);
|
Assert.True(result.Value!.IsEditMode);
|
Assert.True(fixture.Surface.EditModeEnabled);
|
var settings = await fixture.Configuration.LoadAppSettingsAsync();
|
Assert.True(settings.Runtime.EditMode);
|
Assert.Contains(events, runtimeEvent => runtimeEvent.Name == "app.editModeChanged");
|
Assert.Contains(events, runtimeEvent => runtimeEvent.Name == "app.statusChanged");
|
}
|
|
[Fact]
|
public async Task UpdateWidgetLayoutAsync_ConstrainsAppliesSavesAndBroadcasts()
|
{
|
var fixture = await RuntimeFixture.CreateAsync();
|
var events = new List<RuntimeEvent>();
|
using var subscription = fixture.Events.Subscribe(runtimeEvent =>
|
{
|
events.Add(runtimeEvent);
|
return Task.CompletedTask;
|
});
|
await fixture.Runtime.StartAsync();
|
|
var result = await fixture.Runtime.UpdateWidgetLayoutAsync(
|
fixture.WidgetId,
|
new WidgetLayoutPatch { Bounds = new RectD(-5000, -5000, 10, 10), Opacity = 0 });
|
|
Assert.True(result.Succeeded);
|
Assert.NotNull(fixture.Surface.LastAppliedLayout);
|
Assert.Equal(LayoutManager.MinWidth, fixture.Surface.LastAppliedLayout.Bounds.Width);
|
Assert.Equal(LayoutManager.MinOpacity, fixture.Surface.LastAppliedLayout.Opacity);
|
Assert.Contains(events, runtimeEvent => runtimeEvent.Name == "widget.layoutChanged");
|
|
var saved = await fixture.Configuration.LoadLayoutAsync();
|
var savedLayout = Assert.Single(saved.Instances);
|
Assert.Equal(LayoutManager.MinWidth, savedLayout.Bounds.Width);
|
Assert.Equal(LayoutManager.MinOpacity, savedLayout.Opacity);
|
}
|
|
[Fact]
|
public async Task UpdateWidgetLayoutAsync_WhenGlobalLockedRejectsGeometryPatch()
|
{
|
var fixture = await RuntimeFixture.CreateAsync();
|
await fixture.Runtime.StartAsync();
|
await fixture.Runtime.SetGlobalLockedAsync(true);
|
|
var result = await fixture.Runtime.UpdateWidgetLayoutAsync(
|
fixture.WidgetId,
|
new WidgetLayoutPatch { Bounds = new RectD(20, 20, 320, 160) });
|
|
Assert.False(result.Succeeded);
|
Assert.Equal("WidgetLayoutLocked", result.ErrorCode);
|
Assert.Equal(2, fixture.Surface.AppliedLayoutCount);
|
Assert.True(fixture.Surface.LastAppliedLayout?.IsLocked);
|
Assert.Equal(new RectD(100, 100, 320, 160), fixture.Surface.LastAppliedLayout?.Bounds);
|
}
|
|
[Fact]
|
public async Task StartAsync_RestoresRuntimeFlagsBeforeCreatingWidgetSurfaces()
|
{
|
var fixture = await RuntimeFixture.CreateAsync(new RuntimeSettings
|
{
|
EditMode = true,
|
SafeInputMode = true
|
});
|
|
var result = await fixture.Runtime.StartAsync();
|
|
Assert.True(result.Succeeded);
|
Assert.True(fixture.Surface.EditModeEnabled);
|
Assert.True(fixture.Surface.SafeInputModeEnabled);
|
Assert.Equal(1, fixture.Surface.AppliedLayoutCount);
|
}
|
|
private sealed class RuntimeFixture
|
{
|
private RuntimeFixture(
|
IDesktopRuntime runtime,
|
JsonConfigurationService configuration,
|
RuntimeEventBus events,
|
FakeRuntimeSurfaceController surface,
|
WidgetInstanceId widgetId)
|
{
|
Runtime = runtime;
|
Configuration = configuration;
|
Events = events;
|
Surface = surface;
|
WidgetId = widgetId;
|
}
|
|
public IDesktopRuntime Runtime { get; }
|
public JsonConfigurationService Configuration { get; }
|
public RuntimeEventBus Events { get; }
|
public FakeRuntimeSurfaceController Surface { get; }
|
public WidgetInstanceId WidgetId { get; }
|
|
public static async Task<RuntimeFixture> CreateAsync(RuntimeSettings? runtimeSettings = null)
|
{
|
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 events = new RuntimeEventBus();
|
var surface = new FakeRuntimeSurfaceController();
|
var pluginManager = new PluginManager(paths, configuration, new PluginManifestValidator(), diagnostics, new PluginStateMachine());
|
var layoutManager = new LayoutManager(configuration, diagnostics);
|
var widgetId = new WidgetInstanceId("widget-01");
|
|
await CreatePluginAsync(paths);
|
if (runtimeSettings is not null)
|
{
|
await configuration.SaveAppSettingsAsync(new AppSettingsDocument
|
{
|
Runtime = runtimeSettings
|
});
|
}
|
|
await configuration.SaveLayoutAsync(new LayoutDocument
|
{
|
Monitors =
|
[
|
new MonitorSnapshot
|
{
|
Monitors = [PrimaryMonitor()]
|
}
|
],
|
Instances =
|
[
|
new WidgetLayout
|
{
|
InstanceId = widgetId,
|
PluginId = new PluginId("sample.clock"),
|
MonitorId = new MonitorId("primary"),
|
Bounds = new RectD(100, 100, 320, 160)
|
}
|
]
|
});
|
|
var runtime = new DesktopRuntimeCoordinator(
|
configuration,
|
pluginManager,
|
layoutManager,
|
new FakeWin32InteropService(),
|
diagnostics,
|
events,
|
surface,
|
new DisplayChangeMonitor(),
|
new FakeLlmGateway());
|
|
return new RuntimeFixture(runtime, configuration, events, surface, widgetId);
|
}
|
|
private static async Task CreatePluginAsync(DesktopPaths paths)
|
{
|
var directory = Path.Combine(paths.PluginDirectory, "sample.clock");
|
Directory.CreateDirectory(directory);
|
await File.WriteAllTextAsync(Path.Combine(directory, "index.html"), "<!doctype html><title>Clock</title>");
|
await File.WriteAllTextAsync(
|
Path.Combine(directory, "plugin.json"),
|
"""
|
{
|
"id": "sample.clock",
|
"name": "Clock",
|
"version": "1.0.0",
|
"type": "widget",
|
"entry": "index.html",
|
"defaultLayout": {
|
"width": 320,
|
"height": 160,
|
"opacity": 1.0
|
}
|
}
|
""");
|
}
|
|
private static MonitorInfo PrimaryMonitor() => new()
|
{
|
Id = new MonitorId("primary"),
|
Bounds = new RectD(0, 0, 1920, 1080),
|
WorkArea = new RectD(0, 0, 1920, 1040),
|
IsPrimary = true
|
};
|
}
|
|
private sealed class FakeRuntimeSurfaceController : IRuntimeSurfaceController
|
{
|
public bool EditModeEnabled { get; private set; }
|
public bool SafeInputModeEnabled { get; private set; }
|
public WidgetLayout? LastAppliedLayout { get; private set; }
|
public int AppliedLayoutCount { get; private set; }
|
|
public Task ApplyEditModeAsync(bool enabled, CancellationToken cancellationToken = default)
|
{
|
EditModeEnabled = enabled;
|
return Task.CompletedTask;
|
}
|
|
public Task ApplySafeInputModeAsync(bool enabled, CancellationToken cancellationToken = default)
|
{
|
SafeInputModeEnabled = enabled;
|
return Task.CompletedTask;
|
}
|
|
public Task ApplyWidgetLayoutAsync(WidgetLayout layout, CancellationToken cancellationToken = default)
|
{
|
LastAppliedLayout = layout;
|
AppliedLayoutCount++;
|
return Task.CompletedTask;
|
}
|
|
public Task EnableWallpaperAsync(WallpaperRuntimeState wallpaper, CancellationToken cancellationToken = default) => Task.CompletedTask;
|
|
public Task DisableWallpaperAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
|
|
public Task ShutdownAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
|
|
public Task SyncAllLayoutsAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
|
|
public Task SyncWallpaperMonitorsAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
|
|
public Task ReattachDesktopLayersAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
|
}
|
|
private sealed class FakeLlmGateway : ILlmGatewayService
|
{
|
public Task<IReadOnlyList<LlmModelSummary>> GetModelsAsync(CancellationToken cancellationToken = default)
|
=> Task.FromResult<IReadOnlyList<LlmModelSummary>>([]);
|
|
public Task<IReadOnlyList<LlmModelSummary>> GetModelsAsync(LlmPluginRuntimeConfig runtimeConfig, CancellationToken cancellationToken = default)
|
=> Task.FromResult<IReadOnlyList<LlmModelSummary>>([]);
|
|
public Task<IReadOnlyList<AiChatProjectSummary>> GetProjectsAsync(LlmPluginRuntimeConfig runtimeConfig, CancellationToken cancellationToken = default)
|
=> Task.FromResult<IReadOnlyList<AiChatProjectSummary>>([]);
|
|
public Task<AiChatMemoryCapabilities> GetMemoryCapabilitiesAsync(LlmPluginRuntimeConfig runtimeConfig, CancellationToken cancellationToken = default)
|
=> Task.FromResult(new AiChatMemoryCapabilities());
|
|
public Task<AiChatJsonResult> GetMemoriesAsync(LlmPluginRuntimeConfig runtimeConfig, AiChatGetMemoriesRequest request, CancellationToken cancellationToken = default)
|
=> Task.FromResult(new AiChatJsonResult());
|
|
public Task<AiChatJsonResult> CreateMemoryAsync(LlmPluginRuntimeConfig runtimeConfig, AiChatCreateMemoryRequest request, CancellationToken cancellationToken = default)
|
=> Task.FromResult(new AiChatJsonResult());
|
|
public Task DeleteMemoryAsync(LlmPluginRuntimeConfig runtimeConfig, string memoryId, CancellationToken cancellationToken = default) => Task.CompletedTask;
|
|
public Task<AiChatJsonResult> RecallMemoriesAsync(LlmPluginRuntimeConfig runtimeConfig, AiChatRecallMemoriesRequest request, CancellationToken cancellationToken = default)
|
=> Task.FromResult(new AiChatJsonResult());
|
|
public async IAsyncEnumerable<LlmStreamEvent> StreamChatAsync(LlmChatRequest request, [EnumeratorCancellation] CancellationToken cancellationToken = default)
|
{
|
yield break;
|
}
|
|
public Task CancelAsync(string requestId, CancellationToken cancellationToken = default) => Task.CompletedTask;
|
|
public Task CancelPluginRequestsAsync(string pluginId, CancellationToken cancellationToken = default) => Task.CompletedTask;
|
}
|
}
|