Files
bitchat/bitchatTests/Mocks/MockBLEBus.swift
T
3d914dcf46 Convert the remaining tests to Swift Testing (#781)
* SwiftTesting: NoiseProtocolTests + BinaryProtocolPaddingTests

* SwiftTesting: `NotificationStreamAssemblerTests`

* SwiftTesting: `NostrProtocolTests`

* SwiftTesting: `BinaryProtocolTests`

* SwiftTesting: `PeerIDTests`

* SwiftTesting: `BLEServiceTests`

* SwiftTesting: `CommandProcessorTests`

* SwiftTesting: `GCSFilterTests`

* SwiftTesting: `GeohashBookmarksStoreTests`

* Remove `peerID` test constants

* Remove PeerID + String interop from tests

* Refactor IntegrationTests to extract state management

* Refactor global state management of MockBLEService

* NoiseProtocolSwiftTests: `actor` -> `struct`

* Remove measurement tests w/ no benchmark

* `NoiseProtocolSwiftTests` -> `NoiseProtocolTests`

* SwiftTesting: `LocationChannelsTests`

* SwiftTesting: `GossipSyncManagerTests`

* SwiftTesting: `LocationNotesManagerTests`

* Global `sleep` function for tests

* SwiftTesting: `IntegrationTests`

---------

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
2025-10-15 01:04:01 +02:00

58 lines
1.6 KiB
Swift

//
// MockBLEBus.swift
// bitchatTests
//
// This is free and unencumbered software released into the public domain.
// For more information, see <https://unlicense.org>
//
import Foundation
@testable import bitchat
final class MockBLEBus {
private var registry: [PeerID: MockBLEService] = [:]
private var adjacency: [PeerID: Set<PeerID>] = [:]
// Enable automatic flooding for public messages in integration tests only
let autoFloodEnabled: Bool
init(autoFloodEnabled: Bool = false) {
self.autoFloodEnabled = autoFloodEnabled
}
func register(_ service: MockBLEService, for peerID: PeerID) {
registry[peerID] = service
if adjacency[peerID] == nil { adjacency[peerID] = [] }
}
func connect(_ a: PeerID, _ b: PeerID) {
var setA = adjacency[a] ?? []
setA.insert(b)
adjacency[a] = setA
var setB = adjacency[b] ?? []
setB.insert(a)
adjacency[b] = setB
}
func disconnect(_ a: PeerID, _ b: PeerID) {
if var setA = adjacency[a] { setA.remove(b); adjacency[a] = setA }
if var setB = adjacency[b] { setB.remove(a); adjacency[b] = setB }
}
func neighbors(of peerID: PeerID) -> [MockBLEService] {
let ids = adjacency[peerID] ?? []
let result = ids.compactMap { registry[$0] }
return result
}
func isDirectNeighbor(_ a: PeerID, _ b: PeerID) -> Bool {
let res = adjacency[a]?.contains(b) ?? false
return res
}
func service(for peerID: PeerID) -> MockBLEService? {
let svc = registry[peerID]
return svc
}
}