Files
bitchat/bitchatTests/Services/GeohashPresenceServiceTests.swift
T
jackandClaude Opus 5 ac13a0c22f Make the flake class unrepeatable, not just fixed
Raising four deadlines fixed the four tests that happened to fire. The
class was still there: fourteen separate waitUntil helpers, most defaulting
to 1.0s, plus wait call sites with literal budgets. The fifth instance
would have landed the same way, on someone else's unrelated PR.

The rule, now written down in TestConstants.settleTimeout: **a wait
deadline is not a latency budget.** It exists so a genuine hang eventually
fails the suite, so size it for the worst-case scheduler, never for how
long the operation should take. Waits return as soon as their condition
holds, so a generous deadline is free in the passing case and only extends
genuine failures.

- Every wait helper now defaults to TestConstants.settleTimeout (30s), and
  the literal wait call sites below the floor were converted too — sixteen
  sites across ten files.
- TestTimingHygieneTests enforces it by scanning the test sources: wait
  defaults and wait call sites must be at least minimumSettleTimeout, and
  no test may assert an upper bound on elapsed wall-clock time (the
  assertion that started this, which cannot separate correct behaviour from
  a slow machine).
- Both rules waive per line with "test-timing-ok: <reason>", accepted on
  the line or in the comment block above it so the reason has room to be a
  sentence. One legitimate use so far: a NEGATIVE wait in NoiseCoverageTests
  asserting a promotion has *not* completed within 50ms, where a long
  deadline would only make the suite slow while still passing.

Injected production timeouts are deliberately not matched — the Noise
handshake timeouts are the behaviour under test, and short values are
correct there.

Verified the guard actually fails: a canary file with both banned shapes
was flagged with file and line, and the suite went green again once it was
removed. Full suite passes with all 18 cores saturated.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-26 22:23:40 +01:00

270 lines
9.5 KiB
Swift

