fix: skip handles events across midnight

This commit is contained in:
Carlos Valente
2026-03-04 01:21:26 +01:00
committed by Carlos Valente
parent 155cf48a17
commit 27b179276d
2 changed files with 47 additions and 8 deletions
@@ -1,4 +1,4 @@
import { dayInMs, MILLIS_PER_HOUR, millisToString } from 'ontime-utils';
import { dayInMs, MILLIS_PER_HOUR, MILLIS_PER_MINUTE, MILLIS_PER_SECOND, millisToString } from 'ontime-utils';
import { EndAction, Playback, TimeStrategy, TimerPhase, TimerType } from 'ontime-types';
import {
@@ -693,6 +693,44 @@ describe('skippedOutOfEvent()', () => {
state.clock -= testSkipLimit + 1;
expect(skippedOutOfEvent(state, previousTime, testSkipLimit)).toBe(true);
});
it('handles events that cross midnight', () => {
const startedAt = 23 * MILLIS_PER_HOUR + 50 * MILLIS_PER_MINUTE; // 23:50
const expectedFinish = 1 * MILLIS_PER_HOUR + 50 * MILLIS_PER_MINUTE; // 01:50
const clock = 14 * MILLIS_PER_MINUTE + 47 * MILLIS_PER_SECOND; // 00:14:47
const previousTime = clock - 1021; // ~1 second ago
const state = {
clock,
timer: {
expectedFinish,
startedAt,
},
} as RuntimeState;
// Even though clock < startedAt numerically (00:14 < 23:50),
// we're inside the overnight event, so this should NOT be a skip
expect(skippedOutOfEvent(state, previousTime, 1000)).toBe(false);
});
it('correctly detects skip out of an event that crosses midnight', () => {
// Event 23:50-01:50, clock jumps to 02:00 (outside event)
const startedAt = 23 * MILLIS_PER_HOUR + 50 * MILLIS_PER_MINUTE; // 23:50
const expectedFinish = 1 * MILLIS_PER_HOUR + 50 * MILLIS_PER_MINUTE; // 01:50
const previousTime = 1 * MILLIS_PER_HOUR + 49 * MILLIS_PER_MINUTE; // 01:49
const clock = 2 * MILLIS_PER_HOUR; // 02:00 (outside event)
const state = {
clock,
timer: {
expectedFinish,
startedAt,
},
} as RuntimeState;
// Clock jumped from 01:49 to 02:00 (11 min skip) and is now outside the event
expect(skippedOutOfEvent(state, previousTime, 1000)).toBe(true);
});
});
test('normaliseEndTime()', () => {
+8 -7
View File
@@ -1,5 +1,5 @@
import { MaybeNumber, TimerPhase } from 'ontime-types';
import { dayInMs, isPlaybackActive, MILLIS_PER_HOUR } from 'ontime-utils';
import { checkIsNow, dayInMs, isPlaybackActive, MILLIS_PER_HOUR } from 'ontime-utils';
import type { RuntimeState } from '../stores/runtimeState.js';
@@ -98,14 +98,15 @@ export function skippedOutOfEvent(state: RuntimeState, previousTime: number, ski
const { startedAt, expectedFinish } = state.timer;
const { clock } = state;
const hasPassedMidnight = previousTime > dayInMs - skipLimit && clock < skipLimit;
const adjustedClock = hasPassedMidnight ? clock + dayInMs : clock;
const isInsideEvent = checkIsNow(startedAt, expectedFinish, clock);
if (isInsideEvent) return false;
const timeDifference = previousTime - adjustedClock;
const hasSkipped = Math.abs(timeDifference) > skipLimit;
const adjustedExpectedFinish = expectedFinish >= startedAt ? expectedFinish : expectedFinish + dayInMs;
// we are outside the event, but we need to check if we skipped or just finished normally
const timeFromPrevious = Math.abs(previousTime - clock);
// account for midnight when checking skips
const hasSkipped = Math.min(timeFromPrevious, dayInMs - timeFromPrevious) > skipLimit;
return hasSkipped && (adjustedClock > adjustedExpectedFinish || adjustedClock < startedAt);
return hasSkipped;
}
/**