Path: blob/main/components/gitpod-protocol/src/util/timeutil.spec.ts
2500 views
/**1* Copyright (c) 2020 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import * as chai from "chai";7const expect = chai.expect;8import { suite, test } from "@testdeck/mocha";9import { daysBefore, goDurationToHumanReadable, hoursBefore, oneMonthLater, parseGoDurationToMs } from "./timeutil";1011@suite()12export class TimeutilSpec {13@test14testDaysBefore() {15const now = new Date().toISOString();16expect(daysBefore(now, 2)).to.be.eq(hoursBefore(now, 48));17}1819@test20testTimeutil() {21// targeting a 1st, 1th of Jan => 1st of Feb22this.isOneMonthLater(new Date(2000, 0, 1), 1, new Date(2000, 1, 1));2324// targeting a 31th, 30th of Apr => 31st of May25this.isOneMonthLater(new Date(2000, 3, 30), 31, new Date(2000, 4, 31));2627// targeting a 31th, 31th of Mar => 30th of Apr28this.isOneMonthLater(new Date(2000, 2, 31), 31, new Date(2000, 3, 30));2930// targeting a 30th, 30th of Mar => 30th of Apr31this.isOneMonthLater(new Date(2000, 2, 30), 30, new Date(2000, 3, 30));3233// next year34this.isOneMonthLater(new Date(2000, 11, 1), 1, new Date(2001, 0, 1));35this.isOneMonthLater(new Date(2000, 11, 31), 31, new Date(2001, 0, 31));3637// Feb38this.isOneMonthLater(new Date(2001, 0, 31), 31, new Date(2001, 1, 28));39// Feb leap year40this.isOneMonthLater(new Date(2000, 0, 31), 31, new Date(2000, 1, 29));41}4243isOneMonthLater(from: Date, day: number, expectation: Date) {44const later = oneMonthLater(from.toISOString(), day);45expect(later, `expected ${later} to be equal ${expectation}`).to.be.equal(expectation.toISOString());46}4748@test49testDaysBefore2() {50const tests: { date: Date; daysEarlier: number; expectation: string }[] = [51{52date: new Date("2021-07-13T00:00:00.000Z"),53daysEarlier: 365,54expectation: "2020-07-13T00:00:00.000Z",55},56{57date: new Date("2019-02-01T00:00:00.000Z"),58daysEarlier: 365,59expectation: "2018-02-01T00:00:00.000Z",60},61];6263for (const t of tests) {64const actual = daysBefore(t.date.toISOString(), t.daysEarlier);65expect(actual).to.equal(t.expectation, `expected ${actual} to be equal ${t.expectation}`);66}67}6869@test70testParseGoDurationToMs() {71const scenarios = [72{ input: "1ms", out: 1 },73{ input: "1m", out: 1 * 60 * 1000 },74{ input: "1m0s", out: 1 * 60 * 1000 },75{ input: "1m30s", out: 1 * 60 * 1000 + 30 * 1000 },76{ input: "1h1m30s", out: 1 * 60 * 60 * 1000 + 1 * 60 * 1000 + 30 * 1000 },77];7879for (const t of scenarios) {80const actual = parseGoDurationToMs(t.input);81expect(actual).to.equal(t.out, `expected ${t.input} to be ${t.out} in MS`);82}83}8485@test86testGoDurationToHumanReadable() {87const scenarios = [88{ input: "1m", out: "1 minute" },89{ input: "2m", out: "2 minutes" },90{ input: "1m0s", out: "1 minute" },91{ input: "2m0s", out: "2 minutes" },92{ input: "1m30s", out: "1 minute 30 seconds" },93{ input: "2m30s", out: "2 minutes 30 seconds" },94{ input: "1h1m30s", out: "1 hour 1 minute 30 seconds" },95];9697for (const t of scenarios) {98const actual = goDurationToHumanReadable(t.input);99expect(actual).to.equal(t.out, `expected ${t.input} to be ${t.out} in human readable form`);100}101}102}103104105