Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: SPChat/Tests/CoreTests/ReplayTests.swift3// Description: Replay state machine: stepping, bounds, play / pause, speed, scrubbing, delays derived from the real gaps between recorded events, reload.4import Foundation5import Testing6@testable import SPChatCore78@Suite struct ReplayTests {9 private let t0 = Date(timeIntervalSince1970: 1_800_000_000)10 /// Gaps in seconds between consecutive events: 0.3 · 3 · 4 · 172 800 (two days) · 0.11 private var cursor: ReplayCursor { ReplayCursor(times: [0, 0.3, 3.3, 7.3, 172_807.3, 172_807.3].map { t0.addingTimeInterval($0) }) }1213 @Test func startsIdleAndStepsWithinBounds() {14 var c = cursor15 #expect(c.index == nil); #expect(!c.isPlaying); #expect(c.progress == 0); #expect(c.canStepForward); #expect(!c.canStepBack); #expect(c.delayToNext == nil)16 let a = c.stepForward(); #expect(a); #expect(c.index == 0); #expect(!c.canStepBack)17 let b = c.stepBack(); #expect(!b); #expect(c.index == 0)18 for _ in 0..<5 { c.stepForward() }19 #expect(c.index == 5); #expect(c.isAtEnd); #expect(!c.canStepForward); #expect(c.progress == 1)20 let d = c.stepForward(); #expect(!d); #expect(c.index == 5)21 let e = c.stepBack(); #expect(e); #expect(c.index == 4); #expect(c.progress == 0.8)22 }2324 @Test func delaysAreProportionalToRealGapsAndClamped() {25 var c = cursor26 c.seek(to: 0); #expect(c.gapToNext.map { abs($0 - 0.3) < 1e-6 } == true); #expect(c.delayToNext == 0.4, "0.3 s × 0.25 is under the floor")27 c.seek(to: 1); #expect(abs((c.delayToNext ?? 0) - 0.75) < 1e-6, "3 s × 0.25")28 c.seek(to: 2); #expect(abs((c.delayToNext ?? 0) - 1.0) < 1e-6, "4 s × 0.25")29 c.seek(to: 3); #expect(c.delayToNext == 2.5, "two days are clamped to the ceiling")30 c.seek(to: 4); #expect(c.delayToNext == 0.4, "simultaneous events")31 c.seek(to: 5); #expect(c.delayToNext == nil)32 c.seek(to: 2); c.speed = .x2; #expect(abs((c.delayToNext ?? 0) - 0.5) < 1e-6)33 c.speed = .x4; #expect(abs((c.delayToNext ?? 0) - 0.25) < 1e-6)34 c.seek(to: 3); #expect(c.delayToNext == 0.625)35 }3637 @Test func playPauseAndEnd() {38 var c = cursor39 c.play(); #expect(c.isPlaying); #expect(c.index == 0, "play from idle shows the first event")40 c.stepForward(); c.pause(); #expect(!c.isPlaying); #expect(c.index == 1)41 c.toggle(); #expect(c.isPlaying); #expect(c.index == 1, "resuming keeps the position")42 while c.stepForward() {}43 #expect(c.isAtEnd); #expect(!c.isPlaying, "playback stops by itself on the last event")44 c.play(); #expect(c.index == 0); #expect(c.isPlaying, "play at the end starts over")45 c.stop(); #expect(c.index == nil); #expect(!c.isPlaying)46 }4748 @Test func speedCyclesAndScrubbing() {49 var c = cursor50 #expect(c.speed == .x1); c.cycleSpeed(); #expect(c.speed == .x2); c.cycleSpeed(); #expect(c.speed == .x4); c.cycleSpeed(); #expect(c.speed == .x1)51 #expect(ReplayCursor.Speed.allCases.map(\.label) == ["1×", "2×", "4×"])52 c.seek(progress: 0.5); #expect(c.index == 3 || c.index == 2); c.seek(progress: 0.39); #expect(c.index == 2)53 c.seek(progress: -3); #expect(c.index == 0); c.seek(to: 99); #expect(c.index == 5)54 c.play(); c.stepForward(); c.seek(progress: 1); #expect(!c.isPlaying, "scrubbing to the end stops playback")55 }5657 @Test func emptyAndSingleEventTimelines() {58 var e = ReplayCursor()59 #expect(e.isEmpty); #expect(!e.canStepForward); e.play(); #expect(!e.isPlaying); #expect(e.index == nil); let moved = e.stepForward(); #expect(!moved); e.seek(progress: 0.5); #expect(e.index == nil)60 var one = ReplayCursor(times: [t0])61 one.play(); #expect(one.index == 0); #expect(!one.isPlaying); #expect(one.isAtEnd); #expect(one.progress == 1)62 }6364 @Test func missingTimestampsAndReload() {65 var c = ReplayCursor(times: [t0, nil, t0.addingTimeInterval(8)])66 c.seek(to: 0); #expect(c.gapToNext == nil); #expect(c.delayToNext == 0.4)67 c.seek(to: 2); c.reload(times: [t0, nil]); #expect(c.index == 1, "the position is pulled back when events disappeared")68 c.reload(times: [t0, nil, t0.addingTimeInterval(8), t0.addingTimeInterval(9)]); #expect(c.index == 1); #expect(c.count == 4)69 c.play(); #expect(c.isPlaying); c.reload(times: []); #expect(c.index == nil); #expect(!c.isPlaying)70 // events recorded out of order never give a negative pause71 var back = ReplayCursor(times: [t0, t0.addingTimeInterval(-50)]); back.seek(to: 0); #expect(back.gapToNext == 0); #expect(back.delayToNext == 0.4)72 }73}74