2020-02-20 22:50:01 +01:00
|
|
|
import * as assert from "assert"
|
|
|
|
import * as fs from "fs-extra"
|
|
|
|
import * as http from "http"
|
|
|
|
import * as path from "path"
|
2020-10-16 00:00:21 +02:00
|
|
|
import { AuthType } from "../src/node/cli"
|
2020-10-16 00:47:20 +02:00
|
|
|
import { LatestResponse, UpdateHttpProvider } from "../src/node/routes/update"
|
2020-02-20 22:50:01 +01:00
|
|
|
import { SettingsProvider, UpdateSettings } from "../src/node/settings"
|
|
|
|
import { tmpdir } from "../src/node/util"
|
|
|
|
|
2020-10-16 00:00:21 +02:00
|
|
|
describe.skip("update", () => {
|
2020-02-20 22:50:01 +01:00
|
|
|
let version = "1.0.0"
|
|
|
|
let spy: string[] = []
|
|
|
|
const server = http.createServer((request: http.IncomingMessage, response: http.ServerResponse) => {
|
|
|
|
if (!request.url) {
|
|
|
|
throw new Error("no url")
|
|
|
|
}
|
2020-07-22 22:55:14 +02:00
|
|
|
|
2020-02-20 22:50:01 +01:00
|
|
|
spy.push(request.url)
|
2020-07-22 22:55:14 +02:00
|
|
|
|
|
|
|
// Return the latest version.
|
2020-02-20 22:50:01 +01:00
|
|
|
if (request.url === "/latest") {
|
|
|
|
const latest: LatestResponse = {
|
|
|
|
name: version,
|
|
|
|
}
|
2020-07-22 22:55:14 +02:00
|
|
|
response.writeHead(200)
|
2020-02-20 22:50:01 +01:00
|
|
|
return response.end(JSON.stringify(latest))
|
|
|
|
}
|
|
|
|
|
2020-07-22 22:55:14 +02:00
|
|
|
// Anything else is a 404.
|
|
|
|
response.writeHead(404)
|
|
|
|
response.end("not found")
|
2020-02-20 22:50:01 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const jsonPath = path.join(tmpdir, "tests/updates/update.json")
|
|
|
|
const settings = new SettingsProvider<UpdateSettings>(jsonPath)
|
|
|
|
|
|
|
|
let _provider: UpdateHttpProvider | undefined
|
|
|
|
const provider = (): UpdateHttpProvider => {
|
|
|
|
if (!_provider) {
|
|
|
|
const address = server.address()
|
|
|
|
if (!address || typeof address === "string" || !address.port) {
|
|
|
|
throw new Error("unexpected address")
|
|
|
|
}
|
|
|
|
_provider = new UpdateHttpProvider(
|
|
|
|
{
|
|
|
|
auth: AuthType.None,
|
|
|
|
commit: "test",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
`http://${address.address}:${address.port}/latest`,
|
|
|
|
settings,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return _provider
|
|
|
|
}
|
|
|
|
|
|
|
|
before(async () => {
|
2020-03-04 17:31:59 +01:00
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
server.on("error", reject)
|
|
|
|
server.on("listening", resolve)
|
|
|
|
server.listen({
|
|
|
|
port: 0,
|
|
|
|
host: "localhost",
|
|
|
|
})
|
|
|
|
})
|
2020-02-20 22:50:01 +01:00
|
|
|
await fs.remove(path.join(tmpdir, "tests/updates"))
|
2020-07-22 22:55:14 +02:00
|
|
|
await fs.mkdirp(path.join(tmpdir, "tests/updates"))
|
2020-02-20 22:50:01 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
server.close()
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
spy = []
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should get the latest", async () => {
|
|
|
|
version = "2.1.0"
|
|
|
|
|
|
|
|
const p = provider()
|
|
|
|
const now = Date.now()
|
|
|
|
const update = await p.getUpdate()
|
|
|
|
|
|
|
|
assert.deepEqual({ update }, await settings.read())
|
|
|
|
assert.equal(isNaN(update.checked), false)
|
|
|
|
assert.equal(update.checked < Date.now() && update.checked >= now, true)
|
|
|
|
assert.equal(update.version, "2.1.0")
|
|
|
|
assert.deepEqual(spy, ["/latest"])
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should keep existing information", async () => {
|
|
|
|
version = "3.0.1"
|
|
|
|
|
|
|
|
const p = provider()
|
|
|
|
const now = Date.now()
|
|
|
|
const update = await p.getUpdate()
|
|
|
|
|
|
|
|
assert.deepEqual({ update }, await settings.read())
|
|
|
|
assert.equal(isNaN(update.checked), false)
|
|
|
|
assert.equal(update.checked < now, true)
|
|
|
|
assert.equal(update.version, "2.1.0")
|
|
|
|
assert.deepEqual(spy, [])
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should force getting the latest", async () => {
|
|
|
|
version = "4.1.1"
|
|
|
|
|
|
|
|
const p = provider()
|
|
|
|
const now = Date.now()
|
|
|
|
const update = await p.getUpdate(true)
|
|
|
|
|
|
|
|
assert.deepEqual({ update }, await settings.read())
|
|
|
|
assert.equal(isNaN(update.checked), false)
|
|
|
|
assert.equal(update.checked < Date.now() && update.checked >= now, true)
|
|
|
|
assert.equal(update.version, "4.1.1")
|
|
|
|
assert.deepEqual(spy, ["/latest"])
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should get latest after interval passes", async () => {
|
|
|
|
const p = provider()
|
|
|
|
await p.getUpdate()
|
|
|
|
assert.deepEqual(spy, [])
|
|
|
|
|
|
|
|
let checked = Date.now() - 1000 * 60 * 60 * 23
|
|
|
|
await settings.write({ update: { checked, version } })
|
|
|
|
await p.getUpdate()
|
|
|
|
assert.deepEqual(spy, [])
|
|
|
|
|
|
|
|
checked = Date.now() - 1000 * 60 * 60 * 25
|
|
|
|
await settings.write({ update: { checked, version } })
|
|
|
|
|
|
|
|
const update = await p.getUpdate()
|
|
|
|
assert.notEqual(update.checked, checked)
|
|
|
|
assert.deepEqual(spy, ["/latest"])
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should check if it's the current version", async () => {
|
|
|
|
version = "9999999.99999.9999"
|
|
|
|
|
|
|
|
const p = provider()
|
|
|
|
let update = await p.getUpdate(true)
|
|
|
|
assert.equal(p.isLatestVersion(update), false)
|
|
|
|
|
|
|
|
version = "0.0.0"
|
|
|
|
update = await p.getUpdate(true)
|
|
|
|
assert.equal(p.isLatestVersion(update), true)
|
|
|
|
|
|
|
|
// Old version format; make sure it doesn't report as being later.
|
|
|
|
version = "999999.9999-invalid999.99.9"
|
|
|
|
update = await p.getUpdate(true)
|
|
|
|
assert.equal(p.isLatestVersion(update), true)
|
|
|
|
})
|
|
|
|
|
2020-03-25 21:00:35 +01:00
|
|
|
it("should not reject if unable to fetch", async () => {
|
|
|
|
const options = {
|
|
|
|
auth: AuthType.None,
|
|
|
|
commit: "test",
|
|
|
|
}
|
2020-07-22 22:55:14 +02:00
|
|
|
let provider = new UpdateHttpProvider(options, true, "invalid", settings)
|
2020-03-25 21:00:35 +01:00
|
|
|
await assert.doesNotReject(() => provider.getUpdate(true))
|
|
|
|
|
2020-07-22 22:55:14 +02:00
|
|
|
provider = new UpdateHttpProvider(options, true, "http://probably.invalid.dev.localhost/latest", settings)
|
2020-03-25 21:00:35 +01:00
|
|
|
await assert.doesNotReject(() => provider.getUpdate(true))
|
|
|
|
})
|
2020-02-20 22:50:01 +01:00
|
|
|
})
|