From 0e32b3a3af1e09d292f035b91fbb20c97ca25928 Mon Sep 17 00:00:00 2001 From: Alex Christoffer Rasmussen Date: Thu, 6 Jun 2024 22:38:52 +0200 Subject: [PATCH] Fix: link events in sheet when there is nothing to link (#1053) * create failing tests * fix missing optional chaining --- .../utils/__tests__/parserFunctions.test.ts | 130 +++++++++++++++++- apps/server/src/utils/parserFunctions.ts | 4 +- 2 files changed, 130 insertions(+), 4 deletions(-) diff --git a/apps/server/src/utils/__tests__/parserFunctions.test.ts b/apps/server/src/utils/__tests__/parserFunctions.test.ts index b0d9ab638..0d38aa920 100644 --- a/apps/server/src/utils/__tests__/parserFunctions.test.ts +++ b/apps/server/src/utils/__tests__/parserFunctions.test.ts @@ -1,5 +1,21 @@ -import { CustomFields, HttpSubscription, OscSubscription } from 'ontime-types'; -import { sanitiseCustomFields, sanitiseHttpSubscriptions, sanitiseOscSubscriptions } from '../parserFunctions.js'; +import { + CustomFields, + DatabaseModel, + EndAction, + HttpSubscription, + OntimeEvent, + OntimeRundown, + OscSubscription, + SupportedEvent, + TimeStrategy, + TimerType, +} from 'ontime-types'; +import { + parseRundown, + sanitiseCustomFields, + sanitiseHttpSubscriptions, + sanitiseOscSubscriptions, +} from '../parserFunctions.js'; describe('sanitiseOscSubscriptions()', () => { it('returns an empty array if not an array', () => { @@ -155,3 +171,113 @@ describe('sanitiseCustomFields()', () => { expect(sanitationResult).toStrictEqual(expectedCustomFields); }); }); + +describe('parseRundown() linking', () => { + const blankEvent: OntimeEvent = { + id: '', + type: SupportedEvent.Event, + cue: '', + title: '', + note: '', + endAction: EndAction.None, + timerType: TimerType.CountDown, + linkStart: null, + timeStrategy: TimeStrategy.LockDuration, + timeStart: 0, + timeEnd: 0, + duration: 0, + isPublic: false, + skip: false, + colour: '', + revision: 0, + timeWarning: 120000, + timeDanger: 60000, + custom: {}, + }; + + it('returns linked events', () => { + const data: Partial = { + rundown: [ + { + id: '1', + type: SupportedEvent.Event, + skip: false, + } as OntimeEvent, + { + id: '2', + type: SupportedEvent.Event, + linkStart: 'true', + skip: false, + } as OntimeEvent, + ], + }; + + const expected: OntimeRundown = [ + { ...blankEvent, id: '1', cue: '0' }, + { ...blankEvent, id: '2', cue: '1', linkStart: '1' }, + ]; + const result = parseRundown(data); + expect(result).toEqual(expected); + }); + + it('returns unlinkd if no previous', () => { + const data: Partial = { + rundown: [ + { + id: '2', + type: SupportedEvent.Event, + linkStart: 'true', + skip: false, + } as OntimeEvent, + ], + }; + + const expected: OntimeRundown = [{ ...blankEvent, id: '2', cue: '0' }]; + const result = parseRundown(data); + expect(result).toEqual(expected); + }); + + it('returns linked events past blocks and delays', () => { + const data: Partial = { + rundown: [ + { + id: '1', + type: SupportedEvent.Event, + skip: false, + } as OntimeEvent, + { + id: 'delay1', + type: SupportedEvent.Delay, + duration: 0, + }, + { + id: '2', + type: SupportedEvent.Event, + linkStart: 'true', + skip: false, + } as OntimeEvent, + { + id: 'block1', + type: SupportedEvent.Block, + title: '', + }, + { + id: '3', + type: SupportedEvent.Event, + linkStart: 'true', + skip: false, + } as OntimeEvent, + ], + }; + + const expected: OntimeRundown = [ + { ...blankEvent, id: '1', cue: '0' }, + { id: 'delay1', type: SupportedEvent.Delay, duration: 0 }, + { ...blankEvent, id: '2', cue: '1', linkStart: '1' }, + { id: 'block1', type: SupportedEvent.Block, title: '' }, + { ...blankEvent, id: '3', cue: '2', linkStart: '2' }, + ]; + const result = parseRundown(data); + expect(result).toEqual(expected); + }); +}); diff --git a/apps/server/src/utils/parserFunctions.ts b/apps/server/src/utils/parserFunctions.ts index 504e12644..c41267cf6 100644 --- a/apps/server/src/utils/parserFunctions.ts +++ b/apps/server/src/utils/parserFunctions.ts @@ -49,8 +49,8 @@ export const parseRundown = (data: Partial): OntimeRundown => { if (isOntimeEvent(event)) { if (event.linkStart) { - const prevEvent = getLastEvent(rundown).lastEvent; - event.linkStart = prevEvent.id; + const prevId = getLastEvent(rundown).lastEvent?.id ?? null; + event.linkStart = prevId; } newEvent = createEvent(event, eventIndex.toString()); // skip if event is invalid