import Observation import XCTest @testable import OldFella extension AppModelTests { func testOldThreadCleanupDoesNotClearNewSessionRequest() async { let gate = OverlapGate() let model = makeAppModel(service: ProfilePostTestService(overlapGate: gate)) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let old = Task { await model.loadPost(id: "post-2") } await gate.waitForCount(1) await model.signOut() await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let new = Task { await model.loadPost(id: "post-2") } await gate.waitForCount(2) await gate.releaseFirst() await old.value XCTAssertTrue(model.loadingPostIDs.contains("post-2")) await gate.releaseFirst() await new.value XCTAssertFalse(model.loadingPostIDs.contains("post-2")) } func testUnrelatedMutedPostDoesNotDiscardNotificationPage() async { let gate = ThreadGate() let page = FeedPage(posts: [], cursor: nil, mutedPostIDs: ["unrelated"]) let service = ProfilePostTestService(refreshedFeed: page, notificationGate: gate) let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let loading = Task { await model.loadNotifications() } await gate.waitUntilStarted() await model.loadFeed() await gate.release() let accepted = await loading.value XCTAssertTrue(accepted) XCTAssertFalse(model.notifications.isEmpty) XCTAssertNil(model.notificationsErrorMessage) XCTAssertTrue(service.base.notificationsSeenAt.isEmpty) } func testOldNotificationRequestDoesNotClearNewSessionLoading() async { let gate = OverlapGate() let model = makeAppModel(service: ProfilePostTestService(notificationOverlapGate: gate)) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let old = Task { await model.loadNotifications() } await gate.waitForCount(1) await model.signOut() await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let new = Task { await model.loadNotifications() } await gate.waitForCount(2) await gate.releaseFirst() let oldAccepted = await old.value XCTAssertFalse(oldAccepted) XCTAssertTrue(model.isLoadingNotifications) await gate.releaseFirst() let newAccepted = await new.value XCTAssertTrue(newAccepted) XCTAssertFalse(model.isLoadingNotifications) } func testFeedRefreshReconcilesMutedAndFreshPostsAcrossCaches() async throws { let base = MockBlueskyService() var fresh = try await base.loadFollowingFeed(cursor: nil).posts.first { $0.id == "post-3" }! fresh.quote = nil let page = FeedPage(posts: [fresh], cursor: nil, mutedPostIDs: ["post-1"]) let model = makeAppModel(service: ProfilePostTestService(refreshedFeed: page)) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadProfile(actorID: "mae") await model.loadProfile(actorID: fresh.author.id) await model.loadPost(id: "post-2") await model.loadNotifications() XCTAssertNotNil(model.post(id: "post-1")) XCTAssertNotNil(model.posts(for: fresh.author.id).first { $0.id == "post-3" }?.quote) await model.loadFeed() XCTAssertNil(model.post(id: "post-1")) XCTAssertFalse(model.posts(for: "mae").contains { $0.id == "post-1" }) XCTAssertFalse(model.threadContexts.values.joined().contains { $0.id == "post-1" }) XCTAssertFalse(model.notifications.contains { $0.relatedPost?.id == "post-1" || $0.parentPost?.id == "post-1" }) XCTAssertNil(model.posts(for: fresh.author.id).first { $0.id == "post-3" }?.quote) } func testThreadShowsAncestorsThenSelectedPostAndDirectReplies() async throws { let model = makeAppModel(service: MockBlueskyService()) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadPost(id: "post-2") let selected = try XCTUnwrap(model.post(id: "post-2")) XCTAssertEqual(model.threadAncestors(for: selected).map(\.id), ["post-1"]) XCTAssertEqual(model.directReplies(to: selected).map(\.id), []) XCTAssertEqual(model.directReplies(to: model.post(id: "post-1")!).map(\.id), ["post-2"]) } func testLongConversationShowsEveryAncestorAndFullThreadReplies() async throws { let model = makeAppModel(service: MockBlueskyService()) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadPost(id: "thread-selected") let selected = try XCTUnwrap(model.post(id: "thread-selected")) XCTAssertEqual( model.threadAncestors(for: selected).map(\.id), [ "thread-root", "thread-level-one", "thread-level-two" ]) XCTAssertEqual( model.directReplies(to: selected).map(\.id), [ "thread-direct", "thread-other-direct" ]) XCTAssertEqual( model.conversationReplies(to: selected).map(\.id), [ "thread-direct", "thread-nested", "thread-other-direct" ]) XCTAssertFalse(model.directReplies(to: selected).contains { $0.id == "thread-nested" }) } func testReplyCountUsesServerCountWhenChildrenAreNotLoaded() { let model = makeAppModel(service: MockBlueskyService()) let post = Post( id: "unloaded", author: Post.Author(id: "mae", name: "Mae", handle: "mae.example"), text: "Post", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: [], replyCount: 4) XCTAssertEqual(model.replyCount(for: post), 4) } func testReplyCountFallsBackToDistinctKnownDirectReplies() async throws { let model = makeAppModel(service: MockBlueskyService()) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadPost(id: "post-1") let root = try XCTUnwrap(model.post(id: "post-1")) XCTAssertEqual(model.directReplies(to: root).map(\.id), ["post-2"]) XCTAssertEqual(model.replyCount(for: root), 1) var negativeCountPost = root negativeCountPost.replyCount = -3 XCTAssertGreaterThanOrEqual(model.replyCount(for: negativeCountPost), 0) } func testDirectRepliesPreferThreadOrderAndExcludeGrandchildrenWithStaleReplyIDs() async throws { let service = MockBlueskyService( replyBranchesEnabled: true, threadReplyOrder: ["post-branch-b", "post-2", "post-branch-a"]) let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadPost(id: "post-1") let root = try XCTUnwrap(model.post(id: "post-1")) XCTAssertEqual(model.directReplies(to: root).map(\.id), ["post-branch-b", "post-2", "post-branch-a"]) XCTAssertEqual(model.replyCount(for: try XCTUnwrap(model.post(id: "post-branch-a"))), 1) XCTAssertEqual(model.replyCount(for: try XCTUnwrap(model.post(id: "post-branch-b"))), 0) XCTAssertFalse(model.directReplies(to: root).contains { $0.id == "post-branch-child" }) await model.loadPost(id: "post-branch-a") let branch = try XCTUnwrap(model.post(id: "post-branch-a")) XCTAssertEqual(model.threadAncestors(for: branch).map(\.id), ["post-1"]) XCTAssertEqual(model.directReplies(to: branch).map(\.id), ["post-branch-child"]) } func testReplyContextObservesParentOnlyChanges() async throws { let service = ModerationTestService() let author = Post.Author(id: "other", name: "Other", handle: "other") let parent = Post( id: "parent", author: author, text: "Original parent", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) var child = Post( id: "child", author: author, text: "Reply", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) child.replyRootURI = parent.id child.replyParentURI = parent.id service.feedPage = FeedPage(posts: [parent, child], cursor: nil) let model = makeAppModel(service: service) await model.signInWithDemo() let originalContexts = model.replyContexts let changes = ObservationProbe { model.replyContext(for: child) } var updated = parent updated.text = "Updated parent" updated.imageURLs = [URL(string: "https://example.com/updated.jpg")!] service.threadPosts = [updated] await model.loadPost(id: parent.id) await Task.yield() XCTAssertEqual(model.post(id: child.id), child) XCTAssertEqual(model.replyContexts, originalContexts) XCTAssertEqual(model.replyContext(for: child), [updated]) XCTAssertEqual(changes.values, [[updated]]) } }