SPB Git

Repository Public

TranscriptionSyncTests.swift 21.1 KB 325 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : SP Lecture Studio9// File             : Tests/Unit/TranscriptionSyncTests.swift10// Description      : Unit tests of chunk planning, word grouping, phrase11//                    matching, the remote transcription client, encrypted12//                    bundles and tus uploads.13// Module           : Unit14// Repository       : https://git.spboucher.ai/sp-lecture-studio15// Created          : 2026-11-1816// Last Modified    : 2026-11-1817//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author           : Simon-Pierre Boucher23// Position         : Professor of Finance24// Institution      : Université du Québec en Outaouais (UQO)25// Department       : Département des sciences administratives26// Organization     : SP Boucher27//28// Website          : https://spboucher.ai29// Git              : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General          : info@spboucher.ai36// Professional     : contact@spboucher.ai37// Direct           : simon@spboucher.ai38// Research         : research@spboucher.ai39// Development      : dev@spboucher.ai40// Support          : support@spboucher.ai41// Security/Admin   : admin@spboucher.ai42// Notifications    : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification   : Public69// Distribution     : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License          : None specified (no license file in this repository)83// Maintainer       : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import XCTest91@testable import LectureCore92@testable import LectureTranscription93@testable import LectureSync94import LectureStorage95import LectureTesting9697/// In-process HTTP stub: requests made through a session configured with this protocol never touch the network.98final class StubProtocol: URLProtocol {99    nonisolated(unsafe) static var handler: ((URLRequest, Data) throws -> (Int, [String: String], Data))?100    static func session() -> URLSession { let c = URLSessionConfiguration.ephemeral; c.protocolClasses = [StubProtocol.self]; return URLSession(configuration: c) }101    override class func canInit(with request: URLRequest) -> Bool { true }102    override class func canonicalRequest(for request: URLRequest) -> URLRequest { request }103    override func startLoading() {104        var body = request.httpBody ?? Data()105        if let stream = request.httpBodyStream {106            stream.open(); var buf = [UInt8](repeating: 0, count: 65_536)107            while stream.hasBytesAvailable { let n = stream.read(&buf, maxLength: buf.count); if n <= 0 { break }; body.append(buf, count: n) }108            stream.close()109        }110        do {111            let (code, headers, data) = try Self.handler!(request, body)112            client?.urlProtocol(self, didReceive: HTTPURLResponse(url: request.url!, statusCode: code, httpVersion: "HTTP/1.1", headerFields: headers)!, cacheStoragePolicy: .notAllowed)113            client?.urlProtocol(self, didLoad: data); client?.urlProtocolDidFinishLoading(self)114        } catch { client?.urlProtocol(self, didFailWithError: error) }115    }116    override func stopLoading() {}117}118119final class TranscriptionTests: XCTestCase {120    func testChunkPlannerCutsAtSilence() {121        var env = Envelope()122        env.rms = [Float](repeating: -20, count: 12_000); env.peak = [Float](repeating: 0.2, count: 12_000)      // 120 s of speech…123        for i in 4_180..<4_240 { env.rms[i] = -65 }                                                               // …with a pause at 41.8–42.4 s124        for i in 9_300..<9_360 { env.rms[i] = -65 }                                                               // and at 93.0–93.6 s125        let plan = ChunkPlanner.plan(duration: T(120), envelope: env)126        XCTAssertEqual(plan.count, 3)127        XCTAssertEqual(plan[0].end.seconds, 42.1, accuracy: 0.25); XCTAssertEqual(plan[1].end.seconds, 93.3, accuracy: 0.25)128        XCTAssertEqual(plan.first?.start, .zero); XCTAssertEqual(plan.last?.end, T(120))129        for (a, b) in zip(plan, plan.dropFirst()) { XCTAssertEqual(a.end, b.start) }130        XCTAssertTrue(plan.allSatisfy { $0.duration.seconds <= 55.01 })131        XCTAssertEqual(ChunkPlanner.plan(duration: T(30), envelope: nil), [TimeRange(seconds: 0, 30)])132    }133134    func testWordGroupingAndAssembly() {135        var words: [RecognizedWord] = []136        var t = 0.0137        for w in "Bonjour à tous. Aujourd'hui nous voyons la méthode du coût".split(separator: " ") { words.append(RecognizedWord(text: String(w), start: t, duration: 0.3, confidence: 0.9)); t += 0.35 }138        t += 1.5                                                                                                   // long pause139        for w in "Prenons un exemple".split(separator: " ") { words.append(RecognizedWord(text: String(w), start: t, duration: 0.3, confidence: 0.8)); t += 0.35 }140        let segs = TranscriptAssembler.group(words, offset: T(100))141        XCTAssertEqual(segs.map(\.text), ["Bonjour à tous. Aujourd'hui nous voyons la méthode du coût", "Prenons un exemple"])142        XCTAssertEqual(segs[0].range.start, T(100)); XCTAssertEqual(segs[1].range.start.seconds, 100 + 10 * 0.35 + 1.5, accuracy: 0.001)143        let tr = TranscriptAssembler.assemble(chunks: [(TimeRange(seconds: 50, 100), [TranscriptSegment(range: TimeRange(seconds: 1, 3), text: "B")]),144                                                       (TimeRange(seconds: 0, 50), [TranscriptSegment(range: TimeRange(seconds: 48, 60), text: "A"), TranscriptSegment(range: TimeRange(seconds: 2, 3), text: " ")])],145                                              locale: "fr-CA", provider: "x")146        XCTAssertEqual(tr.segments.map(\.text), ["A", "B"])147        XCTAssertEqual(tr.segments[0].range, TimeRange(seconds: 48, 50))                                         // clipped to its chunk148        XCTAssertEqual(tr.segments[1].range, TimeRange(seconds: 51, 53))149    }150151    func testPhraseMatcherFiresOncePerUtterance() {152        var m = PhraseMatcher(phrase: "Reprendre")153        XCTAssertFalse(m.feed("donc la valeur"))154        XCTAssertTrue(m.feed("donc la valeur euh reprendre"))155        XCTAssertFalse(m.feed("donc la valeur euh reprendre donc"))                                                // same occurrence, growing partial result156        XCTAssertTrue(m.feed("donc la valeur euh reprendre donc la valeur REPRENDRE,"))                            // said again157        XCTAssertFalse(m.feed("nouvelle phrase"))                                                                  // new recognition task158        XCTAssertTrue(m.feed("nouvelle phrase reprendre"))159        var end = PhraseMatcher(phrase: "fin de l'enregistrement")160        XCTAssertFalse(end.feed("c'est la fin de la séance")); XCTAssertTrue(end.feed("c'est la fin de l’enregistrement merci"))161    }162163    func testRemoteProviderRequestRetryAndErrors() async throws {164        let chunk = AudioChunk(range: TimeRange(seconds: 0, 2), samples: [Float](repeating: 0.1, count: 32_000), sampleRate: 16_000)165        nonisolated(unsafe) var calls = 0166        nonisolated(unsafe) var seen: (auth: String?, body: Data)?167        StubProtocol.handler = { req, body in168            calls += 1; seen = (req.value(forHTTPHeaderField: "Authorization"), body)169            if calls == 1 { return (503, [:], Data("busy".utf8)) }170            return (200, ["Content-Type": "application/json"], Data(#"{"text":"Bonjour à tous","segments":[{"start":0.2,"end":1.4,"text":" Bonjour à tous"}]}"#.utf8))171        }172        let p = RemoteTranscriptionProvider(configuration: .init(endpoint: "https://stt.example.test/v1/audio/transcriptions", model: "whisper-large"), token: "secret", session: StubProtocol.session())173        let avail = await p.availability(locale: Locale(identifier: "fr-CA")); XCTAssertEqual(avail, .available)174        let segs = try await p.transcribe(chunk, locale: Locale(identifier: "fr-CA"))175        XCTAssertEqual(calls, 2)                                                                                   // one retry after the 503176        XCTAssertEqual(segs.map(\.text), ["Bonjour à tous"]); XCTAssertEqual(segs[0].range, TimeRange(seconds: 0.2, 1.4))177        XCTAssertEqual(seen?.auth, "Bearer secret")178        let body = String(decoding: try XCTUnwrap(seen?.body).prefix(600), as: UTF8.self)179        XCTAssertTrue(body.contains("name=\"model\"\r\n\r\nwhisper-large")); XCTAssertTrue(body.contains("name=\"language\"\r\n\r\nfr")); XCTAssertTrue(body.contains("RIFF"))180181        calls = 0182        StubProtocol.handler = { _, _ in calls += 1; return (401, [:], Data("bad token".utf8)) }183        do { _ = try await p.transcribe(chunk, locale: Locale(identifier: "fr")); XCTFail() } catch { XCTAssertEqual(error as? TranscriptionError, .server(401, "bad token")) }184        XCTAssertEqual(calls, 1)                                                                                   // an authentication error is not retried185186        // Audio is never sent over plain HTTP to a remote host, and an empty configuration explains itself.187        let insecure = RemoteTranscriptionProvider(configuration: .init(endpoint: "http://stt.example.test/v1/audio/transcriptions"), token: nil, session: StubProtocol.session())188        let a1 = await insecure.availability(locale: .current); XCTAssertEqual(a1, .unavailable("insecureEndpoint"))189        do { _ = try await insecure.transcribe(chunk, locale: .current); XCTFail() } catch { XCTAssertEqual(error as? TranscriptionError, .unavailable("insecureEndpoint")) }190        let a2 = await RemoteTranscriptionProvider(configuration: .init(), token: nil).availability(locale: .current); XCTAssertEqual(a2, .unavailable("notConfigured"))191    }192193    /// The project transcriber places provider results on the source timeline (the provider here is a deterministic test double).194    func testProjectTranscriberPlacesChunksOnTheTimeline() async throws {195        struct Echo: TranscriptionProvider {196            let id = "test.echo"; let sendsAudioOffDevice = false197            func availability(locale: Locale) async -> ProviderAvailability { .available }198            func transcribe(_ chunk: AudioChunk, locale: Locale) async throws -> [TranscriptSegment] {199                [TranscriptSegment(range: TimeRange(seconds: 1, 2), text: "chunk@\(Int(chunk.range.start.seconds.rounded()))")]200            }201        }202        let dir = Fixtures.tempDirectory(); defer { try? FileManager.default.removeItem(at: dir) }203        var plan = Fixtures.AudioPlan(duration: 130); plan.tones = [(0...60, 300, 0.2), (115...130, 300, 0.2)]     // 60–115 s is digital silence204        let p = try Fixtures.makeProject(in: dir, audio: plan) { _ in }205        let t = try await ProjectTranscriber.transcribe(package: p.package, manifest: p.manifest, provider: Echo(), locale: Locale(identifier: "fr-CA"))206        XCTAssertEqual(t.provider, "test.echo"); XCTAssertEqual(t.locale, "fr-CA")207        XCTAssertEqual(t.segments.first?.range, TimeRange(seconds: 1, 2))208        XCTAssertTrue(t.segments.allSatisfy { $0.range.start.seconds == Double($0.text.dropFirst(6))! + 1 })       // each segment sits at chunk start + 1 s209        XCTAssertGreaterThanOrEqual(t.segments.count, 2)210        struct Off: TranscriptionProvider {211            let id = "off"; let sendsAudioOffDevice = false212            func availability(locale: Locale) async -> ProviderAvailability { .unavailable("modelNotInstalled") }213            func transcribe(_ chunk: AudioChunk, locale: Locale) async throws -> [TranscriptSegment] { [] }214        }215        do { _ = try await ProjectTranscriber.transcribe(package: p.package, manifest: p.manifest, provider: Off(), locale: .current); XCTFail() }216        catch { XCTAssertEqual(error as? TranscriptionError, .unavailable("modelNotInstalled")) }                 // explains the prerequisite instead of failing silently217    }218}219220final class SyncTests: XCTestCase {221    var dir: URL!222    override func setUp() { dir = Fixtures.tempDirectory("sync") }223    override func tearDown() { try? FileManager.default.removeItem(at: dir); StubProtocol.handler = nil }224225    func payload(_ size: Int) -> URL {226        var d = Data(count: size); d.withUnsafeMutableBytes { p in for i in 0..<p.count { p[i] = UInt8(truncatingIfNeeded: i &* 7 &+ i >> 9) } }227        let u = dir.appendingPathComponent("payload-\(size).bin"); try! d.write(to: u); return u228    }229230    func testEncryptedBundleRoundTripAndTamperDetection() throws {231        let src = payload(2_500_000), enc = dir.appendingPathComponent("b.slsb"), dec = dir.appendingPathComponent("b.out")232        try SecureBundle.encrypt(src, to: enc, passphrase: "pêche-Maloney-42", iterations: 10_000)233        let cipher = try Data(contentsOf: enc)234        XCTAssertNil(cipher.range(of: try Data(contentsOf: src).prefix(64)))                                       // not stored in clear235        try SecureBundle.decrypt(enc, to: dec, passphrase: "pêche-Maloney-42")236        XCTAssertEqual(try Hashing.sha256(of: dec), try Hashing.sha256(of: src))237        XCTAssertThrowsError(try SecureBundle.decrypt(enc, to: dec, passphrase: "mauvais")) { XCTAssertEqual($0 as? SecureBundleError, .wrongPassphraseOrCorrupt) }238        XCTAssertFalse(FileManager.default.fileExists(atPath: dec.path))                                           // no partial plaintext left behind239        var flipped = cipher; flipped[cipher.count / 2] ^= 1; try flipped.write(to: enc)240        XCTAssertThrowsError(try SecureBundle.decrypt(enc, to: dec, passphrase: "pêche-Maloney-42"))241        try cipher.prefix(1_200_000).write(to: enc)                                                                // truncated in the middle242        XCTAssertThrowsError(try SecureBundle.decrypt(enc, to: dec, passphrase: "pêche-Maloney-42"))243        // Truncated exactly at a chunk boundary: still detected, because the last chunk is authenticated as last.244        let firstChunkEnd = 37 + 4 + (1 << 20) + 16245        try cipher.prefix(firstChunkEnd).write(to: enc)246        XCTAssertThrowsError(try SecureBundle.decrypt(enc, to: dec, passphrase: "pêche-Maloney-42")) { XCTAssertEqual($0 as? SecureBundleError, .truncated) }247        let code = SecureBundle.generatePairingCode()248        XCTAssertEqual(code.count, 29); XCTAssertNotEqual(code, SecureBundle.generatePairingCode())249    }250251    /// Minimal in-memory tus 1.0 server.252    final class TusServer: @unchecked Sendable {253        var store: [String: (length: Int64, data: Data)] = [:]254        var failNextPatchAfter: Int?        // bytes accepted before the connection "drops"255        var patches = 0256        func handle(_ req: URLRequest, _ body: Data) throws -> (Int, [String: String], Data) {257            guard req.value(forHTTPHeaderField: "Authorization") == "Bearer tok" else { return (401, [:], Data()) }258            let path = req.url!.path259            switch req.httpMethod {260            case "POST":261                let id = "u\(store.count + 1)"262                store[id] = (Int64(req.value(forHTTPHeaderField: "Upload-Length")!)!, Data())263                return (201, ["Location": "/files/\(id)", "Tus-Resumable": "1.0.0"], Data())264            case "HEAD":265                guard let u = store[(path as NSString).lastPathComponent] else { return (404, [:], Data()) }266                return (200, ["Upload-Offset": String(u.data.count), "Upload-Length": String(u.length)], Data())267            case "PATCH":268                let id = (path as NSString).lastPathComponent269                guard var u = store[id] else { return (404, [:], Data()) }270                guard Int(req.value(forHTTPHeaderField: "Upload-Offset")!) == u.data.count else { return (409, [:], Data()) }271                patches += 1272                if let keep = failNextPatchAfter {273                    failNextPatchAfter = nil274                    u.data.append(body.prefix(keep)); store[id] = u275                    throw URLError(.networkConnectionLost)276                }277                u.data.append(body); store[id] = u278                return (204, ["Upload-Offset": String(u.data.count)], Data())279            default: return (405, [:], Data())280            }281        }282    }283284    func testResumableUploadSurvivesNetworkLossAndRelaunch() async throws {285        let file = payload(700_000)286        let server = TusServer()287        StubProtocol.handler = server.handle288        let uploader = ResumableUploader(endpoint: UploadEndpoint(url: "https://files.example.test/files/"), token: "tok", session: StubProtocol.session())289        uploader.chunkSize = 200_000290        final class Saved: @unchecked Sendable { var states: [UploadState] = [] }291        let saved = Saved()292293        // The connection drops in the middle of the second chunk; the uploader resynchronizes and finishes.294        server.failNextPatchAfter = nil295        nonisolated(unsafe) var armed = false296        StubProtocol.handler = { req, body in297            if req.httpMethod == "PATCH", server.patches == 1, !armed { armed = true; server.failNextPatchAfter = 50_000 }298            return try server.handle(req, body)299        }300        let done = try await uploader.upload(try UploadState(file: file), metadata: ["course": "IMM1003"], onState: { saved.states.append($0) })301        XCTAssertTrue(done.completed); XCTAssertEqual(done.offset, 700_000)302        XCTAssertEqual(server.store["u1"]?.data, try Data(contentsOf: file))                                       // byte-exact despite the partial chunk303        XCTAssertGreaterThanOrEqual(server.patches, 5)304305        // Relaunch: a state persisted mid-transfer continues from the server's offset instead of starting again.306        StubProtocol.handler = server.handle307        var mid = try UploadState(file: file); mid.uploadURL = "https://files.example.test/files/u2"; mid.offset = 123308        server.store["u2"] = (700_000, try Data(contentsOf: file).prefix(400_000))309        let before = server.patches310        let resumed = try await uploader.upload(mid)311        XCTAssertTrue(resumed.completed); XCTAssertEqual(server.patches - before, 2)                               // only the missing 300 000 bytes were sent312        XCTAssertEqual(server.store["u2"]?.data, try Data(contentsOf: file))313314        // The server lost the upload: start over cleanly. A modified file is refused. Plain HTTP is refused.315        var gone = try UploadState(file: file); gone.uploadURL = "https://files.example.test/files/nope"316        let restarted = try await uploader.upload(gone); XCTAssertTrue(restarted.completed); XCTAssertEqual(restarted.uploadURL, "https://files.example.test/files/u3")317        var stale = try UploadState(file: file); stale.fileSize -= 1318        do { _ = try await uploader.upload(stale); XCTFail() } catch { XCTAssertEqual(error as? UploadError, .fileChanged) }319        do { _ = try await ResumableUploader(endpoint: UploadEndpoint(url: "http://files.example.test/files/"), token: "tok", session: StubProtocol.session()).upload(try UploadState(file: file)); XCTFail() }320        catch { XCTAssertEqual(error as? UploadError, .insecureEndpoint) }321        do { _ = try await ResumableUploader(endpoint: UploadEndpoint(url: "https://files.example.test/files/"), token: "wrong", session: StubProtocol.session()).upload(try UploadState(file: file)); XCTFail() }322        catch { XCTAssertEqual(error as? UploadError, .server(401, "")) }323    }324}325
Files main
Menu

Theme