SPB Git

Repository Public

CaptureTests.swift 22.2 KB 314 lines swift

1// SP Lecture Studio — Tests2// File: Tests/Integration/CaptureTests.swift3// Description: Recording engine tests with a synthetic audio source: segments, clock, pause/pre-roll, crash recovery, auto policies, PDF-only mode.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import XCTest8import AVFoundation9@testable import LectureCore10@testable import LectureCapture11@testable import LectureStorage12import LectureRender13import LecturePDF14import LectureTesting1516final class CaptureTests: XCTestCase {17    var dir: URL!18    override func setUp() { dir = Fixtures.tempDirectory("capture") }19    override func tearDown() { try? FileManager.default.removeItem(at: dir) }2021    struct Rig { var package: ProjectPackage; var engine: RecordingEngine; var source: SyntheticAudioSource }2223    func rig(audio: Bool = true, segment: Double = 30, policy: AutoPausePolicy = AutoPausePolicy(), sampleRate: Double = 48_000) throws -> Rig {24        let pdf = Fixtures.makePDF(at: dir.appendingPathComponent("s-\(UUID().uuidString.prefix(4)).pdf"), pages: 5)25        let pkg = try ProjectPackage.create(in: dir, manifest: ProjectManifest(title: "Capture \(UUID().uuidString.prefix(4))"), pdf: pdf, pdfPageCount: 5)26        let source = SyntheticAudioSource(sampleRate: sampleRate)27        var cfg = RecordingConfiguration(); cfg.segmentSeconds = segment; cfg.policy = policy28        cfg.lowDiskBytes = 0; cfg.criticalDiskBytes = 029        let engine = RecordingEngine(package: pkg, manifest: try pkg.loadManifest(), log: try EventLogWriter(url: pkg.eventsURL), audio: audio ? source : nil, configuration: cfg)30        engine.useHostClock { source.hostTime }31        return Rig(package: pkg, engine: engine, source: source)32    }3334    func testClockIsAnchoredMonotonicAndFreezes() {35        let c = RecordingClock(at: T(100))36        XCTAssertEqual(c.now(host: 5), T(100))                                   // frozen until started37        c.start(at: T(100), host: 10)38        XCTAssertEqual(c.now(host: 12.5), T(102.5))39        c.anchor(host: 13, timeline: T(102.9))                                   // audio clock runs 100 ms slow: the clock follows audio40        XCTAssertEqual(c.now(host: 13), T(103.0 - 0.1 + 0.0).ns >= T(102.9).ns ? c.now(host: 13) : T(0))41        XCTAssertGreaterThanOrEqual(c.now(host: 13.01), T(102.5))                // never goes backwards42        let frozen = c.freeze(host: 14)43        XCTAssertEqual(c.now(host: 99), frozen)44        XCTAssertEqual(c.timeline(forHost: 50), frozen)45    }4647    func testSegmentsEventsPauseAndPreRoll() throws {48        var policy = AutoPausePolicy(); policy.preRollSeconds = 0.349        let r = try rig(segment: 30, policy: policy)50        try r.engine.start(currentPage: 0)51        r.source.push(seconds: 20)52        let stroke = Stroke(page: 0, tool: .pen, color: .red, width: 0.01, begin: r.engine.position, points: [StrokePoint(x: 0.1, y: 0.1), StrokePoint(x: 0.2, y: 0.2, dt: 500_000_000)])53        r.source.push(seconds: 0.5)54        let ev = r.engine.record(.strokeAdded(stroke))55        XCTAssertEqual(ev.t.seconds, 20.5, accuracy: 0.001)                       // events are stamped on the audio sample clock56        r.source.push(seconds: 44.5)                                              // 65 s recorded: two full segments + 5 s open57        XCTAssertEqual(try r.package.loadManifest().segments(.audio).count, 2)    // checkpoint written at each finalized segment58        r.engine.pause()59        XCTAssertEqual(r.engine.state, .paused)60        XCTAssertEqual(try r.package.loadManifest().segments(.audio).count, 3)    // pausing finalizes the open segment61        r.source.push(seconds: 10, amplitude: 0.5)                                // audio heard while paused is not recorded…62        XCTAssertEqual(r.engine.position, T(65))63        r.engine.record(.page(index: 1))                                          // …but interactions are stamped at the frozen time64        r.engine.resume()65        r.source.push(seconds: 9.7)66        let m = r.engine.stop()6768        XCTAssertEqual(m.duration.seconds, 65 + 0.3 + 9.7, accuracy: 0.0001)      // …except the 0.3 s pre-roll kept on resume69        let segs = m.segments(.audio)70        XCTAssertEqual(segs.map(\.index), [1, 2, 3, 4])71        for (a, b) in zip(segs, segs.dropFirst()) { XCTAssertEqual(a.start + a.duration, b.start) }    // contiguous, no gap, no overlap72        XCTAssertTrue(segs.allSatisfy { $0.finalized && $0.sha256 != nil }, "\(segs.map { ($0.index, $0.finalized, $0.sha256 != nil, $0.frames) })")73        for s in segs { XCTAssertEqual(MediaProbe.probe(r.package.url(s.path), kind: .audio)?.frames, s.frames) }74        XCTAssertNotNil(m.sessions.first?.endedAt)75        XCTAssertTrue(r.package.diagnose(m, deep: true).isHealthy)7677        let events = r.package.loadEvents().events78        let kinds = events.map { e -> String in79            switch e.e { case .sessionStart: "start"; case .page: "page"; case .strokeAdded: "stroke"; case .pause: "pause"; case .resume: "resume"; case .segment: "segment"; case .sessionEnd: "end"; default: "other" }80        }81        XCTAssertEqual(kinds.filter { $0 != "segment" }, ["start", "page", "stroke", "pause", "page", "resume", "end"])82        XCTAssertEqual(events.first { if case .pause = $0.e { return true }; return false }?.t, T(65))83        XCTAssertEqual(events.last?.t, m.duration)84        // The loudness envelope saved for the editor covers the whole recording.85        XCTAssertEqual(r.package.loadEnvelope()?.duration.seconds ?? 0, 75, accuracy: 0.05)86        // The pre-roll audio is the loud audio heard just before resuming.87        let reader = TimelineAudioReader(package: r.package, manifest: m)88        let pre = reader.read(start: Int64(65.05 * 48_000), count: 4_800), body = reader.read(start: Int64(70 * 48_000), count: 4_800)89        XCTAssertGreaterThan(pre.map { abs($0) }.max() ?? 0, 0.4); XCTAssertLessThan(body.map { abs($0) }.max() ?? 1, 0.25)90    }9192    /// Acceptance: interrupted capture — every finalized segment is recovered; nothing is claimed for bytes that never reached the disk.93    func testCrashRecovery() throws {94        let r = try rig(segment: 30)95        try r.engine.start(currentPage: 2)96        r.source.push(seconds: 40)97        r.engine.record(.marker(Marker(kind: .chapter, label: "Avant le crash")))98        r.source.push(seconds: 35)                                                // 75 s captured: 2 finalized segments + 15 s in the open file99        r.engine.log.flush()100101        // "Kill" the app: take the on-disk state as it is right now, without stop() or any finalization.102        let crashed = dir.appendingPathComponent("crashed.slecture")103        try FileManager.default.copyItem(at: r.package.url, to: crashed)104        let pkg = ProjectPackage(url: crashed)105        let before = try pkg.loadManifest()106        XCTAssertNil(before.sessions.first?.endedAt)107        XCTAssertEqual(before.segments(.audio).count, 2)108        XCTAssertTrue(ProjectRecovery.needsRecovery(pkg, manifest: before))109110        let (manifest, report) = try ProjectRecovery.recover(pkg)111        XCTAssertEqual(report.closedSessions.count, 1)112        XCTAssertGreaterThanOrEqual(manifest.duration, T(60))                     // both finalized segments, always113        XCTAssertLessThanOrEqual(manifest.duration, T(75.001))                    // never more than what was captured114        let onDisk = manifest.segments(.audio).reduce(Int64(0)) { $0 + (MediaProbe.probe(pkg.url($1.path), kind: .audio)?.frames ?? 0) }115        XCTAssertEqual(manifest.duration, MediaTime(frames: onDisk, sampleRate: 48_000))   // the manifest states exactly what can be decoded116        XCTAssertTrue(manifest.sessions[0].recovered)117        XCTAssertFalse(ProjectRecovery.needsRecovery(pkg, manifest: manifest))118        XCTAssertEqual(pkg.loadEvents().events.filter { if case .marker = $0.e { return true }; return false }.count, 1)119        print("MEASURE crash recovery: \(manifest.duration.seconds) s recovered of 75 s captured; open-segment files recovered: \(report.recoveredSegments.map(\.path)), repaired headers: \(report.repairedHeaders), unreadable: \(report.unreadableFiles)")120121        // A second pass changes nothing, and recording can continue after recovery.122        XCTAssertFalse(try ProjectRecovery.recover(pkg).report.didChange)123        let source = SyntheticAudioSource()124        let engine = RecordingEngine(package: pkg, manifest: manifest, log: try EventLogWriter(url: pkg.eventsURL), audio: source)125        engine.useHostClock { source.hostTime }126        try engine.start(currentPage: 0)127        source.push(seconds: 5)128        let after = engine.stop()129        XCTAssertEqual(after.duration, manifest.duration + T(5))130        XCTAssertEqual(after.sessions.count, 2)131132        // The original engine can still stop cleanly: its own package is complete.133        XCTAssertEqual(r.engine.stop().duration, T(75))134    }135136    /// Acceptance: a recorder process killed with SIGKILL (no clean-up of any kind) — the project reopens with every flushed sample.137    func testRealProcessKillRecovery() throws {138        #if os(macOS)139        let tool = Bundle(for: Self.self).bundleURL.deletingLastPathComponent().appendingPathComponent("lecture-crashsim")140        try XCTSkipUnless(FileManager.default.isExecutableFile(atPath: tool.path), "helper not built")141        let p = Process(); p.executableURL = tool; p.arguments = [dir.path, "2", "7"]142        let pipe = Pipe(); p.standardOutput = pipe143        try p.run()144        var line = ""145        while !line.contains("\n") { let d = pipe.fileHandleForReading.availableData; if d.isEmpty { break }; line += String(decoding: d, as: UTF8.self) }146        XCTAssertTrue(line.hasPrefix("READY "), line)147        Thread.sleep(forTimeInterval: 1.3)                                         // real-time recording continues for a while…148        kill(p.processIdentifier, SIGKILL); p.waitUntilExit()                      // …then the process dies without any chance to finalize149        XCTAssertEqual(p.terminationReason, .uncaughtSignal)150151        let pkg = ProjectPackage(url: URL(fileURLWithPath: String(line.dropFirst(6)).trimmingCharacters(in: .whitespacesAndNewlines)))152        let before = try pkg.loadManifest()153        XCTAssertNil(before.sessions.first?.endedAt)154        XCTAssertGreaterThanOrEqual(before.segments(.audio).count, 3)              // 7 s captured with 2 s segments: at least 3 were finalized and checkpointed155        let (m, report) = try ProjectRecovery.recover(pkg)156        XCTAssertGreaterThanOrEqual(m.duration, T(6)); XCTAssertLessThan(m.duration, T(12))157        let decodable = m.segments(.audio).reduce(Int64(0)) { $0 + (MediaProbe.probe(pkg.url($1.path), kind: .audio)?.frames ?? 0) }158        XCTAssertEqual(m.duration, MediaTime(frames: decodable, sampleRate: 48_000))159        XCTAssertTrue(pkg.diagnose(m, deep: true).isHealthy)160        XCTAssertEqual(pkg.loadEvents().events.filter { if case .marker = $0.e { return true }; return false }.count, 1)161        let reader = TimelineAudioReader(package: pkg, manifest: m)162        XCTAssertGreaterThan(reader.read(start: 48_000 * 5, count: 4_800).map { abs($0) }.max() ?? 0, 0.1)163        print("MEASURE SIGKILL recovery: \(m.duration.seconds) s recovered (finalized before kill: 6 s); recovered files \(report.recoveredSegments.count), repaired headers \(report.repairedHeaders.count), unreadable \(report.unreadableFiles.count)")164        #else165        throw XCTSkip("process control is only available on macOS")166        #endif167    }168169    func testOrphanSegmentWithoutManifestEntryIsReattached() throws {170        let r = try rig(segment: 10)171        try r.engine.start(currentPage: 0)172        r.source.push(seconds: 25)173        var m = r.engine.stop()174        XCTAssertEqual(m.duration, T(25))175        // Lose the manifest entries of the last two segments (as if the manifest write had not happened).176        m.sessions[0].segments.removeLast(2); m.sessions[0].duration = T(10); m.sessions[0].endedAt = nil; m.duration = T(10)177        try r.package.saveManifest(m)178        let (fixed, report) = try ProjectRecovery.recover(r.package)179        XCTAssertEqual(report.recoveredSegments.count, 2); XCTAssertEqual(fixed.duration, T(25))180        XCTAssertEqual(fixed.segments(.audio).map(\.start), [T(0), T(10), T(20)])181        // A file that is not audio at all is reported, kept, and not counted.182        try Data("not audio".utf8).write(to: r.package.mediaURL.appendingPathComponent("audio-\(fixed.sessions[0].shortID)-00009.caf"))183        let (again, report2) = try ProjectRecovery.recover(r.package)184        XCTAssertEqual(report2.unreadableFiles.count, 1); XCTAssertEqual(again.duration, T(25))185        XCTAssertTrue(FileManager.default.fileExists(atPath: r.package.mediaURL.appendingPathComponent("audio-\(fixed.sessions[0].shortID)-00009.caf").path))186    }187188    func testAutomaticPoliciesAreAuditedAndNeverDiscard() throws {189        var policy = AutoPausePolicy()190        policy.maxDurationEnabled = true; policy.maxDurationSeconds = 30; policy.countdownSeconds = 5191        policy.silenceEnabled = true; policy.silenceSeconds = 6192        let r = try rig(policy: policy)193        try r.engine.start(currentPage: 0)194        for _ in 0..<40 { r.source.push(seconds: 0.25, amplitude: 0.2); r.engine.tick() }          // 10 s of speech195        for _ in 0..<28 { r.source.push(seconds: 0.25, amplitude: 0); r.engine.tick() }            // 7 s of silence → suggestion at 16 s196        XCTAssertEqual(r.engine.state, .recording)                                                  // a suggestion never pauses by itself197        for _ in 0..<80 where r.engine.state == .recording { r.source.push(seconds: 0.25, amplitude: 0.2); r.engine.tick() }198        XCTAssertEqual(r.engine.state, .paused)                                                     // countdown ended → pause (default), not stop199        let m = r.engine.stop()200        XCTAssertEqual(m.duration.seconds, 30, accuracy: 0.6)201        XCTAssertTrue(r.package.diagnose(m).isHealthy)                                              // everything recorded is kept202        let audit = TimelineIndex(events: r.package.loadEvents().events).autoDecisions.map { "\($0.decision.policy.rawValue):\($0.decision.action.rawValue)" }203        XCTAssertEqual(audit, ["silence:suggestedPause", "silence:suggestionWithdrawn", "maxDuration:countdownStarted", "maxDuration:paused"])204        let pause = r.package.loadEvents().events.compactMap { e -> PauseReason? in if case .pause(let reason) = e.e { return reason }; return nil }205        XCTAssertEqual(pause, [.autoMaxDuration])206    }207208    func testInterruptionPausesAndRouteChangeStartsANewSegment() throws {209        let r = try rig(segment: 30)210        try r.engine.start(currentPage: 0)211        r.source.push(seconds: 5)212        r.source.push(seconds: 5, sampleRate: 44_100)                             // Bluetooth headset connected: hardware rate changes213        r.source.onEvent?(.interruptionBegan)                                      // phone call214        XCTAssertEqual(r.engine.state, .paused)215        r.source.advance(seconds: 60)216        r.source.onEvent?(.interruptionEnded(shouldResume: true))217        XCTAssertEqual(r.engine.state, .paused)                                    // never resumes on its own218        r.engine.resume()219        r.source.push(seconds: 5)220        let m = r.engine.stop()221        XCTAssertEqual(m.segments(.audio).map(\.sampleRate), [48_000, 44_100, 48_000])222        XCTAssertEqual(m.duration.seconds, 15, accuracy: 0.31)223        let reasons = r.package.loadEvents().events.compactMap { e -> PauseReason? in if case .pause(let reason) = e.e { return reason }; return nil }224        XCTAssertEqual(reasons, [.interruption])225        // The mixed-rate recording reads back as one continuous 48 kHz stream.226        let reader = TimelineAudioReader(package: r.package, manifest: m)227        let mid = reader.read(start: Int64(7 * 48_000), count: 48_000)228        XCTAssertEqual((mid.reduce(0) { $0 + $1 * $1 } / 48_000).squareRoot(), 0.2 / Float(2.0.squareRoot()), accuracy: 0.01)229    }230231    /// Acceptance: microphone permission denied → PDF-only mode still records the presentation and exports.232    func testPDFOnlyModeRecordsAndExportsWithoutAudio() async throws {233        let r = try rig(audio: false)234        try r.engine.start(currentPage: 0)235        r.source.advance(seconds: 3)236        let s = Stroke(page: 0, tool: .pen, color: .red, width: 0.012, begin: r.engine.position, points: [StrokePoint(x: 0.2, y: 0.5), StrokePoint(x: 0.8, y: 0.5, dt: 0)])237        r.engine.record(.strokeAdded(s))238        r.source.advance(seconds: 3)239        r.engine.pause(); r.source.advance(seconds: 100); r.engine.resume()240        r.source.advance(seconds: 2)241        let m = r.engine.stop()242        XCTAssertEqual(m.duration, T(8)); XCTAssertTrue(m.segments(.audio).isEmpty)243        let out = dir.appendingPathComponent("silent.mp4")244        _ = try await VideoExporter(source: try RenderSource.load(r.package), settings: RenderSettings(width: 480, height: 270)).export(to: out)245        let report = await MediaVerifier.verify(out, expectedDuration: T(8))246        XCTAssertTrue(report.isValid, "\(report.problems)"); XCTAssertTrue(report.hasAudio)        // a silent track keeps players happy247        let f1 = await MediaVerifier.frame(of: out, at: T(2.9)), f2 = await MediaVerifier.frame(of: out, at: T(3.1))248        XCTAssertFalse(try XCTUnwrap(PixelProbe(try XCTUnwrap(f1))).isReddish(x: 0.5, y: 0.5))249        XCTAssertTrue(try XCTUnwrap(PixelProbe(try XCTUnwrap(f2))).isReddish(x: 0.5, y: 0.5))250    }251252    func testAppendingSessionsAndAnnotatingWhileIdle() throws {253        let r = try rig()254        // Annotation made before any recording: stamped at time zero, visible from the first frame.255        let pre = Stroke(page: 0, tool: .marker, color: .yellow, width: 0.02, begin: r.engine.position, points: [StrokePoint(x: 0.1, y: 0.1), StrokePoint(x: 0.3, y: 0.1, dt: 0)])256        XCTAssertEqual(r.engine.record(.strokeAdded(pre)).t, .zero)257        try r.engine.start(currentPage: 0); r.source.push(seconds: 4); r.engine.stop()258        let between = r.engine.record(.page(index: 3))259        XCTAssertEqual(between.t, T(4))260        try r.engine.start(currentPage: 3); r.source.push(seconds: 6)261        let m = r.engine.stop()262        XCTAssertEqual(m.sessions.map(\.timelineStart), [T(0), T(4)]); XCTAssertEqual(m.duration, T(10))263        let index = TimelineIndex(events: r.package.loadEvents().events)264        XCTAssertEqual(index.visibleStrokes(page: 0, at: .zero).count, 1); XCTAssertEqual(index.page(at: T(5)), 3)265        // Stopping immediately leaves no empty session behind.266        try r.engine.start(currentPage: 0)267        XCTAssertEqual(r.engine.stop().sessions.count, 2)268    }269270    /// Real hardware: records three seconds from the Mac's default microphone through the full engine.271    /// Skipped (not failed) when the process running the tests has no microphone permission.272    func testRealMicrophoneCaptureWhenPermitted() throws {273        try XCTSkipUnless(CapturePermissions.status(.audio) == .granted, "microphone permission not granted to the test runner (status: \(CapturePermissions.status(.audio)))")274        let inputs = MicrophoneSource.availableInputs()275        try XCTSkipIf(inputs.isEmpty, "no audio input device")276        let pdf = Fixtures.makePDF(at: dir.appendingPathComponent("mic.pdf"), pages: 2)277        let pkg = try ProjectPackage.create(in: dir, manifest: ProjectManifest(title: "Micro réel"), pdf: pdf, pdfPageCount: 2)278        var cfg = RecordingConfiguration(); cfg.segmentSeconds = 1279        let engine = RecordingEngine(package: pkg, manifest: try pkg.loadManifest(), log: try EventLogWriter(url: pkg.eventsURL), audio: MicrophoneSource(), configuration: cfg)280        try engine.start(currentPage: 0)281        Thread.sleep(forTimeInterval: 1.6)282        let mid = engine.record(.marker(Marker(kind: .generic)))283        engine.pause(); Thread.sleep(forTimeInterval: 0.6); engine.resume()284        Thread.sleep(forTimeInterval: 1.5)285        let m = engine.stop()286        XCTAssertEqual(m.duration.seconds, 3.1 + 0.3, accuracy: 0.45)                 // 1.6 s + 1.5 s + pre-roll, within engine start-up latency287        XCTAssertGreaterThanOrEqual(m.segments(.audio).count, 3)288        XCTAssertEqual(mid.t.seconds, 1.6, accuracy: 0.25)                             // event stamped on the audio clock289        XCTAssertTrue(pkg.diagnose(m, deep: true).isHealthy)290        let reader = TimelineAudioReader(package: pkg, manifest: m)291        let samples = reader.read(start: 0, count: Int(reader.totalFrames))292        let rms = (samples.reduce(Float(0)) { $0 + $1 * $1 } / Float(max(1, samples.count))).squareRoot()293        print("MEASURE real microphone: device \(inputs.first(where: \.isDefault)?.name ?? "?"), \(m.segments(.audio).first?.sampleRate ?? 0) Hz, \(m.duration.seconds) s in \(m.segments(.audio).count) segments, RMS \(Envelope.dB(rms)) dBFS")294        XCTAssertGreaterThan(Envelope.dB(rms), -100, "the microphone delivered only digital silence")295    }296297    func testPreflightEvaluation() {298        func m(mic: CapturePermission = .granted, levels: [Double] = Array(repeating: -60, count: 50) + Array(repeating: -22, count: 50), peak: Float = 0.5,299               free: Int64 = 50_000_000_000, minutes: Double = 90, battery: Int? = nil, charging: Bool = true, thermal: ProcessInfo.ThermalState = .nominal) -> [PreflightItem] {300            Preflight.evaluate(PreflightMeasurement(microphone: mic, camera: .undetermined, cameraWanted: false, levels: levels, peak: peak, freeBytes: free, plannedMinutes: minutes,301                                                    batteryPercent: battery, charging: charging, thermal: thermal, settings: (1920, 1080, false, true)))302        }303        XCTAssertEqual(Preflight.worst(m()), .ok)304        XCTAssertEqual(m(mic: .denied).first?.code, "mic.denied"); XCTAssertEqual(Preflight.worst(m(mic: .denied)), .failure)305        XCTAssertTrue(m(levels: Array(repeating: -90, count: 100)).contains { $0.code == "level.silent" })306        XCTAssertTrue(m(levels: Array(repeating: -60, count: 50) + Array(repeating: -45, count: 50)).contains { $0.code == "level.low" })307        XCTAssertTrue(m(peak: 1.0).contains { $0.code == "level.clipping" })308        XCTAssertTrue(m(free: 300_000_000).contains { $0.code == "storage.insufficient" })           // ~52 min of audio for a 90 min lecture309        XCTAssertTrue(m(battery: 15, charging: false).contains { $0.code == "battery.low" })310        XCTAssertTrue(m(thermal: .serious).contains { $0.code == "thermal.serious" })311        XCTAssertEqual(Preflight.bytesPerMinute(camera: false), 5_760_000)312    }313}314
Files main
Menu

Theme