Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
274 changes: 274 additions & 0 deletions crates/codex-plus-core/tests/app_paths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
use std::path::{Path, PathBuf};

use codex_plus_core::app_paths;

#[test]
fn normalize_codex_app_path_returns_none_for_empty_path() {
assert!(app_paths::normalize_codex_app_path(Path::new("")).is_none());
}

#[test]
fn normalize_codex_app_path_strips_codex_exe_filename() {
let path = Path::new("/some/dir/Codex.exe");
assert_eq!(
app_paths::normalize_codex_app_path(path),
Some(PathBuf::from("/some/dir"))
);

let lower = Path::new("/some/dir/codex.exe");
assert_eq!(
app_paths::normalize_codex_app_path(lower),
Some(PathBuf::from("/some/dir"))
);
}

#[test]
fn normalize_codex_app_path_keeps_dot_app_extension() {
let path = Path::new("/Applications/Codex.app");
assert_eq!(
app_paths::normalize_codex_app_path(path),
Some(PathBuf::from("/Applications/Codex.app"))
);
}

#[test]
fn normalize_codex_app_path_returns_dir_when_exists() {
let temp = tempfile::tempdir().unwrap();
let dir = temp.path().join("codex-app");
std::fs::create_dir_all(&dir).unwrap();

assert_eq!(app_paths::normalize_codex_app_path(&dir), Some(dir.clone()));
}

#[test]
fn build_codex_executable_adds_exe_for_directory() {
let temp = tempfile::tempdir().unwrap();
let dir = temp.path().join("app");
std::fs::create_dir_all(&dir).unwrap();

let exe = app_paths::build_codex_executable(&dir);
assert_eq!(exe, dir.join("codex.exe"));
}

#[test]
fn build_codex_executable_uses_macos_path_for_dot_app() {
let path = Path::new("/Applications/Codex.app");
let exe = app_paths::build_codex_executable(path);
assert_eq!(
exe,
PathBuf::from("/Applications/Codex.app/Contents/MacOS/Codex")
);
}

#[test]
fn build_codex_executable_prefers_uppercase_when_exists() {
let temp = tempfile::tempdir().unwrap();
let dir = temp.path().join("app");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("Codex.exe"), b"").unwrap();

let exe = app_paths::build_codex_executable(&dir);
assert_eq!(exe, dir.join("Codex.exe"));
}

#[test]
fn find_latest_codex_app_dir_picks_highest_version() {
let temp = tempfile::tempdir().unwrap();
let root = temp.path();

let v1 = root.join("OpenAI.Codex_1.0.0_x64__abc123");
let v2 = root.join("OpenAI.Codex_2.3.1_x64__abc123");
let v0 = root.join("OpenAI.Codex_0.5.0_x64__abc123");

for dir in [&v1, &v2, &v0] {
std::fs::create_dir_all(dir).unwrap();
}

let result = app_paths::find_latest_codex_app_dir(root).unwrap();
assert_eq!(result, v2);
}

#[test]
fn find_latest_codex_app_dir_uses_nested_app_dir_when_present() {
let temp = tempfile::tempdir().unwrap();
let root = temp.path();

let pkg = root.join("OpenAI.Codex_1.0.0_x64__abc123");
let app_dir = pkg.join("app");
std::fs::create_dir_all(&app_dir).unwrap();

let result = app_paths::find_latest_codex_app_dir(root).unwrap();
assert_eq!(result, app_dir);
}

#[test]
fn find_latest_codex_app_dir_returns_none_for_empty_root() {
let temp = tempfile::tempdir().unwrap();
assert!(app_paths::find_latest_codex_app_dir(temp.path()).is_none());
}

#[test]
fn find_latest_codex_app_dir_returns_none_for_missing_root() {
assert!(app_paths::find_latest_codex_app_dir(Path::new("/nonexistent/path")).is_none());
}

#[test]
fn find_latest_codex_app_dir_from_roots_picks_highest_across_roots() {
let temp = tempfile::tempdir().unwrap();
let root1 = temp.path().join("root1");
let root2 = temp.path().join("root2");

let v1 = root1.join("OpenAI.Codex_1.0.0_x64__abc123");
let v2 = root2.join("OpenAI.Codex_3.0.0_x64__abc123");

for dir in [&v1, &v2] {
std::fs::create_dir_all(dir).unwrap();
}

let result = app_paths::find_latest_codex_app_dir_from_roots(&[root1, root2]).unwrap();
assert_eq!(result, v2);
}

#[test]
fn user_data_candidates_from_appends_expected_variants() {
let local = Path::new("/Users/test/AppData/Local");
let roaming = Path::new("/Users/test/AppData/Roaming");

let candidates = app_paths::user_data_candidates_from(Some(local), Some(roaming));

assert!(candidates.contains(&local.join("OpenAI").join("Codex")));
assert!(candidates.contains(&local.join("OpenAI.Codex")));
assert!(candidates.contains(&local.join("Codex")));
assert!(candidates.contains(&roaming.join("OpenAI").join("Codex")));
assert_eq!(candidates.len(), 6);
}

