temp: handle inconsistent errors from server

This commit is contained in:
cv
2023-09-27 09:56:24 +02:00
parent cb871a8c26
commit 85172ae8d7
4 changed files with 66 additions and 1 deletions
+2
View File
@@ -0,0 +1,2 @@
// evaluating the library, we re-export to make it easy to detach
export { deepmerge } from 'deepmerge-ts';
@@ -0,0 +1,30 @@
import { EndAction, TimerType } from 'ontime-types';
import { expect } from 'vitest';
import { validateEndAction, validateTimerType } from './validateEvent';
describe('validateEndAction()', () => {
it('recognises a string representation of an action', () => {
const endAction = validateEndAction('load-next');
expect(endAction).toBe(EndAction.LoadNext);
});
it('returns fallback otherwise', () => {
const emptyAction = validateEndAction('', EndAction.Stop);
const invalidAction = validateEndAction('this-does-not-exist', EndAction.PlayNext);
expect(emptyAction).toBe(EndAction.Stop);
expect(invalidAction).toBe(EndAction.PlayNext);
});
});
describe('validateTimerType()', () => {
it('recognises a string representation of an action', () => {
const timerType = validateTimerType('time-to-end');
expect(timerType).toBe(TimerType.TimeToEnd);
});
it('returns fallback otherwise', () => {
const emptyType = validateTimerType('', TimerType.Clock);
const invalidType = validateTimerType('this-does-not-exist', TimerType.CountDown);
expect(emptyType).toBe(TimerType.Clock);
expect(invalidType).toBe(TimerType.CountDown);
});
});
@@ -0,0 +1,25 @@
import { EndAction, TimerType } from 'ontime-types';
export function validateEndAction(maybeAction: unknown, fallback = EndAction.None) {
if (typeof maybeAction !== 'string') {
return fallback;
}
const isAction = Object.values(EndAction).includes(maybeAction as EndAction);
if (isAction) {
return maybeAction as EndAction;
}
return fallback;
}
export function validateTimerType(maybeTimerType: unknown, fallback = TimerType.CountDown) {
if (typeof maybeTimerType !== 'string') {
return fallback;
}
const isTimerType = Object.values(TimerType).includes(maybeTimerType as TimerType);
if (isTimerType) {
return maybeTimerType as TimerType;
}
return fallback;
}