Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101import Foundationimport Testing@testable import CoreATProtocol
@Suite("ATProtoIdentityResolver protocol")struct IdentityResolverProtocolTests { @Test("Fake resolver returns canned ResolvedIdentity from resolve(identifier:)") func fakeResolveReturnsCannedIdentity() async throws { let canned = IdentityResolver.ResolvedIdentity( handle: "alice.test", did: "did:plc:alice", pdsEndpoint: "https://pds.test", authorizationServer: "https://auth.test" ) let fake = StubIdentityResolver(identity: canned, isValidServer: true)
let result = try await fake.resolve(identifier: "alice.test")
#expect(result.handle == "alice.test") #expect(result.did == "did:plc:alice") #expect(result.pdsEndpoint == "https://pds.test") #expect(result.authorizationServer == "https://auth.test") #expect(result.authServerHost == "auth.test") }
@Test("Fake resolver returns canned isAuthorizationServer answer") func fakeIsAuthorizationServerReturnsCannedBool() async throws { let canned = IdentityResolver.ResolvedIdentity( handle: nil, did: "did:plc:bob", pdsEndpoint: "https://pds.example", authorizationServer: "https://auth.example" ) let approving = StubIdentityResolver(identity: canned, isValidServer: true) let denying = StubIdentityResolver(identity: canned, isValidServer: false)
#expect( try await approving.isAuthorizationServer( "https://auth.example", validFor: "https://pds.example" ) ) #expect( try await !denying.isAuthorizationServer( "https://auth.example", validFor: "https://pds.example" ) ) }
@Test("ATProtoOAuth.init accepts a custom identity resolver") func atProtoOAuthAcceptsCustomResolver() async throws { let canned = IdentityResolver.ResolvedIdentity( handle: "carol.test", did: "did:plc:carol", pdsEndpoint: "https://pds.test", authorizationServer: "https://auth.test" ) let fake = StubIdentityResolver(identity: canned, isValidServer: true)
let storage = ATProtoAuthStorage( retrieveLogin: { nil }, storeLogin: { _ in }, retrievePrivateKey: { nil }, storePrivateKey: { _ in } ) let config = ATProtoOAuthConfig( clientMetadataURL: "https://example.com/client-metadata.json", redirectURI: "example://callback" )
// The injection seam is the value of Step 5 — verify it compiles and // accepts a non-default resolver. End-to-end behavior with the fake // can't be tested offline because authenticate(...) still goes to // live network for client/server metadata; that's a future step. let client = await ATProtoOAuth( config: config, storage: storage, identityResolver: fake ) let pem = await client.privateKeyPEM #expect(!pem.isEmpty) }}
private struct StubIdentityResolver: ATProtoIdentityResolver { let identity: IdentityResolver.ResolvedIdentity let isValidServer: Bool
func resolve(identifier: String) async throws -> IdentityResolver.ResolvedIdentity { identity }
func isAuthorizationServer( _ authorizationServer: String, validFor pdsEndpoint: String ) async throws -> Bool { isValidServer }}