|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import type { Session, Telemetry } from '@irdashies/types'; |
| 3 | +import { CarSpeedsProcessor } from './CarSpeedsProcessor'; |
| 4 | + |
| 5 | +const session = (trackLength = '1 km') => |
| 6 | + ({ WeekendInfo: { TrackLength: trackLength } }) as Session; |
| 7 | + |
| 8 | +const frame = (lapDistPct: number[], sessionTime: number, sessionNum = 1) => |
| 9 | + ({ |
| 10 | + CarIdxLapDistPct: { value: lapDistPct }, |
| 11 | + SessionTime: { value: [sessionTime] }, |
| 12 | + SessionNum: { value: [sessionNum] }, |
| 13 | + }) as unknown as Telemetry; |
| 14 | + |
| 15 | +describe('CarSpeedsProcessor', () => { |
| 16 | + it('derives smoothed km/h speeds at 10 Hz', () => { |
| 17 | + const processor = new CarSpeedsProcessor(); |
| 18 | + processor.init(session()); |
| 19 | + processor.onFrame(frame([0.1], 1)); |
| 20 | + processor.onFrame(frame([0.11], 1.05)); |
| 21 | + expect(processor.snapshot().carSpeeds).toEqual([0]); |
| 22 | + processor.onFrame(frame([0.11], 1.1)); |
| 23 | + expect(processor.snapshot().carSpeeds[0]).toBe(360); |
| 24 | + processor.onFrame(frame([0.125], 1.2)); |
| 25 | + expect(processor.snapshot().carSpeeds[0]).toBe(450); |
| 26 | + }); |
| 27 | + |
| 28 | + it('handles start-finish wrap-around', () => { |
| 29 | + const processor = new CarSpeedsProcessor(); |
| 30 | + processor.init(session()); |
| 31 | + processor.onFrame(frame([0.99], 1)); |
| 32 | + processor.onFrame(frame([0.01], 1.2)); |
| 33 | + expect(processor.snapshot().carSpeeds[0]).toBe(360); |
| 34 | + }); |
| 35 | + |
| 36 | + it('keeps speeds aligned when the driver array grows', () => { |
| 37 | + const processor = new CarSpeedsProcessor(); |
| 38 | + processor.init(session()); |
| 39 | + processor.onFrame(frame([0.1], 1)); |
| 40 | + processor.onFrame(frame([0.11], 1.1)); |
| 41 | + processor.onFrame(frame([0.12, 0.5], 1.2)); |
| 42 | + expect(processor.snapshot().carSpeeds).toEqual([360, 0]); |
| 43 | + }); |
| 44 | + |
| 45 | + it('resets on an in-frame session change', () => { |
| 46 | + const processor = new CarSpeedsProcessor(); |
| 47 | + processor.init(session()); |
| 48 | + processor.onFrame(frame([0.1], 1)); |
| 49 | + processor.onFrame(frame([0.11], 1.1)); |
| 50 | + processor.onFrame(frame([0.2], 2, 2)); |
| 51 | + expect(processor.snapshot()).toMatchObject({ |
| 52 | + carSpeeds: [0], |
| 53 | + sessionNum: 2, |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('clears on disconnect and suppresses replay scrubbing', () => { |
| 58 | + const processor = new CarSpeedsProcessor(); |
| 59 | + processor.init(session()); |
| 60 | + processor.onFrame(frame([0.1], 1)); |
| 61 | + processor.onLifecycle({ type: 'disconnect' }); |
| 62 | + processor.onFrame(frame([0.2], 2)); |
| 63 | + expect(processor.snapshot().carSpeeds).toEqual([]); |
| 64 | + |
| 65 | + processor.onLifecycle({ type: 'enter', replay: true }); |
| 66 | + processor.onFrame(frame([0.3], 3)); |
| 67 | + expect(processor.snapshot().carSpeeds).toEqual([]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('emits zero speeds until session track length is available', () => { |
| 71 | + const processor = new CarSpeedsProcessor(); |
| 72 | + processor.onFrame(frame([0.1, 0.2], 1)); |
| 73 | + expect(processor.snapshot().carSpeeds).toEqual([0, 0]); |
| 74 | + }); |
| 75 | +}); |
0 commit comments