From bdc31d3be375d69380228b8e5d55f5eaa6cfabb9 Mon Sep 17 00:00:00 2001 From: Islam <2553451+qalandarov@users.noreply.github.com> Date: Sun, 5 Oct 2025 11:36:35 +0100 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20auto-register=20mock=20BLE=20se?= =?UTF-8?q?rvices=20on=20creation=20(#746)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EndToEnd/PrivateChatE2ETests.swift | 26 ++++++++++++++++++- .../EndToEnd/PublicChatE2ETests.swift | 1 - .../Integration/IntegrationTests.swift | 1 - bitchatTests/Mocks/MockBLEService.swift | 6 ----- bitchatTests/README.md | 6 ++--- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/bitchatTests/EndToEnd/PrivateChatE2ETests.swift b/bitchatTests/EndToEnd/PrivateChatE2ETests.swift index 928b71e6..c7ce1bae 100644 --- a/bitchatTests/EndToEnd/PrivateChatE2ETests.swift +++ b/bitchatTests/EndToEnd/PrivateChatE2ETests.swift @@ -41,6 +41,31 @@ final class PrivateChatE2ETests: XCTestCase { // MARK: - Basic Private Messaging Tests + func testSimplePrivateMessageShouldNotBeSentWithoutConnection() { + // Intentionally commented out to test + // simulateConnection(alice, bob) + + let expectation = XCTestExpectation(description: "Bob should not receive a private message") + expectation.isInverted = true + + bob.messageDeliveryHandler = { message in + if message.content == TestConstants.testMessage1 && + message.isPrivate && + message.sender == TestConstants.testNickname1 { + expectation.fulfill() + } + } + + // Alice sends private message to Bob + alice.sendPrivateMessage( + TestConstants.testMessage1, + to: TestConstants.testPeerID2, + recipientNickname: TestConstants.testNickname2 + ) + + wait(for: [expectation], timeout: TestConstants.shortTimeout) + } + func testSimplePrivateMessage() { simulateConnection(alice, bob) @@ -280,7 +305,6 @@ final class PrivateChatE2ETests: XCTestCase { let service = MockBluetoothMeshService() service.myPeerID = peerID service.mockNickname = nickname - service._testRegister() return service } diff --git a/bitchatTests/EndToEnd/PublicChatE2ETests.swift b/bitchatTests/EndToEnd/PublicChatE2ETests.swift index 25e58055..46b9ef73 100644 --- a/bitchatTests/EndToEnd/PublicChatE2ETests.swift +++ b/bitchatTests/EndToEnd/PublicChatE2ETests.swift @@ -423,7 +423,6 @@ final class PublicChatE2ETests: XCTestCase { let service = MockBluetoothMeshService() service.myPeerID = peerID service.mockNickname = nickname - service._testRegister() return service } diff --git a/bitchatTests/Integration/IntegrationTests.swift b/bitchatTests/Integration/IntegrationTests.swift index 50249e37..39c951fb 100644 --- a/bitchatTests/Integration/IntegrationTests.swift +++ b/bitchatTests/Integration/IntegrationTests.swift @@ -591,7 +591,6 @@ final class IntegrationTests: XCTestCase { let node = MockBluetoothMeshService() node.myPeerID = peerID node.mockNickname = name - node._testRegister() nodes[name] = node // Create Noise manager diff --git a/bitchatTests/Mocks/MockBLEService.swift b/bitchatTests/Mocks/MockBLEService.swift index ce2730b0..23217c86 100644 --- a/bitchatTests/Mocks/MockBLEService.swift +++ b/bitchatTests/Mocks/MockBLEService.swift @@ -17,7 +17,6 @@ import CoreBluetooth /// simulated connections between peers. Tests call `simulateConnectedPeer` / /// `simulateDisconnectedPeer` to manage topology. /// - `resetTestBus()` clears global state and is called in test `setUp()`. -/// - `_testRegister()` registers a node immediately on creation for deterministic routing. /// - `messageDeliveryHandler` and `packetDeliveryHandler` let tests observe messages/packets /// as they flow, enabling scenarios like manual encryption/relay. /// - A thread-safe `seenMessageIDs` set prevents double-delivery races during flooding. @@ -110,11 +109,6 @@ final class MockBLEService: NSObject { if var setB = adjacency[b] { setB.remove(a); adjacency[b] = setB } } - /// Test-only: register this instance on the bus immediately. - func _testRegister() { - registerIfNeeded() - } - func startServices() { // Mock implementation - do nothing } diff --git a/bitchatTests/README.md b/bitchatTests/README.md index 75050151..061c5169 100644 --- a/bitchatTests/README.md +++ b/bitchatTests/README.md @@ -6,7 +6,7 @@ This test suite uses an in-memory networking harness to make end-to-end and inte - **File:** `bitchatTests/Mocks/MockBLEService.swift` - **Registry/Adjacency:** Global `registry` maps `peerID` to a `MockBLEService` instance; `adjacency` records simulated links between peers. -- **Setup:** Call `MockBLEService.resetTestBus()` in `setUp()` to clear state between tests; use `_testRegister()` when creating a node to register immediately. +- **Setup:** Call `MockBLEService.resetTestBus()` in `setUp()` to clear state between tests. - **Topology:** Use `simulateConnectedPeer(_:)` and `simulateDisconnectedPeer(_:)` to add/remove links. `connectFullMesh()` helpers in tests build larger topologies. - **Handlers:** Tests can observe data via `messageDeliveryHandler` (decoded `BitchatMessage`) and `packetDeliveryHandler` (raw `BitchatPacket`). - **De‑duplication:** A thread-safe `seenMessageIDs` prevents duplicate deliveries during flooding/relays. @@ -35,8 +35,8 @@ This test suite uses an in-memory networking harness to make end-to-end and inte ## Quick Start -- Create nodes with `_testRegister()` and connect them: - - `let svc = MockBLEService(); svc.myPeerID = "PEER1"; svc._testRegister()` +- Create nodes and connect them: + - `let svc = MockBLEService(); svc.myPeerID = "PEER1"` - `svc.simulateConnectedPeer("PEER2")` - Observe messages: - `svc.messageDeliveryHandler = { msg in /* asserts */ }`