package nixcache import ( "strings" "testing" ) func TestCacheName(t *testing.T) { if got, err := CacheName("did:plc:z72i7hdynmk6r22z27h6tvur"); err != nil || got != "plc-z72i7hdynmk6r22z27h6tvur" { t.Errorf("CacheName(plc) = %q, %v", got, err) } if got, err := CacheName(""); err == nil { t.Errorf("CacheName(\"\") = %q; want an error", got) } dids := []string{ "did:web:example.com", "did:plc:UPPER", "did:plc:short", "did:plc:z72i7hdynmk6r22z27h6tvur-extra", "*", } seen := map[string]string{} for _, did := range dids { got, err := CacheName(did) if err != nil { t.Fatalf("CacheName(%q): %v", did, err) } if !strings.HasPrefix(got, "h-") || len(got) != 34 || !validName.MatchString(got) { t.Errorf("CacheName(%q) = %q; want valid h- and 32 chars", did, got) } if other, ok := seen[got]; ok { t.Errorf("CacheName(%q) collides with %q", did, other) } seen[got] = did } }