Updated the Capturing procotol in the Recording package target to pass captured audio as a file URL instead of data.

This commit is contained in:
2026-07-04 15:06:55 +02:00
parent 2fe240de1a
commit 6a0e7698a5
10 changed files with 116 additions and 119 deletions
@@ -0,0 +1,48 @@
import Foundation
import Recording
/// A recording service that records its invocations in call order, each one taking a configurable amount of time, and can be
/// configured to fail.
@MainActor
final class CapturingMock: Capturing {
/// The duration every method takes before returning, simulating slow capture work.
var delay: Duration = .zero
/// The error the service throws from every method, or `nil` when it should succeed.
var error: Error?
/// The names of the methods called on the service, in call order.
private(set) var calls: [String] = []
func start() async throws {
try await called("start")
}
func pause() async throws {
try await called("pause")
}
func resume() async throws {
try await called("resume")
}
func stop() async throws -> URL {
try await called("stop")
return FileManager.default.temporaryDirectory.appending(path: "mock.m4a")
}
private func called(
_ name: String
) async throws {
calls.append(name)
try await Task.sleep(for: delay)
if let error {
throw error
}
}
}
@@ -0,0 +1,2 @@
/// An error to configure the service mocks with.
struct ErrorMock: Error {}
@@ -0,0 +1,30 @@
import Foundation
import Recording
/// A transcribing service with a configurable delay, output, and failure.
@MainActor
final class TranscribingMock: Transcribing {
/// The duration of the simulated transcription work.
var delay: Duration = .seconds(0.2)
/// The error the service throws, or `nil` when it should succeed.
var error: Error?
/// The transcription the service returns.
var transcription = "This is a mocked transcription."
func callAsFunction(
_ audio: URL,
locale: Locale
) async throws -> Transcription {
try await Task.sleep(for: delay)
if let error {
throw error
}
return .init(text: transcription)
}
}
@@ -436,79 +436,3 @@ private extension RecordingView.Model {
}
}
// MARK: - Mocks
/// A recording service that records its invocations in call order, each one taking a configurable amount of time, and can be
/// configured to fail.
@MainActor
private final class CapturingMock: Capturing {
/// The duration every method takes before returning, simulating slow capture work.
var delay: Duration = .zero
/// The error the service throws from every method, or `nil` when it should succeed.
var error: Error?
/// The names of the methods called on the service, in call order.
private(set) var calls: [String] = []
func start() async throws {
try await called("start")
}
func pause() async throws {
try await called("pause")
}
func resume() async throws {
try await called("resume")
}
func stop() async throws -> Data {
try await called("stop")
return .init()
}
private func called(
_ name: String
) async throws {
calls.append(name)
try await Task.sleep(for: delay)
if let error {
throw error
}
}
}
/// A transcribing service with a configurable delay, output, and failure.
@MainActor
private final class TranscribingMock: Transcribing {
/// The duration of the simulated transcription work.
var delay: Duration = .seconds(0.2)
/// The error the service throws, or `nil` when it should succeed.
var error: Error?
/// The transcription the service returns.
var transcription = "This is a mocked transcription."
func callAsFunction(_ audio: Data, locale: Locale) async throws -> Transcription {
try await Task.sleep(for: delay)
if let error {
throw error
}
return .init(text: transcription)
}
}
/// An error to configure the service mocks with.
private struct ErrorMock: Error {}