From bdb4e433c0e5c15b3acfc9295a47b6532b60e8b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Mon, 7 Apr 2025 11:43:43 +0200 Subject: [PATCH 1/3] Fix typo in test suite titles --- packages/compass/src/main/auto-update-manager.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/compass/src/main/auto-update-manager.spec.ts b/packages/compass/src/main/auto-update-manager.spec.ts index c0b73bb278a..d0aabce7026 100644 --- a/packages/compass/src/main/auto-update-manager.spec.ts +++ b/packages/compass/src/main/auto-update-manager.spec.ts @@ -191,7 +191,7 @@ describe('CompassAutoUpdateManager', function () { sandbox.stub(autoUpdater); }); - describe('when electron does not support plaform updates', function () { + describe('when electron does not support platform updates', function () { before(function () { if (supportsAutoupdates) { // eslint-disable-next-line no-console @@ -233,7 +233,7 @@ describe('CompassAutoUpdateManager', function () { }); }); - describe('when electron supports plaform updates', function () { + describe('when electron supports platform updates', function () { before(function () { if (!supportsAutoupdates) { // eslint-disable-next-line no-console From 140a0c51842c239ae73bd89c6779ee80ab02df97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Mon, 7 Apr 2025 13:55:51 +0200 Subject: [PATCH 2/3] Return { available: false } instead of null on no updates --- .../src/main/auto-update-manager.spec.ts | 23 +++++++-- .../compass/src/main/auto-update-manager.ts | 48 ++++++++++++++----- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/packages/compass/src/main/auto-update-manager.spec.ts b/packages/compass/src/main/auto-update-manager.spec.ts index d0aabce7026..00e01aee6b8 100644 --- a/packages/compass/src/main/auto-update-manager.spec.ts +++ b/packages/compass/src/main/auto-update-manager.spec.ts @@ -109,7 +109,7 @@ describe('CompassAutoUpdateManager', function () { const stub = sandbox .stub(CompassAutoUpdateManager, 'checkForUpdate') .callsFake(() => { - return Promise.resolve(null); + return Promise.resolve({ available: false }); }); expect( @@ -128,7 +128,12 @@ describe('CompassAutoUpdateManager', function () { const stub = sandbox .stub(CompassAutoUpdateManager, 'checkForUpdate') .callsFake(() => { - return Promise.resolve({ from: '0.0.0', to: '1.0.0', name: '1.0.0' }); + return Promise.resolve({ + available: true, + from: '0.0.0', + to: '1.0.0', + name: '1.0.0', + }); }); expect( @@ -147,7 +152,12 @@ describe('CompassAutoUpdateManager', function () { const stub = sandbox .stub(CompassAutoUpdateManager, 'checkForUpdate') .callsFake(() => { - return Promise.resolve({ from: '0.0.0', to: '1.0.0', name: '1.0.0' }); + return Promise.resolve({ + available: true, + from: '0.0.0', + to: '1.0.0', + name: '1.0.0', + }); }); expect( @@ -165,7 +175,12 @@ describe('CompassAutoUpdateManager', function () { const stub = sandbox .stub(CompassAutoUpdateManager, 'checkForUpdate') .callsFake(() => { - return wait(100, { from: '0.0.0', to: '1.0.0', name: '1.0.0' }); + return wait(100, { + available: true, + from: '0.0.0', + to: '1.0.0', + name: '1.0.0', + }); }); CompassAutoUpdateManager.setState( diff --git a/packages/compass/src/main/auto-update-manager.ts b/packages/compass/src/main/auto-update-manager.ts index 7b82e179eef..da954e7ce99 100644 --- a/packages/compass/src/main/auto-update-manager.ts +++ b/packages/compass/src/main/auto-update-manager.ts @@ -1,3 +1,4 @@ +import assert from 'assert/strict'; import { EventEmitter } from 'events'; import os from 'os'; import { createLogger } from '@mongodb-js/compass-logging'; @@ -178,7 +179,7 @@ const checkForUpdates: StateEnterAction = async function checkForUpdates( this.maybeInterrupt(); - if (updateInfo) { + if (updateInfo.available) { updateManager.setState(AutoUpdateManagerState.UpdateAvailable, updateInfo); } else { if (fromState === AutoUpdateManagerState.UserPromptedManualCheck) { @@ -575,6 +576,18 @@ export type AutoUpdateManagerOptions = { initialUpdateDelay: number; }; +type AutoUpdateResponse = + | { + available: true; + name: string; + from: string; + to: string; + } + | { + available: false; + reason?: never; + }; + const emitter = new EventEmitter(); class CompassAutoUpdateManager { @@ -618,18 +631,29 @@ class CompassAutoUpdateManager { return url; } - static async checkForUpdate(): Promise<{ - name: string; - from: string; - to: string; - } | null> { + static async checkForUpdate(): Promise { try { const response = await this.fetch((await this.getUpdateCheckURL()).href); - if (response.status !== 200) { - return null; - } + + + assert(response.ok); + try { - return (await response.json()) as any; + const json = await response.json(); + assert( + typeof json === 'object' && json !== null, + 'Expected response to be an object' + ); + assert('name' in json, 'Expected "name" in response'); + assert('to' in json, 'Expected "to" in response'); + assert('from' in json, 'Expected "from" in response'); + + const { name, from, to } = json; + assert(typeof name === 'string', 'Expected "name" to be a string'); + assert(typeof from === 'string', 'Expected "from" to be a string'); + assert(typeof to === 'string', 'Expected "to" to be a string'); + + return { available: true, name, from, to }; } catch (err) { log.warn( mongoLogId(1_001_000_163), @@ -637,7 +661,7 @@ class CompassAutoUpdateManager { 'Failed to parse update info', { error: (err as Error).message } ); - return null; + return { available: false }; } } catch (err) { log.warn( @@ -646,7 +670,7 @@ class CompassAutoUpdateManager { 'Failed to check for update', { error: (err as Error).message } ); - return null; + return { available: false }; } } From c7f52baa0ac03bef3db9ccf510a991707b2c2116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Wed, 9 Apr 2025 13:09:55 +0200 Subject: [PATCH 3/3] Assert 200 status instead of simply ok --- packages/compass/src/main/auto-update-manager.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/compass/src/main/auto-update-manager.ts b/packages/compass/src/main/auto-update-manager.ts index da954e7ce99..6041be187e6 100644 --- a/packages/compass/src/main/auto-update-manager.ts +++ b/packages/compass/src/main/auto-update-manager.ts @@ -635,8 +635,9 @@ class CompassAutoUpdateManager { try { const response = await this.fetch((await this.getUpdateCheckURL()).href); - - assert(response.ok); + if (response.status !== 200) { + return { available: false }; + } try { const json = await response.json();