Isolate auth storage attribution tests from account switches (#62131)

## Why

Switching accounts revokes in-flight metric exports, which can interrupt
the auth storage events these app-server tests use to verify client
attribution.

## What changed

- Add a device-code login test that blocks the credential save by making
  `auth.json` a directory. Verify that login reports the failure and that
  the storage metric carries the `codex_vscode` originator. Keep the
  successful login and notification checks in their own test.
- Seed the concurrent-client test with the API key all three clients use
  so their writes keep the same account. Point the turn-cost probe at a
  local mock server and disable online model discovery.

GitOrigin-RevId: e735c2e0f81c6e58fd545943b47ff7bbd11339d7
This commit is contained in:
Dylan Hurd
2026-11-17 23:45:23 +00:00
committed by copyberry
parent 7066a55a7b
commit c90234de95
2 changed files with 51 additions and 3 deletions
+34 -2
View File
@@ -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(())
}
@@ -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?;