|
| 1 | +""" |
| 2 | +Example usage of the agentic_test_orchestrator module. |
| 3 | +
|
| 4 | +This script demonstrates how to invoke the `run_agentic_test_orchestrator` function. |
| 5 | +Since the orchestrator relies on internal modules like `run_agentic_task` and `load_prompt_template`, |
| 6 | +this example mocks those dependencies to simulate a successful UI test generation workflow |
| 7 | +without making actual LLM calls or requiring a real GitHub issue. |
| 8 | +
|
| 9 | +Scenario: |
| 10 | + We simulate an issue where a user requests UI tests for a login page. |
| 11 | + The orchestrator will step through the 9-step process: |
| 12 | + 1. Check for duplicate test requests |
| 13 | + 2. Review codebase documentation |
| 14 | + 3. Analyze and ask clarifying questions if needed |
| 15 | + 4. Detect frontend type (web/CLI/desktop) |
| 16 | + 5. Create test plan |
| 17 | + 6. Generate UI tests |
| 18 | + 7. Run tests |
| 19 | + 8. Fix and iterate on failing tests |
| 20 | + 9. Submit PR |
| 21 | +""" |
| 22 | + |
| 23 | +import sys |
| 24 | +from pathlib import Path |
| 25 | +from unittest.mock import patch, MagicMock |
| 26 | + |
| 27 | +# Ensure the project root is in sys.path so we can import the module |
| 28 | +project_root = Path(__file__).resolve().parent.parent |
| 29 | +sys.path.append(str(project_root)) |
| 30 | + |
| 31 | +try: |
| 32 | + from pdd.agentic_test_orchestrator import run_agentic_test_orchestrator |
| 33 | +except ImportError: |
| 34 | + print("Error: Could not import 'pdd.agentic_test_orchestrator'.") |
| 35 | + print("Ensure your PYTHONPATH is set correctly or the file structure matches.") |
| 36 | + sys.exit(1) |
| 37 | + |
| 38 | + |
| 39 | +def mock_load_prompt_template(template_name: str) -> str: |
| 40 | + """ |
| 41 | + Mock implementation of load_prompt_template. |
| 42 | + Returns a dummy prompt string based on the requested template name. |
| 43 | + """ |
| 44 | + return f"MOCK PROMPT FOR: {template_name}\nContext: {{issue_content}}" |
| 45 | + |
| 46 | + |
| 47 | +def mock_run_agentic_task(instruction: str, cwd: Path, verbose: bool, quiet: bool, label: str, timeout: float = None, max_retries: int = 3): |
| 48 | + """ |
| 49 | + Mock implementation of run_agentic_task. |
| 50 | + Simulates the output of an LLM agent for each step of the 9-step UI test workflow. |
| 51 | + """ |
| 52 | + step_num = label.replace("step", "") |
| 53 | + |
| 54 | + # Default return values |
| 55 | + success = True |
| 56 | + cost = 0.15 # Simulated cost per step |
| 57 | + provider = "anthropic" |
| 58 | + output = "" |
| 59 | + |
| 60 | + if step_num == "1": |
| 61 | + output = "No duplicate test requests found. Proceeding with UI test generation." |
| 62 | + elif step_num == "2": |
| 63 | + output = """Codebase review complete: |
| 64 | + - Frontend: Next.js application in /frontend |
| 65 | + - Auth pages: /frontend/pages/auth/login.tsx, /frontend/pages/auth/register.tsx |
| 66 | + - Components: LoginForm, RegisterForm in /frontend/components/auth/ |
| 67 | + - API routes: /api/auth/login, /api/auth/register""" |
| 68 | + elif step_num == "3": |
| 69 | + output = """Requirements are clear: |
| 70 | + - Test login page functionality |
| 71 | + - Verify form validation (email format, password requirements) |
| 72 | + - Test successful and failed login scenarios |
| 73 | + - No clarification needed from author.""" |
| 74 | + elif step_num == "4": |
| 75 | + output = """Frontend detected: Next.js (React) |
| 76 | + Test framework: Playwright |
| 77 | + Base URL: http://localhost:3000 |
| 78 | + Authentication: Session-based via NextAuth.js""" |
| 79 | + elif step_num == "5": |
| 80 | + output = """Test Plan: |
| 81 | + 1. Login page renders correctly |
| 82 | + 2. Form validation - invalid email shows error |
| 83 | + 3. Form validation - password too short shows error |
| 84 | + 4. Successful login redirects to dashboard |
| 85 | + 5. Failed login shows error message |
| 86 | + 6. Remember me checkbox persists session |
| 87 | +
|
| 88 | + Estimated tests: 6 test cases |
| 89 | + Files to create: tests/e2e/login.spec.ts""" |
| 90 | + elif step_num == "6": |
| 91 | + output = """FILES_CREATED: tests/e2e/login.spec.ts |
| 92 | +
|
| 93 | + Generated Playwright test file with 6 test cases: |
| 94 | + - test('login page renders correctly') |
| 95 | + - test('shows error for invalid email') |
| 96 | + - test('shows error for short password') |
| 97 | + - test('successful login redirects to dashboard') |
| 98 | + - test('failed login shows error message') |
| 99 | + - test('remember me persists session')""" |
| 100 | + elif step_num == "7": |
| 101 | + output = """Test execution results: |
| 102 | + 6 tests total |
| 103 | + 5 passed |
| 104 | + 1 failed: 'remember me persists session' - localStorage not mocked |
| 105 | +
|
| 106 | + Overall: 83% pass rate""" |
| 107 | + elif step_num == "8": |
| 108 | + output = """Fixed failing test: |
| 109 | + - Added localStorage mock in beforeEach hook |
| 110 | + - Re-ran tests: 6/6 passed |
| 111 | +
|
| 112 | + FILES_MODIFIED: tests/e2e/login.spec.ts |
| 113 | + All tests now passing.""" |
| 114 | + elif step_num == "9": |
| 115 | + output = """PR Created: https://github.com/example/myapp/pull/456 |
| 116 | +
|
| 117 | + Title: Add UI tests for login page (#123) |
| 118 | + Branch: test/issue-123 |
| 119 | + Files: tests/e2e/login.spec.ts""" |
| 120 | + else: |
| 121 | + output = f"Unknown step executed: {step_num}" |
| 122 | + |
| 123 | + return success, output, cost, provider |
| 124 | + |
| 125 | + |
| 126 | +def main(): |
| 127 | + """Main function to run the agentic test orchestrator simulation.""" |
| 128 | + # Define dummy issue data |
| 129 | + issue_data = { |
| 130 | + "issue_url": "https://github.com/example/myapp/issues/123", |
| 131 | + "issue_content": "Create UI tests for the login page. Should test form validation, successful login, and error handling.", |
| 132 | + "repo_owner": "example", |
| 133 | + "repo_name": "myapp", |
| 134 | + "issue_number": 123, |
| 135 | + "issue_author": "test_requester", |
| 136 | + "issue_title": "Add UI tests for login page", |
| 137 | + "cwd": Path("./temp_workspace"), |
| 138 | + "verbose": True, |
| 139 | + "quiet": False, |
| 140 | + "timeout_adder": 0.0, |
| 141 | + "use_github_state": False # Disable for simulation |
| 142 | + } |
| 143 | + |
| 144 | + print("Starting Agentic UI Test Orchestrator Simulation...") |
| 145 | + print("-" * 60) |
| 146 | + |
| 147 | + # Patch the internal dependencies |
| 148 | + with patch("pdd.agentic_test_orchestrator.load_prompt_template", side_effect=mock_load_prompt_template), \ |
| 149 | + patch("pdd.agentic_test_orchestrator.run_agentic_task", side_effect=mock_run_agentic_task): |
| 150 | + |
| 151 | + # Run the orchestrator |
| 152 | + success, final_msg, total_cost, model, changed_files = run_agentic_test_orchestrator( |
| 153 | + **issue_data |
| 154 | + ) |
| 155 | + |
| 156 | + print("-" * 60) |
| 157 | + print("Simulation Complete.") |
| 158 | + print(f"Success: {success}") |
| 159 | + print(f"Final Message: {final_msg}") |
| 160 | + print(f"Total Cost: ${total_cost:.2f}") |
| 161 | + print(f"Model Used: {model}") |
| 162 | + print(f"Changed Files: {changed_files}") |
| 163 | + print("\nNext step: Review the generated tests and merge the PR.") |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == "__main__": |
| 167 | + main() |
0 commit comments