mirror of
https://github.com/cpvalente/ontime.git
synced 2026-09-10 08:39:34 +00:00
@@ -1,8 +1,8 @@
|
|||||||
import {
|
import {
|
||||||
DAY_TO_MS,
|
DAY_TO_MS,
|
||||||
getSelectionByRoll,
|
getSelectionByRoll,
|
||||||
replacePlaceholder,
|
|
||||||
normaliseEndTime,
|
normaliseEndTime,
|
||||||
|
replacePlaceholder,
|
||||||
sortArrayByProperty,
|
sortArrayByProperty,
|
||||||
updateRoll,
|
updateRoll,
|
||||||
} from '../classUtils.js';
|
} from '../classUtils.js';
|
||||||
@@ -59,7 +59,7 @@ describe('test that roll loads selection in right order', () => {
|
|||||||
const eventlist = [
|
const eventlist = [
|
||||||
{
|
{
|
||||||
id: 1,
|
id: 1,
|
||||||
timeStart: 0,
|
timeStart: 5,
|
||||||
timeEnd: 10,
|
timeEnd: 10,
|
||||||
isPublic: false,
|
isPublic: false,
|
||||||
},
|
},
|
||||||
@@ -67,7 +67,7 @@ describe('test that roll loads selection in right order', () => {
|
|||||||
id: 2,
|
id: 2,
|
||||||
timeStart: 10,
|
timeStart: 10,
|
||||||
timeEnd: 20,
|
timeEnd: 20,
|
||||||
isPublic: true,
|
isPublic: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 3,
|
id: 3,
|
||||||
@@ -109,19 +109,35 @@ describe('test that roll loads selection in right order', () => {
|
|||||||
|
|
||||||
it('if timer is at 0', () => {
|
it('if timer is at 0', () => {
|
||||||
const now = 0;
|
const now = 0;
|
||||||
|
const expected = {
|
||||||
|
nowIndex: null,
|
||||||
|
nowId: null,
|
||||||
|
publicIndex: null,
|
||||||
|
nextIndex: 0,
|
||||||
|
publicNextIndex: 4,
|
||||||
|
timers: null,
|
||||||
|
timeToNext: 5,
|
||||||
|
};
|
||||||
|
|
||||||
|
const state = getSelectionByRoll(eventlist, now);
|
||||||
|
expect(state).toStrictEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('if timer is at 5', () => {
|
||||||
|
const now = 5;
|
||||||
const expected = {
|
const expected = {
|
||||||
nowIndex: 0,
|
nowIndex: 0,
|
||||||
nowId: 1,
|
nowId: 1,
|
||||||
publicIndex: null,
|
publicIndex: null,
|
||||||
nextIndex: 1,
|
nextIndex: 1,
|
||||||
publicNextIndex: 1,
|
publicNextIndex: 4,
|
||||||
timers: {
|
timers: {
|
||||||
_finishAt: 10,
|
_finishAt: 10,
|
||||||
_startedAt: 0,
|
_startedAt: 5,
|
||||||
current: 10,
|
current: 5,
|
||||||
duration: 10,
|
duration: 5,
|
||||||
},
|
},
|
||||||
timeToNext: 10,
|
timeToNext: 5,
|
||||||
};
|
};
|
||||||
|
|
||||||
const state = getSelectionByRoll(eventlist, now);
|
const state = getSelectionByRoll(eventlist, now);
|
||||||
@@ -133,7 +149,7 @@ describe('test that roll loads selection in right order', () => {
|
|||||||
const expected = {
|
const expected = {
|
||||||
nowIndex: 1,
|
nowIndex: 1,
|
||||||
nowId: 2,
|
nowId: 2,
|
||||||
publicIndex: 1,
|
publicIndex: null,
|
||||||
nextIndex: 2,
|
nextIndex: 2,
|
||||||
publicNextIndex: 4,
|
publicNextIndex: 4,
|
||||||
timers: {
|
timers: {
|
||||||
@@ -154,7 +170,7 @@ describe('test that roll loads selection in right order', () => {
|
|||||||
const expected = {
|
const expected = {
|
||||||
nowIndex: 2,
|
nowIndex: 2,
|
||||||
nowId: 3,
|
nowId: 3,
|
||||||
publicIndex: 1,
|
publicIndex: null,
|
||||||
nextIndex: 3,
|
nextIndex: 3,
|
||||||
publicNextIndex: 4,
|
publicNextIndex: 4,
|
||||||
timers: {
|
timers: {
|
||||||
@@ -239,15 +255,38 @@ describe('test that roll loads selection in right order', () => {
|
|||||||
nowIndex: null,
|
nowIndex: null,
|
||||||
nowId: null,
|
nowId: null,
|
||||||
publicIndex: null,
|
publicIndex: null,
|
||||||
nextIndex: null,
|
nextIndex: 0,
|
||||||
publicNextIndex: null,
|
publicNextIndex: 4,
|
||||||
timers: null,
|
timers: null,
|
||||||
timeToNext: null,
|
timeToNext: DAY_TO_MS - now + eventlist[0].timeStart,
|
||||||
};
|
};
|
||||||
|
|
||||||
const state = getSelectionByRoll(eventlist, now);
|
const state = getSelectionByRoll(eventlist, now);
|
||||||
expect(state).toStrictEqual(expected);
|
expect(state).toStrictEqual(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('handles rolls to next day with real values', () => {
|
||||||
|
const singleEventList = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
timeStart: 36000000, // 10:00
|
||||||
|
timeEnd: 39600000, // 11:00
|
||||||
|
isPublic: true,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const now = 64800000; // 18:00
|
||||||
|
const expected = {
|
||||||
|
nowIndex: null,
|
||||||
|
nowId: null,
|
||||||
|
publicIndex: null,
|
||||||
|
nextIndex: 0,
|
||||||
|
publicNextIndex: 0,
|
||||||
|
timers: null,
|
||||||
|
timeToNext: DAY_TO_MS - now + singleEventList[0].timeStart,
|
||||||
|
};
|
||||||
|
const state = getSelectionByRoll(singleEventList, now);
|
||||||
|
expect(state).toStrictEqual(expected);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// test getSelectionByRoll()
|
// test getSelectionByRoll()
|
||||||
@@ -372,22 +411,6 @@ describe('test that roll behaviour with overlapping times', () => {
|
|||||||
const state = getSelectionByRoll(eventlist, now);
|
const state = getSelectionByRoll(eventlist, now);
|
||||||
expect(state).toStrictEqual(expected);
|
expect(state).toStrictEqual(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('if timer is at 31', () => {
|
|
||||||
const now = 31;
|
|
||||||
const expected = {
|
|
||||||
nowIndex: null,
|
|
||||||
nowId: null,
|
|
||||||
publicIndex: null,
|
|
||||||
nextIndex: null,
|
|
||||||
publicNextIndex: null,
|
|
||||||
timers: null,
|
|
||||||
timeToNext: null,
|
|
||||||
};
|
|
||||||
|
|
||||||
const state = getSelectionByRoll(eventlist, now);
|
|
||||||
expect(state).toStrictEqual(expected);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// test replacePlaceholder()
|
// test replacePlaceholder()
|
||||||
|
|||||||
@@ -62,16 +62,8 @@ export const getSelectionByRoll = (arr, now) => {
|
|||||||
// current timer
|
// current timer
|
||||||
let timers = null;
|
let timers = null;
|
||||||
|
|
||||||
// Order events by startTime
|
// exit early if there are no events
|
||||||
const orderedEvents = sortArrayByProperty(arr, 'timeStart');
|
if (arr.length < 1) {
|
||||||
|
|
||||||
// flags: select first event if several overlapping
|
|
||||||
let nowFound = false;
|
|
||||||
|
|
||||||
// exit early if we are past the events
|
|
||||||
const lastEvent = orderedEvents[orderedEvents.length - 1];
|
|
||||||
const lastNormalEnd = normaliseEndTime(lastEvent.timeStart, lastEvent.timeEnd);
|
|
||||||
if (now > lastNormalEnd) {
|
|
||||||
return {
|
return {
|
||||||
nowIndex,
|
nowIndex,
|
||||||
nowId,
|
nowId,
|
||||||
@@ -83,6 +75,28 @@ export const getSelectionByRoll = (arr, now) => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Order events by startTime
|
||||||
|
const orderedEvents = sortArrayByProperty(arr, 'timeStart');
|
||||||
|
|
||||||
|
// preload first if we are past events
|
||||||
|
const lastEvent = orderedEvents[orderedEvents.length - 1];
|
||||||
|
const lastNormalEnd = normaliseEndTime(lastEvent.timeStart, lastEvent.timeEnd);
|
||||||
|
|
||||||
|
if (now > lastNormalEnd) {
|
||||||
|
nextIndex = 0;
|
||||||
|
timeToNext = orderedEvents[0].timeStart + DAY_TO_MS - now;
|
||||||
|
|
||||||
|
// look for next public
|
||||||
|
for (const e of orderedEvents) {
|
||||||
|
if (e.isPublic) {
|
||||||
|
publicNextIndex = arr.findIndex((a) => a.id === e.id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// flags: select first event if several overlapping
|
||||||
|
let nowFound = false;
|
||||||
|
|
||||||
// loop through events, look for where we should be
|
// loop through events, look for where we should be
|
||||||
for (const e of orderedEvents) {
|
for (const e of orderedEvents) {
|
||||||
// When does the event end (handle midnight)
|
// When does the event end (handle midnight)
|
||||||
@@ -126,7 +140,7 @@ export const getSelectionByRoll = (arr, now) => {
|
|||||||
// check how far the start is from now
|
// check how far the start is from now
|
||||||
const wait = e.timeStart - now;
|
const wait = e.timeStart - now;
|
||||||
|
|
||||||
if (nextIndex == null || wait < timeToNext) {
|
if (nextIndex === null || wait < timeToNext) {
|
||||||
timeToNext = wait;
|
timeToNext = wait;
|
||||||
nextIndex = arr.findIndex((a) => a.id === e.id);
|
nextIndex = arr.findIndex((a) => a.id === e.id);
|
||||||
}
|
}
|
||||||
@@ -136,6 +150,8 @@ export const getSelectionByRoll = (arr, now) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
nowIndex,
|
nowIndex,
|
||||||
nowId,
|
nowId,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "ontime-server",
|
"name": "ontime-server",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"version": "1.1.0",
|
"version": "1.1.1",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.20.0",
|
"body-parser": "^1.20.0",
|
||||||
"dotenv": "^16.0.0",
|
"dotenv": "^16.0.0",
|
||||||
|
|||||||
Reference in New Issue
Block a user