From 652fe25fd1d9e620f61226c3ff48d097ee99dffb Mon Sep 17 00:00:00 2001 From: Carlos Valente Date: Tue, 3 Sep 2024 21:28:10 +0200 Subject: [PATCH] fix: issue with roll evaluating multiple days --- .../src/services/__tests__/rollUtils.test.ts | 42 ++++++++++++++++++- apps/server/src/services/rollUtils.ts | 17 +------- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/apps/server/src/services/__tests__/rollUtils.test.ts b/apps/server/src/services/__tests__/rollUtils.test.ts index 9612fe481..1d95e9b8b 100644 --- a/apps/server/src/services/__tests__/rollUtils.test.ts +++ b/apps/server/src/services/__tests__/rollUtils.test.ts @@ -1,4 +1,4 @@ -import { OntimeEvent, SupportedEvent } from 'ontime-types'; +import { isOntimeEvent, OntimeEvent, SupportedEvent } from 'ontime-types'; import { MILLIS_PER_HOUR, MILLIS_PER_MINUTE } from 'ontime-utils'; import { loadRoll } from '../rollUtils.js'; @@ -246,6 +246,43 @@ describe('loadRoll() handle edge cases with midnight', () => { }); }); +describe('loadRoll() handle rundowns with several days', () => { + it('should find the correct event, when we have many days', () => { + const now = 11 * MILLIS_PER_HOUR + 30 * MILLIS_PER_MINUTE; + const timedEvents = [ + { + id: '0', + timeStart: 10 * MILLIS_PER_HOUR, + timeEnd: 11 * MILLIS_PER_HOUR, + }, + { + id: '2', + timeStart: 11 * MILLIS_PER_HOUR, + timeEnd: 12 * MILLIS_PER_HOUR, + }, + { + id: '3', + timeStart: 12 * MILLIS_PER_HOUR, + timeEnd: 13 * MILLIS_PER_HOUR, + }, + { + id: '4', + timeStart: 11 * MILLIS_PER_HOUR, + timeEnd: 12 * MILLIS_PER_HOUR, + }, + ]; + + const state = loadRoll(prepareTimedEvents(timedEvents), now); + const expected = { + event: timedEvents[1], + index: 1, + }; + expect(state).toMatchObject(expected); + }); + + }); +}); + describe('loadRoll() handle edge cases with before and after start', () => { it('should prepare first event, if we are not yet in the rundown start', () => { const now = 7 * MILLIS_PER_HOUR; @@ -302,7 +339,7 @@ describe('loadRoll() handle edge cases with before and after start', () => { index: 0, }; const state = loadRoll(singleEventList, now); - expect(state.isPending).toBeUndefined(); + expect(state.isPending).toBeUndefined(); // we are playing the event expect(state).toStrictEqual(expected); }); @@ -443,6 +480,7 @@ describe('loadRoll() test that roll behaviour multi day event edge cases', () => }; const state = loadRoll(eventlist, now); + expect(state.isPending).toBeUndefined(); // we are playing the event expect(state).toStrictEqual(expected); }); }); diff --git a/apps/server/src/services/rollUtils.ts b/apps/server/src/services/rollUtils.ts index 862753c13..1962f2e55 100644 --- a/apps/server/src/services/rollUtils.ts +++ b/apps/server/src/services/rollUtils.ts @@ -15,26 +15,11 @@ export function loadRoll( isPending?: boolean; } { const { firstEvent } = getFirstEvent(timedEvents); - const { lastEvent } = getLastEvent(timedEvents); - if (!firstEvent || !lastEvent) { + if (!firstEvent) { return { event: null, index: null }; } - // check that the rundown wraps around midnight - const wrapsAroundMidnight = firstEvent.timeStart > lastEvent.timeEnd; - - if (!wrapsAroundMidnight) { - // check whether we are before or after the rundown - const lastNormalEnd = normaliseEndTime(lastEvent.timeStart, lastEvent.timeEnd); - const isAfterRundown = timeNow > lastNormalEnd; - const isBeforeRundown = timeNow < firstEvent.timeStart && !isAfterRundown; - - if (isAfterRundown || isBeforeRundown) { - return { event: firstEvent, index: 0, isPending: true }; - } - } - // we know we are in the middle of the rundown and we need to find the current event // account for number of times we went over midnight let daySpan = 0;