package main import ( "testing" intrgb "netdome.biz/paul/impact-80/internal/rgb" ) func sameZones(a, b []intrgb.Zone) bool { if len(a) != len(b) { return false } for i := range a { if a[i] != b[i] { return false } } return true } // README.md: "With `--zone`, commands target exactly the selected zone." // `save` already honours it, so `load` must not rewrite zones the user did // not select — otherwise a single-zone restore silently touches all three. func TestFilterZonesKeepsOnlyTheSelection(t *testing.T) { allZones := []intrgb.Zone{intrgb.ZoneLogo, intrgb.ZoneBacklight, intrgb.ZoneSide} tests := []struct { name string zoneFlag string want []intrgb.Zone }{ {"no flag keeps every zone", "", allZones}, {"logo keeps logo", "logo", []intrgb.Zone{intrgb.ZoneLogo}}, {"backlight keeps backlight", "backlight", []intrgb.Zone{intrgb.ZoneBacklight}}, {"side keeps side", "side", []intrgb.Zone{intrgb.ZoneSide}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { original := targetZone t.Cleanup(func() { targetZone = original }) targetZone = tt.zoneFlag selected, err := selectedZones() if err != nil { t.Fatalf("selectedZones() returned error: %v", err) } got := filterZones(allZones, selected) if !sameZones(got, tt.want) { t.Errorf("filterZones(all, %v) = %v, want %v", selected, got, tt.want) } }) } } // A profile may omit zones the selection names. Those must not be invented. func TestFilterZonesDoesNotAddMissingZones(t *testing.T) { original := targetZone t.Cleanup(func() { targetZone = original }) targetZone = "backlight" profileZones := []intrgb.Zone{intrgb.ZoneSide} selected, err := selectedZones() if err != nil { t.Fatalf("selectedZones() returned error: %v", err) } got := filterZones(profileZones, selected) if len(got) != 0 { t.Errorf("filterZones(side-only profile, backlight) = %v, want empty", got) } } // filterZones must not reorder or mutate its input. func TestFilterZonesDoesNotMutateInput(t *testing.T) { original := targetZone t.Cleanup(func() { targetZone = original }) targetZone = "logo" input := []intrgb.Zone{intrgb.ZoneSide, intrgb.ZoneLogo} selected, err := selectedZones() if err != nil { t.Fatalf("selectedZones() returned error: %v", err) } filterZones(input, selected) if !sameZones(input, []intrgb.Zone{intrgb.ZoneSide, intrgb.ZoneLogo}) { t.Errorf("filterZones() mutated its input: %v", input) } }