Repository Public
1// SP Lecture Studio — Tests2// File: Tests/Integration/ExportTests.swift3// Description: End-to-end export tests: the MP4 is decoded and checked for ink timing, audio/video sync, edits, cancellation and long durations.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import XCTest8import AVFoundation9@testable import LectureCore10@testable import LectureRender11import LecturePDF12import LectureStorage13import LectureTesting1415func T(_ s: Double) -> MediaTime { MediaTime(seconds: s) }1617func residentMemoryMB() -> Double {18 var info = mach_task_basic_info()19 var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size) / 420 let kr = withUnsafeMutablePointer(to: &info) { $0.withMemoryRebound(to: integer_t.self, capacity: 1) { task_info(mach_task_self_, task_flavor_t(MACH_TASK_BASIC_INFO), $0, &count) } }21 return kr == KERN_SUCCESS ? Double(info.resident_size) / 1_048_576 : 022}2324final class ExportTests: XCTestCase {25 var dir: URL!26 override func setUp() { dir = Fixtures.tempDirectory("export") }27 override func tearDown() { try? FileManager.default.removeItem(at: dir) }2829 func makeProject() throws -> Fixtures.SyntheticProject {30 var plan = Fixtures.AudioPlan(duration: 12); plan.clicks = [2, 5, 9]; plan.tones = [(0...12, 220, 0.05)]31 return try Fixtures.makeProject(in: dir, audio: plan, segmentSeconds: 5) { b in32 b.line(page: 0, at: 4, seconds: 1, y: 0.5) // drawn from 4.0 to 5.0 s33 b.add(7, .page(index: 1))34 b.line(page: 1, at: 9, seconds: 0, y: 0.7) // appears at 9.0 s, together with the third click35 }36 }3738 func probe(_ url: URL, _ t: Double) async throws -> PixelProbe {39 let frame = await MediaVerifier.frame(of: url, at: T(t))40 return try XCTUnwrap(PixelProbe(try XCTUnwrap(frame, "no frame at \(t)")))41 }4243 func clicks(_ url: URL, duration: Double) async throws -> [Double] {44 let audio = await MediaVerifier.audioSamples(of: url, range: TimeRange(seconds: 0, duration))45 let a = try XCTUnwrap(audio)46 return MediaVerifier.clickTimes(in: a.samples, sampleRate: a.sampleRate, threshold: 0.3)47 }4849 /// Acceptance: an annotation drawn at time T appears at time T in the export, and audio stays in sync with it.50 func testExportedMovieHasInkAndAudioAtTheRightTime() async throws {51 let p = try makeProject()52 let out = dir.appendingPathComponent("lecture.mp4")53 var settings = RenderSettings(width: 640, height: 360); settings.fps = 3054 let source = try RenderSource.load(p.package)55 let result = try await VideoExporter(source: source, settings: settings).export(to: out)56 XCTAssertEqual(result.frames, 360); XCTAssertGreaterThan(result.reusedFrames, 250)5758 let report = await MediaVerifier.verify(out, expectedDuration: T(12))59 XCTAssertTrue(report.isValid, "\(report.problems)")60 XCTAssertEqual(report.videoSize, CGSize(width: 640, height: 360)); XCTAssertEqual(report.nominalFPS, 30, accuracy: 0.01)61 XCTAssertEqual(report.videoCodec, "avc1"); XCTAssertEqual(report.decodedFrames, 360)62 XCTAssertTrue(report.hasAudio); XCTAssertEqual(report.audioSampleRate, 48_000)6364 // Ink timing (one frame = 33 ms).65 let before = try await probe(out, 3.9); XCTAssertFalse(before.isReddish(x: 0.5, y: 0.5))66 let during = try await probe(out, 4.5); XCTAssertTrue(during.isReddish(x: 0.3, y: 0.5)); XCTAssertFalse(during.isReddish(x: 0.72, y: 0.5))67 let after = try await probe(out, 5.1); XCTAssertTrue(after.isReddish(x: 0.72, y: 0.5))68 let page2 = try await probe(out, 7.1); XCTAssertFalse(page2.isReddish(x: 0.5, y: 0.5))69 let notYet = try await probe(out, 8.93); XCTAssertFalse(notYet.isReddish(x: 0.5, y: 0.7))70 let appeared = try await probe(out, 9.04); XCTAssertTrue(appeared.isReddish(x: 0.5, y: 0.7))7172 // Audio timing: clicks at 2, 5 and 9 s within 3 ms.73 let found = try await clicks(out, duration: 12)74 XCTAssertEqual(found.count, 3, "\(found)")75 for (f, e) in zip(found, [2.0, 5.0, 9.0]) { XCTAssertEqual(f, e, accuracy: 0.003) }76 }7778 /// Acceptance: nondestructive editing — cut, export, undo, re-export; originals untouched.79 func testEditedExportThenUndoAndReexport() async throws {80 let p = try makeProject()81 let sums = try p.manifest.segments(.audio).map { try Hashing.sha256(of: p.package.url($0.path)) }82 var history = EditHistory()83 history.apply { $0.addCut(TimeRange(seconds: 5.5, 8.5)) }84 try p.package.saveEdits(history)8586 let cutURL = dir.appendingPathComponent("cut.mp4")87 let settings = RenderSettings(width: 480, height: 270)88 let r1 = try await VideoExporter(source: try RenderSource.load(p.package), settings: settings).export(to: cutURL)89 XCTAssertEqual(r1.duration, T(9))90 let rep1 = await MediaVerifier.verify(cutURL, expectedDuration: T(9)); XCTAssertTrue(rep1.isValid, "\(rep1.problems)")91 let c1 = try await clicks(cutURL, duration: 9)92 XCTAssertEqual(c1.count, 3); XCTAssertEqual(c1[2], 6.0, accuracy: 0.003) // 9.0 s → 6.0 s after removing 3 s93 let join = try await probe(cutURL, 5.6)94 let band = join.rgb(x: 0.5, y: 0.02); XCTAssertTrue(band.g > 0.4 && band.r < 0.3) // the slide change inside the cut is applied at the join95 let moved = try await probe(cutURL, 6.05); XCTAssertTrue(moved.isReddish(x: 0.5, y: 0.7))96 let notYet = try await probe(cutURL, 5.9); XCTAssertFalse(notYet.isReddish(x: 0.5, y: 0.7))9798 history.undo()99 try p.package.saveEdits(history)100 let fullURL = dir.appendingPathComponent("full.mp4")101 let r2 = try await VideoExporter(source: try RenderSource.load(p.package), settings: settings).export(to: fullURL)102 XCTAssertEqual(r2.duration, T(12))103 let c2 = try await clicks(fullURL, duration: 12); XCTAssertEqual(c2.count, 3); XCTAssertEqual(c2[2], 9.0, accuracy: 0.003)104 XCTAssertEqual(try p.manifest.segments(.audio).map { try Hashing.sha256(of: p.package.url($0.path)) }, sums)105 }106107 func testCancellationLeavesNothingBehindAndHEVCWorks() async throws {108 let p = try makeProject()109 let source = try RenderSource.load(p.package)110 let out = dir.appendingPathComponent("cancel.mp4")111 final class Flag: @unchecked Sendable { var cancel = false }112 let flag = Flag()113 do {114 _ = try await VideoExporter(source: source, settings: RenderSettings(width: 640, height: 360))115 .export(to: out, progress: { if $0 > 0.3 { flag.cancel = true } }, isCancelled: { flag.cancel })116 XCTFail("should have been cancelled")117 } catch { XCTAssertEqual(error as? ExportError, .cancelled) }118 XCTAssertFalse(FileManager.default.fileExists(atPath: out.path))119 let leftovers = try FileManager.default.contentsOfDirectory(atPath: dir.path).filter { $0.contains(".partial") }120 XCTAssertTrue(leftovers.isEmpty, "\(leftovers)")121122 var hevc = RenderSettings(width: 640, height: 360); hevc.codec = .hevc123 try XCTSkipUnless(VideoExporter.isAvailable(.hevc), "HEVC encoder not available on this machine")124 let hevcURL = dir.appendingPathComponent("hevc.mp4")125 _ = try await VideoExporter(source: source, settings: hevc).export(to: hevcURL)126 let rep = await MediaVerifier.verify(hevcURL, expectedDuration: T(12))127 XCTAssertTrue(rep.isValid, "\(rep.problems)"); XCTAssertEqual(rep.videoCodec, "hvc1")128 }129130 func testNothingToExportAndMissingSegmentBecomesSilence() async throws {131 let empty = try ProjectPackage.create(in: dir, manifest: ProjectManifest(title: "Vide"), pdf: Fixtures.makePDF(at: dir.appendingPathComponent("e.pdf"), pages: 1), pdfPageCount: 1)132 do { _ = try await VideoExporter(source: try RenderSource.load(empty), settings: .hd720).export(to: dir.appendingPathComponent("x.mp4")); XCTFail() }133 catch { XCTAssertEqual(error as? ExportError, .nothingToExport) }134135 let p = try makeProject()136 let second = p.manifest.segments(.audio)[1]137 try FileManager.default.moveItem(at: p.package.url(second.path), to: dir.appendingPathComponent("moved.caf")) // simulate missing media138 XCTAssertEqual(p.package.diagnose(p.manifest).missingSegments.map(\.path), [second.path])139 let out = dir.appendingPathComponent("partial-audio.mp4")140 let r = try await VideoExporter(source: try RenderSource.load(p.package), settings: RenderSettings(width: 320, height: 180)).export(to: out)141 XCTAssertEqual(r.unreadableSegments, [second.path]) // reported, not hidden142 let c = try await clicks(out, duration: 12)143 XCTAssertEqual(c.count, 1) // the clicks at 5 s and 9 s were in the missing segment (5–10 s)144 }145146 /// Acceptance: 90-minute synthetic session — A/V sync at the very end, memory and time measured.147 /// Enabled with LECTURE_LONG_TESTS=1 (takes a few minutes and ~600 MB of temporary disk).148 func testNinetyMinuteSyncAndResources() async throws {149 try XCTSkipUnless(ProcessInfo.processInfo.environment["LECTURE_LONG_TESTS"] == "1", "set LECTURE_LONG_TESTS=1 to run the 90-minute export")150 let duration = 5400.0151 let marks: [Double] = [1, 600.5, 1800.25, 2700, 3599.9, 4500.4, 5399]152 var plan = Fixtures.AudioPlan(duration: duration); plan.clicks = marks153 let memBefore = residentMemoryMB()154 let p = try Fixtures.makeProject(in: dir, pages: 20, audio: plan, segmentSeconds: 30) { b in155 for (i, t) in marks.enumerated() {156 if i > 0 { b.add(t - 0.5, .page(index: i)) }157 b.line(page: i, at: t, seconds: 0, y: 0.3 + 0.08 * Double(i))158 }159 // Background activity: a stroke every 20 s, as in a real lecture.160 for k in stride(from: 10.0, to: duration - 10, by: 20) { b.line(page: 0, at: k, seconds: 1.5, y: 0.9, x0: 0.1, x1: 0.2, color: .blue, width: 0.004) }161 }162 XCTAssertEqual(p.manifest.segments(.audio).count, 180)163 let out = dir.appendingPathComponent("long.mp4")164 var settings = RenderSettings(width: 640, height: 360); settings.fps = 30165 var peak = memBefore166 final class Mem: @unchecked Sendable { var peak = 0.0 }167 let mem = Mem()168 let result = try await VideoExporter(source: try RenderSource.load(p.package), settings: settings).export(to: out, progress: { _ in mem.peak = max(mem.peak, residentMemoryMB()) })169 peak = max(peak, mem.peak)170 XCTAssertEqual(result.frames, 162_000)171 let report = await MediaVerifier.verify(out, expectedDuration: T(duration), decodeAll: false)172 XCTAssertTrue(report.isValid, "\(report.problems)")173 for (i, t) in marks.enumerated() {174 let y = 0.3 + 0.08 * Double(i)175 let before = try await probe(out, t - 0.07); XCTAssertFalse(before.isReddish(x: 0.5, y: y), "ink too early at \(t)")176 let after = try await probe(out, t + 0.04); XCTAssertTrue(after.isReddish(x: 0.5, y: y), "ink missing at \(t)")177 let a = await MediaVerifier.audioSamples(of: out, range: TimeRange(seconds: t - 0.5, t + 0.5))178 let audio = try XCTUnwrap(a)179 let c = MediaVerifier.clickTimes(in: audio.samples, sampleRate: audio.sampleRate, threshold: 0.3)180 XCTAssertEqual(try XCTUnwrap(c.first, "click missing at \(t)") + t - 0.5, t, accuracy: 0.003, "audio drift at \(t)")181 }182 let line = String(format: "90-min export: %.1f s wall, %d frames (%d reused), %.1f MB file, peak RSS %.0f MB (start %.0f MB), project %.0f MB on disk",183 result.elapsed, result.frames, result.reusedFrames, Double(result.byteCount) / 1e6, peak, memBefore, Double(p.package.diskUsage()) / 1e6)184 print("MEASURE " + line)185 try? line.write(to: URL(fileURLWithPath: "/tmp/slecture-long-test.txt"), atomically: true, encoding: .utf8)186 XCTAssertLessThan(peak, 1500, "memory must stay bounded on long exports")187 }188}189