from pathlib import Path import pytest from amo.workspace import WORKSPACE_ROOT, compile_workspace_python, list_workspace, read_workspace_file, write_workspace_file, resolve_workspace_path def test_workspace_write_read_and_backup(tmp_path, monkeypatch): monkeypatch.setattr("amo.workspace.WORKSPACE_ROOT", tmp_path) tmp_path.mkdir(exist_ok=True) first = write_workspace_file("demo.py", "def f():\n return 1\n") assert first.startswith("تم حفظ") assert "return 1" in read_workspace_file("demo.py") second = write_workspace_file("demo.py", "def f():\n return 2\n") assert "النسخة السابقة محفوظة" in second assert list((tmp_path / ".amo_backups").iterdir()) def test_path_traversal_is_rejected(tmp_path, monkeypatch): monkeypatch.setattr("amo.workspace.WORKSPACE_ROOT", tmp_path) tmp_path.mkdir(exist_ok=True) with pytest.raises(ValueError): resolve_workspace_path("../escape.txt") with pytest.raises(ValueError): resolve_workspace_path("/tmp/secret.txt") def test_syntax_check_reports_invalid_python(tmp_path, monkeypatch): monkeypatch.setattr("amo.workspace.WORKSPACE_ROOT", tmp_path) tmp_path.mkdir(exist_ok=True) (tmp_path / "bad.py").write_text("def broken(:\n pass\n", encoding="utf-8") result = compile_workspace_python(".") assert "SyntaxError" in result or "invalid syntax" in result def test_workspace_unittest_runs_inseparate_process(tmp_path, monkeypatch): from amo.workspace import run_workspace_unittests monkeypatch.setattr("amo.workspace.WORKSPACE_ROOT", tmp_path) (tmp_path / "test_demo.py").write_text( "import unittest\nclass Demo(unittest.TestCase):\n def test_ok(self):\n self.assertEqual(2 + 2, 4)\n", encoding="utf-8", ) result = run_workspace_unittests(".") assert "Ran 1 test" in result assert "OK" in result def test_linux_tool_is_allowlisted(monkeypatch, tmp_path): from amo.workspace import linux_safe_task monkeypatch.setattr("amo.workspace.WORKSPACE_ROOT", tmp_path) result = linux_safe_task("python_version") assert "python_version" in result denied = linux_safe_task("rm -rf /") assert "غير مسموحة" in denied