|
| 1 | +//! E2E tests for ACP mode startup and approval bridging |
| 2 | +//! |
| 3 | +//! These tests verify that: |
| 4 | +//! 1. ACP mode starts correctly when configured via wire_api = "acp" |
| 5 | +//! 2. The approval bridging infrastructure works correctly |
| 6 | +//! 3. Permission requests from ACP agents are properly displayed in the TUI |
| 7 | +
|
| 8 | +use std::time::Duration; |
| 9 | +use tui_pty_e2e::normalize_for_input_snapshot; |
| 10 | +use tui_pty_e2e::Key; |
| 11 | +use tui_pty_e2e::SessionConfig; |
| 12 | +use tui_pty_e2e::TuiSession; |
| 13 | +use tui_pty_e2e::TIMEOUT; |
| 14 | +use tui_pty_e2e::TIMEOUT_INPUT; |
| 15 | + |
| 16 | +/// Test that ACP mode starts successfully with mock-model |
| 17 | +#[test] |
| 18 | +fn test_acp_mode_startup_with_mock_agent() { |
| 19 | + let config = SessionConfig::new().with_model("mock-model".to_owned()); |
| 20 | + |
| 21 | + let mut session = |
| 22 | + TuiSession::spawn_with_config(24, 80, config).expect("Failed to spawn codex in ACP mode"); |
| 23 | + |
| 24 | + // Wait for the main prompt to appear (indicated by the chevron character) |
| 25 | + session |
| 26 | + .wait_for_text("›", TIMEOUT) |
| 27 | + .expect("ACP mode TUI should start successfully with mock agent"); |
| 28 | + |
| 29 | + std::thread::sleep(TIMEOUT_INPUT); |
| 30 | + |
| 31 | + let contents = session.screen_contents(); |
| 32 | + |
| 33 | + // Verify we're in the TUI and not stuck at an error screen |
| 34 | + assert!( |
| 35 | + contents.contains("›") && contents.contains("context left"), |
| 36 | + "Should show main prompt with context indicator in ACP mode, got: {}", |
| 37 | + contents |
| 38 | + ); |
| 39 | + |
| 40 | + // Should NOT show any ACP-related errors |
| 41 | + assert!( |
| 42 | + !contents.contains("Error") && !contents.contains("error"), |
| 43 | + "ACP mode should start without errors, got: {}", |
| 44 | + contents |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +/// Test that ACP mode can send a prompt and receive a response from mock agent |
| 49 | +#[test] |
| 50 | +fn test_acp_mode_prompt_response_flow() { |
| 51 | + let config = SessionConfig::new() |
| 52 | + .with_model("mock-model".to_owned()) |
| 53 | + .with_mock_response("Hello from ACP mock agent!"); |
| 54 | + |
| 55 | + let mut session = |
| 56 | + TuiSession::spawn_with_config(24, 80, config).expect("Failed to spawn codex in ACP mode"); |
| 57 | + |
| 58 | + // Wait for startup |
| 59 | + session |
| 60 | + .wait_for_text("›", TIMEOUT) |
| 61 | + .expect("ACP mode should start"); |
| 62 | + |
| 63 | + std::thread::sleep(TIMEOUT_INPUT); |
| 64 | + |
| 65 | + // Send a test prompt |
| 66 | + session.send_str("Test ACP prompt").unwrap(); |
| 67 | + std::thread::sleep(TIMEOUT_INPUT); |
| 68 | + session.send_key(Key::Enter).unwrap(); |
| 69 | + |
| 70 | + // Wait for the mock response |
| 71 | + session |
| 72 | + .wait_for_text("Hello from ACP mock agent!", TIMEOUT) |
| 73 | + .expect("Should receive response from mock ACP agent"); |
| 74 | +} |
| 75 | + |
| 76 | +/// Test that ACP approval requests are displayed in the TUI |
| 77 | +/// |
| 78 | +/// This test verifies the approval bridging infrastructure by: |
| 79 | +/// 1. Configuring the mock agent to request permission |
| 80 | +/// 2. Verifying the permission request appears in the TUI |
| 81 | +/// 3. Verifying user can respond to the permission request |
| 82 | +/// |
| 83 | +/// ## Prerequisites for this test to pass: |
| 84 | +/// 1. Mock agent must support MOCK_AGENT_REQUEST_PERMISSION env var |
| 85 | +/// 2. TUI must listen to AcpConnection::take_approval_receiver() |
| 86 | +/// 3. TUI must display ExecApprovalRequestEvent and send ReviewDecision back |
| 87 | +/// |
| 88 | +/// This test is marked #[ignore] until approval bridging is integrated. |
| 89 | +/// Run with: cargo test -p tui-pty-e2e -- --ignored |
| 90 | +#[test] |
| 91 | +#[ignore] |
| 92 | +fn test_acp_approval_request_displayed_in_tui() { |
| 93 | + let config = SessionConfig::new() |
| 94 | + .with_model("mock-model".to_owned()) |
| 95 | + // Configure mock agent to request permission before responding |
| 96 | + .with_agent_env("MOCK_AGENT_REQUEST_PERMISSION", "1"); |
| 97 | + |
| 98 | + let mut session = |
| 99 | + TuiSession::spawn_with_config(24, 80, config).expect("Failed to spawn codex in ACP mode"); |
| 100 | + |
| 101 | + // Wait for startup |
| 102 | + session |
| 103 | + .wait_for_text("›", TIMEOUT) |
| 104 | + .expect("ACP mode should start"); |
| 105 | + |
| 106 | + std::thread::sleep(TIMEOUT_INPUT); |
| 107 | + |
| 108 | + // Send a prompt that triggers a permission request |
| 109 | + session.send_str("Run a shell command").unwrap(); |
| 110 | + std::thread::sleep(TIMEOUT_INPUT); |
| 111 | + session.send_key(Key::Enter).unwrap(); |
| 112 | + |
| 113 | + // Wait for the approval request to appear |
| 114 | + // The TUI should display something like "ACP agent requests permission" |
| 115 | + // or show the approval popup from ExecApprovalRequestEvent |
| 116 | + let approval_appeared = session.wait_for( |
| 117 | + |screen| { |
| 118 | + screen.contains("permission") |
| 119 | + || screen.contains("approve") |
| 120 | + || screen.contains("allow") |
| 121 | + || screen.contains("deny") |
| 122 | + || screen.contains("ACP agent requests") |
| 123 | + }, |
| 124 | + Duration::from_secs(10), |
| 125 | + ); |
| 126 | + |
| 127 | + match approval_appeared { |
| 128 | + Ok(()) => { |
| 129 | + // Approval UI appeared - verify we can see the request |
| 130 | + let contents = session.screen_contents(); |
| 131 | + eprintln!("Approval request displayed:\n{}", contents); |
| 132 | + |
| 133 | + // The approval UI should show options to approve or deny |
| 134 | + assert!( |
| 135 | + contents.contains("allow") |
| 136 | + || contents.contains("approve") |
| 137 | + || contents.contains("deny") |
| 138 | + || contents.contains("reject"), |
| 139 | + "Approval UI should show allow/deny options, got: {}", |
| 140 | + contents |
| 141 | + ); |
| 142 | + } |
| 143 | + Err(e) => { |
| 144 | + // Expected to fail until approval bridging is integrated |
| 145 | + panic!( |
| 146 | + "Approval request not displayed in TUI. \ |
| 147 | + This is expected until approval bridging is integrated. \ |
| 148 | + Error: {}. Screen contents:\n{}", |
| 149 | + e, |
| 150 | + session.screen_contents() |
| 151 | + ); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +/// Test snapshot of ACP mode startup screen |
| 157 | +#[test] |
| 158 | +fn test_acp_mode_startup_snapshot() { |
| 159 | + let config = SessionConfig::new().with_model("mock-model".to_owned()); |
| 160 | + |
| 161 | + let mut session = |
| 162 | + TuiSession::spawn_with_config(24, 80, config).expect("Failed to spawn codex in ACP mode"); |
| 163 | + |
| 164 | + session |
| 165 | + .wait_for_text("›", TIMEOUT) |
| 166 | + .expect("ACP mode should start"); |
| 167 | + |
| 168 | + std::thread::sleep(TIMEOUT_INPUT); |
| 169 | + |
| 170 | + insta::assert_snapshot!( |
| 171 | + "acp_mode_startup", |
| 172 | + normalize_for_input_snapshot(session.screen_contents()) |
| 173 | + ); |
| 174 | +} |
0 commit comments