This document describes BitChat's test infrastructure, including the test target organization, dependency injection patterns for testability, and the verification of core application logic. BitChat utilizes the Swift Testing framework for modern, concurrent test execution and state validation, alongside XCTest for performance benchmarking. The architecture emphasizes high-fidelity mocks to simulate complex networking environments, "pinning" tests to ensure stability during refactoring, and automated performance gates to prevent algorithmic regressions.
BitChat's testing suite is organized into functional areas within the bitchatTests/ directory, mirroring the main application's modular service architecture. The suite covers unit tests for individual services, integration tests for multi-node mesh behavior, and performance gates for algorithmic stability.
| Category | Description | Key Files |
|---|---|---|
| ViewModel Logic | Pinning tests to characterize ChatViewModel behavior and command routing. | ChatViewModelRefactoringTests.swift bitchatTests/ChatViewModelRefactoringTests.swift5-14 |
| Mocks | Test doubles for identity management, keychain, and transport services. | MockIdentityManager.swift bitchatTests/Mocks/MockIdentityManager.swift13 MockTransport.swift (referenced in bitchatTests/ChatViewModelRefactoringTests.swift18) |
| Utilities | Shared constants and helpers for asynchronous test waiting. | TestConstants.swift bitchatTests/TestUtilities/TestConstants.swift12-29 |
| Performance | Benchmarks hot paths to detect regressions against defined floors. | PerformanceBaselineTests (referenced in .github/workflows/swift-tests.yml76) |
| Local Packages | Independent test suites for core utility libraries. | localPackages/BitLogger, localPackages/BitFoundation (referenced in .github/workflows/swift-tests.yml26-29) |
Sources: bitchatTests/ChatViewModelRefactoringTests.swift5-14 bitchatTests/TestUtilities/TestConstants.swift12-29 .github/workflows/swift-tests.yml20-30 Package.swift47-68
BitChat employs protocol-based dependency injection to enable testing of components that interact with hardware, network relays, or persistent storage. The ChatViewModel is initialized with mock implementations of its core dependencies to isolate business logic from side effects.
The MockIdentityManager implements SecureIdentityStateManagerProtocol to simulate identity persistence and blocking logic without accessing the system Keychain bitchatTests/Mocks/MockIdentityManager.swift13-18 It maintains in-memory sets for blockedFingerprints and blockedNostrPubkeys bitchatTests/Mocks/MockIdentityManager.swift14-15
Diagram: Dependency Injection and Mocking Architecture
Sources: bitchatTests/ChatViewModelRefactoringTests.swift18-33 bitchatTests/Mocks/MockIdentityManager.swift13-18 bitchat/Services/TestEnvironment.swift15-25
Tests in ChatViewModelRefactoringTests act as a safety net during refactoring by pinning existing behavior bitchatTests/ChatViewModelRefactoringTests.swift5-7
/msg command is correctly routed to the transport's private messaging logic and does not leak into the public message stream bitchatTests/ChatViewModelRefactoringTests.swift38-64/block command successfully updates the MockIdentityManager state bitchatTests/ChatViewModelRefactoringTests.swift67-88The test suite validates how ChatViewModel handles incoming data from various transports:
privateChats dictionary, keyed by PeerID bitchatTests/ChatViewModelRefactoringTests.swift93-122The TestEnvironment enum provides process-level detection to swap OS-backed dependencies (Keychain, Notifications) for in-memory equivalents. It checks for the presence of XCTestCase, specific environment variables like CI or GITHUB_ACTIONS, and test configuration file paths bitchat/Services/TestEnvironment.swift15-25
Because BitChat is highly concurrent, tests use TestConstants to define standard timeouts for waitUntil helpers bitchatTests/TestUtilities/TestConstants.swift12-20
shortTimeout (1s): For local state transitions bitchatTests/TestUtilities/TestConstants.swift14longTimeout (10s): For work hopping through background queues or contending for the global executor on loaded CI runners bitchatTests/TestUtilities/TestConstants.swift20Sources: bitchatTests/ChatViewModelRefactoringTests.swift38-148 bitchat/Services/TestEnvironment.swift15-25 bitchatTests/TestUtilities/TestConstants.swift12-20
BitChat uses GitHub Actions defined in .github/workflows/swift-tests.yml to automate testing and enforce performance standards.
The scripts/check-perf-floors.sh script acts as a gate against algorithmic regressions (e.g., $O(n)$ to $O(n^2)$) scripts/check-perf-floors.sh3-12
PERF[...] lines from the test output and compares them against perf-floors.json scripts/check-perf-floors.sh51-110--parallel and a 5-minute watchdog that samples hung processes before killing them .github/workflows/swift-tests.yml75-91Diagram: CI/CD Execution and Performance Gate Flow
Sources: .github/workflows/swift-tests.yml10-165 scripts/check-perf-floors.sh3-181 Package.swift47-68
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.