import { describe, it, expect, vi } from "vitest";
import { runTimestampConverter } from "./run";
import type { ToolContext } from "../../core/plugins/plugin-types";

describe("Timestamp Converter", () => {
  const mockContext: ToolContext = {
    signal: new AbortController().signal,
    reportProgress: vi.fn(),
    logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },
  };

  it("should convert a Unix seconds timestamp to date", async () => {
    const result = await runTimestampConverter(
      { text: "0" },
      { mode: "timestamp-to-date", unit: "seconds" },
      mockContext
    );
    expect(result.type).toBe("json");
    const value = (result as { type: "json"; value: Record<string, unknown> }).value;
    expect(value.iso).toBe("1970-01-01T00:00:00.000Z");
    expect(value.unixSeconds).toBe(0);
    expect(value.dayOfWeek).toBe("Thursday");
  });

  it("should convert a known timestamp correctly", async () => {
    const result = await runTimestampConverter(
      { text: "1700000000" },
      { mode: "timestamp-to-date", unit: "seconds" },
      mockContext
    );
    const value = (result as { type: "json"; value: Record<string, unknown> }).value;
    expect(value.iso).toBe("2023-11-14T22:13:20.000Z");
    expect(value.unixSeconds).toBe(1700000000);
  });

  it("should convert milliseconds timestamp to date", async () => {
    const result = await runTimestampConverter(
      { text: "1700000000000" },
      { mode: "timestamp-to-date", unit: "milliseconds" },
      mockContext
    );
    const value = (result as { type: "json"; value: Record<string, unknown> }).value;
    expect(value.iso).toBe("2023-11-14T22:13:20.000Z");
  });

  it("should convert an ISO date string to Unix timestamp (seconds)", async () => {
    const result = await runTimestampConverter(
      { text: "2024-01-15T12:00:00Z" },
      { mode: "date-to-timestamp", unit: "seconds" },
      mockContext
    );
    const value = (result as { type: "json"; value: Record<string, unknown> }).value;
    const both = value.both as Record<string, unknown>;
    expect(value.unixSeconds).toBe(1705320000);
    expect(both.seconds).toBe(1705320000);
  });

  it("should convert an ISO date string to Unix timestamp (milliseconds)", async () => {
    const result = await runTimestampConverter(
      { text: "2024-01-15T12:00:00Z" },
      { mode: "date-to-timestamp", unit: "milliseconds" },
      mockContext
    );
    const value = (result as { type: "json"; value: Record<string, unknown> }).value;
    expect(value.unixMillis).toBe(1705320000000);
  });

  it("should throw on empty input", async () => {
    await expect(
      runTimestampConverter(
        { text: "" },
        { mode: "timestamp-to-date", unit: "seconds" },
        mockContext
      )
    ).rejects.toThrow("No input provided");
  });

  it("should throw on invalid timestamp", async () => {
    await expect(
      runTimestampConverter(
        { text: "not-a-number" },
        { mode: "timestamp-to-date", unit: "seconds" },
        mockContext
      )
    ).rejects.toThrow("Invalid timestamp");
  });

  it("should throw on invalid date string", async () => {
    await expect(
      runTimestampConverter(
        { text: "not-a-date" },
        { mode: "date-to-timestamp", unit: "seconds" },
        mockContext
      )
    ).rejects.toThrow("Invalid date string");
  });
});
