mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-11-29 08:29:22 +00:00
* Bump targets to iOS 17/macOS 14 and optimize PeerList/Nostr hot paths * Migrate all 11 AppEnvironmentModels feature models to SwiftUI @Observable * Inject AppRuntimeDependencies and decompose ChatViewModel into domain extensions * Decompose Nostr protocol/relay policies and chat coordinators * Harden strict concurrency and dead-code hygiene across core packages and app * Clear remaining iOS 17 deprecation warnings in BitchatApp and VoiceRecorder * Fix BLE redundant-link thrash, Tor -1011 bans, EOSE promotion, and duplicate startup subs * Cancel stale foreground Tor reconnect task across background cycles * Bump 2.0.0 build number to 4 * Address SwiftLint, Periphery, and Codex review findings * Preserve @Observable dependencies for group details, geohash counts, and live voice availability * Refresh inbox rows when geohash participant display names update --------- Co-authored-by: jack <jackjackbits@users.noreply.github.com>
293 lines
11 KiB
Swift
293 lines
11 KiB
Swift
//
|
|
// PeerListModelTests.swift
|
|
// bitchatTests
|
|
//
|
|
// This is free and unencumbered software released into the public domain.
|
|
// For more information, see <https://unlicense.org>
|
|
//
|
|
|
|
import BitFoundation
|
|
import Combine
|
|
import Foundation
|
|
import Observation
|
|
import Testing
|
|
@testable import bitchat
|
|
|
|
@MainActor
|
|
struct PeerListModelTests {
|
|
|
|
/// A refresh trigger that leaves the rows as they were must not trigger
|
|
/// SwiftUI `@Observable` invalidations on any tracked property.
|
|
@Test func refreshThatChangesNothingDoesNotRepublish() async {
|
|
let viewModel = makeViewModel()
|
|
let model = PeerListModel(
|
|
chatViewModel: viewModel,
|
|
conversations: viewModel.conversations,
|
|
locationChannelsModel: LocationChannelsModel(manager: makeLocationManager())
|
|
)
|
|
await drainMainQueue()
|
|
|
|
let changes = ObservationCounter()
|
|
withObservationTracking {
|
|
_ = model.meshRows
|
|
_ = model.geohashPeople
|
|
_ = model.groupRows
|
|
_ = model.recentChatRows
|
|
_ = model.inboxRows
|
|
_ = model.reachableMeshPeerCount
|
|
_ = model.connectedMeshPeerCount
|
|
_ = model.visibleGeohashPeerCount
|
|
} onChange: {
|
|
changes.increment()
|
|
}
|
|
let roster = viewModel.allPeers
|
|
for _ in 0..<5 {
|
|
NotificationCenter.default.post(name: Notification.Name("peerStatusUpdated"), object: nil)
|
|
// Every mesh snapshot republishes the roster, usually unchanged.
|
|
viewModel.allPeers = roster
|
|
}
|
|
await drainMainQueue()
|
|
|
|
#expect(changes.value == 0)
|
|
}
|
|
|
|
/// Under `@Observable`, updating direct-message inbox rows must NOT
|
|
/// invalidate views that only observe mesh peer counts or mesh rows.
|
|
@Test func inboxUpdateDoesNotInvalidateMeshObservation() async {
|
|
let viewModel = makeViewModel()
|
|
let model = PeerListModel(
|
|
chatViewModel: viewModel,
|
|
conversations: viewModel.conversations,
|
|
locationChannelsModel: LocationChannelsModel(manager: makeLocationManager())
|
|
)
|
|
await drainMainQueue()
|
|
|
|
let meshInvalidations = ObservationCounter()
|
|
let inboxInvalidations = ObservationCounter()
|
|
withObservationTracking {
|
|
_ = model.meshRows
|
|
_ = model.connectedMeshPeerCount
|
|
_ = model.reachableMeshPeerCount
|
|
} onChange: {
|
|
meshInvalidations.increment()
|
|
}
|
|
withObservationTracking {
|
|
_ = model.inboxRows
|
|
} onChange: {
|
|
inboxInvalidations.increment()
|
|
}
|
|
|
|
let peerID = PeerID(str: "00000000000000f4")
|
|
viewModel.conversations.append(
|
|
BitchatMessage(
|
|
id: "obs-dm-1",
|
|
sender: "alice",
|
|
content: "hello",
|
|
timestamp: Date(timeIntervalSince1970: 1_700_000_000),
|
|
isRelay: false,
|
|
isPrivate: true,
|
|
senderPeerID: peerID
|
|
),
|
|
to: .directPeer(peerID)
|
|
)
|
|
await drainMainQueue()
|
|
|
|
#expect(inboxInvalidations.value == 1)
|
|
#expect(meshInvalidations.value == 0)
|
|
}
|
|
|
|
/// `BitchatPeer.==` compares only peer IDs, so a roster in which someone
|
|
/// changed name, connection or favorite compares equal to the previous
|
|
/// one. Their row must still be rebuilt from it.
|
|
@Test func rosterChangeWithTheSameIDsUpdatesTheRow() async throws {
|
|
let transport = MockTransport()
|
|
let viewModel = makeViewModel(transport: transport)
|
|
let model = PeerListModel(
|
|
chatViewModel: viewModel,
|
|
conversations: viewModel.conversations,
|
|
locationChannelsModel: LocationChannelsModel(manager: makeLocationManager())
|
|
)
|
|
let peerID = PeerID(str: "00000000000000c7")
|
|
let noiseKey = Data(repeating: 0xC7, count: 32)
|
|
func snapshots(nickname: String) -> [TransportPeerSnapshot] {
|
|
[TransportPeerSnapshot(
|
|
peerID: peerID,
|
|
nickname: nickname,
|
|
isConnected: true,
|
|
noisePublicKey: noiseKey,
|
|
lastSeen: Date()
|
|
)]
|
|
}
|
|
|
|
// Through the transport rather than assigning `viewModel.allPeers`:
|
|
// anything else that rebuilds the roster (a favorites change from a
|
|
// parallel test) then yields these same peers.
|
|
transport.updatePeerSnapshots(snapshots(nickname: "alice"))
|
|
let listed = await TestHelpers.waitUntil({
|
|
model.meshRows.contains { $0.peerID == peerID && $0.displayName == "alice" }
|
|
}, timeout: TestConstants.settleTimeout)
|
|
try #require(listed)
|
|
|
|
// Same peer, same place in the roster, new name.
|
|
transport.updatePeerSnapshots(snapshots(nickname: "alicia"))
|
|
let renamed = await TestHelpers.waitUntil({
|
|
model.meshRows.contains { $0.peerID == peerID && $0.displayName == "alicia" }
|
|
}, timeout: TestConstants.settleTimeout)
|
|
#expect(renamed)
|
|
}
|
|
|
|
@Test func inboxPreviewsFormatMediaAndReactionsCleanly() async throws {
|
|
let viewModel = makeViewModel()
|
|
let model = PeerListModel(
|
|
chatViewModel: viewModel,
|
|
conversations: viewModel.conversations,
|
|
locationChannelsModel: LocationChannelsModel(manager: makeLocationManager())
|
|
)
|
|
|
|
let peerID = PeerID(str: "00000000000000a1")
|
|
let baseDate = Date(timeIntervalSince1970: 1_700_000_000)
|
|
|
|
// DM with a voice note should display "Voice message", never the raw filename.
|
|
viewModel.conversations.append(
|
|
BitchatMessage(
|
|
id: "dm-voice-1",
|
|
sender: "Lulose",
|
|
content: "[voice] 0D84B302-F93A-45D5-986A-CE9196C.m4a",
|
|
timestamp: baseDate,
|
|
isRelay: false,
|
|
isPrivate: true,
|
|
senderPeerID: peerID
|
|
),
|
|
to: .directPeer(peerID)
|
|
)
|
|
let hasVoiceRow = await TestHelpers.waitUntil({
|
|
model.inboxRows.first(where: { $0.peerID == peerID })?.subtitle == "Voice message"
|
|
}, timeout: TestConstants.settleTimeout)
|
|
#expect(hasVoiceRow)
|
|
|
|
// Searching by raw UUID must not match; searching by "voice" must match cleanly.
|
|
#expect(model.filteredInboxRows(matching: "0D84B302").isEmpty)
|
|
let voiceSearch = model.filteredInboxRows(matching: "voice")
|
|
#expect(voiceSearch.first(where: { $0.peerID == peerID })?.subtitle == "Voice message")
|
|
|
|
// DM ending with a reaction shows "Reacted ❤️"; toggling it off falls back to "Voice message".
|
|
viewModel.conversations.append(
|
|
BitchatMessage(
|
|
id: "dm-react-1",
|
|
sender: "Lulose",
|
|
content: "[react:dm-voice:❤️]",
|
|
timestamp: baseDate.addingTimeInterval(5),
|
|
isRelay: false,
|
|
isPrivate: true,
|
|
senderPeerID: peerID
|
|
),
|
|
to: .directPeer(peerID)
|
|
)
|
|
let hasReactRow = await TestHelpers.waitUntil({
|
|
model.inboxRows.first(where: { $0.peerID == peerID })?.subtitle == "Reacted ❤️"
|
|
}, timeout: TestConstants.settleTimeout)
|
|
#expect(hasReactRow)
|
|
#expect(model.filteredInboxRows(matching: "react:").isEmpty)
|
|
|
|
viewModel.conversations.append(
|
|
BitchatMessage(
|
|
id: "dm-react-2",
|
|
sender: "Lulose",
|
|
content: "[react:dm-voice:❤️]",
|
|
timestamp: baseDate.addingTimeInterval(6),
|
|
isRelay: false,
|
|
isPrivate: true,
|
|
senderPeerID: peerID
|
|
),
|
|
to: .directPeer(peerID)
|
|
)
|
|
let fellBackToVoice = await TestHelpers.waitUntil({
|
|
model.inboxRows.first(where: { $0.peerID == peerID })?.subtitle == "Voice message"
|
|
}, timeout: TestConstants.settleTimeout)
|
|
#expect(fellBackToVoice)
|
|
}
|
|
|
|
@Test func groupInboxPreviewFormatsSenderReactionAndMedia() async throws {
|
|
let viewModel = makeViewModel()
|
|
let model = PeerListModel(
|
|
chatViewModel: viewModel,
|
|
conversations: viewModel.conversations,
|
|
locationChannelsModel: LocationChannelsModel(manager: makeLocationManager())
|
|
)
|
|
|
|
let creator = GroupMember(
|
|
fingerprint: String(repeating: "a", count: 64),
|
|
signingKey: Data(repeating: 0x22, count: 32),
|
|
nickname: "jack"
|
|
)
|
|
let group = try #require(viewModel.groupStore.createGroup(named: "kooks", creator: creator))
|
|
let groupPeerID = group.peerID
|
|
let senderPeerID = PeerID(str: "00000000000000b2")
|
|
let baseDate = Date(timeIntervalSince1970: 1_700_000_100)
|
|
|
|
viewModel.conversations.append(
|
|
BitchatMessage(
|
|
id: "7831339C-group-msg",
|
|
sender: "jack",
|
|
content: "[image] ABCDEF12-3456.jpg",
|
|
timestamp: baseDate,
|
|
isRelay: false,
|
|
isPrivate: true,
|
|
senderPeerID: senderPeerID
|
|
),
|
|
to: .directPeer(groupPeerID)
|
|
)
|
|
let hasPhotoPreview = await TestHelpers.waitUntil({
|
|
model.inboxRows.first(where: { $0.peerID == groupPeerID })?.subtitle == "jack: Photo"
|
|
}, timeout: TestConstants.settleTimeout)
|
|
#expect(hasPhotoPreview)
|
|
|
|
viewModel.conversations.append(
|
|
BitchatMessage(
|
|
id: "group-react-1",
|
|
sender: "jack",
|
|
content: "[react:7831339C:❤️]",
|
|
timestamp: baseDate.addingTimeInterval(5),
|
|
isRelay: false,
|
|
isPrivate: true,
|
|
senderPeerID: senderPeerID
|
|
),
|
|
to: .directPeer(groupPeerID)
|
|
)
|
|
let hasReactionPreview = await TestHelpers.waitUntil({
|
|
model.inboxRows.first(where: { $0.peerID == groupPeerID })?.subtitle == "jack reacted ❤️"
|
|
}, timeout: TestConstants.settleTimeout)
|
|
#expect(hasReactionPreview)
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private func makeViewModel(transport: MockTransport = MockTransport()) -> ChatViewModel {
|
|
let keychain = MockKeychain()
|
|
return ChatViewModel(
|
|
keychain: keychain,
|
|
idBridge: NostrIdentityBridge(keychain: MockKeychainHelper()),
|
|
identityManager: MockIdentityManager(keychain),
|
|
transport: transport,
|
|
locationManager: makeLocationManager()
|
|
)
|
|
}
|
|
|
|
private func makeLocationManager() -> LocationChannelManager {
|
|
let suiteName = "PeerListModelTests.\(UUID().uuidString)"
|
|
let storage = UserDefaults(suiteName: suiteName) ?? .standard
|
|
storage.removePersistentDomain(forName: suiteName)
|
|
return LocationChannelManager(storage: storage)
|
|
}
|
|
|
|
/// The model's triggers hop to the main queue and then coalesce onto one
|
|
/// more hop, so let a few main-queue turns run.
|
|
private func drainMainQueue() async {
|
|
for _ in 0..<4 {
|
|
await withCheckedContinuation { continuation in
|
|
DispatchQueue.main.async { continuation.resume() }
|
|
}
|
|
}
|
|
}
|
|
}
|