|
| 1 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 2 | +import { CodeSandbox } from '../../src/index.js'; |
| 3 | +import { Sandbox } from '../../src/Sandbox.js'; |
| 4 | +import { SandboxClient } from '../../src/SandboxClient/index.js'; |
| 5 | +import { initializeSDK, TEST_TEMPLATE_ID } from './helpers.js'; |
| 6 | + |
| 7 | +describe('Sandbox Commands', () => { |
| 8 | + let sdk: CodeSandbox; |
| 9 | + let sandbox: Sandbox | undefined; |
| 10 | + let client: SandboxClient | undefined; |
| 11 | + |
| 12 | + beforeAll(async () => { |
| 13 | + sdk = initializeSDK(); |
| 14 | + |
| 15 | + // Create a sandbox for testing |
| 16 | + sandbox = await sdk.sandboxes.create({ |
| 17 | + id: TEST_TEMPLATE_ID, |
| 18 | + }); |
| 19 | + |
| 20 | + // Connect to sandbox |
| 21 | + client = await sandbox.connect(); |
| 22 | + }, 60000); |
| 23 | + |
| 24 | + afterAll(async () => { |
| 25 | + const sandboxId = sandbox?.id; |
| 26 | + |
| 27 | + try { |
| 28 | + if (client) { |
| 29 | + await client.disconnect(); |
| 30 | + client.dispose(); |
| 31 | + client = undefined; |
| 32 | + } |
| 33 | + } catch (error) { |
| 34 | + console.error('Failed to dispose client:', error); |
| 35 | + } |
| 36 | + |
| 37 | + if (sandboxId) { |
| 38 | + try { |
| 39 | + await sdk.sandboxes.shutdown(sandboxId); |
| 40 | + await sdk.sandboxes.delete(sandboxId); |
| 41 | + } catch (error) { |
| 42 | + console.error('Failed to cleanup test sandbox:', sandboxId, error); |
| 43 | + try { |
| 44 | + await sdk.sandboxes.delete(sandboxId); |
| 45 | + } catch (deleteError) { |
| 46 | + console.error('Failed to force delete sandbox:', sandboxId, deleteError); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + describe('Command execution', () => { |
| 53 | + it('should run a simple command and get output', async () => { |
| 54 | + if (!client || !sandbox) throw new Error('Client or sandbox not initialized'); |
| 55 | + |
| 56 | + const output = await client.commands.run('echo "Hello from sandbox"'); |
| 57 | + expect(output).toContain('Hello from sandbox'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should get output from pwd command', async () => { |
| 61 | + if (!client || !sandbox) throw new Error('Client or sandbox not initialized'); |
| 62 | + |
| 63 | + const output = await client.commands.run('pwd'); |
| 64 | + expect(output).toBeTruthy(); |
| 65 | + expect(output.trim()).toMatch(/^\//); // Should start with / |
| 66 | + }); |
| 67 | + |
| 68 | + it('should run multiple commands sequentially', async () => { |
| 69 | + if (!client || !sandbox) throw new Error('Client or sandbox not initialized'); |
| 70 | + |
| 71 | + const output1 = await client.commands.run('echo "first"'); |
| 72 | + const output2 = await client.commands.run('echo "second"'); |
| 73 | + const output3 = await client.commands.run('echo "third"'); |
| 74 | + |
| 75 | + expect(output1).toContain('first'); |
| 76 | + expect(output2).toContain('second'); |
| 77 | + expect(output3).toContain('third'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should run multiple commands with array syntax', async () => { |
| 81 | + if (!client || !sandbox) throw new Error('Client or sandbox not initialized'); |
| 82 | + |
| 83 | + // Array of commands should be joined with && |
| 84 | + const output = await client.commands.run([ |
| 85 | + 'echo "first"', |
| 86 | + 'echo "second"', |
| 87 | + 'echo "third"', |
| 88 | + ]); |
| 89 | + |
| 90 | + expect(output).toContain('first'); |
| 91 | + expect(output).toContain('second'); |
| 92 | + expect(output).toContain('third'); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('Background commands', () => { |
| 97 | + it('should run command in background', async () => { |
| 98 | + if (!client || !sandbox) throw new Error('Client or sandbox not initialized'); |
| 99 | + |
| 100 | + const command = await client.commands.runBackground('sleep 1 && echo "done"'); |
| 101 | + expect(command).toBeDefined(); |
| 102 | + expect(command.status).toBe('RUNNING'); |
| 103 | + |
| 104 | + // Wait for completion |
| 105 | + const output = await command.waitUntilComplete(); |
| 106 | + expect(output).toContain('done'); |
| 107 | + }, 10000); |
| 108 | + |
| 109 | + it('should run multiple commands in background with array syntax', async () => { |
| 110 | + if (!client || !sandbox) throw new Error('Client or sandbox not initialized'); |
| 111 | + |
| 112 | + // Array of commands should be joined with && |
| 113 | + const command = await client.commands.runBackground([ |
| 114 | + 'echo "first"', |
| 115 | + 'echo "second"', |
| 116 | + 'echo "third"', |
| 117 | + ]); |
| 118 | + expect(command).toBeDefined(); |
| 119 | + expect(command.status).toBe('RUNNING'); |
| 120 | + |
| 121 | + // Wait for completion |
| 122 | + const output = await command.waitUntilComplete(); |
| 123 | + expect(output).toContain('first'); |
| 124 | + expect(output).toContain('second'); |
| 125 | + expect(output).toContain('third'); |
| 126 | + }, 10000); |
| 127 | + |
| 128 | + it('should be able to kill background command', async () => { |
| 129 | + if (!client || !sandbox) throw new Error('Client or sandbox not initialized'); |
| 130 | + |
| 131 | + const command = await client.commands.runBackground('sleep 30'); |
| 132 | + expect(command).toBeDefined(); |
| 133 | + |
| 134 | + await command.kill(); |
| 135 | + |
| 136 | + // Command should be killed |
| 137 | + expect(command).toBeDefined(); |
| 138 | + }, 10000); |
| 139 | + }); |
| 140 | + |
| 141 | + describe('Command listing', () => { |
| 142 | + it('should get all commands', async () => { |
| 143 | + if (!client || !sandbox) throw new Error('Client or sandbox not initialized'); |
| 144 | + |
| 145 | + const commands = await client.commands.getAll(); |
| 146 | + expect(Array.isArray(commands)).toBe(true); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe('Working directory', () => { |
| 151 | + it('should run command in specified directory', async () => { |
| 152 | + if (!client || !sandbox) throw new Error('Client or sandbox not initialized'); |
| 153 | + |
| 154 | + // Create a test directory |
| 155 | + await client.fs.mkdir('/test-cwd'); |
| 156 | + |
| 157 | + const output = await client.commands.run('pwd', { cwd: '/test-cwd' }); |
| 158 | + expect(output).toContain('/test-cwd'); |
| 159 | + |
| 160 | + // Cleanup |
| 161 | + await client.fs.remove('/test-cwd'); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('Environment variables', () => { |
| 166 | + it('should run command with custom environment variables', async () => { |
| 167 | + if (!client || !sandbox) throw new Error('Client or sandbox not initialized'); |
| 168 | + |
| 169 | + const output = await client.commands.run('echo $TEST_VAR', { |
| 170 | + env: { TEST_VAR: 'custom_value' }, |
| 171 | + }); |
| 172 | + expect(output).toContain('custom_value'); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments