// The recorder's rules, all of them, with no microphone and no clock. // // `MeetingRecorderMachine` is pure and every event carries its instant, so each // case below fixes a start time and walks a recording through it minute by // minute. **Nothing in this file reads `Date()`** — an elapsed figure tested // against the wall clock is a test that is approximately right, which for an // elapsed figure is the same as wrong. // // The three rules the file's header says were learnt the expensive way each // have a case of their own, and so does the one the audio depends on: a refusal // at Stop keeps the file. import CottonKit import XCTest private let start = Date(timeIntervalSince1970: 1_789_000_000) private func at(_ seconds: Double) -> Date { start.addingTimeInterval(seconds) } /// A recorder already capturing, with the effects that got it there discarded. private func recording() -> MeetingRecorderState { var state = MeetingRecorderState() _ = MeetingRecorderMachine.step(&state, .appeared) _ = MeetingRecorderMachine.step(&state, .gate(.ready)) _ = MeetingRecorderMachine.step(&state, .captureOpened(at: start)) return state } final class MeetingRecorderMachineTests: XCTestCase { // ── The straight path ───────────────────────────────────────────────── func testAskingComesBeforeAnythingPlatformIsBuilt() { var state = MeetingRecorderState() let asked = MeetingRecorderMachine.step(&state, .appeared) XCTAssertEqual(asked, [.askForMicrophone]) XCTAssertEqual(state.stage, .asking) XCTAssertNil( state.startedAt, "nothing is open yet — the gate answers first, which is the app's one order" ) let ready = MeetingRecorderMachine.step(&state, .gate(.ready)) XCTAssertEqual(ready, [.openCapture]) XCTAssertEqual(state.stage, .opening) } func testTheScreenAppearingTwiceDoesNotPromptTwice() { var state = MeetingRecorderState() _ = MeetingRecorderMachine.step(&state, .appeared) let again = MeetingRecorderMachine.step(&state, .appeared) XCTAssertEqual( again, [], "iOS shows nothing the second time, and a caller waiting for an answer would wait for ever" ) } func testCaptureOpeningWritesTheMarker() { var state = MeetingRecorderState() _ = MeetingRecorderMachine.step(&state, .appeared) _ = MeetingRecorderMachine.step(&state, .gate(.ready)) let opened = MeetingRecorderMachine.step(&state, .captureOpened(at: start)) XCTAssertEqual(opened, [.mark(.recording)]) XCTAssertEqual(state.stage, .recording) XCTAssertEqual(state.startedAt, start) } func testTheWholeWalkIdleToSaved() { var state = recording() _ = MeetingRecorderMachine.step(&state, .personPaused(at: at(60))) _ = MeetingRecorderMachine.step(&state, .personResumed(at: at(90))) let stopped = MeetingRecorderMachine.step(&state, .personStopped(at: at(150))) XCTAssertEqual(stopped, [.closeCapture]) XCTAssertEqual(state.stage, .finishing) let closed = MeetingRecorderMachine.step(&state, .captureClosed(at: at(150))) XCTAssertEqual( closed, [.mark(.closed), .prepareAudio], "the marker says closed and the encode starts when CAPTURE ends" ) XCTAssertEqual( state.stage, .finishing, "the capture is being encoded into the container a meeting is filed in" ) let prepared = MeetingRecorderMachine.step(&state, .audioPrepared) XCTAssertEqual(prepared, [.create]) XCTAssertEqual(state.stage, .saving) let route = Route.meetingsMeeting(id: "meeting-1") let created = MeetingRecorderMachine.step(&state, .created(route)) XCTAssertEqual(created, [.clearMark]) XCTAssertEqual(state.stage, .saved(route)) XCTAssertTrue(state.stage.isFinished) } // ── The elapsed figure ──────────────────────────────────────────────── func testAPauseIsNotRecordedAudio() { var state = recording() _ = MeetingRecorderMachine.step(&state, .personPaused(at: at(60))) XCTAssertEqual( state.elapsedMs(at: at(300)), 60_000, accuracy: 1, "the clock stands still while paused — five minutes of wall time is one minute of audio" ) _ = MeetingRecorderMachine.step(&state, .personResumed(at: at(300))) XCTAssertEqual(state.elapsedMs(at: at(330)), 90_000, accuracy: 1) XCTAssertEqual(state.elapsed(at: at(330)), 90_000) } func testAnUnopenedRecorderHasCapturedNothing() { var state = MeetingRecorderState() XCTAssertEqual(state.elapsedMs(at: at(999)), 0) } // ── Rule 1: a person's pause is sticky ──────────────────────────────── func testAnInterruptionEndingDoesNotResumeAPauseThePersonMade() { var state = recording() _ = MeetingRecorderMachine.step(&state, .personPaused(at: at(30))) let arrived = MeetingRecorderMachine.step(&state, .interrupted(at: at(40))) XCTAssertEqual(arrived, [], "it is already paused, and the pause stays THEIRS") XCTAssertEqual(state.stage, .paused(.person)) let ended = MeetingRecorderMachine.step(&state, .interruptionEnded(at: at(50))) XCTAssertEqual( ended, [], "a call ending never resumes a recording the person deliberately paused" ) XCTAssertEqual(state.stage, .paused(.person)) } func testOnlyThePersonsOwnResumeLiftsTheirOwnPause() { var state = recording() _ = MeetingRecorderMachine.step(&state, .personPaused(at: at(30))) let resumed = MeetingRecorderMachine.step(&state, .personResumed(at: at(60))) XCTAssertEqual(resumed, [.resumeCapture, .mark(.recording)]) XCTAssertEqual(state.stage, .recording) } // ── Rule 2: an ended interruption always attempts a resume ──────────── func testAPhoneCallPausesAndGivingTheMicrophoneBackResumes() { var state = recording() let began = MeetingRecorderMachine.step(&state, .interrupted(at: at(20))) XCTAssertEqual(began, [.pauseCapture, .mark(.paused)]) XCTAssertEqual(state.stage, .paused(.interruption)) let ended = MeetingRecorderMachine.step(&state, .interruptionEnded(at: at(80))) XCTAssertEqual( ended, [.resumeCapture, .mark(.recording)], """ iOS routinely omits `shouldResume` after a call, so the attempt is \ unconditional — the event does not even carry the flag. """ ) XCTAssertEqual(state.stage, .recording) XCTAssertEqual(state.elapsedMs(at: at(80)), 20_000, accuracy: 1) } func testAnInterruptionEndingTwiceIsNotTwoResumes() { var state = recording() _ = MeetingRecorderMachine.step(&state, .interrupted(at: at(20))) _ = MeetingRecorderMachine.step(&state, .interruptionEnded(at: at(80))) let again = MeetingRecorderMachine.step(&state, .interruptionEnded(at: at(81))) XCTAssertEqual(again, [], "a late or duplicate notification is ordinary, and a no-op") } // ── A refusal is a state, not an error ──────────────────────────────── func testADeniedMicrophoneIsAStateAndNothingIsOpened() { var state = MeetingRecorderState() _ = MeetingRecorderMachine.step(&state, .appeared) let blocked = MeetingRecorderMachine.step(&state, .gate(.blocked(.microphone, .denied))) XCTAssertEqual(blocked, [], "nothing retries against a decision only the person can make") XCTAssertEqual(state.stage, .blocked(.microphone, .denied)) XCTAssertNil(state.startedAt) } func testARestrictedMicrophoneIsTheSameShapeWithADifferentAnswer() { var state = MeetingRecorderState() _ = MeetingRecorderMachine.step(&state, .appeared) _ = MeetingRecorderMachine.step(&state, .gate(.blocked(.microphone, .restricted))) XCTAssertEqual(state.stage, .blocked(.microphone, .restricted)) } // ── Rule 3: a refusal at Stop keeps the audio ───────────────────────── func testARefusedCreateIsAFailureThatCanBeRetried() { var state = recording() _ = MeetingRecorderMachine.step(&state, .personStopped(at: at(120))) _ = MeetingRecorderMachine.step(&state, .captureClosed(at: at(120))) _ = MeetingRecorderMachine.step(&state, .audioPrepared) let refused = MeetingRecorderMachine.step(&state, .refused("not connected")) XCTAssertEqual( refused, [], "nothing is cleared and nothing is deleted — the marker still names the file" ) XCTAssertEqual(state.stage, .failed("not connected")) let retried = MeetingRecorderMachine.step(&state, .retry) XCTAssertEqual( retried, [.prepareAudio], "back through the encode, which is where WHICH file is filed is decided" ) XCTAssertEqual(state.stage, .finishing) } func testStopFromAPausedRecordingStillCreatesTheMeeting() { var state = recording() _ = MeetingRecorderMachine.step(&state, .personPaused(at: at(45))) let stopped = MeetingRecorderMachine.step(&state, .personStopped(at: at(200))) XCTAssertEqual(stopped, [.closeCapture]) XCTAssertEqual(state.stage, .finishing) XCTAssertEqual( state.elapsed(at: at(200)), 45_000, "and the length is what was captured, not how long the screen was up" ) } /// Two failures wear the same verb. A recorder that never opened captured /// nothing, so filing it would be refused for having no audio and the verb /// would loop — its retry is the microphone again. func testRetryingARecorderThatNeverOpenedStartsTheMicrophoneRatherThanFiling() { var state = MeetingRecorderState() _ = MeetingRecorderMachine.step(&state, .appeared) _ = MeetingRecorderMachine.step(&state, .gate(.ready)) _ = MeetingRecorderMachine.step( &state, .captureFailed(at: start, why: "the microphone couldn't be started")) XCTAssertNil(state.startedAt) let retried = MeetingRecorderMachine.step(&state, .retry) XCTAssertEqual(retried, [.openCapture]) XCTAssertEqual(state.stage, .opening) } func testCaptureFallingOverBanksWhatWasRecorded() { var state = recording() let broke = MeetingRecorderMachine.step( &state, .captureFailed(at: at(75), why: "the microphone couldn't be started")) XCTAssertEqual(broke, []) XCTAssertEqual(state.stage, .failed("the microphone couldn't be started")) XCTAssertEqual( state.elapsedMs(at: at(999)), 75_000, accuracy: 1, "the clock stops when it broke, not when somebody looked" ) } // ── The events a stage has no answer for ────────────────────────────── func testAnEventOutOfOrderChangesNothing() { var state = MeetingRecorderState() XCTAssertEqual(MeetingRecorderMachine.step(&state, .personPaused(at: start)), []) XCTAssertEqual(MeetingRecorderMachine.step(&state, .personStopped(at: start)), []) XCTAssertEqual(MeetingRecorderMachine.step(&state, .captureClosed(at: start)), []) XCTAssertEqual(MeetingRecorderMachine.step(&state, .retry), []) XCTAssertEqual(state.stage, .idle) } func testStopTappedTwiceDoesNotCloseTheFileTwice() { var state = recording() _ = MeetingRecorderMachine.step(&state, .personStopped(at: at(10))) XCTAssertEqual(MeetingRecorderMachine.step(&state, .personStopped(at: at(11))), []) } // ── Finishing: the stage the encode owns ────────────────────────────── /// **The encode is a stage of its own**, because it is a real transcode now /// that the capture is linear PCM — the format a killed process leaves /// readable. A screen saying "filing…" through a minute of it is a screen /// that looks stuck (`MeetingAudioExport.swift`). func testTheEncodeIsItsOwnStageBetweenStopAndTheCall() { var state = recording() _ = MeetingRecorderMachine.step(&state, .personStopped(at: at(30))) XCTAssertEqual(state.stage, .finishing, "Stop closes the file and starts the encode") _ = MeetingRecorderMachine.step(&state, .captureClosed(at: at(30))) XCTAssertEqual(state.stage, .finishing, "and it is still the encode until it answers") XCTAssertEqual( MeetingRecorderMachine.step(&state, .audioPrepared), [.create], "one event either way: an export that failed still leaves a file to file" ) } /// A close that fell over is a failure with the audio intact, from the /// encode's own stage as much as from any other. func testACloseThatFailsIsAFailureAndNotASilentFinish() { var state = recording() _ = MeetingRecorderMachine.step(&state, .personStopped(at: at(30))) let failed = MeetingRecorderMachine.step( &state, .captureFailed(at: at(30), why: "the file could not be closed.")) XCTAssertEqual(failed, []) XCTAssertEqual(state.stage, .failed("the file could not be closed.")) } /// The events the encode's stage has no answer for. func testAPreparedEventOutsideTheEncodeChangesNothing() { var state = recording() XCTAssertEqual(MeetingRecorderMachine.step(&state, .audioPrepared), []) XCTAssertEqual(state.stage, .recording) } func testOnlyRecordingCountsAsCapturing() { XCTAssertTrue(MeetingRecorderStage.recording.isCapturing) XCTAssertFalse(MeetingRecorderStage.paused(.person).isCapturing) XCTAssertFalse(MeetingRecorderStage.paused(.interruption).isCapturing) XCTAssertFalse(MeetingRecorderStage.finishing.isCapturing) XCTAssertFalse(MeetingRecorderStage.saving.isCapturing) XCTAssertFalse(MeetingRecorderStage.saved(.meetings(importing: false)).isCapturing) } }