File size: 9,065 Bytes
c7fa397 | 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | """Test zero-limitation tools, code editing, and storage vault."""
import sys
import os
import tempfile
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from singularity_llm.harness.tools import (
ToolRegistry, get_default_tools, tool_loop, parse_tool_calls,
_tool_shell_exec, _tool_write_file, _tool_read_file,
_tool_code_edit, _tool_list_dir, _tool_make_dir, _tool_delete_file,
)
from singularity_llm.storage.vault import StorageVault
def test_full_terminal_control():
"""Test that shell_exec has zero limitations β can run any command."""
# Test a basic command
result = _tool_shell_exec("echo hello world")
assert "hello world" in result, f"Expected 'hello world' in result: {result}"
print(f" echo: {result.strip()}")
# Test that no commands are blocked
result2 = _tool_shell_exec("python --version")
assert "Error: dangerous command blocked" not in result2
print(f" python --version: {result2.strip()}")
# Test pip command (would be blocked by old safety filter)
result3 = _tool_shell_exec("echo pip install numpy")
assert "dangerous" not in result3.lower()
print(f" pip command: not blocked")
def test_code_edit_tool():
"""Test code_edit tool β can modify files including its own framework."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
f.write("value = 'old'\nprint(value)\n")
path = f.name
try:
# Read original
content = _tool_read_file(path)
assert "old" in content
# Edit the file
result = _tool_code_edit(f'"{path}" "old" "new"')
assert "Replaced" in result, f"Expected replacement: {result}"
print(f" Edit result: {result}")
# Verify change
content = _tool_read_file(path)
assert "new" in content
assert "old" not in content
print(f" Verified: old β new")
finally:
os.unlink(path)
def test_list_dir_tool():
"""Test list_dir tool."""
with tempfile.TemporaryDirectory() as tmpdir:
# Create some files
open(os.path.join(tmpdir, "file1.txt"), "w").close()
open(os.path.join(tmpdir, "file2.py"), "w").close()
os.makedirs(os.path.join(tmpdir, "subdir"))
result = _tool_list_dir(tmpdir)
assert "file1.txt" in result
assert "file2.py" in result
assert "subdir/" in result
print(f" Listed: {result.strip()}")
def test_make_dir_tool():
"""Test make_dir tool."""
with tempfile.TemporaryDirectory() as tmpdir:
new_dir = os.path.join(tmpdir, "new_project", "src")
result = _tool_make_dir(new_dir)
assert "Created" in result
assert os.path.exists(new_dir)
print(f" Created: {new_dir}")
def test_delete_file_tool():
"""Test delete_file tool."""
with tempfile.TemporaryDirectory() as tmpdir:
# Test file deletion
file_path = os.path.join(tmpdir, "temp.txt")
open(file_path, "w").close()
assert os.path.exists(file_path)
result = _tool_delete_file(file_path)
assert "Deleted file" in result
assert not os.path.exists(file_path)
# Test directory deletion
dir_path = os.path.join(tmpdir, "temp_dir")
os.makedirs(dir_path)
result2 = _tool_delete_file(dir_path)
assert "Deleted directory" in result2
assert not os.path.exists(dir_path)
print(f" Deleted file and directory: OK")
def test_zero_limitation_tools_count():
"""Test that all zero-limitation tools are registered."""
tools = get_default_tools()
tool_names = [t.name for t in tools]
assert "shell_exec" in tool_names
assert "write_file" in tool_names
assert "code_edit" in tool_names
assert "list_dir" in tool_names
assert "make_dir" in tool_names
assert "delete_file" in tool_names
assert "read_file" in tool_names
assert "calculate" in tool_names
print(f" Tools: {len(tools)} registered β {tool_names}")
# Check shell_exec description says full control
shell_tool = next(t for t in tools if t.name == "shell_exec")
assert "full control" in shell_tool.description.lower() or "no restriction" in shell_tool.description.lower()
print(f" shell_exec: {shell_tool.description}")
def test_storage_vault():
"""Test mass storage vault β auto-resizing storage."""
with tempfile.TemporaryDirectory() as tmpdir:
vault = StorageVault(data_dir=tmpdir)
# Check directories created
assert os.path.exists(vault.db_dir)
assert os.path.exists(vault.cache_dir)
assert os.path.exists(vault.archive_dir)
assert os.path.exists(vault.artifacts_dir)
print(f" Directories: db, cache, archive, artifacts β all created")
# Store and load artifact
path = vault.store_artifact("test_code.py", b"print('hello')")
assert os.path.exists(path)
loaded = vault.load_artifact("test_code.py")
assert loaded == b"print('hello')"
print(f" Artifact stored and loaded: {loaded}")
# Store text artifact
path2 = vault.store_artifact_text("notes.txt", "Important notes")
loaded2 = vault.load_artifact("notes.txt")
assert loaded2 == b"Important notes"
print(f" Text artifact: {loaded2}")
# List artifacts
artifacts = vault.list_artifacts()
assert len(artifacts) == 2
names = [a["name"] for a in artifacts]
assert "test_code.py" in names
assert "notes.txt" in names
print(f" Artifacts listed: {names}")
# Delete artifact
deleted = vault.delete_artifact("test_code.py")
assert deleted is True
artifacts = vault.list_artifacts()
assert len(artifacts) == 1
print(f" Deleted: test_code.py ({len(artifacts)} remaining)")
# Stats
stats = vault.get_stats()
assert stats["disk_total_gb"] > 0
assert "total_storage_mb" in stats
assert "disk_usage_percent" in stats
print(f" Stats: {stats['total_storage_mb']}MB used, "
f"{stats['disk_free_gb']}GB free, {stats['disk_usage_percent']:.1%} disk usage")
def test_vault_auto_resize():
"""Test vault auto-resize and cleanup."""
with tempfile.TemporaryDirectory() as tmpdir:
vault = StorageVault(data_dir=tmpdir)
# Store many artifacts
for i in range(20):
vault.store_artifact_text(f"artifact_{i}.txt", f"content {i}" * 100)
stats = vault.get_stats()
assert stats["artifacts_storage_bytes"] > 0
print(f" Stored 20 artifacts: {stats['artifacts_storage_bytes']} bytes")
# Force cleanup (should not delete recent files)
result = vault.force_cleanup()
assert "cleanups_performed" in result
# Recent files should still be there
artifacts = vault.list_artifacts()
assert len(artifacts) == 20 # nothing deleted (all recent)
print(f" Cleanup: {result['cleanups_performed']} cycles, {len(artifacts)} artifacts remain")
def test_vault_db_paths():
"""Test vault DB path management."""
with tempfile.TemporaryDirectory() as tmpdir:
vault = StorageVault(data_dir=tmpdir)
db_path = vault.get_db_path("memory")
assert db_path.endswith("memory.db")
assert "db" in db_path
cache_path = vault.get_cache_path("test.cache")
assert "cache" in cache_path
artifact_path = vault.get_artifact_path("code.py")
assert "artifacts" in artifact_path
print(f" DB path: {db_path}")
print(f" Cache path: {cache_path}")
print(f" Artifact path: {artifact_path}")
def test_tool_loop_with_new_tools():
"""Test that tool_loop works with the new tools."""
registry = ToolRegistry()
for tool in get_default_tools():
registry.register(tool)
# Test list_dir via tool loop
text = '[TOOL: list_dir(".")]'
result_text, results = tool_loop(text, registry)
assert len(results) > 0
assert results[0].success
print(f" Tool loop: {results[0].name} β {results[0].output[:50]}...")
if __name__ == "__main__":
print("Running zero-limitation tools and storage vault tests...")
test_full_terminal_control()
print(" β test_full_terminal_control")
test_code_edit_tool()
print(" β test_code_edit_tool")
test_list_dir_tool()
print(" β test_list_dir_tool")
test_make_dir_tool()
print(" β test_make_dir_tool")
test_delete_file_tool()
print(" β test_delete_file_tool")
test_zero_limitation_tools_count()
print(" β test_zero_limitation_tools_count")
test_storage_vault()
print(" β test_storage_vault")
test_vault_auto_resize()
print(" β test_vault_auto_resize")
test_vault_db_paths()
print(" β test_vault_db_paths")
test_tool_loop_with_new_tools()
print(" β test_tool_loop_with_new_tools")
print("\nAll zero-limitation & vault tests passed!")
|