Repository navigation
Expand file tree
/
Copy pathinstall.test.mjs
More file actions
273 lines (252 loc) · 11.7 KB
/
Copy pathinstall.test.mjs
File metadata and controls
273 lines (252 loc) · 11.7 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// install.mjs installs agy where the machine has none (magpie's sign-in
// runs it, niuniu_8964 on X: agy-cli in magpie's container) and keeps its
// own copy current. A local server stands in for Antigravity's manifests
// and downloads; nothing here reaches the network.
import { afterAll, beforeAll, beforeEach, expect, test } from "bun:test"
import { createHash } from "node:crypto"
import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import { gzipSync } from "node:zlib"
import { agyPlatform, installAgy, installedVersion, untarMember, updateOwnAgy } from "./install.mjs"
import { agyCandidates, ownAgyPath } from "./platform.mjs"
const unix = process.platform !== "win32"
// tar builds a ustar archive of [name, bytes, type?] entries.
function tar(entries) {
const blocks = []
for (const [name, data, type = "0"] of entries) {
const header = Buffer.alloc(512)
header.write(name.slice(0, 100), 0)
header.write("0000755\0", 100)
header.write("0000000\0", 108)
header.write("0000000\0", 116)
header.write(data.length.toString(8).padStart(11, "0") + "\0", 124)
header.write("00000000000\0", 136)
header.write(" ", 148)
header.write(type, 156)
header.write("ustar\0", 257)
header.write("00", 263)
let sum = 0
for (const b of header) sum += b
header.write(sum.toString(8).padStart(6, "0") + "\0 ", 148)
blocks.push(header, data, Buffer.alloc((512 - (data.length % 512)) % 512))
}
blocks.push(Buffer.alloc(1024))
return gzipSync(Buffer.concat(blocks))
}
const script = (version) => Buffer.from(`#!/bin/sh\necho "agy ${version}"\n`)
const sha = (b) => createHash("sha512").update(b).digest("hex")
let server
const files = new Map() // path → body
let manifest = null
let slow = false
let root
// failClose makes close() of the file install.mjs opens in dir with the
// given mode (0o600 the download, 0o755 the binary taken out of its
// archive) close for real, then fail with error: an installer that moved
// on before close() resolved installs a binary it never saw closed.
// It's restored after each test: fs.promises is one object.
function failClose(dir, mode, error) {
const real = fs.promises.open
const closed = []
fs.promises.open = async (file, flags, openMode) => {
const handle = await real(file, flags, openMode)
if (openMode !== mode || path.dirname(file) !== dir) return handle
const close = handle.close.bind(handle)
handle.close = async () => {
await close()
closed.push(file)
throw new Error(error)
}
return handle
}
return {
closed,
restore: () => { fs.promises.open = real },
}
}
beforeAll(() => {
server = Bun.serve({
port: 0,
async fetch(req) {
const url = new URL(req.url)
if (url.pathname.startsWith("/manifests/")) {
return manifest ? Response.json({ ...manifest, url: `http://127.0.0.1:${server.port}${manifest.url}` }) : new Response("no", { status: 404 })
}
const body = files.get(url.pathname)
if (!body) return new Response("no", { status: 404 })
if (slow) {
return new Response(new ReadableStream({
async pull(c) {
await Bun.sleep(50)
c.enqueue(new Uint8Array(1024))
},
}))
}
return new Response(body)
},
})
process.env.AGY_BRIDGE_INSTALL_BASE = `http://127.0.0.1:${server.port}/manifests/`
})
afterAll(() => {
server.stop(true)
delete process.env.AGY_BRIDGE_INSTALL_BASE
})
beforeEach(() => {
root = fs.mkdtempSync(path.join(os.tmpdir(), "agy-install-"))
files.clear()
manifest = null
slow = false
})
function release(version, entries) {
const body = tar(entries ?? [["antigravity", script(version)]])
files.set(`/r/${version}.tar.gz`, body)
manifest = { version, url: `/r/${version}.tar.gz`, sha512: sha(body) }
}
test("each machine is named as Antigravity's installer names it", () => {
expect(agyPlatform("linux", "x64", false)).toBe("linux_amd64")
expect(agyPlatform("linux", "arm64", true)).toBe("linux_arm64_musl")
expect(agyPlatform("darwin", "arm64")).toBe("darwin_arm64")
expect(agyPlatform("win32", "x64")).toBe("windows_amd64")
expect(agyPlatform("linux", "ia32", false)).toBe(null)
expect(agyPlatform("android", "arm64", false)).toBe(null)
})
test("the plugin's own agy is in its own data folder, found after the user's", () => {
expect(ownAgyPath({ XDG_DATA_HOME: "/config/data" }, "/config/home", "linux")).toBe("/config/data/magpie-agy-cli/agy")
expect(ownAgyPath({}, "/home/u", "linux")).toBe("/home/u/.local/share/magpie-agy-cli/agy")
expect(ownAgyPath({ XDG_DATA_HOME: "relative" }, "/home/u", "linux")).toBe("/home/u/.local/share/magpie-agy-cli/agy")
expect(ownAgyPath({ LOCALAPPDATA: "C:\\Users\\u\\AppData\\Local" }, "C:\\Users\\u", "win32")).toBe("C:\\Users\\u\\AppData\\Local\\magpie-agy-cli\\agy.exe")
const c = agyCandidates({ PATH: "/usr/bin", XDG_DATA_HOME: "/config/data" }, "/config/home", "linux")
expect(c.at(-1)).toBe("/config/data/magpie-agy-cli/agy")
expect(c[0]).toBe("/usr/bin/agy")
})
test("agy is taken out of its archive, past a long name and a ./ prefix", async () => {
const archive = path.join(root, "a.tar.gz")
const long = "x".repeat(150)
fs.writeFileSync(archive, tar([
["././@LongLink", Buffer.from(long + "\0"), "L"],
["short", Buffer.from("not this one")],
["README", Buffer.alloc(700, 65)],
["./antigravity", Buffer.alloc(5000, 7)],
]))
const out = path.join(root, "agy")
expect(await untarMember(archive, "antigravity", out)).toBe(true)
expect(fs.readFileSync(out).equals(Buffer.alloc(5000, 7))).toBe(true)
expect(await untarMember(archive, "nothere", path.join(root, "none"))).toBe(false)
expect(fs.existsSync(path.join(root, "none"))).toBe(false)
})
test.if(unix)("installs the manifest's agy, checked and runnable, leaving nothing else", async () => {
release("1.5.19")
const bin = path.join(root, "data", "magpie-agy-cli", "agy")
expect(await installAgy({ bin, platform: "linux_amd64" })).toBe("1.5.19")
expect(fs.statSync(bin).mode & 0o111).not.toBe(0)
expect(Bun.spawnSync([bin]).stdout.toString()).toContain("agy 1.5.19")
expect(installedVersion(bin)).toBe("1.5.19")
expect(fs.readdirSync(path.dirname(bin)).sort()).toEqual(["agy", "agy.version"])
})
test.if(unix)("a download that isn't the manifest's is never installed", async () => {
release("1.5.19")
manifest.sha512 = sha(Buffer.from("something else"))
const bin = path.join(root, "agy")
await expect(installAgy({ bin, platform: "linux_amd64" })).rejects.toThrow(/checksum/)
expect(fs.readdirSync(root)).toEqual([])
})
test.if(unix)("an agy that doesn't run here isn't put in place of one that does", async () => {
release("1.5.19")
const bin = path.join(root, "agy")
await installAgy({ bin, platform: "linux_amd64" })
release("1.6.0", [["antigravity", Buffer.from("#!/bin/sh\necho 'cannot execute: required file not found' >&2\nexit 126\n")]])
await expect(installAgy({ bin, platform: "linux_amd64" })).rejects.toThrow(/doesn't run here \(cannot execute/)
expect(Bun.spawnSync([bin]).stdout.toString()).toContain("agy 1.5.19")
expect(installedVersion(bin)).toBe("1.5.19")
})
test.if(unix)("install and update refuse a release below the default-agent version floor", async () => {
const bin = path.join(root, "agy")
release("1.5.18")
await expect(installAgy({ bin, platform: "linux_amd64" })).rejects.toThrow(/agy 1\.5\.18 is older than 1\.5\.19/)
expect(fs.readdirSync(root)).toEqual([])
release("1.5.19")
await installAgy({ bin, platform: "linux_amd64" })
release("1.5.18")
await expect(updateOwnAgy({ bin, used: bin, platform: "linux_amd64", env: {} })).rejects.toThrow(/older than 1\.5\.19/)
release("1.5.20", [["antigravity", script("1.5.18")]])
await expect(updateOwnAgy({ bin, used: bin, platform: "linux_amd64", env: {} })).rejects.toThrow(/older than 1\.5\.19/)
expect(installedVersion(bin)).toBe("1.5.19")
})
test.if(unix)("a manifest the server won't give says so", async () => {
await expect(installAgy({ bin: path.join(root, "agy"), platform: "linux_amd64" })).rejects.toThrow(/answered 404/)
})
test.if(unix)("an install given up stops its download and leaves nothing", async () => {
release("1.5.19")
slow = true
const bin = path.join(root, "agy")
const ctl = new AbortController()
const done = installAgy({ bin, platform: "linux_amd64", signal: ctl.signal })
await Bun.sleep(200)
ctl.abort()
await expect(done).rejects.toThrow()
expect(fs.readdirSync(root)).toEqual([])
})
test.if(unix)("only the plugin's own copy is kept current, and not when the user turned updates off", async () => {
const bin = path.join(root, "agy")
release("1.5.19")
expect(await updateOwnAgy({ bin, platform: "linux_amd64", env: {} })).toBe("none")
await installAgy({ bin, platform: "linux_amd64" })
expect(await updateOwnAgy({ bin, platform: "linux_amd64", env: {} })).toBe("current")
release("1.6.0")
expect(await updateOwnAgy({ bin, platform: "linux_amd64", env: { AGY_CLI_DISABLE_AUTO_UPDATE: "true" } })).toBe("off")
expect(await updateOwnAgy({ bin, platform: "linux_amd64", env: {}, used: "/usr/local/bin/agy" })).toBe("not used")
expect(installedVersion(bin)).toBe("1.5.19")
expect(await updateOwnAgy({ bin, platform: "linux_amd64", env: {}, used: bin })).toBe("updated")
expect(Bun.spawnSync([bin]).stdout.toString()).toContain("agy 1.6.0")
expect(installedVersion(bin)).toBe("1.6.0")
})
// The copy is what pickAgy falls back to where the user's agy is too old
// for this plugin (install.mjs). A copy that is too old itself stands in
// for nothing, so not being the agy in use can't be why it is left old:
// a machine whose own agy never brings itself up (a shadowing `agy` on
// PATH) would then never have a copy it can use, and would say only that
// its agy is older than this plugin runs.
test.if(unix)("a copy too old to stand in for the user's agy is brought up, not left", async () => {
const bin = path.join(root, "agy")
release("1.5.19")
await installAgy({ bin, platform: "linux_amd64" })
fs.writeFileSync(`${bin}.version`, "1.5.17\n")
release("1.6.0")
expect(await updateOwnAgy({ bin, platform: "linux_amd64", env: {}, used: "/usr/local/bin/agy" })).toBe("updated")
expect(installedVersion(bin)).toBe("1.6.0")
expect(Bun.spawnSync([bin]).stdout.toString()).toContain("agy 1.6.0")
})
// Both ways a release arrives (the binary inside its archive, or a raw
// file) are written through a handle whose close the installer awaits
// before the staged binary is run or renamed: a WriteStream's `finish`
// came first, and Linux won't exec a file still open for writing
// (ETXTBSY). A close that fails must fail the install, not be left
// behind it.
for (const [kind, mode] of [["archive", 0o755], ["raw", 0o600]]) {
test.if(unix)(`${kind}: a staged binary whose close fails isn't installed; the old one stays`, async () => {
const bin = path.join(root, "agy")
release("1.5.19")
await installAgy({ bin, platform: "linux_amd64" })
const before = fs.readFileSync(bin)
if (kind === "archive") release("1.5.20")
else {
const body = script("1.5.20")
files.set("/r/raw", body)
manifest = { version: "1.5.20", url: "/r/raw", sha512: sha(body) }
}
const failing = failClose(root, mode, "close: disk gone")
try {
await expect(installAgy({ bin, platform: "linux_amd64" })).rejects.toThrow("close: disk gone")
// closed for real (the fixture file), once, before the failure
expect(fs.readFileSync(bin).equals(before)).toBe(true)
expect(Bun.spawnSync([bin]).stdout.toString()).toContain("agy 1.5.19")
expect(installedVersion(bin)).toBe("1.5.19")
expect(fs.readdirSync(root).sort()).toEqual(["agy", "agy.version"])
expect(failing.closed.length).toBe(1)
expect(path.dirname(failing.closed[0])).toBe(root)
} finally {
failing.restore()
}
})
}