Skip to content

Commit

Permalink
馃悰 Fix the eta shown when installing the browser (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wil Wilsman committed Sep 28, 2021
1 parent bd90409 commit 566306a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 4 additions & 5 deletions packages/core/src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ function formatBytes(int) {

// Formats milleseconds as "MM:SS"
function formatTime(ms) {
let minutes = (ms / 1000 / 60).toFixed().padStart(2, '0');
let seconds = (ms / 1000).toFixed().padStart(2, '0');
let minutes = (ms / 1000 / 60).toString().split('.')[0].padStart(2, '0');
let seconds = (ms / 1000 % 60).toFixed().padStart(2, '0');
return `${minutes}:${seconds}`;
}

Expand All @@ -33,8 +33,7 @@ function formatProgress(prefix, total, start, progress) {
let barContent = Array(Math.max(0, barLen + 1)).join('=') + (
Array(Math.max(0, width - barLen + 1)).join(' '));

let elapsed = new Date() - start;
/* istanbul ignore next: eta in testing is always 0 */
let elapsed = Date.now() - start;
let eta = (ratio >= 1) ? 0 : elapsed * (total / progress - 1);

return (
Expand Down Expand Up @@ -137,7 +136,7 @@ async function install({
let start, progress;

response.on('data', chunk => {
start ??= new Date();
start ??= Date.now();
progress = (progress ?? 0) + chunk.length;
log.progress(formatProgress(premsg, total, start, progress));
});
Expand Down
9 changes: 8 additions & 1 deletion packages/core/test/unit/install.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ describe('Unit / Install', () => {
let dlnock, dlcallback, options;

beforeEach(() => {
logger.mock();

// emulate tty properties for testing
Object.assign(logger.constructor.stdout, {
isTTY: true,
Expand Down Expand Up @@ -78,12 +80,17 @@ describe('Unit / Install', () => {
});

it('logs progress during the archive download', async () => {
let now = Date.now();
// eta is calculated by the elapsed time and remaining progress
spyOn(Date, 'now').and.callFake(() => (now += 65002));
dlcallback.and.callFake(s => [200, s, { 'content-length': s.length * 5 }]);

await install(options);

expect(logger.stderr).toEqual([]);
expect(logger.stdout).toEqual([
'[percy] Downloading Archive v0...',
'[percy] Downloading Archive v0 [====================] 16B/16B 100% 00:00',
'[percy] Downloading Archive v0 [==== ] 16B/80B 20% 04:20',
'[percy] Successfully downloaded Archive v0'
]);
});
Expand Down

0 comments on commit 566306a

Please sign in to comment.