Skip to content

VoiceStudio is the open-source, fully-local ElevenLabs alternative — voice cloning, voice design, video dubbing, dictation, transcription & audiobook creation in 646 languages.

27.8 KB
Raw Blame
import Foundation
import Testing
@preconcurrency import AVFoundation
@testable import VoiceStudioKit

private func fixtureRoot() throws -> URL {
    let root = FileManager.default.temporaryDirectory.appendingPathComponent("phone-workflow-"+UUID().uuidString)
    try PrivateFiles.directory(root); return root
}

@Test func phoneVideoMuxPreservesOrientationAndMarksPlayableAudio() async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let source = root.appendingPathComponent("source.mp4")
    let writer = try AVAssetWriter(outputURL: source, fileType: .mp4)
    let input = AVAssetWriterInput(mediaType: .video, outputSettings: [
        AVVideoCodecKey: AVVideoCodecType.h264, AVVideoWidthKey: 64, AVVideoHeightKey: 64
    ])
    input.transform = CGAffineTransform(rotationAngle: .pi / 2)
    let adapter = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: input, sourcePixelBufferAttributes: [
        kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
        kCVPixelBufferWidthKey as String: 64, kCVPixelBufferHeightKey as String: 64
    ])
    try #require(writer.canAdd(input)); writer.add(input)
    try #require(writer.startWriting()); writer.startSession(atSourceTime: .zero)
    defer { if writer.status == .writing { writer.cancelWriting() } }
    for frame in 0..<30 {
        let deadline = ContinuousClock.now + .seconds(10)
        while !input.isReadyForMoreMediaData && ContinuousClock.now < deadline {
            try await Task.sleep(for: .milliseconds(5))
        }
        try #require(input.isReadyForMoreMediaData)
        var buffer: CVPixelBuffer?
        try #require(CVPixelBufferCreate(kCFAllocatorDefault, 64, 64, kCVPixelFormatType_32BGRA, nil, &buffer) == kCVReturnSuccess)
        let pixels = try #require(buffer)
        CVPixelBufferLockBaseAddress(pixels, [])
        memset(CVPixelBufferGetBaseAddress(pixels), 0, CVPixelBufferGetDataSize(pixels))
        CVPixelBufferUnlockBaseAddress(pixels, [])
        try #require(adapter.append(pixels, withPresentationTime: CMTime(value: Int64(frame), timescale: 30)))
    }
    input.markAsFinished(); writer.endSession(atSourceTime: CMTime(seconds: 1, preferredTimescale: 30))
    await writer.finishWriting(); try #require(writer.status == .completed)
    let wave = root.appendingPathComponent("marked.wav")
    let samples: [Float] = (0..<24000).map { frame in
        let phase = Double(frame) * 2.0 * Double.pi * 440.0 / 24000.0
        return Float(sin(phase) * 0.3)
    }
    try LocalAudio(samples: samples, sampleRate: 24000).markSynthetic().write(to: wave)
    let result = try await AVPhoneDubMedia().finish(source: source, markedWave: wave, output: root.appendingPathComponent("dub"), audioOnly: false, progress: { _ in })
    let asset = AVURLAsset(url: result)
    let video = try #require(try await asset.loadTracks(withMediaType: .video).first)
    #expect(try await asset.loadTracks(withMediaType: .audio).count == 1)
    let transform = try await video.load(.preferredTransform)
    #expect(abs(transform.b - 1) < 0.001 && abs(transform.c + 1) < 0.001)
    #expect(abs(try await asset.load(.duration).seconds - 1) < 0.1)
    let metadata = try await asset.load(.commonMetadata)
    var marked = false
    for item in metadata where item.commonKey == .commonKeyDescription {
        if try await item.load(.stringValue) == "VoiceStudio synthetic audio" { marked = true }
    }
    #expect(marked)
    let decoded = try await AVPhoneAudioDecoder().decode(source: result, output: root.appendingPathComponent("decoded.pcm"), maxSeconds: 5, progress: { _ in })
    #expect(try decoded.frames >= 21600)
    #expect(try decoded.read(start: 0, count: min(24000, decoded.frames)).samples.contains { abs($0) > 0.01 })
}
private func fixtureModel(_ capability: ModelCapability) throws -> PhoneModel {
    try ModelCatalogue.decode(Data(contentsOf: repository.appendingPathComponent("mobile/contract/models.json"))).models.first { $0.capability == capability }!
}
private struct FixtureDecoder: PhoneAudioDecoder {
    var frames = 24000
    func decode(source: URL,output: URL,maxSeconds: Double,progress: @escaping @Sendable (Double) async -> Void) async throws -> PCMFile {
        try Task.checkCancellation()
        try PCMFile.write(PCMChunk(samples: (0..<frames).map { Float(sin(Double($0)*0.1)*0.3) },sampleRate: 24000),to: output)
        await progress(1); return PCMFile(output)
    }
}
private actor FixtureEngine: LocalEngine {
    var transcript = "A reference voice"
    private(set) var transcriptions = 0
    private(set) var translations = 0
    private(set) var clones = 0
    private var failClone = 0
    private var longSegment = false
    private(set) var references: [(frames: Int, text: String)] = []
    func useLongSegment() { longSegment = true }
    func failOnClone(_ count: Int) { failClone = count }
    func empty() { transcript = "" }
    func synthesize(model: PhoneModel,text: String,speaker: Int,speed: Float,progress: @escaping EngineProgressHandler) async throws -> LocalAudio { LocalAudio(samples: Array(repeating: 0.25,count: 24000),sampleRate: 24000) }
    func clone(model: PhoneModel,text: String,reference: PCMChunk,referenceText: String,progress: @escaping EngineProgressHandler) async throws -> LocalAudio {
        clones += 1
        if clones == failClone { throw CancellationError() }
        #expect(reference.sampleRate == 24000); #expect(!referenceText.isEmpty)
        references.append((reference.samples.count, referenceText))
        return LocalAudio(samples: Array(repeating: 0.4,count: 12000),sampleRate: 24000)
    }
    func transcribe(model: PhoneModel,audio: PCMChunk,progress: @escaping EngineProgressHandler) async throws -> String { try Task.checkCancellation(); transcriptions += 1; return transcript }
    func transcribeSegments(model: PhoneModel,audio: PCMChunk,progress: @escaping EngineProgressHandler) async throws -> [SpeechSegment] {
        transcriptions += 1
        if longSegment { return [SpeechSegment(start: 0.5,end: 11.5,text: "Hello long")] }
        return [SpeechSegment(start: 0.1,end: 0.35,text: "Hello"), SpeechSegment(start: 0.5,end: 0.85,text: "World")]
    }
    func streamTranscribe(model: PhoneModel,chunks: AsyncThrowingStream<PCMChunk,any Error>,progress: @escaping EngineProgressHandler) async throws -> AsyncThrowingStream<TranscriptUpdate,any Error> { AsyncThrowingStream { $0.finish() } }
    func translate(model: PhoneModel,text: String,sourceLanguage: String,targetLanguage: String,progress: @escaping EngineProgressHandler) async throws -> String { translations += 1; return "Bonjour" }
}
@Test func phoneVoiceConsentTranscriptPersistenceRenameAndDelete() async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let store = try PhoneVoiceStore(root: root), engine = FixtureEngine(), asr = try fixtureModel(.asr)
    await #expect(throws: (any Error).self) { try await store.create(name: "Voice",source: root,consent: false,asr: asr,engine: engine,decoder: FixtureDecoder()) }
    #expect(await engine.transcriptions == 0)
    let voice = try await store.create(name: " Voice ",source: root,consent: true,asr: asr,engine: engine,decoder: FixtureDecoder())
    #expect(voice.name == "Voice"); #expect(voice.transcript == "A reference voice"); #expect(voice.sampleRate == 24000)
    #expect(voice.modelRepo == asr.repo); #expect(voice.modelRevision == asr.revision)
    #expect(try await store.reference(voice).samples.count == 24000)
    #expect(try Data(contentsOf: await store.preview(voice)).range(of: Data("synthetic".utf8)) == nil)
    let restored = try PhoneVoiceStore(root: root)
    #expect(try await restored.list() == [voice])
    try await restored.rename(voice,name: "Renamed")
    #expect(try await restored.list().first?.name == "Renamed")
    #expect(getxattr(root.path, "com.apple.metadata:com_apple_backup_excludeItem", nil, 0, 0, 0) > 0)
    try await restored.delete(voice); #expect(try await restored.list().isEmpty)
}
@Test func phoneVoiceEmptyTranscriptAndCancellationNeverPublish() async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let store = try PhoneVoiceStore(root: root), engine = FixtureEngine(), asr = try fixtureModel(.asr)
    await engine.empty()
    await #expect(throws: (any Error).self) { try await store.create(name: "Voice",source: root,consent: true,asr: asr,engine: engine,decoder: FixtureDecoder()) }
    #expect(try await store.list().isEmpty)
    let task = Task { try await store.create(name: "Voice",source: root,consent: true,asr: asr,engine: engine,decoder: FixtureDecoder()) }
    task.cancel(); await #expect(throws: CancellationError.self) { try await task.value }
    #expect(try await store.list().isEmpty)
}
@Test func phoneRecentAudioPersistsMarkedAndCanBeRemoved() async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let store = try PhoneGenerationStore(root: root)
    let tts = try fixtureModel(.tts)
    let item = try await store.save(text: "Hello",audio: LocalAudio(samples: [0.2,-0.2],sampleRate: 24000),model: tts)
    #expect(try Data(contentsOf: await store.file(item)).range(of: Data("VoiceStudio synthetic audio".utf8)) != nil)
    let restored = try PhoneGenerationStore(root: root)
    #expect(try await restored.list().first?.text == "Hello")
    #expect(try await restored.list().first?.modelRepo == tts.repo)
    #expect(try await restored.list().first?.modelRevision == tts.revision)
    try await restored.delete(item); #expect(try await restored.list().isEmpty)
}
@Test func phoneAVExtractionResamplesAndRejectsLongReference() async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let source = root.appendingPathComponent("source.wav")
    try LocalAudio(samples: (0..<48000).map { Float(sin(Double($0)*0.1)*0.25) },sampleRate: 48000).markSynthetic().write(to: source)
    let audio = try await AVPhoneAudioDecoder().decode(source: source,output: root.appendingPathComponent("reference.pcm"),maxSeconds: 30,progress: { _ in })
    #expect(abs(try audio.frames - 24000) < 100)
    #expect(try audio.read(start: 0,count: 2400).samples.contains { abs($0) > 0.1 })
    await #expect(throws: (any Error).self) { try await AVPhoneAudioDecoder().decode(source: source,output: root.appendingPathComponent("long.pcm"),maxSeconds: 0.5,progress: { _ in }) }
}
@Test(arguments: ["wav", "caf", "aiff"])
func phoneAVExtractionKeepsTheRightStereoChannel(_ ext: String) async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let source = root.appendingPathComponent("stereo").appendingPathExtension(ext)
    let format = try #require(AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 48000, channels: 2, interleaved: false))
    let buffer = try #require(AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 12000))
    buffer.frameLength = 12000
    let channels = try #require(buffer.floatChannelData)
    for frame in 0..<12000 {
        let phase = Double(frame) * 2.0 * Double.pi * 440.0 / format.sampleRate
        channels[0][frame] = 0
        channels[1][frame] = Float(sin(phase) * 0.4)
    }
    do {
        let writer = try AVAudioFile(forWriting: source, settings: format.settings)
        try writer.write(from: buffer)
    }
    let audio = try await AVPhoneAudioDecoder().decode(source: source, output: root.appendingPathComponent("mono.pcm"), maxSeconds: 1, progress: { _ in })
    let samples = try audio.read(start: 0, count: audio.frames).samples
    #expect(samples.contains { abs($0) > 0.05 })
}
@Test(.enabled(if: ProcessInfo.processInfo.environment["VOICESTUDIO_REAL_AUX"] == "1"))
func realAuxiliaryPhoneVoiceCreationAndSpeak() async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let env = ProcessInfo.processInfo.environment
    let models = try ModelStore(root: repository.appendingPathComponent("mobile/ios/.cache/inference-models"))
    let tts = try fixtureModel(.tts), asr = try fixtureModel(.asr), clone = try fixtureModel(.clone)
    for model in [tts,asr,clone] { try await models.accept(model,fingerprint: model.licence.terms_fingerprint,appVersion: "inference-test",accepted: true) }
    let engine = NativeLocalEngine(store: models,libraries: [.sherpa: try #require(env["SHERPA_ONNX_LIBRARY"]),.omni: try #require(env["OMNIVOICE_LIBRARY"])])
    let reference = try await engine.synthesize(model: tts,text: "The quick brown fox jumps over the lazy dog.")
    let source = root.appendingPathComponent("reference.wav"); try reference.markSynthetic().write(to: source)
    let voices = try PhoneVoiceStore(root: root.appendingPathComponent("voices"))
    let voice = try await voices.create(name: "Phone voice",source: source,consent: true,asr: asr,engine: engine,decoder: AVPhoneAudioDecoder())
    #expect(voice.transcript.lowercased().contains("fox"))
    let result = try await engine.clone(model: clone,text: "Hello world.",reference: voices.reference(voice),referenceText: voice.transcript)
    #expect(result.samples.count > 1000); #expect(result.samples.contains { abs($0) > 0.001 })
}

@Test func phoneTimingPadsCompressesAndMixesWithoutDuplicateSegments() throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let short = LocalAudio(samples: [0.2,0.4],sampleRate: 24000)
    #expect(try fitTiming(short,frames: 4).samples == [0.2,0.4,0,0])
    let wave = LocalAudio(samples: (0..<48000).map { Float(sin(Double($0)*0.1)*0.25) },sampleRate: 24000)
    let compressed = try fitTiming(wave,frames: 24000)
    #expect(compressed.samples.count == 24000)
    #expect(compressed.samples.allSatisfy { $0.isFinite })
    let segment = root.appendingPathComponent("segment.pcm"); try PCMFile.write(PCMChunk(samples: [0.7,0.7],sampleRate: 24000),to: segment)
    let output = root.appendingPathComponent("mix.pcm")
    for _ in 0..<2 {
        let mixed = try mixTimeline(to: output,frames: 4,segments: [(0,PCMFile(segment)),(1,PCMFile(segment))])
        #expect(try mixed.read(start: 0,count: 4).samples == [0.7,1,0.7,0])
        let wave = root.appendingPathComponent("marked.wav"); try mixed.markSynthetic(to: wave)
        #expect(try Data(contentsOf: wave).range(of: Data("VoiceStudio synthetic audio".utf8)) != nil)
    }
}
@Test func phoneDubCancellationResumesFromVerifiedSegmentsAndPersistsHistory() async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let source = root.appendingPathComponent("original.wav"); try Data("fixture".utf8).write(to: source)
    let models = try [fixtureModel(.asr),fixtureModel(.translate),fixtureModel(.clone)]
    let store = try PhoneDubStore(root: root.appendingPathComponent("dubs")), engine = FixtureEngine()
    let request = try await store.create(source: source,title: "Original.wav",targets: [PhoneDubTarget(code: "fr",name: "French"),PhoneDubTarget(code: "de",name: "German")],models: models,consent: true,audioOnly: true)
    await engine.failOnClone(2)
    let pipeline = PhoneDubPipeline(store: store,engine: engine,decoder: FixtureDecoder(),media: FixtureMedia())
    await #expect(throws: CancellationError.self) { try await pipeline.run(id: request.id,models: models) }
    let interrupted = try await store.checkpoint(request.id)
    #expect(interrupted.nextFrame == 24000); #expect(interrupted.renders.count == 1)
    #expect(interrupted.segments.allSatisfy { $0.translations["fr"] != nil })
    let calls = await engine.transcriptions
    let restored = try PhoneDubStore(root: root.appendingPathComponent("dubs"))
    let result = try await PhoneDubPipeline(store: restored,engine: engine,decoder: FixtureDecoder(),media: FixtureMedia()).run(id: request.id,models: models)
    #expect(result.done); #expect(result.outputs.map(\.language) == ["fr","de"])
    #expect(await engine.clones == 5); #expect(await engine.translations == 4)
    #expect(await engine.transcriptions == calls)
    #expect(try await restored.list().first?.request.title == "Original.wav")
    for output in result.outputs {
        let url = try await restored.file(request.id,output.file)
        #expect(try ModelHash.file(url) == output.hash)
        #expect(try Data(contentsOf: url).range(of: Data("VoiceStudio synthetic audio".utf8)) != nil)
    }
    let clones = await engine.clones
    _ = try await pipeline.run(id: request.id,models: models)
    #expect(await engine.clones == clones)
    let output = try await store.file(request.id,result.outputs[0].file)
    try Data("corrupt".utf8).write(to: output)
    await #expect(throws: (any Error).self) { try await pipeline.run(id: request.id,models: models) }
    try await restored.delete(request.id); #expect(try await restored.list().isEmpty)
}
private struct FixtureMedia: PhoneDubMedia {
    func finish(source: URL,markedWave: URL,output: URL,audioOnly: Bool,progress: @escaping @Sendable (Double) async -> Void) async throws -> URL {
        let result = output.appendingPathExtension("wav")
        try? FileManager.default.removeItem(at: result); try FileManager.default.copyItem(at: markedWave,to: result)
        return result
    }
}
@Test func phoneDubConsentPinsAndLicenceWithdrawalBlockBeforeInference() async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let source = root.appendingPathComponent("source.wav"); try Data("fixture".utf8).write(to: source)
    let models = try [fixtureModel(.asr),fixtureModel(.translate),fixtureModel(.clone)]
    let store = try PhoneDubStore(root: root.appendingPathComponent("dubs")), engine = FixtureEngine()
    await #expect(throws: (any Error).self) { try await store.create(source: source,title: "Source",targets: [PhoneDubTarget(code: "fr",name: "French")],models: models,consent: false) }
    #expect(try await store.list().isEmpty)
    let request = try await store.create(source: source,title: "Source",targets: [PhoneDubTarget(code: "fr",name: "French")],models: models,consent: true)
    let blocked = PhoneDubPipeline(store: store,engine: engine,decoder: FixtureDecoder(),media: FixtureMedia(),authorize: { throw ClientError("mobile.local.licence_required") })
    await #expect(throws: (any Error).self) { try await blocked.run(id: request.id,models: models) }
    #expect(await engine.transcriptions == 0)
    let pipeline = PhoneDubPipeline(store: store,engine: engine,decoder: FixtureDecoder(),media: FixtureMedia())
    await #expect(throws: (any Error).self) { try await pipeline.run(id: request.id,models: Array(models.prefix(2))) }
    #expect(await engine.transcriptions == 0)
    // The per-job lock makes a second run or a delete wait for the holder instead of failing.
    try await store.acquire(request.id)
    let waitingRun = Task { try await pipeline.run(id: request.id,models: models) }
    let waitingDelete = Task { try await store.delete(request.id) }
    try await Task.sleep(nanoseconds: 100_000_000)
    #expect(await engine.transcriptions == 0)
    await store.release(request.id)
    _ = try? await waitingRun.value
    _ = try? await waitingDelete.value
    await #expect(throws: (any Error).self) { try await store.file(request.id,"../outside") }
}
@Test(.enabled(if: ProcessInfo.processInfo.environment["VOICESTUDIO_REAL_AUX"] == "1"))
func realAuxiliaryPhoneDubOfflineAndResume() async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let env = ProcessInfo.processInfo.environment
    let models = try [fixtureModel(.asr),fixtureModel(.translate),fixtureModel(.clone)]
    let tts = try fixtureModel(.tts)
    let modelStore = try ModelStore(root: repository.appendingPathComponent("mobile/ios/.cache/inference-models"))
    for model in models+[tts] { try await modelStore.accept(model,fingerprint: model.licence.terms_fingerprint,appVersion: "inference-test",accepted: true) }
    let engine = NativeLocalEngine(store: modelStore,libraries: [.sherpa: try #require(env["SHERPA_ONNX_LIBRARY"]),.omni: try #require(env["OMNIVOICE_LIBRARY"]),.llama: try #require(env["LLAMA_LIBRARY"])])
    let reference = try await engine.synthesize(model: tts,text: "Hello, how are you?")
    let source = root.appendingPathComponent("source.wav"); try reference.markSynthetic().write(to: source)
    let store = try PhoneDubStore(root: root.appendingPathComponent("dubs"))
    let request = try await store.create(source: source,title: "Source.wav",targets: [PhoneDubTarget(code: "fr",name: "French")],models: models,consent: true,audioOnly: true)
    let pipeline = PhoneDubPipeline(store: store,engine: engine,authorize: { for model in models { try await modelStore.checkPermission(model) } })
    let result = try await pipeline.run(id: request.id,models: models)
    #expect(result.done); #expect(result.outputs.count == 1)
    let translated = result.segments.compactMap { $0.translations["fr"] }.joined(separator: " ").lowercased()
    print("Phone dub native segments:", result.segments.map { $0.speech.text })
    print("Phone dub native translation:", translated)
    // Must be French, not the source echoed back (the prompt once named the source "auto").
    let spoken = result.segments.map { $0.speech.text }.joined(separator: " ").lowercased()
    let words = Set(translated.split { !$0.isLetter && $0 != "'" }.map(String.init))
    #expect(translated != spoken && !words.isDisjoint(with: ["le", "la", "les", "des", "du", "un", "une", "et", "est", "par", "pour", "vous", "comment", "toi"]))
    let wave = try await store.file(request.id,result.outputs[0].file)
    let decoded = try await AVPhoneAudioDecoder().decode(source: wave,output: root.appendingPathComponent("result.pcm"),maxSeconds: 30,progress: { _ in })
    #expect(try decoded.frames > 1000)
    #expect(try decoded.read(start: 0,count: min(720000,decoded.frames)).samples.contains { abs($0) > 0.001 })
    let resumed = try await pipeline.run(id: request.id,models: models)
    #expect(resumed.done); #expect(resumed.outputs[0].hash == result.outputs[0].hash)
}

@Test func phoneMediaRejectsRemoteSourcesBeforeOpeningAssets() async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let remote = URL(string: "https://example.invalid/media.wav")!
    await #expect(throws: (any Error).self) { try await AVPhoneAudioDecoder().decode(source: remote,output: root.appendingPathComponent("out.pcm"),maxSeconds: 30,progress: { _ in }) }
    await #expect(throws: (any Error).self) { try await AVPhoneDubMedia().finish(source: remote,markedWave: root.appendingPathComponent("marked.wav"),output: root.appendingPathComponent("out"),audioOnly: true,progress: { _ in }) }
}

