PlotDirector/PlotLine.Tests/AppearanceTests.cs

53 lines
3.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 != 7 || !ApplicationAppearance.IsValid(ApplicationAppearance.DefaultTheme) || ApplicationAppearance.IsValid("dark")) throw new Exception("All seven 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 (!store.GetAsync(5).Result.ShowProjectContextMenu || !store.GetAsync(5).Result.ShowBookContextMenu) throw new Exception("Application bar menus default visible.");
store.SaveApplicationBarAsync(5, false, true).GetAwaiter().GetResult();
if (store.GetAsync(5).Result.ShowProjectContextMenu || !store.GetAsync(5).Result.ShowBookContextMenu) throw new Exception("Application bar preferences are independent per user.");
controller.Theme("plotdirector").GetAwaiter().GetResult();
if(store.GetAsync(4).Result.ThemeKey != "plotdirector") throw new Exception("House theme must persist through normal selection.");
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; }
public Task SaveApplicationBarAsync(int id, bool showProject, bool showBook)
{
var value = GetAsync(id).Result;
value.ShowProjectContextMenu = showProject;
value.ShowBookContextMenu = showBook;
return Task.CompletedTask;
}
}