import { expect } from "chai"; import { pipe } from "effect"; import * as Either from "effect/Either"; import assert from "node:assert"; import { describe, it } from "node:test"; import { iso8601ToDate } from "./Date.js"; import { failWith } from "./Function.js"; describe("iso8601ToDate", () => { it("should encode to exactly the same string as date.toISOString", () => { const date = new Date(); assert.equal( pipe(iso8601ToDate.encode(date), Either.getOrElse(failWith)), date.toISOString(), ); }); it("should decode from an iso string", () => { expect( pipe( iso8601ToDate.decode("2024-10-10T23:00:00.000Z"), Either.getOrElse(failWith), ), ).to.deep.eq(new Date("2024-10-10T23:00:00.000Z")); }); it("should not change local time", () => { expect( pipe( iso8601ToDate.decode("2024-01-01T12:00"), Either.getOrElse(failWith), ).toISOString(), ).to.eq("2024-01-01T11:00:00.000Z"); }); it("should decode strings without time component to local time", () => { expect( pipe( iso8601ToDate.decode("2024-01-01"), Either.getOrElse(failWith), ).toISOString(), ).to.eq("2023-12-31T23:00:00.000Z"); }); });