Move the recording side effects into the RecordingViewModel view model in the Recording package target.

This commit is contained in:
2026-07-04 14:50:41 +02:00
parent 5e102f21e1
commit 702333038c
3 changed files with 97 additions and 124 deletions
@@ -41,7 +41,7 @@ struct RecordingViewModelTests {
) {
let model = Model()
model.state = initial
model.drive(to: initial)
model.pressedMain()
@@ -58,7 +58,7 @@ struct RecordingViewModelTests {
) {
let model = Model()
model.state = initial
model.drive(to: initial)
model.pressedSend()
@@ -83,7 +83,7 @@ struct RecordingViewModelTests {
) {
let model = Model()
model.state = state
model.drive(to: state)
#expect(model.iconMain == expected)
}
@@ -98,7 +98,7 @@ struct RecordingViewModelTests {
) {
let model = Model()
model.state = state
model.drive(to: state)
#expect(model.iconSend == expected)
}
@@ -113,7 +113,7 @@ struct RecordingViewModelTests {
) {
let model = Model()
model.state = state
model.drive(to: state)
#expect(model.isProcessing == expected)
}
@@ -128,7 +128,7 @@ struct RecordingViewModelTests {
) {
let model = Model()
model.state = state
model.drive(to: state)
#expect(model.shouldDisableMain == expected)
}
@@ -143,7 +143,7 @@ struct RecordingViewModelTests {
) {
let model = Model()
model.state = state
model.drive(to: state)
#expect(model.shouldShowSend == expected)
}
@@ -158,7 +158,7 @@ struct RecordingViewModelTests {
) {
let model = Model()
model.state = state
model.drive(to: state)
#expect(model.shouldShowTimer == expected)
}
@@ -174,28 +174,24 @@ struct RecordingViewModelTests {
@Test func `ticks while recording`() async throws {
let model = Model()
model.state = .recording
model.updatedState(shouldRestartTimer: true)
model.pressedMain()
try await Task.sleep(for: .seconds(1.2))
#expect(model.elapsedSeconds >= 1)
#expect(model.textTimer == "00:01")
model.state = .notRecording
model.updatedState(shouldRestartTimer: false)
model.pressedMain()
}
@Test func `stops while paused`() async throws {
let model = Model()
model.state = .recording
model.updatedState(shouldRestartTimer: true)
model.pressedMain()
try await Task.sleep(for: .seconds(1.2))
model.state = .paused
model.updatedState(shouldRestartTimer: false)
model.pressedMain()
let secondsWhenPaused = model.elapsedSeconds
@@ -208,43 +204,42 @@ struct RecordingViewModelTests {
@Test func `keeps elapsed seconds when resumed`() async throws {
let model = Model()
model.state = .recording
model.updatedState(shouldRestartTimer: true)
model.pressedMain()
try await Task.sleep(for: .seconds(1.2))
model.state = .paused
model.updatedState(shouldRestartTimer: false)
model.pressedMain()
let secondsWhenPaused = model.elapsedSeconds
model.state = .recording
model.updatedState(shouldRestartTimer: false)
model.pressedMain()
#expect(model.elapsedSeconds == secondsWhenPaused)
model.state = .notRecording
model.updatedState(shouldRestartTimer: false)
model.pressedMain()
}
@Test func `restarts for a new recording`() async throws {
let model = Model()
let model = Model(
transcribe: TranscribingMock()
)
model.state = .recording
model.updatedState(shouldRestartTimer: true)
model.pressedMain()
try await Task.sleep(for: .seconds(1.2))
model.state = .paused
model.updatedState(shouldRestartTimer: false)
model.pressedMain()
model.pressedSend()
model.state = .recording
model.updatedState(shouldRestartTimer: true)
try await Task.sleep(for: .seconds(0.5))
#expect(model.state == .notRecording)
model.pressedMain()
#expect(model.elapsedSeconds == 0)
model.state = .notRecording
model.updatedState(shouldRestartTimer: false)
model.pressedMain()
}
}
@@ -259,27 +254,22 @@ struct RecordingViewModelTests {
let capturer = CapturingMock()
let model = Model(capturer: capturer)
model.state = .recording
model.updatedState(shouldRestartTimer: true)
model.pressedMain()
try await Task.sleep(for: .seconds(0.1))
#expect(capturer.countStart == 1)
#expect(capturer.countResume == 0)
model.state = .notRecording
model.updatedState(shouldRestartTimer: false)
model.pressedMain()
}
@Test func `pauses the capture while paused`() async throws {
let capturer = CapturingMock()
let model = Model(capturer: capturer)
model.state = .recording
model.updatedState(shouldRestartTimer: true)
model.state = .paused
model.updatedState(shouldRestartTimer: false)
model.pressedMain()
model.pressedMain()
try await Task.sleep(for: .seconds(0.1))
@@ -290,22 +280,16 @@ struct RecordingViewModelTests {
let capturer = CapturingMock()
let model = Model(capturer: capturer)
model.state = .recording
model.updatedState(shouldRestartTimer: true)
model.state = .paused
model.updatedState(shouldRestartTimer: false)
model.state = .recording
model.updatedState(shouldRestartTimer: false)
model.pressedMain()
model.pressedMain()
model.pressedMain()
try await Task.sleep(for: .seconds(0.1))
#expect(capturer.countStart == 1)
#expect(capturer.countResume == 1)
model.state = .notRecording
model.updatedState(shouldRestartTimer: false)
model.pressedMain()
}
@Test func `stops the capture when the input is sent`() async throws {
@@ -315,8 +299,7 @@ struct RecordingViewModelTests {
transcribe: TranscribingMock()
)
model.state = .processing
model.updatedState(shouldRestartTimer: false)
model.drive(to: .processing)
try await Task.sleep(for: .seconds(0.5))
@@ -330,8 +313,7 @@ struct RecordingViewModelTests {
let model = Model(capturer: capturer)
model.state = .recording
model.updatedState(shouldRestartTimer: true)
model.pressedMain()
try await Task.sleep(for: .seconds(0.1))
@@ -351,18 +333,15 @@ struct RecordingViewModelTests {
transcribe: TranscribingMock()
)
model.state = .recording
model.updatedState(shouldRestartTimer: true)
model.pressedMain()
try await Task.sleep(for: .seconds(1.2))
model.state = .paused
model.updatedState(shouldRestartTimer: false)
model.pressedMain()
#expect(model.elapsedSeconds >= 1)
model.state = .processing
model.updatedState(shouldRestartTimer: false)
model.pressedSend()
try await Task.sleep(for: .seconds(0.5))
@@ -380,8 +359,7 @@ struct RecordingViewModelTests {
transcribe: transcriber
)
model.state = .processing
model.updatedState(shouldRestartTimer: false)
model.drive(to: .processing)
try await Task.sleep(for: .seconds(0.5))
@@ -395,26 +373,50 @@ struct RecordingViewModelTests {
transcribe: TranscribingMock()
)
model.state = .processing
model.updatedState(shouldRestartTimer: false)
model.drive(to: .processing)
try await Task.sleep(for: .seconds(0.5))
#expect(model.transcription != nil)
model.state = .recording
model.updatedState(shouldRestartTimer: true)
model.pressedMain()
#expect(model.transcription == nil)
model.state = .notRecording
model.updatedState(shouldRestartTimer: false)
model.pressedMain()
}
}
}
// MARK: - Helpers
private extension RecordingView.Model {
/// Drives the model from its initial state to the given state through its press handlers, as the view's buttons would.
///
/// - Parameter state: The state to drive the model to.
func drive(
to state: State
) {
switch state {
case .notRecording:
break
case .recording:
pressedMain()
case .paused:
pressedMain()
pressedMain()
case .processing:
pressedMain()
pressedMain()
pressedSend()
}
}
}
// MARK: - Mocks
/// A recording service that counts its invocations and can be configured to fail.