/[sudobot]/branches/8.x/tests/utils/datetime.test.ts
ViewVC logotype

Annotation of /branches/8.x/tests/utils/datetime.test.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 2890 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 import { describe, expect, it } from "vitest";
2     import { displayDate, displayTimeSeconds, stringToTimeInterval } from "../../src/utils/datetime";
3    
4     describe("datetime utilities", () => {
5     it("should display a date", () => {
6     const date = new Date("2020-01-01T00:00:00Z");
7     expect(displayDate(date)).toBe(
8     `<t:${date.getTime() / 1000}:f> (<t:${date.getTime() / 1000}:R>)`
9     );
10     });
11    
12     it("should display a time in seconds", () => {
13     expect(displayTimeSeconds(0)).toBe("<t:0:f> (<t:0:R>)");
14     expect(displayTimeSeconds(1)).toBe("<t:1:f> (<t:1:R>)");
15     expect(displayTimeSeconds(6400)).toBe("<t:6400:f> (<t:6400:R>)");
16     });
17    
18     it("should convert a string to a time interval", () => {
19     expect(stringToTimeInterval("0")).toEqual({ error: null, result: 0 });
20     expect(stringToTimeInterval("1")).toEqual({ error: null, result: 1 });
21     expect(stringToTimeInterval("1s")).toEqual({ error: null, result: 1 });
22     expect(stringToTimeInterval("1m")).toEqual({ error: null, result: 60 });
23     expect(stringToTimeInterval("1h")).toEqual({ error: null, result: 3600 });
24     expect(stringToTimeInterval("1d")).toEqual({ error: null, result: 86400 });
25     expect(stringToTimeInterval("1w")).toEqual({ error: null, result: 604800 });
26     expect(stringToTimeInterval("1M")).toEqual({ error: null, result: 2592000 });
27     expect(stringToTimeInterval("1y")).toEqual({ error: null, result: 31536000 });
28     });
29    
30     it("should convert a string with decimals to a time interval", () => {
31     expect(stringToTimeInterval("1.5s")).toEqual({ error: null, result: 1.5 });
32     expect(stringToTimeInterval("1.5m")).toEqual({ error: null, result: 90 });
33     expect(stringToTimeInterval("1.5h")).toEqual({ error: null, result: 5400 });
34     expect(stringToTimeInterval("1.5d")).toEqual({ error: null, result: 129600 });
35     expect(stringToTimeInterval("1.5w")).toEqual({ error: null, result: 907200 });
36     expect(stringToTimeInterval("1.5M")).toEqual({ error: null, result: 3888000 });
37     expect(stringToTimeInterval("1.5y")).toEqual({ error: null, result: 47304000 });
38     });
39    
40     it("should handle a string with mixed units, spaces and values", () => {
41     expect(stringToTimeInterval(" 15s15m22h 14d1w 157M 12y ")).toEqual({
42     error: null,
43     result:
44     15 + 15 * 60 + 22 * 3600 + 14 * 86400 + 7 * 86400 + 157 * 2592000 + 12 * 31536000
45     });
46     });
47    
48     it("should handle invalid time values", () => {
49     expect(stringToTimeInterval("1x")).toEqual({ error: "Invalid time unit: x", result: NaN });
50     });
51    
52     it("should handle invalid numeric time values", () => {
53     expect(stringToTimeInterval("x")).toEqual({
54     error: "Invalid numeric time value",
55     result: NaN
56     });
57     });
58     });

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26