package config import ( "path/filepath" "testing" ) func TestUpsertAddsNewMapping(t *testing.T) { cfg := &Config{} m := Mapping{Name: "a", HouseholdID: "h1", TonieID: "t1", Folder: "/tmp/a"} cfg.Upsert(m) got := cfg.Get("a") if got == nil || got.TonieID != "t1" { t.Fatalf("expected mapping to be added, got %+v", got) } } func TestUpsertReplacesExistingMapping(t *testing.T) { cfg := &Config{} cfg.Upsert(Mapping{Name: "a", HouseholdID: "h1", TonieID: "t1", Folder: "/tmp/a"}) cfg.Upsert(Mapping{Name: "a", HouseholdID: "h1", TonieID: "t2", Folder: "/tmp/a2"}) if len(cfg.Mappings) != 1 { t.Fatalf("expected 1 mapping, got %d", len(cfg.Mappings)) } if cfg.Get("a").TonieID != "t2" { t.Fatalf("expected updated tonie id t2, got %s", cfg.Get("a").TonieID) } } func TestRemoveReturnsFalseWhenMissing(t *testing.T) { cfg := &Config{} if cfg.Remove("nope") { t.Fatal("expected Remove to return false for missing mapping") } } func TestSaveAndLoadRoundtrip(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "config.yaml") cfg := &Config{} cfg.Upsert(Mapping{ Name: "peppa", HouseholdID: "h1", TonieID: "t1", Folder: "/tmp/peppa", PlaylistRef: "https://example.com/playlist/1", Prune: true, }) if err := Save(cfg, path); err != nil { t.Fatalf("Save failed: %v", err) } loaded, err := Load(path) if err != nil { t.Fatalf("Load failed: %v", err) } m := loaded.Get("peppa") if m == nil || m.TonieID != "t1" || m.PlaylistRef != "https://example.com/playlist/1" { t.Fatalf("unexpected loaded mapping: %+v", m) } } func TestLoadMissingFileReturnsEmptyConfig(t *testing.T) { cfg, err := Load(filepath.Join(t.TempDir(), "does-not-exist.yaml")) if err != nil { t.Fatalf("Load failed: %v", err) } if len(cfg.Mappings) != 0 { t.Fatalf("expected empty mappings, got %+v", cfg.Mappings) } }