package rules import ( "context" "testing" ) func TestAndRule_Matches(t *testing.T) { tests := []struct { name string rule *AndRule metadata *URLMetadata expected bool }{ { name: "all child rules match", rule: &AndRule{ Rules: []Rule{ &MimetypeRule{Mimetype: "text/html", Archivers: []ArchiverConfig{}}, // Empty slice for child rule &HostnameRule{Hostname: "example.com", Archivers: []ArchiverConfig{}}, // Empty slice for child rule }, Archivers: []ArchiverConfig{{Key: "obelisk"}}, }, metadata: &URLMetadata{ MimeType: "text/html", Domain: "example.com", }, expected: true, }, { name: "one child rule doesn't match", rule: &AndRule{ Rules: []Rule{ &MimetypeRule{Mimetype: "text/html", Archivers: []ArchiverConfig{}}, &HostnameRule{Hostname: "example.com", Archivers: []ArchiverConfig{}}, }, Archivers: []ArchiverConfig{{Key: "obelisk"}}, }, metadata: &URLMetadata{ MimeType: "text/html", Domain: "other.com", }, expected: false, }, { name: "no child rules", rule: &AndRule{ Rules: []Rule{}, Archivers: []ArchiverConfig{{Key: "obelisk"}}, }, metadata: &URLMetadata{ MimeType: "text/html", }, expected: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { ctx := context.Background() result, err := tt.rule.Matches(ctx, tt.metadata) if err != nil { t.Fatalf("Matches() error = %v", err) } matched := result != nil if matched != tt.expected { t.Errorf("Matches() matched = %v, want %v", matched, tt.expected) } if matched && len(result) != len(tt.rule.Archivers) { t.Errorf("Matches() returned %d archivers, want %d", len(result), len(tt.rule.Archivers)) } }) } } func TestAndRule_IsValid(t *testing.T) { tests := []struct { name string rule *AndRule wantErr bool }{ { name: "valid rule with children", rule: &AndRule{ Rules: []Rule{ &MimetypeRule{Mimetype: "text/html", Archivers: []ArchiverConfig{}}, }, Archivers: []ArchiverConfig{{Key: "obelisk"}}, }, wantErr: false, }, { name: "no child rules", rule: &AndRule{ Rules: []Rule{}, Archivers: []ArchiverConfig{{Key: "obelisk"}}, }, wantErr: true, }, { name: "invalid child rule", rule: &AndRule{ Rules: []Rule{ &MimetypeRule{Mimetype: "", Archivers: []ArchiverConfig{}}, // Invalid }, Archivers: []ArchiverConfig{{Key: "obelisk"}}, }, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { err := tt.rule.IsValid() if (err != nil) != tt.wantErr { t.Errorf("IsValid() error = %v, wantErr %v", err, tt.wantErr) } }) } }