mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 00:45:21 +00:00
Snapshot delivery status in message rows so read receipts render immediately
The publish chain was healthy: the store mutates the shared BitchatMessage and republishes, the .statusChanged fan-out reaches both mirrored conversations, and PrivateInboxModel fires objectWillChange for the selected DM under either key. The break was at the row view: TextMessageView/MediaMessageView stored the reference-typed message and read deliveryStatus in body, so SwiftUI's structural diff compared the field by identity - same instance, mutated in place, row body skipped. The blue tick waited for an unrelated invalidation (proven empirically with a hosting-view probe). Rows now snapshot deliveryStatus as a value at init; every republish rebuilds row values with a fresh enum, the diff sees the change, and the row re-renders immediately. Also fixes in-place send-progress updates in media rows. Regression tests cover both mirrored selection keyings at the feature-model level and the snapshot mechanic itself. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -298,6 +298,47 @@ struct AppArchitectureTests {
|
||||
#expect(inboxModel.messages(for: selectedPeerID).map(\.id) == ["dm-sel-1"])
|
||||
}
|
||||
|
||||
@Test("PrivateInboxModel republishes read receipts for the selected DM (ephemeral- and stable-keyed)")
|
||||
@MainActor
|
||||
func privateInboxModelRepublishesReadReceiptsForSelectedConversation() {
|
||||
// A DM's messages can live under BOTH .directPeer(ephemeral) and
|
||||
// .directPeer(stableKey) (mirroring shares one BitchatMessage
|
||||
// instance); the view's read-receipt update must fire no matter
|
||||
// which of the two keys the selection holds.
|
||||
let ephemeralPeerID = PeerID(str: "abcdef1234567890")
|
||||
let stablePeerID = PeerID(str: String(repeating: "ab", count: 32))
|
||||
|
||||
for selectedPeerID in [ephemeralPeerID, stablePeerID] {
|
||||
let store = ConversationStore()
|
||||
let inboxModel = PrivateInboxModel(conversations: store)
|
||||
store.setSelectedPrivatePeer(selectedPeerID)
|
||||
|
||||
// One shared instance mirrored into both direct conversations,
|
||||
// exactly like `mirrorToEphemeralIfNeeded`.
|
||||
let message = makeArchitectureMessage(
|
||||
id: "dm-read-1",
|
||||
isPrivate: true,
|
||||
senderPeerID: ephemeralPeerID
|
||||
)
|
||||
store.append(message, to: .directPeer(ephemeralPeerID))
|
||||
store.upsertByID(message, in: .directPeer(stablePeerID))
|
||||
|
||||
var emissions = 0
|
||||
let cancellable = inboxModel.objectWillChange.sink { _ in emissions += 1 }
|
||||
defer { cancellable.cancel() }
|
||||
|
||||
// ID-only intent — the exact call `ChatDeliveryCoordinator`
|
||||
// makes when a READ ack arrives.
|
||||
let read = DeliveryStatus.read(by: "builder", at: Date(timeIntervalSince1970: 100))
|
||||
#expect(store.setDeliveryStatus(read, forMessageID: "dm-read-1"))
|
||||
|
||||
// The fan-out emits .statusChanged for both containing
|
||||
// conversations; exactly the selected one republishes the model.
|
||||
#expect(emissions == 1)
|
||||
#expect(inboxModel.messages(for: selectedPeerID).first?.deliveryStatus == read)
|
||||
}
|
||||
}
|
||||
|
||||
@Test("PublicChatModel ignores appends to background conversations")
|
||||
@MainActor
|
||||
func publicChatModelIsolatesBackgroundConversations() {
|
||||
|
||||
@@ -629,6 +629,52 @@ struct ViewSmokeTests {
|
||||
#expect(playback.progress == 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
func messageRows_snapshotDeliveryStatusForSwiftUIDiffing() {
|
||||
// Regression: `BitchatMessage` is a reference type mutated in place
|
||||
// by `ConversationStore.applyDeliveryStatus`, and SwiftUI compares
|
||||
// reference-typed view fields by identity — so a status-only change
|
||||
// (delivered → read) on the SAME instance is invisible to the row's
|
||||
// structural diff and its body gets skipped even when the list
|
||||
// re-renders. The row views must therefore snapshot the status as a
|
||||
// value-typed stored property at init, so a rebuilt row value
|
||||
// compares different and re-renders.
|
||||
func deliveryStatusSnapshot(of row: Any) -> DeliveryStatus? {
|
||||
Mirror(reflecting: row).children
|
||||
.first { $0.label == "deliveryStatus" }?
|
||||
.value as? DeliveryStatus
|
||||
}
|
||||
|
||||
let delivered = DeliveryStatus.delivered(to: "builder", at: Date(timeIntervalSince1970: 50))
|
||||
let message = BitchatMessage(
|
||||
id: "dm-status-1",
|
||||
sender: "anon",
|
||||
content: "hello",
|
||||
timestamp: Date(),
|
||||
isRelay: false,
|
||||
isPrivate: true,
|
||||
recipientNickname: "builder",
|
||||
senderPeerID: PeerID(str: "abcdef1234567890"),
|
||||
deliveryStatus: delivered
|
||||
)
|
||||
|
||||
#expect(deliveryStatusSnapshot(of: TextMessageView(message: message)) == delivered)
|
||||
|
||||
// In-place mutation of the shared instance (what the store does on a
|
||||
// READ ack); a freshly built row must carry the new status value.
|
||||
let read = DeliveryStatus.read(by: "builder", at: Date(timeIntervalSince1970: 100))
|
||||
message.deliveryStatus = read
|
||||
|
||||
#expect(deliveryStatusSnapshot(of: TextMessageView(message: message)) == read)
|
||||
|
||||
let mediaRow = MediaMessageView(
|
||||
message: message,
|
||||
media: .image(URL(fileURLWithPath: "/tmp/never-loaded.jpg")),
|
||||
imagePreviewURL: .constant(nil)
|
||||
)
|
||||
#expect(deliveryStatusSnapshot(of: mediaRow) == read)
|
||||
}
|
||||
|
||||
#if os(iOS)
|
||||
@Test
|
||||
func cameraScannerView_previewAndCoordinatorSmoke() {
|
||||
|
||||
Reference in New Issue
Block a user