merge updatechecker into updater

This commit is contained in:
Girish Ramakrishnan
2025-06-26 13:41:09 +02:00
parent a085e9ed54
commit abd640d36b
8 changed files with 292 additions and 314 deletions

View File

@@ -7,27 +7,145 @@
const BoxError = require('../boxerror.js'),
common = require('./common.js'),
constants = require('../constants.js'),
expect = require('expect.js'),
nock = require('nock'),
paths = require('../paths.js'),
safe = require('safetydance'),
semver = require('semver'),
updater = require('../updater.js');
const UPDATE_VERSION = semver.inc(constants.VERSION, 'major');
describe('updater', function () {
const { setup, cleanup } = common;
const { setup, cleanup, app, appstoreToken, mockApiServerOrigin } = common;
before(setup);
after(cleanup);
describe('settings', function () {
before(setup);
after(cleanup);
it('can get default autoupdate_pattern', async function () {
const pattern = await updater.getAutoupdatePattern();
expect(pattern).to.be('00 00 1,3,5,23 * * *');
it('can get default autoupdate_pattern', async function () {
const pattern = await updater.getAutoupdatePattern();
expect(pattern).to.be('00 00 1,3,5,23 * * *');
});
it('cannot set invalid autoupdate_pattern', async function () {
const [error] = await safe(updater.setAutoupdatePattern('02 * 1 *'));
expect(error.reason).to.be(BoxError.BAD_FIELD);
});
it('can set default autoupdate_pattern', async function () {
await updater.setAutoupdatePattern('02 * 1-5 * * *');
});
});
it('cannot set invalid autoupdate_pattern', async function () {
const [error] = await safe(updater.setAutoupdatePattern('02 * 1 *'));
expect(error.reason).to.be(BoxError.BAD_FIELD);
});
describe('checker', function () {
before(setup);
before(() => { if (!nock.isActive()) nock.activate(); });
after(cleanup);
it('can set default autoupdate_pattern', async function () {
await updater.setAutoupdatePattern('02 * 1-5 * * *');
describe('box updates', function () {
before(async function () {
safe.fs.unlinkSync(paths.UPDATE_CHECKER_FILE);
await updater.setAutoupdatePattern(constants.AUTOUPDATE_PATTERN_NEVER);
});
it('no updates', async function () {
nock.cleanAll();
const scope = nock(mockApiServerOrigin)
.get('/api/v1/boxupdate')
.query({ boxVersion: constants.VERSION, accessToken: appstoreToken, stableOnly: false })
.reply(204, { } );
await updater.checkForUpdates({ stableOnly: false });
expect(updater.getUpdateInfoSync().box).to.not.be.ok();
expect(scope.isDone()).to.be.ok();
});
it('new version', async function () {
nock.cleanAll();
const scope = nock(mockApiServerOrigin)
.get('/api/v1/boxupdate')
.query({ boxVersion: constants.VERSION, accessToken: appstoreToken, stableOnly: false })
.reply(200, { version: UPDATE_VERSION, changelog: [''], sourceTarballUrl: 'box.tar.gz', sourceTarballSigUrl: 'box.tar.gz.sig', boxVersionsUrl: 'box.versions', boxVersionsSigUrl: 'box.versions.sig', unstable: false } );
await updater.checkForUpdates({ stableOnly: false });
expect(updater.getUpdateInfoSync().box).to.be.ok();
expect(updater.getUpdateInfoSync().box.version).to.be(UPDATE_VERSION);
expect(updater.getUpdateInfoSync().box.sourceTarballUrl).to.be('box.tar.gz');
expect(scope.isDone()).to.be.ok();
});
it('bad response offers whatever was last valid', async function () {
nock.cleanAll();
const scope = nock(mockApiServerOrigin)
.get('/api/v1/boxupdate')
.query({ boxVersion: constants.VERSION, accessToken: appstoreToken, stableOnly: false })
.reply(404, { version: '2.0.0-pre.0', changelog: [''], sourceTarballUrl: 'box-pre.tar.gz' } );
await updater.checkForUpdates({ stableOnly: false });
expect(updater.getUpdateInfoSync().box.version).to.be(UPDATE_VERSION);
expect(updater.getUpdateInfoSync().box.sourceTarballUrl).to.be('box.tar.gz');
expect(scope.isDone()).to.be.ok();
});
});
describe('app updates', function () {
before(async function () {
safe.fs.unlinkSync(paths.UPDATE_CHECKER_FILE);
await updater.setAutoupdatePattern(constants.AUTOUPDATE_PATTERN_NEVER);
});
it('no updates', async function () {
nock.cleanAll();
const scope = nock(mockApiServerOrigin)
.get('/api/v1/appupdate')
.query({ boxVersion: constants.VERSION, accessToken: appstoreToken, appId: app.appStoreId, appVersion: app.manifest.version, stableOnly: false })
.reply(204, { } );
await updater._checkAppUpdates({ stableOnly: false });
expect(updater.getUpdateInfoSync()).to.eql({});
expect(scope.isDone()).to.be.ok();
});
it('bad response', async function () {
nock.cleanAll();
const scope = nock(mockApiServerOrigin)
.get('/api/v1/appupdate')
.query({ boxVersion: constants.VERSION, accessToken: appstoreToken, appId: app.appStoreId, appVersion: app.manifest.version, stableOnly: false })
.reply(500, { update: { manifest: { version: '1.0.0', changelog: '* some changes' } } } );
await updater._checkAppUpdates({ stableOnly: false });
expect(updater.getUpdateInfoSync()).to.eql({});
expect(scope.isDone()).to.be.ok();
});
it('offers new version', async function () {
nock.cleanAll();
const scope = nock(mockApiServerOrigin)
.get('/api/v1/appupdate')
.query({ boxVersion: constants.VERSION, accessToken: appstoreToken, appId: app.appStoreId, appVersion: app.manifest.version, stableOnly: false })
.reply(200, { manifest: { version: '2.0.0', changelog: '* some changes' } } );
await updater._checkAppUpdates({ stableOnly: false });
expect(updater.getUpdateInfoSync()).to.eql({ 'appid': { manifest: { version: '2.0.0', changelog: '* some changes' }, unstable: false } });
expect(scope.isDone()).to.be.ok();
});
it('does not offer old version', async function () {
nock.cleanAll();
await updater._checkAppUpdates({ stableOnly: false });
expect(updater.getUpdateInfoSync()).to.eql({ });
});
});
});
});