| 12 | |
| 13 | |
| 14 | def test_sandbox_api_crud_and_exec( |
| 15 | sandbox: Callable[..., Sandbox], |
| 16 | sandbox_client: SandboxClient, |
| 17 | ) -> None: |
| 18 | class _FileOps: |
| 19 | def write(self, path: str, content: str) -> None: |
| 20 | from pathlib import Path |
| 21 | |
| 22 | Path(path).write_text(content) |
| 23 | |
| 24 | def read(self, path: str) -> str: |
| 25 | from pathlib import Path |
| 26 | |
| 27 | return Path(path).read_text() |
| 28 | |
| 29 | with sandbox(delete_on_exit=True) as sb: |
| 30 | assert sb.id |
| 31 | # Server auto-generates a petname (e.g. "feasible-retriever") |
| 32 | assert sb.sandbox.name |
| 33 | parts = sb.sandbox.name.split("-") |
| 34 | assert len(parts) == 2, ( |
| 35 | f"expected petname with 2 parts, got {sb.sandbox.name!r}" |
| 36 | ) |
| 37 | assert all(p.isalpha() and p.islower() for p in parts) |
| 38 | |
| 39 | fetched = sandbox_client.get(sb.sandbox.name) |
| 40 | assert fetched.id == sb.id |
| 41 | |
| 42 | ids = set(sandbox_client.list_ids(limit=100)) |
| 43 | assert sb.id in ids |
| 44 | |
| 45 | result = sb.exec(["python", "-c", "print('sandbox-ok')"]) |
| 46 | assert result.exit_code == 0 |
| 47 | assert "sandbox-ok" in result.stdout |
| 48 | |
| 49 | file_ops = _FileOps() |
| 50 | create_file = sb.exec_python( |
| 51 | file_ops.write, |
| 52 | args=("/sandbox/exec-persistence.txt", "ok"), |
| 53 | ) |
| 54 | assert create_file.exit_code == 0 |
| 55 | |
| 56 | verify_file = sb.exec_python( |
| 57 | file_ops.read, args=("/sandbox/exec-persistence.txt",) |
| 58 | ) |
| 59 | assert verify_file.exit_code == 0 |
| 60 | assert verify_file.stdout.strip() == "ok" |