-
Notifications
You must be signed in to change notification settings - Fork 53
/
test.js
79 lines (72 loc) · 2.51 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import assert from "assert";
import { getServerDate } from "./serverDate";
it(`synchronizes the time with the server`, async () => {
const samples = [
{
requestDate: new Date(`2021-02-02T23:43:31.689Z`),
responseDate: new Date(`2021-02-02T23:43:32.268Z`),
serverDate: new Date(`2021-02-02T23:43:32.000Z`),
},
{
requestDate: new Date(`2021-02-02T23:43:32.268Z`),
responseDate: new Date(`2021-02-02T23:43:32.433Z`),
serverDate: new Date(`2021-02-02T23:43:32.000Z`),
},
{
requestDate: new Date(`2021-02-02T23:43:32.433Z`),
responseDate: new Date(`2021-02-02T23:43:32.601Z`),
serverDate: new Date(`2021-02-02T23:43:32.000Z`),
},
{
requestDate: new Date(`2021-02-02T23:43:32.601Z`),
responseDate: new Date(`2021-02-02T23:43:32.780Z`),
serverDate: new Date(`2021-02-02T23:43:32.000Z`),
},
{
requestDate: new Date(`2021-02-02T23:43:32.780Z`),
responseDate: new Date(`2021-02-02T23:43:32.947Z`),
serverDate: new Date(`2021-02-02T23:43:32.000Z`),
},
{
requestDate: new Date(`2021-02-02T23:43:32.947Z`),
responseDate: new Date(`2021-02-02T23:43:33.135Z`),
serverDate: new Date(`2021-02-02T23:43:33.000Z`),
},
{
requestDate: new Date(`2021-02-02T23:43:33.135Z`),
responseDate: new Date(`2021-02-02T23:43:33.383Z`),
serverDate: new Date(`2021-02-02T23:43:33.000Z`),
},
{
requestDate: new Date(`2021-02-02T23:43:33.383Z`),
responseDate: new Date(`2021-02-02T23:43:33.551Z`),
serverDate: new Date(`2021-02-02T23:43:33.000Z`),
},
{
requestDate: new Date(`2021-02-02T23:43:33.551Z`),
responseDate: new Date(`2021-02-02T23:43:33.726Z`),
serverDate: new Date(`2021-02-02T23:43:33.000Z`),
},
{
requestDate: new Date(`2021-02-02T23:43:33.726Z`),
responseDate: new Date(`2021-02-02T23:43:33.896Z`),
serverDate: new Date(`2021-02-02T23:43:33.000Z`),
},
];
let currentSample = -1;
const fetchSample = async () => {
currentSample++;
if (currentSample === 5) {
throw new Error(`bad sample`);
}
return samples[currentSample];
};
assert.deepStrictEqual(await getServerDate({ fetchSample }), {
date: new Date(`2021-02-02T23:43:32.500Z`),
offset: 67,
uncertainty: 582.5,
});
});