22
33import { assertEquals } from "@std/assert" ;
44import { ProgressBar } from "./unstable_progress_bar.ts" ;
5-
6- async function * getData (
7- loops : number ,
8- bufferSize : number ,
9- ) : AsyncGenerator < Uint8Array > {
10- for ( let i = 0 ; i < loops ; ++ i ) {
11- yield new Uint8Array ( bufferSize ) ;
12- await new Promise ( ( a ) => setTimeout ( a , Math . random ( ) * 100 ) ) ;
13- }
14- }
5+ import { FakeTime } from "@std/testing/time" ;
156
167const decoder = new TextDecoder ( ) ;
178
189Deno . test ( "ProgressBar() outputs default result" , async ( ) => {
10+ using fakeTime = new FakeTime ( ) ;
11+
1912 const { readable, writable } = new TransformStream ( ) ;
2013 const bar = new ProgressBar ( { writable, max : 10 * 1000 } ) ;
14+ for ( let index = 0 ; index < 10 ; index ++ ) {
15+ bar . value += 1000 ;
16+ fakeTime . tick ( 1000 ) ;
17+ }
18+ bar . stop ( ) . then ( ( ) => writable . close ( ) ) ;
19+
20+ const expected = [
21+ "\r\x1b[K[00:00] [--------------------------------------------------] [0.00/9.77 KiB]" ,
22+ "\r\x1b[K[00:01] [#####---------------------------------------------] [0.98/9.77 KiB]" ,
23+ "\r\x1b[K[00:02] [##########----------------------------------------] [1.95/9.77 KiB]" ,
24+ "\r\x1b[K[00:03] [###############-----------------------------------] [2.93/9.77 KiB]" ,
25+ "\r\x1b[K[00:04] [####################------------------------------] [3.91/9.77 KiB]" ,
26+ "\r\x1b[K[00:05] [#########################-------------------------] [4.88/9.77 KiB]" ,
27+ "\r\x1b[K[00:06] [##############################--------------------] [5.86/9.77 KiB]" ,
28+ "\r\x1b[K[00:07] [###################################---------------] [6.84/9.77 KiB]" ,
29+ "\r\x1b[K[00:08] [########################################----------] [7.81/9.77 KiB]" ,
30+ "\r\x1b[K[00:09] [#############################################-----] [8.79/9.77 KiB]" ,
31+ "\r\x1b[K[00:10] [##################################################] [9.77/9.77 KiB]" ,
32+ "\r\x1b[K[00:10] [##################################################] [9.77/9.77 KiB]" ,
33+ "\n" ,
34+ ] ;
2135
22- for await ( const a of getData ( 10 , 1000 ) ) bar . value += a . length ;
36+ const actual : string [ ] = [ ] ;
37+ for await ( const buffer of readable ) {
38+ actual . push ( decoder . decode ( buffer ) ) ;
39+ }
40+ assertEquals ( actual , expected ) ;
41+ } ) ;
42+
43+ Deno . test ( "ProgressBar() prints every second" , async ( ) => {
44+ using fakeTime = new FakeTime ( ) ;
45+ const { readable, writable } = new TransformStream ( ) ;
46+ const bar = new ProgressBar ( { writable, max : 10 * 1000 } ) ;
47+ fakeTime . tick ( 3000 ) ;
2348 bar . stop ( ) . then ( ( ) => writable . close ( ) ) ;
2449
2550 const expected = [
2651 "\r\x1b[K[00:00] [--------------------------------------------------] [0.00/9.77 KiB]" ,
27- "\r\x1b[K[00:00] [##################################################] [9.77/9.77 KiB]" ,
52+ "\r\x1b[K[00:01] [--------------------------------------------------] [0.00/9.77 KiB]" ,
53+ "\r\x1b[K[00:02] [--------------------------------------------------] [0.00/9.77 KiB]" ,
54+ "\r\x1b[K[00:03] [--------------------------------------------------] [0.00/9.77 KiB]" ,
55+ "\r\x1b[K[00:03] [--------------------------------------------------] [0.00/9.77 KiB]" ,
2856 "\n" ,
2957 ] ;
3058
@@ -36,20 +64,17 @@ Deno.test("ProgressBar() outputs default result", async () => {
3664} ) ;
3765
3866Deno . test ( "ProgressBar() can handle a readable.cancel() correctly" , async ( ) => {
67+ using _fakeTime = new FakeTime ( ) ;
3968 const { readable, writable } = new TransformStream ( ) ;
4069 const bar = new ProgressBar ( { writable, max : 10 * 1000 } ) ;
41-
42- for await ( const a of getData ( 10 , 1000 ) ) bar . value += a . length ;
4370 bar . stop ( ) ;
44-
4571 await readable . cancel ( ) ;
4672} ) ;
4773
4874Deno . test ( "ProgressBar() can remove itself when finished" , async ( ) => {
75+ using _fakeTime = new FakeTime ( ) ;
4976 const { readable, writable } = new TransformStream ( ) ;
5077 const bar = new ProgressBar ( { writable, max : 10 * 1000 , clear : true } ) ;
51-
52- for await ( const a of getData ( 10 , 1000 ) ) bar . value += a . length ;
5378 bar . stop ( ) . then ( ( ) => writable . close ( ) ) ;
5479
5580 const expected = [
@@ -65,14 +90,17 @@ Deno.test("ProgressBar() can remove itself when finished", async () => {
6590} ) ;
6691
6792Deno . test ( "ProgressBar() passes correct values to formatter" , async ( ) => {
93+ using _fakeTime = new FakeTime ( ) ;
6894 const { readable, writable } = new TransformStream ( ) ;
6995 let lastTime : undefined | number = undefined ;
7096 let lastValue : undefined | number = undefined ;
97+ let called = false ;
7198 const bar = new ProgressBar ( {
7299 writable,
73100 max : 10 * 1000 ,
74101 keepOpen : false ,
75102 fmt ( x ) {
103+ called = true ;
76104 if ( lastTime != undefined ) assertEquals ( x . previousTime , lastTime ) ;
77105 if ( lastValue != undefined ) assertEquals ( x . previousValue , lastValue ) ;
78106 lastTime = x . time ;
@@ -81,9 +109,9 @@ Deno.test("ProgressBar() passes correct values to formatter", async () => {
81109 } ,
82110 } ) ;
83111
84- for await ( const a of getData ( 10 , 1000 ) ) bar . value += a . length ;
112+ bar . value += 1000 ;
85113 bar . stop ( ) ;
86-
114+ assertEquals ( called , true ) ;
87115 await new Response ( readable ) . bytes ( ) ;
88116} ) ;
89117
0 commit comments