mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-20 06:29:07 +00:00
guard and test midnight
This commit is contained in:
@@ -357,22 +357,87 @@ describe('getExpectedFinish() and getCurrentTime() combined', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('skipedOutOfEvent()', () => {
|
describe('skipedOutOfEvent()', () => {
|
||||||
it('without added times, they combine to be duration', () => {
|
it('normal roll out of event', () => {
|
||||||
const startedAt = 1000;
|
const startedAt = 1000;
|
||||||
const duration = 1000;
|
const duration = 1000;
|
||||||
const pausedTime = 0;
|
|
||||||
const finishedAt = null;
|
|
||||||
const addedTime = 0;
|
|
||||||
const timerType = TimerType.CountDown;
|
|
||||||
const expectedFinish = startedAt + duration;
|
const expectedFinish = startedAt + duration;
|
||||||
const previousTime = 1000;
|
const previousTime = expectedFinish - config.timeSkipLimit / 2;
|
||||||
let clock = previousTime;
|
let clock = previousTime;
|
||||||
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
||||||
clock += config.timeSkipLimit + 10;
|
clock += config.timeSkipLimit;
|
||||||
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
||||||
clock = expectedFinish + 1;
|
});
|
||||||
|
|
||||||
|
it('normal roll backwards out of event', () => {
|
||||||
|
const startedAt = 1000;
|
||||||
|
const duration = 1000;
|
||||||
|
const expectedFinish = startedAt + duration;
|
||||||
|
const previousTime = startedAt + config.timeSkipLimit / 2;
|
||||||
|
let clock = previousTime;
|
||||||
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
||||||
|
clock -= config.timeSkipLimit;
|
||||||
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('normal roll out of event over midnight', () => {
|
||||||
|
const startedAt = dayInMs - config.timeSkipLimit;
|
||||||
|
const expectedFinish = 10;
|
||||||
|
const previousTime = dayInMs - 1;
|
||||||
|
let clock = previousTime;
|
||||||
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
||||||
|
clock = config.timeSkipLimit - 2;
|
||||||
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('normal roll backwards out of event over midnight', () => {
|
||||||
|
const startedAt = dayInMs - config.timeSkipLimit;
|
||||||
|
const expectedFinish = 10;
|
||||||
|
const previousTime = startedAt + 1;
|
||||||
|
let clock = previousTime;
|
||||||
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
||||||
|
clock -= config.timeSkipLimit;
|
||||||
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('skip out of event', () => {
|
||||||
|
const startedAt = 1000;
|
||||||
|
const duration = 1000;
|
||||||
|
const expectedFinish = startedAt + duration;
|
||||||
|
const previousTime = expectedFinish - config.timeSkipLimit / 2;
|
||||||
|
let clock = previousTime;
|
||||||
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
||||||
|
clock += config.timeSkipLimit + 1;
|
||||||
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(true);
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(true);
|
||||||
clock = startedAt - config.timeSkipLimit - 1;
|
});
|
||||||
|
|
||||||
|
it('skip backwards out of event', () => {
|
||||||
|
const startedAt = 1000;
|
||||||
|
const duration = 1000;
|
||||||
|
const expectedFinish = startedAt + duration;
|
||||||
|
const previousTime = startedAt + config.timeSkipLimit / 2;
|
||||||
|
let clock = previousTime;
|
||||||
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
||||||
|
clock -= config.timeSkipLimit + 1;
|
||||||
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('skip out of event over midnight', () => {
|
||||||
|
const startedAt = dayInMs - config.timeSkipLimit;
|
||||||
|
const expectedFinish = 10;
|
||||||
|
const previousTime = dayInMs - 3;
|
||||||
|
let clock = previousTime;
|
||||||
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
||||||
|
clock = config.timeSkipLimit - 2;
|
||||||
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('skip backwards out of event over midnight', () => {
|
||||||
|
const startedAt = dayInMs - config.timeSkipLimit;
|
||||||
|
const expectedFinish = 10;
|
||||||
|
const previousTime = startedAt + 1;
|
||||||
|
let clock = previousTime;
|
||||||
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false);
|
||||||
|
clock -= config.timeSkipLimit + 1;
|
||||||
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(true);
|
expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -67,8 +67,12 @@ export function getCurrent(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function skipedOutOfEvent(previousTime: number, clock: number, startedAt: number, expectedFinish): boolean {
|
export function skipedOutOfEvent(previousTime: number, clock: number, startedAt: number, expectedFinish): boolean {
|
||||||
|
if (previousTime > dayInMs - config.timeSkipLimit && clock < config.timeSkipLimit) {
|
||||||
|
clock += dayInMs;
|
||||||
|
}
|
||||||
const skipTime = previousTime - clock;
|
const skipTime = previousTime - clock;
|
||||||
const hasSkipped = Math.abs(skipTime) > config.timeSkipLimit;
|
const hasSkipped = Math.abs(skipTime) > config.timeSkipLimit;
|
||||||
|
expectedFinish = expectedFinish >= startedAt ? expectedFinish : expectedFinish + dayInMs;
|
||||||
if (hasSkipped) {
|
if (hasSkipped) {
|
||||||
if (clock > expectedFinish || clock < startedAt) {
|
if (clock > expectedFinish || clock < startedAt) {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user