Skip to content

armgit / E2B

Public Imported from GitHub
0

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

chore(js-sdk): JS SDK tests (#90)

Pavel 'Strajk' Doleceka9248f291876
diff --git a/packages/js-sdk/package.json b/packages/js-sdk/package.json
index 7d223385c..a8bedb038 100644
--- a/packages/js-sdk/package.json
+++ b/packages/js-sdk/package.json
@@ -27,6 +27,8 @@
"watch": "rollup -c -w",
"measure": "node test/performance.mjs",
"run": "node test/run.mjs",
+ "test": "vitest",
+ "test:coverage": "vitest run --coverage",
"fix": "npx eslint **/src/**/*.{ts,js} --fix",
"format": "prettier --check --ignore-path .gitignore .",
"format:fix": "prettier --write --ignore-path .gitignore .",
@@ -50,7 +52,8 @@
"rollup-plugin-polyfill-node": "^0.10.2",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.34.0",
- "typescript": "^4.9.4"
+ "typescript": "^4.9.4",
+ "vitest": "^0.34.2"
},
"files": [
"dist",
@@ -78,4 +81,4 @@
"openapi-typescript-fetch": "^1.1.3",
"rpc-websocket-client": "^1.1.4"
}
-}
\ No newline at end of file
+}
diff --git a/packages/js-sdk/src/session/sessionConnection.ts b/packages/js-sdk/src/session/sessionConnection.ts
index 192eb70a1..5b22d7127 100644
--- a/packages/js-sdk/src/session/sessionConnection.ts
+++ b/packages/js-sdk/src/session/sessionConnection.ts
@@ -265,6 +265,7 @@ export class SessionConnection {
throw new Error(formatSettledErrors(results))
}
+ // eslint-disable-next-line @typescript-eslint/member-ordering
async unsubscribe(subID: string) {
const subscription = this.subscribers.find(s => s.subID === subID)
if (!subscription) return
@@ -275,7 +276,7 @@ export class SessionConnection {
this.logger.log(`Unsubscribed '${subID}' from '${subscription.service}'`)
}
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/member-ordering
async subscribe(
service: Service,
handler: SubscriptionHandler,
diff --git a/packages/js-sdk/test/filesystem.test.mjs b/packages/js-sdk/test/filesystem.test.mjs
new file mode 100644
index 000000000..c0d2de05a
--- /dev/null
+++ b/packages/js-sdk/test/filesystem.test.mjs
@@ -0,0 +1,74 @@
+import { Session } from '../src'
+import { expect, test } from 'vitest'
+
+test('list files', async () => {
+ const session = await Session.create({ id: "Nodejs" })
+ await session.filesystem.makeDir("/test/new")
+
+ const ls = await session.filesystem.list("/test")
+ expect(ls.map(x => x.name)).toEqual(["new"])
+
+ await session.close()
+})
+
+test('create file', async () => {
+ const session = await Session.create({id: "Nodejs" })
+ await session.filesystem.makeDir("/test")
+ await session.filesystem.write("/test/test.txt", "Hello World!")
+
+ const ls = await session.filesystem.list("/test")
+ expect(ls.map(x => x.name)).toEqual(["test.txt"])
+
+ await session.close()
+})
+
+test('read and write', async () => {
+ const session = await Session.create({id: "Nodejs" })
+
+ await session.filesystem.write("/tmp/test.txt", "Hello World!")
+
+ const content = await session.filesystem.read("/tmp/test.txt")
+ expect(content).toEqual("Hello World!")
+
+
+ await session.close()
+})
+
+test('list delete files', async () => {
+ const session = await Session.create({id: "Nodejs" })
+ await session.filesystem.makeDir("/test/new")
+
+ let ls = await session.filesystem.list("/test")
+ expect(ls.map(x => x.name)).toEqual(["new"])
+
+ await session.filesystem.remove("/test/new")
+
+ ls = await session.filesystem.list("/test")
+ expect(ls.map(x => x.name)).toEqual([])
+
+ await session.close()
+})
+
+test('watch dir', async () => {
+ const session = new Session({ id: "Nodejs" })
+ await session.open()
+ await session.filesystem.write("/tmp/test.txt", "Hello")
+
+ const watcher = await session.filesystem.watchDir("/tmp")
+
+ const events = []
+ watcher.addEventListener(ev => events.push(ev))
+
+ await watcher.start()
+ await session.filesystem.write("/tmp/test.txt", "World!")
+ await new Promise(r => setTimeout(r, 1000))
+ await watcher.stop()
+
+ expect(events.length).toEqual(1)
+
+ const event = events[0]
+ expect(event["operation"]).toEqual("Write")
+ expect(event["path"]).toEqual("/tmp/test.txt")
+
+ await session.close()
+})
diff --git a/packages/js-sdk/test/process.test.mjs b/packages/js-sdk/test/process.test.mjs
new file mode 100644
index 000000000..1f5d6b8ac
--- /dev/null
+++ b/packages/js-sdk/test/process.test.mjs
@@ -0,0 +1,61 @@
+import { Session } from '../src'
+import { expect, test, vi } from 'vitest'
+
+test('process on stdout/stderr', async () => {
+ const session = await Session.create({ id: "Nodejs" })
+
+ const stdout = []
+ const stderr = []
+
+ const process = await session.process.start({
+ cmd: "pwd",
+ onStdout: data => stdout.push(data),
+ onStderr: data => stderr.push(data),
+ rootdir: "/tmp"
+ })
+
+ const output = await process.finished
+
+ expect(output.error).toEqual(false)
+ expect(output.stdout).toEqual("/tmp")
+ expect(output.stderr).toEqual("")
+ expect(stdout.map(message => message.line)).toEqual(["/tmp"])
+ expect(stderr).toEqual([])
+ await session.close()
+})
+
+test('process on exit', async () => {
+ const session = await Session.create({ id: "Nodejs" })
+
+ const onExit = vi.fn(() => {})
+
+ const process = await session.process.start({
+ cmd: "pwd",
+ onExit
+ })
+
+ await process.finished
+ expect(onExit).toHaveBeenCalled()
+
+ await session.close()
+})
+
+test('process send stdin', async () => {
+ const session = await Session.create({ id: "Nodejs" })
+
+ const process = await session.process.start({
+ cmd: 'while IFS= read -r line; do echo "$line"; sleep 1; done',
+ rootdir: "/code"
+ })
+ await process.sendStdin("ping\n")
+ await process.kill()
+
+ expect(process.output.stdout).toEqual("ping")
+ // TODO: Parity with Python SDK
+ // expect(process.output.messages.length).toEqual(1)
+ // const message = process.output_messages[0]
+ // assert.equal(message.line, "ping")
+ // assert.equal(message.error, false)
+
+ await session.close()
+})
diff --git a/packages/js-sdk/test/session.test.mjs b/packages/js-sdk/test/session.test.mjs
new file mode 100644
index 000000000..2887bde3e
--- /dev/null
+++ b/packages/js-sdk/test/session.test.mjs
@@ -0,0 +1,14 @@
+import { Session } from '../src'
+import { expect, test, vi } from 'vitest'
+
+test('create session', async () => {
+ const session = await Session.create({ id: "Nodejs" })
+ await session.close()
+})
+
+test('create multiple sessions', async () => {
+ const session = await Session.create({ id: "Nodejs" })
+ const session2 = await Session.create({ id: "Nodejs" })
+ await session.close()
+ await session2.close()
+})
diff --git a/packages/js-sdk/vitest.config.ts b/packages/js-sdk/vitest.config.ts
new file mode 100644
index 000000000..049e466b9
--- /dev/null
+++ b/packages/js-sdk/vitest.config.ts
@@ -0,0 +1,12 @@
+import { defineConfig } from 'vitest/config'
+
+export default defineConfig({
+ test: {
+ globals: false,
+ environment: 'node',
+ server: {},
+ deps: {
+ interopDefault: true,
+ },
+ },
+})