package repository import ( "strings" "testing" "github.com/spf13/viper" ) func TestSrrdbLoadGroupRules(t *testing.T) { viper.Reset() viper.Set("groups", map[string]any{ "status": map[string]any{ "neformat": []string{"SHGZ"}, "remove": []any{"BIGLOVE", "CT"}, "ignore": []string{"BABAS"}, }, }) rules, err := srrdbLoadGroupRules() if err != nil { t.Fatalf("srrdbLoadGroupRules: %v", err) } if got := rules.status["SHGZ"]; got != "neformat" { t.Errorf("status[SHGZ] = %q, want neformat", got) } if got := rules.status["CT"]; got != "remove" { t.Errorf("status[CT] = %q, want remove", got) } if !rules.ignore["BABAS"] { t.Error("BABAS must be ignored") } if rules.ignore["CT"] { t.Error("CT must not be ignored") } } // The regression this guards: viper lowercases keys, and if the group names were // read as keys, SHGZ would arrive as shgz and would never match a release line. func TestSrrdbLoadGroupRulesKeepsGroupCase(t *testing.T) { viper.Reset() viper.Set("groups", map[string]any{ "status": map[string]any{ "neformat": []string{"SHGZ", "ENViED"}, }, }) rules, err := srrdbLoadGroupRules() if err != nil { t.Fatalf("srrdbLoadGroupRules: %v", err) } for _, group := range []string{"SHGZ", "ENViED"} { if _, exists := rules.status[group]; !exists { t.Errorf("group %q was not registered with its original case", group) } } } func TestSrrdbLoadGroupRulesRejectsGroupInTwoLists(t *testing.T) { viper.Reset() viper.Set("groups", map[string]any{ "status": map[string]any{ "neformat": []string{"SHGZ"}, "remove": []string{"SHGZ"}, }, }) _, err := srrdbLoadGroupRules() if err == nil { t.Fatal("a group listed under two values must be rejected") } for _, want := range []string{"SHGZ", "neformat", "remove"} { if !strings.Contains(err.Error(), want) { t.Errorf("error %q does not name %q", err, want) } } } func TestSrrdbLoadGroupRulesRejectsNumericGroup(t *testing.T) { viper.Reset() viper.Set("groups", map[string]any{ "status": map[string]any{ "ignore": []any{401}, }, }) if _, err := srrdbLoadGroupRules(); err == nil { t.Error("an unquoted numeric group must be rejected instead of dropped silently") } } func TestSrrdbLoadGroupRulesRejectsUnknownColumn(t *testing.T) { viper.Reset() viper.Set("groups", map[string]any{ "nfo": map[string]any{ "x": []string{"Y"}, }, }) if _, err := srrdbLoadGroupRules(); err == nil { t.Error("a rule targeting a column other than status must be rejected") } }