Repository navigation
Expand file tree
/
Copy pathruntimeArtifacts.test.ts
More file actions
330 lines (302 loc) · 14.3 KB
/
Copy pathruntimeArtifacts.test.ts
File metadata and controls
330 lines (302 loc) · 14.3 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import { existsSync, mkdtempSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { createHash } from "node:crypto";
import { spawnSync } from "node:child_process";
import { c as createTar } from "tar";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
defaultRuntimeCacheRoot,
ensureRuntimeBundle,
getRuntimePackageName,
getRuntimePlatform,
getRuntimeReleaseAssetName,
materializeRuntimeBundle,
} from "../src/runtimeArtifacts.js";
import { COPILOT_CLI_USE_NPM_PACKAGE, COPILOT_CLI_VERSION } from "../src/cliVersion.js";
import { ensureCopilotPackage } from "../scripts/releaseArtifacts.js";
describe("defaultRuntimeCacheRoot", () => {
it.each([
[
"darwin",
"/home/test",
{},
join("/home/test", "Library", "Caches", "github-copilot-sdk", "runtime"),
],
["linux", "/home/test", {}, join("/home/test", ".cache", "github-copilot-sdk", "runtime")],
[
"linux",
"/home/test",
{ XDG_CACHE_HOME: "/cache" },
join("/cache", "github-copilot-sdk", "runtime"),
],
[
"win32",
"C:\\Users\\test",
{ LOCALAPPDATA: "C:\\Users\\test\\AppData\\Local" },
join("C:\\Users\\test\\AppData\\Local", "github-copilot-sdk", "runtime"),
],
])("uses the %s user cache directory", (platform, home, environment, expected) => {
expect(defaultRuntimeCacheRoot(platform, home, environment)).toBe(expected);
});
});
describe("release runtime selection", () => {
it("keeps the compiled CLI version aligned with package metadata", () => {
const packageJson = JSON.parse(
readFileSync(join(import.meta.dirname, "../package.json"), "utf8")
);
expect(COPILOT_CLI_VERSION).toBe(packageJson.copilotCliVersion);
// lgtm[js/trivial-conditional] This generated constant is true for internal canary builds.
if (COPILOT_CLI_USE_NPM_PACKAGE) {
expect(packageJson.dependencies["@github/copilot"]).toBe(COPILOT_CLI_VERSION);
} else {
expect(packageJson.dependencies).not.toHaveProperty("@github/copilot");
}
});
it("can pin an internal npm package without contacting GitHub Releases", () => {
const root = mkdtempSync(join(tmpdir(), "copilot-cli-version-"));
mkdirSync(join(root, "scripts"), { recursive: true });
mkdirSync(join(root, "src"), { recursive: true });
writeFileSync(join(root, "package.json"), "{}\n");
writeFileSync(
join(root, "scripts", "set-cli-version.js"),
readFileSync(join(import.meta.dirname, "../scripts/set-cli-version.js"))
);
const result = spawnSync(
process.execPath,
[join(root, "scripts", "set-cli-version.js"), "9.9.9-canary.test", "--npm-package"],
{ encoding: "utf8" }
);
expect(result.status, result.stderr).toBe(0);
expect(JSON.parse(readFileSync(join(root, "package.json"), "utf8"))).toMatchObject({
copilotCliVersion: "9.9.9-canary.test",
});
expect(existsSync(join(root, "copilot-cli.json"))).toBe(false);
expect(readFileSync(join(root, "src", "cliVersion.ts"), "utf8")).toContain(
"COPILOT_CLI_USE_NPM_PACKAGE = true"
);
});
it.each([
["darwin", "arm64", false, "darwin-arm64"],
["darwin", "x64", false, "darwin-x64"],
["linux", "arm64", false, "linux-arm64"],
["linux", "x64", true, "linuxmusl-x64"],
["win32", "arm64", false, "win32-arm64"],
])("maps %s/%s to %s", (platform, arch, musl, expected) => {
expect(getRuntimePlatform(platform, arch, musl)).toBe(expected);
});
it("uses the platform npm tarball published in the CLI release", () => {
expect(getRuntimeReleaseAssetName("1.2.3-4", "linux-x64")).toBe(
"github-copilot-1.2.3-4-linux-x64.tgz"
);
});
it("uses the SDK platform package namespace", () => {
expect(getRuntimePackageName("linux-x64")).toBe("@github/copilot-sdk-linux-x64");
});
});
describe("materializeRuntimeBundle", () => {
afterEach(() => vi.unstubAllEnvs());
it("materializes an adjacent pair from an absent cache with a stripped environment", () => {
const sourceDir = mkdtempSync(join(tmpdir(), "copilot-runtime-source-"));
const cacheRoot = join(sourceDir, "absent-cache");
const emptyPath = join(sourceDir, "empty-path");
mkdirSync(emptyPath);
const platform = process.platform === "win32" ? "win32-x64" : "test-platform";
const wrapperName = platform.startsWith("win32")
? "copilot-runtime.exe"
: "copilot-runtime";
const prebuilds = join(sourceDir, "prebuilds", platform);
const wrapper = join(prebuilds, wrapperName);
const runtimeNode = join(prebuilds, "runtime.node");
mkdirSync(prebuilds, { recursive: true });
writeFileSync(wrapper, "wrapper");
writeFileSync(runtimeNode, "runtime");
mkdirSync(join(sourceDir, "ripgrep", "bin", platform), { recursive: true });
writeFileSync(join(sourceDir, "ripgrep", "bin", platform, "rg"), "ripgrep");
mkdirSync(join(sourceDir, "definitions"), { recursive: true });
writeFileSync(join(sourceDir, "definitions", "future.json"), "{}");
mkdirSync(join(sourceDir, "copilot-sdk"), { recursive: true });
writeFileSync(join(sourceDir, "copilot-sdk", "extension.js"), "extension SDK");
mkdirSync(join(sourceDir, "preloads"), { recursive: true });
writeFileSync(join(sourceDir, "preloads", "extension_bootstrap.mjs"), "bootstrap");
mkdirSync(join(sourceDir, "sdk"), { recursive: true });
writeFileSync(join(sourceDir, "sdk", "index.js"), "legacy SDK");
writeFileSync(join(sourceDir, "app.js"), "excluded");
writeFileSync(join(sourceDir, "copilot"), "excluded");
writeFileSync(join(sourceDir, "copilot.exe"), "excluded");
writeFileSync(join(sourceDir, "LICENSE.md"), "excluded");
writeFileSync(join(sourceDir, "README.md"), "excluded");
vi.stubEnv("PATH", emptyPath);
vi.stubEnv("COPILOT_CLI_PATH", undefined);
vi.stubEnv("COPILOT_RUNTIME_HOST_COMMAND", undefined);
vi.stubEnv("COPILOT_RUNTIME_PROVIDER_LIB", undefined);
expect(process.env.COPILOT_CLI_PATH).toBeUndefined();
expect(process.env.COPILOT_RUNTIME_HOST_COMMAND).toBeUndefined();
expect(process.env.COPILOT_RUNTIME_PROVIDER_LIB).toBeUndefined();
const installedWrapper = materializeRuntimeBundle(
{ packageRoot: sourceDir, platform },
cacheRoot
);
const installDir = resolve(dirname(installedWrapper), "..", "..");
expect(readFileSync(installedWrapper, "utf8")).toBe("wrapper");
expect(readFileSync(join(installDir, "prebuilds", platform, "runtime.node"), "utf8")).toBe(
"runtime"
);
expect(readFileSync(join(installDir, "ripgrep", "bin", platform, "rg"), "utf8")).toBe(
"ripgrep"
);
expect(readFileSync(join(installDir, "copilot-sdk", "extension.js"), "utf8")).toBe(
"extension SDK"
);
expect(readFileSync(join(installDir, "preloads", "extension_bootstrap.mjs"), "utf8")).toBe(
"bootstrap"
);
expect(readFileSync(join(installDir, "sdk", "index.js"), "utf8")).toBe("legacy SDK");
expect(existsSync(join(installDir, "app.js"))).toBe(false);
expect(existsSync(join(installDir, "copilot"))).toBe(false);
expect(existsSync(join(installDir, "copilot.exe"))).toBe(false);
expect(existsSync(join(installDir, "LICENSE.md"))).toBe(false);
expect(existsSync(join(installDir, "README.md"))).toBe(false);
if (process.platform !== "win32") {
expect(statSync(installedWrapper).mode & 0o111).not.toBe(0);
}
});
it("fails clearly when the package has no runtime.node", () => {
const sourceDir = mkdtempSync(join(tmpdir(), "copilot-runtime-missing-node-"));
const platform = process.platform === "win32" ? "win32-x64" : "test-platform";
const wrapperName = platform.startsWith("win32")
? "copilot-runtime.exe"
: "copilot-runtime";
const prebuilds = join(sourceDir, "prebuilds", platform);
const wrapper = join(prebuilds, wrapperName);
mkdirSync(prebuilds, { recursive: true });
writeFileSync(wrapper, "wrapper");
expect(() =>
materializeRuntimeBundle(
{
packageRoot: sourceDir,
platform,
},
join(sourceDir, "cache")
)
).toThrow(/Copilot runtime\.node not found/);
});
});
describe("ensureRuntimeBundle", () => {
it("resolves the installed platform runtime without network access", async () => {
const root = mkdtempSync(join(tmpdir(), "copilot-packaged-runtime-"));
const nodeModules = join(root, "node_modules");
const platform = "linux-x64";
const packageRoot = join(nodeModules, ...getRuntimePackageName(platform).split("/"));
const prebuilds = join(packageRoot, "prebuilds", platform);
mkdirSync(prebuilds, { recursive: true });
writeFileSync(join(packageRoot, "package.json"), "{}");
writeFileSync(join(prebuilds, "copilot-runtime"), "wrapper");
writeFileSync(join(prebuilds, "runtime.node"), "runtime");
mkdirSync(join(packageRoot, "schemas"));
writeFileSync(join(packageRoot, "schemas", "api.schema.json"), "{}");
const fetcher = vi.fn(() => {
throw new Error("runtime resolution must not fetch");
});
vi.stubGlobal("fetch", fetcher);
const runtimePath = await ensureRuntimeBundle(COPILOT_CLI_VERSION, {
packageSearchPaths: [nodeModules],
platform,
});
expect(runtimePath).toBe(join(prebuilds, "copilot-runtime"));
expect(readFileSync(join(dirname(runtimePath), "runtime.node"), "utf8")).toBe("runtime");
expect(fetcher).not.toHaveBeenCalled();
vi.unstubAllGlobals();
});
it("fails clearly when the platform package is not installed", async () => {
await expect(
ensureRuntimeBundle(COPILOT_CLI_VERSION, {
packageSearchPaths: [],
platform: "linux-x64",
})
).rejects.toThrow(
"Could not resolve @github/copilot-sdk-linux-x64. Reinstall @github/copilot-sdk"
);
});
});
describe("release package acquisition", () => {
it("downloads, verifies, and caches a release package for packaging", async () => {
const sourceRoot = mkdtempSync(join(tmpdir(), "copilot-release-source-"));
const packageRoot = join(sourceRoot, "package");
const platform = "linux-x64";
const prebuilds = join(packageRoot, "prebuilds", platform);
mkdirSync(prebuilds, { recursive: true });
writeFileSync(join(prebuilds, "copilot-runtime"), "wrapper");
writeFileSync(join(prebuilds, "runtime.node"), "runtime");
mkdirSync(join(packageRoot, "schemas"), { recursive: true });
writeFileSync(join(packageRoot, "schemas", "api.schema.json"), "{}");
const archivePath = join(sourceRoot, "runtime.tgz");
await createTar({ cwd: sourceRoot, file: archivePath, gzip: true }, ["package"]);
const archive = readFileSync(archivePath);
const version = "1.2.3-4";
const assetName = getRuntimeReleaseAssetName(version, platform);
const checksum = createHash("sha256").update(archive).digest("hex");
const fetcher = vi.fn(async (input: string | URL | Request) =>
String(input).endsWith("/SHA256SUMS.txt")
? new Response(`${checksum} ${assetName}\n`)
: new Response(archive)
);
const cacheRoot = join(sourceRoot, "cache");
const downloadedPackage = await ensureCopilotPackage(version, {
cacheRoot,
fetch: fetcher,
platform,
});
expect(readFileSync(join(downloadedPackage, "schemas", "api.schema.json"), "utf8")).toBe(
"{}"
);
await expect(
ensureCopilotPackage(version, {
cacheRoot,
fetch: fetcher,
platform,
})
).resolves.toBe(downloadedPackage);
expect(fetcher).toHaveBeenCalledTimes(2);
});
it("rejects a release package that does not match SHA256SUMS.txt", async () => {
const cacheRoot = mkdtempSync(join(tmpdir(), "copilot-release-mismatch-"));
const assetName = getRuntimeReleaseAssetName("1.2.3", "linux-x64");
const fetcher = vi.fn(async (input: string | URL | Request) =>
String(input).endsWith("/SHA256SUMS.txt")
? new Response(`${"0".repeat(64)} ${assetName}\n`)
: new Response("corrupt archive")
);
await expect(
ensureCopilotPackage("1.2.3", {
cacheRoot,
fetch: fetcher,
platform: "linux-x64",
})
).rejects.toThrow("Checksum mismatch");
expect(existsSync(join(cacheRoot, "1.2.3", "packages", "linux-x64"))).toBe(false);
});
it("times out and retries a stalled release download", async () => {
const cacheRoot = mkdtempSync(join(tmpdir(), "copilot-release-timeout-"));
const fetcher = vi.fn(
(_input: string | URL | Request, init?: RequestInit) =>
new Promise<Response>((_resolve, reject) => {
const signal = init?.signal;
if (!signal) {
reject(new Error("Expected a request timeout signal."));
return;
}
signal.addEventListener("abort", () => reject(signal.reason), { once: true });
})
);
await expect(
ensureCopilotPackage("1.2.3-timeout", {
cacheRoot,
fetch: fetcher,
fetchTimeoutMs: 10,
platform: "linux-x64",
})
).rejects.toThrow("Failed to download");
expect(fetcher).toHaveBeenCalledTimes(3);
});
});