from afk_developer.scheduler.tools import shell def test_run_command_executes_ordinary_safe_commands(tmp_path) -> None: result = shell.run_command("echo hello-afk", str(tmp_path)) assert "hello-afk" in result def test_run_command_blocks_dangerous_commands_without_invoking_the_real_shell( monkeypatch, tmp_path ) -> None: calls = [] def fake_run(*args, **kwargs): calls.append((args, kwargs)) raise AssertionError("subprocess.run must not be called for a policy-rejected command") monkeypatch.setattr(shell.subprocess, "run", fake_run) result = shell.run_command("rm -rf /", str(tmp_path)) assert result.startswith("COMMAND_POLICY_REJECTED:") assert calls == [] def test_run_command_still_executes_after_a_prior_rejection(monkeypatch, tmp_path) -> None: # Confirms the policy check does not consume the timeout/subprocess machinery # or otherwise leave run_command in a bad state for the next (safe) call. rejected = shell.run_command("shutdown /s /t 0", str(tmp_path)) assert rejected.startswith("COMMAND_POLICY_REJECTED:") allowed = shell.run_command("echo still-works", str(tmp_path)) assert "still-works" in allowed def test_run_command_rejects_an_unscoped_pytest_run_for_a_scoped_task(monkeypatch, tmp_path) -> None: def fake_run(*_args, **_kwargs): raise AssertionError("subprocess.run must not be called for a policy-rejected command") monkeypatch.setattr(shell.subprocess, "run", fake_run) result = shell.run_command( "python -m pytest -q", str(tmp_path), disallow_unscoped_pytest=True, ) assert result.startswith("TASK_COMMAND_POLICY_REJECTED:")