This repository was archived by the owner on Nov 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathtest.js
More file actions
98 lines (89 loc) · 3.12 KB
/
Copy pathtest.js
File metadata and controls
98 lines (89 loc) · 3.12 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* 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,
});
});
it(`returns offset 0 and local Date on error`, async () => {
const fetchSample = async () => {
throw new Error(`oh dang`);
};
const { date, offset, uncertainty } = await getServerDate({ fetchSample });
assert(offset === 0);
assert(uncertainty === Number.MAX_VALUE);
assert(Date.now() - date < 100);
})
it(`reports errors if you ask it to`, async () => {
const fetchSample = async () => {
throw new Error(`oh dang`);
};
const { errors, fetchCount } = await getServerDate({ fetchSample, withErrors: true });
assert(errors.length === fetchCount);
assert(errors[0].message === `oh dang`);
})