import { describe, it, expect } from "vitest";
import { computeTransferMinutes, formatMinutesAsDuration } from "./computeTransferTime.js";

describe("computeTransferMinutes", () => {
  it("returns positive diff in minutes", () => {
    expect(computeTransferMinutes("2026-04-17T10:00:00Z", "2026-04-17T11:30:00Z")).toBe(90);
  });

  it("returns 0 when times are identical", () => {
    const t = "2026-04-17T10:00:00Z";
    expect(computeTransferMinutes(t, t)).toBe(0);
  });

  it("returns null when departure is before arrival (negative)", () => {
    expect(computeTransferMinutes("2026-04-17T11:00:00Z", "2026-04-17T10:00:00Z")).toBeNull();
  });

  it("returns null when arrival is undefined", () => {
    expect(computeTransferMinutes(undefined, "2026-04-17T10:00:00Z")).toBeNull();
  });

  it("returns null when departure is undefined", () => {
    expect(computeTransferMinutes("2026-04-17T10:00:00Z", undefined)).toBeNull();
  });

  it("returns null for invalid ISO strings", () => {
    expect(computeTransferMinutes("not-a-date", "2026-04-17T10:00:00Z")).toBeNull();
  });
});

describe("formatMinutesAsDuration", () => {
  it("formats 90 minutes as '1h 30m'", () => {
    expect(formatMinutesAsDuration(90)).toBe("1h 30m");
  });

  it("formats 45 minutes as '45m'", () => {
    expect(formatMinutesAsDuration(45)).toBe("45m");
  });

  it("formats 60 minutes as '1h'", () => {
    expect(formatMinutesAsDuration(60)).toBe("1h");
  });

  it("formats 0 as '0m'", () => {
    expect(formatMinutesAsDuration(0)).toBe("0m");
  });
});
