using ClawdDotNet.App.Settings; using Shouldly; namespace ClawdDotNet.App.Tests.Settings; /// /// What happens to the settings that no longer exist here (#528, stage 1). /// /// The occasion. Five keys moved from into the /// instance. System.Text.Json silently skips unknown keys, and /// Save does not write them again afterwards — whoever had entered their own log or /// backup directory would therefore lose the value without finding out. /// A move without fallback logic is fine; one without an announcement is not. /// /// Deliberately no takeover into the instances: an application-wide path, copied into all /// instances, would again be the same folder for all — i.e. exactly the state /// stage 1 eliminates. /// public sealed class SettingsManagerTests : IDisposable { private readonly string _dir = Path.Combine( Path.GetTempPath(), "clawd-tests", "settings-" + Guid.NewGuid().ToString("N")[..8]); private string Datei => Path.Combine(_dir, "Settings.json"); public SettingsManagerTests() => Directory.CreateDirectory(_dir); public void Dispose() { try { Directory.Delete(_dir, recursive: true); } catch { /* A leftover test folder does not turn a run red. */ } } private SettingsManager Loaded(string json) { File.WriteAllText(Datei, json); var manager = new SettingsManager(_dir); manager.Load(); return manager; } [Fact] public void An_old_file_reports_its_abandoned_keys() { var manager = Loaded(""" { "logDirectory": "/var/log/clawd", "backupDirectory": "/srv/sicherungen", "backupKeepCount": 30, "autoBackupEnabled": true, "autoBackupTime": "02:30", "minimumLogLevel": "Debug" } """); manager.AbandonedKeys.ShouldBe( ["logDirectory", "backupDirectory", "backupKeepCount", "autoBackupEnabled", "autoBackupTime"], ignoreOrder: true); } [Fact] public void Whatever_else_the_file_contains_is_still_read() { // The announcement must not change the fact that the rest arrives. var manager = Loaded(""" { "logDirectory": "/var/log/clawd", "minimumLogLevel": "Debug", "maxLogLinesInUi": 4000 } """); manager.AppSettings.MinimumLogLevel.ShouldBe("Debug"); manager.AppSettings.MaxLogLinesInUi.ShouldBe(4000); } [Fact] public void A_fresh_file_reports_nothing() { Loaded("""{ "minimumLogLevel": "Info" }""").AbandonedKeys.ShouldBeEmpty(); } [Fact] public void Saving_drops_the_abandoned_keys() { var manager = Loaded("""{ "logDirectory": "/var/log/clawd" }"""); manager.Save(); File.ReadAllText(Datei).ShouldNotContain("logDirectory"); // And the second load has nothing more to report. var again = new SettingsManager(_dir); again.Load(); again.AbandonedKeys.ShouldBeEmpty(); } [Fact] public void An_unreadable_file_does_not_prevent_the_start() { var manager = Loaded("{ das ist kein JSON"); manager.AppSettings.MinimumLogLevel.ShouldBe("Info"); manager.AbandonedKeys.ShouldBeEmpty(); } }