Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-protocol/src/util/timeutil.spec.ts
2500 views
1
/**
2
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import * as chai from "chai";
8
const expect = chai.expect;
9
import { suite, test } from "@testdeck/mocha";
10
import { daysBefore, goDurationToHumanReadable, hoursBefore, oneMonthLater, parseGoDurationToMs } from "./timeutil";
11
12
@suite()
13
export class TimeutilSpec {
14
@test
15
testDaysBefore() {
16
const now = new Date().toISOString();
17
expect(daysBefore(now, 2)).to.be.eq(hoursBefore(now, 48));
18
}
19
20
@test
21
testTimeutil() {
22
// targeting a 1st, 1th of Jan => 1st of Feb
23
this.isOneMonthLater(new Date(2000, 0, 1), 1, new Date(2000, 1, 1));
24
25
// targeting a 31th, 30th of Apr => 31st of May
26
this.isOneMonthLater(new Date(2000, 3, 30), 31, new Date(2000, 4, 31));
27
28
// targeting a 31th, 31th of Mar => 30th of Apr
29
this.isOneMonthLater(new Date(2000, 2, 31), 31, new Date(2000, 3, 30));
30
31
// targeting a 30th, 30th of Mar => 30th of Apr
32
this.isOneMonthLater(new Date(2000, 2, 30), 30, new Date(2000, 3, 30));
33
34
// next year
35
this.isOneMonthLater(new Date(2000, 11, 1), 1, new Date(2001, 0, 1));
36
this.isOneMonthLater(new Date(2000, 11, 31), 31, new Date(2001, 0, 31));
37
38
// Feb
39
this.isOneMonthLater(new Date(2001, 0, 31), 31, new Date(2001, 1, 28));
40
// Feb leap year
41
this.isOneMonthLater(new Date(2000, 0, 31), 31, new Date(2000, 1, 29));
42
}
43
44
isOneMonthLater(from: Date, day: number, expectation: Date) {
45
const later = oneMonthLater(from.toISOString(), day);
46
expect(later, `expected ${later} to be equal ${expectation}`).to.be.equal(expectation.toISOString());
47
}
48
49
@test
50
testDaysBefore2() {
51
const tests: { date: Date; daysEarlier: number; expectation: string }[] = [
52
{
53
date: new Date("2021-07-13T00:00:00.000Z"),
54
daysEarlier: 365,
55
expectation: "2020-07-13T00:00:00.000Z",
56
},
57
{
58
date: new Date("2019-02-01T00:00:00.000Z"),
59
daysEarlier: 365,
60
expectation: "2018-02-01T00:00:00.000Z",
61
},
62
];
63
64
for (const t of tests) {
65
const actual = daysBefore(t.date.toISOString(), t.daysEarlier);
66
expect(actual).to.equal(t.expectation, `expected ${actual} to be equal ${t.expectation}`);
67
}
68
}
69
70
@test
71
testParseGoDurationToMs() {
72
const scenarios = [
73
{ input: "1ms", out: 1 },
74
{ input: "1m", out: 1 * 60 * 1000 },
75
{ input: "1m0s", out: 1 * 60 * 1000 },
76
{ input: "1m30s", out: 1 * 60 * 1000 + 30 * 1000 },
77
{ input: "1h1m30s", out: 1 * 60 * 60 * 1000 + 1 * 60 * 1000 + 30 * 1000 },
78
];
79
80
for (const t of scenarios) {
81
const actual = parseGoDurationToMs(t.input);
82
expect(actual).to.equal(t.out, `expected ${t.input} to be ${t.out} in MS`);
83
}
84
}
85
86
@test
87
testGoDurationToHumanReadable() {
88
const scenarios = [
89
{ input: "1m", out: "1 minute" },
90
{ input: "2m", out: "2 minutes" },
91
{ input: "1m0s", out: "1 minute" },
92
{ input: "2m0s", out: "2 minutes" },
93
{ input: "1m30s", out: "1 minute 30 seconds" },
94
{ input: "2m30s", out: "2 minutes 30 seconds" },
95
{ input: "1h1m30s", out: "1 hour 1 minute 30 seconds" },
96
];
97
98
for (const t of scenarios) {
99
const actual = goDurationToHumanReadable(t.input);
100
expect(actual).to.equal(t.out, `expected ${t.input} to be ${t.out} in human readable form`);
101
}
102
}
103
}
104
105