package checker import ( "encoding/json" "testing" ) func TestExtractScores(t *testing.T) { raw := json.RawMessage(`{ "score": 82, "grade": "B", "summary": { "dns_score": 90, "dns_grade": "A", "authentication_score": 75, "authentication_grade": "C", "spam_score": 88, "spam_grade": "B", "blacklist_score": 100, "blacklist_grade": "A", "header_score": 70, "header_grade": "C", "content_score": 65, "content_grade": "D" } }`) scores, grades, err := extractScores(raw) if err != nil { t.Fatalf("unexpected error: %v", err) } want := map[string]int{ SectionOverall: 82, SectionDNS: 90, SectionAuthentication: 75, SectionSpam: 88, SectionBlacklist: 100, SectionHeader: 70, SectionContent: 65, } for k, v := range want { if scores[k] != v { t.Errorf("scores[%s] = %d, want %d", k, scores[k], v) } } wantGrades := map[string]string{ SectionOverall: "B", SectionDNS: "A", SectionAuthentication: "C", SectionSpam: "B", SectionBlacklist: "A", SectionHeader: "C", SectionContent: "D", } for k, v := range wantGrades { if grades[k] != v { t.Errorf("grades[%s] = %q, want %q", k, grades[k], v) } } } func TestExtractScoresMissingFieldsDefaultToZero(t *testing.T) { scores, grades, err := extractScores(json.RawMessage(`{"score": 50, "grade": "F"}`)) if err != nil { t.Fatalf("unexpected error: %v", err) } if scores[SectionOverall] != 50 || grades[SectionOverall] != "F" { t.Errorf("overall not extracted: %v / %v", scores[SectionOverall], grades[SectionOverall]) } if scores[SectionDNS] != 0 || grades[SectionDNS] != "" { t.Errorf("missing fields should default to zero values") } for _, s := range AllSections { if _, ok := scores[s]; !ok { t.Errorf("section %q missing from scores map", s) } if _, ok := grades[s]; !ok { t.Errorf("section %q missing from grades map", s) } } } func TestExtractScoresInvalidJSON(t *testing.T) { if _, _, err := extractScores(json.RawMessage(`{not json`)); err == nil { t.Fatal("expected error for invalid JSON, got nil") } } func TestAllSectionsOverallFirst(t *testing.T) { if len(AllSections) == 0 || AllSections[0] != SectionOverall { t.Fatalf("AllSections must start with overall, got %v", AllSections) } }