1
0
mirror of https://git.tuxpa.in/a/code-server.git synced 2024-12-27 12:45:25 +00:00
code-server-2/test/unit/update.test.ts

161 lines
4.9 KiB
TypeScript
Raw Normal View History

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