From 4c78a5d6fd7abb473cbca5892c57ea2fd53fc776 Mon Sep 17 00:00:00 2001 From: Mika Kuns Date: Wed, 2 Sep 2026 11:52:34 +0200 Subject: [PATCH] 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. --- .../EnJsonCoverageTests.cs | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/ClaudeDo.Localization.Tests/EnJsonCoverageTests.cs b/tests/ClaudeDo.Localization.Tests/EnJsonCoverageTests.cs index e9dcd8c9..b587d053 100644 --- a/tests/ClaudeDo.Localization.Tests/EnJsonCoverageTests.cs +++ b/tests/ClaudeDo.Localization.Tests/EnJsonCoverageTests.cs @@ -1,3 +1,4 @@ +using System.Text.Json; using ClaudeDo.Localization; namespace ClaudeDo.Localization.Tests; @@ -16,10 +17,34 @@ public class EnJsonCoverageTests 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] 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)); var enKeys = en!.Strings.Keys.ToHashSet();