import Combine
import XCTest
@testable import bitchat
@MainActor
final class GeohashPresenceServiceTests: XCTestCase {
func test_start_schedulesHeartbeatUsingConfiguredInterval() {
let scheduler = MockGeohashPresenceScheduler()
let service = makeService(scheduler: scheduler, loopMinInterval: 42, loopMaxInterval: 42)
service.start()
XCTAssertEqual(scheduler.intervals, [42])
}
func test_handleLocationChange_invalidatesExistingTimerAndSchedulesQuickRefresh() {
let scheduler = MockGeohashPresenceScheduler()
let service = makeService(scheduler: scheduler, loopMinInterval: 40, loopMaxInterval: 40)
service.start()
let originalTimer = scheduler.timers.first
service.handleLocationChange()
XCTAssertEqual(scheduler.intervals, [40, 5])
XCTAssertEqual(originalTimer?.invalidateCallCount, 1)
}
func test_handleConnectivityChange_onlySchedulesWhenExistingTimerIsMissingOrInvalid() {
let scheduler = MockGeohashPresenceScheduler()
let service = makeService(scheduler: scheduler, loopMinInterval: 33, loopMaxInterval: 33)
service.start()
service.handleConnectivityChange()
XCTAssertEqual(scheduler.intervals, [33])
scheduler.timers.last?.invalidate()
service.handleConnectivityChange()
XCTAssertEqual(scheduler.intervals, [33, 33])
}
func test_performHeartbeat_broadcastsOnlyAllowedPrecisionChannels() async throws {
let identity = try NostrIdentity.generate()
let scheduler = MockGeohashPresenceScheduler()
var sentGeohashes: [String] = []
var lookedUpGeohashes: [String] = []
var sleptNanoseconds: [UInt64] = []
let channels = [
GeohashChannel(level: .region, geohash: "9q"),
GeohashChannel(level: .province, geohash: "9q8y"),
GeohashChannel(level: .city, geohash: "9q8yy"),
GeohashChannel(level: .neighborhood, geohash: "9q8yyk"),
GeohashChannel(level: .block, geohash: "9q8yyk8"),
GeohashChannel(level: .building, geohash: "9q8yyk8y")
]
let service = makeService(
scheduler: scheduler,
availableChannels: channels,
deriveIdentity: { _ in identity },
relayLookup: { geohash, _ in
lookedUpGeohashes.append(geohash)
return ["wss://\(geohash).example"]
},
relaySender: { event, _ in
let geohash = event.tags.first(where: { $0.first == "g" })?[1]
if let geohash {
sentGeohashes.append(geohash)
}
},
sleeper: { nanoseconds in
sleptNanoseconds.append(nanoseconds)
},
loopMinInterval: 17,
loopMaxInterval: 17,
burstMinDelay: 0,
burstMaxDelay: 0
)
service.start()
service.performHeartbeat()
let sentAllAllowedChannels = await waitUntil { sentGeohashes.count == 3 }
XCTAssertTrue(sentAllAllowedChannels)
XCTAssertEqual(Set(sentGeohashes), Set(["9q", "9q8y", "9q8yy"]))
XCTAssertEqual(Set(lookedUpGeohashes), Set(["9q", "9q8y", "9q8yy"]))
XCTAssertEqual(sleptNanoseconds.count, 3)
XCTAssertEqual(scheduler.intervals, [17, 17])
}
func test_performHeartbeat_skipsBroadcastWhenTorIsNotReady() async {
let scheduler = MockGeohashPresenceScheduler()
var sendCount = 0
let service = makeService(
scheduler: scheduler,
torIsReady: { false },
relaySender: { _, _ in sendCount += 1 },
loopMinInterval: 21,
loopMaxInterval: 21
)
service.start()
service.performHeartbeat()
try? await Task.sleep(nanoseconds: 20_000_000)
XCTAssertEqual(sendCount, 0)
XCTAssertEqual(scheduler.intervals, [21, 21])
}
func test_performHeartbeat_skipsBroadcastWhenAppIsBackgrounded() async {
let scheduler = MockGeohashPresenceScheduler()
var sendCount = 0
let service = makeService(
scheduler: scheduler,
torIsForeground: { false },
relaySender: { _, _ in sendCount += 1 },
loopMinInterval: 22,
loopMaxInterval: 22
)
service.start()
service.performHeartbeat()
try? await Task.sleep(nanoseconds: 20_000_000)
XCTAssertEqual(sendCount, 0)
XCTAssertEqual(scheduler.intervals, [22, 22])
}
func test_stopForPanic_cancelsTimerAndSuppressesDelayedBroadcast() async throws {
let identity = try NostrIdentity.generate()
let scheduler = MockGeohashPresenceScheduler()
var sleeperContinuation: CheckedContinuation<Void, Never>?
var sendCount = 0
let service = makeService(
scheduler: scheduler,
deriveIdentity: { _ in identity },
relaySender: { _, _ in sendCount += 1 },
sleeper: { _ in
await withCheckedContinuation { continuation in
sleeperContinuation = continuation
}
},
burstMinDelay: 1,
burstMaxDelay: 1
)
service.start()
service.performHeartbeat()
let delayStarted = await waitUntil {
sleeperContinuation != nil
}
XCTAssertTrue(delayStarted)
service.stopForPanic()
sleeperContinuation?.resume()
try? await Task.sleep(nanoseconds: 20_000_000)
XCTAssertEqual(sendCount, 0)
XCTAssertEqual(scheduler.timers.first?.invalidateCallCount, 1)
}
func test_broadcastPresence_skipsSendWhenNoRelaysAreAvailable() async throws {
let identity = try NostrIdentity.generate()
var sendCount = 0
let service = makeService(
scheduler: MockGeohashPresenceScheduler(),
deriveIdentity: { _ in identity },
relayLookup: { _, _ in [] },
relaySender: { _, _ in sendCount += 1 }
)
service.broadcastPresence(for: "9q8yy")
try? await Task.sleep(nanoseconds: 20_000_000)
XCTAssertEqual(sendCount, 0)
}
func test_broadcastPresence_skipsSendWhenIdentityDerivationFails() async {
enum PresenceError: Error { case failed }
var sendCount = 0
let service = makeService(
scheduler: MockGeohashPresenceScheduler(),
deriveIdentity: { _ in throw PresenceError.failed },
relaySender: { _, _ in sendCount += 1 }
)
service.broadcastPresence(for: "9q8yy")
try? await Task.sleep(nanoseconds: 20_000_000)
XCTAssertEqual(sendCount, 0)
}
private func makeService(
scheduler: MockGeohashPresenceScheduler,
availableChannels: [GeohashChannel] = [
GeohashChannel(level: .city, geohash: "9q8yy")
],
torIsReady: @escaping () -> Bool = { true },
torIsForeground: @escaping () -> Bool = { true },
deriveIdentity: @escaping (String) throws -> NostrIdentity = { _ in try NostrIdentity.generate() },
relayLookup: @escaping (String, Int) -> [String] = { geohash, _ in ["wss://\(geohash).example"] },
relaySender: @escaping (NostrEvent, [String]) -> Void = { _, _ in },
sleeper: @escaping (UInt64) async -> Void = { _ in },
loopMinInterval: TimeInterval = 40,
loopMaxInterval: TimeInterval = 40,
burstMinDelay: TimeInterval = 0,
burstMaxDelay: TimeInterval = 0
) -> GeohashPresenceService {
let locationSubject = PassthroughSubject<[GeohashChannel], Never>()
let torReadySubject = PassthroughSubject<Void, Never>()
return GeohashPresenceService(
availableChannelsProvider: { availableChannels },
locationChanges: locationSubject.eraseToAnyPublisher(),
torReadyPublisher: torReadySubject.eraseToAnyPublisher(),
torIsReady: torIsReady,
torIsForeground: torIsForeground,
deriveIdentity: deriveIdentity,
relayLookup: relayLookup,
relaySender: relaySender,
sleeper: sleeper,
scheduleTimer: scheduler.schedule(interval:handler:),
loopMinInterval: loopMinInterval,
loopMaxInterval: loopMaxInterval,
burstMinDelay: burstMinDelay,
burstMaxDelay: burstMaxDelay
)
}
private func waitUntil(
timeout: TimeInterval = TestConstants.settleTimeout,
condition: @escaping @MainActor () -> Bool
) async -> Bool {
let deadline = Date().addingTimeInterval(timeout)
while Date() < deadline {
if condition() {
return true
}
try? await Task.sleep(nanoseconds: 10_000_000)
}
return condition()
}
}
private final class MockGeohashPresenceScheduler {
private(set) var intervals: [TimeInterval] = []
private(set) var timers: [MockGeohashPresenceTimer] = []
func schedule(interval: TimeInterval, handler: @escaping () -> Void) -> GeohashPresenceTimerProtocol {
intervals.append(interval)
let timer = MockGeohashPresenceTimer(handler: handler)
timers.append(timer)
return timer
}
}
private final class MockGeohashPresenceTimer: GeohashPresenceTimerProtocol {
private let handler: () -> Void
private(set) var isValid = true
private(set) var invalidateCallCount = 0
init(handler: @escaping () -> Void) {
self.handler = handler
}
func invalidate() {
invalidateCallCount += 1
isValid = false
}
}