package main import ( "context" "os" "path/filepath" "testing" ) // design 39 bus-and-switch ยง33: the settings are read again at every reading, and each change of a value read is // written once in the audit under settings. func TestASettingChangedIsReadAtTheNextReadingAndAudited(t *testing.T) { mm := newMesh(t) ctx := context.Background() mm.m.SettingsFile = filepath.Join(t.TempDir(), "settings.json") _ = os.WriteFile(mm.m.SettingsFile, []byte(`{"switch_at_pct": 99}`), 0o600) if err := mm.m.ReadUsage(ctx); err != nil || len(mm.audit.under("settings")) != 0 { t.Fatalf("an unchanged file audited %v (%v)", mm.audit.under("settings"), err) } _ = os.WriteFile(mm.m.SettingsFile, []byte(`{"switch_at_pct": 95, "usage_minutes": 10}`), 0o600) _ = mm.m.ReadUsage(ctx) _ = mm.m.ReadUsage(ctx) if s := mm.m.settings(); s.SwitchAt != 95 || s.UsageEvery.Minutes() != 10 { t.Fatalf("in force %+v", s) } got := mm.audit.under("settings") if len(got) != 1 { t.Fatalf("audited %v", got) } if c, _ := got[0]["switch_at_pct"].(map[string]any); c["from"] != 99.0 || c["to"] != 95.0 { t.Fatalf("audited %v", got[0]) } }