SPB Git

Repository Public

VoiceTests.swift 40.8 KB 733 lines swift

1// SP Notes iOS2// File: Tests/VoiceTests.swift3// Description: Voice notes: the recorder state machine with a fake recorder (pause, resume, interruption, permission), the level4//              meter, the grouping of recognised words into phrases, seeking from the transcript, the server transcription flow5//              against the fake backend with the recorded answers, the AI summary inserted only on request, the lossless `audio`6//              node, keeping or deleting the original audio, dictation, and one run of the real recorder.7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10import AVFoundation11import Foundation12import Testing13import UIKit14import SPNotesCore15@testable import SPNotes1617// MARK: - Fakes1819@MainActor20final class FakeRecorder: AudioRecording {21    var onEvent: ((RecorderEvent) -> Void)?22    var currentTime: Double = 023    var power: Float = -16024    var failsToStart = false25    var refusesResume = false26    private(set) var url: URL?27    private(set) var log: [String] = []2829    func averagePower() -> Float { power }30    func start(to url: URL) throws {31        if failsToStart { throw RecorderError.cannotStart("test") }32        self.url = url33        try Data("audio".utf8).write(to: url)34        log.append("start")35    }36    func pause() { log.append("pause") }37    func resume() -> Bool { log.append("resume"); return !refusesResume }38    func stop() -> Double { log.append("stop"); return currentTime }39    func cancel() { log.append("cancel"); if let url { try? FileManager.default.removeItem(at: url) } }40}4142@MainActor43final class FakeMicrophone: MicrophoneAuthorizing {44    var status: MicrophonePermission45    var answer: MicrophonePermission46    private(set) var requests = 047    init(_ status: MicrophonePermission, answer: MicrophonePermission = .granted) { self.status = status; self.answer = answer }48    func request() async -> MicrophonePermission { requests += 1; status = answer; return status }49}5051@MainActor52final class FakeDictation: SpeechDictating {53    var onText: ((String) -> Void)?54    var onEnd: ((DictationStop) -> Void)?55    var failure: DictationFailure?56    var writtenSeconds = 0.057    private(set) var audioURL: URL?58    private(set) var stops = 059    func start(keepingAudioAt audioURL: URL?) async throws {60        if let failure { throw failure }61        self.audioURL = audioURL62        if let audioURL { try Data("pcm".utf8).write(to: audioURL) }63    }64    func stop() -> Double { stops += 1; return writtenSeconds }65}6667@MainActor68final class FakePlayer: AudioPlaying {69    var duration: Double = 1070    var currentTime: Double = 071    var isPlaying = false72    var rate: Float = 173    func play() { isPlaying = true }74    func pause() { isPlaying = false }75}7677struct FakeSpeech: SpeechTranscribing {78    var language = "fr-CA"79    var available = true80    var pieces: [SpeechPiece] = []81    var error: OnDeviceTranscriptionError?82    func availability() async -> (language: String, available: Bool) { (language, available) }83    func transcribe(fileURL: URL) async throws -> [SpeechPiece] {84        if let error { throw error }85        return pieces86    }87}8889struct NoServer: ServerTranscribing {90    func transcribe(attachmentId: String, summarize: Bool, force: Bool) async throws -> AudioTranscription { throw RepositoryError.offline }91    func wait(attachmentId: String) async throws -> AudioTranscription { throw RepositoryError.offline }92}9394final class Bodies: @unchecked Sendable {95    private let lock = NSLock()96    private var bodies: [Data] = []97    func add(_ body: Data) { lock.withLock { bodies.append(body) } }98    var all: [Data] { lock.withLock { bodies } }99}100101/// What a voice-note model did to its note.102@MainActor103final class NodeSink {104    var commits: [PMNode?] = []105    var insertions: [String] = []106    var deletedAttachments: [String] = []107    var last: AudioBlock? { commits.last.flatMap { $0 }.flatMap(AudioBlock.init(node:)) }108}109110@MainActor111private func fixture(_ name: String, _ ext: String) throws -> String {112    final class Marker {}113    let url = try #require(Bundle(for: Marker.self).url(forResource: name, withExtension: ext))114    return try String(contentsOf: url, encoding: .utf8)115}116117private let attachmentId = "82f70f18-d19f-4d58-ba81-8a4d72099e17"118119@MainActor120private func voiceModel(block: AudioBlock = AudioBlock(attachmentId: attachmentId, duration: 6.87, title: "Réunion", recordedAt: Date(timeIntervalSince1970: 1_795_112_551)),121                        sink: NodeSink, player: FakePlayer = FakePlayer(), speech: FakeSpeech = FakeSpeech(), server: any ServerTranscribing = NoServer(),122                        uploaded: Bool = true, editable: Bool = true) -> VoiceNoteModel {123    VoiceNoteModel(block: block, isEditable: editable, commit: { sink.commits.append($0) }, insertBelow: { sink.insertions.append($0) },124                   localFile: { _ in FileManager.default.temporaryDirectory.appendingPathComponent("voice.m4a") }, makePlayer: { _ in player },125                   onDevice: speech, server: server, ensureUploaded: { _ in uploaded }, deleteAttachment: { sink.deletedAttachments.append($0) })126}127128// MARK: - Recorder129130@MainActor131@Suite("Voix — enregistreur")132struct VoiceRecorderTests {133    private func make(_ status: MicrophonePermission = .granted, answer: MicrophonePermission = .granted) -> (VoiceRecorderModel, FakeRecorder, FakeMicrophone) {134        let recorder = FakeRecorder(), microphone = FakeMicrophone(status, answer: answer)135        return (VoiceRecorderModel(recorder: recorder, authorizer: microphone, sampling: nil), recorder, microphone)136    }137138    @Test func idleRecordingPausedRecordingStopped() async throws {139        let (model, recorder, _) = make()140        #expect(model.phase == .idle && !model.isActive)141        await model.start()142        #expect(model.phase == .recording)143        recorder.currentTime = 2.5; recorder.power = -12144        model.tick()145        #expect(model.elapsed == 2.5 && model.level > 0 && model.history.count == 1)146        model.pause()147        #expect(model.phase == .paused(.user) && model.level == 0)148        model.tick()149        #expect(model.history.count == 1, "the meter does not move while paused")150        model.resume()151        #expect(model.phase == .recording)152        recorder.currentTime = 4153        let audio = try #require(model.stop())154        #expect(model.phase == .stopped(audio) && audio.duration == 4 && model.result == audio)155        #expect(FileManager.default.fileExists(atPath: audio.fileURL.path))156        #expect(audio.fileURL.pathExtension == "m4a")157        #expect(recorder.log == ["start", "pause", "resume", "stop"])158        #expect(model.stop() == nil, "stopping twice does nothing")159        model.reset()160        #expect(model.phase == .idle && FileManager.default.fileExists(atPath: audio.fileURL.path), "reset keeps the file handed to a note")161    }162163    @Test func anInterruptionPausesAndKeepsTheRecording() async throws {164        let (model, recorder, _) = make()165        await model.start()166        recorder.currentTime = 3167        recorder.onEvent?(.interruptionBegan)168        #expect(model.phase == .paused(.interruption) && model.elapsed == 3)169        recorder.onEvent?(.interruptionEnded(shouldResume: true))170        #expect(model.phase == .paused(.interruption), "never resumed without the user")171        // the call is still on: the system refuses the session, the recording stays paused172        recorder.refusesResume = true173        model.resume()174        #expect(model.phase == .paused(.interruption))175        recorder.refusesResume = false176        model.resume()177        #expect(model.phase == .recording)178        recorder.currentTime = 5179        let audio = try #require(model.stop())180        #expect(audio.duration == 5 && FileManager.default.fileExists(atPath: audio.fileURL.path))181    }182183    @Test func aLostInputAndTheBackgroundPauseToo() async throws {184        let (model, recorder, _) = make()185        await model.start()186        recorder.onEvent?(.inputLost)187        #expect(model.phase == .paused(.inputLost))188        model.resume()189        NotificationCenter.default.post(name: UIApplication.didEnterBackgroundNotification, object: nil)190        #expect(model.phase == .paused(.background), "no background audio mode: the recording is paused, and kept")191        #expect(model.isActive)192    }193194    @Test func permissionStates() async {195        var (model, recorder, microphone) = make(.denied)196        await model.start()197        #expect(model.phase == .denied(restricted: false) && recorder.log.isEmpty && microphone.requests == 0)198        // allowed in Settings meanwhile199        microphone.status = .granted200        model.refreshPermission()201        #expect(model.phase == .idle)202        await model.start()203        #expect(model.phase == .recording)204205        (model, recorder, microphone) = make(.restricted)206        await model.start()207        #expect(model.phase == .denied(restricted: true) && recorder.log.isEmpty)208209        (model, recorder, microphone) = make(.undetermined, answer: .granted)210        await model.start()211        #expect(microphone.requests == 1 && model.phase == .recording, "asked at first use")212213        (model, recorder, microphone) = make(.undetermined, answer: .denied)214        await model.start()215        #expect(microphone.requests == 1 && model.phase == .denied(restricted: false) && recorder.log.isEmpty)216    }217218    @Test func aFailureToStartIsAStateAndAnEncodingFailureKeepsTheFile() async throws {219        let (model, recorder, _) = make()220        recorder.failsToStart = true221        await model.start()222        guard case .failed = model.phase else { Issue.record("expected a failure state, got \(model.phase)"); return }223        recorder.failsToStart = false224        await model.start()225        recorder.currentTime = 1.5226        recorder.onEvent?(.encodingFailed)227        let audio = try #require(model.result)228        #expect(audio.duration == 1.5 && FileManager.default.fileExists(atPath: audio.fileURL.path), "what was recorded is kept")229    }230231    @Test func discardingDeletesTheFile() async throws {232        let (model, recorder, _) = make()233        await model.start()234        let url = try #require(recorder.url)235        model.discard()236        #expect(model.phase == .idle && recorder.log.last == "cancel" && !FileManager.default.fileExists(atPath: url.path))237    }238239    @Test func theLevelMeterIsSmoothAndBounded() {240        #expect(LevelMeter.normalized(power: -160) == 0 && LevelMeter.normalized(power: LevelMeter.floor) == 0)241        #expect(LevelMeter.normalized(power: 0) == 1 && LevelMeter.normalized(power: 6) == 1)242        #expect(LevelMeter.normalized(power: .nan) == 0 && LevelMeter.normalized(power: -.infinity) == 0)243        let quiet = LevelMeter.normalized(power: -40), loud = LevelMeter.normalized(power: -10)244        #expect(quiet > 0 && quiet < loud && loud < 1)245        // attack faster than release246        let up = LevelMeter.smoothed(previous: 0, target: 1), down = LevelMeter.smoothed(previous: 1, target: 0)247        #expect(up > 0.5 && up < 1 && down > 0.5 && down < 1 && (1 - down) < up)248        var level: Float = 0249        for _ in 0..<40 { level = LevelMeter.smoothed(previous: level, target: 0.8) }250        #expect(abs(level - 0.8) < 0.001, "converges")251        #expect(LevelMeter.smoothed(previous: 2, target: 5) <= 1 && LevelMeter.smoothed(previous: -1, target: -3) >= 0)252        var history: [Float] = []253        for i in 0..<60 { history = LevelMeter.appending(Float(i) / 10, to: history, capacity: 48) }254        #expect(history.count == 48 && history.allSatisfy { (0...1).contains($0) } && history.first == 1)255        #expect(LevelMeterView.bar(at: 47, history: [0.2, 0.7], capacity: 48) == 0.7 && LevelMeterView.bar(at: 46, history: [0.2, 0.7], capacity: 48) == 0.2)256        #expect(LevelMeterView.bar(at: 0, history: [0.2, 0.7], capacity: 48) == 0)257        #expect(LevelMeterView.steadyLevel(0.6) == 0.5 && LevelMeterView.steadyLevel(0.9) == 1, "five steps under Reduce Motion")258    }259260    /// The real AVAudioRecorder, when the simulator grants the microphone (`xcrun simctl privacy <udid> grant microphone …`).261    @Test(.enabled(if: AVCaptureDevice.authorizationStatus(for: .audio) == .authorized, "the microphone is not authorised for the test host"))262    func theSystemRecorderWritesAPlayableFile() async throws {263        let model = VoiceRecorderModel(recorder: SystemAudioRecorder(), authorizer: SystemMicrophoneAuthorizer())264        await model.start()265        try #require(model.phase == .recording, "phase: \(model.phase)")266        try await Task.sleep(for: .milliseconds(1300))267        model.pause()268        try await Task.sleep(for: .milliseconds(400))269        model.resume()270        try await Task.sleep(for: .milliseconds(400))271        let audio = try #require(model.stop())272        defer { MediaTemp.discard(audio.fileURL) }273        // 1.7 s were asked for around a 0.4 s pause (the simulator's input does not always honour the pause to the sample)274        #expect(audio.duration >= 1.2 && audio.duration < 2.8, "\(audio.duration)")275        let player = try AVAudioPlayer(contentsOf: audio.fileURL)276        #expect(abs(player.duration - audio.duration) < 0.05, "the duration kept is the file's: file \(player.duration) s, kept \(audio.duration) s")277        #expect(player.format.channelCount == 1 && player.format.sampleRate == 44_100)278        #expect(model.history.count > 5, "the meter sampled the input")279    }280}281282// MARK: - Transcript283284@MainActor285@Suite("Voix — transcription")286struct VoiceTranscriptTests {287    @Test func recognisedWordsBecomePhrasesWithTheirStartTime() {288        func piece(_ text: String, _ start: Double, _ duration: Double = 0.3) -> SpeechPiece { SpeechPiece(text: text, start: start, duration: duration) }289        let pieces = [piece("Réunion", 0), piece("du", 0.4), piece("19", 0.6), piece("novembre.", 0.9),290                      piece("Nous", 1.4), piece("retenons", 1.7), piece("le", 2.1), piece("taux", 2.3), piece("fixe", 2.6),291                      // a silence of more than 0.9 s starts a new phrase292                      piece("Marie", 4.4), piece("envoie", 4.8), piece("les", 5.1), piece("documents", 5.3), piece("", 5.6), piece("lundi", 5.9), piece("matin", 6.2)]293        let segments = TranscriptSegmenter.segments(from: pieces)294        #expect(segments.map(\.text) == ["Réunion du 19 novembre.", "Nous retenons le taux fixe", "Marie envoie les documents lundi matin"])295        #expect(segments.map(\.t) == [0, 1.4, 4.4])296        #expect(segments.map(\.timecode) == ["00:00", "00:01", "00:04"])297298        // a long run without punctuation is cut by length, and out-of-order input is sorted299        let run = (0..<40).map { piece("mot\($0)", Double($0) * 0.3) }.reversed()300        let cut = TranscriptSegmenter.segments(from: Array(run))301        #expect(cut.count == 3 && cut[0].text.split(separator: " ").count == TranscriptSegmenter.maxWords && cut[0].t == 0)302        #expect(cut.flatMap { $0.text.split(separator: " ") }.count == 40, "no word lost")303        #expect(TranscriptSegmenter.segments(from: []).isEmpty && TranscriptSegmenter.segments(from: [piece("  ", 1)]).isEmpty)304        #expect(TranscriptSegmenter.segments(from: [piece("Seul", -0.2)]).first?.t == 0)305    }306307    @Test func onDeviceTranscriptionWritesTheNodeWithATruthfulEngine() async throws {308        let sink = NodeSink()309        let speech = FakeSpeech(pieces: [SpeechPiece(text: "Bonjour", start: 0.1, duration: 0.4), SpeechPiece(text: "tout le monde.", start: 0.6, duration: 0.8)])310        let model = voiceModel(sink: sink, speech: speech)311        await model.load()312        await model.runOnDevice()313        #expect(model.transcription == .idle)314        let block = try #require(sink.last)315        #expect(block.transcript == [TranscriptSegment(t: 0.1, text: "Bonjour tout le monde.")])316        #expect(block.transcriptStatus == .done && block.transcriptEngine == "apple-speech-on-device")317        #expect(model.engineLabel == "Transcription sur l’appareil")318        #expect(block.summary == nil, "nothing is invented on the device")319    }320321    @Test func onDeviceStatesUnavailableDeniedAndNoSpeech() async {322        var sink = NodeSink()323        var model = voiceModel(sink: sink, speech: FakeSpeech(language: "nl-NL", available: false))324        await model.load(); await model.runOnDevice()325        #expect(model.transcription == .unavailableOnDevice("nl-NL") && sink.commits.isEmpty)326327        sink = NodeSink()328        model = voiceModel(sink: sink, speech: FakeSpeech(error: .denied(restricted: false)))329        await model.load(); await model.runOnDevice()330        guard case .failed(let message) = model.transcription else { Issue.record("expected a failure"); return }331        #expect(message.contains("Réglages") && sink.commits.isEmpty)332333        sink = NodeSink()334        model = voiceModel(sink: sink, speech: FakeSpeech(pieces: []))335        await model.load(); await model.runOnDevice()336        guard case .failed = model.transcription else { Issue.record("expected a failure"); return }337        #expect(sink.commits.isEmpty)338        model.dismissTranscriptionState()339        #expect(model.transcription == .idle)340    }341342    @Test func tappingATranscriptLineSeeksThePlayerAndTheCurrentLineFollows() async {343        var block = AudioBlock(attachmentId: attachmentId, duration: 10)344        block.transcript = [TranscriptSegment(t: 0, text: "Un"), TranscriptSegment(t: 4.4, text: "Deux"), TranscriptSegment(t: 8, text: "Trois")]345        #expect(block.segmentIndex(at: 0) == 0 && block.segmentIndex(at: 4.39) == 0 && block.segmentIndex(at: 4.4) == 1 && block.segmentIndex(at: 99) == 2)346        #expect(AudioBlock(attachmentId: attachmentId).segmentIndex(at: 3) == nil)347        let player = FakePlayer(), sink = NodeSink()348        let model = voiceModel(block: block, sink: sink, player: player)349        await model.load()350        #expect(model.audioState == .ready && model.currentSegment == nil)351        model.seek(toSegment: 1)352        #expect(player.currentTime == 4.4 && player.isPlaying && model.isPlaying && model.currentSegment == 1)353        player.currentTime = 8.2354        model.tick()355        #expect(model.currentSegment == 2 && model.position == 8.2)356        model.seek(to: 500)357        #expect(player.currentTime == 10, "clamped to the duration")358        model.seek(toSegment: 7)359        #expect(player.currentTime == 10, "an unknown line does nothing")360        model.cycleSpeed()361        #expect(model.speed == 1.25 && player.rate == 1.25)362        // the end of the file stops the playback state363        player.isPlaying = false364        model.tick()365        #expect(!model.isPlaying)366        #expect(sink.commits.isEmpty, "playing never edits the note")367    }368}369370// MARK: - Server flow371372@MainActor373@Suite("Voix — transcription du serveur (faux serveur, réponses enregistrées)", .serialized)374struct VoiceServerTests {375    private func transcriber(_ app: AppModel) -> RepositoryTranscriber {376        RepositoryTranscriber(audio: app.repositories.audio, interval: .milliseconds(20), timeout: .seconds(5))377    }378379    @Test func aPaidTranscriptionIsOnlySentAfterTheConfirmationThenWrittenIntoTheNode() async throws {380        let fake = FakeBackend()381        defer { fake.shutdown() }382        let app = try CaptureTestSupport.model(on: fake)383        let queued = try fixture("voice-transcribe-queued", "json"), done = try fixture("voice-done", "json")384        let bodies = Bodies()385        fake.stub("POST", path: "^/api/audio/[0-9a-f-]+/transcribe$") { request in bodies.add(request.body); return .json(queued, status: 202) }386        fake.stub("GET", path: "^/api/audio/[0-9a-f-]+$", json: done)387388        let sink = NodeSink()389        var start = AudioBlock(attachmentId: attachmentId, duration: 0, title: "Réunion")390        start[attr: "futureAttr"] = .object(["kept": .bool(true)])391        let model = voiceModel(block: start, sink: sink, server: transcriber(app))392        model.requestServerTranscription()393        #expect(model.transcription == .confirmingPaid)394        #expect(fake.count("POST", "^/api/audio/") == 0, "nothing is sent, nothing is charged, before the confirmation")395        model.cancelServerTranscription()396        #expect(model.transcription == .idle && fake.count("POST", "^/api/audio/") == 0)397398        model.requestServerTranscription()399        await model.runOnServer(summarize: true)400        #expect(model.transcription == .idle)401        #expect(fake.count("POST", "^/api/audio/\(attachmentId)/transcribe$") == 1)402        #expect(bodies.all.count == 1 && String(decoding: bodies.all[0], as: UTF8.self).contains(#""summarize":true"#))403404        let block = try #require(sink.last)405        #expect(block.transcript.map(\.text) == ["Réunion du 19 novembre. Nous retenons le taux fixe de 5 ans.", "Marie envoie les documents à la banque lundi matin."])406        #expect(block.transcript.map(\.t) == [0, 4.4])407        #expect(block.transcriptStatus == .done)408        #expect(block.transcriptEngine == "openai:whisper-1", "the engine the server reports")409        #expect(model.engineLabel.contains("openai:whisper-1"))410        #expect(abs(block.duration - 6.87) < 0.001, "the duration measured by the server fills a missing one")411        let summary = try #require(block.summary)412        #expect(summary.model == "gpt-5.4-mini" && summary.decisions == ["Ils retiennent le taux fixe de 5 ans."] && summary.tasks == ["Marie envoie les documents à la banque lundi matin."])413        #expect(block[attr: "futureAttr"] == .object(["kept": .bool(true)]), "unknown attributes survive")414        #expect(block.title == "Réunion" && block.attachmentId == attachmentId)415416        // The AI summary is in the node, shown — and NOT in the note until the user asks.417        #expect(sink.insertions.isEmpty)418        model.insertSummary()419        #expect(sink.insertions == ["Réunion du 19 novembre. Ils retiennent un taux fixe de 5 ans. Marie enverra les documents à la banque lundi matin.\n\n- Ils retiennent le taux fixe de 5 ans."])420        model.createTasks()421        #expect(sink.insertions.last == "- [ ] Marie envoie les documents à la banque lundi matin.")422        #expect(NoteDocument(markdown: sink.insertions.last!).taskItems().total == 1)423        #expect(model.insertedSummary && model.insertedTasks)424    }425426    @Test func anExistingTranscriptIsNotPolledAndNotChargedAgain() async throws {427        let fake = FakeBackend()428        defer { fake.shutdown() }429        let app = try CaptureTestSupport.model(on: fake)430        // `200 {"queued": false, …current state…}`: the transcript already exists431        let done = try fixture("voice-done", "json").replacingOccurrences(of: #"{"attachmentId""#, with: #"{"queued":false,"attachmentId""#)432        fake.stub("POST", path: "^/api/audio/[0-9a-f-]+/transcribe$", json: done)433        let sink = NodeSink()434        let model = voiceModel(sink: sink, server: transcriber(app))435        model.requestServerTranscription()436        await model.runOnServer(summarize: false)437        #expect(sink.last?.transcript.count == 2)438        #expect(fake.count("GET", "^/api/audio/") == 0)439    }440441    @Test func unsupportedOfflineAndFailedAreStatesAndNeverTouchTheNode() async throws {442        let fake = FakeBackend()443        defer { fake.shutdown() }444        let app = try CaptureTestSupport.model(on: fake)445446        // a server without the voice routes answers the HTML 404 of the web application447        let html = try fixture("voice-missing-route", "txt")448        fake.stub(path: "^/api/audio/") { _ in FakeBackend.StubResponse(status: 404, body: Data(html.utf8), headers: ["content-type": "text/html; charset=utf-8"]) }449        var sink = NodeSink()450        var model = voiceModel(sink: sink, server: transcriber(app))451        model.requestServerTranscription()452        await model.runOnServer(summarize: false)453        #expect(model.transcription == .unsupported && sink.commits.isEmpty)454455        fake.clearStubs()456        fake.setOffline(true)457        sink = NodeSink()458        model = voiceModel(sink: sink, server: transcriber(app))459        model.requestServerTranscription()460        await model.runOnServer(summarize: false)461        #expect(model.transcription == .offline && sink.commits.isEmpty)462        fake.setOffline(false)463464        // the recording has not reached the server yet (uploads go through the synchronisation)465        sink = NodeSink()466        let before = fake.count("POST", "^/api/audio/")467        model = voiceModel(sink: sink, server: transcriber(app), uploaded: false)468        model.requestServerTranscription()469        await model.runOnServer(summarize: false)470        #expect(model.transcription == .offline && fake.count("POST", "^/api/audio/") == before, "no request for a file the server does not have")471472        // the server's own refusals473        fake.stub("POST", path: "^/api/audio/[0-9a-f-]+/transcribe$", json: #"{"error":"Transcription is not configured","code":"not_configured"}"#, status: 503)474        sink = NodeSink()475        model = voiceModel(sink: sink, server: transcriber(app))476        model.requestServerTranscription()477        await model.runOnServer(summarize: false)478        #expect(model.transcription == .failed("La transcription n’est pas configurée sur le serveur.") && sink.commits.isEmpty)479480        // a job that fails: the node says so, the transcript stays empty481        fake.clearStubs()482        let failed = #"{"attachmentId":"\#(attachmentId)","status":"failed","transcript":[],"text":"","engine":null,"model":null,"language":null,"duration":null,"segments":null,"summary":null,"error":"Engine refused the file","costUsd":null,"requestedAt":"2026-11-19T18:22:31.175Z","finishedAt":"2026-11-19T18:22:33.000Z"}"#483        let queuedAnswer = try fixtureQueued()484        fake.stub("POST", path: "^/api/audio/[0-9a-f-]+/transcribe$") { _ in .json(queuedAnswer, status: 202) }485        fake.stub("GET", path: "^/api/audio/[0-9a-f-]+$", json: failed)486        sink = NodeSink()487        model = voiceModel(sink: sink, server: transcriber(app))488        model.requestServerTranscription()489        await model.runOnServer(summarize: false)490        #expect(model.transcription == .failed("Engine refused the file"))491        #expect(sink.last?.transcriptStatus == .failed && sink.last?.transcript.isEmpty == true)492493        // a read-only note never asks494        sink = NodeSink()495        model = voiceModel(sink: sink, server: transcriber(app), editable: false)496        model.requestServerTranscription()497        #expect(model.transcription == .idle)498    }499500    /// End to end through the application model: record → inbox capture → upload by the synchronisation → paid transcription →501    /// the node saved in the page → the server has the transcript in the document.502    @Test func aVoiceCaptureIsUploadedTranscribedAndSavedThroughANormalPageSave() async throws {503        let fake = FakeBackend()504        defer { fake.shutdown() }505        let app = try CaptureTestSupport.model(on: fake)506        let url = try MediaTemp.newFileURL(named: "Mémo vocal test.m4a")507        try ToneFile.write(to: url, seconds: 1)508        let recordedAt = Date(timeIntervalSince1970: 1_795_112_551)509        let outcome = try await app.capture(CaptureDraft(kind: CaptureKindName.audio), title: nil, voice: VoiceCapture(fileURL: url, duration: 1, recordedAt: recordedAt))510        #expect(outcome.failedFiles == 0)511        let page = try #require(try await app.store.page(id: outcome.pageId))512        #expect(page.captureKind == CaptureKindName.audio && page.inboxAt != nil && page.title.hasPrefix("Mémo vocal · "))513        let node = try #require(await VoiceNoteStandalone.audioNode(in: app, noteId: outcome.pageId))514        let block = try #require(AudioBlock(node: node))515        let id = try #require(block.attachmentId)516        #expect(block.duration == 1 && block.recordedAt == "2026-11-19T18:22:31.000Z" && block.transcriptStatus == .none && block.id != nil)517        #expect(try #require(try await app.store.attachment(id: id)).uploaded == false)518519        let queuedAnswer = try fixtureQueued()520        fake.stub("POST", path: "^/api/audio/[0-9a-f-]+/transcribe$") { _ in .json(queuedAnswer, status: 202) }521        fake.stub("GET", path: "^/api/audio/[0-9a-f-]+$", json: try fixture("voice-done", "json"))522        let context = VoiceNoteStandalone.context(app: app, noteId: outcome.pageId, blockId: block.id, dismiss: {})523        let model = VoiceNoteModel(node: node, context: context)524        model.requestServerTranscription()525        await model.runOnServer(summarize: true)526        #expect(model.transcription == .idle, "\(model.transcription)")527        #expect(fake.attachment(id) != nil, "the recording was uploaded by the synchronisation before the request")528        #expect(fake.count("POST", "^/api/audio/\(id)/transcribe$") == 1)529530        // the commit is a normal page save531        try await Task.sleep(for: .milliseconds(200))532        let savedNode = try #require(await VoiceNoteStandalone.audioNode(in: app, noteId: outcome.pageId))533        let saved = try #require(AudioBlock(node: savedNode))534        #expect(saved.transcript.count == 2 && saved.summary != nil && saved.transcriptEngine == "openai:whisper-1")535        #expect(try #require(try await app.store.page(id: outcome.pageId)).plainText.contains("Marie envoie les documents"), "the transcript is searchable")536        #expect(await app.syncAndWait() == .completed)537        let server = String(decoding: try #require(fake.page(outcome.pageId)).content, as: UTF8.self)538        #expect(server.contains("Nous retenons le taux fixe de 5 ans.") && server.contains(#""transcriptStatus":"done""#))539        #expect(fake.allPages.count == 1)540541        // « Insérer le résumé » adds blocks below the voice note, in the stored document542        model.insertSummary()543        try await Task.sleep(for: .milliseconds(200))544        let document = try await CaptureTestSupport.document(app, outcome.pageId)545        let index = try #require(document.lines.firstIndex { $0.type == AudioBlock.nodeType })546        #expect(document.lines.count > index + 1 && document.lines[index + 1].type == NodeType.paragraph)547        #expect(document.schemaViolations().isEmpty)548    }549}550551@MainActor private func fixtureQueued() throws -> String { try fixture("voice-transcribe-queued", "json") }552553// MARK: - Node, original audio, dictation, sample554555@MainActor556@Suite("Voix — nœud audio, audio original, dictée")557struct VoiceNodeTests {558    @Test func theAudioNodeRoundTripsLosslessly() throws {559        let json = #"{"type":"doc","content":[{"type":"audio","attrs":{"id":"aud1234567","attachmentId":"82f70f18-d19f-4d58-ba81-8a4d72099e17","duration":6.87,"title":"Réunion","recordedAt":"2026-11-19T18:22:31.175Z","transcript":[{"t":0,"text":"Un","speaker":"A"},{"t":4.4,"text":"Deux"}],"transcriptStatus":"done","transcriptEngine":"openai:whisper-1","summary":{"text":"Résumé","decisions":["D"],"tasks":["T"],"model":"gpt-5.4-mini","at":"2026-11-19T18:22:36.405Z","confidence":0.9},"waveform":[0.1,0.4],"futureFlag":true},"futureKey":{"a":1}}]}"#560        let document = try NoteDocument(json: Data(json.utf8))561        #expect(document.schemaViolations().isEmpty)562        let original = try JSONValue(data: Data(json.utf8))563        #expect(try JSONValue(data: try document.jsonData()) == original, "untouched: identical")564565        // editing through the typed view keeps what this version does not know566        var edited = document567        let index = try #require(edited.lines.firstIndex { $0.type == AudioBlock.nodeType })568        var block = try #require(AudioBlock(node: edited.lines[index].node))569        #expect(block.summary?.text == "Résumé" && block.transcript.count == 2)570        block.title = "Réunion renommée"571        edited.lines[index] = Line(blockAtom: block.node(), path: edited.lines[index].path)572        let attrs = try #require(try JSONValue(data: try edited.jsonData()).objectValue?["content"]?.arrayValue?.first?.objectValue?["attrs"]?.objectValue)573        #expect(attrs["title"] == .string("Réunion renommée"))574        #expect(attrs["waveform"]?.arrayValue?.compactMap(\.doubleValue) == [0.1, 0.4] && attrs["futureFlag"] == .bool(true))575        #expect(attrs["transcript"]?.arrayValue?.first?.objectValue?["speaker"] == .string("A"), "a transcript that is not rewritten keeps its unknown fields")576        #expect(attrs["summary"]?.objectValue?["confidence"]?.doubleValue == 0.9)577        let node = try #require(try JSONValue(data: try edited.jsonData()).objectValue?["content"]?.arrayValue?.first?.objectValue)578        #expect(node["futureKey"]?.objectValue?["a"]?.doubleValue == 1)579    }580581    @Test func deletingTheOriginalKeepsTheTranscriptAndNeedsOne() async throws {582        var block = AudioBlock(attachmentId: attachmentId, duration: 6.87, title: "Réunion")583        let sink = NodeSink()584        // without a transcript there is nothing to keep: the action does nothing585        var model = voiceModel(block: block, sink: sink)586        await model.load()587        await model.deleteOriginalAudio()588        #expect(sink.deletedAttachments.isEmpty && sink.commits.isEmpty && model.hasAudio)589590        block.transcript = [TranscriptSegment(t: 0, text: "Un"), TranscriptSegment(t: 4.4, text: "Deux")]591        block.transcriptStatus = .done592        block.transcriptEngine = "apple-speech-on-device"593        model = voiceModel(block: block, sink: sink)594        await model.load()595        #expect(model.audioState == .ready)596        await model.deleteOriginalAudio()597        #expect(sink.deletedAttachments == [attachmentId])598        let kept = try #require(sink.last)599        #expect(kept.attachmentId == nil && kept.transcript.count == 2 && kept.transcriptStatus == .done && kept.title == "Réunion" && kept.duration == 6.87)600        #expect(model.audioState == .removed && !model.hasAudio)601        // the node without audio is still a valid `audio` node602        #expect(NoteDocument(lines: [Line(blockAtom: kept.node(), path: [])]).schemaViolations().isEmpty)603        // nothing more to transcribe or delete604        model.requestServerTranscription()605        #expect(model.transcription == .idle)606607        // a read-only note keeps everything608        let readOnly = voiceModel(block: block, sink: NodeSink(), editable: false)609        await readOnly.load()610        await readOnly.deleteOriginalAudio()611        #expect(readOnly.hasAudio)612613        // deleting the whole voice note removes the block and its file614        let other = NodeSink()615        let whole = voiceModel(block: block, sink: other)616        await whole.deleteBlock()617        #expect(other.deletedAttachments == [attachmentId] && other.commits.count == 1 && other.commits[0] == nil)618    }619620    @Test func renamingAndAMissingFile() async {621        let sink = NodeSink()622        let model = VoiceNoteModel(block: AudioBlock(attachmentId: attachmentId, duration: 3, title: "Avant"), isEditable: true, commit: { sink.commits.append($0) },623                                   insertBelow: { _ in }, localFile: { _ in throw CocoaError(.fileNoSuchFile) }, server: NoServer(), ensureUploaded: { _ in false }, deleteAttachment: { _ in })624        await model.load()625        #expect(model.audioState == .missing)626        model.rename("  Après ")627        #expect(sink.last?.title == "Après")628        model.rename("Après")629        #expect(sink.commits.count == 1, "no change, no save")630        model.togglePlayback()631        #expect(!model.isPlaying)632    }633634    @Test func dictationAppendsWhatIsSaidAndStops() async throws {635        let engine = FakeDictation()636        let model = DictationModel(engine: engine, silenceTimeout: .milliseconds(120))637        var field = "Déjà écrit."638        model.onUpdate = { field = $0 }639        await model.start(appendingTo: field)640        #expect(model.phase == .listening && engine.audioURL == nil, "no audio is kept unless asked")641        engine.onText?("bonjour")642        engine.onText?("Bonjour tout le monde")643        #expect(field == "Déjà écrit. Bonjour tout le monde", "partial results revise the same dictation")644        try await Task.sleep(for: .milliseconds(400))645        #expect(model.phase == .stopped(.silence) && engine.stops == 1)646        engine.onText?("trop tard")647        #expect(field == "Déjà écrit. Bonjour tout le monde")648        #expect(model.keptAudio == nil)649        #expect(DictationModel.merged(base: "", spoken: " Seul ") == "Seul" && DictationModel.merged(base: "A\n", spoken: "b") == "A\nb" && DictationModel.merged(base: "A", spoken: " ") == "A")650651        // « Garder l’audio »652        let keeping = DictationModel(engine: engine, silenceTimeout: nil)653        keeping.keepsAudio = true654        engine.writtenSeconds = 3.2655        await keeping.start(appendingTo: "")656        let url = try #require(engine.audioURL)657        engine.onText?("Texte dicté")658        keeping.stop()659        #expect(keeping.phase == .stopped(.user))660        #expect(keeping.keptAudio?.fileURL == url && keeping.keptAudio?.duration == 3.2 && keeping.spoken == "Texte dicté")661        // an interruption ends the dictation; the text stays662        await keeping.start(appendingTo: "x")663        engine.onEnd?(.interruption)664        #expect(keeping.phase == .stopped(.interruption))665        await keeping.start(appendingTo: "x")666        NotificationCenter.default.post(name: UIApplication.didEnterBackgroundNotification, object: nil)667        #expect(keeping.phase == .stopped(.background))668    }669670    @Test func dictationPermissionAndAvailabilityStates() async {671        let engine = FakeDictation()672        let model = DictationModel(engine: engine, silenceTimeout: nil)673        for (failure, phase) in [(DictationFailure.microphone(restricted: false), DictationModel.Phase.microphoneDenied(restricted: false)),674                                 (.microphone(restricted: true), .microphoneDenied(restricted: true)),675                                 (.speech(restricted: false), .speechDenied(restricted: false)),676                                 (.unavailable(language: "nl-NL"), .unavailable(language: "nl-NL")),677                                 (.engine("format"), .stopped(.failed("format")))] {678            engine.failure = failure679            await model.start(appendingTo: "")680            #expect(model.phase == phase && !model.isListening)681        }682    }683684    @Test func aDictationWithKeptAudioIsSavedAsTextPlusAVoiceNote() async throws {685        let (app, _) = try CaptureTestSupport.localModel()686        let engine = FakeDictation()687        let dictation = DictationModel(engine: engine, silenceTimeout: nil)688        let model = QuickCaptureModel(mode: .dictation, recorder: VoiceRecorderModel(recorder: FakeRecorder(), authorizer: FakeMicrophone(.granted), sampling: nil),689                                      dictation: dictation, drafts: CaptureDraftStore(defaults: UserDefaults(suiteName: "dict-\(UUID().uuidString)")!))690        dictation.keepsAudio = true691        await model.toggleDictation()692        // the fake engine "records": a real m4a at the URL it was given693        try FileManager.default.removeItem(at: try #require(engine.audioURL))694        try ToneFile.write(to: try #require(engine.audioURL), seconds: 1)695        engine.writtenSeconds = 1696        engine.onText?("Rappeler la banque lundi")697        #expect(model.text == "Rappeler la banque lundi")698        await model.toggleDictation()699        #expect(model.canSave && model.kind == CaptureKindName.note)700        let id = try #require(await model.save(app: app))701        let node = try #require(await VoiceNoteStandalone.audioNode(in: app, noteId: id))702        let block = try #require(AudioBlock(node: node))703        #expect(block.transcript == [TranscriptSegment(t: 0, text: "Rappeler la banque lundi")] && block.transcriptEngine == "apple-speech-on-device" && block.duration == 1)704        #expect(try #require(try await app.store.page(id: id)).title == "Rappeler la banque lundi")705    }706707    @Test func theSeededVoiceNoteHasAPlayableToneATranscriptAndAnAISummary() async throws {708        let (app, _) = try CaptureTestSupport.localModel()709        #expect(await SampleData.seed(into: app))710        let node = try #require(await VoiceNoteStandalone.audioNode(in: app, noteId: VoiceSample.noteID))711        let block = try #require(AudioBlock(node: node))712        #expect(block.transcript.count == 2 && block.summary?.tasks.count == 1 && block.title == "Réunion du 19 novembre")713        let url = try await app.localFile(forAttachment: try #require(block.attachmentId))714        let player = try AVAudioPlayer(contentsOf: url)715        #expect(abs(player.duration - 2) < 0.15, "\(player.duration)")716        #expect(block.segmentIndex(at: 1.5) == 1)717        let model = VoiceNoteModel(node: node, context: VoiceNoteStandalone.context(app: app, noteId: VoiceSample.noteID, blockId: block.id, dismiss: {}))718        await model.load()719        #expect(model.audioState == .ready && model.engineLabel.contains("openai:whisper-1"))720    }721722    @Test func theBlockDetailOfAnAudioNodeIsTheVoiceScreen() {723        BlockDetails.resetForTests()724        defer { BlockDetails.resetForTests() }725        let provider = BlockDetails.provider(for: AudioBlock(attachmentId: attachmentId).node())726        #expect(provider is VoiceBlockDetails)727        #expect(BlockDetails.provider(for: DrawingBlock().node()) is PencilBlockDetails, "the Pencil screens are still registered")728        #expect(Bundle.main.object(forInfoDictionaryKey: "NSMicrophoneUsageDescription") as? String != nil)729        #expect(Bundle.main.object(forInfoDictionaryKey: "NSSpeechRecognitionUsageDescription") as? String != nil)730        #expect((Bundle.main.object(forInfoDictionaryKey: "UIBackgroundModes") as? [String])?.contains("audio") == false, "no background audio mode")731    }732}733
Files main
Menu

Theme