import XCTest @testable import LChatCore // 🤖 Regression guard for issue #580 (iOS threading). A stalled main thread let // a member's repeated taps on a chat row queue; each ran the list's // `path.append(.thread(id))`, so the NavigationStack ended up holding duplicate // copies of one chat and "back" surfaced a phantom second copy. The push must // be idempotent while that chat is already on top. // // This can't reproduce the underlying STALL (that lives in the SwiftUI layout // engine / gesture system), but it pins the state-machine invariant the stall // exposed: N taps on the same chat → exactly one push. final class ChatNavGuardTests: XCTestCase { // Model the list's onSelect reducer over a stack of pushed thread ids // (empty = the chat list on top). Mirrors ThreadListView's push. private func applyTaps(_ taps: [String], guarded: Bool) -> [String] { var stack: [String] = [] for id in taps { if !guarded || ChatNavGuard.shouldPush(threadID: id, topThreadID: stack.last) { stack.append(id) } } return stack } // The bug, reproduced: four queued taps on ONE chat push four copies when // unguarded — exactly the "back went to a second copy" report. func testQueuedTapsDuplicateWithoutGuard() { XCTAssertEqual(applyTaps(["T1", "T1", "T1", "T1"], guarded: false), ["T1", "T1", "T1", "T1"]) } // The fix: the same queued taps collapse to a single push. func testQueuedTapsCollapseWithGuard() { XCTAssertEqual(applyTaps(["T1", "T1", "T1", "T1"], guarded: true), ["T1"]) } // Guarding must not block legitimate navigation between DIFFERENT chats. func testDistinctChatsStillPush() { XCTAssertEqual(applyTaps(["T1", "T2", "T3"], guarded: true), ["T1", "T2", "T3"]) } // Re-opening a chat you just left is legitimate — it isn't on top anymore. func testReopenAfterLeavingPushes() { var stack = ["T1"] stack.removeLast() // back to the list if ChatNavGuard.shouldPush(threadID: "T1", topThreadID: stack.last) { stack.append("T1") } XCTAssertEqual(stack, ["T1"]) } func testShouldPushPredicate() { XCTAssertTrue(ChatNavGuard.shouldPush(threadID: "T1", topThreadID: nil)) // from the list XCTAssertTrue(ChatNavGuard.shouldPush(threadID: "T2", topThreadID: "T1")) // switching chats XCTAssertFalse(ChatNavGuard.shouldPush(threadID: "T1", topThreadID: "T1")) // already on top } }