From 2691019689a50e37d1252a35d627d513176d4a34 Mon Sep 17 00:00:00 2001 From: Taksh Kothari Date: Sat, 28 Nov 2026 01:02:28 +0530 Subject: [PATCH] fix(test): make SimulatedMeshTests storm bound deterministic (#1869) * test(sim): track delivered frame counts per MessageType in SimulatedMesh * fix(test): bound publicMessageRelaysAcrossLineTopologyWithinTTLBudget by .message frames --- bitchatTests/Simulation/SimulatedMesh.swift | 8 ++++++++ bitchatTests/Simulation/SimulatedMeshTests.swift | 8 +++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/bitchatTests/Simulation/SimulatedMesh.swift b/bitchatTests/Simulation/SimulatedMesh.swift index f9e35eba..4585169b 100644 --- a/bitchatTests/Simulation/SimulatedMesh.swift +++ b/bitchatTests/Simulation/SimulatedMesh.swift @@ -36,6 +36,13 @@ final class SimulatedMesh { private var pendingDeliveries: [(from: Int, packet: BitchatPacket)] = [] /// Total (packet, receiving-node) deliveries pumped — the storm bound. private(set) var deliveredFrameCount = 0 + /// Per-`MessageType` delivery counts for deterministic storm bounds that + /// are unaffected by unrelated periodic announce/sync frames. + private(set) var deliveredFrameCountsByType: [UInt8: Int] = [:] + + func deliveredFrameCount(ofType type: MessageType) -> Int { + deliveredFrameCountsByType[type.rawValue, default: 0] + } private(set) var nodes: [Node] = [] private var neighbors: [Set] = [] @@ -176,6 +183,7 @@ final class SimulatedMesh { for receiver in neighbors[from] { for link in links(from: from, at: receiver) { deliveredFrameCount += 1 + deliveredFrameCountsByType[packet.type, default: 0] += 1 nodes[receiver].service._test_ingestFrame(packet, link: link) } } diff --git a/bitchatTests/Simulation/SimulatedMeshTests.swift b/bitchatTests/Simulation/SimulatedMeshTests.swift index 155a92c7..e481ee25 100644 --- a/bitchatTests/Simulation/SimulatedMeshTests.swift +++ b/bitchatTests/Simulation/SimulatedMeshTests.swift @@ -129,7 +129,7 @@ struct SimulatedMeshTests { if now == settled { break } settled = now } - let baseline = mesh.deliveredFrameCount + let baselineMessageFrames = mesh.deliveredFrameCount(ofType: .message) let capture = TransportEventCapture() c.service.eventDelegate = capture @@ -143,8 +143,10 @@ struct SimulatedMeshTests { let arrived = await capture.drainedPublicMessageCount(content: "hello line") == 1 #expect(arrived) // Storm bound: a single public message across one relay hop must - // not multiply into more than a handful of frames. - #expect(mesh.deliveredFrameCount - baseline <= 12) + // not multiply into more than a handful of message frames. Measuring + // `.message` frames specifically prevents unrelated periodic announce + // or sync timers from flaking the bound on slow runners. + #expect(mesh.deliveredFrameCount(ofType: .message) - baselineMessageFrames <= 12) } @Test