Files
bitchat/bitchatTests/VoiceCaptureSessionTests.swift
T
0152196ac2 Deflake CI, and make the flake class unrepeatable (#1491)
* Deflake two iOS-sim CI tests with unbounded timing assumptions

Both failed on main-adjacent CI runs during this session's PRs. Neither
was a product bug; both asserted things about real time that a loaded
runner is under no obligation to honour.

**NetworkReachabilityGateTests: a wall-clock upper bound.**
test_monitor_duplicateUpdatesDoNotPostponeOfflineCommit slept 500ms for
real, then asserted total elapsed time was under 1.4s to prove a duplicate
mid-window had not restarted the 1.0s debounce. One CI run took 3.75s. No
wall-clock bound can separate "deadline preserved" from "runner is slow",
because Task.sleep and the asyncAfter flush are both real time and neither
is bounded above.

The deadline property was already covered deterministically one level
down: test_debounce_duplicateObservationsPreservePendingDeadline drives
ReachabilityDebounce with injected timestamps and checks pendingRemaining
directly. So the monitor test now asserts only what needs a real monitor —
that a duplicate still yields exactly one committed false through the
debounce — with an injected clock for the arithmetic and a generous
liveness budget. Renamed to say what it actually checks. No coverage lost,
and it runs in 0.14s instead of ~3.8s because the real sleep is gone.

**NoiseEncryptionServiceTests: injected timeouts that also arm during
setup.** #1483 diagnosed and fixed exactly this in the quarantine-restore
test, but two sibling tests kept the shape. Their injected
ordinaryResponderHandshakeTimeout (0.04 and 0.06) also arms during the
establishSessions setup handshake, where bob is the responder — so a
preempted runner fires it mid-setup, tears down the half-open responder,
and message 3 gets answered as a fresh initiation. The CI failure named
setup's own `#expect(finalMessage == nil)` seeing a 96-byte message 2,
which is precisely the signature #1483 recorded.

Raised both to 1.0s, matching #1483's remedy: the scenarios still need the
responder timeout to fire, and it still does, just with room for setup to
complete first. Thin 1-second waitUntil budgets in the file now use the
shared TestConstants.longTimeout; every one of them backs a positive
assertion, so waitUntil still returns the moment the condition holds and
nothing gets slower in the passing case.

No assertion weakened in either file. Verified 3x sequentially and 3x with
all 18 cores saturated.

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

* Raise two more starvation-prone test deadlines

Both surfaced on the CI runs for this PR and #1487, both in tests neither
PR touches, both the same shape as the two already fixed here: a deadline
sized for the work rather than for a runner executing many suites at once.

VoiceNotePlaybackControllerTests waited 5s for a @MainActor Task that
playback schedules for the session acquire and its failure path. When that
Task is not scheduled in time the helper reports *two* failures — the wait,
and the `!isPlaying` the un-run failure path has not reset yet — which
reads like a playback bug rather than a starved scheduler. That is exactly
what CI showed.

GeoRelayDirectoryTests waited 10s for work the directory runs in
Task.detached(priority: .utility); utility priority competes with every
other suite. The retry-scheduling case timed out at exactly 10.06s with the
retry never scheduled, reading like a missing retry rather than a starved
background task.

Raised to 30s each with the reasoning recorded at the helper. Both helpers
return as soon as their condition holds, so nothing slows down when tests
pass — only the genuine-failure case takes longer to report.

Verified 3x with all cores saturated: both suites clean.

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

* 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>

* Close the guard's blind spot: named short timeouts

A fifth flake landed on CI while the first guard was in review —
GossipSyncBoardTests timing out at 1.03s — and the guard did not catch it,
because the deadline was `TestConstants.shortTimeout` rather than a
literal. A literals-only scan cannot see a short value behind a symbol,
which is the more common way it is written: 81 wait sites used
shortTimeout (1s) or defaultTimeout (5s), every one of them below the
floor.

Rather than guess which of those were safe to raise, all 81 were converted
and the suite timed. Runtime went 15s -> 72s, which located the genuine
negative waits precisely: ten tests that assert something does *not*
happen and therefore always run their deadline out. Measurement instead of
a heuristic, since a mis-guess in either direction is invisible — too
short reintroduces the flake, too long silently costs a minute a run.

Those 28 sites now use `TestConstants.negativeWaitWindow`, a named
constant whose doc explains the inverted reasoning: for a negative wait,
starvation can only make the assertion *more* likely to hold, so short is
correct, and the name states the polarity instead of leaving a bare
literal that reads like the mistake. Suite is back to 15.3s.

The guard now also rejects `timeout: TestConstants.shortTimeout` and
`defaultTimeout`, while accepting `negativeWaitWindow` by name.

Re-verified with a canary carrying every banned shape — literal default,
both named constants, and an elapsed-time upper bound. All four were
flagged with file and line; the suite went green again on removal.

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

* Delete shortTimeout now that nothing may use it

The hygiene guard bans `TestConstants.shortTimeout` at every wait site
and the last users were converted, so Periphery correctly flagged the
constant itself as dead and failed CI. Remove it from both TestConstants
copies; the banned-name entry stays so the symbol cannot quietly return.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-07-26 23:45:20 +01:00

266 lines
8.5 KiB
Swift

//
// VoiceCaptureSessionTests.swift
// bitchatTests
//
// This is free and unencumbered software released into the public domain.
// For more information, see <https://unlicense.org>
//
import Foundation
import Testing
@testable import bitchat
@MainActor
private final class StubPTTCapture: PTTCapturing {
var onFrames: (([Data]) -> Void)?
var stopResult: (url: URL?, encodedFrames: Int)
var startError: Error?
private(set) var startCount = 0
private(set) var cancelCount = 0
init(
stopResult: (url: URL?, encodedFrames: Int),
startError: Error? = nil
) {
self.stopResult = stopResult
self.startError = startError
}
func start(outputURL: URL) async throws {
startCount += 1
if let startError {
throw startError
}
}
func stop() -> (url: URL?, encodedFrames: Int) {
stopResult
}
func cancel() {
cancelCount += 1
}
}
private final class CaptureLeaseSession: SessionApplying, @unchecked Sendable {
private let lock = NSLock()
private var _activationCalls: [Bool] = []
var activationCalls: [Bool] { lock.withLock { _activationCalls } }
func setCategory(_ category: AudioSessionCoordinator.Category) throws {}
func setActive(_ active: Bool, notifyOthersOnDeactivation: Bool) throws {
lock.withLock { _activationCalls.append(active) }
}
}
@MainActor
private final class GatedVoiceCaptureSession: VoiceCaptureSession {
let isLive = false
private let startError: Error?
private(set) var finishStarted = false
private(set) var cancelCount = 0
private(set) var panicCancelCount = 0
private var finishContinuation: CheckedContinuation<URL?, Never>?
init(startError: Error? = nil) {
self.startError = startError
}
func requestPermission() async -> Bool { true }
func start() async throws {
if let startError { throw startError }
}
func finish() async -> URL? {
finishStarted = true
return await withCheckedContinuation { continuation in
finishContinuation = continuation
}
}
func cancel() async {
cancelCount += 1
}
func panicCancelSynchronously() {
panicCancelCount += 1
}
func resolveFinish(with url: URL?) {
let continuation = finishContinuation
finishContinuation = nil
continuation?.resume(returning: url)
}
}
@MainActor
struct VoiceCaptureSessionTests {
private func waitUntil(
_ condition: () -> Bool,
sourceLocation: SourceLocation = #_sourceLocation
) async {
let deadline = ContinuousClock.now.advanced(by: .seconds(TestConstants.settleTimeout))
while !condition(), ContinuousClock.now < deadline {
await Task.yield()
try? await Task.sleep(nanoseconds: 1_000_000)
}
#expect(condition(), sourceLocation: sourceLocation)
}
private func isRecording(_ state: VoiceRecordingViewModel.State) -> Bool {
if case .recording = state { return true }
return false
}
@Test func staleCaptureCallbackCannotInvalidateNewGeneration() {
let generations = PTTCaptureGeneration()
let old = generations.begin()
generations.invalidate()
let current = generations.begin()
#expect(!generations.invalidate(ifCurrent: old))
#expect(generations.isCurrent(current))
#expect(generations.invalidate(ifCurrent: current))
#expect(!generations.isCurrent(current))
}
@Test func coordinatorCancellationIsNotReportedAsAStartedCapture() async {
let capture = StubPTTCapture(
stopResult: (nil, 0),
startError: CancellationError()
)
let session = PTTLiveVoiceSession(
sendPacket: { _ in },
capture: capture
)
await #expect(throws: CancellationError.self) {
try await session.start()
}
}
@Test func interruptedShortCaptureIsCanceledEvenAfterLongHold() async throws {
let url = FileManager.default.temporaryDirectory
.appendingPathComponent("ptt-interrupted-test-\(UUID().uuidString).m4a")
_ = FileManager.default.createFile(atPath: url.path, contents: Data([0x00]))
defer { try? FileManager.default.removeItem(at: url) }
let capture = StubPTTCapture(stopResult: (url, 1))
var sentPackets: [Data] = []
var now = Date()
let session = PTTLiveVoiceSession(
sendPacket: { sentPackets.append($0) },
capture: capture,
now: { now },
burstID: Data(repeating: 0xA5, count: 8)
)
try await session.start()
now = now.addingTimeInterval(2)
let result = await session.finish()
#expect(result == nil)
#expect(!FileManager.default.fileExists(atPath: url.path))
let packet = try #require(sentPackets.last.flatMap(VoiceBurstPacket.decode))
guard case .canceled = packet.kind else {
Issue.record("Expected a canceled control packet for a subsecond interrupted capture")
return
}
}
@Test func droppingCaptureLeaseReturnsItsCoordinatorToken() async throws {
let rawSession = CaptureLeaseSession()
let coordinator = AudioSessionCoordinator(session: rawSession)
let token = try await coordinator.acquire(.capture) {}
var lease: PTTCaptureSessionLease? = PTTCaptureSessionLease(coordinator: coordinator)
lease?.install(token)
lease = nil
await coordinator.drain()
#expect(rawSession.activationCalls == [true, false])
}
@Test func rejectedNewHoldAndStaleFinalizeLeaveNewerGenerationIdle() async {
let oldSession = GatedVoiceCaptureSession()
let newSession = GatedVoiceCaptureSession(
startError: VoiceRecorder.RecorderError.recordingInProgress
)
var sessions: [GatedVoiceCaptureSession] = [oldSession, newSession]
let viewModel = VoiceRecordingViewModel()
viewModel.sessionProvider = { sessions.removeFirst() }
viewModel.start(shouldShow: true)
await waitUntil { self.isRecording(viewModel.state) }
viewModel.finish(completion: { _ in })
await waitUntil { oldSession.finishStarted }
viewModel.start(shouldShow: true)
await waitUntil { newSession.cancelCount == 1 && viewModel.state == .idle }
#expect(viewModel.state == .idle)
// The older finalize now fails after the newer press has completed.
// It must not replace the newer generation's idle state with an alert.
oldSession.resolveFinish(with: nil)
for _ in 0..<20 {
await Task.yield()
}
#expect(viewModel.state == .idle)
}
@Test func panicSynchronouslyCancelsActiveCaptureAndResetsUI() async {
let session = GatedVoiceCaptureSession()
let viewModel = VoiceRecordingViewModel()
viewModel.sessionProvider = { session }
viewModel.start(shouldShow: true)
await waitUntil { self.isRecording(viewModel.state) }
viewModel.panicWipe()
#expect(session.panicCancelCount == 1)
#expect(viewModel.state == .idle)
#expect(!viewModel.isLiveStreaming)
}
@Test func panicInvalidatesARecordingAlreadyFinalizing() async throws {
let session = GatedVoiceCaptureSession()
let viewModel = VoiceRecordingViewModel()
viewModel.sessionProvider = { session }
let url = FileManager.default.temporaryDirectory
.appendingPathComponent("voice-panic-\(UUID().uuidString).m4a")
try Data([0x01]).write(to: url)
var delivered = false
viewModel.start(shouldShow: true)
await waitUntil { self.isRecording(viewModel.state) }
viewModel.finish { _ in delivered = true }
await waitUntil { session.finishStarted }
viewModel.panicWipe()
session.resolveFinish(with: url)
await waitUntil {
!FileManager.default.fileExists(atPath: url.path)
}
#expect(!delivered)
#expect(viewModel.state == .idle)
}
@Test func liveSessionPanicStopsCaptureWithoutSendingControl() {
let capture = StubPTTCapture(stopResult: (nil, 0))
var sentPackets: [Data] = []
let session = PTTLiveVoiceSession(
sendPacket: { sentPackets.append($0) },
capture: capture
)
session.panicCancelSynchronously()
#expect(capture.cancelCount == 1)
#expect(sentPackets.isEmpty)
}
}