Skip to content

Commit

Permalink
refactor: revise logic and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cpvalente committed Jul 14, 2023
1 parent 0a0ec86 commit c141711
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 17 deletions.
20 changes: 9 additions & 11 deletions apps/server/src/services/rundown-service/RundownService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,13 @@ export function calculateRuntimeDelaysFrom(eventId: string, rundown: OntimeRundo
const updatedRundown = [...rundown];

for (let i = index; i < rundown.length; i++) {
const event = rundown[index];
const event = rundown[i];
if (event.type === SupportedEvent.Delay) {
accumulatedDelay += event.duration;
} else if (event.type === SupportedEvent.Block) {
break;
} else if (event.type === SupportedEvent.Event) {
updatedRundown[index] = {
updatedRundown[i] = {
...event,
delay: accumulatedDelay,
};
Expand All @@ -396,23 +396,21 @@ export function calculateRuntimeDelaysFrom(eventId: string, rundown: OntimeRundo
}

export function getDelayAt(eventIndex: number, rundown: OntimeRundown): number {
const event = rundown[eventIndex];

if (eventIndex < 0) {
if (eventIndex < 1) {
return 0;
}

// we need to check the event before
const event = rundown[eventIndex - 1];

if (event.type === SupportedEvent.Delay) {
return event.duration + getDelayAt(eventIndex - 1, rundown);
}

if (event.type === SupportedEvent.Block) {
} else if (event.type === SupportedEvent.Block) {
return 0;
}

if (event.type === SupportedEvent.Event) {
} else if (event.type === SupportedEvent.Event) {
return event.delay ?? 0;
}
return 0;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EndAction, OntimeRundown, SupportedEvent, TimerType } from 'ontime-types';
import { _applyDelay, calculateRuntimeDelays, getDelayAt } from '../RundownService.js';
import { _applyDelay, calculateRuntimeDelays, calculateRuntimeDelaysFrom, getDelayAt } from '../RundownService.js';

describe('applyDelay()', () => {
it('applies its duration to following events', () => {
Expand Down Expand Up @@ -599,18 +599,163 @@ describe('getDelayAt()', () => {

expect(delayAtStart).toBe(0);
expect(delayOnFirstEvent).toBe(600000);
expect(delayOnSecondEvent).toBe(1800000);
expect(delayOnSecondEvent).toBe(600000 + 1200000);
expect(delayOnBlockedEvent).toBe(0);
});
it('finds delay before a delay block', () => {
const valueOnFirstDelayBlock = getDelayAt(1, delayedRundown);
const valueOnSecondDelayBlock = getDelayAt(3, delayedRundown);
const valueAfterSecondDelayBlock = getDelayAt(4, delayedRundown);

expect(valueOnFirstDelayBlock).toBe(600000);
expect(valueOnSecondDelayBlock).toBe(1800000);
expect(valueOnFirstDelayBlock).toBe(0);
expect(valueOnSecondDelayBlock).toBe(600000);
expect(valueAfterSecondDelayBlock).toBe(600000 + 1200000);
});
it('returns 0 on blocks', () => {
const valueOnBlock = getDelayAt(5, delayedRundown);
it('returns 0 after blocks', () => {
const valueOnBlock = getDelayAt(6, delayedRundown);
expect(valueOnBlock).toBe(0);
});
});

describe('calculateRuntimeDelaysFrom()', () => {
it('updates delays from given id', () => {
const delayedRundown: OntimeRundown = [
{
title: '',
subtitle: '',
presenter: '',
note: '',
endAction: EndAction.None,
timerType: TimerType.CountDown,
timeStart: 600000,
timeEnd: 1200000,
duration: 600000,
isPublic: true,
skip: false,
colour: '',
user0: '',
user1: '',
user2: '',
user3: '',
user4: '',
user5: '',
user6: '',
user7: '',
user8: '',
user9: '',
type: SupportedEvent.Event,
revision: 0,
id: '659e1',
delay: 0,
},
{
duration: 600000,
type: SupportedEvent.Delay,
revision: 0,
id: '07986',
},
{
title: '',
subtitle: '',
presenter: '',
note: '',
endAction: EndAction.None,
timerType: TimerType.CountDown,
timeStart: 1200000,
timeEnd: 1200000,
duration: 0,
isPublic: true,
skip: false,
colour: '',
user0: '',
user1: '',
user2: '',
user3: '',
user4: '',
user5: '',
user6: '',
user7: '',
user8: '',
user9: '',
type: SupportedEvent.Event,
revision: 0,
id: '1c48f',
delay: 0,
},
{
duration: 1200000,
type: SupportedEvent.Delay,
revision: 0,
id: '7db42',
},
{
title: '',
subtitle: '',
presenter: '',
note: '',
endAction: EndAction.None,
timerType: TimerType.CountDown,
timeStart: 600000,
timeEnd: 1200000,
duration: 600000,
isPublic: true,
skip: false,
colour: '',
user0: '',
user1: '',
user2: '',
user3: '',
user4: '',
user5: '',
user6: '',
user7: '',
user8: '',
user9: '',
type: SupportedEvent.Event,
revision: 0,
id: 'd48c2',
delay: 1800000,
},
{
title: '',
type: SupportedEvent.Block,
id: '9870d',
},
{
title: '',
subtitle: '',
presenter: '',
note: '',
endAction: EndAction.None,
timerType: TimerType.CountDown,
timeStart: 1200000,
timeEnd: 1800000,
duration: 600000,
isPublic: true,
skip: false,
colour: '',
user0: '',
user1: '',
user2: '',
user3: '',
user4: '',
user5: '',
user6: '',
user7: '',
user8: '',
user9: '',
type: SupportedEvent.Event,
revision: 0,
id: '2f185',
delay: 0,
},
];

const updatedRundown = calculateRuntimeDelaysFrom('07986', delayedRundown);

// we only update from the 4th on
expect(updatedRundown[0].delay).toBe(0);
// 1 + 3
expect(updatedRundown[4].delay).toBe(600000 + 1200000);
});
});

0 comments on commit c141711

Please sign in to comment.