Skip to content

Commit 4085b6c

Browse files
authored
chore(compass-main): refactor auto-updater to return objects when no update is available (#6849)
* Fix typo in test suite titles * Return { available: false } instead of null on no updates * Assert 200 status instead of simply ok
1 parent f78f0f1 commit 4085b6c

File tree

2 files changed

+56
-16
lines changed

2 files changed

+56
-16
lines changed

packages/compass/src/main/auto-update-manager.spec.ts

+21-6
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe('CompassAutoUpdateManager', function () {
109109
const stub = sandbox
110110
.stub(CompassAutoUpdateManager, 'checkForUpdate')
111111
.callsFake(() => {
112-
return Promise.resolve(null);
112+
return Promise.resolve({ available: false });
113113
});
114114

115115
expect(
@@ -128,7 +128,12 @@ describe('CompassAutoUpdateManager', function () {
128128
const stub = sandbox
129129
.stub(CompassAutoUpdateManager, 'checkForUpdate')
130130
.callsFake(() => {
131-
return Promise.resolve({ from: '0.0.0', to: '1.0.0', name: '1.0.0' });
131+
return Promise.resolve({
132+
available: true,
133+
from: '0.0.0',
134+
to: '1.0.0',
135+
name: '1.0.0',
136+
});
132137
});
133138

134139
expect(
@@ -147,7 +152,12 @@ describe('CompassAutoUpdateManager', function () {
147152
const stub = sandbox
148153
.stub(CompassAutoUpdateManager, 'checkForUpdate')
149154
.callsFake(() => {
150-
return Promise.resolve({ from: '0.0.0', to: '1.0.0', name: '1.0.0' });
155+
return Promise.resolve({
156+
available: true,
157+
from: '0.0.0',
158+
to: '1.0.0',
159+
name: '1.0.0',
160+
});
151161
});
152162

153163
expect(
@@ -165,7 +175,12 @@ describe('CompassAutoUpdateManager', function () {
165175
const stub = sandbox
166176
.stub(CompassAutoUpdateManager, 'checkForUpdate')
167177
.callsFake(() => {
168-
return wait(100, { from: '0.0.0', to: '1.0.0', name: '1.0.0' });
178+
return wait(100, {
179+
available: true,
180+
from: '0.0.0',
181+
to: '1.0.0',
182+
name: '1.0.0',
183+
});
169184
});
170185

171186
CompassAutoUpdateManager.setState(
@@ -191,7 +206,7 @@ describe('CompassAutoUpdateManager', function () {
191206
sandbox.stub(autoUpdater);
192207
});
193208

194-
describe('when electron does not support plaform updates', function () {
209+
describe('when electron does not support platform updates', function () {
195210
before(function () {
196211
if (supportsAutoupdates) {
197212
// eslint-disable-next-line no-console
@@ -233,7 +248,7 @@ describe('CompassAutoUpdateManager', function () {
233248
});
234249
});
235250

236-
describe('when electron supports plaform updates', function () {
251+
describe('when electron supports platform updates', function () {
237252
before(function () {
238253
if (!supportsAutoupdates) {
239254
// eslint-disable-next-line no-console

packages/compass/src/main/auto-update-manager.ts

+35-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from 'assert/strict';
12
import { EventEmitter } from 'events';
23
import os from 'os';
34
import { createLogger } from '@mongodb-js/compass-logging';
@@ -178,7 +179,7 @@ const checkForUpdates: StateEnterAction = async function checkForUpdates(
178179

179180
this.maybeInterrupt();
180181

181-
if (updateInfo) {
182+
if (updateInfo.available) {
182183
updateManager.setState(AutoUpdateManagerState.UpdateAvailable, updateInfo);
183184
} else {
184185
if (fromState === AutoUpdateManagerState.UserPromptedManualCheck) {
@@ -575,6 +576,18 @@ export type AutoUpdateManagerOptions = {
575576
initialUpdateDelay: number;
576577
};
577578

579+
type AutoUpdateResponse =
580+
| {
581+
available: true;
582+
name: string;
583+
from: string;
584+
to: string;
585+
}
586+
| {
587+
available: false;
588+
reason?: never;
589+
};
590+
578591
const emitter = new EventEmitter();
579592

580593
class CompassAutoUpdateManager {
@@ -618,26 +631,38 @@ class CompassAutoUpdateManager {
618631
return url;
619632
}
620633

621-
static async checkForUpdate(): Promise<{
622-
name: string;
623-
from: string;
624-
to: string;
625-
} | null> {
634+
static async checkForUpdate(): Promise<AutoUpdateResponse> {
626635
try {
627636
const response = await this.fetch((await this.getUpdateCheckURL()).href);
637+
628638
if (response.status !== 200) {
629-
return null;
639+
return { available: false };
630640
}
641+
631642
try {
632-
return (await response.json()) as any;
643+
const json = await response.json();
644+
assert(
645+
typeof json === 'object' && json !== null,
646+
'Expected response to be an object'
647+
);
648+
assert('name' in json, 'Expected "name" in response');
649+
assert('to' in json, 'Expected "to" in response');
650+
assert('from' in json, 'Expected "from" in response');
651+
652+
const { name, from, to } = json;
653+
assert(typeof name === 'string', 'Expected "name" to be a string');
654+
assert(typeof from === 'string', 'Expected "from" to be a string');
655+
assert(typeof to === 'string', 'Expected "to" to be a string');
656+
657+
return { available: true, name, from, to };
633658
} catch (err) {
634659
log.warn(
635660
mongoLogId(1_001_000_163),
636661
'AutoUpdateManager',
637662
'Failed to parse update info',
638663
{ error: (err as Error).message }
639664
);
640-
return null;
665+
return { available: false };
641666
}
642667
} catch (err) {
643668
log.warn(
@@ -646,7 +671,7 @@ class CompassAutoUpdateManager {
646671
'Failed to check for update',
647672
{ error: (err as Error).message }
648673
);
649-
return null;
674+
return { available: false };
650675
}
651676
}
652677

0 commit comments

Comments
 (0)