Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/cli/src/ui/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { describe, expect, it } from "vitest";

import {
formatBytes,
formatDuration,
formatRenderPipelineDetail,
formatRenderSummaryDetail,
formatScreenshotFallbackHint,
Expand Down Expand Up @@ -168,3 +170,30 @@ describe("formatRenderSummaryDetail", () => {
);
});
});

describe("formatDuration", () => {
it("shows seconds with one decimal under a minute", () => {
expect(formatDuration(0)).toBe("0.0s");
expect(formatDuration(1234)).toBe("1.2s");
expect(formatDuration(59_940)).toBe("59.9s");
});

it("carries a rounded-up 60.0 seconds into the next minute", () => {
expect(formatDuration(59_960)).toBe("1m 0.0s");
expect(formatDuration(119_970)).toBe("2m 0.0s");
});

it("splits minutes and seconds past one minute", () => {
expect(formatDuration(60_000)).toBe("1m 0.0s");
expect(formatDuration(83_400)).toBe("1m 23.4s");
});
});

describe("formatBytes", () => {
it("steps up a unit when rounding reaches the next one", () => {
expect(formatBytes(1023)).toBe("1023 B");
expect(formatBytes(1536)).toBe("1.5 KB");
expect(formatBytes(1_048_575)).toBe("1.0 MB");
expect(formatBytes(1_048_576)).toBe("1.0 MB");
});
});
12 changes: 6 additions & 6 deletions packages/cli/src/ui/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { c } from "./colors.js";

export function formatBytes(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
const kb = Math.round((bytes / 1024) * 10) / 10;
if (kb < 1024) return `${kb.toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}

export function formatDuration(ms: number): string {
const seconds = ms / 1000;
if (seconds < 60) return `${seconds.toFixed(1)}s`;
const minutes = Math.floor(seconds / 60);
const remaining = seconds - minutes * 60;
return `${minutes}m ${remaining.toFixed(1)}s`;
const tenths = Math.round(ms / 100);
if (tenths < 600) return `${(tenths / 10).toFixed(1)}s`;
const minutes = Math.floor(tenths / 600);
return `${minutes}m ${((tenths - minutes * 600) / 10).toFixed(1)}s`;
}

/**
Expand Down
Loading