import Observation import XCTest @testable import OldFella extension AppModelTests { func testReplyContextDoesNotObserveUnrelatedNotificationPosts() async throws { let service = ModerationTestService() let author = Post.Author(id: "other", name: "Other", handle: "other", isFollowing: true) func post(_ id: String, text: String) -> Post { Post( id: id, author: author, text: text, likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) } let parent = post("parent", text: "Original parent") var child = post("child", text: "Reply") child.replyRootURI = parent.id child.replyParentURI = parent.id let standalone = post("standalone", text: "A separate post") let unrelated = post("notification-post", text: "An unrelated notification post") service.feedPage = FeedPage( posts: [child, parent, standalone], cursor: nil, replyContexts: [child.id: [parent]]) setUnrelatedNotification(service, author: author, post: unrelated) let model = makeAppModel(service: service) await model.signInWithDemo() XCTAssertEqual(model.timelineRowIDs, [child.id, standalone.id]) XCTAssertEqual(model.replyContext(for: child), [parent]) XCTAssertTrue(model.replyContext(for: standalone).isEmpty) let originalContexts = model.replyContexts let replyChanges = ObservationProbe { model.replyContext(for: child) } let standaloneChanges = ObservationProbe { model.replyContext(for: standalone) } await model.loadNotifications() await Task.yield() XCTAssertEqual(model.post(id: unrelated.id), unrelated) XCTAssertEqual(model.timelineRowIDs, [child.id, standalone.id]) XCTAssertEqual(model.replyContexts, originalContexts) XCTAssertEqual(model.replyContext(for: child), [parent]) XCTAssertTrue(model.replyContext(for: standalone).isEmpty) XCTAssertEqual(replyChanges.invalidationCount, 0) XCTAssertTrue(replyChanges.values.isEmpty, "An unrelated cached post must not update reply context") XCTAssertEqual(standaloneChanges.invalidationCount, 0) XCTAssertTrue(standaloneChanges.values.isEmpty, "An unrelated cached post must not update a standalone row") replyChanges.reset() standaloneChanges.reset() var updated = parent updated.text = "Updated parent" service.threadPosts = [updated] await model.loadPost(id: parent.id) await Task.yield() XCTAssertEqual(model.replyContext(for: child), [updated]) XCTAssertEqual(replyChanges.invalidationCount, 1) XCTAssertEqual(replyChanges.values, [[updated]]) XCTAssertEqual(standaloneChanges.invalidationCount, 0) XCTAssertTrue(standaloneChanges.values.isEmpty) assertSyntheticReplyContext(model, child: child, standalone: standalone, updated: updated) } func testPreparedFeedRowsKeepIdentityAcrossContentUpdates() async throws { let model = makeAppModel(service: MockBlueskyService()) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let rows = model.timelineRows let first = try XCTUnwrap(rows.first) XCTAssertEqual(rows.map(\.id), model.timelinePosts.map(\.id)) for row in rows { XCTAssertTrue(row === model.postViewState(for: row.post)) } let changes = ObservationProbe { model.timelineRows.map(\.id) } await model.toggleLike(postID: first.id) await model.refreshFeed() await Task.yield() XCTAssertTrue(first.post.isLiked) XCTAssertTrue(zip(model.timelineRows, rows).allSatisfy { $0 === $1 }) XCTAssertTrue(changes.values.isEmpty) await model.hideAccountFromTimeline(actorID: first.post.author.id, block: false) XCTAssertFalse(model.timelineRows.contains { $0.post.author.id == first.post.author.id }) await model.signOut() XCTAssertTrue(model.timelineRows.isEmpty) } func testLikeObservesOnlyTheChangedRow() async throws { let service = ActionFailureTestService() service.failActions = false service.actionDelay = 100_000_000 let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let target = model.postViewState(for: try XCTUnwrap(model.post(id: "post-1"))) let other = model.postViewState(for: try XCTUnwrap(model.post(id: "post-2"))) let account = ObservationProbe { (model.account, model.isRestoringSession, model.errorMessage) } let timeline = ObservationProbe { (model.hasUnreadNotifications, model.timelineRowIDs) } let otherRow = ObservationProbe { (other.post, other.isBusy, other.isUncertain, other.decision, other.quotedPost) } let busy = ObservationProbe { target.isBusy } let post = ObservationProbe { target.post } let original = target.post let action = Task { await model.toggleLike(postID: original.id) } while !target.isBusy { await Task.yield() } await Task.yield() XCTAssertEqual(busy.values, [true]) await action.value await Task.yield() XCTAssertEqual(busy.values, [true, false]) XCTAssertEqual(post.values.count, 1) XCTAssertTrue(target.post.isLiked) XCTAssertEqual(target.post.likeCount, original.likeCount + 1) XCTAssertTrue(account.values.isEmpty) XCTAssertTrue(timeline.values.isEmpty) XCTAssertTrue(otherRow.values.isEmpty) } func testPostRowObservesModerationDecisionChanges() async throws { let service = ModerationTestService() let author = Post.Author(id: "other", name: "Other", handle: "other") let post = Post( id: "post", author: author, text: "hidden text", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) service.feedPage = FeedPage(posts: [post], cursor: nil) let model = makeAppModel(service: service) await model.signInWithDemo() let state = model.postViewState(for: post) let decisions = ObservationProbe { state.decision } service.mutedWords = [.init(value: "hidden")] await model.reloadContentPreferences() await Task.yield() XCTAssertEqual(decisions.values.count, 1) XCTAssertTrue(decisions.values.allSatisfy { $0 == nil }) XCTAssertNil(state.decision) await model.loadFeed() let reloaded = model.postViewState(for: post) XCTAssertEqual(reloaded.decision?.action, .hide) XCTAssertTrue(model.timelineRowIDs.isEmpty) } func testFailedLikeUpdatesOnlyItsRow() async throws { for unknown in [false, true] { let service = ActionFailureTestService() service.actionDelay = 100_000_000 if !unknown { service.likeFailure = ActionNotSentError(underlying: ServiceError.invalidResponse) } let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let target = model.postViewState(for: try XCTUnwrap(model.post(id: "post-1"))) let other = model.postViewState(for: try XCTUnwrap(model.post(id: "post-2"))) let original = target.post let busy = ObservationProbe { target.isBusy } let uncertain = ObservationProbe { target.isUncertain } let otherRow = ObservationProbe { (other.post, other.isBusy, other.isUncertain, other.decision, other.quotedPost) } let action = Task { await model.toggleLike(postID: original.id) } while !target.isBusy { await Task.yield() } await Task.yield() XCTAssertEqual(busy.values, [true]) await action.value await Task.yield() XCTAssertEqual(busy.values, [true, false]) XCTAssertEqual(target.post, original) XCTAssertEqual(target.isUncertain, unknown) XCTAssertEqual(uncertain.values, unknown ? [true] : []) XCTAssertTrue(otherRow.values.isEmpty) XCTAssertNotNil(model.errorMessage) } } func testLikeAndRepostUpdateCounts() async { let model = makeAppModel(service: MockBlueskyService()) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.toggleLike(postID: "post-1") await model.toggleRepost(postID: "post-1") XCTAssertEqual(model.post(id: "post-1")?.likeCount, 19) XCTAssertEqual(model.post(id: "post-1")?.repostCount, 4) await model.toggleLike(postID: "post-1") XCTAssertEqual(model.post(id: "post-1")?.likeCount, 18) } func testProfileInteractionsUpdateCanonicalFeedPost() async throws { let model = makeAppModel(service: MockBlueskyService()) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadProfile(actorID: "mae") let initialTimelineIDs = model.timelinePosts.map(\.id) await model.toggleLike(postID: "post-1") await model.toggleRepost(postID: "post-1") let likedProfilePost = try XCTUnwrap(model.posts(for: "mae").first) let likedSharedPost = try XCTUnwrap(model.post(id: "post-1")) XCTAssertTrue(likedProfilePost.isLiked) XCTAssertEqual(likedProfilePost.likeCount, 19) XCTAssertTrue(likedProfilePost.isReposted) XCTAssertEqual(likedProfilePost.repostCount, 4) XCTAssertEqual(likedProfilePost, likedSharedPost) await model.toggleLike(postID: "post-1") await model.toggleRepost(postID: "post-1") let unlikedProfilePost = try XCTUnwrap(model.posts(for: "mae").first) let unlikedSharedPost = try XCTUnwrap(model.post(id: "post-1")) XCTAssertFalse(unlikedProfilePost.isLiked) XCTAssertEqual(unlikedProfilePost.likeCount, 18) XCTAssertFalse(unlikedProfilePost.isReposted) XCTAssertEqual(unlikedProfilePost.repostCount, 3) XCTAssertEqual(unlikedProfilePost, unlikedSharedPost) XCTAssertEqual(model.timelinePosts.map(\.id), initialTimelineIDs) } private func setUnrelatedNotification(_ service: ModerationTestService, author: Post.Author, post: Post) { service.notificationPage = NotificationPage( notifications: [ FeedNotification( id: "notification", author: author, reason: .like, indexedAt: Date(timeIntervalSince1970: 1_800_000_000), isRead: false, postID: post.id, relatedPost: post) ], cursor: nil) } private func assertSyntheticReplyContext( _ model: AppModel, child: Post, standalone: Post, updated: Post ) { var synthetic = child synthetic.replyRootURI = standalone.id synthetic.replyParentURI = standalone.id XCTAssertEqual(model.replyContext(for: synthetic), [standalone]) XCTAssertEqual(model.replyContext(for: child), [updated]) } }