fix: delays account for midnight

This commit is contained in:
Carlos Valente
2026-07-14 13:06:35 +02:00
parent 0ff226f0ec
commit e1f6b22d19
4 changed files with 82 additions and 13 deletions
@@ -506,6 +506,7 @@ describe('getCurrent()', () => {
timeStart: 79200000, // 22:00:00
timeEnd: 600000, // 00:10:00
countToEnd: true,
dayOffset: 0,
},
clock: 79500000, // 22:05:00
timer: {
@@ -516,6 +517,7 @@ describe('getCurrent()', () => {
rundown: {
actualStart: 79200000,
plannedEnd: 600000,
currentDay: 0,
},
_timer: {
pausedAt: null,
@@ -527,6 +529,35 @@ describe('getCurrent()', () => {
expect(current).toBe(dayInMs - 79500000 + 600000);
});
it('handles overnight count-to-end after midnight', () => {
const state = {
eventNow: {
timeStart: 23 * MILLIS_PER_HOUR, // 23:00:00
timeEnd: 1 * MILLIS_PER_HOUR, // 01:00:00
countToEnd: true,
dayOffset: 0,
},
clock: 30 * MILLIS_PER_MINUTE, // 00:30:00 on day 1
timer: {
addedTime: 0,
duration: Infinity,
startedAt: 23 * MILLIS_PER_HOUR,
},
rundown: {
actualStart: 23 * MILLIS_PER_HOUR,
plannedEnd: 1 * MILLIS_PER_HOUR,
currentDay: 1,
},
_timer: {
pausedAt: null,
hasFinished: false,
},
} as RuntimeState;
const current = getCurrent(state);
expect(current).toBe(30 * MILLIS_PER_MINUTE);
});
it('handles events that were started late', () => {
const state = {
clock: 82000000, // 22:46:40 <--- starting 16 min after the scheduled end
+5 -3
View File
@@ -77,9 +77,11 @@ export function getCurrent(state: RuntimeState): number {
if (countToEnd) {
// count to end runs to its fixed end, so added time does not stretch the countdown
const isEventOverMidnight = timeStart > timeEnd;
const correctDay = isEventOverMidnight ? dayInMs : 0;
return correctDay - clock + timeEnd;
const dayOffset = state.eventNow.dayOffset ?? 0;
const currentDay =
state.rundown.currentDay ?? (timeStart > timeEnd && clock <= timeEnd ? dayOffset + 1 : dayOffset);
const endDay = timeStart > timeEnd ? dayOffset + 1 : dayOffset;
return timeEnd + endDay * dayInMs - (clock + currentDay * dayInMs);
}
if (startedAt === null) {