import { describe, expect, it } from "vitest";
import { formatBytes, formatImageId, formatUptime } from "./formatters.js";
describe("formatBytes", () => {
it("should return '0 B' for 0 bytes", (): void => {
expect(formatBytes(0)).toBe("0 B");
});
it("should format bytes correctly", (): void => {
expect(formatBytes(500)).toBe("500.0 B");
});
it("should format kilobytes correctly", (): void => {
expect(formatBytes(1024)).toBe("1.0 KB");
expect(formatBytes(1536)).toBe("1.5 KB");
});
it("should format megabytes correctly", (): void => {
expect(formatBytes(1048576)).toBe("1.0 MB");
expect(formatBytes(1572864)).toBe("1.5 MB");
});
it("should format gigabytes correctly", (): void => {
expect(formatBytes(1073741824)).toBe("1.0 GB");
});
it("should format terabytes correctly", (): void => {
expect(formatBytes(1099511627776)).toBe("1.0 TB");
});
});
describe("formatUptime", () => {
it("should format minutes only when less than 1 hour", (): void => {
const now = Date.now();
const thirtyMinutesAgo = new Date(now - 30 * 60 * 1000).toISOString();
expect(formatUptime(thirtyMinutesAgo)).toBe("30m");
});
it("should format hours and minutes when less than 1 day", (): void => {
const now = Date.now();
const twoHoursAgo = new Date(now - 2 * 60 * 60 * 1000 - 15 * 60 * 1000).toISOString();
expect(formatUptime(twoHoursAgo)).toBe("2h 15m");
});
it("should format days and hours when 1 day or more", (): void => {
const now = Date.now();
const threeDaysAgo = new Date(now - 3 * 24 * 60 * 60 * 1000 - 5 * 60 * 60 * 1000).toISOString();
expect(formatUptime(threeDaysAgo)).toBe("3d 5h");
});
it("should handle 0 minutes", (): void => {
const now = Date.now();
const justNow = new Date(now - 30 * 1000).toISOString(); // 30 seconds ago
expect(formatUptime(justNow)).toBe("0m");
});
});
describe("formatImageId", () => {
it("should truncate sha256 image ID to 12 characters", (): void => {
const fullId = "sha256:abc123def456789012345678901234567890abcd";
expect(formatImageId(fullId)).toBe("abc123def456");
});
it("should handle ID without sha256 prefix", (): void => {
const shortId = "abc123def456789012345678";
expect(formatImageId(shortId)).toBe("abc123def456");
});
it("should return full ID if shorter than 12 chars", (): void => {
const shortId = "abc123";
expect(formatImageId(shortId)).toBe("abc123");
});
it("should handle empty string", (): void => {
expect(formatImageId("")).toBe("");
});
});