Skip to content

Commit a92cf05

Browse files
committed
fix tests
1 parent 77d2e1b commit a92cf05

8 files changed

+452
-0
lines changed

tests/e2e/sandbox-commands.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('Sandbox Commands', () => {
2626

2727
try {
2828
if (client) {
29+
await client.disconnect();
2930
client.dispose();
3031
client = undefined;
3132
}

tests/e2e/sandbox-filesystem.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe('Sandbox Filesystem', () => {
2727

2828
try {
2929
if (client) {
30+
await client.disconnect();
3031
client.dispose();
3132
client = undefined;
3233
}

tests/e2e/sandbox-hosts.test.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 Hosts', () => {
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('Host URL generation', () => {
53+
it('should generate URL for a port', async () => {
54+
if (!client || !sandbox) throw new Error('Client or sandbox not initialized');
55+
56+
const url = client.hosts.getUrl(3000);
57+
expect(url).toBeTruthy();
58+
expect(url).toContain('csb.app');
59+
expect(url).toContain('3000');
60+
expect(url).toContain(sandbox.id);
61+
});
62+
63+
it('should generate URL with custom protocol', async () => {
64+
if (!client || !sandbox) throw new Error('Client or sandbox not initialized');
65+
66+
const url = client.hosts.getUrl(8080, 'http');
67+
expect(url).toBeTruthy();
68+
expect(url.startsWith('http://')).toBe(true);
69+
expect(url).toContain('8080');
70+
});
71+
72+
it('should generate URL with https by default', async () => {
73+
if (!client || !sandbox) throw new Error('Client or sandbox not initialized');
74+
75+
const url = client.hosts.getUrl(4000);
76+
expect(url.startsWith('https://')).toBe(true);
77+
});
78+
});
79+
80+
describe('Host headers and cookies', () => {
81+
it('should get headers', async () => {
82+
if (!client || !sandbox) throw new Error('Client or sandbox not initialized');
83+
84+
const headers = client.hosts.getHeaders();
85+
expect(headers).toBeDefined();
86+
expect(typeof headers).toBe('object');
87+
});
88+
89+
it('should get cookies', async () => {
90+
if (!client || !sandbox) throw new Error('Client or sandbox not initialized');
91+
92+
const cookies = client.hosts.getCookies();
93+
expect(cookies).toBeDefined();
94+
expect(typeof cookies).toBe('object');
95+
});
96+
});
97+
});
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 Interpreters', () => {
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('JavaScript interpreter', () => {
53+
it('should execute simple JavaScript code', async () => {
54+
if (!client || !sandbox) throw new Error('Client or sandbox not initialized');
55+
56+
const result = await client.interpreters.javascript('2 + 2');
57+
expect(result).toContain('4');
58+
});
59+
60+
it('should execute JavaScript with variables', async () => {
61+
if (!client || !sandbox) throw new Error('Client or sandbox not initialized');
62+
63+
const result = await client.interpreters.javascript(`
64+
const x = 10;
65+
const y = 20;
66+
console.log(x + y);
67+
`);
68+
expect(result).toContain('30');
69+
});
70+
71+
it('should execute JavaScript with return statement', async () => {
72+
if (!client || !sandbox) throw new Error('Client or sandbox not initialized');
73+
74+
const result = await client.interpreters.javascript(`
75+
const greeting = 'Hello from JavaScript';
76+
console.log(greeting);
77+
`);
78+
expect(result).toContain('Hello from JavaScript');
79+
});
80+
});
81+
82+
describe('Python interpreter', () => {
83+
it('should execute simple Python code', async () => {
84+
if (!client || !sandbox) throw new Error('Client or sandbox not initialized');
85+
86+
const result = await client.interpreters.python('2 + 2');
87+
expect(result).toContain('4');
88+
});
89+
90+
it('should execute Python with variables', async () => {
91+
if (!client || !sandbox) throw new Error('Client or sandbox not initialized');
92+
93+
const result = await client.interpreters.python(`
94+
x = 10
95+
y = 20
96+
print(x + y)`);
97+
expect(result).toContain('30');
98+
});
99+
100+
it('should execute Python with print statement', async () => {
101+
if (!client || !sandbox) throw new Error('Client or sandbox not initialized');
102+
103+
const result = await client.interpreters.python(`
104+
message = 'Hello from Python'
105+
print(message)
106+
`);
107+
expect(result).toContain('Hello from Python');
108+
});
109+
});
110+
});

tests/e2e/sandbox-ports.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('Sandbox Ports', () => {
2626

2727
try {
2828
if (client) {
29+
await client.disconnect();
2930
client.dispose();
3031
client = undefined;
3132
}

tests/e2e/sandbox-setup.test.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)