Repository Public
1// SP Lecture Studio2// File: Apps/LectureStudio/Shared/Support/SelfTest.swift3// Description: Debug-only end-to-end self-test of the real workflow (import, record, annotate, pause, reopen, edit, export, verify) and demo content.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7#if DEBUG8import Foundation9import CoreGraphics10import CoreText11import AVFoundation12import LectureCore13import LectureStorage14import LecturePDF15import LectureCapture16import LectureRender17#if os(iOS)18import UIKit19#endif2021/// Launch arguments (Debug builds only):22/// -LectureDemoPDF YES adds a generated 20-page lecture to an empty library (used by the UI tests)23/// -LectureSelfTest YES runs the scripted end-to-end test, writes selftest-report.json, then exits24/// -LectureSelfTestSeconds N length of the recording (default 14)25/// -LectureSelfTestSilent YES never ask for the microphone: run in PDF-only mode26enum DebugSupport {27 static var wantsSelfTest: Bool { UserDefaults.standard.bool(forKey: "LectureSelfTest") }28 static var wantsDemo: Bool { UserDefaults.standard.bool(forKey: "LectureDemoPDF") }2930 /// A 20-page deck drawn with Core Graphics (titles, body text, a coloured band per page).31 static func makeDemoPDF(pages: Int = 20) -> URL {32 let url = FileManager.default.temporaryDirectory.appendingPathComponent("Démo évaluation immobilière.pdf")33 var box = CGRect(x: 0, y: 0, width: 960, height: 540)34 guard let ctx = CGContext(url as CFURL, mediaBox: &box, nil) else { return url }35 let hues: [(CGFloat, CGFloat, CGFloat)] = [(0.16, 0.33, 0.78), (0.1, 0.55, 0.35), (0.75, 0.4, 0.1), (0.5, 0.2, 0.65)]36 func text(_ s: String, _ p: CGPoint, _ size: CGFloat, bold: Bool = false, gray: CGFloat = 0.1) {37 let font = CTFontCreateWithName((bold ? "Helvetica-Bold" : "Helvetica") as CFString, size, nil)38 let attr = NSAttributedString(string: s, attributes: [NSAttributedString.Key(kCTFontAttributeName as String): font,39 NSAttributedString.Key(kCTForegroundColorAttributeName as String): CGColor(gray: gray, alpha: 1)])40 ctx.textPosition = p; CTLineDraw(CTLineCreateWithAttributedString(attr), ctx)41 }42 for i in 0..<pages {43 ctx.beginPDFPage(nil)44 ctx.setFillColor(CGColor(gray: 1, alpha: 1)); ctx.fill(box)45 let h = hues[i % hues.count]46 ctx.setFillColor(CGColor(red: h.0, green: h.1, blue: h.2, alpha: 1)); ctx.fill(CGRect(x: 0, y: 470, width: 960, height: 70))47 text("Séance 8 — Méthode de comparaison", CGPoint(x: 40, y: 494), 28, bold: true, gray: 1)48 text("Diapositive \(i + 1)", CGPoint(x: 40, y: 400), 40, bold: true)49 for (k, line) in ["Sélection des comparables dans un rayon de 500 m", "Ajustements : superficie, âge, état, garage", "Rapprochement et valeur indiquée"].enumerated() {50 text("• " + line, CGPoint(x: 60, y: 330 - CGFloat(k) * 46), 24)51 }52 text("\(i + 1) / \(pages)", CGPoint(x: 880, y: 20), 14, gray: 0.5)53 ctx.endPDFPage()54 }55 ctx.closePDF()56 return url57 }5859 @MainActor static func installDemoIfNeeded(_ model: AppModel) {60 guard wantsDemo, model.items.isEmpty else { return }61 model.createLecture(fromPDF: makeDemoPDF(), title: "Démo — méthode de comparaison", courseCode: "IMM1003", sessionNumber: 8)62 model.selectedID = nil63 }64}6566struct SelfTestReport: Codable {67 var platform = ""68 var startedAt = Date()69 var requestedSeconds = 0.070 var microphone = ""71 var recordedSeconds = 0.072 var audioSegments = 073 var pauses = 074 var strokesRecorded = 075 var strokesAfterReopen = 076 var pagesVisited = 077 var audioRMSdB = -120.078 var cutSeconds = 0.079 var exportSeconds = 0.080 var exportElapsed = 0.081 var videoSize = ""82 var videoCodec = ""83 var videoPlayable = false84 var inkTimingOK = false85 var inkTimingDetail = ""86 var annotatedPDFPages = 087 var annotatedPDFInkOK = false88 var sourcePDFUnchanged = false89 var originalsUnchanged = false90 var reexportAfterUndoSeconds = 0.091 var files: [String] = []92 var failures: [String] = []93 var passed: Bool { failures.isEmpty }94}9596@MainActor97enum SelfTest {98 static func pixel(_ image: CGImage, x: Double, y: Double) -> (r: Double, g: Double, b: Double) {99 var px = [UInt8](repeating: 0, count: 4)100 let ctx = CGContext(data: &px, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: Bitmap.sRGB, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!101 let w = Double(image.width), h = Double(image.height)102 ctx.draw(image, in: CGRect(x: -x * w, y: -(1 - y) * h + 1, width: w, height: h))103 return (Double(px[0]) / 255, Double(px[1]) / 255, Double(px[2]) / 255)104 }105 static func isRed(_ image: CGImage?, x: Double, y: Double) -> Bool {106 guard let image else { return false }107 let c = pixel(image, x: x, y: y)108 return c.r > 0.6 && c.g < 0.4 && c.b < 0.4109 }110111 static func run() async -> SelfTestReport {112 var report = SelfTestReport()113 #if os(macOS)114 report.platform = "macOS \(ProcessInfo.processInfo.operatingSystemVersionString)"115 #else116 report.platform = "\(UIDevice.current.userInterfaceIdiom == .pad ? "iPadOS" : "iOS") \(UIDevice.current.systemVersion) (\(UIDevice.current.name))"117 #endif118 let seconds = max(8, UserDefaults.standard.double(forKey: "LectureSelfTestSeconds") == 0 ? 14 : UserDefaults.standard.double(forKey: "LectureSelfTestSeconds"))119 report.requestedSeconds = seconds120 func fail(_ m: String) { report.failures.append(m) }121 let fm = FileManager.default122 let root = AppModel.defaultRoot().deletingLastPathComponent().appendingPathComponent("SelfTest-\(UUID().uuidString.prefix(8))/Lectures")123 let model = AppModel(root: root)124125 // 1. Import a 20-page PDF (offline: nothing in this test touches the network).126 let pdf = DebugSupport.makeDemoPDF()127 let pdfHash = try? Hashing.sha256(of: pdf)128 guard let id = model.createLecture(fromPDF: pdf, title: "Autotest", courseCode: "TEST", sessionNumber: 1), let session = model.session(for: id) else {129 fail("project creation failed"); return report130 }131 if session.pageCount != 20 { fail("expected 20 pages, got \(session.pageCount)") }132133 // 2. Record with the real microphone when permitted, otherwise in PDF-only mode.134 var silent = UserDefaults.standard.bool(forKey: "LectureSelfTestSilent")135 if !silent, CapturePermissions.status(.audio) == .undetermined {136 // The system prompt needs a person. Wait a little for an answer, then continue without sound rather than hang.137 Task { _ = await CapturePermissions.request(.audio) }138 for _ in 0..<50 where CapturePermissions.status(.audio) == .undetermined { try? await Task.sleep(nanoseconds: 500_000_000) }139 if CapturePermissions.status(.audio) == .undetermined { silent = true; report.microphone = "permission prompt not answered within 25 s → PDF-only mode" }140 }141 var outcome = silent ? await session.startRecording(withoutAudio: true) : await session.startRecording()142 if case .needsMicrophoneDecision = outcome { report.microphone = "denied or undetermined → PDF-only mode"; outcome = await session.startRecording(withoutAudio: true) }143 else if report.microphone.isEmpty { report.microphone = silent ? "not requested (silent run)" : "granted" }144 if case .failed(let m) = outcome { fail("recording did not start: \(m)"); return report }145146 // 3. Scripted lecture: a stroke every ~4 s, a slide change every ~6 s, a marker, zoom, pointer, one pause.147 var strokeTimes: [(begin: MediaTime, end: MediaTime, page: Int, y: Double)] = []148 let begun = Date()149 var nextStroke = 2.0, nextPage = 8.0, paused = false, strokeIndex = 0 // slide changes fall 2 s after a stroke, never on one150 func addStroke() {151 let y = 0.34 + 0.055 * Double(strokeIndex % 10) // a distinct height per stroke: checks never confuse two strokes152 let now = session.strokeBeginTime()153 let begin = MediaTime(ns: max(0, now.ns - 800_000_000))154 let pts = (0..<48).map { i in StrokePoint(x: 0.2 + 0.6 * Double(i) / 47, y: y, dt: Int64(Double(i) / 47 * 0.8e9), w: 1) }155 session.add(Stroke(page: session.page, tool: .pen, color: .red, width: 0.008, begin: begin, points: pts))156 strokeTimes.append((begin, begin + MediaTime(seconds: 0.8), session.page, y)); strokeIndex += 1157 }158 while Date().timeIntervalSince(begun) < seconds + (paused ? 2 : 0) {159 let t = Date().timeIntervalSince(begun)160 if t >= nextStroke { addStroke(); nextStroke += 4 }161 if t >= nextPage { session.nextPage(); nextPage += 4 }162 if !paused, t >= seconds * 0.45 {163 session.addMarker(.retake)164 session.pause(); paused = true; report.pauses += 1165 try? await Task.sleep(nanoseconds: 2_000_000_000)166 session.resume()167 session.applyViewport(Viewport(zoom: 1.6, cx: 0.5, cy: 0.5), force: true)168 session.movePointer(x: 0.5, y: 0.3)169 }170 if paused, t >= seconds * 0.45 + 5, !session.viewport.isFit { session.hidePointer(); session.applyViewport(.fit, force: true) }171 try? await Task.sleep(nanoseconds: 100_000_000)172 }173 session.stopRecording()174 for _ in 0..<80 where session.recState != .idle { try? await Task.sleep(nanoseconds: 100_000_000) }175 if session.recState != .idle { fail("recording did not stop") }176177 let manifest = session.manifest178 report.recordedSeconds = manifest.duration.seconds179 report.audioSegments = manifest.segments(.audio).count180 report.strokesRecorded = strokeTimes.count181 if abs(manifest.duration.seconds - seconds) > 2.5 { fail("recorded \(manifest.duration.seconds) s for a \(seconds) s session") }182 if report.microphone == "granted", report.audioSegments == 0 { fail("no audio segment although the microphone was granted") }183 if !session.package.diagnose(manifest, deep: true).isHealthy { fail("project diagnostics failed after recording") }184 let sums = manifest.segments(.audio).compactMap { try? Hashing.sha256(of: session.package.url($0.path)) }185 if report.audioSegments > 0 {186 let reader = TimelineAudioReader(package: session.package, manifest: manifest)187 let all = reader.read(start: 0, count: Int(reader.totalFrames))188 let rms = (all.reduce(Float(0)) { $0 + $1 * $1 } / Float(max(1, all.count))).squareRoot()189 report.audioRMSdB = Double(Envelope.dB(rms))190 }191192 // 4. Reopen the project from disk, as after quitting the app.193 let package = session.package194 session.close()195 guard let reopened = try? ProjectSession(package: package, settings: model.settings) else { fail("project did not reopen"); return report }196 report.strokesAfterReopen = (0..<reopened.pageCount).reduce(0) { $0 + reopened.strokes(on: $1).count }197 let index = TimelineIndex(events: package.loadEvents().events)198 report.pagesVisited = Set(index.pageChanges.map(\.page)).count199 if report.strokesAfterReopen != strokeTimes.count { fail("\(strokeTimes.count) strokes drawn, \(report.strokesAfterReopen) after reopening") }200 if index.pauses.count != report.pauses { fail("pause not recorded in the timeline") }201 reopened.close()202203 // 5. Nondestructive edit: cut one second in the first third, then export.204 var history = package.loadEdits()205 let cut = TimeRange(seconds: 0.5, 1.5)206 history.apply { $0.addCut(cut) }207 try? package.saveEdits(history)208 report.cutSeconds = cut.duration.seconds209 guard let editSession = try? ProjectSession(package: package, settings: model.settings) else { fail("reopen for export failed"); return report }210 let coordinator = ExportCoordinator(session: editSession, settings: model.settings, exportsRoot: model.exportsRoot)211 var opt = ExportOptions(); opt.preset = .hd1080; opt.captionsSRT = false; opt.archive = true212 coordinator.options = opt213 let exportStart = Date()214 coordinator.start()215 while coordinator.running { try? await Task.sleep(nanoseconds: 200_000_000) }216 report.exportElapsed = Date().timeIntervalSince(exportStart)217 if let e = coordinator.error { fail("export: \(e)") }218 report.files = coordinator.files.map { $0.url.lastPathComponent }219220 // 6. Verify the exported movie by decoding it.221 if let video = coordinator.files.first(where: { $0.kind == "video" })?.url {222 let media = await MediaVerifier.verify(video, decodeAll: true)223 report.exportSeconds = media.duration.seconds224 report.videoSize = "\(Int(media.videoSize.width))x\(Int(media.videoSize.height))"; report.videoCodec = media.videoCodec; report.videoPlayable = media.isValid225 if !media.isValid { fail("exported movie invalid: \(media.problems)") }226 if media.videoSize != CGSize(width: 1920, height: 1080) { fail("expected 1920x1080, got \(report.videoSize)") }227 if abs(media.duration.seconds - (manifest.duration.seconds - 1)) > 0.2 { fail("edited duration \(media.duration.seconds) s, expected \(manifest.duration.seconds - 1) s") }228 // Ink timing: for each stroke made at zoom 1, absent just before it begins and present once drawn.229 let map = history.current.timeMap(duration: manifest.duration)230 var checked = 0, good = 0231 for s in strokeTimes where index.viewport(at: s.end).isFit && index.viewport(at: s.begin).isFit && s.begin.seconds > 2 {232 let before = await MediaVerifier.frame(of: video, at: map.toOutput(s.begin) - MediaTime(seconds: 0.15))233 let after = await MediaVerifier.frame(of: video, at: map.toOutput(s.end) + MediaTime(seconds: 0.12))234 checked += 1235 if !isRed(before, x: 0.5, y: s.y), isRed(after, x: 0.5, y: s.y) { good += 1 }236 else { report.inkTimingDetail += "[stroke \(s.begin.seconds)–\(s.end.seconds) p\(s.page) before=\(isRed(before, x: 0.5, y: s.y)) after=\(isRed(after, x: 0.5, y: s.y))] " }237 }238 report.inkTimingDetail = "\(good)/\(checked) strokes appear at their recorded time " + report.inkTimingDetail239 report.inkTimingOK = checked > 0 && good == checked240 if !report.inkTimingOK { fail("ink timing: \(report.inkTimingDetail)") }241 } else { fail("no video exported") }242243 // 7. Annotated PDF matches the final annotation state; the source PDF is untouched.244 if let annotated = coordinator.files.first(where: { $0.kind == "pdf" })?.url, let doc = try? SlideDocument(url: annotated) {245 report.annotatedPDFPages = doc.pageCount246 if let last = strokeTimes.last, let img = doc.image(page: last.page, pixelSize: CGSize(width: 960, height: 540)) { report.annotatedPDFInkOK = isRed(img, x: 0.5, y: last.y) }247 if doc.pageCount != 20 || !report.annotatedPDFInkOK { fail("annotated PDF does not match the final annotations") }248 } else { fail("no annotated PDF") }249 report.sourcePDFUnchanged = (try? Hashing.sha256(of: package.pdfURL)) == pdfHash && (try? Hashing.sha256(of: pdf)) == pdfHash250 if !report.sourcePDFUnchanged { fail("source PDF changed") }251252 // 8. Undo the cut and export again: the originals are intact and the full length is back.253 history.undo(); try? package.saveEdits(history)254 var fast = RenderSettings(width: 640, height: 360)255 fast.fps = 30256 let again = model.exportsRoot.appendingPathComponent("selftest-reexport.mp4")257 try? fm.createDirectory(at: model.exportsRoot, withIntermediateDirectories: true)258 if let source = try? RenderSource.load(package), let r = try? await VideoExporter(source: source, settings: fast).export(to: again) { report.reexportAfterUndoSeconds = r.duration.seconds }259 if abs(report.reexportAfterUndoSeconds - manifest.duration.seconds) > 0.05 { fail("re-export after undo is \(report.reexportAfterUndoSeconds) s") }260 report.originalsUnchanged = manifest.segments(.audio).compactMap { try? Hashing.sha256(of: package.url($0.path)) } == sums261 if !report.originalsUnchanged { fail("source recordings changed") }262 editSession.close()263264 let enc = JSONEncoder(); enc.outputFormatting = [.prettyPrinted, .sortedKeys]; enc.dateEncodingStrategy = .iso8601265 if let data = try? enc.encode(report) {266 let out = AppModel.defaultRoot().deletingLastPathComponent().appendingPathComponent("selftest-report.json")267 try? data.write(to: out, options: .atomic)268 print("SELFTEST_REPORT " + out.path)269 print(String(decoding: data, as: UTF8.self))270 }271 print(report.passed ? "SELFTEST PASSED" : "SELFTEST FAILED: \(report.failures)")272 return report273 }274}275#endif276