import Foundation import CoreGraphics enum ContinuityChecks { static func check(_ value: @autoclosure () -> Bool, _ message: String) throws { if !value() { throw NSError(domain: "ContinuityChecks", code: 1, userInfo: [NSLocalizedDescriptionKey: message]) } } static func run() throws { let frontal = FacePose(x: 0.5, y: 0.4, height: 0.2, yaw: 0, roll: 0, eyesVisible: true) var turned = frontal turned.yaw = 0.6 let choices = ContinuityPlanner.choose([ [SegmentCandidate(start: 0, quality: 1, entry: turned, exit: turned), SegmentCandidate(start: 2, quality: 0.9, entry: frontal, exit: frontal)], [SegmentCandidate(start: 1, quality: 1, entry: frontal, exit: frontal)] ]) try check(choices.map(\.start) == [2, 1], "Joint planning did not prefer matching neighboring poses") let missing = VisualSample(time: 0, faceCount: 0, pose: nil, color: nil, quality: 0) let analysis = ClipVisualAnalysis(sourceSignature: "test", samples: [missing]) try check(!ContinuityPlanner.review(analysis, start: 0, length: 1).messages.isEmpty, "Missing face was not flagged") let bounds = ContinuityPlanner.candidates(analysis: analysis, duration: 1, length: 2, currentStart: 10) try check(bounds.allSatisfy { $0.start == 0 }, "Candidate escaped short-source bounds") try check(analysis.window(start: 100, length: 1).isEmpty, "Sparse analysis pretends to know an unsampled window") let color = FaceColor(luminance: 0.3, redToGreen: 1.02, blueToGreen: 0.98) let sample = VisualSample(time: 0, faceCount: 1, pose: frontal, color: color, quality: 1) let reference = FaceColor(luminance: 0.9, redToGreen: 2, blueToGreen: 0.1) let correction = ContinuityPlanner.colorAdjustment(samples: [sample], reference: reference, strength: 1) try check(abs(correction.brightness) <= 0.06 && correction.redGamma <= 1.06 && correction.blueGamma >= 0.94, "Unbounded color change") try check(ContinuityPlanner.colorAdjustment(samples: [sample], reference: reference, strength: 0) == ColorAdjustment(), "Zero strength changes colors") var ambiguous = sample ambiguous.faceCount = 2 try check(ContinuityPlanner.colorAdjustment(samples: [ambiguous], reference: reference, strength: 1) == ColorAdjustment(), "Ambiguous face affected colors") try check(ContinuityPlanner.colorAdjustment(samples: [missing], reference: reference, strength: 1) == ColorAdjustment(), "Background used when no face is known") let box = CGRect(x: 0.3, y: 0.3, width: 0.4, height: 0.4) let redBackground = try image(background: (1, 0, 0)) let blueBackground = try image(background: (0, 0, 1)) let redMeasurement = VisualAnalyzer.measureFaceColor(redBackground, box: box) let blueMeasurement = VisualAnalyzer.measureFaceColor(blueBackground, box: box) try check(redMeasurement != nil && redMeasurement == blueMeasurement, "Background color contaminated face measurements") func clip(height: Double) -> GrowthLapseProjectClip { var pose = frontal pose.height = height let analysis = ClipVisualAnalysis(sourceSignature: "test", samples: [VisualSample(time: 0, faceCount: 1, pose: pose, color: color, quality: 1)]) return GrowthLapseProjectClip(index: 1, sourcePath: "/tmp/source.mp4", displayName: "Test", duration: 4, width: 1920, height: 1080, ageWeeks: nil, segmentStart: 0, segmentLength: 2, normalizedClipPath: "/tmp/cache/clip.mp4", visualAnalysis: analysis, variants: nil) } let profile = ContinuityPlanner.sharedProfile(clips: [clip(height: 0.08), clip(height: 0.35)], dimensions: VideoDimensions(width: 1920, height: 1080), requested: 0.26, eyeY: 0.4) try check(profile.faceHeightRatio > 0.35 && profile.faceHeightRatio <= 0.38, "Different camera distances were not negotiated") let extreme = ContinuityPlanner.sharedProfile(clips: [clip(height: 0.08), clip(height: 0.9)], dimensions: VideoDimensions(width: 1920, height: 1080), requested: 0.26, eyeY: 0.4) try check(extreme.faceHeightRatio <= 0.38, "One extreme close-up forces excessive zoom on all clips") print("PASS joint transition planning, sparse-window handling, face-only bounded colors and camera-distance profile") } private static func image(background: (CGFloat, CGFloat, CGFloat)) throws -> CGImage { let context = CGContext(data: nil, width: 100, height: 100, bitsPerComponent: 8, bytesPerRow: 400, space: CGColorSpace(name: CGColorSpace.sRGB)!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)! context.setFillColor(CGColor(red: background.0, green: background.1, blue: background.2, alpha: 1)) context.fill(CGRect(x: 0, y: 0, width: 100, height: 100)) context.setFillColor(CGColor(gray: 0.5, alpha: 1)) context.fill(CGRect(x: 30, y: 30, width: 40, height: 40)) return context.makeImage()! } }