Files
Khaemhat/tests/Khaemhat.Application.Tests/LocalizationAndPreferencesTests.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

126 lines
4.7 KiB
C#

using System.Globalization;
using Khaemhat.Application.Localization;
using Khaemhat.Application.Preferences;
namespace Khaemhat.Application.Tests;
/// <summary>The texts of the interface (SPEC §1.9, §10.8) and the user preferences (SPEC §10.6, §11.12).</summary>
[TestClass]
public sealed class LocalizationAndPreferencesTests
{
private static readonly CultureInfo Italian = CultureInfo.GetCultureInfo("it-IT");
[TestMethod]
public void Texts_FlattenNestedObjects_AndUnderscoreNamesTheParent()
{
var texts = Provider(("a.json", """{"Shell": {"Menu": {"Home": "Home", "Parties": {"_": "Anagrafiche", "Customers": "Clienti"}}}}"""));
Assert.AreEqual("Home", texts["Shell.Menu.Home"]);
Assert.AreEqual("Anagrafiche", texts["Shell.Menu.Parties"]);
Assert.AreEqual("Clienti", texts["Shell.Menu.Parties.Customers"]);
Assert.HasCount(3, texts.Keys);
}
[TestMethod]
public void Texts_MissingKeysAreVisible()
{
var texts = Provider(("a.json", """{"A": "a"}"""));
Assert.AreEqual("[B.C]", texts["B.C"]);
Assert.IsFalse(texts.Contains("B.C"));
Assert.IsTrue(texts.Contains("A"));
}
[TestMethod]
public void Texts_RefuseAKeyDefinedTwice_AcrossFiles()
{
var error = Assert.ThrowsExactly<InvalidDataException>(() => Provider(("a.json", """{"A": {"B": "1"}}"""), ("b.json", """{"A": {"B": "2"}}""")));
StringAssert.Contains(error.Message, "A.B");
StringAssert.Contains(error.Message, "b.json");
}
[TestMethod]
public void Texts_RefuseValuesThatAreNotStrings()
{
Assert.ThrowsExactly<InvalidDataException>(() => Provider(("a.json", """{"A": 3}""")));
Assert.ThrowsExactly<InvalidDataException>(() => Provider(("a.json", """["A"]""")));
}
[TestMethod]
public void Texts_FormatInTheCulture()
{
var texts = Provider(("a.json", """{"Total": "Totale {0:N2}", "Days": "restano {0} giorni"}"""));
Assert.AreEqual("Totale 1.234,50", texts.Format("Total", 1234.5m));
Assert.AreEqual("restano 7 giorni", texts.Format("Days", 7));
Assert.AreEqual("€ 1.234,50", texts.Money(1234.5m));
Assert.AreEqual("13/10/2026", texts.Date(new DateOnly(2026, 10, 13)));
Assert.AreEqual("1.235", texts.Number(1234.5m));
}
[TestMethod]
public void Preferences_RoundTripThroughSettings()
{
var preferences = new UserPreferences
{
Theme = ThemeMode.HighContrast,
Accent = "malachite",
Density = Density.Compact,
TextSize = 120,
TabularNumbers = false,
ReducedMotion = true,
Tooltips = false,
RowHover = false,
MenuCollapsed = true,
ShowDashboard = false,
StartPage = StartPage.Page,
StartPath = "/anagrafiche/clienti",
};
var back = UserPreferences.FromSettings(preferences.ToSettings());
Assert.AreEqual(preferences, back);
Assert.AreEqual("high-contrast", back.ThemeAttribute);
Assert.AreEqual("compact", back.DensityAttribute);
}
[TestMethod]
public void Preferences_IgnoreUnknownOrUnsafeValues()
{
var settings = new Dictionary<string, string?>(StringComparer.Ordinal)
{
[PreferenceKeys.Theme] = "neon",
[PreferenceKeys.Accent] = "<script>",
[PreferenceKeys.TextSize] = "300",
[PreferenceKeys.TabularNumbers] = "forse",
[PreferenceKeys.StartPage] = "page",
[PreferenceKeys.StartPath] = "https://example.com/",
};
var preferences = UserPreferences.FromSettings(settings);
Assert.AreEqual(UserPreferences.Default.Theme, preferences.Theme);
Assert.AreEqual(UserPreferences.Default.Accent, preferences.Accent);
Assert.AreEqual(UserPreferences.Default.TextSize, preferences.TextSize);
Assert.AreEqual(UserPreferences.Default.TabularNumbers, preferences.TabularNumbers);
Assert.IsNull(preferences.StartPath);
}
[TestMethod]
[DataRow("/", true)]
[DataRow("/anagrafiche/clienti", true)]
[DataRow("/profilo?scheda=aspetto", true)]
[DataRow("//evil.example", false)]
[DataRow("/\\evil.example", false)]
[DataRow("https://evil.example", false)]
[DataRow("anagrafiche", false)]
[DataRow("", false)]
[DataRow(null, false)]
public void Preferences_StartPathMustBeLocal(string? path, bool local) =>
Assert.AreEqual(local, UserPreferences.IsLocalPath(path));
private static JsonTextProvider Provider(params (string Name, string Json)[] documents) =>
new(Italian, documents.Select(d => new KeyValuePair<string, string>(d.Name, d.Json)));
}