Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/backend/conat/test/time.test.ts
1450 views
1
/*
2
DEVELOPMENT:
3
4
pnpm test ./time.test.ts
5
*/
6
7
import { timeClient, createTimeService } from "@cocalc/conat/service/time";
8
import time, { getSkew } from "@cocalc/conat/time";
9
import { before, after } from "@cocalc/backend/conat/test/setup";
10
11
beforeAll(before);
12
13
describe("get time from conat", () => {
14
it("tries to get the time before the skew, so it is not initialized yet", () => {
15
expect(time).toThrow("clock skew not known");
16
});
17
18
it("gets the skew, so that time is initialized", async () => {
19
const skew = await getSkew();
20
expect(Math.abs(skew)).toBeLessThan(1000);
21
});
22
23
it("gets the time, which should be close to our time on a test system", () => {
24
// times in ms, so divide by 1000 so expecting to be within a second
25
expect(time() / 1000).toBeCloseTo(Date.now() / 1000, 0);
26
});
27
28
it("time is a number", () => {
29
expect(typeof time()).toBe("number");
30
});
31
});
32
33
describe("start the time server and client and test that it works", () => {
34
it("starts the time server and queries it", async () => {
35
createTimeService();
36
const client = timeClient();
37
const t = await client.time();
38
expect(Math.abs(Date.now() - t)).toBeLessThan(200);
39
});
40
});
41
42
afterAll(after);
43
44