Files
Anubis/tests/Anubis.Tests/ContrastTests.cs
Alby96andClaude Opus 5.5 f9d71559e8
ci / test (postgres) (push) Successful in 2m22s
ci / test (sqlite) (push) Failing after 2m27s
ci / image (push) Skipped
feat(server): web server, interface shell, demo mode and Docker image (refs #11 #12 #13 #14 #15 #16 #17)
Phase 1 completed on top of the library foundations:
- Server: startup state machine (config, first run, database down, schema,
  identity, ready) with an error page that retries by itself; the configured
  database is the only one ever opened, and a vanished SQLite file is never
  recreated. REST API and status WebSocket; session cookie auth, CSRF on
  every form and write call; desktop and demo answer only the computer itself.
- Interface: static Blazor rendering with plain forms (no interactive
  runtime), menu, top bar and phone bottom bar as in the mockups, light and
  dark themes with appendix C tokens and a contrast test, Panoramica, Diario,
  Rischio, Impostazioni; service pages for sign-in, first access, desktop
  first run and the database error page with its choices.
- Demo mode (tasks seed-demo): appendix F in a database of its own under the
  temp folder, frozen clock, never touches the configured installation.
- Docker: entrypoint gives /data to PUID:PGID and drops root with setpriv,
  HEALTHCHECK through bash /dev/tcp; proven with SQLite, PostgreSQL, a wrong
  configuration and a database that goes away and comes back.
- tasks screenshots drives the installed Edge/Chrome through DevTools for
  exact 1440 and 390 pixel shots; anubis db export|import on the command line.

Fixes from the independent review: return paths without control characters,
Host check against DNS rebinding, attempt limit counted before the check and
also on the error-page password, one-use TOTP codes and a code to switch the
second factor off, error-page choices serialized with the startup loop,
identity checked before migrating, restore restarts with "Ferma tutto" on,
damaged archives reported instead of crashing. Running the server tests on
PostgreSQL exposed pages rendered before their data arrived; data-loading
components now load first (LoadedComponent).

Decisions in ADR 0007. Tests: 100 on SQLite and on PostgreSQL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 12:40:15 +01:00

89 lines
3.5 KiB
C#

using System.Globalization;
using System.Text.RegularExpressions;
namespace Anubis.Tests;
/// <summary>
/// SPEC 13.4: the seven text colours reach 4.5:1 on the page and on the panels, the control border 3:1 on the panels,
/// in both themes; and the style uses no colour outside appendix C.
/// </summary>
[TestClass]
[TestCategory(Category.Unit)]
public sealed partial class ContrastTests
{
private static readonly string[] _texts = ["--text", "--text-2", "--text-3", "--link", "--gain", "--loss", "--attention"];
private static string Css() => File.ReadAllText(Path.Combine(Repo.Root.FullName, "src", "Anubis.Server", "Assets", "app.css"));
private static Dictionary<string, string> Block(string css, string selector)
{
var start = css.IndexOf(selector + " {", StringComparison.Ordinal);
Assert.IsGreaterThanOrEqualTo(0, start, "Missing block " + selector);
var end = css.IndexOf('}', start);
return Token().Matches(css[start..end]).ToDictionary(m => m.Groups[1].Value, m => m.Groups[2].Value.ToLowerInvariant(), StringComparer.Ordinal);
}
public static IEnumerable<object[]> Themes() => [[":root"], [":root[data-theme=\"dark\"]"], [":root:not([data-theme=\"light\"])"]];
[TestMethod]
[DynamicData(nameof(Themes))]
public void TextAndControlsAreReadable(string selector)
{
var tokens = Block(Css(), selector);
foreach (var text in _texts)
{
foreach (var background in new[] { "--page", "--surface" })
{
var ratio = Ratio(tokens[text], tokens[background]);
Assert.IsGreaterThanOrEqualTo(4.5, ratio, $"{selector} {text} on {background}: {ratio:0.00}");
}
}
Assert.IsGreaterThanOrEqualTo(3.0, Ratio(tokens["--control"], tokens["--surface"]), selector + " control border");
}
[TestMethod]
public void DarkThemeIsTheSameForTheSystemAndTheSetting()
{
var css = Css();
CollectionAssert.AreEquivalent(Block(css, ":root[data-theme=\"dark\"]").ToList(), Block(css, ":root:not([data-theme=\"light\"])").ToList());
}
[TestMethod]
public void OnlyColoursOfAppendixC()
{
var spec = File.ReadAllText(Path.Combine(Repo.Root.FullName, "docs", "SPEC.md"));
var appendix = spec[spec.IndexOf("## Appendice C", StringComparison.Ordinal)..spec.IndexOf("## Appendice D", StringComparison.Ordinal)];
var allowed = Hex().Matches(appendix).Select(m => m.Value.ToLowerInvariant()).ToHashSet(StringComparer.Ordinal);
var used = Hex().Matches(Css()).Select(m => m.Value.ToLowerInvariant()).Distinct().ToList();
Assert.IsNotEmpty(used);
foreach (var colour in used)
{
Assert.Contains(colour, allowed, colour + " is not in appendix C");
}
}
private static double Ratio(string a, string b)
{
var (la, lb) = (Luminance(a), Luminance(b));
return (Math.Max(la, lb) + 0.05) / (Math.Min(la, lb) + 0.05);
}
private static double Luminance(string hex)
{
double Channel(int i)
{
var c = int.Parse(hex.AsSpan(1 + (2 * i), 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture) / 255.0;
return c <= 0.03928 ? c / 12.92 : Math.Pow((c + 0.055) / 1.055, 2.4);
}
return (0.2126 * Channel(0)) + (0.7152 * Channel(1)) + (0.0722 * Channel(2));
}
[GeneratedRegex(@"(--[a-z0-9-]+):\s*(#[0-9a-fA-F]{6})")]
private static partial Regex Token();
[GeneratedRegex(@"#[0-9a-fA-F]{6}(?![0-9a-fA-F])")]
private static partial Regex Hex();
}