Improved the Capturing protocol in the Recording package target to capture interruptions and pause any active capture process.

This commit is contained in:
2026-07-04 15:19:26 +02:00
parent 6a0e7698a5
commit 1f1ac630f3
5 changed files with 211 additions and 17 deletions
@@ -6,6 +6,9 @@ import Recording
@MainActor
final class CapturingMock: Capturing {
/// The stream of events the service emits outside its method calls.
let events: AsyncStream<CapturingEvent>
/// The duration every method takes before returning, simulating slow capture work.
var delay: Duration = .zero
@@ -15,6 +18,18 @@ final class CapturingMock: Capturing {
/// The names of the methods called on the service, in call order.
private(set) var calls: [String] = []
/// The continuation that feeds ``events``.
private let continuation: AsyncStream<CapturingEvent>.Continuation
init() {
(events, continuation) = AsyncStream.makeStream(of: CapturingEvent.self)
}
/// Emits an interruption of the ongoing capture through ``events``.
func interrupt() {
continuation.yield(.interrupted)
}
func start() async throws {
try await called("start")
}
@@ -342,6 +342,47 @@ struct RecordingViewModelTests {
}
// MARK: Interruptions
@MainActor
@Suite("Interruptions")
struct Interruptions {
@Test func `pauses an ongoing recording when the capture is interrupted`() async throws {
let capturer = CapturingMock()
let model = Model(capturer: capturer)
model.pressedMain()
try await Task.sleep(for: .seconds(0.1))
capturer.interrupt()
try await Task.sleep(for: .seconds(0.1))
#expect(model.state == .paused)
#expect(capturer.calls == ["start", "pause"])
}
@Test func `changes nothing when the capture is interrupted while paused`() async throws {
let capturer = CapturingMock()
let model = Model(capturer: capturer)
model.pressedMain()
model.pressedMain()
try await Task.sleep(for: .seconds(0.1))
capturer.interrupt()
try await Task.sleep(for: .seconds(0.1))
#expect(model.state == .paused)
#expect(capturer.calls == ["start", "pause"])
}
}
// MARK: Processing
@MainActor