using Microsoft.Extensions.Logging.Abstractions;
|
using Moli.Desktop.Configuration;
|
using Moli.Desktop.Diagnostics;
|
using Moli.Desktop.Host;
|
using Moli.Desktop.Layout;
|
using Moli.Desktop.Plugins;
|
|
namespace Moli.Desktop.Tests.Configuration;
|
|
public class JsonConfigurationServiceTests
|
{
|
[Fact]
|
public async Task LoadAllAsync_CreatesMissingDocumentsWithSchemaVersion()
|
{
|
var (service, paths, _) = CreateService();
|
|
var snapshot = await service.LoadAllAsync();
|
|
Assert.Equal(1, snapshot.AppSettings.SchemaVersion);
|
Assert.Equal(1, snapshot.Plugins.SchemaVersion);
|
Assert.Equal(1, snapshot.Layout.SchemaVersion);
|
Assert.True(File.Exists(Path.Combine(paths.ConfigDirectory, "appsettings.json")));
|
Assert.True(File.Exists(Path.Combine(paths.ConfigDirectory, "plugins.json")));
|
Assert.True(File.Exists(Path.Combine(paths.ConfigDirectory, "layout.json")));
|
}
|
|
[Fact]
|
public async Task LoadAppSettingsAsync_BacksUpInvalidJsonAndRecoversDefault()
|
{
|
var (service, paths, diagnostics) = CreateService();
|
Directory.CreateDirectory(paths.ConfigDirectory);
|
await File.WriteAllTextAsync(Path.Combine(paths.ConfigDirectory, "appsettings.json"), "{ broken json");
|
|
var document = await service.LoadAppSettingsAsync();
|
|
Assert.Equal(1, document.SchemaVersion);
|
Assert.NotEmpty(Directory.GetFiles(paths.ConfigDirectory, "appsettings.invalid.*.json"));
|
Assert.Contains(diagnostics.Snapshot(), entry => entry.Code == DiagnosticCodes.ConfigurationRecovered);
|
}
|
|
[Fact]
|
public async Task SaveAppSettingsAsync_WritesBackupOnOverwrite()
|
{
|
var (service, paths, _) = CreateService();
|
|
await service.SaveAppSettingsAsync(new AppSettingsDocument());
|
await service.SaveAppSettingsAsync(new AppSettingsDocument
|
{
|
Runtime = new RuntimeSettings { ApiPort = 9000 }
|
});
|
|
Assert.True(File.Exists(Path.Combine(paths.ConfigDirectory, "appsettings.json.bak")));
|
}
|
|
[Fact]
|
public async Task SaveAndLoadLayoutAsync_UsesDisplayLayerJsonNames()
|
{
|
var (service, paths, _) = CreateService();
|
|
await service.SaveLayoutAsync(new LayoutDocument
|
{
|
Instances =
|
[
|
new WidgetLayout
|
{
|
InstanceId = new WidgetInstanceId("widget-01"),
|
PluginId = new PluginId("sample.clock"),
|
MonitorId = new MonitorId("primary"),
|
Bounds = new RectD(0, 0, 320, 160),
|
DisplayLayer = DisplayLayer.AppTop
|
}
|
]
|
});
|
|
var json = await File.ReadAllTextAsync(Path.Combine(paths.ConfigDirectory, "layout.json"));
|
Assert.Contains("\"displayLayer\": \"app-top\"", json);
|
|
var loaded = await service.LoadLayoutAsync();
|
Assert.Equal(DisplayLayer.AppTop, Assert.Single(loaded.Instances).DisplayLayer);
|
}
|
|
private static (JsonConfigurationService Service, DesktopPaths Paths, DiagnosticsService Diagnostics) CreateService()
|
{
|
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 JsonConfigurationService(paths, diagnostics), paths, diagnostics);
|
}
|
}
|