import Foundation import 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 } }