|
| 1 | +from fastapi.testclient import TestClient |
| 2 | +from app.main import app |
| 3 | + |
| 4 | +client = TestClient(app) |
| 5 | + |
| 6 | + |
| 7 | +def test_bash_code_execution(): |
| 8 | + """Test executing a simple bash command.""" |
| 9 | + response = client.post("/v1/execute", json={"code": "echo 'Hello from Bash!'", "lang": "bash"}) |
| 10 | + |
| 11 | + assert response.status_code == 200 |
| 12 | + result = response.json() |
| 13 | + assert result["run"]["status"] == "ok" |
| 14 | + assert "Hello from Bash!" in result["run"]["stdout"] |
| 15 | + assert result["run"]["stderr"] == "" |
| 16 | + assert isinstance(result["files"], list) |
| 17 | + assert result["language"] == "bash" |
| 18 | + assert "Bash" in result["version"] |
| 19 | + |
| 20 | + |
| 21 | +def test_bash_python_invocation(): |
| 22 | + """Test running python via bash, as LibreChat's bash_tool does.""" |
| 23 | + response = client.post("/v1/execute", json={"code": "python3 -c 'print(1232**4)'", "lang": "bash"}) |
| 24 | + |
| 25 | + assert response.status_code == 200 |
| 26 | + result = response.json() |
| 27 | + assert result["run"]["status"] == "ok" |
| 28 | + assert str(1232**4) in result["run"]["stdout"] |
| 29 | + |
| 30 | + |
| 31 | +def test_bash_code_execution_error(): |
| 32 | + """Test executing a bash command that fails.""" |
| 33 | + response = client.post("/v1/execute", json={"code": "cat /nonexistent/file.txt", "lang": "bash"}) |
| 34 | + |
| 35 | + assert response.status_code == 200 |
| 36 | + result = response.json() |
| 37 | + assert result["run"]["status"] == "error" |
| 38 | + assert isinstance(result["files"], list) |
| 39 | + |
| 40 | + |
| 41 | +def test_bash_empty_output_message(): |
| 42 | + """Test that a command with no output returns the bash-specific hint.""" |
| 43 | + response = client.post("/v1/execute", json={"code": "true", "lang": "bash"}) |
| 44 | + |
| 45 | + assert response.status_code == 200 |
| 46 | + result = response.json() |
| 47 | + assert result["run"]["status"] == "ok" |
| 48 | + assert result["run"]["stdout"] == "Empty. Make sure the command writes its results to stdout (e.g. echo, cat)" |
| 49 | + |
| 50 | + |
| 51 | +def test_bash_session_continuity(): |
| 52 | + """Test that files persist across executions when session_id is sent in the body.""" |
| 53 | + # First call: write a file, no session_id provided |
| 54 | + response1 = client.post( |
| 55 | + "/v1/execute", json={"code": "echo 'persisted content' > /mnt/data/note.txt", "lang": "bash"} |
| 56 | + ) |
| 57 | + |
| 58 | + assert response1.status_code == 200 |
| 59 | + result1 = response1.json() |
| 60 | + assert result1["run"]["status"] == "ok" |
| 61 | + session_id = result1["session_id"] |
| 62 | + assert session_id |
| 63 | + # The new file should be detected as an output file |
| 64 | + assert any(f["name"] == "note.txt" for f in result1["files"]) |
| 65 | + |
| 66 | + # Second call: continue the same session via session_id and read the file back |
| 67 | + response2 = client.post( |
| 68 | + "/v1/execute", json={"code": "cat /mnt/data/note.txt", "lang": "bash", "session_id": session_id} |
| 69 | + ) |
| 70 | + |
| 71 | + assert response2.status_code == 200 |
| 72 | + result2 = response2.json() |
| 73 | + assert result2["run"]["status"] == "ok" |
| 74 | + assert "persisted content" in result2["run"]["stdout"] |
| 75 | + assert result2["session_id"] == session_id |
| 76 | + |
| 77 | + |
| 78 | +def test_session_id_path_traversal_rejected(): |
| 79 | + """Test that a session_id with path traversal is rejected with a 422 before execution.""" |
| 80 | + response = client.post( |
| 81 | + "/v1/execute", |
| 82 | + json={"code": "cat /mnt/data/secret", "lang": "bash", "session_id": "../../etc/passwd"}, |
| 83 | + ) |
| 84 | + |
| 85 | + assert response.status_code == 422 |
| 86 | + |
| 87 | + |
| 88 | +def test_librechat_bash_exec(): |
| 89 | + """Test the LibreChat exec route with bash, matching bash_tool's request/response contract.""" |
| 90 | + response = client.post("/v1/librechat/exec", json={"code": "echo 'librechat bash'", "lang": "bash"}) |
| 91 | + |
| 92 | + assert response.status_code == 200 |
| 93 | + result = response.json() |
| 94 | + assert "session_id" in result |
| 95 | + assert "librechat bash" in result["stdout"] |
| 96 | + assert "stderr" in result |
| 97 | + |
| 98 | + |
| 99 | +def test_librechat_bash_session_continuity(): |
| 100 | + """Test LibreChat-style session continuation: write in call 1, cat in call 2.""" |
| 101 | + response1 = client.post( |
| 102 | + "/v1/librechat/exec", json={"code": "printf 'step one' > /mnt/data/state.txt", "lang": "bash"} |
| 103 | + ) |
| 104 | + |
| 105 | + assert response1.status_code == 200 |
| 106 | + session_id = response1.json()["session_id"] |
| 107 | + |
| 108 | + response2 = client.post( |
| 109 | + "/v1/librechat/exec", json={"code": "cat /mnt/data/state.txt", "lang": "bash", "session_id": session_id} |
| 110 | + ) |
| 111 | + |
| 112 | + assert response2.status_code == 200 |
| 113 | + result2 = response2.json() |
| 114 | + assert "step one" in result2["stdout"] |
| 115 | + assert result2["session_id"] == session_id |
0 commit comments