@Test func phoneLibrariesRemoveUnpublishedWorkAfterInterruption() async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    for metadata in ["voice.json","request.json"] {
        let library = root.appendingPathComponent(metadata)
        let pending = library.appendingPathComponent(UUID().uuidString)
        try PrivateFiles.directory(pending); try Data("partial".utf8).write(to: pending.appendingPathComponent("reference.pcm"))
        if metadata == "voice.json" { _ = try PhoneVoiceStore(root: library) } else { _ = try PhoneDubStore(root: library) }
        #expect(!FileManager.default.fileExists(atPath: pending.path))
    }
}

@Test func reviewBlankDubReferenceFallsBackAndRepairsSavedBlank() async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let source = root.appendingPathComponent("source.wav"); try Data("fixture".utf8).write(to: source)
    let models = try [fixtureModel(.asr),fixtureModel(.translate),fixtureModel(.clone)]
    let store = try PhoneDubStore(root: root.appendingPathComponent("dubs")), engine = FixtureEngine()
    await engine.empty(); await engine.failOnClone(2)
    let request = try await store.create(source: source,title: "Source",targets: [PhoneDubTarget(code: "fr",name: "French")],models: models,consent: true,audioOnly: true)
    let pipeline = PhoneDubPipeline(store: store,engine: engine,decoder: FixtureDecoder(),media: FixtureMedia())
    await #expect(throws: CancellationError.self) { try await pipeline.run(id: request.id,models: models) }
    var checkpoint = try await store.checkpoint(request.id)
    #expect(checkpoint.segments.first?.referenceText == "Hello")
    if checkpoint.segments.count > 1 { checkpoint.segments[1].referenceText = " \n"; try await store.save(request.id,checkpoint) }
    let result = try await pipeline.run(id: request.id,models: models)
    #expect(result.done)
    #expect(result.segments.map(\.referenceText) == ["Hello","World"])
}

