41 lines
2.7 KiB
C#
41 lines
2.7 KiB
C#
using PlotLine.Services;
|
|
using PlotLine.Data;
|
|
using PlotLine.Controllers;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
internal static class AppearanceTests
|
|
{
|
|
public static void RegistryAndPersistence()
|
|
{
|
|
if (ApplicationAppearance.Themes.Count != 4 || ApplicationAppearance.IsValid("dark")) throw new Exception("Four explicit theme identifiers required.");
|
|
var store = new AppearanceMemoryPreferences();
|
|
var controller = new AppearanceController(new ToolbarTestUser(4), store);
|
|
controller.Theme("modern-dark").GetAwaiter().GetResult();
|
|
controller.Theme("warm-light", true).GetAwaiter().GetResult();
|
|
if (store.GetAsync(4).Result.ThemeKey != "modern-dark" || store.GetAsync(5).Result.ThemeKey is not null) throw new Exception("Legacy migration must preserve explicit choices and isolate users.");
|
|
controller.Ribbon(true).GetAwaiter().GetResult();
|
|
if (!store.GetAsync(4).Result.RibbonCollapsed || store.GetAsync(4).Result.ThemeKey != "modern-dark") throw new Exception("Ribbon preference must not overwrite theme.");
|
|
if (controller.Theme("invalid").Result is not BadRequestResult) throw new Exception("Invalid themes rejected.");
|
|
if (ApplicationRibbon.ActiveCategory("StoryCalendar", "Calendar") != "story" || ApplicationRibbon.ActiveCategory("Books", "Overview") != "book") throw new Exception("Category maps feature routes.");
|
|
foreach (var command in ApplicationRibbon.Categories.SelectMany(x => x.Groups).SelectMany(x => x.Commands))
|
|
{
|
|
_ = ApplicationRibbon.Icon(command.IconKey);
|
|
if (command.ToolbarKey is not null && !ToolbarCommands.All.Any(x => x.Key == command.ToolbarKey)) throw new Exception("Registry key invalid.");
|
|
var type = typeof(AppearanceController).Assembly.GetType($"PlotLine.Controllers.{command.Controller}Controller");
|
|
if (type is null || !type.GetMethods().Any(x => x.Name == command.Action)) throw new Exception($"Missing destination: {command.Controller}/{command.Action}");
|
|
}
|
|
}
|
|
}
|
|
internal sealed class AppearanceMemoryPreferences : IAppearanceRepository
|
|
{
|
|
private readonly System.Collections.Concurrent.ConcurrentDictionary<int, ApplicationAppearance> values = new();
|
|
public Task<ApplicationAppearance> GetAsync(int id) => Task.FromResult(values.GetOrAdd(id, _ => new()));
|
|
public Task SaveThemeAsync(int id, string theme, bool onlyIfUnset = false)
|
|
{
|
|
var value = GetAsync(id).Result;
|
|
lock(value) { if (!onlyIfUnset || value.ThemeKey is null) value.ThemeKey = theme; }
|
|
return Task.CompletedTask;
|
|
}
|
|
public Task SaveRibbonAsync(int id, bool collapsed) { GetAsync(id).Result.RibbonCollapsed = collapsed; return Task.CompletedTask; }
|
|
}
|