Repository navigation
Expand file tree
/
Copy pathexecute.test.ts
More file actions
98 lines (82 loc) · 2.91 KB
/
Copy pathexecute.test.ts
File metadata and controls
98 lines (82 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {
LAUNCH_PROTOCOL_VERSION,
LAUNCH_PROTOCOL_VERSION_VARIABLE,
POST_UPDATE_SERVICE_START_EXIT_CODE,
RELAUNCH_EXIT_CODE,
type UpdateAction,
} from "@magnitudedev/release"
import { Effect, Option } from "effect"
import { afterEach, describe, expect, it, vi } from "vitest"
import { executeUpdate } from "./execute"
import {
UpdateCommandFailed,
type CliUpdaterShape,
} from "./updater"
const action: UpdateAction = {
method: "npm",
command: "npm",
args: ["install", "-g", "@magnitudedev/cli@1.2.3"],
}
const updaterWith = (
runUpdate: CliUpdaterShape["runUpdate"],
): CliUpdaterShape => ({
packageManager: Option.some("npm"),
discover: Effect.never,
updateTarget: Effect.succeed(Option.some("1.2.3")),
dismissVersion: () => Effect.void,
runUpdate,
})
const originalProtocolVersion = process.env[LAUNCH_PROTOCOL_VERSION_VARIABLE]
afterEach(() => {
vi.restoreAllMocks()
if (originalProtocolVersion === undefined) {
delete process.env[LAUNCH_PROTOCOL_VERSION_VARIABLE]
} else {
process.env[LAUNCH_PROTOCOL_VERSION_VARIABLE] = originalProtocolVersion
}
})
describe("executeUpdate", () => {
it("returns success without mutating process exit state", async () => {
vi.spyOn(process.stdout, "write").mockImplementation(() => true)
const previousExitCode = process.exitCode
const exitCode = await Effect.runPromise(executeUpdate(
updaterWith(() => Effect.void),
action,
))
expect(exitCode).toBe(0)
expect(process.exitCode).toBe(previousExitCode)
})
it("returns the relaunch code only for a compatible launcher", async () => {
vi.spyOn(process.stdout, "write").mockImplementation(() => true)
process.env[LAUNCH_PROTOCOL_VERSION_VARIABLE] = String(LAUNCH_PROTOCOL_VERSION)
const exitCode = await Effect.runPromise(executeUpdate(
updaterWith(() => Effect.void),
action,
{ relaunch: true },
))
expect(exitCode).toBe(RELAUNCH_EXIT_CODE)
})
it("asks a compatible launcher to start the service after an explicit update", async () => {
vi.spyOn(process.stdout, "write").mockImplementation(() => true)
process.env[LAUNCH_PROTOCOL_VERSION_VARIABLE] = String(LAUNCH_PROTOCOL_VERSION)
const exitCode = await Effect.runPromise(executeUpdate(
updaterWith(() => Effect.void),
action,
))
expect(exitCode).toBe(POST_UPDATE_SERVICE_START_EXIT_CODE)
})
it("returns failure without mutating process exit state", async () => {
vi.spyOn(process.stdout, "write").mockImplementation(() => true)
vi.spyOn(process.stderr, "write").mockImplementation(() => true)
const previousExitCode = process.exitCode
const exitCode = await Effect.runPromise(executeUpdate(
updaterWith(() => Effect.fail(new UpdateCommandFailed({
command: "npm",
reason: "exited with status 7",
}))),
action,
))
expect(exitCode).toBe(1)
expect(process.exitCode).toBe(previousExitCode)
})
})