forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-native.test.mjs
More file actions
236 lines (201 loc) · 9.53 KB
/
Copy pathfetch-native.test.mjs
File metadata and controls
236 lines (201 loc) · 9.53 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { execFileSync, spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import test from 'node:test';
const version = '1.0.79';
const integrity = 'sha512-test-integrity';
const runtimeContent = 'runtime content';
const wrapperContent = 'wrapper content';
const stagingSchema = 'hostless-runtime-v2';
const scriptPath = fileURLToPath(new URL('./fetch-native.mjs', import.meta.url));
for (const classifier of ['linux-x64', 'linux-arm64', 'win32-x64', 'win32-arm64', 'darwin-arm64']) {
test(`${classifier}: complete hostless artifacts use incremental fast path without a CLI`, (t) => {
const fixture = createFixture(t, classifier);
const result = runScript(fixture);
assert.equal(result.status, 0, result.stderr);
assert.match(result.stdout, /already staged/);
assert.equal(fs.existsSync(fixture.npmMarkerPath), false);
});
test(`${classifier}: missing runtime wrapper does not use incremental fast path`, (t) => {
const fixture = createFixture(t, classifier);
fs.rmSync(fixture.wrapperPath);
const result = runScript(fixture);
assertRestagingAttempted(fixture, result);
});
test(`${classifier}: legacy staging schema does not use incremental fast path`, (t) => {
const fixture = createFixture(t, classifier);
const stampPath = path.join(fixture.stagingDir, classifier, '.version');
fs.writeFileSync(stampPath, fs.readFileSync(stampPath, 'utf8').replace(stagingSchema, 'hostless-runtime-v1'));
const result = runScript(fixture);
assertRestagingAttempted(fixture, result);
});
test(`${classifier}: missing platform metadata does not use incremental fast path`, (t) => {
const fixture = createFixture(t, classifier);
fs.rmSync(fixture.platformPropertiesPath);
const result = runScript(fixture);
assertRestagingAttempted(fixture, result);
});
test(`${classifier}: missing retained runtime asset does not use incremental fast path`, (t) => {
const fixture = createFixture(t, classifier);
fs.rmSync(fixture.ripgrepPath);
const result = runScript(fixture);
assertRestagingAttempted(fixture, result);
});
test(`${classifier}: complete matching artifacts use incremental fast path`, (t) => {
const fixture = createFixture(t, classifier);
const result = runScript(fixture);
assert.equal(result.status, 0, result.stderr);
assert.match(result.stdout, /already staged/);
assert.equal(fs.existsSync(fixture.npmMarkerPath), false);
});
}
test('stages retained package assets and excludes CLI-only content', (t) => {
const classifier = 'linux-x64';
const fixture = createFixture(t, classifier);
const packageRoot = path.join(fixture.repoRoot, 'package-root', 'package');
fs.mkdirSync(path.join(packageRoot, 'prebuilds', classifier), { recursive: true });
fs.mkdirSync(path.join(packageRoot, 'ripgrep', 'bin', classifier), { recursive: true });
fs.mkdirSync(path.join(packageRoot, 'definitions'), { recursive: true });
fs.writeFileSync(path.join(packageRoot, 'copilot'), 'excluded');
fs.writeFileSync(path.join(packageRoot, 'prebuilds', classifier, 'runtime.node'), runtimeContent);
fs.writeFileSync(path.join(packageRoot, 'prebuilds', classifier, 'copilot-runtime'), wrapperContent);
fs.writeFileSync(path.join(packageRoot, 'ripgrep', 'bin', classifier, 'rg'), 'ripgrep content');
fs.chmodSync(path.join(packageRoot, 'ripgrep', 'bin', classifier, 'rg'), 0o755);
fs.writeFileSync(path.join(packageRoot, 'definitions', 'future.json'), '{}');
fs.writeFileSync(path.join(packageRoot, 'app.js'), 'excluded');
fs.writeFileSync(path.join(packageRoot, 'LICENSE.md'), 'excluded');
fs.writeFileSync(path.join(packageRoot, 'README.md'), 'excluded');
const tarball = path.join(fixture.repoRoot, 'fixture.tgz');
execFileSync('tar', ['-czf', tarball, '-C', path.dirname(packageRoot), 'package']);
const packageIntegrity = digest(fs.readFileSync(tarball));
fs.writeFileSync(
path.join(fixture.repoRoot, 'nodejs', 'package-lock.json'),
JSON.stringify({
packages: {
[`node_modules/@github/copilot-${classifier}`]: { version, integrity: packageIntegrity },
},
}),
);
const fakeNpmPath = path.join(fixture.fakeBinDir, process.platform === 'win32' ? 'npm.cmd' : 'npm');
const fakeNpm =
process.platform === 'win32'
? '@copy "%FETCH_NATIVE_TARBALL%" "%4\\fixture.tgz" >nul\r\n@echo fixture.tgz\r\n'
: '#!/bin/sh\ncp "$FETCH_NATIVE_TARBALL" "$4/fixture.tgz"\nprintf "fixture.tgz\\n"\n';
fs.writeFileSync(fakeNpmPath, fakeNpm);
fs.chmodSync(fakeNpmPath, 0o755);
fs.rmSync(path.join(fixture.stagingDir, classifier), { recursive: true, force: true });
const result = runScript(fixture, { FETCH_NATIVE_TARBALL: tarball });
assert.equal(result.status, 0, result.stderr);
const resourceDir = path.join(fixture.stagingDir, classifier, 'native', classifier);
assert.equal(fs.readFileSync(path.join(resourceDir, 'ripgrep', 'bin', classifier, 'rg'), 'utf8'), 'ripgrep content');
assert.equal(fs.readFileSync(path.join(resourceDir, 'definitions', 'future.json'), 'utf8'), '{}');
assert.equal(fs.existsSync(path.join(resourceDir, 'app.js')), false);
assert.equal(fs.existsSync(path.join(resourceDir, 'copilot')), false);
assert.equal(fs.existsSync(path.join(resourceDir, 'LICENSE.md')), false);
assert.equal(fs.existsSync(path.join(resourceDir, 'README.md')), false);
assert.match(fs.readFileSync(path.join(resourceDir, 'runtime-assets.list'), 'utf8'), /ripgrep\/bin\/linux-x64\/rg/);
});
function createFixture(t, classifier) {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'fetch-native-test-'));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const repoRoot = path.join(root, 'repo');
const stagingDir = path.join(root, 'staging');
const resourceDir = path.join(stagingDir, classifier, 'native', classifier);
const fakeBinDir = path.join(root, 'bin');
const npmMarkerPath = path.join(root, 'npm-invoked');
fs.mkdirSync(path.join(repoRoot, 'nodejs'), { recursive: true });
fs.mkdirSync(resourceDir, { recursive: true });
fs.mkdirSync(fakeBinDir);
fs.writeFileSync(
path.join(repoRoot, 'nodejs', 'package-lock.json'),
JSON.stringify({
packages: {
[`node_modules/@github/copilot-${classifier}`]: { version, integrity },
},
}),
);
const runtimePath = path.join(resourceDir, 'runtime.node');
const wrapperPath = path.join(
resourceDir,
classifier.startsWith('win32') ? 'copilot-runtime.exe' : 'copilot-runtime',
);
const platformPropertiesPath = path.join(resourceDir, 'platform.properties');
const ripgrepPath = path.join(resourceDir, 'ripgrep', 'bin', classifier, 'rg');
const inventoryPath = path.join(resourceDir, 'runtime-assets.list');
fs.mkdirSync(path.dirname(ripgrepPath), { recursive: true });
fs.writeFileSync(runtimePath, runtimeContent);
fs.writeFileSync(wrapperPath, wrapperContent);
fs.writeFileSync(ripgrepPath, 'ripgrep content');
fs.writeFileSync(
inventoryPath,
`644\truntime.node\n755\tcopilot-runtime\n755\tripgrep/bin/${classifier}/rg\n`,
);
fs.writeFileSync(platformPropertiesPath, `classifier=${classifier}\nversion=${version}\n`);
fs.writeFileSync(
path.join(stagingDir, classifier, '.version'),
`${stagingSchema}\n${version}\n${integrity}\n${digestTree(resourceDir)}\n`,
);
const fakeNpmPath = path.join(fakeBinDir, process.platform === 'win32' ? 'npm.cmd' : 'npm');
if (process.platform === 'win32') {
fs.writeFileSync(fakeNpmPath, '@echo off\r\n> "%FETCH_NATIVE_NPM_MARKER%" echo invoked\r\nexit /b 42\r\n');
} else {
fs.writeFileSync(fakeNpmPath, '#!/bin/sh\nprintf invoked > "$FETCH_NATIVE_NPM_MARKER"\nexit 42\n');
fs.chmodSync(fakeNpmPath, 0o755);
}
return {
classifier,
repoRoot,
stagingDir,
fakeBinDir,
npmMarkerPath,
runtimePath,
wrapperPath,
ripgrepPath,
platformPropertiesPath,
};
}
function runScript(fixture, extraEnv = {}) {
return spawnSync(process.execPath, [scriptPath, fixture.repoRoot, fixture.stagingDir, fixture.classifier], {
encoding: 'utf8',
env: {
...process.env,
PATH: `${fixture.fakeBinDir}${path.delimiter}${process.env.PATH}`,
FETCH_NATIVE_NPM_MARKER: fixture.npmMarkerPath,
...extraEnv,
},
});
}
function assertRestagingAttempted(fixture, result) {
assert.notEqual(result.status, 0, 'The fake npm command should make restaging fail');
assert.equal(fs.readFileSync(fixture.npmMarkerPath, 'utf8').trim(), 'invoked');
}
function digestTree(directory) {
const hash = createHash('sha512');
for (const file of walkFiles(directory).sort()) {
const relative = path.relative(directory, file).split(path.sep).join('/');
hash.update(relative).update('\0').update(fs.readFileSync(file)).update('\0');
}
return `sha512-${hash.digest('base64')}`;
}
function digest(content) {
return `sha512-${createHash('sha512').update(content).digest('base64')}`;
}
function walkFiles(directory) {
const files = [];
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
const entryPath = path.join(directory, entry.name);
if (entry.isDirectory()) {
files.push(...walkFiles(entryPath));
} else {
files.push(entryPath);
}
}
return files;
}