Files
Khaemhat/tests/Khaemhat.Web.Tests/Shell/PreferencesStoreTests.cs
T
Alby96andClaude Opus 5.5 cf6f849cc1 feat: application shell, localized texts and user preferences (M3, part 1)
The pages built so far now live inside the shell of the mockups instead of the
M0 placeholder, every interface text comes from Resources/it-IT, and each user
keeps their own look and start page.

- Shell: top bar, collapsible side menu with fly-out sub-menus, status bar and
  tabs that keep each page alive (page type and route values per tab, so a tab
  never shows another page); at most 12 tabs, least-used clean tab evicted.
- One shortcut table with Alt variants for the combinations browsers keep
  (Ctrl+W, Ctrl+Tab, Ctrl+N); command palette for pages and commands.
- ITexts over embedded JSON; tests forbid literal texts in .razor components
  and check that every key used exists.
- User settings in sys_user_settings / user_settings; theme written on the
  server; profile page with live preview, start page, sessions and shortcuts.
- Home, coming-soon pages for later milestones, localized sign-in and error
  pages; ADR 0020; wiki, CHANGELOG, STATUS and data model updated.

Refs #25 #26 #31 #33

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 16:44:59 +01:00

48 lines
2.6 KiB
C#

using Khaemhat.Application.Preferences;
using Khaemhat.Application.Security;
using Khaemhat.Core.Security;
using Khaemhat.Web.Tests.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace Khaemhat.Web.Tests.Shell;
/// <summary>The preferences are kept per user: instance accounts in the system database, company users in their company (issue #31).</summary>
[TestClass]
public sealed class PreferencesStoreTests
{
[TestMethod]
public async Task Preferences_AreSavedPerUser_InTheRightDatabase()
{
var (app, _, company) = await TestInstance.StartAsync(CancellationToken.None);
await using (app)
{
var settings = app.Services.GetRequiredService<UserSettingsService>();
var root = new Actor(TestInstance.SuperUser, UserLevel.SuperUser, 1, null, null);
var companyUser = new Actor("mario", UserLevel.Standard, null, 1, company);
var otherCompanyUser = new Actor("lucia", UserLevel.Standard, null, 2, company);
Assert.AreEqual(UserPreferences.Default, await settings.GetPreferencesAsync(root));
var dark = UserPreferences.Default with { Theme = ThemeMode.Dark, Accent = "ametista", MenuCollapsed = true };
await settings.SavePreferencesAsync(root, dark);
await settings.SavePreferencesAsync(companyUser, UserPreferences.Default with { Density = Density.Compact });
Assert.AreEqual(dark, await settings.GetPreferencesAsync(root));
Assert.AreEqual(Density.Compact, (await settings.GetPreferencesAsync(companyUser)).Density);
Assert.AreEqual(ThemeMode.Auto, (await settings.GetPreferencesAsync(companyUser)).Theme, "Users do not share preferences.");
Assert.AreEqual(UserPreferences.Default, await settings.GetPreferencesAsync(otherCompanyUser));
// A saved value can be changed and removed.
await settings.SetAsync(root, PreferenceKeys.OpenTabs, "{\"active\":\"/\"}");
Assert.AreEqual("{\"active\":\"/\"}", await settings.GetAsync(root, PreferenceKeys.OpenTabs));
await settings.SetAsync(root, PreferenceKeys.OpenTabs, null);
Assert.IsNull(await settings.GetAsync(root, PreferenceKeys.OpenTabs));
// Limits: the key is short, the value at most 64 KB.
await Assert.ThrowsExactlyAsync<ValidationFailedException>(() => settings.SetAsync(root, new string('k', 201), "x"));
await Assert.ThrowsExactlyAsync<ValidationFailedException>(() => settings.SetAsync(root, "ui.big", new string('x', UserSettingsService.MaxValueLength + 1)));
await app.StopAsync();
}
}
}