agentmux_launcher/
binary_resolution.rs1pub(crate) fn find_cef_binary(runtime_dir: &std::path::Path) -> std::path::PathBuf {
10 let ext = if cfg!(target_os = "windows") { ".exe" } else { "" };
11
12 let versioned = format!("agentmux-{}{}", env!("CARGO_PKG_VERSION"), ext);
13 let versioned_path = runtime_dir.join(&versioned);
14 if versioned_path.exists() {
15 return versioned_path;
16 }
17
18 if let Ok(entries) = std::fs::read_dir(runtime_dir) {
19 let prefix = "agentmux-";
20 let cef_prefix = "agentmux-cef";
21 for entry in entries.flatten() {
22 let name = entry.file_name();
23 let name = name.to_string_lossy();
24 if name.starts_with(prefix)
25 && name[prefix.len()..].starts_with(|c: char| c.is_ascii_digit())
32 && !name.starts_with(cef_prefix)
33 && !name.starts_with("agentmux-srv")
34 && !name.starts_with("agentmux-launcher")
41 && name.ends_with(ext)
42 {
43 return entry.path();
44 }
45 }
46 }
47
48 let versioned_old = format!("agentmux-cef-{}{}", env!("CARGO_PKG_VERSION"), ext);
49 let versioned_old_path = runtime_dir.join(&versioned_old);
50 if versioned_old_path.exists() {
51 return versioned_old_path;
52 }
53
54 runtime_dir.join(format!("agentmux-cef{}", ext))
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 fn touch(dir: &std::path::Path, name: &str) {
62 std::fs::write(dir.join(name), b"").unwrap();
63 }
64
65 fn host_name() -> String {
67 format!("agentmux-cef{}", if cfg!(target_os = "windows") { ".exe" } else { "" })
68 }
69
70 #[test]
73 fn a_package_helper_next_to_the_host_is_not_the_host() {
74 let dir = tempfile::tempdir().unwrap();
75 for n in ["agentmux-cef", "agentmux-launcher", "agentmux-install-update", "agentmux-srv-0.60.3-linux.x64"] {
76 touch(dir.path(), n);
77 }
78 assert_eq!(find_cef_binary(dir.path()), dir.path().join(host_name()));
79 }
80
81 #[test]
82 fn any_agentmux_helper_is_ignored() {
83 let dir = tempfile::tempdir().unwrap();
84 for n in ["agentmux-cef", "agentmux-bashwrap", "agentmux-mcp", "agentmux-install-update"] {
85 touch(dir.path(), n);
86 }
87 assert_eq!(find_cef_binary(dir.path()), dir.path().join(host_name()));
88 }
89
90 #[test]
91 fn an_older_versioned_host_is_still_found() {
92 let dir = tempfile::tempdir().unwrap();
93 for n in ["agentmux-launcher", "agentmux-0.50.1", "agentmux-install-update"] {
94 touch(dir.path(), n);
95 }
96 let ext = if cfg!(target_os = "windows") { ".exe" } else { "" };
97 let want = format!("agentmux-0.50.1{ext}");
98 if ext == ".exe" {
100 touch(dir.path(), &want);
101 }
102 assert_eq!(find_cef_binary(dir.path()), dir.path().join(want));
103 }
104
105 #[test]
106 fn the_exact_versioned_name_wins() {
107 let dir = tempfile::tempdir().unwrap();
108 let ext = if cfg!(target_os = "windows") { ".exe" } else { "" };
109 let exact = format!("agentmux-{}{ext}", env!("CARGO_PKG_VERSION"));
110 for n in [exact.as_str(), "agentmux-0.50.1", "agentmux-cef"] {
111 touch(dir.path(), n);
112 }
113 assert_eq!(find_cef_binary(dir.path()), dir.path().join(exact));
114 }
115}