|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest' |
| 2 | +import fs from 'fs' |
| 3 | +import path from 'path' |
| 4 | +import { NotASandboxLocalEnvironment } from '../not-a-sandbox-local-environment.js' |
| 5 | + |
| 6 | +const TEST_DIR = '/tmp/strands-test-not-a-sandbox' |
| 7 | +// Written relative (resolved against process.cwd()) to exercise _resolvePath; cleaned up below. |
| 8 | +const REL_NAME = 'strands-not-a-sandbox-rel-probe.txt' |
| 9 | +const REL_ABS = path.join(process.cwd(), REL_NAME) |
| 10 | + |
| 11 | +describe.skipIf(process.platform === 'win32')('NotASandboxLocalEnvironment', () => { |
| 12 | + let sandbox: NotASandboxLocalEnvironment |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + fs.rmSync(TEST_DIR, { recursive: true, force: true }) |
| 16 | + fs.mkdirSync(TEST_DIR, { recursive: true }) |
| 17 | + sandbox = new NotASandboxLocalEnvironment() |
| 18 | + }) |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + fs.rmSync(TEST_DIR, { recursive: true, force: true }) |
| 22 | + fs.rmSync(REL_ABS, { force: true }) |
| 23 | + }) |
| 24 | + |
| 25 | + describe('execute', () => { |
| 26 | + it('runs a command on the host', async () => { |
| 27 | + const result = await sandbox.execute('echo hello') |
| 28 | + expect(result.exitCode).toBe(0) |
| 29 | + expect(result.stdout).toBe('hello\n') |
| 30 | + }) |
| 31 | + |
| 32 | + it('respects the cwd option', async () => { |
| 33 | + const result = await sandbox.execute('pwd', { cwd: TEST_DIR }) |
| 34 | + expect(result.stdout.trim()).toBe(TEST_DIR) |
| 35 | + }) |
| 36 | + |
| 37 | + it('reports a non-zero exit code and stderr from a failing command', async () => { |
| 38 | + const result = await sandbox.execute('echo oops >&2; exit 3') |
| 39 | + expect(result.exitCode).toBe(3) |
| 40 | + expect(result.stderr).toBe('oops\n') |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + describe('executeCode', () => { |
| 45 | + it('runs code through the named interpreter', async () => { |
| 46 | + const result = await sandbox.executeCode('print(2 + 2)', 'python3', { cwd: TEST_DIR }) |
| 47 | + expect(result.exitCode).toBe(0) |
| 48 | + expect(result.stdout).toBe('4\n') |
| 49 | + }) |
| 50 | + |
| 51 | + it('rejects a language with invalid characters', async () => { |
| 52 | + await expect(sandbox.executeCode('x', '../../bin/sh')).rejects.toThrow('invalid characters') |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + describe('read/write (native fs)', () => { |
| 57 | + it('text file roundtrip via absolute path', async () => { |
| 58 | + const file = path.join(TEST_DIR, 'note.txt') |
| 59 | + await sandbox.writeText(file, 'hello host') |
| 60 | + expect(await sandbox.readText(file)).toBe('hello host') |
| 61 | + }) |
| 62 | + |
| 63 | + it('binary roundtrip preserves all byte values', async () => { |
| 64 | + const file = path.join(TEST_DIR, 'all-bytes.bin') |
| 65 | + const bytes = new Uint8Array(256) |
| 66 | + for (let i = 0; i < 256; i++) bytes[i] = i |
| 67 | + await sandbox.writeFile(file, bytes) |
| 68 | + expect(Array.from(await sandbox.readFile(file))).toStrictEqual(Array.from(bytes)) |
| 69 | + }) |
| 70 | + |
| 71 | + it('creates missing parent directories on write', async () => { |
| 72 | + const file = path.join(TEST_DIR, 'deep/nested/file.txt') |
| 73 | + await sandbox.writeText(file, 'deep') |
| 74 | + expect(await sandbox.readText(file)).toBe('deep') |
| 75 | + }) |
| 76 | + |
| 77 | + it('throws when reading a nonexistent file', async () => { |
| 78 | + await expect(sandbox.readFile(path.join(TEST_DIR, 'nope.txt'))).rejects.toThrow() |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + describe('removeFile', () => { |
| 83 | + it('removes a file', async () => { |
| 84 | + const file = path.join(TEST_DIR, 'delete-me.txt') |
| 85 | + await sandbox.writeText(file, 'bye') |
| 86 | + await sandbox.removeFile(file) |
| 87 | + await expect(sandbox.readFile(file)).rejects.toThrow() |
| 88 | + }) |
| 89 | + |
| 90 | + it('throws on a nonexistent file', async () => { |
| 91 | + await expect(sandbox.removeFile(path.join(TEST_DIR, 'nope.txt'))).rejects.toThrow() |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + describe('listFiles', () => { |
| 96 | + it('returns entries sorted by name with isDir and size metadata', async () => { |
| 97 | + await sandbox.writeText(path.join(TEST_DIR, 'c.txt'), 'cc') |
| 98 | + await sandbox.writeText(path.join(TEST_DIR, 'a.txt'), 'a') |
| 99 | + await sandbox.writeText(path.join(TEST_DIR, 'b.txt'), 'bbb') |
| 100 | + fs.mkdirSync(path.join(TEST_DIR, 'sub')) |
| 101 | + |
| 102 | + const files = await sandbox.listFiles(TEST_DIR) |
| 103 | + expect(files.map((f) => f.name)).toStrictEqual(['a.txt', 'b.txt', 'c.txt', 'sub']) |
| 104 | + |
| 105 | + const a = files.find((f) => f.name === 'a.txt') |
| 106 | + expect(a?.isDir).toBe(false) |
| 107 | + expect(a?.size).toBe(1) |
| 108 | + expect(files.find((f) => f.name === 'sub')?.isDir).toBe(true) |
| 109 | + }) |
| 110 | + |
| 111 | + it('throws on a nonexistent directory', async () => { |
| 112 | + await expect(sandbox.listFiles(path.join(TEST_DIR, 'no-such-dir'))).rejects.toThrow() |
| 113 | + }) |
| 114 | + }) |
| 115 | + |
| 116 | + describe('_resolvePath (relative vs absolute)', () => { |
| 117 | + it('writes absolute paths as-is', async () => { |
| 118 | + const file = path.join(TEST_DIR, 'abs.txt') |
| 119 | + await sandbox.writeText(file, 'absolute') |
| 120 | + expect(fs.readFileSync(file, 'utf8')).toBe('absolute') |
| 121 | + }) |
| 122 | + |
| 123 | + it('resolves relative paths against process.cwd()', async () => { |
| 124 | + await sandbox.writeText(REL_NAME, 'relative') |
| 125 | + expect(fs.readFileSync(REL_ABS, 'utf8')).toBe('relative') |
| 126 | + }) |
| 127 | + }) |
| 128 | +}) |
0 commit comments