|
| 1 | +"""CodeInterpreterToolSet read_file 工具单元测试 |
| 2 | +
|
| 3 | +测试 read_file 工具的 base64 编码行为和 raw 参数控制。 |
| 4 | +Tests the read_file tool's base64 encoding behavior and the raw parameter control. |
| 5 | +""" |
| 6 | + |
| 7 | +import base64 |
| 8 | +import threading |
| 9 | +from unittest.mock import MagicMock, patch |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from agentrun.integration.builtin.sandbox import CodeInterpreterToolSet |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def toolset(): |
| 18 | + """创建 CodeInterpreterToolSet 实例,绕过 __init__ / Create instance bypassing __init__.""" |
| 19 | + with patch.object(CodeInterpreterToolSet, "__init__", lambda self: None): |
| 20 | + ts = CodeInterpreterToolSet() |
| 21 | + ts.sandbox = None |
| 22 | + ts.sandbox_id = "" |
| 23 | + ts._lock = threading.Lock() |
| 24 | + ts.template_name = "test-tpl" |
| 25 | + ts.template_type = MagicMock() |
| 26 | + ts.sandbox_idle_timeout_seconds = 600 |
| 27 | + ts.config = None |
| 28 | + ts.oss_mount_config = None |
| 29 | + ts.nas_config = None |
| 30 | + ts.polar_fs_config = None |
| 31 | + return ts |
| 32 | + |
| 33 | + |
| 34 | +def _make_mock_sandbox(file_content: str): |
| 35 | + """构造一个模拟沙箱,其 file.read 返回指定内容 / Build mock sandbox with file.read returning given content.""" |
| 36 | + from agentrun.sandbox.code_interpreter_sandbox import CodeInterpreterSandbox |
| 37 | + |
| 38 | + mock_sb = MagicMock(spec=CodeInterpreterSandbox) |
| 39 | + mock_sb.file.read.return_value = file_content |
| 40 | + return mock_sb |
| 41 | + |
| 42 | + |
| 43 | +class TestReadFileBase64Default: |
| 44 | + """测试 read_file 默认返回 base64 编码内容 / Test that read_file returns base64 by default.""" |
| 45 | + |
| 46 | + def test_returns_base64_encoded_content(self, toolset): |
| 47 | + """默认情况下内容应为 base64 编码 / Content should be base64 encoded by default.""" |
| 48 | + file_content = "hello world" |
| 49 | + mock_sb = _make_mock_sandbox(file_content) |
| 50 | + |
| 51 | + with patch.object(toolset, "_run_in_sandbox", side_effect=lambda fn: fn(mock_sb)): |
| 52 | + result = toolset.read_file(path="/tmp/test.txt") |
| 53 | + |
| 54 | + expected_b64 = base64.b64encode(b"hello world").decode("ascii") |
| 55 | + assert result["content"] == expected_b64 |
| 56 | + assert result["encoding"] == "base64" |
| 57 | + assert result["path"] == "/tmp/test.txt" |
| 58 | + |
| 59 | + def test_base64_roundtrip(self, toolset): |
| 60 | + """base64 解码后应等于原始内容 / Decoded base64 should equal original content.""" |
| 61 | + file_content = "中文内容 line1\nline2" |
| 62 | + mock_sb = _make_mock_sandbox(file_content) |
| 63 | + |
| 64 | + with patch.object(toolset, "_run_in_sandbox", side_effect=lambda fn: fn(mock_sb)): |
| 65 | + result = toolset.read_file(path="/tmp/utf8.txt") |
| 66 | + |
| 67 | + decoded = base64.b64decode(result["content"]).decode("utf-8") |
| 68 | + assert decoded == file_content |
| 69 | + |
| 70 | + def test_bytes_content_also_base64_encoded(self, toolset): |
| 71 | + """当底层返回 bytes 时同样应 base64 编码 / Bytes content should also be base64 encoded.""" |
| 72 | + file_bytes = b"\x00\x01\x02\x03" |
| 73 | + from agentrun.sandbox.code_interpreter_sandbox import CodeInterpreterSandbox |
| 74 | + |
| 75 | + mock_sb = MagicMock(spec=CodeInterpreterSandbox) |
| 76 | + mock_sb.file.read.return_value = file_bytes |
| 77 | + |
| 78 | + with patch.object(toolset, "_run_in_sandbox", side_effect=lambda fn: fn(mock_sb)): |
| 79 | + result = toolset.read_file(path="/tmp/binary.bin") |
| 80 | + |
| 81 | + expected_b64 = base64.b64encode(file_bytes).decode("ascii") |
| 82 | + assert result["content"] == expected_b64 |
| 83 | + assert result["encoding"] == "base64" |
| 84 | + |
| 85 | + |
| 86 | +class TestReadFileRawParam: |
| 87 | + """测试 raw=True 时返回原始内容 / Test that raw=True returns plain text content.""" |
| 88 | + |
| 89 | + def test_raw_true_returns_plain_content(self, toolset): |
| 90 | + """raw=True 时应返回原始文本 / raw=True should return raw text.""" |
| 91 | + file_content = "plain text content" |
| 92 | + mock_sb = _make_mock_sandbox(file_content) |
| 93 | + |
| 94 | + with patch.object(toolset, "_run_in_sandbox", side_effect=lambda fn: fn(mock_sb)): |
| 95 | + result = toolset.read_file(path="/tmp/plain.txt", raw=True) |
| 96 | + |
| 97 | + assert result["content"] == file_content |
| 98 | + assert result["encoding"] == "raw" |
| 99 | + assert result["path"] == "/tmp/plain.txt" |
| 100 | + |
| 101 | + def test_raw_false_same_as_default(self, toolset): |
| 102 | + """raw=False 应与默认行为一致 / raw=False should behave identically to default.""" |
| 103 | + file_content = "some content" |
| 104 | + mock_sb = _make_mock_sandbox(file_content) |
| 105 | + |
| 106 | + with patch.object(toolset, "_run_in_sandbox", side_effect=lambda fn: fn(mock_sb)): |
| 107 | + result_explicit = toolset.read_file(path="/tmp/f.txt", raw=False) |
| 108 | + |
| 109 | + mock_sb2 = _make_mock_sandbox(file_content) |
| 110 | + with patch.object(toolset, "_run_in_sandbox", side_effect=lambda fn: fn(mock_sb2)): |
| 111 | + result_default = toolset.read_file(path="/tmp/f.txt") |
| 112 | + |
| 113 | + assert result_explicit == result_default |
| 114 | + assert result_explicit["encoding"] == "base64" |
0 commit comments