@Test func blankRecognitionOfALongSegmentUsesTheWholeSegmentWithItsOwnText() async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let source = root.appendingPathComponent("source.wav"); try Data("fixture".utf8).write(to: source)
    let models = try [fixtureModel(.asr),fixtureModel(.translate),fixtureModel(.clone)]
    let store = try PhoneDubStore(root: root.appendingPathComponent("dubs")), engine = FixtureEngine()
    await engine.empty(); await engine.useLongSegment(); await engine.failOnClone(1)
    let request = try await store.create(source: source,title: "Source",targets: [PhoneDubTarget(code: "fr",name: "French")],models: models,consent: true,audioOnly: true)
    let pipeline = PhoneDubPipeline(store: store,engine: engine,decoder: FixtureDecoder(frames: 24000 * 12),media: FixtureMedia())
    await #expect(throws: CancellationError.self) { try await pipeline.run(id: request.id,models: models) }
    // The resumed job must pair the same audio with the same words as the interrupted one would have.
    #expect(try await pipeline.run(id: request.id,models: models).done)
    let references = await engine.references
    #expect(references.count == 1)
    #expect(references.first?.text == "Hello long")
    #expect(references.first?.frames == 11 * 24000)
}

@Test func reviewCorruptRecordsDoNotHideHealthyLibraries() async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let voicesRoot = root.appendingPathComponent("voices"), dubsRoot = root.appendingPathComponent("dubs")
    let voices = try PhoneVoiceStore(root: voicesRoot), dubs = try PhoneDubStore(root: dubsRoot)
    let recentRoot = root.appendingPathComponent("recent"), recent = try PhoneGenerationStore(root: recentRoot)
    let voice = try await voices.create(name: "Voice",source: root,consent: true,asr: fixtureModel(.asr),engine: FixtureEngine(),decoder: FixtureDecoder())
    let source = root.appendingPathComponent("source.wav"); try Data("fixture".utf8).write(to: source)
    let request = try await dubs.create(source: source,title: "Source",targets: [PhoneDubTarget(code: "fr",name: "French")],models: [fixtureModel(.asr),fixtureModel(.translate),fixtureModel(.clone)],consent: true)
    let generation = try await recent.save(text: "Hello",audio: LocalAudio(samples: [0.2],sampleRate: 24000))
    for (directory,metadata) in [(voicesRoot,"voice.json"),(dubsRoot,"request.json")] {
        let bad = directory.appendingPathComponent(UUID().uuidString); try PrivateFiles.directory(bad)
        try Data("broken".utf8).write(to: bad.appendingPathComponent(metadata))
    }
    try Data("broken".utf8).write(to: recentRoot.appendingPathComponent("broken.json"))
    #expect(try await voices.list() == [voice])
    #expect(try await dubs.list().map(\.id) == [request.id])
    #expect(try await recent.list().map(\.id) == [generation.id])
}

@Test func reviewReferenceDecodeCapsAcceptedDurationTolerance() async throws {
    let root = try fixtureRoot(); defer { try? FileManager.default.removeItem(at: root) }
    let source = root.appendingPathComponent("recorded.wav")
    try LocalAudio(samples: Array(repeating: 0.2,count: 720120),sampleRate: 24000).markSynthetic().write(to: source)
    let store = try PhoneVoiceStore(root: root.appendingPathComponent("voices"))
    let voice = try await store.create(name: "Voice",source: source,consent: true,asr: fixtureModel(.asr),engine: FixtureEngine(),decoder: AVPhoneAudioDecoder())
    #expect(try await store.reference(voice).samples.count == 720000)
}