|
| 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 Setup', () => { |
| 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('Setup operations', () => { |
| 53 | + it('should get setup status', async () => { |
| 54 | + if (!client || !sandbox) throw new Error('Client or sandbox not initialized'); |
| 55 | + |
| 56 | + const status = client.setup.status; |
| 57 | + expect(status).toBeDefined(); |
| 58 | + expect(['RUNNING', 'FINISHED', 'STOPPED', 'IDLE']).toContain(status); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should get setup steps', async () => { |
| 62 | + if (!client || !sandbox) throw new Error('Client or sandbox not initialized'); |
| 63 | + |
| 64 | + const steps = client.setup.getSteps(); |
| 65 | + expect(Array.isArray(steps)).toBe(true); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should get current step index', async () => { |
| 69 | + if (!client || !sandbox) throw new Error('Client or sandbox not initialized'); |
| 70 | + |
| 71 | + const currentStepIndex = client.setup.currentStepIndex; |
| 72 | + expect(typeof currentStepIndex).toBe('number'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should wait until setup completes', async () => { |
| 76 | + if (!client || !sandbox) throw new Error('Client or sandbox not initialized'); |
| 77 | + |
| 78 | + // If setup is already finished, this should resolve immediately |
| 79 | + await client.setup.waitUntilComplete(); |
| 80 | + |
| 81 | + const status = client.setup.status; |
| 82 | + expect(status).toBe('FINISHED'); |
| 83 | + }, 60000); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('Setup steps', () => { |
| 87 | + it('should have step properties', async () => { |
| 88 | + if (!client || !sandbox) throw new Error('Client or sandbox not initialized'); |
| 89 | + |
| 90 | + const steps = client.setup.getSteps(); |
| 91 | + |
| 92 | + if (steps.length > 0) { |
| 93 | + const firstStep = steps[0]; |
| 94 | + expect(firstStep.name).toBeDefined(); |
| 95 | + expect(firstStep.command).toBeDefined(); |
| 96 | + expect(firstStep.status).toBeDefined(); |
| 97 | + } |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments