refactor: extract midnight cross rules

This commit is contained in:
Carlos Valente
2026-03-03 20:44:36 +01:00
committed by Carlos Valente
parent 5a5450d7ef
commit 2ef0b81743
3 changed files with 34 additions and 4 deletions
@@ -1,5 +1,5 @@
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 { EndAction, Playback, TimeOfDay, TimeStrategy, TimerPhase, TimerType } from 'ontime-types';
import {
findDayOffset,
@@ -7,6 +7,7 @@ import {
getExpectedFinish,
getRuntimeOffset,
getTimerPhase,
hasCrossedMidnight,
normaliseEndTime,
skippedOutOfEvent,
} from '../timerUtils.js';
@@ -537,6 +538,25 @@ describe('getExpectedFinish() and getCurrentTime() combined', () => {
});
});
describe('hasCrossedMidnight()', () => {
it('returns true when clock wraps from late to early', () => {
const previous = (23 * MILLIS_PER_HOUR + 59 * MILLIS_PER_MINUTE) as TimeOfDay; // 23:59
const current = (1 * MILLIS_PER_MINUTE) as TimeOfDay; // 00:01
expect(hasCrossedMidnight(previous, current)).toBe(true);
});
it('returns false when clock moves forward on the same day', () => {
const previous = (10 * MILLIS_PER_HOUR) as TimeOfDay; // 10:00
const current = (10 * MILLIS_PER_HOUR + 5 * MILLIS_PER_MINUTE) as TimeOfDay; // 10:05
expect(hasCrossedMidnight(previous, current)).toBe(false);
});
it('returns false when clock value is unchanged', () => {
const time = (15 * MILLIS_PER_HOUR) as TimeOfDay; // 15:00
expect(hasCrossedMidnight(time, time)).toBe(false);
});
});
describe('skippedOutOfEvent()', () => {
const testSkipLimit = 32;
it('does not consider an event end as a skip', () => {
+10 -1
View File
@@ -1,4 +1,4 @@
import { Day, MaybeNumber, TimerPhase } from 'ontime-types';
import { Day, MaybeNumber, TimeOfDay, TimerPhase } from 'ontime-types';
import { checkIsNow, dayInMs, isPlaybackActive, MILLIS_PER_HOUR } from 'ontime-utils';
import type { RuntimeState } from '../stores/runtimeState.js';
@@ -8,6 +8,15 @@ import type { RuntimeState } from '../stores/runtimeState.js';
*/
export const normaliseEndTime = (start: number, end: number) => (end < start ? end + dayInMs : end);
/**
* Checks whether the local wall clock wrapped into a new day
* This currently uses a simple wrap heuristic and is centralized
* so day-boundary behavior can be evolved in one place later.
*/
export function hasCrossedMidnight(previous: TimeOfDay, current: TimeOfDay): boolean {
return previous > current;
}
/**
* Calculates expected finish time of a running timer
* @param {RuntimeState} state runtime state
+3 -2
View File
@@ -35,6 +35,7 @@ import {
getExpectedFinish,
getRuntimeOffset,
getTimerPhase,
hasCrossedMidnight,
} from '../services/timerUtils.js';
import { loadRoll, normaliseRollStart } from '../services/rollUtils.js';
import { timerConfig } from '../setup/config.js';
@@ -555,8 +556,8 @@ export function update(): UpdateResult {
// 2. are we waiting to roll?
if (runtimeState.timer.playback === Playback.Roll && runtimeState.timer.secondaryTimer !== null) {
const hasCrossedMidnight = previousClock > now;
return updateIfWaitingToRoll(hasCrossedMidnight);
const clockHasCrossedMidnight = hasCrossedMidnight(previousClock, now);
return updateIfWaitingToRoll(clockHasCrossedMidnight);
}
// 3. at this point we know that we are playing an event