| |
| |
| |
| |
| |
|
|
| |
| """ |
| Simple test showing how users will use CodingEnv.from_docker_image(). |
| |
| This is the simplest possible usage |
| """ |
|
|
| import sys |
| from pathlib import Path |
|
|
| |
| sys.path.insert(0, str(Path(__file__).parent.parent / "src")) |
|
|
| from coding_env import CodeAction, CodingEnv |
|
|
|
|
| def main(): |
| """Test CodingEnv.from_docker_image().""" |
| print("=" * 60) |
| print("CodingEnv.from_docker_image() Test") |
| print("=" * 60) |
| print() |
|
|
| try: |
| |
| print("Creating client from Docker image...") |
| print(" CodingEnv.from_docker_image('coding-env:latest')") |
| print() |
|
|
| client = CodingEnv.from_docker_image("coding-env:latest") |
|
|
| print("β Client created and container started!\n") |
|
|
| |
| print("Testing the environment:") |
| print("-" * 60) |
|
|
| |
| print("\n1. Reset:") |
| result = client.reset() |
| print(f" stdout: {result.observation.stdout}") |
| print(f" stderr: {result.observation.stderr}") |
| print(f" exit_code: {result.observation.exit_code}") |
|
|
| |
| state = client.state() |
| print(f" State: episode_id={state.episode_id}, step_count={state.step_count}") |
|
|
| |
| print("\n2. Execute Python code:") |
|
|
| code_samples = [ |
| "print('Hello, World!')", |
| "x = 5 + 3\nprint(f'Result: {x}')", |
| "import math\nprint(f'Pi is approximately {math.pi:.4f}')", |
| "# Multi-line calculation\nfor i in range(1, 4):\n print(f'{i} squared is {i**2}')", |
| ] |
|
|
| for i, code in enumerate(code_samples, 1): |
| result = client.step(CodeAction(code=code)) |
| print(f" {i}. Code: {code.replace(chr(10), '\\n')[:50]}...") |
| print(f" β stdout: {result.observation.stdout.strip()}") |
| print(f" β exit_code: {result.observation.exit_code}") |
| if result.observation.stderr: |
| print(f" β stderr: {result.observation.stderr}") |
|
|
| |
| print("\n3. Test error scenarios:") |
|
|
| error_samples = [ |
| ("Division by zero", "x = 1 / 0\nprint('Should not reach here')"), |
| ("Undefined variable", "print(undefined_variable)"), |
| ("Syntax error", "print('Hello'"), |
| ] |
|
|
| for i, (description, code) in enumerate(error_samples, 1): |
| result = client.step(CodeAction(code=code)) |
| print(f" {i}. {description}") |
| print(f" Code: {code.replace(chr(10), '\\n')[:40]}...") |
| print(f" β exit_code: {result.observation.exit_code}") |
| if result.observation.stderr: |
| |
| error_msg = result.observation.stderr[:100] |
| if len(result.observation.stderr) > 100: |
| error_msg += "..." |
| print(f" β stderr: {error_msg}") |
|
|
| |
| print("\n4. Check final state:") |
| state = client.state() |
| print(f" episode_id: {state.episode_id}") |
| print(f" step_count: {state.step_count}") |
| print(f" last_exit_code: {state.last_exit_code}") |
|
|
| print("\n" + "-" * 60) |
| print("\nβ All operations successful!") |
| print() |
|
|
| print("Cleaning up...") |
| client.close() |
| print("β Container stopped and removed") |
| print() |
|
|
| print("=" * 60) |
| print("Test completed successfully! π") |
| print("=" * 60) |
|
|
| return True |
|
|
| except Exception as e: |
| print(f"\nβ Test failed: {e}") |
| import traceback |
| traceback.print_exc() |
| return False |
|
|
|
|
| if __name__ == "__main__": |
| success = main() |
| exit(0 if success else 1) |
|
|