fix: add event (#499)

* refactor: remove guards on event creation

* style: minimum safe block size

* fix: insert after finds previous valid cue
This commit is contained in:
Carlos Valente
2023-08-26 21:45:51 +02:00
committed by GitHub
parent af99882919
commit cd9dbcdc68
11 changed files with 119 additions and 27 deletions
+20 -2
View File
@@ -1,4 +1,4 @@
import { OntimeRundown, SupportedEvent } from 'ontime-types';
import { OntimeDelay, OntimeEvent, OntimeRundown, SupportedEvent } from 'ontime-types';
import { getCueCandidate, getIncrement, sanitiseCue } from './cueUtils.js';
@@ -32,7 +32,7 @@ describe('getIncrement()', () => {
});
});
describe('findCueName()', () => {
describe('getCueCandidate()', () => {
describe('in the beginning of the rundown', () => {
it('names cue as 1 if next event does not collide', () => {
const testRundown = [
@@ -89,6 +89,24 @@ describe('findCueName()', () => {
expect(cue).toBe('Presenter1.1');
});
});
describe('considers edge cases', () => {
it('previousEvent might not be a cue', () => {
const testRundown = [
{ id: '1', cue: '10', type: SupportedEvent.Event } as OntimeEvent,
{ id: '2', type: SupportedEvent.Delay } as OntimeDelay,
];
const cue = getCueCandidate(testRundown, '2');
expect(cue).toBe('11');
});
});
it('there might not be events before', () => {
const testRundown = [
{ id: '1', type: SupportedEvent.Delay } as OntimeDelay,
{ id: '2', type: SupportedEvent.Delay } as OntimeDelay,
];
const cue = getCueCandidate(testRundown, '2');
expect(cue).toBe('1');
});
});
describe('findCueName() with mixed events', () => {
+17 -6
View File
@@ -1,6 +1,6 @@
import { OntimeEvent, OntimeRundown } from 'ontime-types';
import { isOntimeEvent, OntimeEvent, OntimeRundown, OntimeRundownEntry } from 'ontime-types';
import { getFirstEvent, getNextEvent } from '../rundown-utils/rundownUtils.js';
import { getFirstEvent, getNextEvent, getPreviousEvent } from '../rundown-utils/rundownUtils.js';
import { isNumeric } from '../types/types.js';
/**
@@ -9,7 +9,7 @@ import { isNumeric } from '../types/types.js';
*/
export function getIncrement(input: string): string {
// Check if the input string contains a number at the end
const match = input.match(/^(\D*)(\d+)(\.\d+)?$/);
const match = input?.match(/^(\D*)(\d+)(\.\d+)?$/);
if (match) {
// If a number is found, extract the non-numeric prefix, integer part, and decimal part
@@ -61,15 +61,26 @@ export function getCueCandidate(rundown: OntimeRundown, insertAfterId?: string):
}
// get elements around
const previousEvent = rundown.at(afterIndex);
let previousEvent: OntimeRundownEntry | undefined | null | OntimeEvent = rundown.at(afterIndex);
if (!isOntimeEvent(previousEvent)) {
previousEvent = getPreviousEvent(rundown, insertAfterId) as null | OntimeEvent;
}
let cue = '1';
const nextEvent = getNextEvent(rundown, insertAfterId);
// try and increment the cue
let cue = getIncrement((previousEvent as OntimeEvent).cue);
if (isOntimeEvent(previousEvent)) {
cue = getIncrement(previousEvent.cue);
}
// if increment is clashing with next, we add a decimal instead
if (cue === nextEvent?.cue) {
cue = (previousEvent as OntimeEvent).cue + '.1';
if (previousEvent === null) {
cue = '0.1';
} else {
cue = previousEvent.cue + '.1';
}
}
return cue;