Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2
3import { compareDateTimeStrings } from "./dateTime.ts";
4
5describe("compareDateTimeStrings", () => {
6 it("compares valid date-time strings by absolute time", () => {
7 expect(
8 compareDateTimeStrings("2026-09-01T12:00:00.000Z", "2026-09-01T05:00:00.000-07:00"),
9 ).toBe(0);
10 expect(
11 compareDateTimeStrings("2026-09-01T12:00:01.000Z", "2026-09-01T12:00:00.000Z"),
12 ).toBeGreaterThan(0);
13 });
14
15 it.each([
16 ["2024-02-29T12:00:00Z", "2024-02-29T17:30:00+05:30"],
17 ["0000-01-01T00:00:00.000Z", "+000000-01-01T00:00:00.000+00:00"],
18 ["2026-09-01T24:00:00Z", "2026-09-02T00:00:00Z"],
19 ])("preserves equal ISO instants %s and %s", (left, right) => {
20 expect(compareDateTimeStrings(left, right)).toBe(0);
21 });
22
23 it.each(["2014-03-02", "2014-02-30T00:00:00.000Z", "2026-09-01T24:01:00Z"])(
24 "treats %s as malformed without native date guessing",
25 (malformed) => {
26 const valid = "1970-01-01T00:00:00.000Z";
27 expect(compareDateTimeStrings(malformed, valid)).toBeLessThan(0);
28 expect(compareDateTimeStrings(valid, malformed)).toBeGreaterThan(0);
29 expect(compareDateTimeStrings(malformed, "invalid")).toBeLessThan(0);
30 expect(compareDateTimeStrings("invalid", malformed)).toBeGreaterThan(0);
31 expect(compareDateTimeStrings(malformed, malformed)).toBe(0);
32 },
33 );
34
35 it("gives every permutation of mixed values the same order", () => {
36 const early = "2026-09-01T12:00:00.000+14:00";
37 const late = "2026-09-01T00:00:00.000-12:00";
38 const malformed = "2026-09-01T06:invalid";
39 const expected = [malformed, early, late];
40
41 const permutations = [
42 [early, late, malformed],
43 [early, malformed, late],
44 [late, early, malformed],
45 [late, malformed, early],
46 [malformed, early, late],
47 [malformed, late, early],
48 ];
49
50 for (const values of permutations) {
51 expect(values.toSorted(compareDateTimeStrings)).toEqual(expected);
52 }
53 });
54});