Skip to content

Commit 650efcd

Browse files
committed
Fix tests
# Conflicts: # packages/js-sdk/package-lock.json # packages/js-sdk/package.json
1 parent 207863e commit 650efcd

File tree

6 files changed

+187
-55
lines changed

6 files changed

+187
-55
lines changed

packages/js-sdk/tests/mouse.test.ts

+27-23
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
1-
import { expect } from 'vitest'
2-
import { sandboxTest } from './setup'
1+
import { expect } from "vitest";
2+
import { sandboxTest } from "./setup";
33

4-
sandboxTest('cursor position', async ({ sandbox }) => {
5-
const pos = await sandbox.getCursorPosition()
6-
const size = await sandbox.getScreenSize()
7-
// By the default, the cursor's position is in the center of the screen
8-
expect(pos).toEqual({ x: size.width / 2, y: size.height / 2 })
9-
})
4+
sandboxTest("cursor position", async ({ sandbox }) => {
5+
const pos = await sandbox.getCursorPosition();
6+
const size = await sandbox.getScreenSize();
7+
if (size) {
8+
// By the default, the cursor's position is in the center of the screen
9+
expect(pos).toEqual({ x: size.width / 2, y: size.height / 2 });
10+
} else {
11+
throw new Error("Screen size is null");
12+
}
13+
});
1014

11-
sandboxTest('mouse move', async ({ sandbox }) => {
12-
await sandbox.moveMouse(100, 200)
13-
const pos2 = await sandbox.getCursorPosition()
14-
expect(pos2).toEqual({ x: 100, y: 200 })
15-
})
15+
sandboxTest("mouse move", async ({ sandbox }) => {
16+
await sandbox.moveMouse(100, 200);
17+
const pos2 = await sandbox.getCursorPosition();
18+
expect(pos2).toEqual({ x: 100, y: 200 });
19+
});
1620

17-
sandboxTest('mouse left click', async ({ sandbox }) => {
21+
sandboxTest("mouse left click", async ({ sandbox }) => {
1822
// TODO: Implement
19-
})
23+
});
2024

21-
sandboxTest('mouse double click', async ({ sandbox }) => {
25+
sandboxTest("mouse double click", async ({ sandbox }) => {
2226
// TODO: Implement
23-
})
27+
});
2428

25-
sandboxTest('mouse right click', async ({ sandbox }) => {
29+
sandboxTest("mouse right click", async ({ sandbox }) => {
2630
// TODO: Implement
27-
})
31+
});
2832

29-
sandboxTest('mouse middle click', async ({ sandbox }) => {
33+
sandboxTest("mouse middle click", async ({ sandbox }) => {
3034
// TODO: Implement
31-
})
35+
});
3236

33-
sandboxTest('mouse scroll', async ({ sandbox }) => {
37+
sandboxTest("mouse scroll", async ({ sandbox }) => {
3438
// TODO: Implement
35-
})
39+
});

packages/python-sdk/poetry.lock

+90-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/python-sdk/pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ python = "^3.9"
1414

1515
e2b = "^1.0.4"
1616
requests = "^2.32.3"
17+
pillow = "^11.1.0"
1718

1819
[tool.poetry.group.dev.dependencies]
1920
pytest = "^7.4.0"

packages/python-sdk/pytest.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
[pytest]
33
markers =
44
skip_debug: skip test if E2B_DEBUG is set.
5-
asyncio_mode=auto
5+
# asyncio_mode=auto
66

77
addopts = "--import-mode=importlib" "--numprocesses=2"

packages/python-sdk/tests/conftest.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
import pytest
22
import os
3-
4-
from logging import warning
5-
3+
import logging
64
from e2b_desktop import Sandbox
75

6+
# Set up timeout and logger
87
timeout = 60
8+
logger = logging.getLogger(__name__)
99

10-
@pytest.fixture()
10+
@pytest.fixture(scope="function")
1111
def sandbox(debug):
12+
# Create a new sandbox instance for each test
1213
sandbox = Sandbox(timeout=timeout)
1314

1415
try:
1516
yield sandbox
1617
finally:
18+
# Ensure proper cleanup
1719
try:
1820
sandbox.kill()
19-
except:
21+
except Exception as e:
2022
if not debug:
21-
warning(
22-
"Failed to kill sandbox — this is expected if the test runs with local envd."
23-
)
23+
logger.warning(f"Failed to kill sandbox — this is expected if the test runs with local envd. Error: {e}")
2424

2525

