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 Geladen(string json) { File.WriteAllText(Datei, json); var manager = new SettingsManager(_dir); manager.Load(); return manager; } [Fact] public void Eine_alte_Datei_meldet_ihre_aufgegebenen_Schluessel() { var manager = Geladen(""" { "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 Was_die_Datei_sonst_enthaelt_wird_weiter_gelesen() { // The announcement must not change the fact that the rest arrives. var manager = Geladen(""" { "logDirectory": "/var/log/clawd", "minimumLogLevel": "Debug", "maxLogLinesInUi": 4000 } """); manager.AppSettings.MinimumLogLevel.ShouldBe("Debug"); manager.AppSettings.MaxLogLinesInUi.ShouldBe(4000); } [Fact] public void Eine_frische_Datei_meldet_nichts() { Geladen("""{ "minimumLogLevel": "Info" }""").AbandonedKeys.ShouldBeEmpty(); } [Fact] public void Speichern_wird_die_aufgegebenen_Schluessel_los() { var manager = Geladen("""{ "logDirectory": "/var/log/clawd" }"""); manager.Save(); File.ReadAllText(Datei).ShouldNotContain("logDirectory"); // And the second load has nothing more to report. var erneut = new SettingsManager(_dir); erneut.Load(); erneut.AbandonedKeys.ShouldBeEmpty(); } [Fact] public void Eine_unlesbare_Datei_verhindert_den_Start_nicht() { var manager = Geladen("{ das ist kein JSON"); manager.AppSettings.MinimumLogLevel.ShouldBe("Info"); manager.AbandonedKeys.ShouldBeEmpty(); } }