hako/internal/archival/rules/or_rule_test.go
2026-01-12 19:35:18 +01:00

137 lines
3 KiB
Go

package rules
import (
"context"
"testing"
)
func TestOrRule_Matches(t *testing.T) {
tests := []struct {
name string
rule *OrRule
metadata *URLMetadata
expected bool
}{
{
name: "first child rule matches",
rule: &OrRule{
Rules: []Rule{
&MimetypeRule{Mimetype: "text/html", Archivers: []ArchiverConfig{}},
&HostnameRule{Hostname: "other.com", Archivers: []ArchiverConfig{}},
},
Archivers: []ArchiverConfig{{Key: "obelisk"}},
},
metadata: &URLMetadata{
MimeType: "text/html",
Domain: "example.com",
},
expected: true,
},
{
name: "second child rule matches",
rule: &OrRule{
Rules: []Rule{
&MimetypeRule{Mimetype: "application/pdf", Archivers: []ArchiverConfig{}},
&HostnameRule{Hostname: "example.com", Archivers: []ArchiverConfig{}},
},
Archivers: []ArchiverConfig{{Key: "obelisk"}},
},
metadata: &URLMetadata{
MimeType: "text/html",
Domain: "example.com",
},
expected: true,
},
{
name: "no child rules match",
rule: &OrRule{
Rules: []Rule{
&MimetypeRule{Mimetype: "application/pdf", Archivers: []ArchiverConfig{}},
&HostnameRule{Hostname: "other.com", Archivers: []ArchiverConfig{}},
},
Archivers: []ArchiverConfig{{Key: "obelisk"}},
},
metadata: &URLMetadata{
MimeType: "text/html",
Domain: "example.com",
},
expected: false,
},
{
name: "no child rules",
rule: &OrRule{
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 TestOrRule_IsValid(t *testing.T) {
tests := []struct {
name string
rule *OrRule
wantErr bool
}{
{
name: "valid rule with children",
rule: &OrRule{
Rules: []Rule{
&MimetypeRule{Mimetype: "text/html", Archivers: []ArchiverConfig{}},
},
Archivers: []ArchiverConfig{{Key: "obelisk"}},
},
wantErr: false,
},
{
name: "no child rules",
rule: &OrRule{
Rules: []Rule{},
Archivers: []ArchiverConfig{{Key: "obelisk"}},
},
wantErr: true,
},
{
name: "invalid child rule",
rule: &OrRule{
Rules: []Rule{
&HostnameRule{Hostname: "", 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)
}
})
}
}