Make lifecycle delayed read-pass deterministic in tests

The coordinator scheduled its delayed owner-level read pass via
DispatchQueue.main.asyncAfter, which a busy CI runner's main queue can
delay past any reasonable polling deadline. Scheduling is now an
injected context member (scheduleOnMainAfter); the ChatViewModel witness
keeps the exact asyncAfter behavior while the test mock runs the work
synchronously, eliminating the wall-clock poll entirely.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
jack
2026-06-11 09:50:47 +02:00
co-authored by Claude Fable 5
parent 09087b74cc
commit 6c0dbbbd0d
2 changed files with 23 additions and 8 deletions
@@ -54,6 +54,13 @@ private final class MockChatLifecycleContext: ChatLifecycleContext {
managerReadMarks.append(peerID)
}
// Scheduled work runs synchronously so tests never poll wall-clock queues.
private(set) var scheduledDelays: [TimeInterval] = []
func scheduleOnMainAfter(_ delay: TimeInterval, _ work: @escaping @MainActor () -> Void) {
scheduledDelays.append(delay)
work()
}
func synchronizePrivateConversationStore() { privateStoreSyncCount += 1 }
func addSystemMessage(_ content: String) { systemMessages.append(content) }
@@ -299,10 +306,9 @@ struct ChatLifecycleCoordinatorContextTests {
#expect(context.refreshBluetoothStateCount == 2)
#expect(context.managerReadMarks == [peerID])
let deadline = Date().addingTimeInterval(2)
while context.ownerLevelReadPasses.isEmpty && Date() < deadline {
try? await Task.sleep(nanoseconds: 20_000_000)
}
// The mock executes scheduled work synchronously, so the delayed
// owner-level pass has already run - no wall-clock polling.
#expect(context.scheduledDelays == [TransportConfig.uiAnimationMediumSeconds])
#expect(context.ownerLevelReadPasses == [peerID])
}
@Test @MainActor