A community based topic aggregation platform built on atproto
1package did 2 3import ( 4 "strings" 5 "testing" 6) 7 8func TestGenerateCommunityDID(t *testing.T) { 9 tests := []struct { 10 name string 11 plcDirectoryURL string 12 want string 13 isDevEnv bool 14 }{ 15 { 16 name: "generates did:plc in dev mode", 17 isDevEnv: true, 18 plcDirectoryURL: "https://plc.directory", 19 want: "did:plc:", 20 }, 21 { 22 name: "generates did:plc in prod mode", 23 isDevEnv: false, 24 plcDirectoryURL: "https://plc.directory", 25 want: "did:plc:", 26 }, 27 } 28 29 for _, tt := range tests { 30 t.Run(tt.name, func(t *testing.T) { 31 g := NewGenerator(tt.isDevEnv, tt.plcDirectoryURL) 32 did, err := g.GenerateCommunityDID() 33 if err != nil { 34 t.Fatalf("GenerateCommunityDID() error = %v", err) 35 } 36 37 if !strings.HasPrefix(did, tt.want) { 38 t.Errorf("GenerateCommunityDID() = %v, want prefix %v", did, tt.want) 39 } 40 41 // Verify it's a valid DID 42 if !ValidateDID(did) { 43 t.Errorf("Generated DID failed validation: %v", did) 44 } 45 }) 46 } 47} 48 49func TestGenerateCommunityDID_Uniqueness(t *testing.T) { 50 g := NewGenerator(true, "https://plc.directory") 51 52 // Generate 100 DIDs and ensure they're all unique 53 dids := make(map[string]bool) 54 for i := 0; i < 100; i++ { 55 did, err := g.GenerateCommunityDID() 56 if err != nil { 57 t.Fatalf("GenerateCommunityDID() error = %v", err) 58 } 59 60 if dids[did] { 61 t.Errorf("Duplicate DID generated: %v", did) 62 } 63 dids[did] = true 64 } 65} 66 67func TestValidateDID(t *testing.T) { 68 tests := []struct { 69 name string 70 did string 71 want bool 72 }{ 73 { 74 name: "valid did:plc", 75 did: "did:plc:z72i7hdynmk6r22z27h6tvur", 76 want: true, 77 }, 78 { 79 name: "valid did:plc with base32", 80 did: "did:plc:abc123xyz", 81 want: true, 82 }, 83 { 84 name: "valid did:web", 85 did: "did:web:coves.social", 86 want: true, 87 }, 88 { 89 name: "valid did:web with path", 90 did: "did:web:coves.social:community:gaming", 91 want: true, 92 }, 93 { 94 name: "invalid: missing prefix", 95 did: "plc:abc123", 96 want: false, 97 }, 98 { 99 name: "invalid: missing method", 100 did: "did::abc123", 101 want: false, 102 }, 103 { 104 name: "invalid: missing identifier", 105 did: "did:plc:", 106 want: false, 107 }, 108 { 109 name: "invalid: only did", 110 did: "did:", 111 want: false, 112 }, 113 { 114 name: "invalid: empty string", 115 did: "", 116 want: false, 117 }, 118 } 119 120 for _, tt := range tests { 121 t.Run(tt.name, func(t *testing.T) { 122 if got := ValidateDID(tt.did); got != tt.want { 123 t.Errorf("ValidateDID(%v) = %v, want %v", tt.did, got, tt.want) 124 } 125 }) 126 } 127}