okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8820fa433
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { build } from 'esbuild' ; 4 5const built = await build ({ stdin :{ contents :"export * from './src/lib/download-eta';" , resolveDir :process . cwd () }, bundle :true , write :false , format :'esm' , platform :'node' , loader :{ '.json' :'json' }, external :[ 'react' ] }); 6const src = built . outputFiles [ 0 ]. text . replace ( / from\s * [ "' ] react [ "' ] / , 'from "data:text/javascript,export const useRef=()=>({current:null}),useEffect=()=>{},useState=v=>[v,()=>{}]"' ); 7const { etaMinutes, etaLabel, installProgress} = await import ( `data:text/javascript;base64, ${ Buffer . from ( src ). toString ( 'base64' )} ` ); 8 9test ( 'the time left comes from the recent rate, and says nothing until there is one' , () => { 10assert . equal ( etaMinutes ([{ at :0 , done :1 }], 50 ), null ); 11assert . equal ( etaMinutes ([{ at :0 , done :1 }, { at :5000 , done :3 }], 50 ), null , 'under ten seconds of samples' ); 12assert . equal ( etaMinutes ([{ at :0 , done :1 }, { at :20000 , done :1 }], 50 ), null , 'nothing moved' ); 13assert . equal ( etaMinutes ([{ at :0 , done :0 }, { at :20000 , done :10 }], 60 ), 2 , '0.5 GB/s, 60 GB left: 120 s' ); 14assert . equal ( etaMinutes ([{ at :0 , done :0 }, { at :60000 , done :1 }], 0.1 ), 1 , 'never below one minute' ); 15}); 16 17test ( 'the label is minutes, and hours past an hour and a half' , () => { 18assert . equal ( etaLabel ( null ), '' ); 19assert . equal ( etaLabel ( 3 ), 'About 3 min left' ); 20assert . equal ( etaLabel ( 125 ), 'About 2 h 5 min left' ); 21assert . equal ( etaLabel ( 120 ), 'About 2 h left' ); 22}); 23 24test ( 'one count over all the parts of the job: the model files and, when there is one, the engine' , () => { 25const model = { kind :'model' , done_gb :30 , total_gb :60 , state :'downloading' }; 26const engine = { kind :'engine' , done_gb :0.3 , total_gb :0.7 , state :'downloading' }; 27const both = installProgress ({ parts :[ model , engine ], download :{ done_gb :0 , total_gb :1 } }); 28assert . equal ( both . total_gb , 60.7 ); 29assert . ok ( Math . abs ( both . done_gb - 30.3 ) < 1e-9 ); 30assert . equal ( both . percent , 49 ); 31assert . ok ( Math . abs ( both . remaining_gb - 30.4 ) < 1e-9 ); 32const modelOnly = installProgress ({ parts :[ model ] }); 33assert . equal ( modelOnly . total_gb , 60 ); 34assert . equal ( installProgress ({ parts :[ model , { ...engine , state :'skipped' }, { kind :'engine' , state :'current' , total_gb :1 , done_gb :1 }] }). total_gb , 60 , 'a part that is skipped or already current is not downloaded' ); 35// no parts: the job's own combined figures 36assert . deepEqual ( installProgress ({ download :{ done_gb :5 , total_gb :50 , percent :10 } }), { done_gb :5 , total_gb :50 , remaining_gb :45 , percent :10 , file :undefined }); 37assert . equal ( installProgress ({}), undefined ); 38assert . equal ( installProgress ( undefined ), undefined ); 39});