Files
bitchat/bitchatTests/ProtocolContractTests.swift
T
jackandjack ef84fdcdd1 Refactor: Tier 2 deduplications, shared TLV/persistence/cache helpers, and RAII audio tokens (#1910)
* Deduplicate MessageOutboxStore recovery state machine and MessageRouter loops

* Unify NostrInboundPipeline and GeohashSubscriptionManager duplicate paths

* Remove dead notifyUIChanged() plumbing and retire BitchatDelegate

* Deduplicate BLEService core paths, link state types, and gossip sync

* Deduplicate NoiseProtocol DH operations, key loading, and rate limiter

* Consolidate AppSupport JSON stores, BoundedCache, Keychain upserts, and Favorites load

* Add shared TLVWriter/TLVReader in BitFoundation and migrate protocol codecs

* Deduplicate cross-platform views, utilities, and audio session RAII

* Address PR 1904 review: bound BoundedCache hot-key history and clean Periphery warnings

---------

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
2026-11-26 21:56:35 -10:00

96 lines
4.5 KiB
Swift

import Testing
import Foundation
import Combine
import CoreBluetooth
import BitFoundation
@testable import bitchat
private final class DefaultTransportProbe: Transport {
weak var eventDelegate: TransportEventDelegate?
weak var peerEventsDelegate: TransportPeerEventsDelegate?
let subject = CurrentValueSubject<[TransportPeerSnapshot], Never>([])
let myPeerID = PeerID(str: "0011223344556677")
var myNickname = "Tester"
private(set) var sentMessages: [(content: String, mentions: [String])] = []
func currentPeerSnapshots() -> [TransportPeerSnapshot] { subject.value }
func setNickname(_ nickname: String) { myNickname = nickname }
func startServices() {}
func stopServices() {}
func emergencyDisconnectAll() {}
func isPeerConnected(_ peerID: PeerID) -> Bool { false }
func isPeerReachable(_ peerID: PeerID) -> Bool { false }
func peerNickname(peerID: PeerID) -> String? { nil }
func getPeerNicknames() -> [PeerID: String] { [:] }
func getFingerprint(for peerID: PeerID) -> String? { nil }
func getNoiseSessionState(for peerID: PeerID) -> LazyHandshakeState { .none }
func triggerHandshake(with peerID: PeerID) {}
func sendMessage(_ content: String, mentions: [String]) { sentMessages.append((content, mentions)) }
func sendPrivateMessage(_ content: String, to peerID: PeerID, recipientNickname: String, messageID: String) {}
func sendReadReceipt(_ receipt: ReadReceipt, to peerID: PeerID) {}
func sendFavoriteNotification(to peerID: PeerID, isFavorite: Bool) {}
func sendBroadcastAnnounce() {}
func sendDeliveryAck(for messageID: String, to peerID: PeerID) {}
}
struct ProtocolContractTests {
@Test
func commandInfo_exposesAliasesPlaceholdersAndGeoVariants() {
// Aliases must match what CommandProcessor actually accepts —
// the suggestion panel is the only command-discovery surface.
#expect(CommandInfo.message.id == "msg")
#expect(CommandInfo.message.alias == "/msg")
#expect(CommandInfo.message.placeholder != nil)
#expect(CommandInfo.clear.placeholder == nil)
#expect(CommandInfo.favorite.description.isEmpty == false)
#expect(CommandInfo.all(isGeoPublic: false, isGeoDM: false).contains(.help))
// Favorites are rejected by the processor in geohash contexts, so
// they are suggested only in mesh.
#expect(CommandInfo.all(isGeoPublic: false, isGeoDM: false).contains(.favorite))
#expect(CommandInfo.all(isGeoPublic: true, isGeoDM: false).contains(.favorite) == false)
#expect(CommandInfo.all(isGeoPublic: false, isGeoDM: true).contains(.unfavorite) == false)
}
@Test
func protocolEnums_haveStableContracts() {
#expect(MessageType.requestSync.description == "requestSync")
#expect(NoisePayloadType.verifyResponse.description == "verifyResponse")
#expect(DeliveryStatus.sending.displayText == "Sending...")
#expect(DeliveryStatus.sent.displayText == "Sent")
#expect(DeliveryStatus.delivered(to: "Alice", at: Date()).displayText == "Delivered to Alice")
#expect(DeliveryStatus.read(by: "Bob", at: Date()).displayText == "Read by Bob")
#expect(DeliveryStatus.failed(reason: "oops").displayText == "Failed: oops")
#expect(DeliveryStatus.partiallyDelivered(reached: 1, total: 3).displayText == "Delivered to 1/3")
}
@Test
func transportDefaults_forwardOrNoOp() {
let probe = DefaultTransportProbe()
let peerID = PeerID(str: "0123456789abcdef")
probe.sendMessage("hello", mentions: ["@alice"], messageID: "msg-1", timestamp: Date())
#expect(probe.sentMessages.count == 1)
#expect(probe.sentMessages.first?.content == "hello")
// Mesh-only features are capability protocols now, not inert
// defaults: a core-only transport simply doesn't have them.
#expect(!(probe as AnyObject is MeshFileTransferring))
#expect(!(probe as AnyObject is MeshDiagnosing))
#expect(probe.peerCapabilities(peerID).isEmpty)
// Secure delivery defaults to prompt delivery (itself defaulting to
// reachability) for transports without a forgeable link layer.
#expect(probe.canDeliverSecurely(to: peerID) == false)
}
@Test
func previewMessage_exposesStableSampleShape() {
let preview = BitchatMessage.preview
#expect(preview.sender == "John Doe")
#expect(preview.content == "Hello")
#expect(preview.deliveryStatus == .sent)
#expect(preview.isPrivate == false)
}
}