import { describe, expect, test } from "bun:test"; // Mock context for tool execution const ctx = { directory: "/test" } as any; /** * Helper to parse JSON string responses from tools. * Returns parsed object or throws on parse failure. */ function parseResult(result: string): Record { return JSON.parse(result); } // ============================================================================ // NOW - Get current time // ============================================================================ describe("now", () => { test("returns current time with default timezone", async () => { const { now } = await import("../tools/time.ts"); const result = await now.execute({} as any, ctx); const data = parseResult(result); expect(data.iso).toBeDefined(); expect(data.unix_ms).toBeDefined(); expect(data.human).toBeDefined(); expect(data.timezone).toBe("Europe/Berlin"); expect(data.date).toMatch(/^\d{4}-\d{2}-\d{2}$/); expect(data.time).toMatch(/^\d{2}:\d{2}:\d{2}$/); }); test("returns current time in UTC timezone", async () => { const { now } = await import("../tools/time.ts"); const result = await now.execute({ timezone: "UTC" } as any, ctx); const data = parseResult(result); expect(data.timezone).toBe("UTC"); expect(data.iso).toBeDefined(); }); test("returns current time with custom format", async () => { const { now } = await import("../tools/time.ts"); const result = await now.execute({ timezone: "UTC", format: "yyyy-MM-dd", } as any, ctx); const data = parseResult(result); expect(data.formatted).toMatch(/^\d{4}-\d{2}-\d{2}$/); }); test("returns error for invalid timezone", async () => { const { now } = await import("../tools/time.ts"); const result = await now.execute( { timezone: "Invalid/Timezone" } as any, ctx, ); expect(result).toContain("Invalid timezone"); }); }); // ============================================================================ // CONVERT - Convert between timezones // ============================================================================ describe("convert", () => { test("converts time between timezones", async () => { const { convert } = await import("../tools/time.ts"); const result = await convert.execute({ time: "2024-03-15T12:00:00", from_timezone: "UTC", to_timezone: "America/New_York", } as any, ctx); const data = parseResult(result); expect(data.original).toBeDefined(); expect(data.converted).toBeDefined(); expect(data.original.timezone).toBe("UTC"); expect(data.converted.timezone).toBe("America/New_York"); }); test("converts 'now' between timezones", async () => { const { convert } = await import("../tools/time.ts"); const result = await convert.execute({ time: "now", from_timezone: "Europe/Berlin", to_timezone: "Asia/Tokyo", } as any, ctx); const data = parseResult(result); expect(data.original.timezone).toBe("Europe/Berlin"); expect(data.converted.timezone).toBe("Asia/Tokyo"); }); test("returns error for invalid source timezone", async () => { const { convert } = await import("../tools/time.ts"); const result = await convert.execute({ time: "2024-03-15T12:00:00", from_timezone: "Invalid/Source", to_timezone: "UTC", } as any, ctx); expect(result).toContain("Invalid timezone"); }); test("returns error for invalid target timezone", async () => { const { convert } = await import("../tools/time.ts"); const result = await convert.execute({ time: "2024-03-15T12:00:00", from_timezone: "UTC", to_timezone: "Invalid/Target", } as any, ctx); expect(result).toContain("Invalid timezone"); }); test("returns error for invalid time format", async () => { const { convert } = await import("../tools/time.ts"); const result = await convert.execute({ time: "not-a-valid-time", from_timezone: "UTC", to_timezone: "America/New_York", } as any, ctx); expect(result).toContain("Invalid time format"); }); }); // ============================================================================ // PARSE - Parse date/time strings // ============================================================================ describe("parse", () => { test("parses ISO 8601 date string", async () => { const { parse } = await import("../tools/time.ts"); const result = await parse.execute({ input: "2024-03-15T14:30:00Z", timezone: "UTC", } as any, ctx); const data = parseResult(result); expect(data.valid).toBe(true); expect(data.parsed.iso).toBeDefined(); expect(data.parsed.date).toBe("2024-03-15"); expect(data.parsed.time).toBe("14:30:00"); }); test("parses Unix timestamp in milliseconds", async () => { const { parse } = await import("../tools/time.ts"); const result = await parse.execute({ input: "1710511800000", } as any, ctx); const data = parseResult(result); expect(data.valid).toBe(true); expect(data.parsed.unix_ms).toBe(1710511800000); }); test("parses SQL format", async () => { const { parse } = await import("../tools/time.ts"); const result = await parse.execute({ input: "2024-03-15 14:30:00", } as any, ctx); const data = parseResult(result); expect(data.valid).toBe(true); expect(data.parsed.date).toBe("2024-03-15"); }); test("returns error for invalid input", async () => { const { parse } = await import("../tools/time.ts"); const result = await parse.execute({ input: "completely invalid date", } as any, ctx); const data = parseResult(result); expect(data.valid).toBe(false); expect(data.error).toBeDefined(); }); test("returns error for invalid timezone", async () => { const { parse } = await import("../tools/time.ts"); const result = await parse.execute({ input: "2024-03-15T14:30:00", timezone: "Invalid/Zone", } as any, ctx); expect(result).toContain("Invalid timezone"); }); }); // ============================================================================ // FORMAT - Format timestamps // ============================================================================ describe("format", () => { test("formats as ISO by default", async () => { const { format } = await import("../tools/time.ts"); const result = await format.execute({ time: "2024-03-15T14:30:00Z", timezone: "UTC", } as any, ctx); const data = parseResult(result); expect(data.formatted).toContain("2024-03-15T14:30:00"); expect(data.format_used).toBe("ISO 8601"); }); test("formats with custom format string", async () => { const { format } = await import("../tools/time.ts"); const result = await format.execute({ time: "2024-03-15T14:30:00Z", format_type: "custom", custom_format: "yyyy/MM/dd", } as any, ctx); const data = parseResult(result); expect(data.formatted).toBe("2024/03/15"); expect(data.format_used).toContain("Custom"); }); test("formats with locale", async () => { const { format } = await import("../tools/time.ts"); const result = await format.execute({ time: "2024-03-15T14:30:00Z", format_type: "locale_full", locale: "de-DE", } as any, ctx); const data = parseResult(result); expect(data.formatted).toBeDefined(); expect(data.locale).toBe("de-DE"); }); test("returns error for custom format without custom_format arg", async () => { const { format } = await import("../tools/time.ts"); const result = await format.execute({ time: "2024-03-15T14:30:00Z", format_type: "custom", } as any, ctx); expect(result).toContain("Custom format requires"); }); test("returns error for invalid time", async () => { const { format } = await import("../tools/time.ts"); const result = await format.execute({ time: "invalid-time", } as any, ctx); expect(result).toContain("Invalid time"); }); test("formats with locale_short", async () => { const { format } = await import("../tools/time.ts"); const result = await format.execute({ time: "2024-03-15T14:30:00Z", format_type: "locale_short", } as any, ctx); const data = parseResult(result); expect(data.formatted).toBeDefined(); expect(data.format_used).toBe("Locale short"); }); }); // ============================================================================ // RELATIVE - Relative time strings // ============================================================================ describe("relative", () => { test("returns relative time in the past", async () => { const { relative } = await import("../tools/time.ts"); const pastTime = new Date(Date.now() - 2 * 60 * 60 * 1000).toISOString(); const result = await relative.execute({ time: pastTime, } as any, ctx); const data = parseResult(result); expect(data.relative).toContain("ago"); expect(data.is_past).toBe(true); expect(data.direction).toBe("past"); }); test("returns relative time in the future", async () => { const { relative } = await import("../tools/time.ts"); const futureTime = new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString(); const result = await relative.execute({ time: futureTime, } as any, ctx); const data = parseResult(result); expect(data.relative).toContain("in"); expect(data.is_past).toBe(false); expect(data.direction).toBe("future"); }); test("uses custom reference point", async () => { const { relative } = await import("../tools/time.ts"); const result = await relative.execute({ time: "2024-03-15T14:00:00", reference: "2024-03-15T12:00:00", } as any, ctx); const data = parseResult(result); expect(data.relative).toContain("in"); expect(data.breakdown.hours).toBe(2); }); test("returns error for invalid target time", async () => { const { relative } = await import("../tools/time.ts"); const result = await relative.execute({ time: "invalid-time", } as any, ctx); expect(result).toContain("Invalid target time"); }); test("returns error for invalid reference time", async () => { const { relative } = await import("../tools/time.ts"); const result = await relative.execute({ time: "2024-03-15T14:00:00", reference: "invalid-reference", } as any, ctx); expect(result).toContain("Invalid reference time"); }); test("supports different styles", async () => { const { relative } = await import("../tools/time.ts"); const pastTime = new Date(Date.now() - 2 * 60 * 60 * 1000).toISOString(); const result = await relative.execute({ time: pastTime, style: "short", } as any, ctx); const data = parseResult(result); expect(data.style).toBe("short"); expect(data.relative).toBeDefined(); }); }); // ============================================================================ // DURATION - Calculate duration between timestamps // ============================================================================ describe("duration", () => { test("calculates duration between two dates", async () => { const { duration } = await import("../tools/time.ts"); const result = await duration.execute({ start: "2024-03-01T12:00:00", end: "2024-03-15T18:30:00", } as any, ctx); const data = parseResult(result); expect(data.breakdown.days).toBe(14); expect(data.breakdown.hours).toBe(6); expect(data.breakdown.minutes).toBe(30); expect(data.human).toBeDefined(); }); test("returns error when end is before start", async () => { const { duration } = await import("../tools/time.ts"); const result = await duration.execute({ start: "2024-03-15T18:30:00", end: "2024-03-01T12:00:00", } as any, ctx); expect(result).toContain("Invalid interval"); }); test("returns duration in specific unit", async () => { const { duration } = await import("../tools/time.ts"); const result = await duration.execute({ start: "2024-03-01T00:00:00", end: "2024-03-08T00:00:00", unit: "days", } as any, ctx); const data = parseResult(result); expect(data.value_in_days).toBe(7); }); test("returns error for invalid start time", async () => { const { duration } = await import("../tools/time.ts"); const result = await duration.execute({ start: "invalid-start", end: "2024-03-15T12:00:00", } as any, ctx); expect(result).toContain("Invalid start time"); }); test("returns error for invalid end time", async () => { const { duration } = await import("../tools/time.ts"); const result = await duration.execute({ start: "2024-03-01T12:00:00", end: "invalid-end", } as any, ctx); expect(result).toContain("Invalid end time"); }); test("calculates duration with 'now'", async () => { const { duration } = await import("../tools/time.ts"); const futureDate = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000) .toISOString(); const result = await duration.execute({ start: "now", end: futureDate, } as any, ctx); const data = parseResult(result); expect(data.start.iso).toBeDefined(); expect(data.end.iso).toBeDefined(); expect(data.total_milliseconds).toBeDefined(); }); }); // ============================================================================ // ADD - Add/subtract time from a date // ============================================================================ describe("add", () => { test("adds days to a date", async () => { const { add } = await import("../tools/time.ts"); const result = await add.execute({ time: "2024-03-15T12:00:00", days: 7, } as any, ctx); const data = parseResult(result); expect(data.result.date).toBe("2024-03-22"); expect(data.change_applied).toContain("7 day"); }); test("subtracts hours from a date", async () => { const { add } = await import("../tools/time.ts"); const result = await add.execute({ time: "2024-03-15T14:00:00", hours: -2, } as any, ctx); const data = parseResult(result); expect(data.result.time).toBe("12:00:00"); expect(data.change_applied).toContain("-2 hour"); }); test("adds multiple units", async () => { const { add } = await import("../tools/time.ts"); const result = await add.execute({ time: "2024-03-15T12:00:00", years: 1, months: 2, days: 3, } as any, ctx); const data = parseResult(result); expect(data.result.date).toBe("2025-05-18"); expect(data.change_applied).toContain("1 year"); expect(data.change_applied).toContain("2 month"); expect(data.change_applied).toContain("3 day"); }); test("returns no change when no units provided", async () => { const { add } = await import("../tools/time.ts"); const result = await add.execute({ time: "2024-03-15T12:00:00", } as any, ctx); const data = parseResult(result); expect(data.change_applied).toBe("no change"); expect(data.difference_ms).toBe(0); }); test("returns error for invalid time", async () => { const { add } = await import("../tools/time.ts"); const result = await add.execute({ time: "invalid-time", days: 1, } as any, ctx); expect(result).toContain("Invalid time"); }); test("adds weeks to a date", async () => { const { add } = await import("../tools/time.ts"); const result = await add.execute({ time: "2024-03-15T12:00:00", weeks: 2, } as any, ctx); const data = parseResult(result); expect(data.result.date).toBe("2024-03-29"); expect(data.change_applied).toContain("2 week"); }); }); // ============================================================================ // COMPARE - Compare two timestamps // ============================================================================ describe("compare", () => { test("compares two dates - time1 before time2", async () => { const { compare } = await import("../tools/time.ts"); const result = await compare.execute({ time1: "2024-03-15T12:00:00", time2: "2024-03-20T12:00:00", } as any, ctx); const data = parseResult(result); expect(data.comparison).toBe("time1 is before time2"); expect(data.is_before).toBe(true); expect(data.is_after).toBe(false); expect(data.is_equal).toBe(false); }); test("compares two dates - time1 after time2", async () => { const { compare } = await import("../tools/time.ts"); const result = await compare.execute({ time1: "2024-03-20T12:00:00", time2: "2024-03-15T12:00:00", } as any, ctx); const data = parseResult(result); expect(data.comparison).toBe("time1 is after time2"); expect(data.is_before).toBe(false); expect(data.is_after).toBe(true); }); test("compares equal dates", async () => { const { compare } = await import("../tools/time.ts"); const result = await compare.execute({ time1: "2024-03-15T12:00:00", time2: "2024-03-15T12:00:00", } as any, ctx); const data = parseResult(result); expect(data.comparison).toBe("time1 equals time2"); expect(data.is_equal).toBe(true); expect(data.difference_ms).toBe(0); }); test("returns error for invalid time1", async () => { const { compare } = await import("../tools/time.ts"); const result = await compare.execute({ time1: "invalid-time1", time2: "2024-03-15T12:00:00", } as any, ctx); expect(result).toContain("Invalid time1"); }); test("returns error for invalid time2", async () => { const { compare } = await import("../tools/time.ts"); const result = await compare.execute({ time1: "2024-03-15T12:00:00", time2: "invalid-time2", } as any, ctx); expect(result).toContain("Invalid time2"); }); test("provides difference breakdown", async () => { const { compare } = await import("../tools/time.ts"); const result = await compare.execute({ time1: "2024-03-15T10:00:00", time2: "2024-03-16T14:30:30", } as any, ctx); const data = parseResult(result); expect(data.difference_breakdown.days).toBe(1); expect(data.difference_breakdown.hours).toBe(4); expect(data.difference_breakdown.minutes).toBe(30); expect(data.difference_breakdown.seconds).toBe(30); }); }); // ============================================================================ // UNIX - Unix timestamp conversion // ============================================================================ describe("unix", () => { test("converts ISO to Unix milliseconds", async () => { const { unix } = await import("../tools/time.ts"); const result = await unix.execute({ time: "2024-03-15T14:30:00Z", } as any, ctx); const data = parseResult(result); expect(data.unix_ms).toBeDefined(); expect(data.output_value).toBe(data.unix_ms); expect(data.note).toBeDefined(); }); test("converts to Unix seconds", async () => { const { unix } = await import("../tools/time.ts"); const result = await unix.execute({ time: "2024-03-15T14:30:00Z", output_unit: "seconds", } as any, ctx); const data = parseResult(result); expect(data.unix_seconds).toBeDefined(); expect(data.output_value).toBe(data.unix_seconds); }); test("converts Unix timestamp to ISO", async () => { const { unix } = await import("../tools/time.ts"); const result = await unix.execute({ time: "1710511800000", } as any, ctx); const data = parseResult(result); expect(data.iso).toBeDefined(); expect(data.human).toBeDefined(); expect(data.unix_ms).toBe(1710511800000); }); test("returns error for invalid time", async () => { const { unix } = await import("../tools/time.ts"); const result = await unix.execute({ time: "invalid-time", } as any, ctx); expect(result).toContain("Invalid time"); }); test("handles 'now' input", async () => { const { unix } = await import("../tools/time.ts"); const before = Date.now(); const result = await unix.execute({ time: "now", } as any, ctx); const after = Date.now(); const data = parseResult(result); expect(data.unix_ms).toBeGreaterThanOrEqual(before); expect(data.unix_ms).toBeLessThanOrEqual(after); }); }); // ============================================================================ // INFO - Timezone information // ============================================================================ describe("info", () => { test("returns timezone info for default timezone", async () => { const { info } = await import("../tools/time.ts"); const result = await info.execute({} as any, ctx); const data = parseResult(result); expect(data.timezone).toBe("Europe/Berlin"); expect(data.iana_id).toBe("Europe/Berlin"); expect(data.current_offset).toBeDefined(); expect(data.dst).toBeDefined(); }); test("returns timezone info for specific timezone", async () => { const { info } = await import("../tools/time.ts"); const result = await info.execute({ timezone: "America/New_York", } as any, ctx); const data = parseResult(result); expect(data.timezone).toBe("America/New_York"); expect(data.current_offset).toBeDefined(); expect(data.dst.has_dst).toBe(true); }); test("returns error for invalid timezone", async () => { const { info } = await import("../tools/time.ts"); const result = await info.execute({ timezone: "Invalid/Timezone", } as any, ctx); expect(result).toContain("Invalid timezone"); }); test("lists common timezones", async () => { const { info } = await import("../tools/time.ts"); const result = await info.execute({ list_timezones: true, } as any, ctx); const data = parseResult(result); expect(data.timezones).toBeDefined(); expect(Array.isArray(data.timezones)).toBe(true); expect(data.timezones).toContain("UTC"); expect(data.timezones).toContain("Europe/Berlin"); expect(data.timezones).toContain("America/New_York"); }); test("checks DST status at specific time", async () => { const { info } = await import("../tools/time.ts"); const result = await info.execute({ timezone: "America/New_York", time: "2024-07-15T12:00:00", } as any, ctx); const data = parseResult(result); expect(data.dst.is_currently_observing).toBe(true); }); test("returns timezone info for UTC", async () => { const { info } = await import("../tools/time.ts"); const result = await info.execute({ timezone: "UTC", } as any, ctx); const data = parseResult(result); expect(data.timezone).toBe("UTC"); expect(data.dst.has_dst).toBe(false); }); test("returns error for invalid time parameter", async () => { const { info } = await import("../tools/time.ts"); const result = await info.execute({ timezone: "UTC", time: "not-a-valid-time", } as any, ctx); expect(result).toContain("Invalid time"); }); }); // ============================================================================ // COVERAGE - EDGE CASES // ============================================================================ describe("coverage - edge cases", () => { // --------------------------------------------------------------------------- // parseInput format variations // --------------------------------------------------------------------------- describe("parse - additional formats", () => { test("parses RFC 2822 format", async () => { const { parse } = await import("../tools/time.ts"); const result = await parse.execute({ input: "Fri, 15 Mar 2024 14:30:00 GMT", timezone: "UTC", } as any, ctx); const data = parseResult(result); expect(data.valid).toBe(true); expect(data.parsed.date).toBe("2024-03-15"); expect(data.parsed.time).toBe("14:30:00"); }); test("parses HTTP date format", async () => { const { parse } = await import("../tools/time.ts"); const result = await parse.execute({ input: "Fri, 15 Mar 2024 14:30:00 GMT", } as any, ctx); const data = parseResult(result); expect(data.valid).toBe(true); expect(data.parsed.date).toBe("2024-03-15"); }); test("rejects Unix timestamp 0", async () => { const { parse } = await import("../tools/time.ts"); const result = await parse.execute({ input: "0", } as any, ctx); const data = parseResult(result); expect(data.valid).toBe(false); expect(data.error).toBeDefined(); }); test("handles case-insensitive NOW", async () => { const { parse } = await import("../tools/time.ts"); const before = Date.now(); const result = await parse.execute({ input: "NOW", } as any, ctx); const after = Date.now(); const data = parseResult(result); expect(data.valid).toBe(true); expect(data.parsed.unix_ms).toBeGreaterThanOrEqual(before); expect(data.parsed.unix_ms).toBeLessThanOrEqual(after); }); test("handles case-insensitive Now", async () => { const { parse } = await import("../tools/time.ts"); const before = Date.now(); const result = await parse.execute({ input: "Now", } as any, ctx); const after = Date.now(); const data = parseResult(result); expect(data.valid).toBe(true); expect(data.parsed.unix_ms).toBeGreaterThanOrEqual(before); expect(data.parsed.unix_ms).toBeLessThanOrEqual(after); }); }); // --------------------------------------------------------------------------- // format tool additional branches // --------------------------------------------------------------------------- describe("format - additional format types", () => { test("formats with locale (defaults to medium)", async () => { const { format } = await import("../tools/time.ts"); const result = await format.execute({ time: "2024-03-15T14:30:00Z", format_type: "locale", } as any, ctx); const data = parseResult(result); expect(data.formatted).toBeDefined(); expect(data.format_used).toBe("Locale medium"); }); test("formats with locale_medium explicitly", async () => { const { format } = await import("../tools/time.ts"); const result = await format.execute({ time: "2024-03-15T14:30:00Z", format_type: "locale_medium", } as any, ctx); const data = parseResult(result); expect(data.formatted).toBeDefined(); expect(data.format_used).toBe("Locale medium"); }); test("returns error for invalid timezone", async () => { const { format } = await import("../tools/time.ts"); const result = await format.execute({ time: "2024-03-15T14:30:00Z", timezone: "Invalid/Timezone", } as any, ctx); expect(result).toContain("Invalid timezone"); }); }); // --------------------------------------------------------------------------- // relative tool additional options // --------------------------------------------------------------------------- describe("relative - additional options", () => { test("supports style: long explicitly", async () => { const { relative } = await import("../tools/time.ts"); const pastTime = new Date(Date.now() - 2 * 60 * 60 * 1000).toISOString(); const result = await relative.execute({ time: pastTime, style: "long", } as any, ctx); const data = parseResult(result); expect(data.style).toBe("long"); expect(data.relative).toBeDefined(); }); test("supports style: narrow", async () => { const { relative } = await import("../tools/time.ts"); const pastTime = new Date(Date.now() - 2 * 60 * 60 * 1000).toISOString(); const result = await relative.execute({ time: pastTime, style: "narrow", } as any, ctx); const data = parseResult(result); expect(data.style).toBe("narrow"); expect(data.relative).toBeDefined(); }); test("supports unit: years", async () => { const { relative } = await import("../tools/time.ts"); const result = await relative.execute({ time: "2026-03-15T14:00:00", reference: "2024-03-15T14:00:00", unit: "years", } as any, ctx); const data = parseResult(result); expect(data.unit_used).toBe("years"); expect(data.relative).toBeDefined(); }); test("supports unit: months", async () => { const { relative } = await import("../tools/time.ts"); const result = await relative.execute({ time: "2024-05-15T14:00:00", reference: "2024-03-15T14:00:00", unit: "months", } as any, ctx); const data = parseResult(result); expect(data.unit_used).toBe("months"); expect(data.relative).toBeDefined(); }); test("supports unit: weeks", async () => { const { relative } = await import("../tools/time.ts"); const result = await relative.execute({ time: "2024-03-29T14:00:00", reference: "2024-03-15T14:00:00", unit: "weeks", } as any, ctx); const data = parseResult(result); expect(data.unit_used).toBe("weeks"); expect(data.relative).toBeDefined(); }); test("supports unit: days", async () => { const { relative } = await import("../tools/time.ts"); const result = await relative.execute({ time: "2024-03-20T14:00:00", reference: "2024-03-15T14:00:00", unit: "days", } as any, ctx); const data = parseResult(result); expect(data.unit_used).toBe("days"); expect(data.relative).toBeDefined(); }); test("supports unit: hours", async () => { const { relative } = await import("../tools/time.ts"); const result = await relative.execute({ time: "2024-03-15T18:00:00", reference: "2024-03-15T14:00:00", unit: "hours", } as any, ctx); const data = parseResult(result); expect(data.unit_used).toBe("hours"); expect(data.relative).toBeDefined(); }); test("supports unit: minutes", async () => { const { relative } = await import("../tools/time.ts"); const result = await relative.execute({ time: "2024-03-15T14:30:00", reference: "2024-03-15T14:00:00", unit: "minutes", } as any, ctx); const data = parseResult(result); expect(data.unit_used).toBe("minutes"); expect(data.relative).toBeDefined(); }); test("supports unit: seconds", async () => { const { relative } = await import("../tools/time.ts"); const result = await relative.execute({ time: "2024-03-15T14:00:30", reference: "2024-03-15T14:00:00", unit: "seconds", } as any, ctx); const data = parseResult(result); expect(data.unit_used).toBe("seconds"); expect(data.relative).toBeDefined(); }); test("supports locale parameter", async () => { const { relative } = await import("../tools/time.ts"); const pastTime = new Date(Date.now() - 2 * 60 * 60 * 1000).toISOString(); const result = await relative.execute({ time: pastTime, locale: "de", } as any, ctx); const data = parseResult(result); expect(data.relative).toBeDefined(); }); test("returns error for invalid timezone", async () => { const { relative } = await import("../tools/time.ts"); const result = await relative.execute({ time: "2024-03-15T14:00:00", timezone: "Invalid/Timezone", } as any, ctx); expect(result).toContain("Invalid timezone"); }); test("handles equal target and reference times", async () => { const { relative } = await import("../tools/time.ts"); const result = await relative.execute({ time: "2024-03-15T14:00:00", reference: "2024-03-15T14:00:00", } as any, ctx); const data = parseResult(result); // Luxon returns "in 0 sec." for equal times expect(data.relative).toBe("in 0 sec."); }); }); // --------------------------------------------------------------------------- // duration tool additional options // --------------------------------------------------------------------------- describe("duration - additional unit options", () => { test("returns duration in milliseconds", async () => { const { duration } = await import("../tools/time.ts"); const result = await duration.execute({ start: "2024-03-01T00:00:00", end: "2024-03-01T00:00:01", unit: "milliseconds", } as any, ctx); const data = parseResult(result); expect(data.value_in_milliseconds).toBe(1000); }); test("returns duration in seconds", async () => { const { duration } = await import("../tools/time.ts"); const result = await duration.execute({ start: "2024-03-01T00:00:00", end: "2024-03-01T00:01:00", unit: "seconds", } as any, ctx); const data = parseResult(result); expect(data.value_in_seconds).toBe(60); }); test("returns duration in minutes", async () => { const { duration } = await import("../tools/time.ts"); const result = await duration.execute({ start: "2024-03-01T00:00:00", end: "2024-03-01T01:00:00", unit: "minutes", } as any, ctx); const data = parseResult(result); expect(data.value_in_minutes).toBe(60); }); test("returns duration in hours", async () => { const { duration } = await import("../tools/time.ts"); const result = await duration.execute({ start: "2024-03-01T00:00:00", end: "2024-03-01T12:00:00", unit: "hours", } as any, ctx); const data = parseResult(result); expect(data.value_in_hours).toBe(12); }); test("returns duration in weeks", async () => { const { duration } = await import("../tools/time.ts"); const result = await duration.execute({ start: "2024-03-01T00:00:00", end: "2024-03-15T00:00:00", unit: "weeks", } as any, ctx); const data = parseResult(result); expect(data.value_in_weeks).toBe(2); }); test("returns duration in months", async () => { const { duration } = await import("../tools/time.ts"); const result = await duration.execute({ start: "2024-01-01T00:00:00", end: "2024-04-01T00:00:00", unit: "months", } as any, ctx); const data = parseResult(result); expect(data.value_in_months).toBe(3); }); test("returns duration in years", async () => { const { duration } = await import("../tools/time.ts"); const result = await duration.execute({ start: "2022-01-01T00:00:00", end: "2024-01-01T00:00:00", unit: "years", } as any, ctx); const data = parseResult(result); expect(data.value_in_years).toBe(2); }); test("returns error for invalid timezone", async () => { const { duration } = await import("../tools/time.ts"); const result = await duration.execute({ start: "2024-03-01T00:00:00", end: "2024-03-15T00:00:00", timezone: "Invalid/Timezone", } as any, ctx); expect(result).toContain("Invalid timezone"); }); }); // --------------------------------------------------------------------------- // add tool additional units // --------------------------------------------------------------------------- describe("add - additional units", () => { test("adds quarters to a date", async () => { const { add } = await import("../tools/time.ts"); const result = await add.execute({ time: "2024-03-15T12:00:00", quarters: 1, } as any, ctx); const data = parseResult(result); expect(data.result.date).toBe("2024-06-15"); expect(data.change_applied).toContain("1 quarter"); }); test("adds months to a date (solo)", async () => { const { add } = await import("../tools/time.ts"); const result = await add.execute({ time: "2024-03-15T12:00:00", months: 2, } as any, ctx); const data = parseResult(result); expect(data.result.date).toBe("2024-05-15"); expect(data.change_applied).toContain("2 month"); }); test("adds minutes to a time", async () => { const { add } = await import("../tools/time.ts"); const result = await add.execute({ time: "2024-03-15T12:00:00", minutes: 30, } as any, ctx); const data = parseResult(result); expect(data.result.time).toBe("12:30:00"); expect(data.change_applied).toContain("30 minute"); }); test("adds seconds to a time", async () => { const { add } = await import("../tools/time.ts"); const result = await add.execute({ time: "2024-03-15T12:00:00", seconds: 45, } as any, ctx); const data = parseResult(result); expect(data.result.time).toBe("12:00:45"); expect(data.change_applied).toContain("45 second"); }); test("adds milliseconds to a time", async () => { const { add } = await import("../tools/time.ts"); const result = await add.execute({ time: "2024-03-15T12:00:00", milliseconds: 500, } as any, ctx); const data = parseResult(result); expect(data.change_applied).toContain("500 millisecond"); expect(data.difference_ms).toBe(500); }); test("returns error for invalid timezone", async () => { const { add } = await import("../tools/time.ts"); const result = await add.execute({ time: "2024-03-15T12:00:00", days: 1, timezone: "Invalid/Timezone", } as any, ctx); expect(result).toContain("Invalid timezone"); }); test("returns error for invalid result (huge duration overflow)", async () => { const { add } = await import("../tools/time.ts"); // Adding 1 million years causes an invalid date (outside Luxon's valid range) const result = await add.execute({ time: "2024-01-01T00:00:00", years: 1000000, } as any, ctx); expect(result).toContain("Result is invalid"); }); }); // --------------------------------------------------------------------------- // compare tool invalid timezone // --------------------------------------------------------------------------- describe("compare - error paths", () => { test("returns error for invalid timezone", async () => { const { compare } = await import("../tools/time.ts"); const result = await compare.execute({ time1: "2024-03-15T12:00:00", time2: "2024-03-20T12:00:00", timezone: "Invalid/Timezone", } as any, ctx); expect(result).toContain("Invalid timezone"); }); }); // --------------------------------------------------------------------------- // convert tool conversion failure // --------------------------------------------------------------------------- describe("convert - conversion failure", () => { test("returns error when conversion produces invalid date (edge of valid range)", async () => { const { convert } = await import("../tools/time.ts"); // Max valid Unix timestamp (8639999999999999 ms = year 275760-09-12T23:59:59.999Z) // is valid in UTC but becomes invalid when converted to timezones with positive offsets // (like Asia/Tokyo with UTC+9) because the offset pushes it outside Luxon's valid range const result = await convert.execute({ time: "8639999999999999", from_timezone: "UTC", to_timezone: "Asia/Tokyo", } as any, ctx); expect(result).toContain("Conversion failed"); }); }); // --------------------------------------------------------------------------- // unix tool invalid timezone // --------------------------------------------------------------------------- describe("unix - error paths", () => { test("returns error for invalid timezone", async () => { const { unix } = await import("../tools/time.ts"); const result = await unix.execute({ time: "2024-03-15T14:30:00Z", timezone: "Invalid/Timezone", } as any, ctx); expect(result).toContain("Invalid timezone"); }); }); // --------------------------------------------------------------------------- // relative tool toRelative() null fallback // --------------------------------------------------------------------------- describe("relative - toRelative null fallback", () => { test("returns 'unknown' when toRelative returns null (auto unit)", async () => { const { DateTime } = await import("luxon"); const { relative } = await import("../tools/time.ts"); // Mock toRelative to return null const originalToRelative = DateTime.prototype.toRelative; DateTime.prototype.toRelative = function () { return null; }; try { const result = await relative.execute({ time: "2024-03-15T14:00:00", reference: "2024-03-15T12:00:00", unit: "auto", } as any, ctx); const data = parseResult(result); expect(data.relative).toBe("unknown"); expect(data.unit_used).toBe("auto"); } finally { // Restore original method DateTime.prototype.toRelative = originalToRelative; } }); test("returns 'unknown' when toRelative returns null (specific unit)", async () => { const { DateTime } = await import("luxon"); const { relative } = await import("../tools/time.ts"); // Mock toRelative to return null const originalToRelative = DateTime.prototype.toRelative; DateTime.prototype.toRelative = function () { return null; }; try { const result = await relative.execute({ time: "2024-03-15T14:00:00", reference: "2024-03-15T12:00:00", unit: "days", } as any, ctx); const data = parseResult(result); expect(data.relative).toBe("unknown"); expect(data.unit_used).toBe("days"); } finally { // Restore original method DateTime.prototype.toRelative = originalToRelative; } }); }); });