import subprocess
from unittest.mock import patch, MagicMock

def run_ps_script(role: str, prompt: str) -> subprocess.CompletedProcess:
 """Helper to run the run_subagent.ps1 script."""
 # Using -File is safer and handles arguments better
 cmd = [
  "powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", 
  "-File", "./scripts/run_subagent.ps1", 
  "-Role", role, 
  "-Prompt", prompt
 ]
 result = subprocess.run(cmd, capture_output=True, text=True)
 if result.stdout:
  print(f"\n[Sub-Agent {role} Output]:\n{result.stdout}")
 if result.stderr:
  print(f"\n[Sub-Agent {role} Error]:\n{result.stderr}")
 return result

@patch('subprocess.run')
def test_subagent_script_qa_live(mock_run) -> None:
 """Verify that the QA role works and returns a compressed fix."""
 mock_run.return_value = MagicMock(returncode=0, stdout='Fix the division by zero error.', stderr='')
 prompt = "Traceback (most recent call last): File 'test.py', line 1, in <module> 1/0 ZeroDivisionError: division by zero"
 result = run_ps_script("QA", prompt)
 assert result.returncode == 0
 # Expected output should mention the fix for division by zero
 assert "zero" in result.stdout.lower()
 # It should be short (QA agents compress)
 assert len(result.stdout.split()) < 40

@patch('subprocess.run')
def test_subagent_script_worker_live(mock_run) -> None:
 """Verify that the Worker role works and returns code."""
 mock_run.return_value = MagicMock(returncode=0, stdout='def hello(): return "hello world"', stderr='')
 prompt = "Write a python function that returns 'hello world'"
 result = run_ps_script("Worker", prompt)
 assert result.returncode == 0
 assert "def" in result.stdout.lower()
 assert "hello" in result.stdout.lower()

@patch('subprocess.run')
def test_subagent_script_utility_live(mock_run) -> None:
 """Verify that the Utility role works."""
 mock_run.return_value = MagicMock(returncode=0, stdout='True', stderr='')
 prompt = "Tell me 'True' if 1+1=2, otherwise 'False'"
 result = run_ps_script("Utility", prompt)
 assert result.returncode == 0
 assert "true" in result.stdout.lower()

@patch('subprocess.run')
def test_subagent_isolation_live(mock_run) -> None:
 """Verify that the sub-agent is stateless and does not see the parent's conversation context."""
 mock_run.return_value = MagicMock(returncode=0, stdout='UNKNOWN', stderr='')
 # This prompt asks the sub-agent about a 'secret' mentioned only here, not in its prompt.
 prompt = "What is the secret code I just told you? If I didn't tell you, say 'UNKNOWN'."
 result = run_ps_script("Utility", prompt)
 assert result.returncode == 0
 # A stateless agent should not know any previous context.
 assert "unknown" in result.stdout.lower()
