import Foundation import Testing @testable import CoreATProtocol @Suite("IdentityResolver hostname validation") struct HostnameValidationTests { @Test("Valid DNS-style handles are accepted") func valid() throws { try IdentityResolver.validateHostname("alice.bsky.social") try IdentityResolver.validateHostname("example.com") try IdentityResolver.validateHostname("a-b.c-d.example.com") } @Test("Handles with URL authority characters are rejected", arguments: [ "alice@bsky.social", "alice.bsky.social/extra", "alice.bsky.social?q=1", "alice.bsky.social#frag", "alice.bsky.social:8080", "alice bsky social", "aliçe.bsky.social", ]) func rejectsInjection(_ input: String) { #expect(throws: IdentityError.self) { try IdentityResolver.validateHostname(input) } } @Test("Empty, single-label, and malformed handles are rejected", arguments: [ "", "com", ".com", "alice.", "alice..com", "-alice.com", "alice-.com", ]) func rejectsMalformed(_ input: String) { #expect(throws: IdentityError.self) { try IdentityResolver.validateHostname(input) } } @Test("Labels over 63 characters are rejected") func tooLongLabel() { let longLabel = String(repeating: "a", count: 64) #expect(throws: IdentityError.self) { try IdentityResolver.validateHostname("\(longLabel).com") } } } @Suite("DID document PDS lookup") struct DIDDocumentPDSTests { @Test("Service matched by #atproto_pds suffix") func matchBySuffix() { let doc = DIDDocument( id: "did:plc:abc", alsoKnownAs: ["at://alice.bsky.social"], service: [DIDService(id: "#atproto_pds", type: "Whatever", serviceEndpoint: "https://pds.example")] ) #expect(doc.pdsEndpoint == "https://pds.example") } @Test("Service matched by AtprotoPersonalDataServer type") func matchByType() { let doc = DIDDocument( id: "did:plc:abc", alsoKnownAs: ["at://alice.bsky.social"], service: [DIDService(id: "#other", type: "AtprotoPersonalDataServer", serviceEndpoint: "https://pds.example")] ) #expect(doc.pdsEndpoint == "https://pds.example") } @Test("Document with no matching service returns nil") func noMatch() { let doc = DIDDocument( id: "did:plc:abc", alsoKnownAs: nil, service: [DIDService(id: "#other", type: "Unrelated", serviceEndpoint: "https://example.com")] ) #expect(doc.pdsEndpoint == nil) } }