import base64
import importlib.util
import io
import json
from pathlib import Path
import subprocess
import sys
import tempfile
import types
import unittest
from unittest.mock import MagicMock, patch
import zipfile
spec = importlib.util.spec_from_file_location("gateway", Path(__file__).with_name("app.py"))
gateway = importlib.util.module_from_spec(spec)
spec.loader.exec_module(gateway)
class GatewayTests(unittest.TestCase):
def execute_file_tool(self, root, name, arguments, track_changes=False):
payload = base64.b64encode(json.dumps({
"name": name, "arguments": arguments, "trackChanges": track_changes
}).encode()).decode()
script = gateway.SCRIPT.replace("pathlib.Path('/workspace')", "pathlib.Path(" + repr(str(root)) + ")")
result = subprocess.run([sys.executable, "-X", "utf8", "-c", "PAYLOAD=" + repr(payload) + "\n" + script], capture_output=True, timeout=10)
self.assertEqual(0, result.returncode, result.stderr.decode())
return json.loads(result.stdout)
def test_file_roundtrip_and_parent_traversal_rejection(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory) / "workspace"
root.mkdir()
self.assertIn("written", self.execute_file_tool(root, "write_file", {"path": "src/test.txt", "content": "真实内容"}))
self.assertEqual("真实内容", self.execute_file_tool(root, "read_file", {"path": "src/test.txt"})["content"])
self.assertIn("error", self.execute_file_tool(root, "write_file", {"path": "../outside.txt", "content": "denied"}))
self.assertFalse((Path(directory) / "outside.txt").exists())
def test_large_file_read_is_rejected(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
(root / "large.txt").write_bytes(b"x" * 64001)
self.assertIn("error", self.execute_file_tool(root, "read_file", {"path": "large.txt"}))
def test_git_history_distinguishes_empty_failed_and_saved_versions(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
self.assertEqual([], self.execute_file_tool(root, "version_list", {})["versions"])
subprocess.run(["git", "init"], cwd=root, check=True, capture_output=True)
self.assertEqual([], self.execute_file_tool(root, "version_list", {})["versions"])
(root / "index.html").write_text("
Versioned source
")
saved = self.execute_file_tool(root, "version_save", {"message": "Initial version"})
self.assertEqual("Initial version", saved["versions"][0]["message"])
self.assertEqual(saved, self.execute_file_tool(root, "version_list", {}))
(root / ".git/config").write_text("[invalid")
self.assertEqual("Git history unavailable", self.execute_file_tool(root, "version_list", {})["error"])
def test_source_snapshot_checks_start_and_excludes_credentials_and_local_data(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
(root / "package.json").write_text('{"scripts":{"start":"node server.js"}}')
result = self.execute_file_tool(root, "release_snapshot", {})
self.assertIn("server.js", result["error"])
(root / "server.js").write_text("console.log('ready')")
(root / ".env").write_text("secret")
(root / "data").mkdir()
(root / "data/students.db").write_text("private rows")
result = self.execute_file_tool(root, "release_snapshot", {})
self.assertEqual("NODE", result["runtime"])
with zipfile.ZipFile(io.BytesIO(base64.b64decode(result["archive"]))) as archive:
self.assertEqual({"package.json", "server.js"}, set(archive.namelist()))
def test_workspace_inspection_reports_missing_start_even_with_other_source(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
(root / "index.js").write_text("console.log('other source')")
package = root / "package.json"
package.write_text('{"scripts":{"start":"node ./src/server.cjs"}}')
result = self.execute_file_tool(root, "workspace_inspect", {})
self.assertEqual(1, result["sourceFiles"])
self.assertEqual("src/server.cjs", result["missingStartFile"])
(root / "src").mkdir()
(root / "src/server.cjs").write_text("console.log('ready')")
self.assertNotIn("missingStartFile", self.execute_file_tool(root, "workspace_inspect", {}))
package.write_text('{"scripts":{"start":"node dist/server.js","build":"tsc"}}')
self.assertNotIn("missingStartFile", self.execute_file_tool(root, "workspace_inspect", {}))
def test_agent_file_change_counts_and_manual_edits_are_separate(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
self.assertNotIn("fileChanges", self.execute_file_tool(root, "write_file", {
"path": "manual.txt", "content": "user\n"
}))
results = []
for tool, args in [
("write_file", {"path": "src/code.js", "content": "one\ntwo\n"}),
("write_file", {"path": "src/code.js", "content": "one\nthree\nfour\n"}),
("delete_file", {"path": "src/code.js"}),
]:
result = self.execute_file_tool(root, tool, args, track_changes=True)
self.assertTrue(result["fileChanges"]["complete"])
files = result["fileChanges"]["files"]
self.assertEqual(["src/code.js"], [file["path"] for file in files])
results.append(tuple(files[0][key] for key in ("status", "added", "removed")))
self.assertEqual([("ADDED", 2, 0), ("MODIFIED", 2, 1), ("DELETED", 0, 3)], results)
def test_source_scan_failure_reports_partial_and_keeps_successful_write(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
(root / "large.dat").write_bytes(b"x" * 8_000_001)
result = self.execute_file_tool(root, "write_file", {"path": "index.js", "content": "ok"}, True)
self.assertEqual(2, result["written"])
self.assertEqual({"complete": False, "files": []}, result["fileChanges"])
def test_workspace_inspection_requires_nonempty_source_and_excludes_dependencies(self):
with tempfile.TemporaryDirectory() as directory:
root=Path(directory)
(root/'node_modules').mkdir()
(root/'node_modules/fake.js').write_text('dependency code')
(root/'index.html').write_text('')
(root/'.env').write_text('SECRET=must-not-appear')
self.assertEqual(0,self.execute_file_tool(root,'workspace_inspect',{})['sourceFiles'])
(root/'index.html').write_text('Actual source
')
result=self.execute_file_tool(root,'workspace_inspect',{})
self.assertEqual(1,result['sourceFiles'])
self.assertTrue(result['complete'])
self.assertEqual(['index.html'],[f['path'] for f in result['files']])
def test_archive_excludes_git_and_dependency_files(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
(root / "index.html").write_text("Test
")
(root / "node_modules").mkdir()
(root / "node_modules/ignored.js").write_text("ignore")
result = self.execute_file_tool(root, "archive_source", {})
with zipfile.ZipFile(io.BytesIO(base64.b64decode(result["base64"]))) as archive:
self.assertEqual(["index.html"], archive.namelist())
def test_invalid_identity_and_missing_credentials_fail_closed(self):
with self.assertRaises(gateway.HTTPException):
gateway.workspace_name("../../host")
with patch.object(gateway, "TOKEN", "x" * 32):
with self.assertRaises(gateway.HTTPException):
gateway.authorize("Bearer wrong")
gateway.authorize("Bearer " + "x" * 32)
def test_created_workspace_has_isolation_and_persistent_data(self):
core, net = MagicMock(), MagicMock()
net.read_namespaced_network_policy.side_effect = gateway.ApiException(status=404)
core.read_namespaced_persistent_volume_claim.side_effect = gateway.ApiException(status=404)
core.read_namespaced_pod.side_effect = gateway.ApiException(status=404)
core.create_namespaced_pod.return_value = types.SimpleNamespace(status=types.SimpleNamespace(phase="Running", container_statuses=[types.SimpleNamespace(ready=True)]))
with patch.object(gateway.app.state, "core", core, create=True), patch.object(gateway.app.state, "net", net, create=True), patch.object(gateway, "IMAGE", "test/isolated-image:local"):
gateway.ensure_workspace("00000000-0000-0000-0000-000000000001")
pod = core.create_namespaced_pod.call_args.kwargs["body"]["spec"]
self.assertEqual("gvisor", pod["runtimeClassName"])
self.assertFalse(pod["automountServiceAccountToken"])
self.assertTrue(pod["containers"][0]["securityContext"]["readOnlyRootFilesystem"])
self.assertEqual(["ALL"], pod["containers"][0]["securityContext"]["capabilities"]["drop"])
self.assertIn("persistentVolumeClaim", pod["volumes"][0])
policy = net.create_namespaced_network_policy.call_args.kwargs["body"]["spec"]
self.assertEqual([], policy["ingress"])
self.assertIn("169.254.0.0/16", policy["egress"][1]["to"][0]["ipBlock"]["except"])
def test_existing_workspace_does_not_submit_creates_to_admission(self):
core, net = MagicMock(), MagicMock()
core.read_namespaced_pod.return_value = types.SimpleNamespace(
status=types.SimpleNamespace(phase="Running", container_statuses=[types.SimpleNamespace(ready=True)])
)
project = "00000000-0000-0000-0000-000000000004"
with (patch.object(gateway.app.state, "core", core, create=True),
patch.object(gateway.app.state, "net", net, create=True)):
for _ in range(3):
self.assertEqual("dev-00000000000000000000000000000004", gateway.ensure_workspace(project))
net.create_namespaced_network_policy.assert_not_called()
core.create_namespaced_persistent_volume_claim.assert_not_called()
core.create_namespaced_pod.assert_not_called()
def test_create_with_lost_response_checks_for_resource_before_retry(self):
resource = object()
read = MagicMock(side_effect=[gateway.ApiException(status=404), resource])
create = MagicMock(side_effect=gateway.ApiException(status=503))
with patch.object(gateway.time, "sleep"):
self.assertIs(resource, gateway.ensure_resource(read, create, "qa", {"metadata": {"name": "workspace"}}))
create.assert_called_once()
def test_quota_denial_of_new_resource_reports_capacity(self):
read = MagicMock(side_effect=gateway.ApiException(status=404))
denial = gateway.ApiException(status=403)
denial.body = json.dumps({"message": 'pods "workspace" is forbidden: exceeded quota: quota, requested: pods=1'})
create = MagicMock(side_effect=denial)
with self.assertRaises(gateway.HTTPException) as error:
gateway.ensure_resource(read, create, "qa", {"metadata": {"name": "workspace"}})
self.assertEqual((503, "WORKSPACE_CAPACITY"), (error.exception.status_code, error.exception.detail))
create.assert_called_once()
def test_transient_exec_handshake_recovers_before_tool_input(self):
project = "00000000-0000-0000-0000-000000000002"
request = gateway.Tool(name="write_file", arguments={"path": "index.html", "content": "test"})
connection = MagicMock()
with (patch.object(gateway, "TOKEN", "x" * 32),
patch.object(gateway, "ensure_workspace"),
patch.object(gateway, "stream", side_effect=[gateway.ApiException(status=503), connection]) as stream,
patch.object(gateway.time, "sleep"),
patch.object(gateway, "drain_exec_output", return_value='{"written":4}')):
self.assertEqual({"written": 4}, gateway.execute_tool(project, request, "Bearer " + "x" * 32, False))
self.assertEqual(2, stream.call_count)
connection.write_stdin.assert_called_once()
connection.close.assert_called_once()
def test_interrupted_tool_input_is_not_replayed(self):
project = "00000000-0000-0000-0000-000000000003"
request = gateway.Tool(name="run_command", arguments={"command": "npm install"})
connection = MagicMock()
connection.write_stdin.side_effect = gateway.WebSocketException("disconnected")
with (patch.object(gateway, "TOKEN", "x" * 32),
patch.object(gateway, "ensure_workspace"),
patch.object(gateway, "stream", return_value=connection) as stream):
with self.assertRaises(gateway.HTTPException) as error:
gateway.execute_tool(project, request, "Bearer " + "x" * 32, False)
self.assertEqual((502, "WORKSPACE_RESULT_UNKNOWN"), (error.exception.status_code, error.exception.detail))
stream.assert_called_once()
connection.write_stdin.assert_called_once()
connection.close.assert_called_once()
if __name__ == "__main__":
unittest.main()