forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstallation.test.ts
More file actions
230 lines (213 loc) · 8.71 KB
/
Copy pathinstallation.test.ts
File metadata and controls
230 lines (213 loc) · 8.71 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { describe, expect } from "bun:test"
import { Effect, Layer, Stream } from "effect"
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
import { Installation } from "../../src/installation"
import { InstallationChannel } from "@opencode-ai/core/installation/version"
import { AppProcess } from "@opencode-ai/core/process"
import { testEffect } from "../lib/effect"
const encoder = new TextEncoder()
function mockHttpClient(handler: (request: HttpClientRequest.HttpClientRequest) => Response) {
const client = HttpClient.make((request) => Effect.succeed(HttpClientResponse.fromWeb(request, handler(request))))
return Layer.succeed(HttpClient.HttpClient, client)
}
function mockSpawner(
handler: (cmd: string, args: readonly string[]) => string | { code: number; stdout?: string; stderr?: string } = () =>
"",
) {
const spawner = ChildProcessSpawner.make((command) => {
const std = ChildProcess.isStandardCommand(command) ? command : undefined
const result = handler(std?.command ?? "", std?.args ?? [])
const output = typeof result === "string" ? { code: 0, stdout: result, stderr: "" } : result
return Effect.succeed(
ChildProcessSpawner.makeHandle({
pid: ChildProcessSpawner.ProcessId(0),
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(output.code)),
isRunning: Effect.succeed(false),
kill: () => Effect.void,
stdin: { [Symbol.for("effect/Sink/TypeId")]: Symbol.for("effect/Sink/TypeId") } as any,
stdout: output.stdout ? Stream.make(encoder.encode(output.stdout)) : Stream.empty,
stderr: output.stderr ? Stream.make(encoder.encode(output.stderr)) : Stream.empty,
all: Stream.empty,
getInputFd: () => ({ [Symbol.for("effect/Sink/TypeId")]: Symbol.for("effect/Sink/TypeId") }) as any,
getOutputFd: () => Stream.empty,
unref: Effect.succeed(Effect.void),
}),
)
})
return Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner)
}
function jsonResponse(body: unknown) {
return new Response(JSON.stringify(body), {
status: 200,
headers: { "content-type": "application/json" },
})
}
function testLayer(
httpHandler: (request: HttpClientRequest.HttpClientRequest) => Response,
spawnHandler?: (cmd: string, args: readonly string[]) => string | { code: number; stdout?: string; stderr?: string },
) {
const appProcess = AppProcess.layer.pipe(Layer.provide(mockSpawner(spawnHandler)))
return Installation.layer.pipe(Layer.provide(mockHttpClient(httpHandler)), Layer.provide(appProcess))
}
describe("installation", () => {
describe("latest", () => {
testEffect(testLayer(() => jsonResponse({ tag_name: "v1.2.3" }))).effect(
"reads release version from GitHub releases",
() =>
Effect.gen(function* () {
const result = yield* Installation.use.latest("unknown")
expect(result).toBe("1.2.3")
}),
)
testEffect(testLayer(() => jsonResponse({ tag_name: "v4.0.0-beta.1" }))).effect(
"strips v prefix from GitHub release tag",
() =>
Effect.gen(function* () {
const result = yield* Installation.use.latest("curl")
expect(result).toBe("4.0.0-beta.1")
}),
)
const npmCalls: string[] = []
testEffect(
testLayer((request) => {
npmCalls.push(request.url)
return jsonResponse({ version: "1.5.0" })
}),
).effect("reads npm versions via registry", () =>
Effect.gen(function* () {
const result = yield* Installation.use.latest("npm")
expect(result).toBe("1.5.0")
expect(npmCalls).toContain(`https://registry.npmjs.org/opencode-ai/${InstallationChannel}`)
}),
)
const bunCalls: string[] = []
testEffect(
testLayer((request) => {
bunCalls.push(request.url)
return jsonResponse({ version: "1.6.0" })
}),
).effect("reads bun versions via registry", () =>
Effect.gen(function* () {
const result = yield* Installation.use.latest("bun")
expect(result).toBe("1.6.0")
expect(bunCalls).toContain(`https://registry.npmjs.org/opencode-ai/${InstallationChannel}`)
}),
)
const pnpmCalls: string[] = []
testEffect(
testLayer((request) => {
pnpmCalls.push(request.url)
return jsonResponse({ version: "1.7.0" })
}),
).effect("reads pnpm versions via registry", () =>
Effect.gen(function* () {
const result = yield* Installation.use.latest("pnpm")
expect(result).toBe("1.7.0")
expect(pnpmCalls).toContain(`https://registry.npmjs.org/opencode-ai/${InstallationChannel}`)
}),
)
testEffect(testLayer(() => jsonResponse({ version: "2.3.4" }))).effect("reads scoop manifest versions", () =>
Effect.gen(function* () {
const result = yield* Installation.use.latest("scoop")
expect(result).toBe("2.3.4")
}),
)
testEffect(testLayer(() => jsonResponse({ d: { results: [{ Version: "3.4.5" }] } }))).effect(
"reads chocolatey feed versions",
() =>
Effect.gen(function* () {
const result = yield* Installation.use.latest("choco")
expect(result).toBe("3.4.5")
}),
)
testEffect(
testLayer(
() => jsonResponse({ versions: { stable: "2.0.0" } }),
(cmd, args) => {
// getBrewFormula: return core formula (no tap)
if (cmd === "brew" && args.includes("--formula") && args.includes("anomalyco/tap/opencode")) return ""
if (cmd === "brew" && args.includes("--formula") && args.includes("opencode")) return "opencode"
return ""
},
),
).effect("reads brew formulae API versions", () =>
Effect.gen(function* () {
const result = yield* Installation.use.latest("brew")
expect(result).toBe("2.0.0")
}),
)
const brewInfoJson = JSON.stringify({
formulae: [{ versions: { stable: "2.1.0" } }],
})
testEffect(
testLayer(
() => jsonResponse({}), // HTTP not used for tap formula
(cmd, args) => {
if (cmd === "brew" && args.includes("anomalyco/tap/opencode") && args.includes("--formula")) return "opencode"
if (cmd === "brew" && args.includes("--json=v2")) return brewInfoJson
return ""
},
),
).effect("reads brew tap info JSON via CLI", () =>
Effect.gen(function* () {
const result = yield* Installation.use.latest("brew")
expect(result).toBe("2.1.0")
}),
)
})
describe("upgrade", () => {
testEffect(
testLayer(
() => jsonResponse({}),
(cmd) => {
if (cmd === "npm") return { code: 1, stderr: "token=secret command output" }
return ""
},
),
).effect("returns sanitized typed errors for failed package upgrades", () =>
Effect.gen(function* () {
const error = yield* Effect.flip(Installation.use.upgrade("npm", "9.9.9"))
expect(error).toBeInstanceOf(Installation.UpgradeFailedError)
expect(error.stderr).toBe("Upgrade failed for npm (exit code 1).")
expect(error.message).toBe(error.stderr)
expect(error.stderr).not.toContain("secret")
expect(error.stderr).not.toContain("command output")
}),
)
testEffect(
testLayer(
() => new Response("install script with token=secret", { status: 200 }),
(cmd, args) => {
if (cmd === "bash" && args[0] === "--version") return "GNU bash"
if (cmd === "bash" || cmd === "sh") return { code: 1, stderr: "script output with token=secret" }
return ""
},
),
).effect("returns sanitized typed errors when the curl install script fails", () =>
Effect.gen(function* () {
const error = yield* Effect.flip(Installation.use.upgrade("curl", "9.9.9"))
expect(error).toBeInstanceOf(Installation.UpgradeFailedError)
expect(error.stderr).toBe("Upgrade failed for curl (exit code 1).")
expect(error.message).toBe(error.stderr)
expect(error.stderr).not.toContain("secret")
expect(error.stderr).not.toContain("script output")
}),
)
testEffect(
testLayer(
() => new Response("install script", { status: 200 }),
(cmd, args) => {
if (cmd === "bash" && args[0] === "--version") return { code: 1, stderr: "missing" }
if (cmd === "bash") return { code: 1, stderr: "should not execute installer with bash" }
if (cmd === "sh") return "ok"
return ""
},
),
).effect("falls back to sh when bash is unavailable during curl upgrade", () =>
Effect.gen(function* () {
yield* Installation.use.upgrade("curl", "9.9.9")
}),
)
})
})