|
| 1 | +import XCTest |
| 2 | +@testable import CineScreen |
| 3 | + |
| 4 | +final class RenderSnapshotTests: XCTestCase { |
| 5 | + func testProximityFactorBinarySearch() { |
| 6 | + let times = [1000.0, 2000.0] |
| 7 | + XCTAssertEqual(RenderSnapshot.proximityFactor(to: times, at: 1000, window: 140), 0) |
| 8 | + XCTAssertEqual(RenderSnapshot.proximityFactor(to: times, at: 1070, window: 140), 0.5, accuracy: 1e-9) |
| 9 | + XCTAssertEqual(RenderSnapshot.proximityFactor(to: times, at: 1500, window: 140), 1) |
| 10 | + XCTAssertEqual(RenderSnapshot.proximityFactor(to: times, at: 500, window: 140), 1) |
| 11 | + XCTAssertEqual(RenderSnapshot.proximityFactor(to: times, at: 2010, window: 140), 10.0 / 140.0, accuracy: 1e-9) |
| 12 | + XCTAssertEqual(RenderSnapshot.proximityFactor(to: [], at: 0, window: 140), 1) |
| 13 | + } |
| 14 | + |
| 15 | + /// The smoothing collapse keys off mouse-DOWNs only — an up (e.g. a drag |
| 16 | + /// release, often far from any ring) must not snap the sprite. |
| 17 | + func testMouseUpsDoNotCollapseSmoothing() { |
| 18 | + let metadata = Fixtures.metadata( |
| 19 | + keyframes: [Fixtures.keyframe(0, 100, 100), Fixtures.keyframe(5000, 100, 100)], |
| 20 | + clicks: [Fixtures.click(1000, action: .down), Fixtures.click(1600, action: .up)] |
| 21 | + ) |
| 22 | + let snapshot = RenderSnapshot(metadata: metadata, zoomSections: []) |
| 23 | + XCTAssertEqual(snapshot.adaptiveCursorSmoothTime(atMilliseconds: 1000), 0, accuracy: 1e-9) |
| 24 | + XCTAssertEqual( |
| 25 | + snapshot.adaptiveCursorSmoothTime(atMilliseconds: 1600), |
| 26 | + CursorAnimationStyle.mellow.smoothTime, |
| 27 | + accuracy: 1e-9 |
| 28 | + ) |
| 29 | + } |
| 30 | + |
| 31 | + /// A sprite trailing far behind a stationary pointer must tighten — |
| 32 | + /// the move-then-hover case, where speed alone reads as calm. |
| 33 | + func testLagUrgencyTightensSmoothTime() { |
| 34 | + let metadata = Fixtures.metadata( |
| 35 | + keyframes: [Fixtures.keyframe(0, 0, 0), Fixtures.keyframe(5000, 0, 0)] |
| 36 | + ) |
| 37 | + let snapshot = RenderSnapshot(metadata: metadata, zoomSections: []) |
| 38 | + let calm = snapshot.adaptiveCursorSmoothTime(atMilliseconds: 2500, spriteAt: SIMD2(0, 0)) |
| 39 | + let lagging = snapshot.adaptiveCursorSmoothTime(atMilliseconds: 2500, spriteAt: SIMD2(500, 0)) |
| 40 | + XCTAssertLessThan(lagging, calm) |
| 41 | + XCTAssertEqual(lagging, CursorAnimationStyle.mellow.minSmoothTime, accuracy: 1e-9) |
| 42 | + } |
| 43 | + |
| 44 | + func testZoomStateRampAndIdentity() { |
| 45 | + let metadata = Fixtures.metadata( |
| 46 | + keyframes: [Fixtures.keyframe(0, 1000, 500), Fixtures.keyframe(10_000, 1000, 500)], |
| 47 | + sections: [Fixtures.section(1000, 5000)] |
| 48 | + ) |
| 49 | + let snapshot = RenderSnapshot(metadata: metadata, zoomSections: metadata.zoom.sections) |
| 50 | + XCTAssertEqual(snapshot.zoomState(atMilliseconds: 500).scale, 1.0) |
| 51 | + XCTAssertEqual(snapshot.zoomState(atMilliseconds: 1000).scale, 1.0, accuracy: 1e-4) |
| 52 | + XCTAssertEqual(snapshot.zoomState(atMilliseconds: 3000).scale, 2.0, accuracy: 1e-4) |
| 53 | + XCTAssertEqual(snapshot.zoomState(atMilliseconds: 6000).scale, 1.0) |
| 54 | + } |
| 55 | + |
| 56 | + /// The pan lookup table is binary-searched by time — it must be |
| 57 | + /// monotonic no matter how many sections feed it. |
| 58 | + func testPanSamplesAreMonotonic() { |
| 59 | + let metadata = Fixtures.metadata( |
| 60 | + keyframes: stride(from: 0.0, through: 10_000, by: 50).map { |
| 61 | + Fixtures.keyframe($0, 500 + $0 / 10, 400) |
| 62 | + }, |
| 63 | + sections: [Fixtures.section(1000, 4000), Fixtures.section(6000, 9000)] |
| 64 | + ) |
| 65 | + let snapshot = RenderSnapshot(metadata: metadata, zoomSections: metadata.zoom.sections) |
| 66 | + XCTAssertFalse(snapshot.panSamples.isEmpty) |
| 67 | + for pair in zip(snapshot.panSamples, snapshot.panSamples.dropFirst()) { |
| 68 | + XCTAssertLessThanOrEqual(pair.0.t, pair.1.t) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// Defensive sort: old metadata files may carry sections in drag order. |
| 73 | + func testInitSortsSections() { |
| 74 | + let metadata = Fixtures.metadata( |
| 75 | + sections: [Fixtures.section(6000, 9000), Fixtures.section(1000, 4000)] |
| 76 | + ) |
| 77 | + let snapshot = RenderSnapshot(metadata: metadata, zoomSections: metadata.zoom.sections) |
| 78 | + XCTAssertEqual(snapshot.zoomSections.map(\.startTime), [1000, 6000]) |
| 79 | + } |
| 80 | + |
| 81 | + func testRawCursorPositionInterpolatesAndClamps() { |
| 82 | + let metadata = Fixtures.metadata( |
| 83 | + keyframes: [Fixtures.keyframe(0, 0, 0), Fixtures.keyframe(1000, 100, 200)] |
| 84 | + ) |
| 85 | + let mid = RenderSnapshot.rawCursorPosition(atMilliseconds: 500, metadata: metadata) |
| 86 | + XCTAssertEqual(Double(mid?.x ?? -1), 50, accuracy: 0.001) |
| 87 | + XCTAssertEqual(Double(mid?.y ?? -1), 100, accuracy: 0.001) |
| 88 | + let past = RenderSnapshot.rawCursorPosition(atMilliseconds: 5000, metadata: metadata) |
| 89 | + XCTAssertEqual(Double(past?.x ?? -1), 100, accuracy: 0.001) |
| 90 | + XCTAssertNil(RenderSnapshot.rawCursorPosition(atMilliseconds: 0, metadata: Fixtures.metadata())) |
| 91 | + } |
| 92 | +} |
0 commit comments