use crate::holder::{AppId, Holder, Location, Noun, Remedy};
use crate::host::{Listed, Os};
use std::path::Component;
pub(crate) const HOLDERS: &[Holder] = &[
Holder {
kind: "antigravity_app",
noun: Noun::One("the Antigravity app"),
location: Location::Within("Antigravity.app"),
remedy: Remedy::ReopenApp {
app: AppId::MacBundle("com.google.antigravity"),
name: "Antigravity",
},
},
Holder {
kind: "antigravity_ide",
noun: Noun::One("the Antigravity IDE"),
location: Location::Within("Antigravity IDE.app"),
remedy: Remedy::Do("quit the Antigravity IDE"),
},
Holder {
kind: "antigravity_program",
noun: Noun::Counted {
one: "other Antigravity program, such as `agy`",
many: "other Antigravity programs, such as `agy`",
},
location: Location::Anywhere,
remedy: Remedy::Restart,
},
];
const PROGRAMS: [&str; 4] = ["agy", "antigravity", "language_server", "antigravity-ide"];
const BUNDLES: [&str; 2] = ["Antigravity.app", "Antigravity IDE.app"];
pub(crate) fn holds(os: Os, one: &Listed) -> bool {
let named = |name: &str| match os {
Os::Windows => {
let name = name.to_ascii_lowercase();
let name = name.strip_suffix(".exe").unwrap_or(&name).to_owned();
PROGRAMS.contains(&name.as_str())
}
Os::MacOs | Os::Linux => PROGRAMS.contains(&name),
};
let in_a_bundle = os == Os::MacOs
&& one.path.components().any(|part| match part {
Component::Normal(name) => BUNDLES.iter().any(|bundle| name == *bundle),
_ => false,
});
in_a_bundle
|| one
.path
.file_name()
.and_then(std::ffi::OsStr::to_str)
.is_some_and(named)
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
fn at(path: &str) -> Listed {
Listed {
pid: 812,
parent: 1,
path: PathBuf::from(path),
package: None,
placed: true,
}
}
#[test]
fn every_part_of_antigravity_holds_its_login_and_nothing_else_does() {
for path in [
"/Users/a/.local/bin/agy",
"/Applications/Antigravity.app/Contents/Resources/bin/language_server",
"/Applications/Antigravity.app/Contents/MacOS/Antigravity",
"/Applications/Antigravity IDE.app/Contents/Frameworks/Code Helper.app/x",
] {
assert!(holds(Os::MacOs, &at(path)), "{path}");
}
assert!(holds(Os::Linux, &at("/home/a/.local/bin/agy")));
assert!(holds(
Os::Linux,
&at("/opt/antigravity-ide/antigravity-ide")
));
assert!(
holds(Os::Windows, &at("AGY.EXE")),
"Windows names a program in any case"
);
assert!(holds(Os::Windows, &at("language_server.exe")));
for path in [
"/usr/bin/claude",
"/Applications/Claude.app/Contents/MacOS/Claude",
"/home/a/agy-notes/vim",
"/usr/bin/agyx",
] {
assert!(!holds(Os::MacOs, &at(path)), "{path}");
assert!(!holds(Os::Linux, &at(path)), "{path}");
}
}
}
#[cfg(test)]
mod refusing {
use crate::context::ScratchMachine;
use crate::error::Error;
use crate::provider::agy::engine::Agy;
#[test]
fn antigravitys_login_moves_only_while_nothing_holds_it() {
let home = tempfile::tempdir().unwrap();
let m = ScratchMachine::at(home.path(), |ctx| ctx);
assert!(crate::switch::unheld(&m.ctx, &Agy).is_ok());
m.host.runs_at("agy", &["/Users/a/.local/bin/agy"]);
m.host.runs_at(
"language_server",
&["/Applications/Antigravity.app/Contents/Resources/bin/language_server"],
);
match crate::switch::unheld(&m.ctx, &Agy) {
Err(Error::LoginHeld { holding, .. }) => {
assert_eq!(holding.len(), 2, "{holding:?}");
assert!(holding.iter().any(|one| one.starts_with("agy (pid ")));
let said = Error::LoginHeld {
tool: crate::provider::ProviderId::Agy,
holding,
}
.to_string();
assert!(said.contains("Antigravity's login is in use by"), "{said}");
assert!(said.ends_with("Nothing has changed."), "{said}");
}
other => panic!("{other:?}"),
}
m.host.without_a_process_list();
assert!(matches!(
crate::switch::unheld(&m.ctx, &Agy),
Err(Error::AppStateUnknown {
app: "Antigravity",
..
})
));
}
#[test]
fn claude_code_and_codex_switch_whatever_runs() {
let home = tempfile::tempdir().unwrap();
let m = ScratchMachine::at(home.path(), |ctx| ctx);
m.host.without_a_process_list();
for tool in [
crate::provider::documents()
.find(|tool| tool.id() == crate::provider::ProviderId::Claude)
.unwrap(),
crate::provider::documents()
.find(|tool| tool.id() == crate::provider::ProviderId::Codex)
.unwrap(),
] {
assert!(crate::switch::unheld(&m.ctx, tool).is_ok());
}
}
}
#[cfg(test)]
mod saying {
use crate::context::ScratchMachine;
use crate::provider::ProviderId;
use crate::switch::{StillHolding, still_holding};
#[test]
fn what_holds_antigravitys_login_is_said_by_kind() {
let home = tempfile::tempdir().unwrap();
let m = ScratchMachine::at(home.path(), |ctx| ctx);
assert!(matches!(
still_holding(&m.ctx, ProviderId::Agy),
StillHolding::Nothing
));
m.host.runs_at(
"language_server",
&["/Applications/Antigravity.app/Contents/Resources/bin/language_server"],
);
m.host.runs_at("agy", &["agy", "/Users/a/.local/bin/agy"]);
let StillHolding::These(holding) = still_holding(&m.ctx, ProviderId::Agy) else {
panic!("held");
};
let kinds: Vec<&str> = holding.iter().map(|held| held.holder.kind).collect();
assert_eq!(kinds, ["antigravity_app", "antigravity_program"]);
assert_eq!(holding[1].pids.len(), 2);
assert!(ProviderId::Agy.waits_for_holders());
assert!(!ProviderId::Codex.waits_for_holders());
}
}