using System.Text.Json.Nodes; using Marathon.UI.Services; namespace Marathon.UI.Tests; public sealed class JsonSettingsWriterTests : IDisposable { private readonly string _tempPath = Path.Combine(Path.GetTempPath(), $"marathon-settings-{Guid.NewGuid():N}.json"); [Fact] public async Task Save_writes_section_and_creates_file() { var writer = new JsonSettingsWriter(_tempPath); await writer.SaveSectionAsync("Localization", new LocalizationOptions { DefaultCulture = "en-US" }); File.Exists(_tempPath).Should().BeTrue(); var json = await File.ReadAllTextAsync(_tempPath); json.Should().Contain("\"DefaultCulture\""); json.Should().Contain("\"en-US\""); } [Fact] public async Task Save_preserves_other_sections() { await File.WriteAllTextAsync(_tempPath, "{\"Untouched\":{\"Value\":42}}"); var writer = new JsonSettingsWriter(_tempPath); await writer.SaveSectionAsync("Localization", new LocalizationOptions { DefaultCulture = "ru-RU" }); var root = await writer.ReadAllAsync(); root["Untouched"].Should().NotBeNull(); root["Untouched"]!["Value"]!.GetValue().Should().Be(42); root["Localization"]!["DefaultCulture"]!.GetValue().Should().Be("ru-RU"); } [Fact] public async Task Reset_removes_section_only() { var writer = new JsonSettingsWriter(_tempPath); await writer.SaveSectionAsync("A", new { X = 1 }); await writer.SaveSectionAsync("B", new { Y = 2 }); await writer.ResetSectionAsync("A"); var root = await writer.ReadAllAsync(); root.ContainsKey("A").Should().BeFalse(); root.ContainsKey("B").Should().BeTrue(); } [Fact] public async Task Reset_when_file_missing_is_a_no_op() { var writer = new JsonSettingsWriter(_tempPath); await writer.ResetSectionAsync("Anything"); File.Exists(_tempPath).Should().BeFalse(); } public void Dispose() { try { if (File.Exists(_tempPath)) { File.Delete(_tempPath); } } catch { // best effort } } }