import Foundation import UIKit extension MockBlueskyService { func loadProfile(actorID: String) async throws -> Profile? { if ProcessInfo.processInfo.arguments.contains("-ui-testing-profile-details-delay") { try await Task.sleep(nanoseconds: 3_000_000_000) } if actorID == account.id { if let edited = loadState.editedProfile { return Profile( author: Post.Author(id: account.id, name: edited.draft.displayName, handle: account.handle, avatarURL: edited.avatarURL), bio: edited.draft.bio, location: nil, website: nil, followerCount: 780, followingCount: 730, links: edited.draft.links.compactMap { link in URL(string: link.address).map { Profile.Link(id: link.id.uuidString, title: link.label, url: $0) } }) } return Profile( author: Post.Author( id: account.id, name: account.displayName, handle: account.handle, moderationLabels: ProcessInfo.processInfo.arguments.contains("-ui-testing-info-label") ? [.init(value: "scam", sourceDID: account.id)] : []), bio: "somewhere between nature and tech is a programmer lusting after bikes, that programmer is me\n\n" + "🇮🇹/🇬🇧\n\nDM for Signal if you're a mutual.\n\n" + "Doing software things at Bluesky, views & shitposts are my own", location: nil, website: nil, followerCount: 780, followingCount: 730, links: [ Profile.Link( id: "photos", title: "My Photos!", url: URL(string: "https://geesawra.github.io/crossgrain/did:plc:6ll5xi67lyuyovt6fiv4fnjo")!), Profile.Link(id: "github", title: "GitHub", url: URL(string: "https://github.com/geesawra")!), Profile.Link( id: "hidden", title: "Hidden link", url: URL(string: "https://example.org")!, moderationLabels: [.init(value: "!warn", sourceDID: account.id)]) ] ) } return profiles.first { $0.author.id == actorID } } func loadProfileConnections(actorID: String, kind: ProfileConnectionKind, cursor: String?) async throws -> ProfileConnectionsPage { guard actorID == account.id || profiles.contains(where: { $0.author.id == actorID }) else { return ProfileConnectionsPage(accounts: [], cursor: nil) } if cursor != nil, kind == .followers, !loadState.didFailProfileConnectionsPage, ProcessInfo.processInfo.arguments.contains("-ui-testing-profile-connections-failure") { loadState.didFailProfileConnectionsPage = true throw URLError(.notConnectedToInternet) } let candidates = profiles.filter { $0.author.id != actorID } let ordered = kind == .followers ? candidates : Array(candidates.reversed()) let start = cursor.flatMap(Int.init) ?? 0 let accounts = ordered.dropFirst(start).prefix(1).map { profile in var author = profile.author author.bio = profile.bio return author } return ProfileConnectionsPage( accounts: accounts, cursor: start + accounts.count < ordered.count ? String(start + accounts.count) : nil) } func loadProfileEdit(accountID: String) async throws -> EditableProfile { guard accountID == account.id else { throw ServiceError.noSession } if let edited = loadState.editedProfile { return edited } guard let profile = try await loadProfile(actorID: accountID) else { throw ServiceError.profileUnavailable } let avatarURL = ProcessInfo.processInfo.arguments.contains("-ui-testing-profile-avatar") ? URL(string: "data:image/png;base64,\(Self.imageGridPNGData.base64EncodedString())") : profile.author.avatarURL return EditableProfile(accountID: accountID, recordCID: "original", avatarURL: avatarURL, draft: ProfileEdit( displayName: profile.author.name, bio: profile.bio, links: profile.links.map { .init(title: $0.title, address: $0.url.absoluteString) })) } func saveProfileEdit(_ draft: ProfileEdit, original: EditableProfile, accountID: String) async throws -> ProfileEditResult { guard accountID == account.id, original.accountID == accountID else { throw ServiceError.noSession } loadState.profileSaveAttempts += 1 if loadState.profileSaveAttempts == 1, ProcessInfo.processInfo.arguments.contains("-ui-testing-profile-save-failure") { throw URLError(.notConnectedToInternet) } let avatarURL: URL? switch draft.avatar { case .keep: avatarURL = original.avatarURL case .remove: avatarURL = nil case .replace: avatarURL = URL(string: "https://example.test/avatar.jpg") } var saved = draft saved.avatar = .keep loadState.editedProfile = EditableProfile( accountID: accountID, recordCID: UUID().uuidString, avatarURL: avatarURL, draft: saved) return ProfileEditResult( account: Account(handle: account.handle, displayName: saved.displayName, id: account.id, avatarURL: avatarURL), linkIDs: saved.links.map { $0.id.uuidString }) } func loadAuthorPosts(actorID: String, cursor: String?) async throws -> FeedPage { loadState.authorFeedLoadCount += 1 loadState.authorFeedActorIDs.append(actorID) loadState.authorFeedCursors.append(cursor) if ProcessInfo.processInfo.arguments.contains("-ui-testing-profile-fast-load") { try await Task.sleep(nanoseconds: loadState.authorFeedLoadCount == 1 ? 2_000_000_000 : 6_000_000_000) } if authorFeedResponseDelayNanoseconds > 0 { try await Task.sleep(nanoseconds: authorFeedResponseDelayNanoseconds) } if (failsFirstAuthorFeedLoad && loadState.authorFeedLoadCount == 1) || failsAuthorFeedLoadAtCall == loadState.authorFeedLoadCount { throw ServiceError.requestFailed("The author feed failed.") } if usesRichScrollFixture { return cursor == nil ? richScrollFeedPage(authorID: actorID) : FeedPage(posts: [], cursor: nil) } guard cursor != nil else { if ProcessInfo.processInfo.arguments.contains("-ui-testing-profile-fast-load"), loadState.authorFeedLoadCount > 1 { let refreshedPost = Post( id: "profile-post-refreshed", author: authors[0], text: "Another post from Mae.", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) return FeedPage( posts: [refreshedPost] + firstPage.filter { $0.author.id == actorID }, cursor: "author-page-2-\(actorID)") } return FeedPage(posts: firstPage.filter { $0.author.id == actorID }, cursor: "author-page-2-\(actorID)") } guard actorID == "mae" else { return FeedPage(posts: [], cursor: nil) } let morePost = Post( id: "profile-post-more", author: authors[0], text: "Another post from Mae.", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) let duplicate = firstPage.first { $0.author.id == actorID } return FeedPage(posts: [duplicate, morePost].compactMap { $0 }.map(withInteractions), cursor: nil) } }