#[test]
fn user_data_candidates_from_handles_none_inputs() {
assert!(app_paths::user_data_candidates_from(None, None).is_empty());

let local = Path::new("/local");
let candidates = app_paths::user_data_candidates_from(Some(local), None);
assert_eq!(candidates.len(), 3);
}

#[test]
fn latest_appx_install_location_from_output_extracts_first_non_empty_line() {
let output = "\n C:\\Program Files\\WindowsApps\\OpenAI.Codex_1.0.0 \n\n";
assert_eq!(
app_paths::latest_appx_install_location_from_output(output),
Some("C:\\Program Files\\WindowsApps\\OpenAI.Codex_1.0.0".to_string())
);
}

#[test]
fn latest_appx_install_location_from_output_returns_none_for_empty() {
assert!(app_paths::latest_appx_install_location_from_output("").is_none());
assert!(app_paths::latest_appx_install_location_from_output(" \n \n").is_none());
}

#[test]
fn codex_app_version_extracts_version_from_package_dir() {
let temp = tempfile::tempdir().unwrap();
let pkg = temp.path().join("OpenAI.Codex_2.5.10_x64__abc123");
let app_dir = pkg.join("app");
std::fs::create_dir_all(&app_dir).unwrap();

assert_eq!(
app_paths::codex_app_version(&app_dir),
Some("2.5.10".to_string())
);
}

#[test]
fn codex_app_version_reads_macos_plist() {
let temp = tempfile::tempdir().unwrap();
let app = temp.path().join("Codex.app");
let contents_dir = app.join("Contents");
std::fs::create_dir_all(&contents_dir).unwrap();
std::fs::write(
contents_dir.join("Info.plist"),
r#"<?xml version="1.0"?>
<plist version="1.0">
<dict>
<key>CFBundleShortVersionString</key>
<string>3.1.4</string>
</dict>
</plist>"#,
)
.unwrap();

assert_eq!(
app_paths::codex_app_version(&app),
Some("3.1.4".to_string())
);
}

#[test]
fn codex_app_version_returns_none_for_non_package_dir() {
let temp = tempfile::tempdir().unwrap();
assert!(app_paths::codex_app_version(temp.path()).is_none());
}

#[test]
fn packaged_app_user_model_id_builds_expected_format() {
let path = Path::new("/WindowsApps/OpenAI.Codex_2.5.0_x64__abc123def/app");
assert_eq!(
app_paths::packaged_app_user_model_id(path),
Some("OpenAI.Codex_abc123def!App".to_string())
);
}

#[test]
fn packaged_app_user_model_id_returns_none_for_non_package_path() {
let path = Path::new("/some/random/path");
assert!(app_paths::packaged_app_user_model_id(path).is_none());
}

#[test]
fn find_macos_codex_app_finds_app_in_search_root() {
let temp = tempfile::tempdir().unwrap();
let app_dir = temp.path().join("Codex.app");
std::fs::create_dir_all(&app_dir).unwrap();

let result = app_paths::find_macos_codex_app(&[temp.path().to_path_buf()]);
assert_eq!(result, Some(app_dir));
}

#[test]
fn find_macos_codex_app_finds_alternative_names() {
let temp = tempfile::tempdir().unwrap();
let app_dir = temp.path().join("OpenAI Codex.app");
std::fs::create_dir_all(&app_dir).unwrap();

let result = app_paths::find_macos_codex_app(&[temp.path().to_path_buf()]);
assert_eq!(result, Some(app_dir));
}

#[test]
fn find_macos_codex_app_returns_none_when_not_found() {
let temp = tempfile::tempdir().unwrap();
assert!(app_paths::find_macos_codex_app(&[temp.path().to_path_buf()]).is_none());
}

#[test]
fn find_macos_codex_app_accepts_direct_app_path() {
let temp = tempfile::tempdir().unwrap();
let app_dir = temp.path().join("CustomCodex.app");
std::fs::create_dir_all(&app_dir).unwrap();

let result = app_paths::find_macos_codex_app(&[app_dir.clone()]);
assert_eq!(result, Some(app_dir));
}

#[test]
fn codex_beta_package_is_also_recognized() {
let temp = tempfile::tempdir().unwrap();
let root = temp.path();

let beta = root.join("OpenAI.CodexBeta_1.0.0_x64__beta123");
std::fs::create_dir_all(&beta).unwrap();

let result = app_paths::find_latest_codex_app_dir(root).unwrap();
assert_eq!(result, beta);
}
Loading