Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

align codex auth cli status output

Raine Virtad986273ac034
diff --git a/src/providers/codex/mod.rs b/src/providers/codex/mod.rs
index 7f1a2d7..c12ef4c 100644
--- a/src/providers/codex/mod.rs
+++ b/src/providers/codex/mod.rs
@@ -274,11 +274,10 @@ impl CliHandlers for CodexCli {
let store = file_store();
let manager = CodexAuthManager::new(store);
let saved = manager.persist_initial_tokens(&tokens)?;
- println!("Auth saved in {}", manager.store.auth_path());
- if let Some(ref aid) = saved.account_id {
- println!("Account: {aid}");
- }
- println!("Authentication complete");
+ print!(
+ "{}",
+ format_auth_saved_output(&manager.store.auth_path(), saved.account_id.as_deref())
+ );
Ok(())
}
@@ -287,11 +286,10 @@ impl CliHandlers for CodexCli {
let store = file_store();
let manager = CodexAuthManager::new(store);
let saved = manager.persist_initial_tokens(&tokens)?;
- println!("Auth saved in {}", manager.store.auth_path());
- if let Some(ref aid) = saved.account_id {
- println!("Account: {aid}");
- }
- println!("Authentication complete");
+ print!(
+ "{}",
+ format_auth_saved_output(&manager.store.auth_path(), saved.account_id.as_deref())
+ );
Ok(())
}
@@ -300,18 +298,12 @@ impl CliHandlers for CodexCli {
let stored = store.load_auth()?;
match stored {
Some(auth) => {
- println!("Auth path: {}", store.auth_path());
- println!("Authenticated: true");
- if let Some(ref aid) = auth.account_id {
- println!("Account: {aid}");
- }
- let remaining = auth.expires.saturating_sub(
- std::time::SystemTime::now()
- .duration_since(std::time::UNIX_EPOCH)
- .unwrap_or_default()
- .as_millis() as u64,
- ) / 1000;
- println!("Expires in {remaining}s");
+ println!(
+ "Account: {}",
+ auth.account_id.as_deref().unwrap_or("(none)")
+ );
+ println!("{}", format_expiry(auth.expires, now_ms()));
+ println!("Storage: {}", store.auth_path());
Ok(())
}
None => {
@@ -330,6 +322,40 @@ impl CliHandlers for CodexCli {
pub(crate) static CODEX_CLI: CodexCli = CodexCli;
+// ---------------------------------------------------------------------------
+// CLI helpers
+// ---------------------------------------------------------------------------
+
+fn now_ms() -> u64 {
+ std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .unwrap_or_default()
+ .as_millis() as u64
+}
+
+fn format_expiry(expires: u64, now: u64) -> String {
+ let remaining = (i128::from(expires) - i128::from(now)).div_euclid(1000);
+ let iso = time::OffsetDateTime::from_unix_timestamp_nanos(i128::from(expires) * 1_000_000)
+ .ok()
+ .and_then(|dt| {
+ let fmt = time::format_description::parse_borrowed::<2>(
+ "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z",
+ )
+ .ok()?;
+ dt.format(&fmt).ok()
+ })
+ .unwrap_or_else(|| "invalid".to_string());
+ format!("Expires: {iso} (in {remaining}s)")
+}
+
+fn format_auth_saved_output(auth_path: &str, account_id: Option<&str>) -> String {
+ let mut out = format!("Auth saved in {auth_path}\n");
+ if let Some(account_id) = account_id {
+ out.push_str(&format!("Account: {account_id}\n"));
+ }
+ out
+}
+
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
@@ -409,4 +435,39 @@ mod tests {
let result = CODEX_CLI.logout();
assert!(result.is_ok());
}
+
+ #[test]
+ fn format_auth_saved_output_with_account() {
+ assert_eq!(
+ format_auth_saved_output("/tmp/auth.json", Some("acct_1")),
+ "Auth saved in /tmp/auth.json\nAccount: acct_1\n"
+ );
+ }
+
+ #[test]
+ fn format_auth_saved_output_without_account() {
+ assert_eq!(
+ format_auth_saved_output("/tmp/auth.json", None),
+ "Auth saved in /tmp/auth.json\n"
+ );
+ }
+
+ #[test]
+ fn format_expiry_with_future_expiry() {
+ // 2100-01-01T00:00:00Z in ms
+ let expires = 4102444800000;
+ let now = 4102444790000; // 10s before
+ let output = format_expiry(expires, now);
+ assert!(output.starts_with("Expires: 2100-01-01T00:00:00.000Z (in "));
+ assert!(output.ends_with("s)"));
+ }
+
+ #[test]
+ fn format_expiry_with_past_expiry() {
+ // 2000-01-01T00:00:00Z in ms
+ let expires = 946684800000;
+ let now = 946684810000; // 10s after
+ let output = format_expiry(expires, now);
+ assert!(output.starts_with("Expires: 2000-01-01T00:00:00.000Z (in -"));
+ }
}
diff --git a/tests/codex_auth.rs b/tests/codex_auth.rs
index 41ae555..1b4b766 100644
--- a/tests/codex_auth.rs
+++ b/tests/codex_auth.rs
@@ -1,5 +1,4 @@
use assert_cmd::Command;
-use predicates::str::contains;
use tempfile::TempDir;
/// Run a codex auth command with a temp config dir that isolates
@@ -23,7 +22,19 @@ fn codex_auth_status_reads_stored_auth() -> Result<(), Box<dyn std::error::Error
auth_dir.join("auth.json"),
r#"{"access":"a","refresh":"r","expires":4102444800000,"accountId":"acct_1"}"#,
)?;
- cmd.assert().success().stdout(contains("Account: acct_1"));
+ let output = cmd.assert().success().get_output().stdout.clone();
+ let out = String::from_utf8(output)?;
+ let lines: Vec<_> = out.lines().collect();
+ assert_eq!(lines.len(), 3, "{out}");
+ assert_eq!(lines[0], "Account: acct_1");
+ assert!(
+ lines[1].starts_with("Expires: 2100-01-01T00:00:00.000Z (in "),
+ "{out}"
+ );
+ assert!(lines[1].ends_with("s)"), "{out}");
+ assert!(lines[2].starts_with("Storage: "), "{out}");
+ assert!(!out.contains("Auth path:"));
+ assert!(!out.contains("Authenticated: true"));
Ok(())
}
@@ -36,7 +47,11 @@ fn codex_auth_status_reads_legacy_account_id_key() -> Result<(), Box<dyn std::er
auth_dir.join("auth.json"),
r#"{"access":"a","refresh":"r","expires":4102444800000,"account_id":"acct_2"}"#,
)?;
- cmd.assert().success().stdout(contains("Account: acct_2"));
+ let output = cmd.assert().success().get_output().stdout.clone();
+ let out = String::from_utf8(output)?;
+ let lines: Vec<_> = out.lines().collect();
+ assert_eq!(lines.len(), 3, "{out}");
+ assert_eq!(lines[0], "Account: acct_2");
Ok(())
}
@@ -45,12 +60,12 @@ fn codex_auth_status_no_auth() -> Result<(), Box<dyn std::error::Error>> {
let (mut cmd, _temp) = codex_cmd();
let output = cmd.output()?;
assert_eq!(output.status.code(), Some(1));
- assert!(String::from_utf8(output.stdout)?.contains("Not authenticated"));
+ assert_eq!(String::from_utf8(output.stdout)?, "Not authenticated\n");
Ok(())
}
#[test]
-fn codex_auth_status_shows_auth_path() -> Result<(), Box<dyn std::error::Error>> {
+fn codex_auth_status_shows_storage_path() -> Result<(), Box<dyn std::error::Error>> {
let (mut cmd, temp) = codex_cmd();
let auth_dir = temp.path().join("codex");
std::fs::create_dir_all(&auth_dir)?;
@@ -58,13 +73,15 @@ fn codex_auth_status_shows_auth_path() -> Result<(), Box<dyn std::error::Error>>
auth_dir.join("auth.json"),
r#"{"access":"a","refresh":"r","expires":4102444800000,"accountId":"acct_3"}"#,
)?;
- cmd.assert().success().stdout(contains("Auth path:"));
+ let output = cmd.assert().success().get_output().stdout.clone();
+ let out = String::from_utf8(output)?;
+ assert!(out.contains("Storage:"), "{out}");
+ assert!(!out.contains("Auth path:"), "{out}");
Ok(())
}
#[test]
-fn codex_auth_status_no_account_id_shows_no_account_line() -> Result<(), Box<dyn std::error::Error>>
-{
+fn codex_auth_status_no_account_id_shows_none() -> Result<(), Box<dyn std::error::Error>> {
let (mut cmd, temp) = codex_cmd();
let auth_dir = temp.path().join("codex");
std::fs::create_dir_all(&auth_dir)?;
@@ -75,7 +92,31 @@ fn codex_auth_status_no_account_id_shows_no_account_line() -> Result<(), Box<dyn
let output = cmd.output()?;
let out = String::from_utf8(output.stdout)?;
assert!(output.status.success());
- assert!(out.contains("Authenticated: true"));
- assert!(!out.contains("Account:"));
+ assert!(out.contains("Account: (none)"), "{out}");
+ assert!(!out.contains("Auth path:"), "{out}");
+ assert!(!out.contains("Authenticated: true"), "{out}");
+ Ok(())
+}
+
+#[test]
+fn codex_auth_status_expired_auth_shows_negative_seconds() -> Result<(), Box<dyn std::error::Error>>
+{
+ let (mut cmd, temp) = codex_cmd();
+ let auth_dir = temp.path().join("codex");
+ std::fs::create_dir_all(&auth_dir)?;
+ std::fs::write(
+ auth_dir.join("auth.json"),
+ r#"{"access":"a","refresh":"r","expires":946684800000,"accountId":"acct_1"}"#,
+ )?;
+ let output = cmd.assert().success().get_output().stdout.clone();
+ let out = String::from_utf8(output)?;
+ let lines: Vec<_> = out.lines().collect();
+ assert_eq!(lines.len(), 3, "{out}");
+ assert!(lines[0].starts_with("Account:"));
+ assert!(
+ lines[1].starts_with("Expires: 2000-01-01T00:00:00.000Z (in -"),
+ "{out}"
+ );
+ assert!(lines[2].starts_with("Storage: "), "{out}");
Ok(())
}