package config import ( "os" "path/filepath" "testing" ) func TestLoadOrEmpty_NoFile(t *testing.T) { cfg, err := LoadOrEmpty("/tmp/unitdore-nonexistent-test.yaml") if err != nil { t.Fatalf("expected no error for missing file, got: %v", err) } if len(cfg.Units) != 0 { t.Errorf("expected empty units, got %d", len(cfg.Units)) } } func TestSaveAndLoad(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "units.yaml") cfg := &Config{ Units: []Unit{ {Name: "nginx", Runtime: "podman", Order: 1, Enabled: true}, {Name: "myapp", Runtime: "docker", User: "hein", Order: 2, Enabled: true}, }, } if err := cfg.Save(path); err != nil { t.Fatalf("Save failed: %v", err) } loaded, err := Load(path) if err != nil { t.Fatalf("Load failed: %v", err) } if len(loaded.Units) != 2 { t.Errorf("expected 2 units, got %d", len(loaded.Units)) } if loaded.Units[0].Name != "nginx" { t.Errorf("expected nginx, got %s", loaded.Units[0].Name) } if loaded.Units[1].User != "hein" { t.Errorf("expected user hein, got %s", loaded.Units[1].User) } } func TestFindUnit(t *testing.T) { cfg := &Config{ Units: []Unit{ {Name: "nginx", Runtime: "podman"}, {Name: "myapp", Runtime: "docker"}, }, } u := cfg.FindUnit("nginx") if u == nil { t.Fatal("expected to find nginx, got nil") } if u.Runtime != "podman" { t.Errorf("expected podman, got %s", u.Runtime) } missing := cfg.FindUnit("nothere") if missing != nil { t.Errorf("expected nil for missing unit, got %+v", missing) } } func TestAddUnit(t *testing.T) { cfg := &Config{} added := cfg.AddUnit(Unit{Name: "nginx", Runtime: "podman", Enabled: true}) if !added { t.Error("expected AddUnit to return true for new unit") } if len(cfg.Units) != 1 { t.Errorf("expected 1 unit, got %d", len(cfg.Units)) } // Adding a duplicate should return false and not grow the list added = cfg.AddUnit(Unit{Name: "nginx", Runtime: "docker"}) if added { t.Error("expected AddUnit to return false for duplicate") } if len(cfg.Units) != 1 { t.Errorf("expected still 1 unit, got %d", len(cfg.Units)) } } func TestLoad_InvalidYAML(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "bad.yaml") // Use a tab character in a mapping key — this is genuinely invalid YAML os.WriteFile(path, []byte("units:\n - name: ok\n\t broken: tab-indent"), 0644) _, err := Load(path) if err == nil { t.Error("expected error for invalid YAML, got nil") } } func TestSave_CreatesParentDir(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "subdir", "nested", "units.yaml") cfg := &Config{Units: []Unit{{Name: "test", Runtime: "podman", Enabled: true}}} if err := cfg.Save(path); err != nil { t.Fatalf("Save failed to create parent dirs: %v", err) } if _, err := os.Stat(path); err != nil { t.Errorf("expected file to exist: %v", err) } } func TestDisabledReason_Roundtrip(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "units.yaml") cfg := &Config{ Units: []Unit{ { Name: "ghost", Runtime: "podman", Enabled: false, DisabledReason: "container not found", }, }, } if err := cfg.Save(path); err != nil { t.Fatalf("Save failed: %v", err) } loaded, err := Load(path) if err != nil { t.Fatalf("Load failed: %v", err) } if loaded.Units[0].DisabledReason != "container not found" { t.Errorf("expected disabled_reason to survive roundtrip, got: %q", loaded.Units[0].DisabledReason) } }