mirror of
https://github.com/debba/wp-media-rewind.git
synced 2026-08-03 07:29:00 +02:00
91 lines
2.3 KiB
TypeScript
91 lines
2.3 KiB
TypeScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { buildSummary, formatBytes, formatDuration } from "../src/utils/summary.js";
|
|
|
|
test("formatBytes scales units", () => {
|
|
assert.equal(formatBytes(0), "0 B");
|
|
assert.equal(formatBytes(512), "512 B");
|
|
assert.equal(formatBytes(2048), "2.00 KB");
|
|
assert.equal(formatBytes(5 * 1024 * 1024), "5.00 MB");
|
|
assert.equal(formatBytes(3 * 1024 ** 3), "3.00 GB");
|
|
});
|
|
|
|
test("formatDuration formats ranges", () => {
|
|
assert.equal(formatDuration(250), "250ms");
|
|
assert.equal(formatDuration(1500), "2s");
|
|
assert.equal(formatDuration(65_000), "1m5s");
|
|
assert.equal(formatDuration(3_725_000), "1h2m5s");
|
|
});
|
|
|
|
test("buildSummary includes counters and rate", () => {
|
|
const out = buildSummary({
|
|
totalFound: 100,
|
|
attachmentGuids: 40,
|
|
ok: 80,
|
|
skipped: 10,
|
|
failed: 10,
|
|
bytes: 1024,
|
|
elapsedMs: 5000,
|
|
outputDir: "/tmp/out",
|
|
});
|
|
assert.match(out, /URLs found in dump : 100 \(40 attachment GUIDs\)/);
|
|
assert.match(out, /✓ Downloaded\s*: 80/);
|
|
assert.match(out, /↷ Skipped\s*\(exists\)\s*: 10/);
|
|
assert.match(out, /✗ Failed\s*: 10/);
|
|
assert.match(out, /Success rate\s*: 80%/);
|
|
assert.match(out, /1\.00 KB/);
|
|
assert.match(out, /5s/);
|
|
assert.match(out, /\/tmp\/out/);
|
|
});
|
|
|
|
test("buildSummary lists failures with truncation", () => {
|
|
const failures = Array.from({ length: 12 }, (_, i) => ({
|
|
url: `https://x.test/f${i}.jpg`,
|
|
reason: "no_snapshot",
|
|
}));
|
|
const out = buildSummary({
|
|
totalFound: 12,
|
|
attachmentGuids: 0,
|
|
ok: 0,
|
|
skipped: 0,
|
|
failed: 12,
|
|
bytes: 0,
|
|
elapsedMs: 100,
|
|
outputDir: "/o",
|
|
failures,
|
|
maxFailuresShown: 5,
|
|
});
|
|
assert.match(out, /f0\.jpg/);
|
|
assert.match(out, /f4\.jpg/);
|
|
assert.ok(!out.includes("f5.jpg"));
|
|
assert.match(out, /and 7 more/);
|
|
});
|
|
|
|
test("buildSummary omits manifest line when not provided", () => {
|
|
const out = buildSummary({
|
|
totalFound: 1,
|
|
attachmentGuids: 0,
|
|
ok: 1,
|
|
skipped: 0,
|
|
failed: 0,
|
|
bytes: 100,
|
|
elapsedMs: 10,
|
|
outputDir: "/o",
|
|
});
|
|
assert.ok(!out.includes("Manifest"));
|
|
});
|
|
|
|
test("buildSummary handles 0 processed", () => {
|
|
const out = buildSummary({
|
|
totalFound: 0,
|
|
attachmentGuids: 0,
|
|
ok: 0,
|
|
skipped: 0,
|
|
failed: 0,
|
|
bytes: 0,
|
|
elapsedMs: 5,
|
|
outputDir: "/o",
|
|
});
|
|
assert.match(out, /Success rate\s*: n\/a/);
|
|
});
|