Files
Khaemhat/tests/Khaemhat.Web.Tests/Shell/HelpTests.cs
T
Alby96andClaude Opus 5.5 f415dc37a8 feat: in-app help, notifications panel and global search (#32)
F1 and "?" now open the wiki page of the screen inside the application
(/aiuto, mockup 011) from a copy of the wiki shipped next to the program,
so help works offline and always matches the installed version. Markdown
is converted in-house and fully encoded; images of the repository are
served from the copy. KHAEMHAT_HELP_URL sets the online wiki behind
"Apri nel wiki"; "Segnala un problema" opens a new issue of that repo.
A test checks that every page and menu entry has an existing wiki page.

The bell opens a notifications panel (mockup 010) with all/unread
filters and mark read. Broadcast notifications are now read per user
through sys_notification_reads / notification_reads instead of one
shared read_at.

The global search asks ISearchProvider implementations through
GlobalSearchService after a short pause; the first provider finds help
pages and sections, # and @ say which modules will fill them.

Escape also closes a side panel when the focus fell out of it.
Decisions in ADR 0023.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 19:39:17 +01:00

163 lines
6.8 KiB
C#

using System.Reflection;
using System.Text.RegularExpressions;
using Khaemhat.Web.Help;
using Khaemhat.Web.Shell;
using Microsoft.AspNetCore.Components;
namespace Khaemhat.Web.Tests.Shell;
/// <summary>The help of SPEC §17 (issue #32, mockup 011): the wiki shipped with the application and its links.</summary>
[TestClass]
public sealed partial class HelpTests
{
private static readonly WikiLinks Links = new(page => "aiuto/" + page, url => url.EndsWith("local.png", StringComparison.Ordinal) ? "aiuto/media/local.png" : null);
private static readonly WikiLibrary Wiki = new(Path.Combine(AppContext.BaseDirectory, "help"));
[TestMethod]
public void Markdown_Blocks()
{
var html = WikiMarkdown.ToHtml(
"""
# Titolo *pagina*
> **Stato:** bozza · **Revisione:** oggi
Un paragrafo
su due righe.
## Come si usa
1. Primo
2. Secondo
- annidato
- altro
3. Terzo
| Campo | Regola |
|---|---|
| Codice | `a\|b` e **grassetto** |
> Nota importante.
```
<script>alert(1)</script>
```
---
""",
Links);
StringAssert.Contains(html, "<h1 id=\"titolo-pagina\">Titolo <em>pagina</em></h1>");
StringAssert.Contains(html, "<div class=\"kh-help-meta\"><p><strong>Stato:</strong> bozza");
StringAssert.Contains(html, "<p>Un paragrafo su due righe.</p>");
StringAssert.Contains(html, "<h2 id=\"come-si-usa\">Come si usa</h2>");
StringAssert.Contains(html, "<ol><li>Primo</li><li>Secondo<ul><li>annidato</li><li>altro</li></ul>\n</li><li>Terzo</li></ol>");
StringAssert.Contains(html, "<th>Campo</th><th>Regola</th>");
StringAssert.Contains(html, "<td><code>a|b</code> e <strong>grassetto</strong></td>");
StringAssert.Contains(html, "<div class=\"callout info\"><div><p>Nota importante.</p>");
StringAssert.Contains(html, "<pre><code>&lt;script&gt;alert(1)&lt;/script&gt;</code></pre>");
StringAssert.Contains(html, "<hr />");
}
[TestMethod]
public void Markdown_LinksAndImages_AreSafe()
{
var html = WikiMarkdown.ToHtml(
"Vedi [Anagrafiche](Anagrafiche), [clienti](Anagrafiche#clienti), [sito](https://example.com), [qui](#come-si-usa), "
+ "[male](javascript:alert) e <b>non HTML</b>. ![Mockup](https://host/raw/branch/main/local.png) ![Fuori](https://host/x.png) ![Rotta](ftp://x)",
Links);
StringAssert.Contains(html, "<a href=\"aiuto/Anagrafiche\">Anagrafiche</a>");
StringAssert.Contains(html, "<a href=\"aiuto/Anagrafiche#clienti\">clienti</a>");
StringAssert.Contains(html, "<a href=\"https://example.com\" target=\"_blank\" rel=\"noopener\">sito</a>");
StringAssert.Contains(html, "<a href=\"#come-si-usa\">qui</a>");
Assert.DoesNotContain("javascript:", html);
StringAssert.Contains(html, "male e &lt;b&gt;non HTML&lt;/b&gt;");
StringAssert.Contains(html, "<img src=\"aiuto/media/local.png\" alt=\"Mockup\" loading=\"lazy\" />");
StringAssert.Contains(html, "<img src=\"https://host/x.png\" alt=\"Fuori\"");
StringAssert.Contains(html, "Rotta");
Assert.DoesNotContain("ftp://", html);
}
[TestMethod]
public void Wiki_IsShipped_WithItsIndex_AndEveryPageConverts()
{
Assert.IsTrue(Wiki.IsAvailable, "The build copies docs/wiki next to the program.");
Assert.IsNotEmpty(Wiki.Sidebar);
foreach (var name in Wiki.PageNames)
{
var page = Wiki.Get(name);
Assert.IsNotNull(page, name);
Assert.IsFalse(string.IsNullOrWhiteSpace(page.Title), name);
Assert.DoesNotContain("<script", page.Html, name);
}
Assert.IsNotNull(Wiki.MediaFile("docs/mockups/thumbs/004-dashboard.png"));
Assert.IsNull(Wiki.MediaFile("../wiki/Home.md"));
Assert.IsNull(Wiki.MediaFile("../../../../Windows/win.ini"));
}
[TestMethod]
public void EveryLinkBetweenWikiPages_FindsItsPage()
{
var broken = new List<string>();
foreach (var name in Wiki.PageNames)
{
foreach (Match link in PageLink().Matches(Wiki.Get(name)!.Html))
{
if (!Wiki.Exists(link.Groups[1].Value))
{
broken.Add(name + " -> " + link.Groups[1].Value);
}
}
}
Assert.IsEmpty(broken, string.Join(", ", broken));
}
[TestMethod]
public void EveryPage_AndEveryMenuEntry_HasAHelpPageThatExists()
{
var routes = typeof(HelpLinks).Assembly.GetTypes()
.Where(t => typeof(IComponent).IsAssignableFrom(t))
.SelectMany(t => t.GetCustomAttributes<RouteAttribute>())
.Select(r => "/" + r.Template.Split('{')[0].Trim('/'))
.Concat(MenuCatalog.All.Select(i => i.Href))
.Distinct()
.ToList();
Assert.IsGreaterThan(20, routes.Count);
var missing = routes.Where(r => !Wiki.Exists(HelpLinks.PageFor(r))).Select(r => r + " -> " + HelpLinks.PageFor(r)).ToList();
Assert.IsEmpty(missing, string.Join(", ", missing));
Assert.AreEqual("aiuto/Dashboard", HelpLinks.LocalFor("/"));
Assert.AreEqual("aiuto/Anagrafiche", HelpLinks.LocalFor("/anagrafiche/clienti?x=1"));
}
[TestMethod]
public async Task Search_FindsPageTitles_ThenTheirSections()
{
var provider = new HelpSearchProvider(Wiki);
var root = new Khaemhat.Application.Security.Actor("root", Khaemhat.Core.Security.UserLevel.SuperUser, 1, null, null);
var hits = await provider.SearchAsync(root, Khaemhat.Application.Search.SearchQuery.Parse("scorciatoie"), CancellationToken.None);
Assert.AreEqual("aiuto/Interfaccia-e-scorciatoie", hits[0].Href);
Assert.IsTrue(hits.All(h => h.Href.StartsWith("aiuto/", StringComparison.Ordinal)));
Assert.IsTrue(hits.Where(h => h.Detail is not null).All(h => h.Href.Contains('#', StringComparison.Ordinal)));
Assert.IsLessThanOrEqualTo(Khaemhat.Application.Search.GlobalSearchService.PerProvider, hits.Count);
}
[TestMethod]
public void OnlineWiki_AndProblemReports_FollowTheConfiguredAddress()
{
Assert.AreEqual(HelpLinks.DefaultBaseUrl, HelpLinks.BaseUrl(null));
Assert.AreEqual("https://git.example.com/acme/khaemhat/wiki/", HelpLinks.BaseUrl("https://git.example.com/acme/khaemhat/wiki"));
Assert.AreEqual("https://git.example.com/acme/khaemhat/issues/new", HelpLinks.IssuesUrl("https://git.example.com/acme/khaemhat/wiki/"));
Assert.IsNull(HelpLinks.IssuesUrl("https://docs.example.com/"));
}
[GeneratedRegex("href=\"aiuto/([^\"#]+)")]
private static partial Regex PageLink();
}