2022-01-24 06:59:58 +00:00
|
|
|
import { setTimeout, setImmediate } from 'timers/promises'
|
|
|
|
import { Eltro as t, assert, stub } from 'eltro'
|
|
|
|
import fs from 'fs/promises'
|
|
|
|
import Application from '../core/application.mjs'
|
|
|
|
import Util from '../core/util.mjs'
|
2022-01-28 07:03:18 +00:00
|
|
|
import StaticProvider from '../core/providers/static.mjs'
|
2022-02-04 09:33:03 +00:00
|
|
|
import { createFakeContext } from './helpers.mjs'
|
2022-01-24 06:59:58 +00:00
|
|
|
|
|
|
|
const util = new Util(import.meta.url)
|
|
|
|
|
2022-01-27 18:25:33 +00:00
|
|
|
const logger = {
|
2022-01-24 06:59:58 +00:00
|
|
|
info: stub(),
|
|
|
|
warn: stub(),
|
|
|
|
error: stub(),
|
|
|
|
}
|
|
|
|
function createProvider() {
|
|
|
|
return {
|
|
|
|
getLatestVersion: stub(),
|
|
|
|
downloadVersion: stub(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.describe('constructor()', function() {
|
2022-02-04 09:33:03 +00:00
|
|
|
let ctx
|
2022-01-24 06:59:58 +00:00
|
|
|
|
|
|
|
t.beforeEach(function() {
|
2022-02-04 09:33:03 +00:00
|
|
|
return createFakeContext()
|
|
|
|
.then(function(res) { ctx = res })
|
2022-01-24 06:59:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should auto-create application', function() {
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.notOk(ctx.db.data.core.test)
|
2022-01-24 06:59:58 +00:00
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
new Application(ctx, {}, 'test')
|
2022-01-24 06:59:58 +00:00
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.ok(ctx.db.data.core.test)
|
|
|
|
assert.ok(ctx.db.data.core.test.versions)
|
|
|
|
assert.strictEqual(ctx.db.data.core.test.active, '')
|
|
|
|
assert.strictEqual(ctx.db.data.core.test.latestInstalled, '')
|
|
|
|
assert.strictEqual(ctx.db.data.core.test.latestVersion, '')
|
2022-01-24 06:59:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should keep config and other of itself', function() {
|
|
|
|
const assertTest = { a: 1 }
|
|
|
|
const assertName = 'test'
|
2022-02-04 09:33:03 +00:00
|
|
|
ctx.db.config = {
|
2022-01-24 06:59:58 +00:00
|
|
|
test: assertTest,
|
|
|
|
app: { b: 2},
|
|
|
|
manage: { c: 3 },
|
|
|
|
}
|
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
let app = new Application(ctx, {}, assertName)
|
|
|
|
assert.notStrictEqual(app.config, assertTest)
|
|
|
|
assert.strictEqual(app.config.a, assertTest.a)
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.strictEqual(app.config.updateEvery, 180)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(app.config.startWaitUntilFail, 60 * 1000)
|
|
|
|
assert.strictEqual(app.config.heartbeatTimeout, 3 * 1000)
|
|
|
|
assert.strictEqual(app.config.heartbeatAttempts, 5)
|
|
|
|
assert.strictEqual(app.config.heartbeatAttemptsWait, 2 * 1000)
|
|
|
|
assert.strictEqual(app.config.heartbeatPath, '/')
|
|
|
|
assert.strictEqual(app.ctx.db, ctx.db)
|
|
|
|
assert.strictEqual(app.ctx.app, app)
|
|
|
|
assert.strictEqual(app.ctx.util, ctx.util)
|
2022-01-24 06:59:58 +00:00
|
|
|
assert.strictEqual(app.name, assertName)
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.strictEqual(app.fresh, true)
|
|
|
|
assert.strictEqual(app.running, false)
|
|
|
|
assert.ok(app.http)
|
|
|
|
assert.ok(app.http.sockets)
|
|
|
|
assert.strictEqual(typeof(app.http.createServer), 'function')
|
|
|
|
assert.strictEqual(typeof(app.http.closeServer), 'function')
|
|
|
|
})
|
|
|
|
|
2022-02-15 11:28:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
t.test('should not default updateEvery if its zero', function() {
|
|
|
|
const assertTest = { a: 1, updateEvery: 0 }
|
|
|
|
const assertName = 'test'
|
|
|
|
ctx.db.config = {
|
|
|
|
test: assertTest,
|
|
|
|
app: { b: 2},
|
|
|
|
manage: { c: 3 },
|
|
|
|
}
|
|
|
|
|
|
|
|
let app = new Application(ctx, {}, assertName)
|
|
|
|
assert.notStrictEqual(app.config, assertTest)
|
|
|
|
assert.strictEqual(app.config.a, assertTest.a)
|
|
|
|
assert.strictEqual(app.config.updateEvery, assertTest.updateEvery)
|
|
|
|
assert.strictEqual(app.config.updateEvery, 0)
|
|
|
|
})
|
|
|
|
|
2022-01-28 11:47:55 +00:00
|
|
|
t.test('should create http instance correctly', function() {
|
2022-02-04 09:33:03 +00:00
|
|
|
ctx.db.config = {
|
2022-01-28 11:47:55 +00:00
|
|
|
testapp: { a: 1, https: true },
|
|
|
|
app: { b: 2},
|
|
|
|
manage: { c: 3 },
|
|
|
|
}
|
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
let app = new Application(ctx, {}, 'testapp')
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.ok(app.http)
|
|
|
|
assert.ok(app.http.ishttps)
|
2022-01-24 06:59:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should keep provider', function() {
|
|
|
|
const assertProvider = { a: 1 }
|
2022-02-04 09:33:03 +00:00
|
|
|
let app = new Application(ctx, assertProvider, 'test')
|
2022-01-24 06:59:58 +00:00
|
|
|
assert.strictEqual(app.provider, assertProvider)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.timeout(250).describe('#startAutoupdater()', function() {
|
2022-02-04 09:33:03 +00:00
|
|
|
let ctx
|
2022-01-24 06:59:58 +00:00
|
|
|
|
|
|
|
t.beforeEach(function() {
|
2022-02-04 09:33:03 +00:00
|
|
|
return createFakeContext()
|
|
|
|
.then(function(res) { ctx = res })
|
2022-01-24 06:59:58 +00:00
|
|
|
})
|
2022-01-28 07:03:18 +00:00
|
|
|
|
|
|
|
t.test('should do nothing if provider is static', async function() {
|
|
|
|
const stubInterval = stub()
|
|
|
|
stubInterval.throws(new Error('should not be seen'))
|
|
|
|
let provider = new StaticProvider()
|
2022-02-04 09:33:03 +00:00
|
|
|
let app = new Application(ctx, provider, 'teststatic', { setInterval: stubInterval })
|
2022-01-28 07:03:18 +00:00
|
|
|
|
|
|
|
app.startAutoupdater()
|
|
|
|
})
|
2022-02-15 11:28:30 +00:00
|
|
|
|
|
|
|
t.test('should do nothing if updateEvery is zero', async function() {
|
|
|
|
const stubInterval = stub()
|
|
|
|
stubInterval.throws(new Error('should not be seen'))
|
|
|
|
ctx.db.config.test = { updateEvery: 0 }
|
|
|
|
let app = new Application(ctx, { }, 'test', { setInterval: stubInterval })
|
|
|
|
|
|
|
|
app.startAutoupdater()
|
|
|
|
})
|
2022-01-24 06:59:58 +00:00
|
|
|
|
|
|
|
t.test('should call setInterval correctly', function() {
|
|
|
|
const assertTimeMinutes = 1440
|
|
|
|
const stubInterval = stub()
|
|
|
|
const stubUnref = stub()
|
|
|
|
stubInterval.returns({ unref: stubUnref })
|
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
ctx.db.config.test = {
|
|
|
|
updateEvery: assertTimeMinutes,
|
|
|
|
}
|
2022-01-24 06:59:58 +00:00
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
let app = new Application(ctx, {}, 'test', { setInterval: stubInterval })
|
2022-01-24 06:59:58 +00:00
|
|
|
|
|
|
|
assert.strictEqual(stubInterval.called, false)
|
|
|
|
assert.strictEqual(stubUnref.called, false)
|
|
|
|
|
|
|
|
app.startAutoupdater()
|
|
|
|
|
|
|
|
assert.strictEqual(stubInterval.called, true)
|
|
|
|
assert.strictEqual(stubUnref.called, true)
|
|
|
|
assert.strictEqual(typeof(stubInterval.firstCall[0]), 'function')
|
|
|
|
assert.strictEqual(stubInterval.firstCall[1], assertTimeMinutes * 60 * 1000)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should support default value if updateEvery is not defined', function() {
|
|
|
|
const stubInterval = stub()
|
|
|
|
stubInterval.returns({ unref: function() {} })
|
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
let app = new Application(ctx, {}, 'test', { setInterval: stubInterval })
|
2022-01-24 06:59:58 +00:00
|
|
|
|
|
|
|
assert.strictEqual(stubInterval.called, false)
|
|
|
|
|
|
|
|
app.startAutoupdater()
|
|
|
|
|
|
|
|
assert.strictEqual(stubInterval.called, true)
|
|
|
|
assert.strictEqual(typeof(stubInterval.firstCall[0]), 'function')
|
|
|
|
assert.strictEqual(stubInterval.firstCall[1], 3 * 60 * 60 * 1000)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should call update as promise correctly', async function() {
|
|
|
|
const stubUpdate = stub()
|
|
|
|
const stubInterval = stub()
|
|
|
|
stubInterval.returns({ unref: function() {} })
|
|
|
|
|
|
|
|
stubUpdate.returnWith(function() {
|
|
|
|
return Promise.resolve()
|
|
|
|
})
|
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
let app = new Application(ctx, {}, 'test', { setInterval: stubInterval })
|
2022-01-24 06:59:58 +00:00
|
|
|
app.update = stubUpdate
|
|
|
|
app.startAutoupdater()
|
|
|
|
|
|
|
|
assert.strictEqual(typeof(stubInterval.firstCall[0]), 'function')
|
|
|
|
assert.notStrictEqual(stubInterval.firstCall, stubUpdate)
|
|
|
|
|
|
|
|
stubInterval.firstCall[0]()
|
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
while (ctx.db.data.core.test.updater === '') {
|
2022-01-24 06:59:58 +00:00
|
|
|
await setTimeout(10)
|
|
|
|
}
|
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.test.updater, /auto/i)
|
|
|
|
assert.match(ctx.db.data.core.test.updater, /update/i)
|
2022-01-24 06:59:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should add any errors to last in db update check on errors when updating', async function() {
|
|
|
|
const stubInterval = stub()
|
|
|
|
const assertErrorMessage = 'Ai Do'
|
|
|
|
stubInterval.returns({ unref: function() {} })
|
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
let app = new Application(ctx, {}, 'test', { setInterval: stubInterval })
|
2022-01-24 06:59:58 +00:00
|
|
|
app.update = function() {
|
|
|
|
return Promise.reject(new Error(assertErrorMessage))
|
|
|
|
}
|
|
|
|
app.startAutoupdater()
|
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.test.updater, '')
|
2022-01-24 06:59:58 +00:00
|
|
|
|
|
|
|
stubInterval.firstCall[0]()
|
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
while (ctx.db.data.core.test.updater === '') {
|
2022-01-24 06:59:58 +00:00
|
|
|
await setTimeout(10)
|
|
|
|
}
|
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.test.updater, /auto/i)
|
|
|
|
assert.match(ctx.db.data.core.test.updater, /update/i)
|
|
|
|
assert.match(ctx.db.data.core.test.updater, new RegExp(assertErrorMessage))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.timeout(250).describe('#closeServer()', function() {
|
|
|
|
let app
|
|
|
|
let stubCloseServer
|
|
|
|
|
|
|
|
t.beforeEach(function() {
|
|
|
|
return createFakeContext()
|
|
|
|
.then(function(res) {
|
|
|
|
let provider = createProvider()
|
|
|
|
app = new Application(res, provider, 'testapp')
|
|
|
|
app.http.closeServer = stubCloseServer = stub().resolves()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should call closeServer correctly', async function() {
|
|
|
|
const assertError = new Error('Moonlight Fiesta')
|
|
|
|
stubCloseServer.rejects(assertError)
|
|
|
|
|
|
|
|
let err = await assert.isRejected(app.closeServer())
|
|
|
|
|
|
|
|
assert.strictEqual(err, assertError)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('otherwise should work fine', async function() {
|
|
|
|
await app.closeServer()
|
2022-01-24 06:59:58 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.timeout(250).describe('#update()', function() {
|
2022-02-04 09:33:03 +00:00
|
|
|
let ctx
|
2022-01-24 06:59:58 +00:00
|
|
|
let app
|
|
|
|
let provider
|
2022-01-27 18:25:33 +00:00
|
|
|
let stubExtract
|
|
|
|
let stubRunCommand
|
|
|
|
let stubWrite
|
|
|
|
let stubFsMkdir
|
|
|
|
let stubFsRm
|
|
|
|
let stubFsStat
|
2022-01-24 06:59:58 +00:00
|
|
|
|
|
|
|
t.beforeEach(function() {
|
2022-02-04 09:33:03 +00:00
|
|
|
return createFakeContext()
|
|
|
|
.then(function(res) {
|
|
|
|
ctx = res
|
|
|
|
ctx.util.extractFile = stubExtract = stub().resolves()
|
|
|
|
ctx.util.runCommand = stubRunCommand = stub().resolves()
|
|
|
|
ctx.db.write = stubWrite = stub().resolves()
|
|
|
|
|
|
|
|
provider = createProvider()
|
|
|
|
app = new Application(ctx, provider, 'testapp', {
|
|
|
|
fs: {
|
|
|
|
mkdir: stubFsMkdir = stub(),
|
|
|
|
rm: stubFsRm = stub(),
|
|
|
|
stat: stubFsStat = stub(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
stubFsMkdir.resolves()
|
|
|
|
stubFsRm.resolves()
|
|
|
|
stubFsStat.resolves({})
|
|
|
|
provider.downloadVersion.resolves()
|
|
|
|
provider.getLatestVersion.resolves({ version: '123456789', link: 'httplinkhere', filename: 'test.7z' })
|
2022-01-27 18:25:33 +00:00
|
|
|
})
|
2022-01-24 06:59:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.afterEach(function() {
|
|
|
|
return fs.rm('./test/testapp/123456789', { force: true, recursive: true })
|
|
|
|
})
|
|
|
|
|
2022-01-28 07:03:18 +00:00
|
|
|
t.test('should do nothing if provider is static', async function() {
|
|
|
|
provider = new StaticProvider()
|
2022-02-04 09:33:03 +00:00
|
|
|
app = new Application(ctx, provider, 'teststatic')
|
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
stubWrite.reset().resolves()
|
2022-01-28 07:03:18 +00:00
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
let result = await app.update()
|
2022-01-28 07:03:18 +00:00
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
assert.strictEqual(result, null)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.teststatic.updater, /static/i)
|
|
|
|
assert.match(ctx.db.data.core.teststatic.updater, /nothing/i)
|
|
|
|
let old = ctx.db.data.core.teststatic.updater
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.ok(stubWrite.called)
|
|
|
|
assert.strictEqual(stubWrite.callCount, 1)
|
|
|
|
|
|
|
|
await app.update()
|
2022-02-14 08:15:50 +00:00
|
|
|
assert.strictEqual(result, null)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.teststatic.updater, old)
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.strictEqual(stubWrite.callCount, 1)
|
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
ctx.db.data.core.teststatic.updater = 'asdf'
|
2022-01-28 11:47:55 +00:00
|
|
|
|
|
|
|
await app.update()
|
2022-02-14 08:15:50 +00:00
|
|
|
assert.strictEqual(result, null)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.teststatic.updater, old)
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.strictEqual(stubWrite.callCount, 2)
|
|
|
|
|
|
|
|
await app.update()
|
2022-02-14 08:15:50 +00:00
|
|
|
assert.strictEqual(result, null)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.teststatic.updater, old)
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.strictEqual(stubWrite.callCount, 2)
|
2022-01-28 07:03:18 +00:00
|
|
|
})
|
|
|
|
|
2022-01-24 06:59:58 +00:00
|
|
|
t.test('multiple calls should be safe', async function() {
|
2022-02-04 09:33:03 +00:00
|
|
|
ctx.db.data.core.testapp.updater = ''
|
2022-01-24 06:59:58 +00:00
|
|
|
|
|
|
|
provider.getLatestVersion.returnWith(function() {
|
|
|
|
return new Promise(function() {})
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.strictEqual(app.updating, false)
|
|
|
|
|
|
|
|
app.update()
|
|
|
|
await setImmediate()
|
|
|
|
|
|
|
|
assert.strictEqual(app.updating, true)
|
|
|
|
assert.strictEqual(provider.getLatestVersion.callCount, 1)
|
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
let result = await app.update()
|
2022-01-24 06:59:58 +00:00
|
|
|
await setImmediate()
|
|
|
|
|
|
|
|
assert.strictEqual(provider.getLatestVersion.callCount, 1)
|
2022-02-14 08:15:50 +00:00
|
|
|
assert.strictEqual(result, null)
|
2022-01-24 06:59:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should check for latest version', async function() {
|
|
|
|
const assertError = new Error('Ore wa Subete wo Shihaisuru')
|
|
|
|
provider.getLatestVersion.rejects(assertError)
|
2022-02-04 09:33:03 +00:00
|
|
|
ctx.db.data.core.testapp.updater = ''
|
2022-01-24 06:59:58 +00:00
|
|
|
|
|
|
|
let err = await assert.isRejected(app.update())
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-01-24 06:59:58 +00:00
|
|
|
assert.strictEqual(app.updating, false)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.strictEqual(err, assertError)
|
2022-01-28 11:47:55 +00:00
|
|
|
|
|
|
|
assert.ok(stubWrite.called)
|
|
|
|
assert.ok(stubWrite.callCount >= 1)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /check/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /version/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, new RegExp(new Date().toISOString().split('T')[0]))
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, new RegExp(assertError.message))
|
2022-01-24 06:59:58 +00:00
|
|
|
})
|
|
|
|
|
2022-01-24 17:06:40 +00:00
|
|
|
t.test('should call provider download latest correctly if new 7zip version', async function() {
|
2022-01-24 06:59:58 +00:00
|
|
|
const assertError = new Error('Without a fight')
|
|
|
|
const assertLink = 'All of you'
|
|
|
|
const assertVersion = { version: '123456789', link: assertLink, filename: 'test.7z' }
|
|
|
|
const assertTarget = util.getPathFromRoot('./testapp/123456789/file.7z')
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 0)
|
2022-01-24 06:59:58 +00:00
|
|
|
|
|
|
|
provider.getLatestVersion.resolves(assertVersion)
|
|
|
|
provider.downloadVersion.rejects(assertError)
|
2022-02-04 09:33:03 +00:00
|
|
|
ctx.db.data.core.testapp.updater = ''
|
2022-01-24 06:59:58 +00:00
|
|
|
|
|
|
|
let err = await assert.isRejected(app.update())
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-01-24 06:59:58 +00:00
|
|
|
assert.strictEqual(app.updating, false)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.strictEqual(err, assertError)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /found/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, new RegExp(assertVersion.version))
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /downloading/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, new RegExp(assertLink))
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, new RegExp(assertTarget.replace(/\\/g, '\\\\')))
|
2022-01-24 06:59:58 +00:00
|
|
|
assert.strictEqual(provider.downloadVersion.firstCall[0], assertVersion)
|
|
|
|
assert.strictEqual(provider.downloadVersion.firstCall[1], assertTarget)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.testapp.updater, new RegExp(assertError.message))
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 1)
|
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions[0], assertVersion)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.ok(assertVersion.log)
|
|
|
|
assert.match(assertVersion.log, /\. \n/)
|
|
|
|
assert.match(assertVersion.log, new RegExp(assertError.message))
|
|
|
|
assert.ok(assertVersion.log.endsWith('\n'))
|
|
|
|
assert.strictEqual(assertVersion.failtodownload, 1)
|
|
|
|
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.ok(stubWrite.called)
|
|
|
|
assert.ok(stubWrite.callCount >= 2)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.ok(stubFsMkdir.called)
|
|
|
|
assert.match(stubFsMkdir.firstCall[0], /123456789/)
|
|
|
|
assert.ok(stubFsMkdir.firstCall[1]?.recursive)
|
|
|
|
|
|
|
|
// Test if subsequent calls increment counter
|
|
|
|
|
|
|
|
err = await assert.isRejected(app.update())
|
2022-01-24 06:59:58 +00:00
|
|
|
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.strictEqual(app.updating, false)
|
|
|
|
assert.strictEqual(err, assertError)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 1)
|
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions[0], assertVersion)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.strictEqual(assertVersion.failtodownload, 2)
|
2022-01-24 06:59:58 +00:00
|
|
|
})
|
2022-01-24 17:06:40 +00:00
|
|
|
|
|
|
|
t.test('should call provider download latest correctly if new 7zip version', async function() {
|
|
|
|
const assertError = new Error('Without a fight')
|
|
|
|
const assertLink = 'All of you'
|
|
|
|
const assertVersion = { version: '123456789', link: assertLink, filename: 'test.7z' }
|
|
|
|
const assertTarget = util.getPathFromRoot('./testapp/123456789/file.7z')
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 0)
|
2022-01-24 17:06:40 +00:00
|
|
|
|
2022-01-24 06:59:58 +00:00
|
|
|
provider.getLatestVersion.resolves(assertVersion)
|
|
|
|
provider.downloadVersion.rejects(assertError)
|
2022-02-04 09:33:03 +00:00
|
|
|
ctx.db.data.core.testapp.updater = ''
|
2022-01-24 06:59:58 +00:00
|
|
|
|
2022-01-24 17:06:40 +00:00
|
|
|
let err = await assert.isRejected(app.update())
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-01-24 17:06:40 +00:00
|
|
|
assert.strictEqual(app.updating, false)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.strictEqual(err, assertError)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /found/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, new RegExp(assertVersion.version))
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /downloading/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, new RegExp(assertLink))
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, new RegExp(assertTarget.replace(/\\/g, '\\\\')))
|
2022-01-24 17:06:40 +00:00
|
|
|
assert.strictEqual(provider.downloadVersion.firstCall[0], assertVersion)
|
|
|
|
assert.strictEqual(provider.downloadVersion.firstCall[1], assertTarget)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.testapp.updater, new RegExp(assertError.message))
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 1)
|
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions[0], assertVersion)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.ok(assertVersion.log)
|
|
|
|
assert.match(assertVersion.log, /\. \n/)
|
|
|
|
assert.match(assertVersion.log, new RegExp(assertError.message))
|
|
|
|
assert.ok(assertVersion.log.endsWith('\n'))
|
|
|
|
assert.strictEqual(assertVersion.failtodownload, 1)
|
|
|
|
|
|
|
|
assert.ok(stubFsRm.called)
|
|
|
|
assert.match(stubFsRm.firstCall[0], /123456789/)
|
|
|
|
assert.ok(stubFsRm.firstCall[1]?.recursive)
|
|
|
|
assert.ok(stubFsRm.firstCall[1]?.force)
|
|
|
|
|
|
|
|
// Test if subsequent calls increment counter
|
|
|
|
|
|
|
|
err = await assert.isRejected(app.update())
|
|
|
|
|
|
|
|
assert.strictEqual(app.updating, false)
|
|
|
|
assert.strictEqual(err, assertError)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 1)
|
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions[0], assertVersion)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.strictEqual(assertVersion.failtodownload, 2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should call extract on util correctly', async function() {
|
|
|
|
const assertExtractText = 'Reverdations of Success'
|
|
|
|
const assertError = new Error('Dai Gekisen')
|
|
|
|
const assertTarget = util.getPathFromRoot('./testapp/123456789/file.7z')
|
2022-01-28 07:03:18 +00:00
|
|
|
const assertFolder = util.getPathFromRoot('./testapp/123456789')
|
2022-01-27 18:25:33 +00:00
|
|
|
|
|
|
|
stubExtract.returnWith(function(target, stream) {
|
|
|
|
stream(assertExtractText)
|
|
|
|
return Promise.reject(assertError)
|
|
|
|
})
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 0)
|
2022-01-27 18:25:33 +00:00
|
|
|
|
|
|
|
let err = await assert.isRejected(app.update())
|
|
|
|
|
|
|
|
assert.strictEqual(app.updating, false)
|
|
|
|
assert.strictEqual(err, assertError)
|
|
|
|
assert.strictEqual(app.updating, false)
|
|
|
|
assert.strictEqual(stubExtract.firstCall[0], assertTarget)
|
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.testapp.updater, new RegExp('extracting[^.]+file\.7z', 'i'))
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, new RegExp(assertError.message))
|
2022-01-27 18:25:33 +00:00
|
|
|
|
|
|
|
assert.ok(stubFsRm.called)
|
2022-01-28 07:03:18 +00:00
|
|
|
assert.strictEqual(stubFsRm.firstCall[0], assertFolder)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.ok(stubFsRm.firstCall[1]?.recursive)
|
|
|
|
assert.ok(stubFsRm.firstCall[1]?.force)
|
|
|
|
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.ok(stubWrite.called)
|
|
|
|
assert.ok(stubWrite.callCount >= 3)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 1)
|
|
|
|
let version = ctx.db.data.core.testapp.versions[0]
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.ok(version.log)
|
|
|
|
assert.match(version.log, /\. \n/)
|
|
|
|
assert.match(version.log, new RegExp('extracting[^.]+file\.7z', 'i'))
|
|
|
|
assert.match(version.log, new RegExp(assertError.message))
|
|
|
|
assert.match(version.log, new RegExp(assertExtractText))
|
|
|
|
assert.ok(version.log.endsWith('\n'))
|
|
|
|
assert.strictEqual(version.failtodownload, 1)
|
|
|
|
|
|
|
|
// Test if subsequent calls increment counter
|
|
|
|
|
|
|
|
err = await assert.isRejected(app.update())
|
|
|
|
|
|
|
|
assert.strictEqual(app.updating, false)
|
|
|
|
assert.strictEqual(err, assertError)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 1)
|
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions[0], version)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.strictEqual(version.failtodownload, 2)
|
|
|
|
})
|
|
|
|
|
2022-01-28 07:03:18 +00:00
|
|
|
t.test('should call fs remove the archieve file', async function() {
|
|
|
|
const assertError = new Error('Tiny Kizuna')
|
|
|
|
const assertTarget = util.getPathFromRoot('./testapp/123456789/file.7z')
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 0)
|
2022-01-28 07:03:18 +00:00
|
|
|
stubFsRm.rejects(assertError)
|
|
|
|
|
|
|
|
await app.update()
|
|
|
|
|
|
|
|
assert.strictEqual(app.updating, false)
|
|
|
|
assert.ok(stubFsRm.called)
|
|
|
|
assert.strictEqual(stubFsRm.callCount, 1)
|
|
|
|
assert.strictEqual(stubFsRm.firstCall[0], assertTarget)
|
|
|
|
assert.ok(stubFsRm.firstCall[1]?.force)
|
|
|
|
})
|
|
|
|
|
2022-01-27 18:25:33 +00:00
|
|
|
t.test('should not call npm install if stat fails to find index.mjs', async function() {
|
|
|
|
const assertError = new Error('File not found')
|
|
|
|
const assertTarget = util.getPathFromRoot('./testapp/123456789/index.mjs')
|
2022-02-14 08:15:50 +00:00
|
|
|
const stubUpdated = stub()
|
2022-01-27 18:25:33 +00:00
|
|
|
stubRunCommand.rejects(new Error('should not be seen'))
|
|
|
|
stubFsStat.rejects(assertError)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 0)
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
app.once('updated', stubUpdated)
|
|
|
|
|
2022-01-27 18:25:33 +00:00
|
|
|
let err = await assert.isRejected(app.update())
|
|
|
|
|
|
|
|
assert.strictEqual(app.updating, false)
|
|
|
|
assert.ok(stubExtract.called)
|
|
|
|
assert.strictEqual(err, assertError)
|
|
|
|
assert.strictEqual(stubFsStat.firstCall[0], assertTarget)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /index\.mjs/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /missing/i)
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 1)
|
|
|
|
let version = ctx.db.data.core.testapp.versions[0]
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.ok(version.log)
|
|
|
|
assert.match(version.log, /index\.mjs/i)
|
|
|
|
assert.match(version.log, /missing/i)
|
|
|
|
assert.ok(version.log.endsWith('\n'))
|
|
|
|
assert.strictEqual(version.failtodownload, 1)
|
|
|
|
|
|
|
|
// Test if subsequent calls increment counter
|
|
|
|
|
|
|
|
err = await assert.isRejected(app.update())
|
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
assert.notOk(stubUpdated.called)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.strictEqual(app.updating, false)
|
|
|
|
assert.strictEqual(err, assertError)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 1)
|
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions[0], version)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.strictEqual(version.failtodownload, 2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should not call npm install if package.json is missing but shuld pass', async function() {
|
|
|
|
const assertError = new Error('File not found')
|
|
|
|
const assertTarget = util.getPathFromRoot('./testapp/123456789/package.json')
|
|
|
|
stubRunCommand.rejects(new Error('should not be seen'))
|
|
|
|
stubFsStat.returnWith(function(path) {
|
|
|
|
if (path.endsWith('package.json')) {
|
|
|
|
return Promise.reject(assertError)
|
|
|
|
}
|
|
|
|
return Promise.resolve({})
|
|
|
|
})
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 0)
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
let result = await new Promise(function(res, rej) {
|
|
|
|
app.once('updated', res)
|
|
|
|
app.update().catch(rej)
|
|
|
|
})
|
2022-01-27 18:25:33 +00:00
|
|
|
|
|
|
|
assert.strictEqual(app.updating, false)
|
|
|
|
assert.strictEqual(stubFsStat.callCount, 2)
|
|
|
|
assert.strictEqual(stubFsStat.secondCall[0], assertTarget)
|
|
|
|
assert.ok(stubExtract.called)
|
|
|
|
assert.notOk(stubRunCommand.called)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /package\.json/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /contain/i)
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 1)
|
|
|
|
let version = ctx.db.data.core.testapp.versions[0]
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.ok(version.log)
|
2022-02-14 08:15:50 +00:00
|
|
|
assert.strictEqual(result, version)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.match(version.log, /package\.json/i)
|
|
|
|
assert.match(version.log, /contain/i)
|
|
|
|
assert.ok(version.log.endsWith('\n'))
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.ok(ctx.db.data.core.testapp.latestInstalled)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.match(version.log, /finished/i)
|
|
|
|
assert.match(version.log, /updating/i)
|
|
|
|
|
|
|
|
// Test if subsequent calls do nothing
|
|
|
|
|
|
|
|
provider.downloadVersion.reset()
|
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
result = await app.update()
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
assert.strictEqual(result, null)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 1)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.notOk(provider.downloadVersion.called)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /already/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /nothing/i)
|
2022-01-27 18:25:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should otherwise call npm install correctly', async function() {
|
|
|
|
const assertExtractText = 'Egao no Hikair ni Tsutsumarete'
|
|
|
|
const assertNpmText = 'Dadadadash'
|
|
|
|
const assertVersion = { version: '123456789', link: 'somelinkhere', filename: 'test.7z' }
|
|
|
|
const assertError = new Error('Nagisa')
|
|
|
|
const assertTarget = util.getPathFromRoot('./testapp/123456789')
|
2022-01-28 07:03:18 +00:00
|
|
|
const assertTargetCheck = util.getPathFromRoot('./testapp/123456789/')
|
2022-02-14 08:15:50 +00:00
|
|
|
const stubUpdated = stub()
|
2022-01-27 18:25:33 +00:00
|
|
|
provider.getLatestVersion.resolves(assertVersion)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 0)
|
2022-01-27 18:25:33 +00:00
|
|
|
|
|
|
|
stubExtract.returnWith(function(target, stream) {
|
|
|
|
stream(assertExtractText)
|
|
|
|
return Promise.resolve()
|
|
|
|
})
|
|
|
|
stubRunCommand.returnWith(function(command, options, folder, stream) {
|
|
|
|
stream(assertNpmText)
|
|
|
|
return Promise.reject(assertError)
|
|
|
|
})
|
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
app.once('updated', stubUpdated)
|
|
|
|
|
2022-01-27 18:25:33 +00:00
|
|
|
let err = await assert.isRejected(app.update())
|
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
assert.notOk(stubUpdated.called)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.strictEqual(app.updating, false)
|
|
|
|
assert.strictEqual(err, assertError)
|
|
|
|
assert.strictEqual(stubRunCommand.firstCall[0], 'npm.cmd')
|
|
|
|
assert.ok(stubRunCommand.firstCall[1])
|
|
|
|
assert.strictEqual(stubRunCommand.firstCall[1][0], 'install')
|
|
|
|
assert.ok(stubRunCommand.firstCall[1].includes('--production'), 'should have --production')
|
|
|
|
assert.ok(stubRunCommand.firstCall[1].includes('--no-optional'), 'should have --no-optional')
|
|
|
|
assert.ok(stubRunCommand.firstCall[1].includes('--no-package-lock'), 'should have --no-package-lock')
|
|
|
|
assert.ok(stubRunCommand.firstCall[1].includes('--no-audit'), 'should have --no-audit')
|
|
|
|
assert.strictEqual(stubRunCommand.firstCall[2], assertTarget)
|
|
|
|
|
2022-01-28 07:03:18 +00:00
|
|
|
for (let i = 0; i < stubFsRm.callCount; i++) {
|
|
|
|
assert.notStrictEqual(stubFsRm.getCall(i)[0], assertTarget)
|
|
|
|
assert.notStrictEqual(stubFsRm.getCall(i)[0], assertTargetCheck)
|
|
|
|
}
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.ok(stubWrite.called)
|
|
|
|
assert.ok(stubWrite.callCount >= 4)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /npm/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /install/i)
|
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 1)
|
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions[0], assertVersion)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.ok(assertVersion.log)
|
|
|
|
assert.match(assertVersion.log, /\. \n/)
|
|
|
|
assert.match(assertVersion.log, new RegExp(assertExtractText))
|
|
|
|
assert.match(assertVersion.log, new RegExp(assertNpmText))
|
|
|
|
assert.match(assertVersion.log, new RegExp(assertError.message))
|
|
|
|
assert.match(assertVersion.log, /npm/i)
|
|
|
|
assert.match(assertVersion.log, /install/i)
|
|
|
|
assert.ok(assertVersion.log.endsWith('\n'))
|
|
|
|
assert.strictEqual(assertVersion.failtoinstall, 1)
|
|
|
|
|
|
|
|
// Test if subsequent calls increment counter
|
|
|
|
|
|
|
|
err = await assert.isRejected(app.update())
|
|
|
|
|
|
|
|
assert.strictEqual(app.updating, false)
|
|
|
|
assert.strictEqual(err, assertError)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 1)
|
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions[0], assertVersion)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.strictEqual(assertVersion.failtoinstall, 2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should update latest installed correctly', async function() {
|
|
|
|
const assertVersion = { version: '123456789', link: 'httplinkhere', filename: 'test.7z' }
|
|
|
|
provider.getLatestVersion.resolves(assertVersion)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.notStrictEqual(ctx.db.data.core.testapp.latestInstalled, assertVersion.version)
|
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 0)
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
let result = await new Promise(function(res, rej) {
|
|
|
|
app.once('updated', res)
|
|
|
|
app.update().catch(rej)
|
|
|
|
})
|
2022-01-27 18:25:33 +00:00
|
|
|
|
|
|
|
assert.strictEqual(app.updating, false)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.latestInstalled, assertVersion.version)
|
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 1)
|
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions[0], assertVersion)
|
2022-02-14 08:15:50 +00:00
|
|
|
assert.strictEqual(result, assertVersion)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.ok(assertVersion.log)
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.ok(stubWrite.callCount >= 4)
|
|
|
|
assert.strictEqual(assertVersion.installed, true)
|
|
|
|
assert.strictEqual(assertVersion.stable, 0)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.match(assertVersion.log, /found/i)
|
|
|
|
assert.match(assertVersion.log, new RegExp(assertVersion.version))
|
|
|
|
assert.match(assertVersion.log, /downloading/i)
|
|
|
|
assert.match(assertVersion.log, new RegExp('extracting[^.]+file\.7z', 'i'))
|
|
|
|
assert.match(assertVersion.log, /finished/i)
|
|
|
|
assert.match(assertVersion.log, /updating/i)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should update existing version if found', async function() {
|
|
|
|
const assertNewLink = 'THE last pain'
|
|
|
|
const assertNewFilename = 'The place of hope.7z'
|
2022-01-28 11:47:55 +00:00
|
|
|
const assertStable = 100
|
2022-02-14 08:15:50 +00:00
|
|
|
const stubUpdated = stub()
|
2022-01-27 18:25:33 +00:00
|
|
|
const oldLog = 'The Smell of Sea\n'
|
2022-01-28 11:47:55 +00:00
|
|
|
const assertVersion = { version: '123456789', link: 'httplinkhere', filename: 'test.7z', log: oldLog, stable: assertStable }
|
2022-01-24 17:06:40 +00:00
|
|
|
|
2022-01-27 18:25:33 +00:00
|
|
|
assertVersion.id = assertVersion.version
|
2022-02-04 09:33:03 +00:00
|
|
|
ctx.db.upsert(ctx.db.data.core.testapp.versions, assertVersion)
|
2022-01-27 18:25:33 +00:00
|
|
|
|
|
|
|
provider.getLatestVersion.resolves({ version: assertVersion.version, link: assertNewLink, filename: assertNewFilename })
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 1)
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
app.once('updated', stubUpdated)
|
|
|
|
|
|
|
|
let result = await app.update()
|
|
|
|
|
|
|
|
assert.ok(stubUpdated.called)
|
|
|
|
assert.strictEqual(result, stubUpdated.firstCall[0])
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.ok(assertVersion.log)
|
|
|
|
assert.ok(stubWrite.callCount >= 4)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.strictEqual(app.updating, false)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions.length, 1)
|
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.versions[0], assertVersion)
|
2022-02-14 08:15:50 +00:00
|
|
|
assert.strictEqual(result, assertVersion)
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.strictEqual(assertVersion.stable, assertStable)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.ok(assertVersion.log)
|
|
|
|
assert.ok(assertVersion.log.startsWith(oldLog))
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.strictEqual(assertVersion.installed, true)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.match(assertVersion.log, /found/i)
|
|
|
|
assert.match(assertVersion.log, new RegExp(assertVersion.version))
|
|
|
|
assert.match(assertVersion.log, /downloading/i)
|
|
|
|
assert.match(assertVersion.log, new RegExp('extracting[^.]+file\.7z', 'i'))
|
|
|
|
assert.match(assertVersion.log, /finished/i)
|
|
|
|
assert.match(assertVersion.log, /updating/i)
|
2022-01-24 17:06:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should do nothing if latestInstalled matches version', async function() {
|
2022-01-24 06:59:58 +00:00
|
|
|
const assertError = new Error('should not be seen')
|
|
|
|
const assertVersion = { version: '999.888.777.666', filename: 'test.7z' }
|
2022-02-14 08:15:50 +00:00
|
|
|
const stubUpdated = stub()
|
2022-01-24 06:59:58 +00:00
|
|
|
provider.getLatestVersion.resolves(assertVersion)
|
|
|
|
provider.downloadVersion.rejects(assertError)
|
2022-02-04 09:33:03 +00:00
|
|
|
ctx.db.data.core.testapp.updater = ''
|
|
|
|
ctx.db.data.core.testapp.latestInstalled = assertVersion.version
|
2022-01-24 06:59:58 +00:00
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
app.once('updated', stubUpdated)
|
|
|
|
|
|
|
|
let result = await app.update()
|
|
|
|
|
|
|
|
assert.strictEqual(result, null)
|
|
|
|
assert.notOk(stubUpdated.called)
|
|
|
|
|
|
|
|
result = await app.update()
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
assert.notOk(stubUpdated.called)
|
|
|
|
assert.strictEqual(result, null)
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.ok(stubWrite.callCount >= 1)
|
|
|
|
assert.strictEqual(app.updating, false)
|
|
|
|
assert.notOk(provider.downloadVersion.called)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /already/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /nothing/i)
|
2022-01-28 11:47:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should do nothing if installed version is found', async function() {
|
|
|
|
const assertError = new Error('should not be seen')
|
|
|
|
const assertVersion = { version: '999.888.777.666', filename: 'test.7z' }
|
2022-02-14 08:15:50 +00:00
|
|
|
const stubUpdated = stub()
|
2022-01-28 11:47:55 +00:00
|
|
|
provider.getLatestVersion.resolves(assertVersion)
|
|
|
|
provider.downloadVersion.rejects(assertError)
|
2022-02-04 09:33:03 +00:00
|
|
|
ctx.db.data.core.testapp.updater = ''
|
2022-01-28 11:47:55 +00:00
|
|
|
|
2022-02-04 09:33:03 +00:00
|
|
|
ctx.db.upsert(ctx.db.data.core.testapp.versions, { id: '111.111.111.111', installed: true })
|
|
|
|
ctx.db.upsert(ctx.db.data.core.testapp.versions, { id: '222.222.222.222', installed: true })
|
|
|
|
ctx.db.upsert(ctx.db.data.core.testapp.versions, { id: '999.888.777.666', installed: true })
|
|
|
|
ctx.db.upsert(ctx.db.data.core.testapp.versions, { id: '333.333.333.333', installed: true })
|
2022-01-28 11:47:55 +00:00
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
app.once('updated', stubUpdated)
|
|
|
|
|
2022-01-28 11:47:55 +00:00
|
|
|
await app.update()
|
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
assert.notOk(stubUpdated.called)
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.ok(stubWrite.callCount >= 1)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.strictEqual(app.updating, false)
|
2022-01-24 06:59:58 +00:00
|
|
|
assert.notOk(provider.downloadVersion.called)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /already/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /nothing/i)
|
2022-01-24 06:59:58 +00:00
|
|
|
})
|
2022-01-27 18:25:33 +00:00
|
|
|
|
|
|
|
t.test('should do nothing it exists and failtodownload is higher than 3', async function() {
|
|
|
|
const assertError = new Error('should not be seen')
|
|
|
|
const assertVersion = { version: '999.888.777.666', filename: 'test.7z' }
|
2022-02-14 08:15:50 +00:00
|
|
|
const stubUpdated = stub()
|
2022-01-27 18:25:33 +00:00
|
|
|
provider.getLatestVersion.resolves(assertVersion)
|
|
|
|
provider.downloadVersion.rejects(assertError)
|
2022-02-04 09:33:03 +00:00
|
|
|
ctx.db.data.core.testapp.updater = ''
|
|
|
|
ctx.db.upsert(ctx.db.data.core.testapp.versions, { id: '999.888.777.666', version: '999.888.777.666', link: 'httplinkhere', filename: 'test.7z', failtodownload: 4 })
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
app.once('updated', stubUpdated)
|
|
|
|
|
|
|
|
let result = await app.update()
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
assert.notOk(stubUpdated.called)
|
|
|
|
assert.strictEqual(result, null)
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.ok(stubWrite.callCount >= 1)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.strictEqual(app.updating, false)
|
|
|
|
assert.notOk(provider.downloadVersion.called)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /many/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /fail/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /skip/i)
|
2022-01-27 18:25:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should do nothing it exists and failtoinstall is higher than 3', async function() {
|
|
|
|
const assertError = new Error('should not be seen')
|
|
|
|
const assertVersion = { version: '999.888.777.666', filename: 'test.7z' }
|
2022-02-14 08:15:50 +00:00
|
|
|
const stubUpdated = stub()
|
2022-01-27 18:25:33 +00:00
|
|
|
provider.getLatestVersion.resolves(assertVersion)
|
|
|
|
provider.downloadVersion.rejects(assertError)
|
2022-02-04 09:33:03 +00:00
|
|
|
ctx.db.data.core.testapp.updater = ''
|
|
|
|
ctx.db.upsert(ctx.db.data.core.testapp.versions, { id: '999.888.777.666', version: '999.888.777.666', link: 'httplinkhere', filename: 'test.7z', failtoinstall: 4 })
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
app.once('updated', stubUpdated)
|
|
|
|
|
|
|
|
let result = await app.update()
|
2022-01-27 18:25:33 +00:00
|
|
|
|
2022-02-14 08:15:50 +00:00
|
|
|
assert.notOk(stubUpdated.called)
|
|
|
|
assert.strictEqual(result, null)
|
2022-01-28 11:47:55 +00:00
|
|
|
assert.ok(stubWrite.callCount >= 1)
|
2022-01-27 18:25:33 +00:00
|
|
|
assert.strictEqual(app.updating, false)
|
|
|
|
assert.notOk(provider.downloadVersion.called)
|
2022-02-04 09:33:03 +00:00
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /many/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /fail/i)
|
|
|
|
assert.match(ctx.db.data.core.testapp.updater, /skip/i)
|
2022-01-27 18:25:33 +00:00
|
|
|
})
|
2022-01-24 06:59:58 +00:00
|
|
|
})
|
2022-01-28 11:47:55 +00:00
|
|
|
|
|
|
|
t.timeout(250).describe('#registerModule()', function() {
|
|
|
|
const assertAppName = 'testappregister'
|
2022-02-04 09:33:03 +00:00
|
|
|
let ctx
|
2022-01-28 11:47:55 +00:00
|
|
|
let app
|
|
|
|
|
|
|
|
t.beforeEach(function() {
|
2022-02-04 09:33:03 +00:00
|
|
|
return createFakeContext()
|
|
|
|
.then(function(res) {
|
|
|
|
ctx = res
|
|
|
|
let provider = new StaticProvider()
|
|
|
|
app = new Application(ctx, provider, assertAppName)
|
|
|
|
})
|
2022-01-28 11:47:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should fail if not an object with start', function() {
|
|
|
|
let tests = [
|
|
|
|
[1, 'number'],
|
|
|
|
[0, 'empty number'],
|
|
|
|
['text', 'string'],
|
|
|
|
['', 'empty string'],
|
|
|
|
[{}, 'empty object'],
|
|
|
|
[[], 'array'],
|
|
|
|
[{ a: 1 }, 'non-empty object'],
|
|
|
|
]
|
|
|
|
|
|
|
|
tests.forEach(function(check) {
|
|
|
|
assert.throws(function() {
|
|
|
|
app.registerModule(check[0])
|
|
|
|
}, function(err) {
|
|
|
|
assert.match(err.message, /registerModule/i)
|
|
|
|
assert.match(err.message, /function/i)
|
|
|
|
assert.match(err.message, /start/i)
|
|
|
|
assert.match(err.message, new RegExp(assertAppName, 'i'))
|
|
|
|
return true
|
|
|
|
}, `should fail if called with ${check[1]}`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should otherwise succeed if object has function start', function() {
|
|
|
|
const assertFunction = function() {}
|
|
|
|
const assertModule = { start: assertFunction }
|
|
|
|
|
|
|
|
app.registerModule(assertModule)
|
|
|
|
|
|
|
|
assert.strictEqual(app.module, assertModule)
|
|
|
|
|
|
|
|
app.registerModule(assertFunction)
|
|
|
|
|
|
|
|
assert.notStrictEqual(app.module, assertModule)
|
|
|
|
assert.deepStrictEqual(app.module, assertModule)
|
|
|
|
})
|
|
|
|
})
|