Suite of AT Protocol TypeScript libraries built on web standards
1import { assertEquals, assertThrows } from "@std/assert";
2import {
3 ensureValidDatetime,
4 InvalidDatetimeError,
5 isValidDatetime,
6 normalizeDatetime,
7 normalizeDatetimeAlways,
8} from "../mod.ts";
9
10Deno.test("conforms to interop valid datetimes", async () => {
11 const expectValid = (h: string) => {
12 ensureValidDatetime(h);
13 normalizeDatetime(h);
14 normalizeDatetimeAlways(h);
15 };
16
17 const filePath =
18 new URL("./interop/datetime_syntax_valid.txt", import.meta.url).pathname;
19 const fileContent = await Deno.readTextFile(filePath);
20 const lines = fileContent.split("\n");
21
22 for (const line of lines) {
23 if (line.startsWith("#") || line.length === 0) {
24 continue;
25 }
26 if (!isValidDatetime(line)) {
27 console.log(line);
28 }
29 expectValid(line);
30 }
31});
32
33Deno.test("conforms to interop invalid datetimes", async () => {
34 const expectInvalid = (h: string) => {
35 assertThrows(() => ensureValidDatetime(h), InvalidDatetimeError);
36 };
37
38 const filePath =
39 new URL("./interop/datetime_syntax_invalid.txt", import.meta.url).pathname;
40 const fileContent = await Deno.readTextFile(filePath);
41 const lines = fileContent.split("\n");
42
43 for (const line of lines) {
44 if (line.startsWith("#") || line.length === 0) {
45 continue;
46 }
47 expectInvalid(line);
48 }
49});
50
51Deno.test("conforms to interop invalid parse (semantics) datetimes", async () => {
52 const expectInvalid = (h: string) => {
53 assertThrows(() => ensureValidDatetime(h), InvalidDatetimeError);
54 };
55
56 const filePath =
57 new URL("./interop/datetime_parse_invalid.txt", import.meta.url).pathname;
58 const fileContent = await Deno.readTextFile(filePath);
59 const lines = fileContent.split("\n");
60
61 for (const line of lines) {
62 if (line.startsWith("#") || line.length === 0) {
63 continue;
64 }
65 expectInvalid(line);
66 }
67});
68
69Deno.test("normalization - normalizes datetimes", () => {
70 assertEquals(
71 normalizeDatetime("1234-04-12T23:20:50Z"),
72 "1234-04-12T23:20:50.000Z",
73 );
74 assertEquals(
75 normalizeDatetime("1985-04-12T23:20:50Z"),
76 "1985-04-12T23:20:50.000Z",
77 );
78 assertEquals(
79 normalizeDatetime("1985-04-12T23:20:50.123"),
80 "1985-04-12T23:20:50.123Z",
81 );
82 assertEquals(
83 normalizeDatetime("1985-04-12 23:20:50.123"),
84 "1985-04-12T23:20:50.123Z",
85 );
86 assertEquals(
87 normalizeDatetime("1985-04-12T10:20:50.1+01:00"),
88 "1985-04-12T09:20:50.100Z",
89 );
90 assertEquals(
91 normalizeDatetime("Fri, 02 Jan 1999 12:34:56 GMT"),
92 "1999-01-02T12:34:56.000Z",
93 );
94});
95
96Deno.test("normalization - throws on invalid normalized datetimes", () => {
97 assertThrows(() => normalizeDatetime(""), InvalidDatetimeError);
98 assertThrows(() => normalizeDatetime("blah"), InvalidDatetimeError);
99 assertThrows(
100 () => normalizeDatetime("1999-19-39T23:20:50.123Z"),
101 InvalidDatetimeError,
102 );
103 assertThrows(
104 () => normalizeDatetime("-000001-12-31T23:00:00.000Z"),
105 InvalidDatetimeError,
106 );
107 assertThrows(
108 () => normalizeDatetime("0000-01-01T00:00:00+01:00"),
109 InvalidDatetimeError,
110 );
111 assertThrows(
112 () => normalizeDatetime("0001-01-01T00:00:00+01:00"),
113 InvalidDatetimeError,
114 );
115});
116
117Deno.test("normalization - normalizes datetimes always", () => {
118 assertEquals(
119 normalizeDatetimeAlways("1985-04-12T23:20:50Z"),
120 "1985-04-12T23:20:50.000Z",
121 );
122 assertEquals(
123 normalizeDatetimeAlways("blah"),
124 "1970-01-01T00:00:00.000Z",
125 );
126 assertEquals(
127 normalizeDatetimeAlways("0000-01-01T00:00:00+01:00"),
128 "1970-01-01T00:00:00.000Z",
129 );
130});