2626
@pytest.fixture
2727
def debug():
28+
# Determine if debugging is enabled
2829
return os.getenv("E2B_DEBUG") is not None
2930

31+
3032
@pytest.fixture(autouse=True)
3133
def skip_by_debug(request, debug):
34+
# Skip test if E2B_DEBUG is set
3235
if request.node.get_closest_marker("skip_debug"):
3336
if debug:
3437
pytest.skip("skipped because E2B_DEBUG is set")
+57-21
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,67 @@
11
import os
22
import time
33
from e2b_desktop import Sandbox
4+
from PIL import ImageChops, Image
5+
import io
46

5-
# def test_right_click(sandbox: Sandbox):
6-
# sandbox.right_click()
7-
# time.sleep(5)
8-
# pos = sandbox.locate_on_screen("Create Document")
9-
# print(pos)
10-
# assert pos is not None
7+
def crop_around_point(image, cursor_pos):
8+
x, y = cursor_pos
9+
box = (x - 50, y - 50, x + 50, y + 50) # 100x100 box
10+
return image.crop(box)
1111

12+
def images_are_equal(img1, img2):
13+
# Use ImageChops to find the difference
14+
diff = ImageChops.difference(img1, img2)
15+
# Check if there is any difference
16+
return diff.getbbox() is None # Returns True if images are equal, False if different
1217

13-
# def test_screenshot(sandbox: Sandbox):
14-
# screenshot_path = "test-screenshot.png"
15-
# sandbox.screenshot(screenshot_path)
16-
# assert os.path.exists(screenshot_path)
18+
def test_right_click(sandbox: Sandbox):
19+
# Capture the initial screenshot
20+
initial_screenshot_bytes = sandbox.take_screenshot()
21+
initial_image = Image.open(io.BytesIO(initial_screenshot_bytes))
22+
23+
# Get cursor position and perform right click
24+
cursor_pos = sandbox.get_cursor_position()
25+
sandbox.right_click()
26+
time.sleep(5) # Wait for UI to respond
27+
28+
# Capture and process the second screenshot
29+
post_click_screenshot_bytes = sandbox.take_screenshot()
30+
post_click_image = Image.open(io.BytesIO(post_click_screenshot_bytes))
31+
32+
# Crop both images around the cursor position
33+
cropped_image_1 = crop_around_point(initial_image, cursor_pos)
34+
cropped_image_2 = crop_around_point(post_click_image, cursor_pos)
35+
36+
# Compare the cropped images
37+
assert not images_are_equal(cropped_image_1, cropped_image_2), "The image around the cursor did not change after right-click."
1738

18-
# def test_get_cursor_position(sandbox: Sandbox):
19-
# pos = sandbox.get_cursor_position()
20-
# assert pos == (512, 384) # In the middle of the screen
39+
def test_screenshot(sandbox: Sandbox):
40+
image = sandbox.take_screenshot()
41+
assert image, "Screenshot was not taken successfully"
42+
43+
# Check if the image is a valid image format
44+
try:
45+
img = Image.open(io.BytesIO(image))
46+
img.verify() # Verify that it is an image
47+
except Exception:
48+
assert False, "The screenshot is not a valid image."
2149

50+
def test_get_cursor_position(sandbox: Sandbox):
51+
pos = sandbox.get_cursor_position()
52+
assert pos == (512, 384), f"Expected cursor position to be (512, 384), but got {pos}"
2253

23-
# def test_get_screen_size(sandbox: Sandbox):
24-
# size = sandbox.get_screen_size()
25-
# assert size == (1024, 768) # 1024x768 screen
2654

27-
# def test_write(sandbox: Sandbox):
28-
# # Create a file and open it in a text editor
29-
# text_file_path = "/home/user/test.txt"
30-
# sandbox.write(text_file_path, "hello")
31-
# sandbox.open_file(text_file_path)
55+
def test_get_screen_size(sandbox: Sandbox):
56+
size = sandbox.get_screen_size()
57+
assert size == (1024, 768), f"Expected screen size to be (1024, 768), but got {size}"
58+
59+
60+
def test_write(sandbox: Sandbox):
61+
# Create a file and open it in a text editor
62+
text_file_path = "/home/user/test.txt"
63+
sandbox.files.write(text_file_path, "hello")
64+
sandbox.open(text_file_path)
65+
# Add an assertion here, perhaps check if the content is correct
66+
content = sandbox.files.read(text_file_path)
67+
assert content == "hello", f"Expected content 'hello' in {text_file_path}, but got {content}"

0 commit comments

Comments
 (0)