diff --git a/codex-rs/app-server/tests/suite/v2/account.rs b/codex-rs/app-server/tests/suite/v2/account.rs index c8bb9eccbe..3f4d2836f6 100644 --- a/codex-rs/app-server/tests/suite/v2/account.rs +++ b/codex-rs/app-server/tests/suite/v2/account.rs @@ -2278,6 +2278,20 @@ async fn login_account_chatgpt_device_code_returns_error_when_disabled() -> Resu #[tokio::test] async fn login_account_chatgpt_device_code_succeeds_and_notifies() -> Result<()> { + assert_device_login(DeviceLoginStorage::Writable).await +} + +#[tokio::test] +async fn device_code_login_reports_failed_save_with_client_originator() -> Result<()> { + assert_device_login(DeviceLoginStorage::BlockSave).await +} + +enum DeviceLoginStorage { + Writable, + BlockSave, +} + +async fn assert_device_login(storage: DeviceLoginStorage) -> Result<()> { let codex_home = TempDir::new()?; let mock_server = MockServer::start().await; create_config_toml( @@ -2289,7 +2303,12 @@ async fn login_account_chatgpt_device_code_succeeds_and_notifies() -> Result<()> }, )?; write_models_cache(codex_home.path()).await?; - let collector = super::auth_storage_originator::configure_collector(codex_home.path()).await?; + let collector = match storage { + DeviceLoginStorage::Writable => None, + DeviceLoginStorage::BlockSave => { + Some(super::auth_storage_originator::configure_collector(codex_home.path()).await?) + } + }; mock_device_code_usercode(&mock_server, /*interval_seconds*/ 0).await; mock_device_code_token_success(&mock_server).await; @@ -2320,6 +2339,11 @@ async fn login_account_chatgpt_device_code_succeeds_and_notifies() -> Result<()> }) .await?; + if collector.is_some() { + // A successful account switch revokes in-flight exports for the old account. + // Exercise attribution on the same async device-login save path without switching. + std::fs::create_dir(codex_home.path().join("auth.json"))?; + } let request_id = mcp.send_login_account_chatgpt_device_code_request().await?; let login: LoginAccountResponse = timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(request_id)).await??; @@ -2344,6 +2368,15 @@ async fn login_account_chatgpt_device_code_succeeds_and_notifies() -> Result<()> bail!("unexpected notification: {parsed:?}"); }; assert_eq!(payload.login_id, Some(login_id)); + if let Some(collector) = collector { + assert!(!payload.success); + assert!(payload.error.is_some()); + return super::auth_storage_originator::assert_saved_originators( + &collector, + &["codex_vscode"], + ) + .await; + } assert_eq!(payload.success, true); assert_eq!(payload.error, None); @@ -2362,7 +2395,6 @@ async fn login_account_chatgpt_device_code_succeeds_and_notifies() -> Result<()> codex_home.path().join("auth.json").exists(), "auth.json should be created when device code login succeeds" ); - super::auth_storage_originator::assert_saved_originators(&collector, &["codex_vscode"]).await?; Ok(()) } diff --git a/codex-rs/app-server/tests/suite/v2/auth_storage_originator_tests.rs b/codex-rs/app-server/tests/suite/v2/auth_storage_originator_tests.rs index 86817d5fe5..d0493656d0 100644 --- a/codex-rs/app-server/tests/suite/v2/auth_storage_originator_tests.rs +++ b/codex-rs/app-server/tests/suite/v2/auth_storage_originator_tests.rs @@ -21,7 +21,9 @@ use codex_app_server_protocol::TurnStartResponse; use codex_app_server_protocol::TurnStatus; use codex_app_server_protocol::UserInput; use codex_login::AuthCredentialsStoreMode; +use codex_login::AuthKeyringBackendKind; use codex_login::REFRESH_TOKEN_URL_OVERRIDE_ENV_VAR; +use codex_login::login_with_api_key; use core_test_support::responses; use pretty_assertions::assert_eq; use serde_json::Value; @@ -123,9 +125,23 @@ pub(super) async fn assert_saved_originators( #[tokio::test] async fn concurrent_connections_export_their_own_storage_originator() -> Result<()> { let codex_home = TempDir::new()?; + // An account switch revokes in-flight network requests, including delta exports. + // Keep the account fixed while measuring writes from the three different clients. + login_with_api_key( + codex_home.path(), + "sk-test-key", + AuthCredentialsStoreMode::File, + AuthKeyringBackendKind::default(), + )?; + // Storage metrics enable the turn-cost probe. Keep it on loopback, and skip the + // online model catalog refresh this three-client login test does not exercise. + let backend = MockServer::start().await; std::fs::write( codex_home.path().join("config.toml"), - "cli_auth_credentials_store = \"file\"\n", + format!( + "cli_auth_credentials_store = \"file\"\nfeatures.api_key_model_discovery = false\nchatgpt_base_url = {:?}\n", + backend.uri() + ), )?; let collector = configure_collector(codex_home.path()).await?; let (_process, address) = spawn_websocket_server(codex_home.path()).await?;