import { describe, expect, it } from "vitest";
import { formatBytes } from "./formatBytes";

describe("formatBytes", () => {
  it("returns bytes under 1 KB unchanged", () => {
    expect(formatBytes(0)).toBe("0 B");
    expect(formatBytes(512)).toBe("512 B");
    expect(formatBytes(999)).toBe("999 B");
  });

  it("formats KB with one decimal when small", () => {
    expect(formatBytes(1000)).toBe("1 KB");
    expect(formatBytes(1500)).toBe("1.5 KB");
  });

  it("formats MB with one decimal", () => {
    expect(formatBytes(3_400_000)).toBe("3.4 MB");
    expect(formatBytes(4_600_000)).toBe("4.6 MB");
  });

  it("drops decimals once value >= 100", () => {
    expect(formatBytes(150 * 1000 * 1000)).toBe("150 MB");
  });

  it("handles GB and TB", () => {
    expect(formatBytes(2 * 1000 ** 3)).toBe("2 GB");
    expect(formatBytes(3 * 1000 ** 4)).toBe("3 TB");
  });

  it("guards against invalid input", () => {
    expect(formatBytes(-1)).toBe("0 B");
    expect(formatBytes(Number.NaN)).toBe("0 B");
  });
});
