From 735d4ab89c48bc509b2fffc21fb21332dd697404 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Wed, 6 May 2026 11:21:22 +0200 Subject: [PATCH] test: isolate app-server-client in-process test state (#21328) ## Why The in-process `app-server-client` tests were still building their configs from the ambient `codex_home` and letting the embedded app server create its own state DB when `state_db` was absent. That matters because in-process startup falls back to `init_state_db_from_config(...)` in that case, so tests can otherwise share persisted state instead of getting isolated fixtures: [`app-server/src/in_process.rs`](https://github.com/openai/codex/blob/a98623511ba433154ec811fc63091617f5945438/codex-rs/app-server/src/in_process.rs#L368-L373). ## What changed - Give each in-process test client its own temporary `codex_home`. - Initialize the matching state DB from that per-client config and pass it into the client explicitly. - Keep the temp directory alive for the lifetime of the test client through a small `TestClient` wrapper. - Add `tempfile` as a dev dependency for the new harness. The updated setup lives in [`app-server-client/src/lib.rs`](https://github.com/openai/codex/blob/35c1133d45d10931914dbb88a1246a195d025ff6/codex-rs/app-server-client/src/lib.rs#L982-L1055). ## Testing - Existing `codex-app-server-client` tests continue to exercise the updated in-process client path through the isolated helper. --- codex-rs/Cargo.lock | 1 + codex-rs/app-server-client/Cargo.toml | 1 + codex-rs/app-server-client/src/lib.rs | 61 ++++++++++++++++++++++++--- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 9bdf2e7182..1dbb51cc1f 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -1944,6 +1944,7 @@ dependencies = [ "pretty_assertions", "serde", "serde_json", + "tempfile", "tokio", "tokio-tungstenite", "toml 0.9.11+spec-1.1.0", diff --git a/codex-rs/app-server-client/Cargo.toml b/codex-rs/app-server-client/Cargo.toml index d9c1ade097..fee29db47e 100644 --- a/codex-rs/app-server-client/Cargo.toml +++ b/codex-rs/app-server-client/Cargo.toml @@ -33,4 +33,5 @@ url = { workspace = true } [dev-dependencies] pretty_assertions = { workspace = true } serde_json = { workspace = true } +tempfile = { workspace = true } tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } diff --git a/codex-rs/app-server-client/src/lib.rs b/codex-rs/app-server-client/src/lib.rs index 0911cc448d..a22eed1c80 100644 --- a/codex-rs/app-server-client/src/lib.rs +++ b/codex-rs/app-server-client/src/lib.rs @@ -954,9 +954,13 @@ mod tests { use codex_app_server_protocol::ToolRequestUserInputParams; use codex_app_server_protocol::ToolRequestUserInputQuestion; use codex_core::config::ConfigBuilder; + use codex_core::init_state_db_from_config; use futures::SinkExt; use futures::StreamExt; use pretty_assertions::assert_eq; + use std::ops::Deref; + use std::path::Path; + use tempfile::TempDir; use tokio::net::TcpListener; use tokio::time::Duration; use tokio::time::timeout; @@ -975,19 +979,59 @@ mod tests { } } + async fn build_test_config_for_codex_home(codex_home: &Path) -> Config { + match ConfigBuilder::default() + .codex_home(codex_home.to_path_buf()) + .build() + .await + { + Ok(config) => config, + Err(_) => Config::load_default_with_cli_overrides_for_codex_home( + codex_home.to_path_buf(), + Vec::new(), + ) + .await + .expect("default config should load"), + } + } + + struct TestClient { + _codex_home: TempDir, + client: InProcessAppServerClient, + } + + impl Deref for TestClient { + type Target = InProcessAppServerClient; + + fn deref(&self) -> &Self::Target { + &self.client + } + } + + impl TestClient { + async fn shutdown(self) -> IoResult<()> { + self.client.shutdown().await + } + } + async fn start_test_client_with_capacity( session_source: SessionSource, channel_capacity: usize, - ) -> InProcessAppServerClient { - InProcessAppServerClient::start(InProcessClientStartArgs { + ) -> TestClient { + let codex_home = TempDir::new().expect("temp dir"); + let config = Arc::new(build_test_config_for_codex_home(codex_home.path()).await); + let state_db = init_state_db_from_config(config.as_ref()) + .await + .expect("state db should initialize for in-process test"); + let client = InProcessAppServerClient::start(InProcessClientStartArgs { arg0_paths: Arg0DispatchPaths::default(), - config: Arc::new(build_test_config().await), + config, cli_overrides: Vec::new(), loader_overrides: LoaderOverrides::default(), cloud_requirements: CloudRequirementsLoader::default(), feedback: CodexFeedback::new(), log_db: None, - state_db: None, + state_db: Some(state_db), environment_manager: Arc::new(EnvironmentManager::default_for_tests()), config_warnings: Vec::new(), session_source, @@ -999,10 +1043,15 @@ mod tests { channel_capacity, }) .await - .expect("in-process app-server client should start") + .expect("in-process app-server client should start"); + + TestClient { + _codex_home: codex_home, + client, + } } - async fn start_test_client(session_source: SessionSource) -> InProcessAppServerClient { + async fn start_test_client(session_source: SessionSource) -> TestClient { start_test_client_with_capacity(session_source, DEFAULT_IN_PROCESS_CHANNEL_CAPACITY).await }