import Foundation import XCTest @testable import OldFella final class ProfileConnectionsServiceTests: XCTestCase { func testLoadsFollowersAndFollowingWithProfileSummaries() async throws { let fixture = try SecureSessionUploadFixture() defer { fixture.cleanUp() } let actorID = "did:plc:subject" let cursor = "next/+ page" let followersPath = "/xrpc/app.bsky.graph.getFollowers" let followingPath = "/xrpc/app.bsky.graph.getFollows" StubURLProtocol.handler = { request in guard let url = request.url, [followersPath, followingPath].contains(url.path) else { return try fixture.response(request) } XCTAssertEqual(url.host, "pds.example.test") XCTAssertEqual(request.value(forHTTPHeaderField: "Authorization"), "Bearer \(fixture.token)") let query = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems XCTAssertEqual(query?.first { $0.name == "actor" }?.value, actorID) let requestedCursor = query?.first { $0.name == "cursor" }?.value let subject = ["did": actorID, "handle": "subject.example"] let nextAccount = ["did": "did:plc:next", "handle": "next.example"] let response: [String: Any] switch (url.path, requestedCursor) { case (followersPath, nil): response = [ "subject": subject, "cursor": cursor, "followers": [ ["did": "did:plc:zoe", "handle": "zoe.example", "displayName": "Zoe Lane", "avatar": "https://cdn.example/avatar.jpg", "description": "A bio."], ["did": "did:plc:muted", "handle": "muted.example", "viewer": ["muted": true]], ["did": "did:plc:alex", "handle": "alex.example"], ["did": "did:plc:blocked", "handle": "blocked.example", "viewer": ["blockedBy": true]] ] ] case (followersPath, cursor): response = ["subject": subject, "followers": [nextAccount]] case (followingPath, nil): response = ["subject": subject, "follows": [["did": "did:plc:sam", "handle": "sam.example"]]] default: XCTFail("Unexpected request: \(url)") throw ServiceError.invalidResponse } return (200, try JSONSerialization.data(withJSONObject: response)) } let service = fixture.service _ = try await service.signIn( handle: "reader.bsky.social", appPassword: "dedicated-app-password", authFactorToken: nil) let followers = try await service.loadProfileConnections(actorID: actorID, kind: .followers, cursor: nil) XCTAssertEqual(followers.accounts.map(\.id), ["did:plc:zoe", "did:plc:alex"]) let zoe = try XCTUnwrap(followers.accounts.first) XCTAssertEqual(zoe.name, "Zoe Lane") XCTAssertEqual(zoe.handle, "zoe.example") XCTAssertEqual(zoe.bio, "A bio.") XCTAssertEqual(zoe.avatarURL?.absoluteString, "https://cdn.example/avatar.jpg") XCTAssertEqual(followers.cursor, cursor) let next = try await service.loadProfileConnections(actorID: actorID, kind: .followers, cursor: followers.cursor) XCTAssertEqual(next.accounts.map(\.id), ["did:plc:next"]) XCTAssertNil(next.cursor) let following = try await service.loadProfileConnections(actorID: actorID, kind: .following, cursor: nil) XCTAssertEqual(following.accounts.map(\.id), ["did:plc:sam"]) } }