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