// SPDX-License-Identifier: MIT package checker import ( "context" "errors" "reflect" "testing" sdk "git.happydns.org/checker-sdk-go/checker" ) func TestStatusFromGrok(t *testing.T) { cases := map[string]sdk.Status{ "SECURE": sdk.StatusOK, " secure ": sdk.StatusOK, "INSECURE": sdk.StatusInfo, "BOGUS": sdk.StatusCrit, "INDETERMINATE": sdk.StatusWarn, "NON_EXISTENT": sdk.StatusInfo, "NOERROR": sdk.StatusInfo, "": sdk.StatusUnknown, "WHATEVER": sdk.StatusUnknown, } for in, want := range cases { if got := statusFromGrok(in); got != want { t.Errorf("statusFromGrok(%q) = %v, want %v", in, got, want) } } } func TestOrderedZones_PrefersOrder(t *testing.T) { d := &DNSVizData{ Order: []string{"keep.", "this."}, Zones: map[string]ZoneAnalysis{"keep.": {}, "this.": {}, "other.": {}}, } if got := orderedZones(d); !reflect.DeepEqual(got, []string{"keep.", "this."}) { t.Errorf("expected explicit Order to be returned, got %v", got) } } func TestOrderedZones_SortsByDepth(t *testing.T) { d := &DNSVizData{ Zones: map[string]ZoneAnalysis{ ".": {}, "com.": {}, "example.com.": {}, }, } got := orderedZones(d) want := []string{"example.com.", "com.", "."} if !reflect.DeepEqual(got, want) { t.Errorf("orderedZones=%v, want %v", got, want) } } func TestLoadData_ReturnsErrorState(t *testing.T) { obs := stubObs{err: errors.New("boom")} data, errState := loadData(context.Background(), obs, "code_x") if data != nil { t.Errorf("expected nil data on error, got %+v", data) } if len(errState) != 1 || errState[0].Status != sdk.StatusError || errState[0].Code != "code_x" { t.Errorf("unexpected error state: %+v", errState) } } func TestLoadData_Success(t *testing.T) { d := &DNSVizData{Domain: "example.com", Zones: map[string]ZoneAnalysis{"example.com.": {Status: "SECURE"}}} obs := stubObs{value: d} got, errState := loadData(context.Background(), obs, "code_y") if errState != nil { t.Fatalf("unexpected error state: %+v", errState) } if got == nil || got.Domain != "example.com" { t.Errorf("unexpected data: %+v", got) } } func TestRules_Wired(t *testing.T) { rs := Rules() if len(rs) == 0 { t.Fatal("expected at least one rule") } seen := map[string]bool{} for _, r := range rs { if r.Name() == "" || r.Description() == "" { t.Errorf("rule has empty Name/Description: %T", r) } if seen[r.Name()] { t.Errorf("duplicate rule name: %s", r.Name()) } seen[r.Name()] = true } }