| 1 | import { toTimeoutSeconds } from "@/AWS/Lambda/Function.ts"; |
| 2 | import * as Duration from "effect/Duration"; |
| 3 | import { describe, expect, it } from "alchemy-test"; |
| 4 | |
| 5 | describe( |
| 6 | "toTimeoutSeconds", |
| 7 | { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] }, |
| 8 | () => { |
| 9 | it("returns undefined for undefined", () => { |
| 10 | expect(toTimeoutSeconds(undefined)).toBeUndefined(); |
| 11 | }); |
| 12 | |
| 13 | it("converts a Duration to whole seconds", () => { |
| 14 | expect(toTimeoutSeconds(Duration.seconds(30))).toBe(30); |
| 15 | expect(toTimeoutSeconds(Duration.minutes(2))).toBe(120); |
| 16 | }); |
| 17 | |
| 18 | it("rounds sub-second and partial durations up", () => { |
| 19 | expect(toTimeoutSeconds(Duration.millis(1))).toBe(1); |
| 20 | expect(toTimeoutSeconds(Duration.millis(1500))).toBe(2); |
| 21 | expect(toTimeoutSeconds(Duration.millis(2001))).toBe(3); |
| 22 | }); |
| 23 | |
| 24 | it("returns undefined for an infinite Duration", () => { |
| 25 | expect(toTimeoutSeconds(Duration.infinity)).toBeUndefined(); |
| 26 | }); |
| 27 | |
| 28 | describe("after state JSON round-trip", () => { |
| 29 | const roundTrip = (d: Duration.Duration) => |
| 30 | JSON.parse(JSON.stringify(d)) as Duration.Duration; |
| 31 | |
| 32 | it("converts a rehydrated Millis Duration", () => { |
| 33 | const json = roundTrip(Duration.seconds(42)); |
| 34 | expect(json).toEqual({ |
| 35 | _id: "Duration", |
| 36 | _tag: "Millis", |
| 37 | millis: 42_000, |
| 38 | }); |
| 39 | expect(toTimeoutSeconds(json)).toBe(42); |
| 40 | }); |
| 41 | |
| 42 | it("converts a rehydrated Nanos Duration", () => { |
| 43 | const json = roundTrip(Duration.nanos(5_000_000_000n)); |
| 44 | expect(toTimeoutSeconds(json)).toBe(5); |
| 45 | }); |
| 46 | |
| 47 | it("returns undefined for a rehydrated Infinity Duration", () => { |
| 48 | expect(toTimeoutSeconds(roundTrip(Duration.infinity))).toBeUndefined(); |
| 49 | }); |
| 50 | }); |
| 51 | }, |
| 52 | ); |