Test unread resolution and conversation actions

Split the existing PR change into a smaller reviewable piece.
Original change: feat: clear unread badge for a single conversation
This commit is contained in:
Taksh committed 2026-10-05 16:10:53 +05:30
1 parent 8bdcfcce26
commit dfaa2488a8
2 files changed
+146

No files matched your search

@@ -85,6 +85,52 @@ struct ChatUnreadStateResolverTests {
))
}
@Test
func matchingUnreadPeerIDsIncludesStableNoiseKeyAlias() {
let peerID = PeerID(str: "ephemeral")
let stableID = PeerID(str: "stable")
let context = ChatUnreadPeerContext(
peerID: peerID,
noiseKeyPeerID: stableID,
nostrPeerID: nil,
nickname: nil
)
#expect(ChatUnreadStateResolver.matchingUnreadPeerIDs(
for: context,
unreadPrivateMessages: [stableID],
privateChats: [:]
) == [stableID])
}
@Test
func matchingUnreadPeerIDsIncludesGeoDMAlias() {
let peerID = PeerID(str: "mesh-peer")
let geoDM = PeerID(nostr_: "0123456789abcdef")
let context = ChatUnreadPeerContext(
peerID: peerID,
noiseKeyPeerID: nil,
nostrPeerID: nil,
nickname: "Alice"
)
let message = BitchatMessage(
sender: "alice",
content: "hi",
timestamp: Date(timeIntervalSince1970: 1),
isRelay: false,
originalSender: nil,
isPrivate: true,
recipientNickname: "me",
senderPeerID: geoDM
)
#expect(ChatUnreadStateResolver.matchingUnreadPeerIDs(
for: context,
unreadPrivateMessages: [geoDM],
privateChats: [geoDM: [message]]
) == [geoDM])
}
@Test
func unmatchedUnreadStateReturnsFalse() {
let peerID = PeerID(str: "mesh-peer")
@@ -0,0 +1,100 @@
import BitFoundation
import Foundation
import Testing
@testable import bitchat
@MainActor
struct ClearUnreadConversationTests {
@Test("markPrivateChatRead clears unread without removing messages")
func markPrivateChatRead_clearsBadgeOnly() {
let viewModel = makeViewModel()
let peerID = PeerID(str: "0011223344556677")
let message = BitchatMessage(
sender: "alice",
content: "ping",
timestamp: Date(),
isRelay: false,
isPrivate: true,
senderPeerID: peerID
)
viewModel.appendPrivateMessage(message, to: peerID)
viewModel.markPrivateChatUnread(peerID)
#expect(viewModel.hasUnreadMessages(for: peerID))
#expect(viewModel.privateMessages(for: peerID).count == 1)
viewModel.markPrivateChatRead(peerID)
#expect(!viewModel.hasUnreadMessages(for: peerID))
#expect(viewModel.privateMessages(for: peerID).count == 1)
}
@Test("PeerListModel.clearUnread forwards to the conversation store")
func peerListModel_clearUnread() {
let viewModel = makeViewModel()
let conversations = viewModel.conversations
let locationChannelsModel = LocationChannelsModel()
let peerListModel = PeerListModel(
chatViewModel: viewModel,
conversations: conversations,
locationChannelsModel: locationChannelsModel
)
let peerID = PeerID(str: "0011223344556677")
conversations.markUnread(.directPeer(peerID))
#expect(viewModel.hasUnreadMessages(for: peerID))
peerListModel.clearUnread(for: peerID)
#expect(!viewModel.hasUnreadMessages(for: peerID))
}
@Test("ConversationUIModel.clearUnread clears one peer badge")
func conversationUIModel_clearUnread() {
let viewModel = makeViewModel()
let privateConversationModel = PrivateConversationModel(
chatViewModel: viewModel,
conversations: viewModel.conversations
)
let uiModel = ConversationUIModel(
chatViewModel: viewModel,
privateConversationModel: privateConversationModel,
conversations: viewModel.conversations
)
let peerID = PeerID(str: "0011223344556677")
viewModel.markPrivateChatUnread(peerID)
uiModel.clearUnread(for: peerID)
#expect(!viewModel.hasUnreadMessages(for: peerID))
}
@Test("markPrivateChatRead is a no-op when the conversation is already read")
func markPrivateChatRead_idempotent() {
let viewModel = makeViewModel()
let peerID = PeerID(str: "0011223344556677")
viewModel.markPrivateChatRead(peerID)
viewModel.markPrivateChatUnread(peerID)
viewModel.markPrivateChatRead(peerID)
viewModel.markPrivateChatRead(peerID)
#expect(!viewModel.hasUnreadMessages(for: peerID))
}
}
@MainActor
private func makeViewModel() -> ChatViewModel {
let keychain = MockKeychain()
let keychainHelper = MockKeychainHelper()
let idBridge = NostrIdentityBridge(keychain: keychainHelper)
let identityManager = MockIdentityManager(keychain)
return ChatViewModel(
keychain: keychain,
idBridge: idBridge,
identityManager: identityManager,
transport: MockTransport()
)
}