2023-01-20 15:31:04 +01:00
|
|
|
import dayjs from '../../../src/common/dayjs'
|
2022-11-29 22:51:28 +01:00
|
|
|
import {
|
|
|
|
|
ValidationError,
|
|
|
|
|
ValidationErrorCode,
|
|
|
|
|
} from '../../../src/common/request'
|
2022-11-27 16:14:56 +01:00
|
|
|
import { Reservation } from '../../../src/common/reservation'
|
2022-11-29 22:51:28 +01:00
|
|
|
import { schedule, SchedulerInput } from '../../../src/common/scheduler'
|
2021-11-28 13:06:52 +01:00
|
|
|
|
2022-11-27 16:14:56 +01:00
|
|
|
jest.mock('../../../src/common/logger')
|
2022-11-29 22:51:28 +01:00
|
|
|
jest.mock('../../../src/common/reserver')
|
2023-01-21 15:17:21 +01:00
|
|
|
jest.mock('uuid', () => ({ v4: () => '1234' }))
|
2022-10-23 11:55:47 +02:00
|
|
|
jest.useFakeTimers().setSystemTime(new Date('2022-01-01'))
|
2021-11-28 13:06:52 +01:00
|
|
|
|
2022-03-29 22:41:44 +02:00
|
|
|
describe('scheduler', () => {
|
2021-11-28 13:06:52 +01:00
|
|
|
test('should handle valid requests within reservation window', async () => {
|
2022-11-29 22:51:28 +01:00
|
|
|
jest.spyOn(Reservation, 'save').mockResolvedValueOnce()
|
2021-11-28 13:06:52 +01:00
|
|
|
const start = dayjs().add(15, 'minutes')
|
|
|
|
|
const end = start.add(15, 'minutes')
|
|
|
|
|
|
2022-03-29 22:41:44 +02:00
|
|
|
const payload: SchedulerInput = {
|
2022-10-23 11:55:47 +02:00
|
|
|
username: 'collin',
|
|
|
|
|
password: 'password',
|
2021-11-28 19:40:16 +01:00
|
|
|
dateRange: { start: start.toISOString(), end: end.toISOString() },
|
2022-10-23 11:55:47 +02:00
|
|
|
opponent: { id: '123', name: 'collin' },
|
2021-11-28 19:40:16 +01:00
|
|
|
}
|
2021-11-28 13:06:52 +01:00
|
|
|
|
2022-11-29 22:51:28 +01:00
|
|
|
expect(await schedule(payload)).toMatchSnapshot({
|
2022-10-23 11:55:47 +02:00
|
|
|
scheduledReservation: {
|
|
|
|
|
reservation: {
|
2023-01-21 15:17:21 +01:00
|
|
|
id: expect.any(String),
|
2022-10-23 11:55:47 +02:00
|
|
|
user: {
|
2021-11-28 13:06:52 +01:00
|
|
|
username: 'collin',
|
2022-11-04 14:16:11 +01:00
|
|
|
password: expect.any(String),
|
2022-10-23 11:55:47 +02:00
|
|
|
},
|
|
|
|
|
dateRange: { start, end },
|
2022-11-04 14:16:11 +01:00
|
|
|
opponent: { id: '123', name: 'collin' },
|
2022-10-23 11:55:47 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
2021-11-28 13:06:52 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('should handle valid requests outside of reservation window', async () => {
|
|
|
|
|
const start = dayjs().add(15, 'days')
|
|
|
|
|
const end = start.add(15, 'minutes')
|
2022-03-29 22:41:44 +02:00
|
|
|
const payload: SchedulerInput = {
|
2022-10-23 11:55:47 +02:00
|
|
|
username: 'collin',
|
|
|
|
|
password: 'password',
|
2021-11-28 19:40:16 +01:00
|
|
|
dateRange: { start: start.toISOString(), end: end.toISOString() },
|
2022-10-23 11:55:47 +02:00
|
|
|
opponent: { id: '123', name: 'collin' },
|
2021-11-28 19:40:16 +01:00
|
|
|
}
|
2021-11-28 13:06:52 +01:00
|
|
|
|
2022-11-29 22:51:28 +01:00
|
|
|
await expect(await schedule(payload)).toMatchSnapshot({
|
2022-10-23 11:55:47 +02:00
|
|
|
scheduledReservation: {
|
|
|
|
|
reservation: new Reservation(
|
|
|
|
|
{ username: 'collin', password: expect.any(String) },
|
|
|
|
|
{ start, end },
|
2023-01-21 15:17:21 +01:00
|
|
|
{ id: '123', name: 'collin' },
|
|
|
|
|
undefined,
|
2023-01-28 10:51:46 +01:00
|
|
|
'1234'
|
2022-10-23 11:55:47 +02:00
|
|
|
),
|
|
|
|
|
scheduledFor: start
|
|
|
|
|
.subtract(7, 'days')
|
|
|
|
|
.hour(0)
|
|
|
|
|
.minute(0)
|
|
|
|
|
.second(0)
|
|
|
|
|
.millisecond(0),
|
|
|
|
|
},
|
2021-11-28 13:06:52 +01:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('should throw error for invalid requests', async () => {
|
|
|
|
|
const start = dayjs().add(15, 'days')
|
|
|
|
|
const end = start.add(15, 'minutes')
|
2021-11-28 19:40:16 +01:00
|
|
|
|
2022-03-29 22:41:44 +02:00
|
|
|
const payload: SchedulerInput = {
|
2022-10-23 11:55:47 +02:00
|
|
|
password: 'password',
|
2021-11-28 19:40:16 +01:00
|
|
|
dateRange: { start: start.toISOString(), end: end.toISOString() },
|
2022-10-23 11:55:47 +02:00
|
|
|
opponent: { id: '123', name: 'collin' },
|
2021-11-28 19:40:16 +01:00
|
|
|
}
|
2021-11-28 13:06:52 +01:00
|
|
|
|
2022-11-29 22:51:28 +01:00
|
|
|
await expect(schedule(payload)).rejects.toThrowError(
|
2022-10-23 11:55:47 +02:00
|
|
|
new ValidationError(
|
|
|
|
|
'Invalid request',
|
|
|
|
|
ValidationErrorCode.INVALID_REQUEST_BODY
|
|
|
|
|
)
|
|
|
|
|
)
|
2021-11-28 13:06:52 +01:00
|
|
|
})
|
2022-10-23 11:55:47 +02:00
|
|
|
})
|