import Observation import XCTest @testable import OldFella extension AppModelTests { func testFollowingAndProfileRepostAttributionRemainInSeparateMaps() async { let repostAuthor = Post.Author(id: "alex", name: "Alex Kim", handle: "alex.example") let service = ProfilePostTestService( initialRepostedBy: ["post-1": repostAuthor], followingRepostedBy: ["post-1": repostAuthor]) let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") XCTAssertEqual(model.repostedBy["post-1"], repostAuthor) await model.loadProfile(actorID: "mae") XCTAssertEqual(model.profileRepostedBy["mae"]?["post-1"], repostAuthor) XCTAssertNil(model.profileRepostedBy["alex"]?["post-1"]) await model.signOut() XCTAssertTrue(model.repostedBy.isEmpty) XCTAssertTrue(model.profileRepostedBy.isEmpty) } func testProfilePagesMergeReplyContextsAndActorScopedRepostAttribution() async { let root = Post( id: "profile-reply-root", author: Post.Author(id: "alex", name: "Alex Kim", handle: "alex.example"), text: "The start of this conversation.", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: [] ) var reply = Post( id: "profile-reply", author: Post.Author(id: "mae", name: "Mae Rivers", handle: "mae.example"), text: "A reply from the profile.", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: [] ) reply.replyRootURI = root.id reply.replyParentURI = root.id let initialReposter = Post.Author(id: "alex", name: "Alex Kim", handle: "alex.example") let laterReposter = Post.Author(id: "sam", name: "Sam Lee", handle: "sam.example") let service = ProfilePostTestService( additionalProfilePost: reply, profileReplyContexts: [reply.id: [root]], initialRepostedBy: ["post-1": initialReposter], laterRepostedBy: ["profile-post-more": laterReposter] ) let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadProfile(actorID: "mae") XCTAssertEqual(model.replyContext(for: reply).map(\.id), [root.id]) XCTAssertEqual(model.profileRepostedBy["mae"]?["post-1"], initialReposter) await model.loadNextProfilePage(actorID: "mae") XCTAssertEqual(model.profileRepostedBy["mae"]?["profile-post-more"], laterReposter) XCTAssertNil(model.profileRepostedBy["alex"]?["post-1"]) } func testProfilePaginationUsesCursorAndAppendsUniquePostsInServerOrder() async { let model = makeAppModel(service: MockBlueskyService()) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let timelineIDs = model.timelinePosts.map(\.id) await model.loadProfile(actorID: "mae") XCTAssertEqual(model.posts(for: "mae").map(\.id), ["post-1"]) XCTAssertEqual(model.profileCursors["mae"], "author-page-2-mae") XCTAssertFalse(model.posts.contains { $0.id == "profile-post-more" }) XCTAssertEqual(model.timelinePosts.map(\.id), timelineIDs) await model.loadNextProfilePage(actorID: "mae") XCTAssertEqual(model.posts(for: "mae").map(\.id), ["post-1", "profile-post-more"]) XCTAssertEqual(model.post(id: "profile-post-more")?.text, "Another post from Mae.") XCTAssertNil(model.profileCursors["mae"]) XCTAssertEqual(model.timelinePosts.map(\.id), timelineIDs) XCTAssertFalse(model.timelinePosts.contains { $0.id == "profile-post-more" }) } func testProfilePaginationFailureKeepsCursorAndExistingPostsForRetry() async { let service = MockBlueskyService(failsAuthorFeedLoadAtCall: 2) let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadProfile(actorID: "mae") let initialPosts = model.posts(for: "mae") let cursor = model.profileCursors["mae"] await model.loadNextProfilePage(actorID: "mae") XCTAssertEqual(model.posts(for: "mae"), initialPosts) XCTAssertEqual(model.profileCursors["mae"], cursor) XCTAssertNil(model.errorMessage) XCTAssertNotNil(model.profileErrors["mae"]) await model.loadNextProfilePage(actorID: "mae") XCTAssertEqual(model.posts(for: "mae").map(\.id), ["post-1", "profile-post-more"]) XCTAssertNil(model.profileCursors["mae"]) XCTAssertNil(model.errorMessage) XCTAssertEqual(service.authorFeedCursors, [nil, cursor, cursor]) } func testProfilePaginationIsIndependentAndGuardsDuplicateRequests() async { let service = MockBlueskyService(authorFeedResponseDelayNanoseconds: 80_000_000) let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadProfile(actorID: "mae") await model.loadProfile(actorID: "alex") let alexCursor = model.profileCursors["alex"] let firstLoad = Task { await model.loadNextProfilePage(actorID: "mae") } while !model.loadingMoreProfileIDs.contains("mae") { await Task.yield() } await model.loadNextProfilePage(actorID: "mae") await firstLoad.value XCTAssertEqual(service.authorFeedActorIDs, ["mae", "alex", "mae"]) XCTAssertEqual(service.authorFeedCursors, [nil, nil, "author-page-2-mae"]) XCTAssertEqual(model.posts(for: "alex").map(\.id), ["post-2"]) XCTAssertEqual(model.profileCursors["alex"], alexCursor) XCTAssertEqual(model.posts(for: "mae").map(\.id), ["post-1", "profile-post-more"]) } func testProfilePaginationKeepsCanonicalSharedPostState() async throws { let model = makeAppModel(service: MockBlueskyService()) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadProfile(actorID: "mae") await model.loadNextProfilePage(actorID: "mae") await model.toggleLike(postID: "profile-post-more") let profilePost = try XCTUnwrap(model.posts(for: "mae").last) let sharedPost = try XCTUnwrap(model.post(id: "profile-post-more")) XCTAssertTrue(profilePost.isLiked) XCTAssertEqual(profilePost.likeCount, 1) XCTAssertEqual(profilePost, sharedPost) } func testModerationUpdatesFeedOnlyAfterSuccess() async { let service = ModerationTestService() let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadModeration(actorID: "mae") await model.setModeration(actorID: "mae", block: false) XCTAssertEqual(service.actions, ["mute:true:mae"]) XCTAssertEqual(model.timelinePosts.map(\.id), ["post-3"]) await model.setModeration(actorID: "mae", block: false) XCTAssertEqual(model.timelinePosts.map(\.id), ["post-1", "post-3"]) await model.setModeration(actorID: "mae", block: true) XCTAssertTrue(model.moderationStates["mae"]?.isBlocked == true) await model.setModeration(actorID: "mae", block: true) XCTAssertFalse(model.moderationStates["mae"]?.isBlocked ?? true) XCTAssertEqual(service.actions.suffix(2), ["block:true:mae", "block:false:mae"]) } func testUndoModerationReloadsAnEmptyCachedProfile() async { for block in [false, true] { let service = ModerationTestService() let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadModeration(actorID: "mae") await model.setModeration(actorID: "mae", block: block) service.emptyAuthorPosts = true await model.loadProfile(actorID: "mae") XCTAssertEqual(model.authorPosts["mae"]?.count, 0) service.emptyAuthorPosts = false await model.setModeration(actorID: "mae", block: block) XCTAssertFalse(model.isHidden("mae")) XCTAssertEqual(service.authorPostLoads, 2) XCTAssertFalse(model.posts(for: "mae").isEmpty) } } func testQuoteVisibilityChecksEmbeddedAndFallbackAuthorsAndUndo() async throws { let service = ModerationTestService() let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let fallback = try XCTUnwrap(model.post(id: "post-1")) let quote = Post.Quote(postID: fallback.id, author: fallback.author, text: fallback.text) let missingAuthor = Post.Quote(postID: fallback.id) _ = await model.loadModeration(actorID: fallback.author.id) for block in [false, true] { await model.setModeration(actorID: fallback.author.id, block: block) XCTAssertTrue(model.isQuoteHidden(quote, fallbackPost: nil)) XCTAssertTrue(model.isQuoteHidden(missingAuthor, fallbackPost: fallback)) XCTAssertTrue(model.isQuoteHidden(quote, fallbackPost: fallback)) await model.setModeration(actorID: fallback.author.id, block: block) XCTAssertFalse(model.isQuoteHidden(quote, fallbackPost: fallback)) } } func testModerationRefreshAndNotificationFiltering() async { let service = ModerationTestService() let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadNotifications() await model.loadNextNotificationsPage() _ = await model.loadModeration(actorID: "mae") await model.setModeration(actorID: "mae", block: false) XCTAssertFalse( model.visibleNotifications.contains { $0.author.id == "mae" || $0.parentPost?.author.id == "mae" }) service.state = ModerationState() _ = await model.loadModeration(actorID: "mae") XCTAssertFalse(model.isHidden("mae")) XCTAssertEqual(model.visibleNotifications.count, 7) } func testModerationResultFromSignedOutSessionIsIgnored() async { let service = ModerationTestService() let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") service.suspendLoad = true let pending = Task { await model.loadModeration(actorID: "mae") } while service.pendingLoad == nil { await Task.yield() } await model.signOut() await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") service.pendingLoad?.resume(returning: ModerationState(isMuted: true)) _ = await pending.value XCTAssertNil(model.moderationStates["mae"]) XCTAssertFalse(model.isHidden("mae")) } }