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