SPB Git

Repository Public

BlocksAudioTests.swift 19.5 KB 412 lines swift

1// SP Notes Core2// File: Tests/SPNotesFeaturesTests/BlocksAudioTests.swift3// Description: The voice note: the pure transcript helpers, the view model's state machine with fake engines (record /4//              pause / resume / stop / hand-over or upload / transcription requested, polled, done, failed, cancelled;5//              offline; microphone refused), and the real AVFoundation player on a generated file, muted. The real6//              recorder is never started here: it would raise the system's microphone prompt on the developer's Mac.7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10import AVFoundation11import Foundation12import SPNotesDocument13import SPNotesSync14import Testing15@testable import SPNotesFeatures16@testable import SPNotesFeaturesCatalog1718@MainActor19final class FakeRecorder: AudioRecording {20    var permission: AudioPermission = .granted21    var grants = true22    var failsToStart = false23    private(set) var log: [String] = []24    private(set) var url: URL?25    var currentTime: TimeInterval = 026    var level: Double = 0.42728    func requestPermission() async -> Bool {29        log.append("permission")30        permission = grants ? .granted : .denied31        return grants32    }3334    func start(to url: URL) throws {35        if failsToStart { throw AudioEngineError.cannotRecord("no input") }36        log.append("start")37        self.url = url38        try Data("fake m4a bytes".utf8).write(to: url)39    }4041    func pause() { log.append("pause") }42    func resume() { log.append("resume") }43    func stop() -> TimeInterval { log.append("stop"); return currentTime }44}4546@MainActor47final class FakePlayer: AudioPlaying {48    var fileDuration: TimeInterval = 6.8749    var failsToLoad = false50    private(set) var loaded: URL?51    private(set) var log: [String] = []52    var currentTime: TimeInterval = 053    var duration: TimeInterval { loaded == nil ? 0 : fileDuration }54    var isPlaying = false55    var rate: Double = 156    var onFinish: (@MainActor () -> Void)?5758    func load(url: URL) throws -> TimeInterval {59        if failsToLoad { throw AudioEngineError.cannotPlay("bad file") }60        loaded = url61        log.append("load")62        return fileDuration63    }64    func play() { isPlaying = true; log.append("play") }65    func pause() { isPlaying = false; log.append("pause") }66    func seek(to time: TimeInterval) { currentTime = time; log.append("seek \(time)") }67    func finish() { isPlaying = false; onFinish?() }68}6970struct FakeFiles: AudioFileProvider {71    var adoptedId: String?72    var failure: RepositoryError?73    func localFileURL(attachmentId: String) async throws -> URL {74        if let failure { throw failure }75        return URL(fileURLWithPath: "/tmp/spn-fake-\(attachmentId).m4a")76    }77    func adopt(recording: URL, pageId: String?) async throws -> String? { adoptedId }78}7980@Suite("Blocks · voice note: transcript logic")81struct AudioLogicTests {82    @Test("the active segment is the last one that started; nil before the first")83    func activeSegment() {84        let segments = [TranscriptSegment(t: 0.5, text: "a"), TranscriptSegment(t: 4.4, text: "b"), TranscriptSegment(t: 9, text: "c")]85        #expect(AudioTranscriptLogic.activeIndex(segments, at: 0) == nil)86        #expect(AudioTranscriptLogic.activeIndex(segments, at: 0.5) == 0)87        #expect(AudioTranscriptLogic.activeIndex(segments, at: 4.39) == 0)88        #expect(AudioTranscriptLogic.activeIndex(segments, at: 4.4) == 1)89        #expect(AudioTranscriptLogic.activeIndex(segments, at: 120) == 2)90        #expect(AudioTranscriptLogic.activeIndex([], at: 3) == nil)91        // Segments stored out of order are still resolved by time.92        let shuffled = [segments[2], segments[0], segments[1]]93        #expect(AudioTranscriptLogic.activeIndex(shuffled, at: 5) == 2)94    }9596    @Test("seek time is clamped to the recording; a single segment at 0 has no timings; fractions; level meter; rate labels")97    func helpers() {98        #expect(AudioTranscriptLogic.seekTime(TranscriptSegment(t: 12, text: "x"), duration: 10) == 10)99        #expect(AudioTranscriptLogic.seekTime(TranscriptSegment(t: -1, text: "x"), duration: 10) == 0)100        #expect(AudioTranscriptLogic.seekTime(TranscriptSegment(t: 12, text: "x"), duration: 0) == 12)101        #expect(!AudioTranscriptLogic.hasTimings([TranscriptSegment(t: 0, text: "tout le texte")]))102        #expect(AudioTranscriptLogic.hasTimings([TranscriptSegment(t: 0, text: "a"), TranscriptSegment(t: 3, text: "b")]))103        #expect(AudioTranscriptLogic.fraction(time: 5, duration: 10) == 0.5)104        #expect(AudioTranscriptLogic.fraction(time: 50, duration: 10) == 1)105        #expect(AudioTranscriptLogic.fraction(time: 5, duration: 0) == 0)106        #expect(AudioTranscriptLogic.timestamp(Date(timeIntervalSince1970: 1_795_112_551.405)) == "2026-11-19T18:22:31.405Z")107        #expect(AudioLevel.normalized(decibels: -160) == 0)108        #expect(AudioLevel.normalized(decibels: 0) == 1)109        #expect(AudioLevel.normalized(decibels: -25) == 0.5)110        #expect(AudioLevel.normalized(decibels: .nan) == 0)111        #expect(AudioBlockView.rateLabel(1) == "1×")112        #expect(AudioBlockView.rateLabel(1.25) == "1,25×")113        for bar in 0..<28 { #expect((0.08...1).contains(AudioLevelMeter.height(bar: bar, of: 28, level: 0.7))) }114    }115}116117@Suite("Blocks · voice note: view model", .serialized)118@MainActor119struct AudioModelTests {120    private func make(_ world: FeatureWorld, block: AudioBlock? = nil, files: (any AudioFileProvider)? = nil, recorder: FakeRecorder = FakeRecorder(),121                      player: FakePlayer = FakePlayer(), changes: Box<[AudioBlock]> = Box([])) -> AudioBlockModel {122        AudioBlockModel(repositories: world.repositories, block: block, pageId: BlockSamples.sourcePageId, files: files, recorder: recorder, player: player,123                        pollInterval: .milliseconds(10), pollTimeout: .seconds(5), tickInterval: nil, now: { FeatureSamples.now }) { changes.value.append($0) }124    }125126    final class Box<T>: @unchecked Sendable { var value: T; init(_ value: T) { self.value = value } }127128    @Test("happy path: idle → recording → paused → recording → stopped → uploaded → transcription requested → pending → done, node handed to the editor")129    func fullCycle() async throws {130        let world = try await FeatureWorld()131        let recorder = FakeRecorder(), changes = Box<[AudioBlock]>([])132        let model = make(world, recorder: recorder, changes: changes)133        #expect(model.stage == .empty)134        #expect(!model.canTranscribe)135136        await model.startRecording()137        #expect(model.stage == .recording)138        recorder.currentTime = 3139        model.tick()140        #expect(model.elapsed == 3)141        #expect(model.inputLevel == 0.4)142        model.pauseRecording()143        #expect(model.stage == .paused)144        #expect(model.inputLevel == 0)145        model.resumeRecording()146        #expect(model.stage == .recording)147        recorder.currentTime = 6.874148        await model.stopRecording()149        #expect(recorder.log == ["permission", "start", "pause", "resume", "stop"])150        #expect(model.stage == .ready)151        let id = try #require(model.block.attachmentId)152        #expect(model.block.duration == 6.87)153        #expect(model.block.recordedAt == AudioTranscriptLogic.timestamp(FeatureSamples.now))154        #expect(model.block.title == "Note vocale")155        #expect(changes.value.count == 1)156        #expect(world.fake.count("POST", "^/api/files") + world.fake.count("PUT", "^/api/files") + world.fake.count("POST", "^/api/attachments") >= 1)157        #expect(world.fake.count("POST", "^/api/audio/") == 0)                 // nothing paid happened by itself158159        model.wantsSummary = true160        await model.requestTranscription()161        #expect(model.transcription == .pending)162        #expect(model.block.transcriptStatus == .pending)163        await model.waitUntilSettled()164        #expect(model.transcription == .done)165        #expect(model.segments.map(\.t) == [0, 4.4])166        #expect(model.block.transcriptStatus == .done)167        #expect(model.block.transcriptEngine == "openai:whisper-1")168        #expect(model.summary?.decisions == ["Ils retiennent le taux fixe de 5 ans."])169        #expect(model.summary?.model == "gpt-5.4-mini")170        #expect(model.summary?.at == "2026-11-19T18:22:36.405Z")171        #expect(changes.value.last?.attachmentId == id)172        #expect(changes.value.last?.transcript.count == 2)173        #expect(world.fake.count("POST", "^/api/audio/") == 1)174        // The node the editor saves round-trips through the typed view.175        #expect(AudioBlock(storage: changes.value.last!.node()).summary?.tasks.count == 1)176    }177178    @Test("microphone refused: nothing starts and the denial is explained; a recorder that cannot start shows a failure")179    func permission() async throws {180        let world = try await FeatureWorld(samples: false)181        let recorder = FakeRecorder()182        recorder.grants = false183        let model = make(world, recorder: recorder)184        await model.startRecording()185        #expect(model.stage == .empty)186        #expect(model.permissionDenied)187        #expect(recorder.log == ["permission"])188189        recorder.grants = true190        recorder.failsToStart = true191        await model.startRecording()192        #expect(!model.permissionDenied)193        #expect(model.stage == .empty)194        #expect(model.actionFailure != nil)195    }196197    @Test("offline: the recording is kept on the device, the failure is calm, « réessayer » uploads it later; discarding returns to empty")198    func offlineUpload() async throws {199        let world = try await FeatureWorld()200        let recorder = FakeRecorder(), changes = Box<[AudioBlock]>([])201        let model = make(world, recorder: recorder, changes: changes)202        world.setOffline(true)203        await model.startRecording()204        recorder.currentTime = 2205        await model.stopRecording()206        #expect(model.stage == .recorded)207        #expect(model.actionFailure?.isOffline == true)208        #expect(model.block.attachmentId == nil)209        #expect(changes.value.isEmpty)210        #expect(!model.canTranscribe)211        world.setOffline(false)212        await model.store()213        #expect(model.stage == .ready)214        #expect(model.actionFailure == nil)215        #expect(changes.value.count == 1)216217        let other = make(world, recorder: FakeRecorder())218        world.setOffline(true)219        await other.startRecording()220        await other.stopRecording()221        other.discardRecording()222        #expect(other.stage == .empty)223        #expect(other.block.duration == 0)224        #expect(other.actionFailure == nil)225    }226227    @Test("offline-first hand-over: a host that adopts the file gives the attachment id without the server")228    func adoption() async throws {229        let world = try await FeatureWorld()230        world.setOffline(true)231        let changes = Box<[AudioBlock]>([])232        let model = make(world, files: FakeFiles(adoptedId: "0a0a0a0a-1111-4222-8333-444444444444"), changes: changes)233        await model.startRecording()234        await model.stopRecording()235        #expect(model.stage == .ready)236        #expect(model.block.attachmentId == "0a0a0a0a-1111-4222-8333-444444444444")237        #expect(changes.value.count == 1)238        // Transcribing still needs the server: calm failure, state unchanged.239        await model.requestTranscription()240        #expect(model.transcription == .none)241        #expect(model.actionFailure?.isOffline == true)242    }243244    @Test("transcription: server error, failed job, unsupported server, stop waiting then check")245    func transcriptionFailures() async throws {246        let world = try await FeatureWorld()247        let model = make(world, block: BlockSamples.untranscribedAudio)248        #expect(model.stage == .ready)249        #expect(model.canTranscribe)250251        world.fake.inject(.status(500), method: "POST", path: "^/api/audio/")252        await model.requestTranscription()253        #expect(model.transcription == .none)254        #expect(model.actionFailure?.kind == .server)255256        // A job that fails.257        world.fake.clearStubs()258        world.fake.stub("POST", path: "^/api/audio/[0-9a-f-]{36}/transcribe", json: BlockSamples.transcriptionPendingJSON, status: 202)259        world.fake.stub("GET", path: "^/api/audio/[0-9a-f-]{36}", json: BlockSamples.transcriptionFailedJSON)260        await model.requestTranscription()261        await model.waitUntilSettled()262        #expect(model.transcription == .failed("Le fichier audio est illisible"))263        #expect(model.block.transcriptStatus == .failed)264        #expect(model.canTranscribe)                                            // « transcrire de nouveau » is offered265266        // Stop waiting: the state stays pending, nothing else is asked; « vérifier » reads the result.267        world.fake.clearStubs()268        world.fake.stub("POST", path: "^/api/audio/[0-9a-f-]{36}/transcribe", json: BlockSamples.transcriptionPendingJSON, status: 202)269        world.fake.stub("GET", path: "^/api/audio/[0-9a-f-]{36}", json: BlockSamples.transcriptionPendingJSON)270        await model.requestTranscription(force: true)271        #expect(model.isWaiting)272        model.stopWaiting()273        #expect(!model.isWaiting)274        #expect(model.transcription == .pending)275        world.fake.clearStubs()276        world.fake.stub("GET", path: "^/api/audio/[0-9a-f-]{36}", json: BlockSamples.transcriptionDoneJSON)277        await model.checkTranscription()278        #expect(model.transcription == .done)279        #expect(model.segments.count == 2)280281        // A server without the voice routes.282        let bare = try await FeatureWorld(samples: false)283        let old = make(bare, block: BlockSamples.untranscribedAudio)284        await old.requestTranscription()285        #expect(old.transcription == .none)286        #expect(old.actionFailure != nil)287        #expect(old.actionFailure?.isOffline == false)288    }289290    @Test("a note left pending is read once at load (free) and finishes; a transcribed note needs no request")291    func loadPending() async throws {292        let world = try await FeatureWorld()293        var pending = BlockSamples.untranscribedAudio294        pending.transcriptStatus = .pending295        world.fake.clearStubs()296        world.fake.stub("GET", path: "^/api/audio/[0-9a-f-]{36}", json: BlockSamples.transcriptionDoneJSON)297        let model = make(world, block: pending)298        #expect(model.transcription == .pending)299        await model.load()300        #expect(model.transcription == .done)301        #expect(world.fake.count("POST", "^/api/audio/") == 0)302303        world.fake.clearLog()304        let done = make(world, block: BlockSamples.transcribedAudio)305        await done.load()306        #expect(done.transcription == .done)307        #expect(done.summary != nil)308        #expect(world.fake.count("GET", "^/api/audio/") == 0)309    }310311    @Test("playback: lazy load through the host's files, play / pause, seek by fraction and by transcript line, rate, end of file")312    func playback() async throws {313        let world = try await FeatureWorld()314        let player = FakePlayer()315        let model = make(world, block: BlockSamples.transcribedAudio, files: FakeFiles(), player: player)316        #expect(model.activeSegmentIndex == nil)317        await model.togglePlayback()318        #expect(player.log == ["load", "play"])319        #expect(model.isPlaying)320        #expect(model.playbackDuration == 6.87)321        player.currentTime = 5322        model.tick()323        #expect(model.position == 5)324        #expect(model.activeSegmentIndex == 1)325        await model.togglePlayback()326        #expect(!model.isPlaying)327        await model.seek(toFraction: 0.5)328        #expect(abs(model.position - 3.435) < 0.001)329        #expect(model.activeSegmentIndex == 0)330        await model.seek(toSegment: 1)331        #expect(player.currentTime == 4.4)332        #expect(model.isPlaying)                                                // a tap on a line plays from there333        await model.skip(100)334        #expect(model.position == 6.87)                                         // clamped335        model.setRate(1.5)336        #expect(player.rate == 1.5)337        model.setRate(9)338        #expect(model.rate == 2)339        player.finish()340        #expect(!model.isPlaying)341        #expect(model.position == 6.87)342        await model.togglePlayback()                                            // replay starts over343        #expect(player.currentTime == 0)344    }345346    @Test("playback is honest when the audio cannot be had: no provider, offline download, unreadable file")347    func playbackFailures() async throws {348        let world = try await FeatureWorld()349        let none = make(world, block: BlockSamples.transcribedAudio)350        await none.togglePlayback()351        #expect(!none.isPlaying)352        #expect(none.playbackFailure != nil)353354        let offline = make(world, block: BlockSamples.transcribedAudio, files: FakeFiles(failure: .offline))355        await offline.togglePlayback()356        #expect(offline.playbackFailure?.isOffline == true)357358        let player = FakePlayer()359        player.failsToLoad = true360        let broken = make(world, block: BlockSamples.transcribedAudio, files: FakeFiles(), player: player)361        await broken.togglePlayback()362        #expect(!broken.isPlaying)363        #expect(broken.playbackFailure?.kind == .other)364    }365366    @Test("title edits reach the editor once the note exists; closing during a recording keeps the file")367    func titleAndClose() async throws {368        let world = try await FeatureWorld()369        let changes = Box<[AudioBlock]>([])370        let model = make(world, block: BlockSamples.transcribedAudio, changes: changes)371        model.setTitle("  Rendez-vous banque ")372        #expect(changes.value.last?.title == "Rendez-vous banque")373        model.setTitle("Rendez-vous banque")374        #expect(changes.value.count == 1)375376        let recorder = FakeRecorder()377        let recording = make(world, recorder: recorder)378        await recording.startRecording()379        recorder.currentTime = 4380        recording.close()381        #expect(recording.stage == .recorded)382        #expect(recording.block.duration == 4)383    }384385    @Test("real AVFoundation player on a generated AAC file, muted: duration, seek, rate, play and pause")386    func realPlayer() async throws {387        let url = try BlockSamples.sineRecording(seconds: 20)   // long enough not to end during the test on a busy machine388        defer { try? FileManager.default.removeItem(at: url) }389        #expect((try FileManager.default.attributesOfItem(atPath: url.path)[.size] as? Int ?? 0) > 500)390        let player = SystemAudioPlayer()391        player.volume = 0                                                       // never audible392        let duration = try player.load(url: url)393        #expect(abs(duration - 20) < 0.3)394        player.seek(to: 1)395        #expect(abs(player.currentTime - 1) < 0.1)396        player.rate = 1.5397        player.play()398        #expect(player.isPlaying)399        // The audio thread may need a moment on a busy machine: wait for the position to advance, at most 5 s.400        for _ in 0..<100 where player.currentTime <= 1 { try await Task.sleep(for: .milliseconds(50)) }401        #expect(player.currentTime > 1)402        player.pause()403        #expect(!player.isPlaying)404        player.seek(to: 99)405        #expect(player.currentTime <= duration)406        #expect(throws: AudioEngineError.self) { try SystemAudioPlayer().load(url: URL(fileURLWithPath: "/tmp/spn-does-not-exist.m4a")) }407        // The recorder's settings describe an AAC mono file (the recorder itself is not started: no microphone prompt in tests).408        #expect(SystemAudioRecorder.settings[AVFormatIDKey] as? Int == Int(kAudioFormatMPEG4AAC))409        #expect(SystemAudioRecorder.settings[AVNumberOfChannelsKey] as? Int == 1)410    }411}412
Files main
Menu

Theme