mirror of https://git.tuxpa.in/a/code-server.git
feat: setup jest
This commit is contained in:
parent
c52198f30d
commit
cef7d42652
|
@ -0,0 +1,5 @@
|
|||
module.exports = {
|
||||
preset: 'ts-jest',
|
||||
testEnvironment: 'node',
|
||||
testPathIgnorePatterns: ["/node_modules/", "lib/vscode/"]
|
||||
};
|
|
@ -35,6 +35,7 @@
|
|||
"@types/express": "^4.17.8",
|
||||
"@types/fs-extra": "^8.0.1",
|
||||
"@types/http-proxy": "^1.17.4",
|
||||
"@types/jest": "^26.0.20",
|
||||
"@types/js-yaml": "^3.12.3",
|
||||
"@types/mocha": "^8.0.3",
|
||||
"@types/node": "^12.12.7",
|
||||
|
@ -55,6 +56,7 @@
|
|||
"eslint-config-prettier": "^6.0.0",
|
||||
"eslint-plugin-import": "^2.18.2",
|
||||
"eslint-plugin-prettier": "^3.1.0",
|
||||
"jest": "^26.6.3",
|
||||
"leaked-handles": "^5.2.0",
|
||||
"mocha": "^8.1.2",
|
||||
"parcel-bundler": "^1.12.4",
|
||||
|
|
162
test/cli.test.ts
162
test/cli.test.ts
|
@ -1,5 +1,4 @@
|
|||
import { Level, logger } from "@coder/logger"
|
||||
import * as assert from "assert"
|
||||
import * as fs from "fs-extra"
|
||||
import * as net from "net"
|
||||
import * as os from "os"
|
||||
|
@ -32,11 +31,11 @@ describe("parser", () => {
|
|||
}
|
||||
|
||||
it("should parse nothing", () => {
|
||||
assert.deepEqual(parse([]), { _: [] })
|
||||
expect(parse([])).toBe({ _: [] })
|
||||
})
|
||||
|
||||
it("should parse all available options", () => {
|
||||
assert.deepEqual(
|
||||
expect(
|
||||
parse([
|
||||
"--bind-addr=192.169.0.1:8080",
|
||||
"--auth",
|
||||
|
@ -73,7 +72,7 @@ describe("parser", () => {
|
|||
"--",
|
||||
"-5",
|
||||
"--6",
|
||||
]),
|
||||
])).toEqual(
|
||||
{
|
||||
_: ["1", "2", "3", "4", "-5", "--6"],
|
||||
auth: "none",
|
||||
|
@ -102,7 +101,7 @@ describe("parser", () => {
|
|||
})
|
||||
|
||||
it("should work with short options", () => {
|
||||
assert.deepEqual(parse(["-vvv", "-v"]), {
|
||||
expect(parse(["-vvv", "-v"])).toEqual({
|
||||
_: [],
|
||||
verbose: true,
|
||||
version: true,
|
||||
|
@ -111,102 +110,108 @@ describe("parser", () => {
|
|||
|
||||
it("should use log level env var", async () => {
|
||||
const args = parse([])
|
||||
assert.deepEqual(args, { _: [] })
|
||||
expect(args).toEqual({ _: [] })
|
||||
|
||||
process.env.LOG_LEVEL = "debug"
|
||||
assert.deepEqual(await setDefaults(args), {
|
||||
const defaults = await setDefaults(args)
|
||||
expect(defaults).toEqual({
|
||||
...defaults,
|
||||
_: [],
|
||||
log: "debug",
|
||||
verbose: false,
|
||||
})
|
||||
assert.equal(process.env.LOG_LEVEL, "debug")
|
||||
assert.equal(logger.level, Level.Debug)
|
||||
expect(process.env.LOG_LEVEL).toEqual("debug")
|
||||
expect(logger.level).toEqual(Level.Debug)
|
||||
|
||||
process.env.LOG_LEVEL = "trace"
|
||||
assert.deepEqual(await setDefaults(args), {
|
||||
...defaults,
|
||||
const updated = await setDefaults(args)
|
||||
expect(updated).toBe( {
|
||||
...updated,
|
||||
_: [],
|
||||
log: "trace",
|
||||
verbose: true,
|
||||
})
|
||||
assert.equal(process.env.LOG_LEVEL, "trace")
|
||||
assert.equal(logger.level, Level.Trace)
|
||||
expect(process.env.LOG_LEVEL).toEqual("trace")
|
||||
expect(logger.level).toEqual(Level.Trace)
|
||||
})
|
||||
|
||||
it("should prefer --log to env var and --verbose to --log", async () => {
|
||||
let args = parse(["--log", "info"])
|
||||
assert.deepEqual(args, {
|
||||
expect(args).toEqual({
|
||||
_: [],
|
||||
log: "info",
|
||||
})
|
||||
|
||||
process.env.LOG_LEVEL = "debug"
|
||||
assert.deepEqual(await setDefaults(args), {
|
||||
const defaults = await setDefaults(args)
|
||||
expect(defaults).toEqual({
|
||||
...defaults,
|
||||
_: [],
|
||||
log: "info",
|
||||
verbose: false,
|
||||
})
|
||||
assert.equal(process.env.LOG_LEVEL, "info")
|
||||
assert.equal(logger.level, Level.Info)
|
||||
expect(process.env.LOG_LEVEL).toEqual("info")
|
||||
expect(logger.level).toEqual(Level.Info)
|
||||
|
||||
process.env.LOG_LEVEL = "trace"
|
||||
assert.deepEqual(await setDefaults(args), {
|
||||
const updated = await setDefaults(args)
|
||||
expect(updated).toEqual( {
|
||||
...defaults,
|
||||
_: [],
|
||||
log: "info",
|
||||
verbose: false,
|
||||
})
|
||||
assert.equal(process.env.LOG_LEVEL, "info")
|
||||
assert.equal(logger.level, Level.Info)
|
||||
expect(process.env.LOG_LEVEL).toEqual("info")
|
||||
expect(logger.level).toEqual(Level.Info)
|
||||
|
||||
args = parse(["--log", "info", "--verbose"])
|
||||
assert.deepEqual(args, {
|
||||
expect(args).toEqual({
|
||||
_: [],
|
||||
log: "info",
|
||||
verbose: true,
|
||||
})
|
||||
|
||||
process.env.LOG_LEVEL = "warn"
|
||||
assert.deepEqual(await setDefaults(args), {
|
||||
const updatedAgain = await setDefaults(args)
|
||||
expect(updatedAgain).toEqual( {
|
||||
...defaults,
|
||||
_: [],
|
||||
log: "trace",
|
||||
verbose: true,
|
||||
})
|
||||
assert.equal(process.env.LOG_LEVEL, "trace")
|
||||
assert.equal(logger.level, Level.Trace)
|
||||
expect(process.env.LOG_LEVEL).toEqual("trace")
|
||||
expect(logger.level).toEqual(Level.Trace)
|
||||
})
|
||||
|
||||
it("should ignore invalid log level env var", async () => {
|
||||
process.env.LOG_LEVEL = "bogus"
|
||||
assert.deepEqual(await setDefaults(parse([])), {
|
||||
_: [],
|
||||
const defaults = await setDefaults(parse([]))
|
||||
expect(defaults).toEqual({
|
||||
...defaults,
|
||||
_: [],
|
||||
})
|
||||
})
|
||||
|
||||
it("should error if value isn't provided", () => {
|
||||
assert.throws(() => parse(["--auth"]), /--auth requires a value/)
|
||||
assert.throws(() => parse(["--auth=", "--log=debug"]), /--auth requires a value/)
|
||||
assert.throws(() => parse(["--auth", "--log"]), /--auth requires a value/)
|
||||
assert.throws(() => parse(["--auth", "--invalid"]), /--auth requires a value/)
|
||||
assert.throws(() => parse(["--bind-addr"]), /--bind-addr requires a value/)
|
||||
expect(() => parse(["--auth"])).toThrowError(/--auth requires a value/)
|
||||
expect(() => parse(["--auth=", "--log=debug"])).toThrowError(/--auth requires a value/)
|
||||
expect(() => parse(["--auth", "--log"])).toThrowError(/--auth requires a value/)
|
||||
expect(() => parse(["--auth", "--invalid"])).toThrowError(/--auth requires a value/)
|
||||
expect(() => parse(["--bind-addr"])).toThrowError(/--bind-addr requires a value/)
|
||||
})
|
||||
|
||||
it("should error if value is invalid", () => {
|
||||
assert.throws(() => parse(["--port", "foo"]), /--port must be a number/)
|
||||
assert.throws(() => parse(["--auth", "invalid"]), /--auth valid values: \[password, none\]/)
|
||||
assert.throws(() => parse(["--log", "invalid"]), /--log valid values: \[trace, debug, info, warn, error\]/)
|
||||
expect(() => parse(["--port", "foo"])).toThrowError(/--port must be a number/)
|
||||
expect(() => parse(["--auth", "invalid"])).toThrowError(/--auth valid values: \[password, none\]/)
|
||||
expect(() => parse(["--log", "invalid"])).toThrowError(/--log valid values: \[trace, debug, info, warn, error\]/)
|
||||
})
|
||||
|
||||
it("should error if the option doesn't exist", () => {
|
||||
assert.throws(() => parse(["--foo"]), /Unknown option --foo/)
|
||||
expect(()=> parse(["--foo"])).toThrowError(/Unknown option --foo/)
|
||||
})
|
||||
|
||||
it("should not error if the value is optional", () => {
|
||||
assert.deepEqual(parse(["--cert"]), {
|
||||
expect(parse(["--cert"])).toEqual({
|
||||
_: [],
|
||||
cert: {
|
||||
value: undefined,
|
||||
|
@ -215,43 +220,46 @@ describe("parser", () => {
|
|||
})
|
||||
|
||||
it("should not allow option-like values", () => {
|
||||
assert.throws(() => parse(["--socket", "--socket-path-value"]), /--socket requires a value/)
|
||||
expect(() => parse(["--socket", "--socket-path-value"])).toThrowError(/--socket requires a value/)
|
||||
// If you actually had a path like this you would do this instead:
|
||||
assert.deepEqual(parse(["--socket", "./--socket-path-value"]), {
|
||||
expect(parse(["--socket", "./--socket-path-value"])).toEqual({
|
||||
_: [],
|
||||
socket: path.resolve("--socket-path-value"),
|
||||
})
|
||||
assert.throws(() => parse(["--cert", "--socket-path-value"]), /Unknown option --socket-path-value/)
|
||||
expect(() => parse(["--cert", "--socket-path-value"])).toThrowError(/Unknown option --socket-path-value/)
|
||||
})
|
||||
|
||||
it("should allow positional arguments before options", () => {
|
||||
assert.deepEqual(parse(["foo", "test", "--auth", "none"]), {
|
||||
expect(parse(["foo", "test", "--auth", "none"])).toEqual({
|
||||
_: ["foo", "test"],
|
||||
auth: "none",
|
||||
})
|
||||
})
|
||||
|
||||
it("should support repeatable flags", () => {
|
||||
assert.deepEqual(parse(["--proxy-domain", "*.coder.com"]), {
|
||||
expect(parse(["--proxy-domain", "*.coder.com"])).toEqual({
|
||||
_: [],
|
||||
"proxy-domain": ["*.coder.com"],
|
||||
})
|
||||
assert.deepEqual(parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"]), {
|
||||
expect(parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"])).toEqual({
|
||||
_: [],
|
||||
"proxy-domain": ["*.coder.com", "test.com"],
|
||||
})
|
||||
})
|
||||
|
||||
it("should enforce cert-key with cert value or otherwise generate one", async () => {
|
||||
it(
|
||||
"should enforce cert-key with cert value or otherwise generate one",
|
||||
async () => {
|
||||
const args = parse(["--cert"])
|
||||
assert.deepEqual(args, {
|
||||
expect(args).toEqual( {
|
||||
_: [],
|
||||
cert: {
|
||||
value: undefined,
|
||||
},
|
||||
})
|
||||
assert.throws(() => parse(["--cert", "test"]), /--cert-key is missing/)
|
||||
assert.deepEqual(await setDefaults(args), {
|
||||
expect(() => parse(["--cert", "test"])).toThrowError(/--cert-key is missing/)
|
||||
const defaultArgs = await setDefaults(args)
|
||||
expect(defaultArgs).toEqual({
|
||||
_: [],
|
||||
...defaults,
|
||||
cert: {
|
||||
|
@ -259,11 +267,13 @@ describe("parser", () => {
|
|||
},
|
||||
"cert-key": path.join(paths.data, "localhost.key"),
|
||||
})
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
it("should override with --link", async () => {
|
||||
const args = parse("--cert test --cert-key test --socket test --host 0.0.0.0 --port 8888 --link test".split(" "))
|
||||
assert.deepEqual(await setDefaults(args), {
|
||||
const defaultArgs = await setDefaults(args)
|
||||
expect(defaultArgs).toEqual({
|
||||
_: [],
|
||||
...defaults,
|
||||
auth: "none",
|
||||
|
@ -281,11 +291,12 @@ describe("parser", () => {
|
|||
it("should use env var password", async () => {
|
||||
process.env.PASSWORD = "test"
|
||||
const args = parse([])
|
||||
assert.deepEqual(args, {
|
||||
expect(args).toEqual({
|
||||
_: [],
|
||||
})
|
||||
|
||||
assert.deepEqual(await setDefaults(args), {
|
||||
const defaultArgs = await setDefaults(args)
|
||||
expect(defaultArgs).toEqual({
|
||||
...defaults,
|
||||
_: [],
|
||||
password: "test",
|
||||
|
@ -296,11 +307,12 @@ describe("parser", () => {
|
|||
it("should use env var hashed password", async () => {
|
||||
process.env.HASHED_PASSWORD = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" // test
|
||||
const args = parse([])
|
||||
assert.deepEqual(args, {
|
||||
expect(args).toEqual({
|
||||
_: [],
|
||||
})
|
||||
|
||||
assert.deepEqual(await setDefaults(args), {
|
||||
const defaultArgs = await setDefaults(args)
|
||||
expect(defaultArgs).toEqual({
|
||||
...defaults,
|
||||
_: [],
|
||||
"hashed-password": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
|
||||
|
@ -310,12 +322,13 @@ describe("parser", () => {
|
|||
|
||||
it("should filter proxy domains", async () => {
|
||||
const args = parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "coder.com", "--proxy-domain", "coder.org"])
|
||||
assert.deepEqual(args, {
|
||||
expect(args).toEqual({
|
||||
_: [],
|
||||
"proxy-domain": ["*.coder.com", "coder.com", "coder.org"],
|
||||
})
|
||||
|
||||
assert.deepEqual(await setDefaults(args), {
|
||||
const defaultArgs = await setDefaults(args)
|
||||
expect(defaultArgs).toEqual({
|
||||
...defaults,
|
||||
_: [],
|
||||
"proxy-domain": ["coder.com", "coder.org"],
|
||||
|
@ -328,7 +341,7 @@ describe("cli", () => {
|
|||
const testDir = path.join(tmpdir, "tests/cli")
|
||||
const vscodeIpcPath = path.join(os.tmpdir(), "vscode-ipc")
|
||||
|
||||
before(async () => {
|
||||
beforeAll(async () => {
|
||||
await fs.remove(testDir)
|
||||
await fs.mkdirp(testDir)
|
||||
})
|
||||
|
@ -341,44 +354,52 @@ describe("cli", () => {
|
|||
|
||||
it("should use existing if inside code-server", async () => {
|
||||
process.env.VSCODE_IPC_HOOK_CLI = "test"
|
||||
assert.strictEqual(await shouldOpenInExistingInstance(args), "test")
|
||||
const shouldOpen = await shouldOpenInExistingInstance(args)
|
||||
expect(shouldOpen).toStrictEqual("test")
|
||||
|
||||
args.port = 8081
|
||||
args._.push("./file")
|
||||
assert.strictEqual(await shouldOpenInExistingInstance(args), "test")
|
||||
const _shouldOpen = await shouldOpenInExistingInstance(args)
|
||||
expect(_shouldOpen).toStrictEqual("test")
|
||||
})
|
||||
|
||||
it("should use existing if --reuse-window is set", async () => {
|
||||
args["reuse-window"] = true
|
||||
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined)
|
||||
const shouldOpen = await shouldOpenInExistingInstance(args)
|
||||
await expect(shouldOpen).toStrictEqual(undefined)
|
||||
|
||||
await fs.writeFile(vscodeIpcPath, "test")
|
||||
assert.strictEqual(await shouldOpenInExistingInstance(args), "test")
|
||||
await expect(shouldOpenInExistingInstance(args)).resolves.toStrictEqual("test")
|
||||
|
||||
args.port = 8081
|
||||
assert.strictEqual(await shouldOpenInExistingInstance(args), "test")
|
||||
await expect(shouldOpenInExistingInstance(args)).resolves.toStrictEqual("test")
|
||||
})
|
||||
|
||||
// TODO
|
||||
// fix red squiggles
|
||||
// and don't use should Open on all these
|
||||
it("should use existing if --new-window is set", async () => {
|
||||
args["new-window"] = true
|
||||
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined)
|
||||
expect(await shouldOpenInExistingInstance(args).toStrictEqual(undefined)
|
||||
|
||||
await fs.writeFile(vscodeIpcPath, "test")
|
||||
assert.strictEqual(await shouldOpenInExistingInstance(args), "test")
|
||||
expect(await shouldOpenInExistingInstance(args).toStrictEqual("test")
|
||||
|
||||
args.port = 8081
|
||||
assert.strictEqual(await shouldOpenInExistingInstance(args), "test")
|
||||
expect(await shouldOpenInExistingInstance(args).toStrictEqual("test")
|
||||
})
|
||||
|
||||
it("should use existing if no unrelated flags are set, has positional, and socket is active", async () => {
|
||||
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined)
|
||||
it(
|
||||
"should use existing if no unrelated flags are set, has positional, and socket is active",
|
||||
async () => {
|
||||
expect(await shouldOpenInExistingInstance(args).toStrictEqual(undefined)
|
||||
|
||||
args._.push("./file")
|
||||
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined)
|
||||
expect(await shouldOpenInExistingInstance(args).toStrictEqual(undefined)
|
||||
|
||||
const socketPath = path.join(testDir, "socket")
|
||||
await fs.writeFile(vscodeIpcPath, socketPath)
|
||||
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined)
|
||||
expect(await shouldOpenInExistingInstance(args).toStrictEqual(undefined)
|
||||
|
||||
await new Promise((resolve) => {
|
||||
const server = net.createServer(() => {
|
||||
|
@ -389,9 +410,10 @@ describe("cli", () => {
|
|||
server.listen(socketPath)
|
||||
})
|
||||
|
||||
assert.strictEqual(await shouldOpenInExistingInstance(args), socketPath)
|
||||
expect(await shouldOpenInExistingInstance(args).toStrictEqual(socketPath)
|
||||
|
||||
args.port = 8081
|
||||
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined)
|
||||
})
|
||||
expect(await shouldOpenInExistingInstance(args).toStrictEqual(undefined)
|
||||
}
|
||||
)
|
||||
})
|
||||
|
|
|
@ -2,7 +2,6 @@ import { logger } from "@coder/logger"
|
|||
import * as assert from "assert"
|
||||
import * as express from "express"
|
||||
import * as fs from "fs"
|
||||
import { describe } from "mocha"
|
||||
import * as path from "path"
|
||||
import { PluginAPI } from "../src/node/plugin"
|
||||
import * as apps from "../src/node/routes/apps"
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import { field, logger } from "@coder/logger"
|
||||
import * as assert from "assert"
|
||||
import * as fs from "fs-extra"
|
||||
import "leaked-handles"
|
||||
import * as net from "net"
|
||||
|
@ -44,7 +43,7 @@ describe("SocketProxyProvider", () => {
|
|||
})
|
||||
}
|
||||
|
||||
before(async () => {
|
||||
beforeAll(async () => {
|
||||
const cert = await generateCertificate("localhost")
|
||||
const options = {
|
||||
cert: fs.readFileSync(cert.cert),
|
||||
|
@ -93,14 +92,16 @@ describe("SocketProxyProvider", () => {
|
|||
|
||||
it("should work without a proxy", async () => {
|
||||
server.write("server->client")
|
||||
assert.equal(await getData(fromServerToClient), "server->client")
|
||||
const dataFromServerToClient = await (await getData(fromServerToClient)).toString()
|
||||
expect(dataFromServerToClient).toBe("server->client")
|
||||
client.write("client->server")
|
||||
assert.equal(await getData(fromClientToServer), "client->server")
|
||||
assert.equal(errors, 0)
|
||||
const dataFromClientToServer = await (await getData(fromClientToServer)).toString()
|
||||
expect(dataFromClientToServer).toBe("client->server")
|
||||
expect(errors).toEqual(0)
|
||||
})
|
||||
|
||||
it("should work with a proxy", async () => {
|
||||
assert.equal(server instanceof tls.TLSSocket, true)
|
||||
expect(server instanceof tls.TLSSocket).toBe(true)
|
||||
proxy = (await provider.createProxy(server))
|
||||
.on("data", (d) => fromClientToProxy.emit(d))
|
||||
.on("error", (error) => onProxyError.emit({ event: "error", error }))
|
||||
|
@ -110,10 +111,12 @@ describe("SocketProxyProvider", () => {
|
|||
provider.stop() // We don't need more proxies.
|
||||
|
||||
proxy.write("server proxy->client")
|
||||
assert.equal(await getData(fromServerToClient), "server proxy->client")
|
||||
const dataFromServerToClient = await (await getData(fromServerToClient)).toString()
|
||||
expect(dataFromServerToClient).toBe("server proxy->client")
|
||||
client.write("client->server proxy")
|
||||
assert.equal(await getData(fromClientToProxy), "client->server proxy")
|
||||
assert.equal(errors, 0)
|
||||
const dataFromClientToProxy = await (await getData(fromClientToProxy)).toString()
|
||||
expect(dataFromClientToProxy).toBe("client->server proxy")
|
||||
expect(errors).toEqual(0)
|
||||
})
|
||||
|
||||
it("should close", async () => {
|
||||
|
@ -122,3 +125,4 @@ describe("SocketProxyProvider", () => {
|
|||
proxy.end()
|
||||
})
|
||||
})
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
import * as assert from "assert"
|
||||
import * as fs from "fs-extra"
|
||||
import * as http from "http"
|
||||
import * as path from "path"
|
||||
|
@ -45,7 +44,7 @@ describe.skip("update", () => {
|
|||
return _provider
|
||||
}
|
||||
|
||||
before(async () => {
|
||||
beforeAll(async () => {
|
||||
await new Promise((resolve, reject) => {
|
||||
server.on("error", reject)
|
||||
server.on("listening", resolve)
|
||||
|
@ -58,7 +57,7 @@ describe.skip("update", () => {
|
|||
await fs.mkdirp(path.join(tmpdir, "tests/updates"))
|
||||
})
|
||||
|
||||
after(() => {
|
||||
afterAll(() => {
|
||||
server.close()
|
||||
})
|
||||
|
||||
|
@ -73,11 +72,11 @@ describe.skip("update", () => {
|
|||
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"])
|
||||
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"])
|
||||
})
|
||||
|
||||
it("should keep existing information", async () => {
|
||||
|
@ -87,11 +86,11 @@ describe.skip("update", () => {
|
|||
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, [])
|
||||
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([])
|
||||
})
|
||||
|
||||
it("should force getting the latest", async () => {
|
||||
|
@ -101,29 +100,29 @@ describe.skip("update", () => {
|
|||
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"])
|
||||
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"])
|
||||
})
|
||||
|
||||
it("should get latest after interval passes", async () => {
|
||||
const p = provider()
|
||||
await p.getUpdate()
|
||||
assert.deepEqual(spy, [])
|
||||
expect(spy).toEqual([])
|
||||
|
||||
let checked = Date.now() - 1000 * 60 * 60 * 23
|
||||
await settings.write({ update: { checked, version } })
|
||||
await p.getUpdate()
|
||||
assert.deepEqual(spy, [])
|
||||
expect(spy).toEqual([])
|
||||
|
||||
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"])
|
||||
expect(update.checked).not.toBe(checked)
|
||||
expect(spy).toBe(["/latest"])
|
||||
})
|
||||
|
||||
it("should check if it's the current version", async () => {
|
||||
|
@ -131,23 +130,24 @@ describe.skip("update", () => {
|
|||
|
||||
const p = provider()
|
||||
let update = await p.getUpdate(true)
|
||||
assert.equal(p.isLatestVersion(update), false)
|
||||
expect(p.isLatestVersion(update)).toBe(false)
|
||||
|
||||
version = "0.0.0"
|
||||
update = await p.getUpdate(true)
|
||||
assert.equal(p.isLatestVersion(update), true)
|
||||
expect(p.isLatestVersion(update)).toBe(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)
|
||||
expect(p.isLatestVersion(update)).toBe(true)
|
||||
})
|
||||
|
||||
it("should not reject if unable to fetch", async () => {
|
||||
expect.assertions(2)
|
||||
let provider = new UpdateProvider("invalid", settings)
|
||||
await assert.doesNotReject(() => provider.getUpdate(true))
|
||||
await expect(() => provider.getUpdate(true)).resolves.toBe(undefined)
|
||||
|
||||
provider = new UpdateProvider("http://probably.invalid.dev.localhost/latest", settings)
|
||||
await assert.doesNotReject(() => provider.getUpdate(true))
|
||||
await expect(() => provider.getUpdate(true)).resolves.toBe(undefined)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,19 +1,18 @@
|
|||
import * as assert from "assert"
|
||||
import { normalize } from "../src/common/util"
|
||||
|
||||
describe("util", () => {
|
||||
describe("normalize", () => {
|
||||
it("should remove multiple slashes", () => {
|
||||
assert.equal(normalize("//foo//bar//baz///mumble"), "/foo/bar/baz/mumble")
|
||||
expect(normalize("//foo//bar//baz///mumble")).toBe("/foo/bar/baz/mumble")
|
||||
})
|
||||
|
||||
it("should remove trailing slashes", () => {
|
||||
assert.equal(normalize("qux///"), "qux")
|
||||
expect(normalize("qux///")).toBe("qux")
|
||||
})
|
||||
|
||||
it("should preserve trailing slash if it exists", () => {
|
||||
assert.equal(normalize("qux///", true), "qux/")
|
||||
assert.equal(normalize("qux", true), "qux")
|
||||
expect(normalize("qux///", true)).toBe("qux/")
|
||||
expect(normalize("qux", true)).toBe("qux")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue