using System.Text.Json;
using System.Text.RegularExpressions;
using Shouldly;
namespace ClawdDotNet.Web.Tests;
///
/// #764 / #661: The WebUI texts come from language packs (wwwroot/i18n/en.json and
/// de.json). A key missing in one pack shows up as the bare key on screen — in the
/// language nobody tested in. So every run checks the packs, not a person after each change.
///
public sealed partial class LanguagePackTests
{
private static readonly string WwwRoot = Path.Combine(
FindRepositoryRoot(), "src", "ClawdDotNet.Manager.Web", "wwwroot");
private static string FindRepositoryRoot()
{
for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir is not null; dir = dir.Parent)
{
if (File.Exists(Path.Combine(dir.FullName, "ClawdDotNet.slnx")))
return dir.FullName;
}
throw new InvalidOperationException("Repository root not found above " + AppContext.BaseDirectory);
}
private static Dictionary Pack(string language)
=> JsonSerializer.Deserialize>(
File.ReadAllText(Path.Combine(WwwRoot, "i18n", language + ".json")))!;
[GeneratedRegex(@"\{(\w+)\}")]
private static partial Regex Placeholder();
// t("key" … — but not erstelleElement("div": the t must stand on its own.
[GeneratedRegex(@"(? k != "trigger.instructionHint")
.Where(k => !Placeholder().Matches(en[k]).Select(m => m.Value).ToHashSet()
.SetEquals(Placeholder().Matches(de[k]).Select(m => m.Value)))
.ToList();
mismatches.ShouldBeEmpty();
}
[Fact]
public void Every_key_the_code_asks_for_exists()
{
var en = Pack("en");
var missing = Directory.EnumerateFiles(WwwRoot, "*.js", SearchOption.AllDirectories)
.Where(f => !f.Contains(Path.DirectorySeparatorChar + "vendor" + Path.DirectorySeparatorChar))
.SelectMany(f => TranslateCall().Matches(File.ReadAllText(f))
.Select(m => (File: Path.GetFileName(f), Key: m.Groups[1].Value)))
.Where(x => !en.ContainsKey(x.Key))
// The example in the doc comment of i18n.js.
.Where(x => x.Key != "runs.count")
.Select(x => $"{x.File}: {x.Key}")
.Distinct()
.ToList();
missing.ShouldBeEmpty();
}
[Fact]
public void No_text_is_empty()
{
foreach (var language in new[] { "en", "de" })
Pack(language).Where(p => string.IsNullOrWhiteSpace(p.Value)).Select(p => p.Key).ShouldBeEmpty(language);
}
}