|
| 1 | +from collections.abc import Callable |
| 2 | +from pathlib import Path |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from agents import RunConfig |
| 8 | +from agents.run_config import SandboxRunConfig |
| 9 | +from agents.sandbox import SandboxAgent |
| 10 | +from agents.sandbox.entries import Dir, File, LocalDir, LocalFile |
| 11 | +from agents.sandbox.manifest import Manifest |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(params=["sandbox_config", "run_config", "agent"]) |
| 15 | +def configure_manifest(request: pytest.FixtureRequest) -> Callable[[Any], Manifest]: |
| 16 | + def configure(value: Any) -> Manifest: |
| 17 | + if request.param == "sandbox_config": |
| 18 | + manifest = SandboxRunConfig(manifest=value).manifest |
| 19 | + elif request.param == "run_config": |
| 20 | + config = RunConfig(sandbox={"manifest": value}) |
| 21 | + assert config.sandbox is not None |
| 22 | + manifest = config.sandbox.manifest |
| 23 | + else: |
| 24 | + manifest = SandboxAgent(name="test", default_manifest=value).default_manifest |
| 25 | + assert isinstance(manifest, Manifest) |
| 26 | + return manifest |
| 27 | + |
| 28 | + return configure |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize("entry_type", ["local_file", "local_dir"]) |
| 32 | +@pytest.mark.parametrize("nested", [False, True]) |
| 33 | +def test_dictionary_manifest_rejects_host_sources( |
| 34 | + configure_manifest: Callable[[Any], Manifest], entry_type: str, nested: bool |
| 35 | +) -> None: |
| 36 | + entry: dict[str, Any] = {"type": entry_type, "src": ".env"} |
| 37 | + if nested: |
| 38 | + entry = {"type": "dir", "children": {"copied": entry}} |
| 39 | + |
| 40 | + with pytest.raises(TypeError, match="must be configured on a trusted Manifest"): |
| 41 | + configure_manifest({"entries": {"output": entry}}) |
| 42 | + |
| 43 | + |
| 44 | +def test_dictionary_manifest_rejects_typed_host_source_entries( |
| 45 | + configure_manifest: Callable[[Any], Manifest], |
| 46 | +) -> None: |
| 47 | + with pytest.raises(TypeError, match="must be configured on a trusted Manifest"): |
| 48 | + configure_manifest({"entries": {"output": Dir(children={"copied": LocalFile(src=".env")})}}) |
| 49 | + |
| 50 | + |
| 51 | +def test_configuration_preserves_trusted_manifest_host_sources( |
| 52 | + configure_manifest: Callable[[Any], Manifest], |
| 53 | +) -> None: |
| 54 | + manifest = Manifest( |
| 55 | + entries={ |
| 56 | + "file": LocalFile(src="input.txt"), |
| 57 | + "nested": Dir(children={"directory": LocalDir(src="data")}), |
| 58 | + } |
| 59 | + ) |
| 60 | + |
| 61 | + assert configure_manifest(manifest) is manifest |
| 62 | + file = manifest.entries["file"] |
| 63 | + assert isinstance(file, LocalFile) |
| 64 | + assert file.src == Path("input.txt") |
| 65 | + |
| 66 | + |
| 67 | +def test_dictionary_manifest_preserves_entries_without_host_sources( |
| 68 | + configure_manifest: Callable[[Any], Manifest], |
| 69 | +) -> None: |
| 70 | + manifest = configure_manifest( |
| 71 | + { |
| 72 | + "entries": { |
| 73 | + "nested": { |
| 74 | + "type": "dir", |
| 75 | + "children": {"hello.txt": {"type": "file", "content": "hello"}}, |
| 76 | + }, |
| 77 | + "empty": {"type": "local_dir"}, |
| 78 | + } |
| 79 | + } |
| 80 | + ) |
| 81 | + |
| 82 | + directory = manifest.entries["nested"] |
| 83 | + assert isinstance(directory, Dir) |
| 84 | + file = directory.children["hello.txt"] |
| 85 | + assert isinstance(file, File) |
| 86 | + assert file.content == b"hello" |
| 87 | + empty = manifest.entries["empty"] |
| 88 | + assert isinstance(empty, LocalDir) |
| 89 | + assert empty.src is None |
0 commit comments