Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Add pty tests in js

Jakub Novakb334c0e261e7
diff --git a/packages/js-sdk/tests/sandbox/pty/ptyCreate.test.ts b/packages/js-sdk/tests/sandbox/pty/ptyCreate.test.ts
new file mode 100644
index 000000000..a64d7f891
--- /dev/null
+++ b/packages/js-sdk/tests/sandbox/pty/ptyCreate.test.ts
@@ -0,0 +1,30 @@
+import { sandboxTest } from '../../setup'
+import { assert } from 'vitest'
+
+sandboxTest('should create a PTY and execute commands', async ({ sandbox }) => {
+ let output = ''
+ const decoder = new TextDecoder()
+ const appendData = (data: Uint8Array) => {
+ output += decoder.decode(data)
+ }
+
+ const terminal = await sandbox.pty.create({
+ cols: 80,
+ rows: 24,
+ onData: appendData,
+ envs: { ABC: '123' },
+ })
+
+ await sandbox.pty.sendInput(
+ terminal.pid,
+ new Uint8Array(Buffer.from('echo $ABC\n'))
+ )
+ await sandbox.pty.sendInput(
+ terminal.pid,
+ new Uint8Array(Buffer.from('exit\n'))
+ )
+ await terminal.wait()
+ assert.equal(terminal.exitCode, 0)
+
+ assert.include(output, '123')
+})
diff --git a/packages/js-sdk/tests/sandbox/pty/resize.test.ts b/packages/js-sdk/tests/sandbox/pty/resize.test.ts
new file mode 100644
index 000000000..c48ad8b3b
--- /dev/null
+++ b/packages/js-sdk/tests/sandbox/pty/resize.test.ts
@@ -0,0 +1,50 @@
+import { sandboxTest } from '../../setup'
+import { assert } from 'vitest'
+
+sandboxTest('resize', async ({ sandbox }) => {
+ let output = ''
+ const decoder = new TextDecoder()
+ const appendData = (data: Uint8Array) => {
+ output += decoder.decode(data)
+ }
+
+ const terminal = await sandbox.pty.create({
+ cols: 80,
+ rows: 24,
+ onData: appendData,
+ })
+
+ await sandbox.pty.sendInput(
+ terminal.pid,
+ new Uint8Array(Buffer.from('tput cols\n'))
+ )
+ await sandbox.pty.sendInput(
+ terminal.pid,
+ new Uint8Array(Buffer.from('exit\n'))
+ )
+
+ await terminal.wait()
+ assert.equal(terminal.exitCode, 0)
+ assert.include(output, '80')
+
+ output = ''
+
+ const resizedTerminal = await sandbox.pty.create({
+ cols: 80,
+ rows: 24,
+ onData: appendData,
+ })
+ await sandbox.pty.resize(resizedTerminal.pid, { cols: 100, rows: 24 })
+ await sandbox.pty.sendInput(
+ resizedTerminal.pid,
+ new Uint8Array(Buffer.from('tput cols\n'))
+ )
+ await sandbox.pty.sendInput(
+ resizedTerminal.pid,
+ new Uint8Array(Buffer.from('exit\n'))
+ )
+
+ await resizedTerminal.wait()
+ assert.equal(resizedTerminal.exitCode, 0)
+ assert.include(output, '100')
+})
diff --git a/packages/js-sdk/tests/sandbox/pty/sendInput.test.ts b/packages/js-sdk/tests/sandbox/pty/sendInput.test.ts
new file mode 100644
index 000000000..b32d7f23d
--- /dev/null
+++ b/packages/js-sdk/tests/sandbox/pty/sendInput.test.ts
@@ -0,0 +1,18 @@
+import { expect } from 'vitest'
+import { sandboxTest } from '../../setup'
+
+sandboxTest('send input', async ({ sandbox }) => {
+ const terminal = await sandbox.pty.create({
+ cols: 80,
+ rows: 24,
+ onData: () => null,
+ })
+
+ await sandbox.pty.sendInput(
+ terminal.pid,
+ new Uint8Array(Buffer.from('exit\n'))
+ )
+
+ await terminal.wait()
+ expect(terminal.exitCode).toBe(0)
+})