mirror of
https://github.com/debba/wp-media-rewind.git
synced 2026-08-03 07:29:00 +02:00
63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { ProgressBar } from "../src/utils/progress.js";
|
|
|
|
test("format reflects ticks", () => {
|
|
const bar = new ProgressBar({ total: 4, width: 10, enabled: false });
|
|
bar.tick("ok");
|
|
bar.tick("skipped");
|
|
const line = bar.format();
|
|
assert.match(line, /50%/);
|
|
assert.match(line, /2\/4/);
|
|
assert.match(line, /ok:1/);
|
|
assert.match(line, /skip:1/);
|
|
assert.match(line, /fail:0/);
|
|
});
|
|
|
|
test("snapshot returns running totals", () => {
|
|
const bar = new ProgressBar({ total: 3, enabled: false });
|
|
bar.tick("ok");
|
|
bar.tick("failed");
|
|
assert.deepEqual(bar.snapshot(), { ok: 1, skipped: 0, failed: 1, done: 2 });
|
|
});
|
|
|
|
test("format reaches 100% on completion", () => {
|
|
const bar = new ProgressBar({ total: 2, width: 4, enabled: false });
|
|
bar.tick("ok");
|
|
bar.tick("ok");
|
|
assert.match(bar.format(), /100%/);
|
|
});
|
|
|
|
test("disabled bar does not write to stream", () => {
|
|
let written = "";
|
|
const fakeStream = {
|
|
isTTY: false,
|
|
write: (s: string) => {
|
|
written += s;
|
|
return true;
|
|
},
|
|
} as unknown as NodeJS.WriteStream;
|
|
const bar = new ProgressBar({ total: 1, stream: fakeStream });
|
|
bar.tick("ok");
|
|
bar.finish();
|
|
assert.equal(written, "");
|
|
});
|
|
|
|
test("enabled bar writes carriage-return updates", () => {
|
|
let written = "";
|
|
const fakeStream = {
|
|
isTTY: true,
|
|
write: (s: string) => {
|
|
written += s;
|
|
return true;
|
|
},
|
|
} as unknown as NodeJS.WriteStream;
|
|
const bar = new ProgressBar({ total: 2, width: 4, stream: fakeStream });
|
|
bar.tick("ok");
|
|
bar.tick("ok");
|
|
bar.finish();
|
|
assert.ok(written.startsWith("\r"));
|
|
assert.ok(written.endsWith("\n"));
|
|
assert.match(written, /100%/);
|
|
});
|