Skip to content

Commit 8bfd3c0

Browse files
0divjakubno
andauthored
Test new error handling indicating when the sandbox is not found (#540)
Added a test in `js-sdk` for e2b-dev/infra#231 --------- Co-authored-by: Jakub Novák <jakub@e2b.dev>
1 parent 3cfed0e commit 8bfd3c0

3 files changed

Lines changed: 63 additions & 23 deletions

File tree

packages/js-sdk/tests/sandbox/commands/sendStdin.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ sandboxTest('send stdin to process', async ({ sandbox }) => {
77

88
await sandbox.commands.sendStdin(cmd.pid, text)
99

10-
1110
for (let i = 0; i < 5; i++) {
1211
if (cmd.stdout === text) {
1312
break
@@ -64,6 +63,5 @@ sandboxTest('send multiline string to stdin', async ({ sandbox }) => {
6463

6564
await cmd.kill()
6665

67-
6866
assert.equal(cmd.stdout, text)
6967
})

packages/js-sdk/tests/sandbox/connect.test.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { test, assert } from 'vitest'
1+
import { assert, test } from 'vitest'
22

33
import { Sandbox } from '../../src'
4-
import { isDebug, template } from '../setup.js'
4+
import { isDebug, sandboxTest, template } from '../setup.js'
55

66
test('connect', async () => {
77
const sbx = await Sandbox.create(template, { timeoutMs: 10_000 })
@@ -19,3 +19,16 @@ test('connect', async () => {
1919
}
2020
}
2121
})
22+
23+
sandboxTest.skipIf(isDebug)(
24+
'connect to non-running sandbox',
25+
async ({ sandbox }) => {
26+
const isRunning = await sandbox.isRunning()
27+
assert.isTrue(isRunning)
28+
await sandbox.kill()
29+
30+
const sbxConnection = await Sandbox.connect(sandbox.sandboxId)
31+
const isRunning2 = await sbxConnection.isRunning()
32+
assert.isFalse(isRunning2)
33+
}
34+
)

packages/js-sdk/tests/sandbox/host.test.ts

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,59 @@ import { assert } from 'vitest'
22

33
import { isDebug, sandboxTest, wait } from '../setup.js'
44

5-
sandboxTest('ping server in sandbox', async ({ sandbox }) => {
6-
const cmd = await sandbox.commands.run('python -m http.server 8000', { background: true })
5+
sandboxTest(
6+
'ping server in running sandbox',
7+
async ({ sandbox }) => {
8+
const cmd = await sandbox.commands.run('python -m http.server 8000', {
9+
background: true,
10+
})
711

8-
try {
9-
await wait(1000)
12+
try {
13+
await wait(1000)
1014

11-
const host = sandbox.getHost(8000)
15+
const host = sandbox.getHost(8000)
1216

13-
let res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`)
17+
let res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`)
1418

15-
for (let i = 0; i < 20; i++) {
16-
if (res.status === 200) {
17-
break
18-
}
19+
for (let i = 0; i < 20; i++) {
20+
if (res.status === 200) {
21+
break
22+
}
1923

20-
res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`)
21-
await wait(500)
24+
res = await fetch(`${isDebug ? 'http' : 'https'}://${host}`)
25+
await wait(500)
26+
}
27+
assert.equal(res.status, 200)
28+
} finally {
29+
try {
30+
await cmd.kill()
31+
} catch (e) {
32+
console.error(e)
33+
}
2234
}
23-
assert.equal(res.status, 200)
24-
} finally {
25-
try {
26-
await cmd.kill()
27-
} catch (e) {
28-
console.error(e)
35+
},
36+
60_000
37+
)
38+
39+
sandboxTest.skipIf(isDebug)(
40+
'ping server in non-running sandbox',
41+
async ({ sandbox }) => {
42+
const host = sandbox.getHost(3000)
43+
const url = `https://${host}`
44+
45+
await sandbox.kill()
46+
47+
const res = await fetch(url)
48+
assert.equal(res.status, 502)
49+
50+
const text = await res.text()
51+
const json = JSON.parse(text) as {
52+
message: string
53+
sandboxId: string
54+
code: number
2955
}
56+
assert.equal(json.message, 'Sandbox not found')
57+
assert.isTrue(sandbox.sandboxId.startsWith(json.sandboxId))
58+
assert.equal(json.code, 0)
3059
}
31-
}, 60_000 )
60+
)

0 commit comments

Comments
 (0)