Files

140 lines
3.3 KiB
Go

package labeler
import (
"context"
"testing"
)
func TestParseTakedownInput_RepoURL(t *testing.T) {
// These tests only exercise parsing logic, not PDS resolution.
// ResolveIdentity calls are tested with mock server below.
tests := []struct {
name string
input string
wantRepo string
}{
{"full URL", "https://atcr.io/r/handle/myimage", "myimage"},
{"no scheme", "atcr.io/r/handle/myimage", "myimage"},
{"handle/repo", "handle/myimage", "myimage"},
{"trailing slash", "atcr.io/r/handle/myimage/", "myimage"},
{"custom domain", "https://registry.example.com/r/handle/myimage", "myimage"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// These will fail on ResolveIdentity since there's no real PDS,
// but we can at least verify the parsing doesn't panic
_, err := ParseTakedownInput(context.Background(), tt.input)
if err == nil {
t.Skip("ResolveIdentity succeeded unexpectedly (network available)")
}
// The error should be from resolution, not parsing
if err != nil {
t.Logf("Expected resolution error: %v", err)
}
})
}
}
func TestParseTakedownInput_ATURI(t *testing.T) {
tests := []struct {
name string
input string
wantDID string
wantRepo string
}{
{
"full AT URI with collection",
"at://did:plc:abc123/io.atcr.repo.page/myimage",
"did:plc:abc123",
"myimage",
},
{
"DID only (user-level)",
"at://did:plc:abc123",
"did:plc:abc123",
"",
},
{
"DID with collection and rkey",
"at://did:plc:xyz/io.atcr.manifest/sha256-deadbeef",
"did:plc:xyz",
"sha256-deadbeef",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
input, err := ParseTakedownInput(context.Background(), tt.input)
if err != nil {
// Resolution may fail for handle-based AT URIs
t.Logf("Parse error (may be expected): %v", err)
return
}
if input.DID != tt.wantDID {
t.Errorf("DID = %q, want %q", input.DID, tt.wantDID)
}
if input.Repository != tt.wantRepo {
t.Errorf("Repository = %q, want %q", input.Repository, tt.wantRepo)
}
})
}
}
func TestParseTakedownInput_DID(t *testing.T) {
// Direct DID input (user-level takedown)
input, err := ParseTakedownInput(context.Background(), "at://did:plc:abc123")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if input.DID != "did:plc:abc123" {
t.Errorf("DID = %q, want did:plc:abc123", input.DID)
}
if input.Repository != "" {
t.Errorf("Repository = %q, want empty (user-level)", input.Repository)
}
}
func TestExtractRepoField(t *testing.T) {
tests := []struct {
name string
value string
collection string
want string
}{
{
"manifest with repository",
`{"$type":"io.atcr.manifest","repository":"myimage","digest":"sha256:abc"}`,
"io.atcr.manifest",
"myimage",
},
{
"tag with repository",
`{"$type":"io.atcr.tag","repository":"myimage","tag":"latest"}`,
"io.atcr.tag",
"myimage",
},
{
"no repository field",
`{"$type":"io.atcr.manifest","digest":"sha256:abc"}`,
"io.atcr.manifest",
"",
},
{
"invalid JSON",
`{invalid}`,
"io.atcr.manifest",
"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := extractRepoField([]byte(tt.value), tt.collection)
if got != tt.want {
t.Errorf("extractRepoField() = %q, want %q", got, tt.want)
}
})
}
}