refactor: restructure model to contain an object of rundowns

This commit is contained in:
Carlos Valente
2025-03-15 09:04:33 +01:00
parent 433e9f6a4f
commit dbf8eeb7fb
111 changed files with 4366 additions and 4612 deletions
@@ -1,5 +1,11 @@
import { OntimeRundown, PlayableEvent, Playback, SupportedEvent, TimerPhase } from 'ontime-types';
import { deepmerge } from 'ontime-utils';
import { PlayableEvent, Playback, TimerPhase } from 'ontime-types';
import { initRundown } from '../../services/rundown-service/RundownService.js';
import {
makeOntimeBlock,
makeOntimeEvent,
makeRundown,
} from '../../services/rundown-service/__mocks__/rundown.mocks.js';
import {
type RuntimeState,
@@ -13,7 +19,6 @@ import {
start,
stop,
} from '../runtimeState.js';
import { initRundown } from '../../services/rundown-service/RundownService.js';
const mockEvent = {
type: 'event',
@@ -23,6 +28,7 @@ const mockEvent = {
timeEnd: 1000,
duration: 1000,
skip: false,
currentBlock: null,
} as PlayableEvent;
const mockState = {
@@ -51,11 +57,6 @@ const mockState = {
},
} as RuntimeState;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const makeMockState = (patch: RuntimeState): RuntimeState => {
return deepmerge(mockState, patch);
};
beforeAll(() => {
vi.mock('../../classes/data-provider/DataProvider.js', () => {
return {
@@ -70,23 +71,14 @@ beforeAll(() => {
});
describe('mutation on runtimeState', () => {
beforeEach(() => {
beforeEach(async () => {
clear();
vi.mock('../../services/rundown-service/RundownService.js', async (importOriginal) => {
const actual = (await importOriginal()) as object;
return {
...actual,
getPlayableEvents: vi.fn().mockReturnValue([
{
id: 'mock',
cue: 'mock',
timeStart: 0,
timeEnd: 1000,
duration: 1000,
},
]),
initRunddown: vi.fn().mockReturnValue(undefined),
};
});
});
@@ -97,15 +89,18 @@ describe('mutation on runtimeState', () => {
describe('playback operations', async () => {
it('refuses if nothing is loaded', () => {
initRundown(makeRundown({}), {});
let success = start(mockState);
expect(success).toBe(false);
success = pause();
expect(success).toBe(false);
});
test('normal playback cycle', () => {
// 1. Load event
load(mockEvent, [mockEvent]);
const mockRundown = makeRundown({ entries: { [mockEvent.id]: mockEvent }, order: [mockEvent.id] });
load(mockEvent, mockRundown, mockRundown.order);
let newState = getState();
expect(newState.eventNow?.id).toBe(mockEvent.id);
expect(newState.timer.playback).toBe(Playback.Armed);
@@ -170,17 +165,21 @@ describe('mutation on runtimeState', () => {
});
// do this before the test so that it is applied
const event1 = { ...mockEvent, id: 'event1', timeStart: 0, timeEnd: 1000, duration: 1000 };
const event2 = { ...mockEvent, id: 'event2', timeStart: 1000, timeEnd: 1500, duration: 500 };
const entries = {
event1: { ...mockEvent, id: 'event1', timeStart: 0, timeEnd: 1000, duration: 1000, currentBlock: null },
event2: { ...mockEvent, id: 'event2', timeStart: 1000, timeEnd: 1500, duration: 500, currentBlock: null },
};
const rundown = makeRundown({ entries, order: ['event1', 'event2'] });
// force update
vi.useFakeTimers();
await initRundown([event1, event2], {});
await initRundown(rundown, {});
vi.runAllTimers();
vi.useRealTimers();
test('runtime offset', async () => {
// 1. Load event
load(event1, [event1, event2]);
load(entries.event1, rundown, rundown.order);
let newState = getState();
expect(newState.runtime.actualStart).toBeNull();
expect(newState.runtime.plannedStart).toBe(0);
@@ -197,11 +196,11 @@ describe('mutation on runtimeState', () => {
}
expect(newState.runtime.actualStart).toBe(newState.clock);
expect(newState.runtime.offset).toBe(event1.timeStart - newState.clock);
expect(newState.runtime.expectedEnd).toBe(event2.timeEnd - newState.runtime.offset);
expect(newState.runtime.offset).toBe(entries.event1.timeStart - newState.clock);
expect(newState.runtime.expectedEnd).toBe(entries.event2.timeEnd - newState.runtime.offset);
// 3. Next event
load(event2, [event1, event2]);
load(entries.event2, rundown, rundown.order);
start();
newState = getState();
@@ -214,10 +213,10 @@ describe('mutation on runtimeState', () => {
const forgivingActualStart = Math.abs(newState.runtime.actualStart - firstStart);
expect(forgivingActualStart).toBeLessThanOrEqual(1);
// we are over-under, the difference between the schedule and the actual start
const delayBefore = event2.timeStart - newState.clock;
const delayBefore = entries.event2.timeStart - newState.clock;
expect(newState.runtime.offset).toBe(delayBefore);
// finish is the difference between the runtime and the schedule
expect(newState.runtime.expectedEnd).toBe(event2.timeEnd - newState.runtime.offset);
expect(newState.runtime.expectedEnd).toBe(entries.event2.timeEnd - newState.runtime.offset);
expect(newState.currentBlock.block).toBeNull();
// 4. Add time
@@ -228,7 +227,7 @@ describe('mutation on runtimeState', () => {
}
expect(newState.runtime.offset).toBe(delayBefore - 10);
expect(newState.runtime.expectedEnd).toBe(event2.timeEnd - newState.runtime.offset);
expect(newState.runtime.expectedEnd).toBe(entries.event2.timeEnd - newState.runtime.offset);
// 5. Stop event
stop();
@@ -237,8 +236,6 @@ describe('mutation on runtimeState', () => {
expect(newState.runtime.offset).toBe(0);
expect(newState.runtime.expectedEnd).toBeNull();
});
test.todo('runtime offset on timers in overtime', () => {});
});
});
@@ -253,14 +250,17 @@ describe('roll mode', () => {
});
describe('normal roll', () => {
const rundown = [
{ ...mockEvent, id: '1', timeStart: 1000, duration: 1000, timeEnd: 2000 },
{ ...mockEvent, id: '2', timeStart: 2000, duration: 1000, timeEnd: 3000 },
{ ...mockEvent, id: '3', timeStart: 3000, duration: 1000, timeEnd: 4000 },
] as PlayableEvent[];
const rundown = makeRundown({
entries: {
1: { ...mockEvent, id: '1', timeStart: 1000, duration: 1000, timeEnd: 2000 },
2: { ...mockEvent, id: '2', timeStart: 2000, duration: 1000, timeEnd: 3000 },
3: { ...mockEvent, id: '3', timeStart: 3000, duration: 1000, timeEnd: 4000 },
},
order: ['1', '2', '3'],
});
test('pending event', () => {
const { eventId, didStart } = roll(rundown);
const { eventId, didStart } = roll(rundown, rundown.order);
const state = getState();
expect(eventId).toBe('1');
@@ -271,29 +271,32 @@ describe('roll mode', () => {
test('roll events', () => {
vi.setSystemTime('jan 1 00:00:01');
let result = roll(rundown);
let result = roll(rundown, rundown.order);
expect(result).toStrictEqual({ eventId: '1', didStart: true });
vi.setSystemTime('jan 1 00:00:02');
result = roll(rundown);
result = roll(rundown, rundown.order);
expect(result).toStrictEqual({ eventId: '2', didStart: true });
vi.setSystemTime('jan 1 00:00:03:500');
result = roll(rundown);
result = roll(rundown, rundown.order);
expect(result).toStrictEqual({ eventId: '3', didStart: true });
});
});
describe('roll takover', () => {
const rundown = [
{ ...mockEvent, id: '1', timeStart: 1000, duration: 1000, timeEnd: 2000 },
{ ...mockEvent, id: '2', timeStart: 2000, duration: 1000, timeEnd: 3000 },
{ ...mockEvent, id: '3', timeStart: 3000, duration: 1000, timeEnd: 4000 },
] as PlayableEvent[];
const rundown = makeRundown({
entries: {
1: { ...mockEvent, id: '1', timeStart: 1000, duration: 1000, timeEnd: 2000 },
2: { ...mockEvent, id: '2', timeStart: 2000, duration: 1000, timeEnd: 3000 },
3: { ...mockEvent, id: '3', timeStart: 3000, duration: 1000, timeEnd: 4000 },
},
order: ['1', '2', '3'],
});
test('from load', () => {
load(rundown[2], rundown);
const result = roll(rundown);
load(rundown.entries[3] as PlayableEvent, rundown, rundown.order);
const result = roll(rundown, rundown.order);
expect(result).toStrictEqual({ eventId: '3', didStart: false });
const state = getState();
expect(state.timer.phase).toBe(TimerPhase.Pending);
@@ -301,9 +304,9 @@ describe('roll mode', () => {
});
test('from play', () => {
load(rundown[0], rundown);
load(rundown.entries[1] as PlayableEvent, rundown, rundown.order);
start();
const result = roll(rundown);
const result = roll(rundown, rundown.order);
expect(result).toStrictEqual({ eventId: '1', didStart: false });
expect(getState().runtime.offset).toBe(1000);
});
@@ -311,153 +314,167 @@ describe('roll mode', () => {
describe('roll continue with offset', () => {
test('no gaps', () => {
const rundown = [
{ ...mockEvent, id: '1', timeStart: 1000, duration: 1000, timeEnd: 2000 },
{ ...mockEvent, id: '2', timeStart: 2000, duration: 1000, timeEnd: 3000 },
{ ...mockEvent, id: '3', timeStart: 3000, duration: 1000, timeEnd: 4000 },
] as PlayableEvent[];
const rundown = makeRundown({
entries: {
1: { ...mockEvent, id: '1', timeStart: 1000, duration: 1000, timeEnd: 2000 },
2: { ...mockEvent, id: '2', timeStart: 2000, duration: 1000, timeEnd: 3000 },
3: { ...mockEvent, id: '3', timeStart: 3000, duration: 1000, timeEnd: 4000 },
},
order: ['1', '2', '3'],
});
load(rundown[0], rundown);
load(rundown.entries[1] as PlayableEvent, rundown, rundown.order);
start();
let result = roll(rundown, getState().runtime.offset);
let result = roll(rundown, rundown.order, getState().runtime.offset);
expect(result).toStrictEqual({ eventId: '1', didStart: false });
expect(getState().runtime.offset).toBe(1000);
vi.setSystemTime('jan 1 00:00:01');
result = roll(rundown, getState().runtime.offset);
result = roll(rundown, rundown.order, getState().runtime.offset);
expect(result).toStrictEqual({ eventId: '2', didStart: true });
expect(getState().runtime.offset).toBe(1000);
vi.setSystemTime('jan 1 00:00:02');
result = roll(rundown, getState().runtime.offset);
result = roll(rundown, rundown.order, getState().runtime.offset);
expect(result).toStrictEqual({ eventId: '3', didStart: true });
expect(getState().runtime.offset).toBe(1000);
});
test.todo('with gaps', () => {
//this is a bit involved as it also depends somewhat on the RintimeService
});
});
});
describe('loadBlock', () => {
test('from no-block to a block will clear startedAt', () => {
const rundown = [
{ id: '0', type: SupportedEvent.Event },
{ id: '1', type: SupportedEvent.Block },
{ id: '2', type: SupportedEvent.Event },
{ id: '3', type: SupportedEvent.Block },
{ id: '4', type: SupportedEvent.Event },
] as OntimeRundown;
const rundown = makeRundown({
entries: {
0: makeOntimeEvent({ id: '0', currentBlock: null }),
1: makeOntimeBlock({ id: '1', events: ['11'] }),
11: makeOntimeEvent({ id: '11', currentBlock: '1' }),
2: makeOntimeBlock({ id: '2', events: [] }),
3: makeOntimeEvent({ id: '3', currentBlock: null }),
},
order: ['0', '1', '2', '3'],
});
const state = {
currentBlock: {
block: null,
startedAt: 123,
},
eventNow: rundown[2],
eventNow: rundown.entries[11],
} as RuntimeState;
loadBlock(rundown, state);
expect(state).toMatchObject({
currentBlock: { block: rundown[1], startedAt: null },
eventNow: rundown[2],
currentBlock: { block: rundown.entries[1], startedAt: null },
eventNow: rundown.entries[11],
});
});
test('from block to a different block will clear startedAt', () => {
const rundown = [
{ id: '0', type: SupportedEvent.Event },
{ id: '1', type: SupportedEvent.Block },
{ id: '2', type: SupportedEvent.Event },
{ id: '3', type: SupportedEvent.Block },
{ id: '4', type: SupportedEvent.Event },
] as OntimeRundown;
const rundown = makeRundown({
entries: {
0: makeOntimeEvent({ id: '0', currentBlock: null }),
1: makeOntimeBlock({ id: '1', events: ['11'] }),
11: makeOntimeEvent({ id: '11', currentBlock: '1' }),
2: makeOntimeBlock({ id: '2', events: ['22'] }),
22: makeOntimeEvent({ id: '22', currentBlock: '2' }),
},
order: ['0', '1', '2'],
});
const state = {
currentBlock: {
block: rundown[1],
block: rundown.entries[1],
startedAt: 123,
},
eventNow: rundown[4],
eventNow: rundown.entries[22],
} as RuntimeState;
loadBlock(rundown, state);
expect(state).toMatchObject({
currentBlock: { block: rundown[3], startedAt: null },
eventNow: rundown[4],
currentBlock: { block: rundown.entries[2], startedAt: null },
eventNow: rundown.entries[22],
});
});
test('from block to a no-block will clear startedAt', () => {
const rundown = [
{ id: '0', type: SupportedEvent.Event },
{ id: '1', type: SupportedEvent.Block },
{ id: '2', type: SupportedEvent.Event },
{ id: '3', type: SupportedEvent.Block },
{ id: '4', type: SupportedEvent.Event },
] as OntimeRundown;
const rundown = makeRundown({
entries: {
0: makeOntimeEvent({ id: '0', currentBlock: null }),
1: makeOntimeBlock({ id: '1', events: ['11'] }),
11: makeOntimeEvent({ id: '11', currentBlock: '1' }),
2: makeOntimeBlock({ id: '2', events: ['22'] }),
22: makeOntimeEvent({ id: '22', currentBlock: '2' }),
},
order: ['0', '1', '2'],
});
const state = {
currentBlock: {
block: rundown[1],
block: rundown.entries[1],
startedAt: 123,
},
eventNow: rundown[0],
eventNow: rundown.entries[0],
} as RuntimeState;
loadBlock(rundown, state);
expect(state).toMatchObject({
currentBlock: { block: null, startedAt: null },
eventNow: rundown[0],
eventNow: rundown.entries[0],
});
});
test('from block to same block will keep startedAt', () => {
const rundown = [
{ id: '0', type: SupportedEvent.Block },
{ id: '1', type: SupportedEvent.Event },
{ id: '2', type: SupportedEvent.Event },
] as OntimeRundown;
const rundown = makeRundown({
entries: {
0: makeOntimeBlock({ id: '0', events: ['1', '2'] }),
1: makeOntimeEvent({ id: '1', currentBlock: '0' }),
2: makeOntimeEvent({ id: '2', currentBlock: '0' }),
},
order: ['0'],
});
const state = {
currentBlock: {
block: rundown[0],
block: rundown.entries[0],
startedAt: 123,
},
eventNow: rundown[2],
eventNow: rundown.entries[2],
} as RuntimeState;
loadBlock(rundown, state);
expect(state).toMatchObject({
currentBlock: { block: rundown[0], startedAt: 123 },
eventNow: rundown[2],
currentBlock: { block: rundown.entries[0], startedAt: 123 },
eventNow: rundown.entries[2],
});
});
test('from no-block to no-block will keep startedAt', () => {
const rundown = [
{ id: '0', type: SupportedEvent.Event },
{ id: '1', type: SupportedEvent.Event },
] as OntimeRundown;
const rundown = makeRundown({
entries: {
0: makeOntimeEvent({ id: '0', currentBlock: null }),
1: makeOntimeEvent({ id: '1', currentBlock: null }),
},
order: ['0', '1'],
});
const state = {
currentBlock: {
block: null,
startedAt: 123,
},
eventNow: rundown[0],
eventNow: rundown.entries[0],
} as RuntimeState;
loadBlock(rundown, state);
expect(state).toMatchObject({
currentBlock: { block: null, startedAt: 123 },
eventNow: rundown[0],
eventNow: rundown.entries[0],
});
});
});