-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathhelpers.test.ts
More file actions
390 lines (320 loc) · 13.5 KB
/
helpers.test.ts
File metadata and controls
390 lines (320 loc) · 13.5 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import {
MAX_ZIP_SIZE_BYTES,
convertWindowsScriptsToLinux,
convertWindowsScriptsToLinuxSync,
copySourceTree,
copySourceTreeSync,
createZipFromDir,
createZipFromDirSync,
enforceZipSizeLimit,
enforceZipSizeLimitSync,
ensureDirClean,
ensureDirCleanSync,
isNodeRuntime,
isPythonRuntime,
resolveCodeLocation,
resolveProjectPaths,
resolveProjectPathsSync,
} from '../helpers.js';
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
// ── Pure function tests ──────────────────────────────────────────────
describe('isPythonRuntime', () => {
it('returns true for PYTHON_3_12', () => {
expect(isPythonRuntime('PYTHON_3_12')).toBe(true);
});
it('returns true for PYTHON_3_13', () => {
expect(isPythonRuntime('PYTHON_3_13')).toBe(true);
});
it('returns false for NODE_20', () => {
expect(isPythonRuntime('NODE_20')).toBe(false);
});
it('returns false for NODE_22', () => {
expect(isPythonRuntime('NODE_22')).toBe(false);
});
});
describe('isNodeRuntime', () => {
it('returns true for NODE_20', () => {
expect(isNodeRuntime('NODE_20')).toBe(true);
});
it('returns true for NODE_22', () => {
expect(isNodeRuntime('NODE_22')).toBe(true);
});
it('returns false for PYTHON_3_12', () => {
expect(isNodeRuntime('PYTHON_3_12')).toBe(false);
});
});
describe('resolveCodeLocation', () => {
it('returns absolute path unchanged', () => {
expect(resolveCodeLocation('/absolute/path/to/code', '/home/user/proj/agentcore')).toBe('/absolute/path/to/code');
});
it('resolves relative path against repository root (parent of agentcore/)', () => {
const result = resolveCodeLocation('./app/MyAgent', '/home/user/proj/agentcore');
expect(result).toContain('/home/user/proj/app/MyAgent');
});
it('resolves relative path without leading ./', () => {
const result = resolveCodeLocation('app/MyAgent', '/home/user/proj/agentcore');
expect(result).toContain('/home/user/proj/app/MyAgent');
});
});
describe('MAX_ZIP_SIZE_BYTES', () => {
it('is 250 MB', () => {
expect(MAX_ZIP_SIZE_BYTES).toBe(250 * 1024 * 1024);
});
});
// ── Real filesystem tests ────────────────────────────────────────────
describe('ensureDirClean', () => {
let root: string;
beforeAll(() => {
root = mkdtempSync(join(tmpdir(), 'helpers-clean-'));
});
afterAll(() => {
rmSync(root, { recursive: true, force: true });
});
it('creates a fresh empty directory', async () => {
const dir = join(root, 'fresh');
await ensureDirClean(dir);
expect(existsSync(dir)).toBe(true);
});
it('removes existing contents and recreates', async () => {
const dir = join(root, 'dirty');
mkdirSync(dir, { recursive: true });
writeFileSync(join(dir, 'old.txt'), 'old');
await ensureDirClean(dir);
expect(existsSync(join(dir, 'old.txt'))).toBe(false);
expect(existsSync(dir)).toBe(true);
});
it('sync version works the same', () => {
const dir = join(root, 'sync-clean');
mkdirSync(dir, { recursive: true });
writeFileSync(join(dir, 'old.txt'), 'old');
ensureDirCleanSync(dir);
expect(existsSync(join(dir, 'old.txt'))).toBe(false);
expect(existsSync(dir)).toBe(true);
});
});
describe('copySourceTree', () => {
let root: string;
beforeAll(() => {
root = mkdtempSync(join(tmpdir(), 'helpers-copy-'));
});
afterAll(() => {
rmSync(root, { recursive: true, force: true });
});
it('copies files and subdirectories', async () => {
const src = join(root, 'src');
const dest = join(root, 'dest');
mkdirSync(join(src, 'sub'), { recursive: true });
writeFileSync(join(src, 'main.py'), 'print("hello")');
writeFileSync(join(src, 'sub', 'util.py'), 'pass');
mkdirSync(dest, { recursive: true });
await copySourceTree(src, dest);
expect(readFileSync(join(dest, 'main.py'), 'utf-8')).toBe('print("hello")');
expect(readFileSync(join(dest, 'sub', 'util.py'), 'utf-8')).toBe('pass');
});
it('excludes __pycache__, .git, node_modules, .venv', async () => {
const src = join(root, 'src-excl');
const dest = join(root, 'dest-excl');
mkdirSync(src, { recursive: true });
mkdirSync(join(src, '__pycache__'), { recursive: true });
mkdirSync(join(src, '.git'), { recursive: true });
mkdirSync(join(src, 'node_modules'), { recursive: true });
mkdirSync(join(src, '.venv'), { recursive: true });
writeFileSync(join(src, '__pycache__', 'cached.pyc'), 'cache');
writeFileSync(join(src, '.git', 'HEAD'), 'ref');
writeFileSync(join(src, 'keep.py'), 'keep');
mkdirSync(dest, { recursive: true });
await copySourceTree(src, dest);
expect(existsSync(join(dest, 'keep.py'))).toBe(true);
expect(existsSync(join(dest, '__pycache__'))).toBe(false);
expect(existsSync(join(dest, '.git'))).toBe(false);
expect(existsSync(join(dest, 'node_modules'))).toBe(false);
expect(existsSync(join(dest, '.venv'))).toBe(false);
});
it('throws for non-existent source', async () => {
await expect(copySourceTree(join(root, 'nope'), join(root, 'x'))).rejects.toThrow('not found');
});
it('sync version copies files', () => {
const src = join(root, 'src-sync');
const dest = join(root, 'dest-sync');
mkdirSync(src, { recursive: true });
writeFileSync(join(src, 'a.py'), 'a');
mkdirSync(dest, { recursive: true });
copySourceTreeSync(src, dest);
expect(readFileSync(join(dest, 'a.py'), 'utf-8')).toBe('a');
});
it('sync version throws for non-existent source', () => {
expect(() => copySourceTreeSync(join(root, 'missing'), join(root, 'y'))).toThrow('not found');
});
});
describe('createZipFromDir + enforceZipSizeLimit', () => {
let root: string;
beforeAll(() => {
root = mkdtempSync(join(tmpdir(), 'helpers-zip-'));
});
afterAll(() => {
rmSync(root, { recursive: true, force: true });
});
it('creates a valid zip file from a directory', async () => {
const src = join(root, 'zipme');
const zipPath = join(root, 'output.zip');
mkdirSync(join(src, 'sub'), { recursive: true });
writeFileSync(join(src, 'file.txt'), 'hello');
writeFileSync(join(src, 'sub', 'nested.txt'), 'nested');
await createZipFromDir(src, zipPath);
expect(existsSync(zipPath)).toBe(true);
// Zip should be non-empty
const size = await enforceZipSizeLimit(zipPath);
expect(size).toBeGreaterThan(0);
});
it('sync version creates a valid zip file', () => {
const src = join(root, 'zipme-sync');
const zipPath = join(root, 'output-sync.zip');
mkdirSync(src, { recursive: true });
writeFileSync(join(src, 'data.txt'), 'data');
createZipFromDirSync(src, zipPath);
expect(existsSync(zipPath)).toBe(true);
const size = enforceZipSizeLimitSync(zipPath);
expect(size).toBeGreaterThan(0);
});
it('excludes __pycache__ from zip', async () => {
const src = join(root, 'zip-excl');
const zipPath = join(root, 'excl.zip');
mkdirSync(join(src, '__pycache__'), { recursive: true });
writeFileSync(join(src, '__pycache__', 'cached.pyc'), 'x'.repeat(10000));
writeFileSync(join(src, 'main.py'), 'print("hi")');
await createZipFromDir(src, zipPath);
// Create another zip with just main.py for size comparison
const src2 = join(root, 'zip-just-main');
mkdirSync(src2, { recursive: true });
writeFileSync(join(src2, 'main.py'), 'print("hi")');
const zipPath2 = join(root, 'just-main.zip');
await createZipFromDir(src2, zipPath2);
const sizeWithExcl = await enforceZipSizeLimit(zipPath);
const sizeJustMain = await enforceZipSizeLimit(zipPath2);
// Sizes should be identical since __pycache__ was excluded
expect(sizeWithExcl).toBe(sizeJustMain);
});
});
describe('resolveProjectPaths', () => {
let root: string;
beforeAll(() => {
root = mkdtempSync(join(tmpdir(), 'helpers-resolve-'));
// Create a minimal project structure with pyproject.toml (source files live alongside it)
writeFileSync(join(root, 'pyproject.toml'), '[project]\nname = "test"');
writeFileSync(join(root, 'main.py'), 'print("hello")');
});
afterAll(() => {
rmSync(root, { recursive: true, force: true });
});
it('resolves paths with explicit projectRoot', async () => {
const paths = await resolveProjectPaths({ projectRoot: root });
expect(paths.projectRoot).toBe(root);
expect(paths.pyprojectPath).toBe(join(root, 'pyproject.toml'));
expect(paths.srcDir).toBe(root);
});
it('uses agent name for build directory', async () => {
const paths = await resolveProjectPaths({ projectRoot: root }, 'MyAgent');
expect(paths.buildDir).toContain('MyAgent');
expect(paths.stagingDir).toContain('MyAgent');
});
it('defaults agent name to "default"', async () => {
const paths = await resolveProjectPaths({ projectRoot: root });
expect(paths.buildDir).toContain('default');
});
it('throws when pyproject.toml not found', async () => {
// Use a completely separate tmpdir so findUp doesn't discover the parent's pyproject.toml
const isolated = mkdtempSync(join(tmpdir(), 'helpers-no-pyproject-'));
try {
await expect(resolveProjectPaths({ projectRoot: isolated })).rejects.toThrow();
} finally {
rmSync(isolated, { recursive: true, force: true });
}
});
it('sync version resolves paths', () => {
const paths = resolveProjectPathsSync({ projectRoot: root }, 'SyncAgent');
expect(paths.projectRoot).toBe(root);
expect(paths.buildDir).toContain('SyncAgent');
});
});
describe('convertWindowsScriptsToLinux (shebang rewriting on non-Windows)', () => {
let root: string;
beforeAll(() => {
root = mkdtempSync(join(tmpdir(), 'helpers-shebang-'));
});
afterAll(() => {
rmSync(root, { recursive: true, force: true });
});
it('rewrites hardcoded macOS shebang to portable shebang', async () => {
const staging = join(root, 'staging1');
const binDir = join(staging, 'bin');
mkdirSync(binDir, { recursive: true });
writeFileSync(join(binDir, 'myscript'), '#!/Users/dev/.venv/bin/python3\nimport sys\nprint(sys.argv)');
await convertWindowsScriptsToLinux(staging);
const content = readFileSync(join(binDir, 'myscript'), 'utf-8');
expect(content).toMatch(/^#!\/usr\/bin\/env python3/);
expect(content).not.toContain('/Users/');
});
it('rewrites hardcoded Linux home path shebang', async () => {
const staging = join(root, 'staging2');
const binDir = join(staging, 'bin');
mkdirSync(binDir, { recursive: true });
writeFileSync(join(binDir, 'tool'), '#!/home/user/.local/bin/python3\nimport os');
await convertWindowsScriptsToLinux(staging);
const content = readFileSync(join(binDir, 'tool'), 'utf-8');
expect(content).toMatch(/^#!\/usr\/bin\/env python3/);
});
it('leaves portable shebang unchanged', async () => {
const staging = join(root, 'staging3');
const binDir = join(staging, 'bin');
mkdirSync(binDir, { recursive: true });
const original = '#!/usr/bin/env python3\nimport sys';
writeFileSync(join(binDir, 'good'), original);
await convertWindowsScriptsToLinux(staging);
const content = readFileSync(join(binDir, 'good'), 'utf-8');
expect(content).toBe(original);
});
it('handles missing bin directory gracefully', async () => {
const staging = join(root, 'no-bin');
mkdirSync(staging, { recursive: true });
// Should not throw
await convertWindowsScriptsToLinux(staging);
});
it('sync version rewrites shebangs', () => {
const staging = join(root, 'staging-sync');
const binDir = join(staging, 'bin');
mkdirSync(binDir, { recursive: true });
writeFileSync(join(binDir, 'script'), '#!/Users/ci/.pyenv/versions/3.12/bin/python3\nimport sys');
convertWindowsScriptsToLinuxSync(staging);
const content = readFileSync(join(binDir, 'script'), 'utf-8');
expect(content).toMatch(/^#!\/usr\/bin\/env python3/);
});
it('async version skips subdirectories in bin (only processes files)', async () => {
const staging = join(root, 'staging-subdir-async');
const binDir = join(staging, 'bin');
mkdirSync(join(binDir, 'subdir'), { recursive: true });
writeFileSync(join(binDir, 'myscript'), '#!/Users/dev/.venv/bin/python3\nimport os');
await convertWindowsScriptsToLinux(staging);
const content = readFileSync(join(binDir, 'myscript'), 'utf-8');
expect(content).toMatch(/^#!\/usr\/bin\/env python3/);
expect(existsSync(join(binDir, 'subdir'))).toBe(true);
});
it('sync version skips subdirectories in bin (only processes files)', () => {
const staging = join(root, 'staging-subdir-sync');
const binDir = join(staging, 'bin');
mkdirSync(join(binDir, 'subdir'), { recursive: true });
writeFileSync(join(binDir, 'myscript'), '#!/Users/dev/.venv/bin/python3\nimport os');
convertWindowsScriptsToLinuxSync(staging);
const content = readFileSync(join(binDir, 'myscript'), 'utf-8');
expect(content).toMatch(/^#!\/usr\/bin\/env python3/);
expect(existsSync(join(binDir, 'subdir'))).toBe(true);
});
it('sync version handles missing bin directory gracefully', () => {
const staging = join(root, 'staging-no-bin-sync');
mkdirSync(staging, { recursive: true });
convertWindowsScriptsToLinuxSync(staging);
expect(existsSync(join(staging, 'bin'))).toBe(false);
});
});