Skip to content

refactor(cli/unstable): use Date.now() internally and use FakeTime for testing #6686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 30, 2025
Merged
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
4 changes: 2 additions & 2 deletions cli/unstable_progress_bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,15 @@ export class ProgressBar {
.pipeTo(writable, { preventClose: this.#keepOpen })
.catch(() => clearInterval(this.#id));
this.#writer = stream.writable.getWriter();
this.#startTime = performance.now();
this.#startTime = Date.now();
this.#previousTime = 0;
this.#previousValue = this.value;

this.#id = setInterval(() => this.#print(), 1000);
this.#print();
}
#createFormatterObject() {
const time = performance.now() - this.#startTime;
const time = Date.now() - this.#startTime;

const size = this.value / this.max * this.#barLength | 0;
const fillChars = this.#fillChar.repeat(size);
Expand Down
66 changes: 47 additions & 19 deletions cli/unstable_progress_bar_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,57 @@

import { assertEquals } from "@std/assert";
import { ProgressBar } from "./unstable_progress_bar.ts";

async function* getData(
loops: number,
bufferSize: number,
): AsyncGenerator<Uint8Array> {
for (let i = 0; i < loops; ++i) {
yield new Uint8Array(bufferSize);
await new Promise((a) => setTimeout(a, Math.random() * 100));
}
}
import { FakeTime } from "@std/testing/time";

const decoder = new TextDecoder();

Deno.test("ProgressBar() outputs default result", async () => {
using fakeTime = new FakeTime();

const { readable, writable } = new TransformStream();
const bar = new ProgressBar({ writable, max: 10 * 1000 });
for (let index = 0; index < 10; index++) {
bar.value += 1000;
fakeTime.tick(1000);
}
bar.stop().then(() => writable.close());

const expected = [
"\r\x1b[K[00:00] [--------------------------------------------------] [0.00/9.77 KiB]",
"\r\x1b[K[00:01] [#####---------------------------------------------] [0.98/9.77 KiB]",
"\r\x1b[K[00:02] [##########----------------------------------------] [1.95/9.77 KiB]",
"\r\x1b[K[00:03] [###############-----------------------------------] [2.93/9.77 KiB]",
"\r\x1b[K[00:04] [####################------------------------------] [3.91/9.77 KiB]",
"\r\x1b[K[00:05] [#########################-------------------------] [4.88/9.77 KiB]",
"\r\x1b[K[00:06] [##############################--------------------] [5.86/9.77 KiB]",
"\r\x1b[K[00:07] [###################################---------------] [6.84/9.77 KiB]",
"\r\x1b[K[00:08] [########################################----------] [7.81/9.77 KiB]",
"\r\x1b[K[00:09] [#############################################-----] [8.79/9.77 KiB]",
"\r\x1b[K[00:10] [##################################################] [9.77/9.77 KiB]",
"\r\x1b[K[00:10] [##################################################] [9.77/9.77 KiB]",
"\n",
];

for await (const a of getData(10, 1000)) bar.value += a.length;
const actual: string[] = [];
for await (const buffer of readable) {
actual.push(decoder.decode(buffer));
}
assertEquals(actual, expected);
});

Deno.test("ProgressBar() prints every second", async () => {
using fakeTime = new FakeTime();
const { readable, writable } = new TransformStream();
const bar = new ProgressBar({ writable, max: 10 * 1000 });
fakeTime.tick(3000);
bar.stop().then(() => writable.close());

const expected = [
"\r\x1b[K[00:00] [--------------------------------------------------] [0.00/9.77 KiB]",
"\r\x1b[K[00:00] [##################################################] [9.77/9.77 KiB]",
"\r\x1b[K[00:01] [--------------------------------------------------] [0.00/9.77 KiB]",
"\r\x1b[K[00:02] [--------------------------------------------------] [0.00/9.77 KiB]",
"\r\x1b[K[00:03] [--------------------------------------------------] [0.00/9.77 KiB]",
"\r\x1b[K[00:03] [--------------------------------------------------] [0.00/9.77 KiB]",
"\n",
];

Expand All @@ -36,20 +64,17 @@ Deno.test("ProgressBar() outputs default result", async () => {
});

Deno.test("ProgressBar() can handle a readable.cancel() correctly", async () => {
using _fakeTime = new FakeTime();
const { readable, writable } = new TransformStream();
const bar = new ProgressBar({ writable, max: 10 * 1000 });

for await (const a of getData(10, 1000)) bar.value += a.length;
bar.stop();

await readable.cancel();
});

Deno.test("ProgressBar() can remove itself when finished", async () => {
using _fakeTime = new FakeTime();
const { readable, writable } = new TransformStream();
const bar = new ProgressBar({ writable, max: 10 * 1000, clear: true });

for await (const a of getData(10, 1000)) bar.value += a.length;
bar.stop().then(() => writable.close());

const expected = [
Expand All @@ -65,14 +90,17 @@ Deno.test("ProgressBar() can remove itself when finished", async () => {
});

Deno.test("ProgressBar() passes correct values to formatter", async () => {
using _fakeTime = new FakeTime();
const { readable, writable } = new TransformStream();
let lastTime: undefined | number = undefined;
let lastValue: undefined | number = undefined;
let called = false;
const bar = new ProgressBar({
writable,
max: 10 * 1000,
keepOpen: false,
fmt(x) {
called = true;
if (lastTime != undefined) assertEquals(x.previousTime, lastTime);
if (lastValue != undefined) assertEquals(x.previousValue, lastValue);
lastTime = x.time;
Expand All @@ -81,9 +109,9 @@ Deno.test("ProgressBar() passes correct values to formatter", async () => {
},
});

for await (const a of getData(10, 1000)) bar.value += a.length;
bar.value += 1000;
bar.stop();

assertEquals(called, true);
await new Response(readable).bytes();
});

Expand Down
Loading