using System.Text.RegularExpressions; using Xunit; namespace Mimante.Tests; /// /// The user guide (docs/guida, published as the wiki of the repository, as in Febe): every page exists with a title, /// every link between pages points to a page that exists in the wiki format, the sidebar and the home page reach every /// page, and the points Alberto looks for are on the page where he looks for them. /// public sealed partial class GuideTests { /// The pages of the wiki, without «.md», in the order of the sidebar. public static readonly string[] Pages = [ "Home", "Installazione-Docker-e-Unraid", "Primo-avvio", "Aste", "Cerca-e-prodotti", "Come-decide-il-modello", "Paletti-e-rischio", "Storico-ed-esportazioni", "Puntate-gratis", "Impostazioni", "Telegram", "Dati-e-backup", "Sicurezza-e-riservatezza", "Problemi-comuni", "Sviluppo", ]; /// (point, page that must say it, phrases that must all be there), compared without case, emphasis and line breaks. public static TheoryData MandatoryPoints => new() { { "la sessione si incolla nelle Impostazioni", "Primo-avvio", ["impostazioni", "accesso a bidoo", "copia come curl"] }, { "senza utente il bot non punta", "Primo-avvio", ["non punta"] }, { "il registro risponde in HTTP", "Installazione-Docker-e-Unraid", ["--insecure-registry", "ui_password", "db_password", "db_host"] }, { "aggiornare da Bidoo BidBot", "Installazione-Docker-e-Unraid", ["/mnt/user/appdata/mimante", "pg_dump"] }, { "i tre modi delle aste", "Aste", ["ferma", "osserva", "attiva"] }, { "il modello è l'unico decisore", "Come-decide-il-modello", ["paletti"] }, { "l'HALT si toglie a mano", "Paletti-e-rischio", ["halt", "togli il blocco"] }, { "il riscatto è spento di fabbrica", "Puntate-gratis", ["spento di fabbrica"] }, { "Telegram con BotFather", "Telegram", ["@botfather", "/newbot", "chat id"] }, { "chi ha /config ha la sessione", "Sicurezza-e-riservatezza", ["/config/sessione", "internet"] }, { "pubblicare la guida", "Sviluppo", ["docs/guida", "mimante.wiki.git"] }, }; [Fact] public void EveryPageExistsWithATitle() { var guide = GuideFolder(); foreach (var page in Pages) { var path = Path.Combine(guide, page + ".md"); Assert.True(File.Exists(path), $"manca la pagina {page}"); Assert.StartsWith("# ", File.ReadLines(path).First()); } Assert.True(File.Exists(Path.Combine(guide, "_Sidebar.md")), "manca _Sidebar.md"); var unknown = Directory.EnumerateFiles(guide, "*.md") .Select(Path.GetFileNameWithoutExtension) .Where(name => name != "_Sidebar" && !Pages.Contains(name)) .ToList(); Assert.True(unknown.Count == 0, "pagine non elencate in GuideTests.Pages: " + string.Join(", ", unknown)); } [Fact] public void EveryWikiLinkPointsToAnExistingPageAndEveryPageIsReachable() { var guide = GuideFolder(); var problems = new List(); var linkedFrom = new Dictionary>(StringComparer.Ordinal); foreach (var file in Directory.EnumerateFiles(guide, "*.md")) { var name = Path.GetFileNameWithoutExtension(file); var targets = new HashSet(StringComparer.Ordinal); // Code is not a link: «`[Testo](Nome-Pagina)`» explains the format. var text = InlineCode().Replace(FencedCode().Replace(File.ReadAllText(file), string.Empty), string.Empty); foreach (Match match in Link().Matches(text)) { var target = match.Groups["target"].Value; if (target.StartsWith("http://", StringComparison.Ordinal) || target.StartsWith("https://", StringComparison.Ordinal)) continue; if (!WikiPage().IsMatch(target)) problems.Add($"{name}: «{target}» non è il nome di una pagina del wiki (niente .md, cartelle o ancore)"); else if (target == "_Sidebar" || !File.Exists(Path.Combine(guide, target + ".md"))) problems.Add($"{name}: «{target}» non esiste"); targets.Add(target); } linkedFrom[name] = targets; } Assert.True(problems.Count == 0, string.Join("\n", problems)); foreach (var page in Pages) { Assert.True(linkedFrom["_Sidebar"].Contains(page), $"la barra laterale non porta a {page}"); if (page != "Home") Assert.True(linkedFrom["Home"].Contains(page), $"l'indice della pagina iniziale non porta a {page}"); } } [Fact] public void ImagesComeFromTheScreenshotsOfTheRepository() { var guide = GuideFolder(); var shots = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(guide))!, "docs", "schermate"); var problems = new List(); foreach (var file in Directory.EnumerateFiles(guide, "*.md")) { foreach (Match match in Image().Matches(File.ReadAllText(file))) { var target = match.Groups["target"].Value; const string prefix = "https://gitea.encke-hake.ts.net/Alby96/Mimante/raw/branch/main/docs/schermate/"; if (!target.StartsWith(prefix, StringComparison.Ordinal)) problems.Add($"{Path.GetFileName(file)}: immagine fuori da docs/schermate: {target}"); else if (!File.Exists(Path.Combine(shots, target[prefix.Length..]))) problems.Add($"{Path.GetFileName(file)}: la schermata {target[prefix.Length..]} non esiste"); } } Assert.True(problems.Count == 0, string.Join("\n", problems)); } [Theory] [MemberData(nameof(MandatoryPoints))] public void TheGuideSaysWhatAlbertoLooksFor(string point, string page, string[] phrases) { var text = Normalize(File.ReadAllText(Path.Combine(GuideFolder(), page + ".md"))); var missing = phrases.Where(p => !text.Contains(Normalize(p), StringComparison.Ordinal)).ToList(); Assert.True(missing.Count == 0, $"«{point}»: in {page} mancano: {string.Join(" | ", missing)}"); } /// Lower case, typographic apostrophes as plain ones, no emphasis or code marks, one space for any run of blanks. public static string Normalize(string text) { var plain = text.Replace("**", "", StringComparison.Ordinal).Replace("`", "", StringComparison.Ordinal).Replace('’', '\'').ToLowerInvariant(); return Blanks().Replace(plain, " "); } private static string GuideFolder() { for (var d = new DirectoryInfo(AppContext.BaseDirectory); d != null; d = d.Parent) if (File.Exists(Path.Combine(d.FullName, "Mimante.slnx"))) return Path.Combine(d.FullName, "docs", "guida"); throw new InvalidOperationException("Mimante.slnx not found above " + AppContext.BaseDirectory); } [GeneratedRegex(@"(?[^)\s]+)\)")] private static partial Regex Link(); [GeneratedRegex(@"!\[[^\]]*\]\((?[^)\s]+)\)")] private static partial Regex Image(); [GeneratedRegex(@"^_?[A-Za-z0-9]+(-[A-Za-z0-9]+)*$")] private static partial Regex WikiPage(); [GeneratedRegex(@"\s+")] private static partial Regex Blanks(); [GeneratedRegex(@"^```.*?^```", RegexOptions.Singleline | RegexOptions.Multiline)] private static partial Regex FencedCode(); [GeneratedRegex(@"`[^`\n]*`")] private static partial Regex InlineCode(); }