db-vendo-client/test/e2e/lib/util.js

51 lines
1.2 KiB
JavaScript
Raw Normal View History

'use strict'
const isRoughlyEqual = require('is-roughly-equal')
2017-12-11 16:33:17 +01:00
const {DateTime} = require('luxon')
2018-05-14 00:35:21 +02:00
const a = require('assert')
const {join} = require('path')
2020-05-21 19:04:22 +02:00
const tape = require('tape')
2017-12-11 16:33:17 +01:00
const hour = 60 * 60 * 1000
2018-05-14 00:35:21 +02:00
const day = 24 * hour
const week = 7 * day
const T_MOCK = 1592225430 * 1000
2017-12-11 16:33:17 +01:00
// next Monday 10 am
const createWhen = (timezone, locale) => {
const t = process.env.VCR_MODE && !process.env.VCR_OFF
? T_MOCK
: Date.now()
return DateTime.fromMillis(t, {
zone: timezone,
locale,
}).startOf('week').plus({weeks: 1, hours: 10}).toJSDate()
}
2018-05-14 00:35:21 +02:00
const assertValidWhen = (actual, expected, name) => {
const ts = +new Date(actual)
2018-05-14 00:35:21 +02:00
a.ok(!Number.isNaN(ts), name + ' is not parsable by Date')
// the timestamps might be from long-distance trains
a.ok(isRoughlyEqual(day + 6 * hour, +expected, ts), name + ' is out of range')
}
// HTTP request mocking
if (process.env.VCR_MODE && !process.env.VCR_OFF) {
const replayer = require('replayer')
replayer.configure({
headerWhitelist: [
'Content-Type', 'Accept-Encoding', 'Accept',
],
includeHeaderValues: true,
touchHits: false,
})
replayer.fixtureDir(join(__dirname, '..', 'fixtures'))
}
2020-05-21 19:04:22 +02:00
const test = tape
module.exports = {
2020-05-21 19:04:22 +02:00
hour, createWhen, assertValidWhen,
test,
}