Files

87 lines
2.4 KiB
Go

package labeler
import (
"testing"
"time"
)
func TestLabelToOutput(t *testing.T) {
now := time.Date(2026, 3, 22, 10, 0, 0, 0, time.UTC)
exp := time.Date(2026, 4, 22, 10, 0, 0, 0, time.UTC)
label := Label{
ID: 1,
Src: "did:web:labeler.atcr.io",
URI: "at://did:plc:abc/io.atcr.manifest/sha256-123",
CID: "bafyabc",
Val: "!takedown",
Neg: false,
Cts: now,
Exp: &exp,
SubjectDID: "did:plc:abc",
SubjectRepo: "myimage",
}
out := labelToOutput(label)
if out.Src != "did:web:labeler.atcr.io" {
t.Errorf("Src = %q, want did:web:labeler.atcr.io", out.Src)
}
if out.URI != "at://did:plc:abc/io.atcr.manifest/sha256-123" {
t.Errorf("URI = %q", out.URI)
}
if out.CID != "bafyabc" {
t.Errorf("CID = %q, want bafyabc", out.CID)
}
if out.Val != "!takedown" {
t.Errorf("Val = %q", out.Val)
}
if out.Neg {
t.Error("expected Neg=false")
}
if out.Cts != "2026-03-22T10:00:00Z" {
t.Errorf("Cts = %q", out.Cts)
}
if out.Exp != "2026-04-22T10:00:00Z" {
t.Errorf("Exp = %q", out.Exp)
}
}
func TestLabelToOutput_NoExpiration(t *testing.T) {
label := Label{
Src: "did:web:labeler.atcr.io",
URI: "at://did:plc:abc",
Val: "!takedown",
Cts: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC),
}
out := labelToOutput(label)
if out.Exp != "" {
t.Errorf("expected empty Exp, got %q", out.Exp)
}
}
func TestMatchesAnyPattern(t *testing.T) {
tests := []struct {
name string
uri string
patterns []string
want bool
}{
{"exact match", "at://did:plc:abc/io.atcr.manifest/sha256-123", []string{"at://did:plc:abc/io.atcr.manifest/sha256-123"}, true},
{"no match", "at://did:plc:abc/io.atcr.manifest/sha256-123", []string{"at://did:plc:def/io.atcr.manifest/sha256-123"}, false},
{"wildcard match", "at://did:plc:abc/io.atcr.manifest/sha256-123", []string{"at://did:plc:abc/*"}, true},
{"wildcard no match", "at://did:plc:abc/io.atcr.manifest/sha256-123", []string{"at://did:plc:def/*"}, false},
{"empty patterns", "at://did:plc:abc/io.atcr.manifest/sha256-123", []string{}, false},
{"multiple patterns", "at://did:plc:abc/io.atcr.manifest/sha256-123", []string{"at://did:plc:def/*", "at://did:plc:abc/*"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := matchesAnyPattern(tt.uri, tt.patterns)
if got != tt.want {
t.Errorf("matchesAnyPattern(%q, %v) = %v, want %v", tt.uri, tt.patterns, got, tt.want)
}
})
}
}