fix(localization): make locale-parity test able to detect a malformed locale file
LocaleStore.Load silently skips a *.json that fails to parse, so a broken de.json left store.Available with only "en" and the parity loop compared English against itself, always green. Add two guards before the loop: loaded-locale count must match the number of *.json files in the folder, and "de" must be present via TryGet, both with failure messages naming the missing file. Also independently JsonDocument.Parse every *.json so a syntax error surfaces as a syntax error with the file name, not as "locale missing". LocaleStore's catch-and-skip is untouched -- a broken translation must still not crash the app at runtime.
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Text.Json;
|
||||||
using ClaudeDo.Localization;
|
using ClaudeDo.Localization;
|
||||||
|
|
||||||
namespace ClaudeDo.Localization.Tests;
|
namespace ClaudeDo.Localization.Tests;
|
||||||
@@ -16,10 +17,34 @@ public class EnJsonCoverageTests
|
|||||||
throw new DirectoryNotFoundException("Could not locate src/ClaudeDo.Localization/locales");
|
throw new DirectoryNotFoundException("Could not locate src/ClaudeDo.Localization/locales");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void All_locale_files_parse_as_valid_json()
|
||||||
|
{
|
||||||
|
var localesDir = LocalesDir();
|
||||||
|
foreach (var path in Directory.EnumerateFiles(localesDir, "*.json"))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
JsonDocument.Parse(File.ReadAllText(path));
|
||||||
|
}
|
||||||
|
catch (JsonException ex)
|
||||||
|
{
|
||||||
|
Assert.Fail($"{Path.GetFileName(path)} is not valid JSON: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Every_locale_has_same_keys_as_english()
|
public void Every_locale_has_same_keys_as_english()
|
||||||
{
|
{
|
||||||
var store = LocaleStore.Load(LocalesDir());
|
var localesDir = LocalesDir();
|
||||||
|
var jsonFileCount = Directory.EnumerateFiles(localesDir, "*.json").Count();
|
||||||
|
var store = LocaleStore.Load(localesDir);
|
||||||
|
Assert.True(store.Available.Count == jsonFileCount,
|
||||||
|
$"Expected {jsonFileCount} locale(s) to load from {localesDir}, but only {store.Available.Count} loaded " +
|
||||||
|
$"(loaded: {string.Join(", ", store.Available.Select(l => l.Code))}). A *.json file failed to parse.");
|
||||||
|
Assert.True(store.TryGet("de", out _), "Locale 'de' did not load — de.json failed to parse.");
|
||||||
|
|
||||||
Assert.True(store.TryGet("en", out var en));
|
Assert.True(store.TryGet("en", out var en));
|
||||||
var enKeys = en!.Strings.Keys.ToHashSet();
|
var enKeys = en!.Strings.Keys.ToHashSet();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user