Files
bitchat/bitchatTests/Services/RelayControllerTests.swift
T
806c420313 Add comprehensive test coverage for ChatViewModel and BLEService (#962)
* Add comprehensive test coverage for ChatViewModel and BLEService

This commit adds 14 new tests to improve the safety net for future
refactoring of ChatViewModel and BLEService:

New test files:
- ChatViewModelTorTests: Tor lifecycle notification handlers (8 tests)
- ChatViewModelDeliveryStatusTests: Delivery status state machine (6 tests)
- ChatViewModelRefactoringTests: Command routing and message handling (4 tests)
- BLEServiceCoreTests: Packet deduplication and stale broadcast filtering (2 tests)
- PublicMessagePipelineTests: Message ordering and deduplication (4 tests)
- MessageRouterTests: Transport selection and outbox behavior (4 tests)
- PrivateChatManagerTests: Chat selection and read receipts (2 tests)
- UnifiedPeerServiceTests: Fingerprint resolution and blocking (2 tests)
- RelayControllerTests: TTL, handshake, and fragment relay logic (4 tests)

Modified files:
- MockTransport: Added peer snapshot publishing on connect/disconnect
- TestHelpers: Added waitUntil polling helper for async tests
- MockIdentityManager: Extended for new test scenarios

Total: 454 tests across 64 suites (was 440)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* Fix flaky test by using waitUntil instead of fixed sleep

Replace fixed 200ms sleep with waitUntil helper for more reliable
async assertion in routing tests. This prevents timing issues on CI.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-16 16:02:13 -10:00

96 lines
2.7 KiB
Swift

//
// RelayControllerTests.swift
// bitchatTests
//
// Tests for relay decision logic.
//
import Testing
import Foundation
@testable import bitchat
struct RelayControllerTests {
@Test
func ttlOne_doesNotRelay() async {
let decision = RelayController.decide(
ttl: 1,
senderIsSelf: false,
isEncrypted: false,
isDirectedEncrypted: false,
isFragment: false,
isDirectedFragment: false,
isHandshake: false,
isAnnounce: false,
degree: 0,
highDegreeThreshold: TransportConfig.bleHighDegreeThreshold
)
#expect(!decision.shouldRelay)
#expect(decision.newTTL == 1)
}
@Test
func handshake_alwaysRelaysWithTTLDecrement() async {
let decision = RelayController.decide(
ttl: 3,
senderIsSelf: false,
isEncrypted: false,
isDirectedEncrypted: false,
isFragment: false,
isDirectedFragment: false,
isHandshake: true,
isAnnounce: false,
degree: 3,
highDegreeThreshold: TransportConfig.bleHighDegreeThreshold
)
#expect(decision.shouldRelay)
#expect(decision.newTTL == 2)
#expect(decision.delayMs >= 10 && decision.delayMs <= 35)
}
@Test
func fragment_relaysWithFragmentCap() async {
let decision = RelayController.decide(
ttl: 10,
senderIsSelf: false,
isEncrypted: false,
isDirectedEncrypted: false,
isFragment: true,
isDirectedFragment: false,
isHandshake: false,
isAnnounce: false,
degree: 3,
highDegreeThreshold: TransportConfig.bleHighDegreeThreshold
)
let ttlCap = min(UInt8(10), TransportConfig.bleFragmentRelayTtlCap)
let expected = ttlCap &- 1
#expect(decision.shouldRelay)
#expect(decision.newTTL == expected)
#expect(decision.delayMs >= TransportConfig.bleFragmentRelayMinDelayMs)
#expect(decision.delayMs <= TransportConfig.bleFragmentRelayMaxDelayMs)
}
@Test
func denseGraph_capsTTL() async {
let decision = RelayController.decide(
ttl: 10,
senderIsSelf: false,
isEncrypted: false,
isDirectedEncrypted: false,
isFragment: false,
isDirectedFragment: false,
isHandshake: false,
isAnnounce: false,
degree: TransportConfig.bleHighDegreeThreshold,
highDegreeThreshold: TransportConfig.bleHighDegreeThreshold
)
#expect(decision.shouldRelay)
#expect(decision.newTTL == 4)
}
}