import Foundation import Testing @testable import CoreATProtocol @Suite("AT Protocol syntax") struct SyntaxTests { @Test("DIDs accept plc and web forms and reject malformed strings") func dids() throws { let plc = try DID("did:plc:4uz2445cjiw7w4nobfgnu35f") #expect(plc.method == "plc") #expect(plc.identifier == "4uz2445cjiw7w4nobfgnu35f") #expect(plc.isPLC) let web = try DID("did:web:catalog.effem.app") #expect(web.isWeb) #expect(web.identifier == "catalog.effem.app") for bad in ["did:", "did:plc", "did:PLC:abc", "plc:abc", "did:plc:abc:", "did:plc:a b", "did:plc:abc%"] { #expect(throws: ATSyntaxError.self, "\(bad)") { try DID(bad) } } } @Test("Handles normalise to lowercase and reject bad labels") func handles() throws { #expect(try Handle("Alice.Bsky.Social").rawValue == "alice.bsky.social") #expect(Handle.isValid("handle.invalid")) for bad in ["alice", "-alice.com", "alice-.com", "alice..com", "alice.123", ".alice.com", String(repeating: "a", count: 64) + ".com"] { #expect(!Handle.isValid(bad), "\(bad)") } } @Test("NSIDs split into authority and name") func nsids() throws { let nsid = try NSID("xyz.effem.feed.comment") #expect(nsid.authority == "xyz.effem.feed") #expect(nsid.name == "comment") #expect(NSID.isValid("com.atproto.repo.strongRef")) for bad in ["xyz.effem", "1xyz.effem.feed", "xyz.effem.feed.1comment", "xyz.effem.feed.com-ment", "xyz..feed.comment"] { #expect(!NSID.isValid(bad), "\(bad)") } } @Test("Record keys follow the allowed character set") func recordKeys() { #expect(RecordKey.isValid("3jx2abcdefghi")) #expect(RecordKey.isValid("self")) #expect(RecordKey.isValid("a.b_c:d~e-f")) #expect(!RecordKey.isValid(".")) #expect(!RecordKey.isValid("..")) #expect(!RecordKey.isValid("")) #expect(!RecordKey.isValid("has space")) #expect(!RecordKey.isValid("slash/no")) } @Test("TIDs round-trip through their integer form and increase monotonically") func tids() throws { let tid = try TID("3jzfcijpj2z2a") #expect(TID(integer: tid.integerValue).rawValue == tid.rawValue) #expect(!TID.isValid("3jzfcijpj2z2")) #expect(!TID.isValid("zjzfcijpj2z2a")) #expect(!TID.isValid("3jzfcijpj2z21")) let first = TID.now() let second = TID.now() #expect(first < second) #expect(abs(second.date.timeIntervalSinceNow) < 5) } @Test("AT-URIs parse authority, collection and record key") func atURIs() throws { let uri = try ATURI("at://did:plc:abc123/xyz.effem.feed.comment/3jx2abcdefghi") #expect(uri.authority == "did:plc:abc123") #expect(uri.collection?.rawValue == "xyz.effem.feed.comment") #expect(uri.recordKey?.rawValue == "3jx2abcdefghi") #expect(uri.authorityIsDID) let handleURI = try ATURI("at://alice.bsky.social/xyz.effem.feed.list") #expect(handleURI.collection?.rawValue == "xyz.effem.feed.list") #expect(handleURI.recordKey == nil) #expect(!handleURI.authorityIsDID) let bare = try ATURI("at://did:plc:abc123") #expect(bare.collection == nil) for bad in ["https://x", "at://", "at://did:plc:abc/not-an-nsid/rk", "at://did:plc:abc/xyz.effem.feed.comment/", "at://did:plc:abc/xyz.effem.feed.comment/rk/extra"] { #expect(throws: ATSyntaxError.self, "\(bad)") { try ATURI(bad) } } let built = try ATURI(authority: "did:plc:abc123", collection: NSID("xyz.effem.feed.boost"), recordKey: RecordKey("3jx2abcdefghi")) #expect(built.rawValue == "at://did:plc:abc123/xyz.effem.feed.boost/3jx2abcdefghi") } @Test("Datetimes parse offsets and fractions and format to milliseconds") func datetimes() throws { let zulu = try ATDatetime("2026-01-02T03:04:05.000Z") #expect(zulu.date.timeIntervalSince1970 == 1767323045) let micros = try ATDatetime("2026-01-02T03:04:05.123456Z") #expect(abs(micros.date.timeIntervalSince1970 - 1767323045.123456) < 0.000001) let offset = try ATDatetime("2026-01-02T05:04:05+02:00") #expect(offset.date == zulu.date) #expect(ATDatetime.string(from: zulu.date) == "2026-01-02T03:04:05.000Z") #expect(ATDatetime(date: micros.date).rawValue == "2026-01-02T03:04:05.123Z") for bad in ["2026-01-02", "2026-01-02T03:04:05", "2026-01-02T03:04:05-00:00", "2026-13-02T03:04:05Z", "2026-02-30T03:04:05Z", "2026-01-02 03:04:05Z", "2026-01-02T03:04:05.Z"] { #expect(!ATDatetime.isValid(bad), "\(bad)") } } }