|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | +use app_test_support::TestAppServer; |
| 5 | +use app_test_support::to_response; |
| 6 | +use codex_app_server_protocol::EnvironmentAddResponse; |
| 7 | +use codex_app_server_protocol::EnvironmentInfoResponse; |
| 8 | +use codex_app_server_protocol::EnvironmentShellInfo; |
| 9 | +use codex_app_server_protocol::JSONRPCError; |
| 10 | +use codex_app_server_protocol::JSONRPCErrorError; |
| 11 | +use codex_app_server_protocol::JSONRPCResponse; |
| 12 | +use codex_app_server_protocol::RequestId; |
| 13 | +use codex_utils_path_uri::PathUri; |
| 14 | +use pretty_assertions::assert_eq; |
| 15 | +use serde_json::json; |
| 16 | +use tempfile::TempDir; |
| 17 | +use tokio::net::TcpListener; |
| 18 | +use tokio::time::timeout; |
| 19 | + |
| 20 | +use super::exec_server_test_support::accept_exec_server_environment; |
| 21 | + |
| 22 | +const RPC_TIMEOUT: Duration = Duration::from_secs(10); |
| 23 | +const INVALID_REQUEST_ERROR_CODE: i64 = -32600; |
| 24 | +const INTERNAL_ERROR_CODE: i64 = -32603; |
| 25 | + |
| 26 | +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
| 27 | +async fn environment_info_returns_remote_environment_info() -> Result<()> { |
| 28 | + let listener = TcpListener::bind("127.0.0.1:0").await?; |
| 29 | + let exec_server_url = format!("ws://{}", listener.local_addr()?); |
| 30 | + let exec_server = tokio::spawn(async move { |
| 31 | + accept_exec_server_environment( |
| 32 | + listener, |
| 33 | + json!({ |
| 34 | + "shell": {"name": "zsh", "path": "/bin/zsh"}, |
| 35 | + "cwd": "file:///workspace", |
| 36 | + }), |
| 37 | + ) |
| 38 | + .await?; |
| 39 | + Ok::<_, anyhow::Error>(()) |
| 40 | + }); |
| 41 | + |
| 42 | + let codex_home = TempDir::new()?; |
| 43 | + let mut app_server = TestAppServer::new(codex_home.path()).await?; |
| 44 | + timeout(RPC_TIMEOUT, app_server.initialize()).await??; |
| 45 | + add_environment( |
| 46 | + &mut app_server, |
| 47 | + &exec_server_url, |
| 48 | + /*connect_timeout_ms*/ None, |
| 49 | + ) |
| 50 | + .await?; |
| 51 | + |
| 52 | + let request_id = app_server |
| 53 | + .send_raw_request( |
| 54 | + "environment/info", |
| 55 | + Some(json!({"environmentId": "remote-a"})), |
| 56 | + ) |
| 57 | + .await?; |
| 58 | + let response: JSONRPCResponse = timeout( |
| 59 | + RPC_TIMEOUT, |
| 60 | + app_server.read_stream_until_response_message(RequestId::Integer(request_id)), |
| 61 | + ) |
| 62 | + .await??; |
| 63 | + assert_eq!( |
| 64 | + to_response::<EnvironmentInfoResponse>(response)?, |
| 65 | + EnvironmentInfoResponse { |
| 66 | + shell: EnvironmentShellInfo { |
| 67 | + name: "zsh".to_string(), |
| 68 | + path: "/bin/zsh".to_string(), |
| 69 | + }, |
| 70 | + cwd: Some(PathUri::parse("file:///workspace")?), |
| 71 | + } |
| 72 | + ); |
| 73 | + timeout(RPC_TIMEOUT, exec_server).await???; |
| 74 | + Ok(()) |
| 75 | +} |
| 76 | + |
| 77 | +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
| 78 | +async fn environment_info_accepts_missing_cwd() -> Result<()> { |
| 79 | + let listener = TcpListener::bind("127.0.0.1:0").await?; |
| 80 | + let exec_server_url = format!("ws://{}", listener.local_addr()?); |
| 81 | + let exec_server = tokio::spawn(async move { |
| 82 | + accept_exec_server_environment( |
| 83 | + listener, |
| 84 | + json!({"shell": {"name": "zsh", "path": "/bin/zsh"}}), |
| 85 | + ) |
| 86 | + .await?; |
| 87 | + Ok::<_, anyhow::Error>(()) |
| 88 | + }); |
| 89 | + |
| 90 | + let codex_home = TempDir::new()?; |
| 91 | + let mut app_server = TestAppServer::new(codex_home.path()).await?; |
| 92 | + timeout(RPC_TIMEOUT, app_server.initialize()).await??; |
| 93 | + add_environment( |
| 94 | + &mut app_server, |
| 95 | + &exec_server_url, |
| 96 | + /*connect_timeout_ms*/ None, |
| 97 | + ) |
| 98 | + .await?; |
| 99 | + |
| 100 | + let request_id = app_server |
| 101 | + .send_raw_request( |
| 102 | + "environment/info", |
| 103 | + Some(json!({"environmentId": "remote-a"})), |
| 104 | + ) |
| 105 | + .await?; |
| 106 | + let response: JSONRPCResponse = timeout( |
| 107 | + RPC_TIMEOUT, |
| 108 | + app_server.read_stream_until_response_message(RequestId::Integer(request_id)), |
| 109 | + ) |
| 110 | + .await??; |
| 111 | + assert_eq!( |
| 112 | + to_response::<EnvironmentInfoResponse>(response)?, |
| 113 | + EnvironmentInfoResponse { |
| 114 | + shell: EnvironmentShellInfo { |
| 115 | + name: "zsh".to_string(), |
| 116 | + path: "/bin/zsh".to_string(), |
| 117 | + }, |
| 118 | + cwd: None, |
| 119 | + } |
| 120 | + ); |
| 121 | + timeout(RPC_TIMEOUT, exec_server).await???; |
| 122 | + Ok(()) |
| 123 | +} |
| 124 | + |
| 125 | +#[tokio::test] |
| 126 | +async fn environment_info_rejects_unknown_environment() -> Result<()> { |
| 127 | + let codex_home = TempDir::new()?; |
| 128 | + let mut app_server = TestAppServer::new(codex_home.path()).await?; |
| 129 | + timeout(RPC_TIMEOUT, app_server.initialize()).await??; |
| 130 | + |
| 131 | + let request_id = app_server |
| 132 | + .send_raw_request( |
| 133 | + "environment/info", |
| 134 | + Some(json!({"environmentId": "missing"})), |
| 135 | + ) |
| 136 | + .await?; |
| 137 | + let error = timeout( |
| 138 | + RPC_TIMEOUT, |
| 139 | + app_server.read_stream_until_error_message(RequestId::Integer(request_id)), |
| 140 | + ) |
| 141 | + .await??; |
| 142 | + assert_eq!( |
| 143 | + error, |
| 144 | + JSONRPCError { |
| 145 | + id: RequestId::Integer(request_id), |
| 146 | + error: JSONRPCErrorError { |
| 147 | + code: INVALID_REQUEST_ERROR_CODE, |
| 148 | + message: "unknown environment id `missing`".to_string(), |
| 149 | + data: None, |
| 150 | + }, |
| 151 | + } |
| 152 | + ); |
| 153 | + Ok(()) |
| 154 | +} |
| 155 | + |
| 156 | +#[tokio::test] |
| 157 | +async fn environment_info_reports_connection_failure() -> Result<()> { |
| 158 | + let listener = TcpListener::bind("127.0.0.1:0").await?; |
| 159 | + let exec_server_url = format!("ws://{}", listener.local_addr()?); |
| 160 | + let codex_home = TempDir::new()?; |
| 161 | + let mut app_server = TestAppServer::new(codex_home.path()).await?; |
| 162 | + timeout(RPC_TIMEOUT, app_server.initialize()).await??; |
| 163 | + add_environment(&mut app_server, &exec_server_url, Some(50)).await?; |
| 164 | + |
| 165 | + let request_id = app_server |
| 166 | + .send_raw_request( |
| 167 | + "environment/info", |
| 168 | + Some(json!({"environmentId": "remote-a"})), |
| 169 | + ) |
| 170 | + .await?; |
| 171 | + let error = timeout( |
| 172 | + RPC_TIMEOUT, |
| 173 | + app_server.read_stream_until_error_message(RequestId::Integer(request_id)), |
| 174 | + ) |
| 175 | + .await??; |
| 176 | + assert_eq!(error.error.code, INTERNAL_ERROR_CODE); |
| 177 | + assert!( |
| 178 | + error |
| 179 | + .error |
| 180 | + .message |
| 181 | + .contains("failed to get info for environment `remote-a`") |
| 182 | + ); |
| 183 | + Ok(()) |
| 184 | +} |
| 185 | + |
| 186 | +async fn add_environment( |
| 187 | + app_server: &mut TestAppServer, |
| 188 | + exec_server_url: &str, |
| 189 | + connect_timeout_ms: Option<u64>, |
| 190 | +) -> Result<()> { |
| 191 | + let request_id = app_server |
| 192 | + .send_raw_request( |
| 193 | + "environment/add", |
| 194 | + Some(json!({ |
| 195 | + "environmentId": "remote-a", |
| 196 | + "execServerUrl": exec_server_url, |
| 197 | + "connectTimeoutMs": connect_timeout_ms, |
| 198 | + })), |
| 199 | + ) |
| 200 | + .await?; |
| 201 | + let response: JSONRPCResponse = timeout( |
| 202 | + RPC_TIMEOUT, |
| 203 | + app_server.read_stream_until_response_message(RequestId::Integer(request_id)), |
| 204 | + ) |
| 205 | + .await??; |
| 206 | + let _: EnvironmentAddResponse = to_response(response)?; |
| 207 | + Ok(()) |
| 208 | +} |
0 commit comments