use super::{Check, ok, warn};
use crate::context::Context;
use crate::provider::agy::{document, live::AgyLogin, paths};
use crate::provider::{Provider, ProviderId};
use crate::state::State;
use crate::store::{Backend, RawStore};
use std::path::PathBuf;
pub(crate) struct AgyFacts {
pub(crate) program: Option<PathBuf>,
pub(crate) enrolled: usize,
pub(crate) login: Result<Option<(String, Backend)>, String>,
pub(crate) holding: Option<Vec<String>>,
pub(crate) terms_read: bool,
}
impl AgyFacts {
pub(crate) fn here(&self) -> bool {
self.program.is_some() || self.enrolled > 0
}
}
pub(super) fn agy_facts(ctx: &Context, state: Option<&State>) -> AgyFacts {
let mut facts = AgyFacts {
program: crate::provider::program_of(ctx, ProviderId::Agy),
enrolled: state.map_or(0, |state| {
state
.accounts
.iter()
.filter(|account| account.provider() == ProviderId::Agy)
.count()
}),
login: Ok(None),
holding: Some(Vec::new()),
terms_read: state.is_some_and(|state| state.terms_read.contains_key("agy")),
};
if !facts.here() {
return facts;
}
let store = AgyLogin::of(ctx);
facts.login = store
.read(&paths::service(ctx))
.map_err(|e| e.to_string())
.map(|found| {
let found: serde_json::Value = serde_json::from_str(&found?).ok()?;
Some((document::email(&found)?.to_owned(), store.kind()))
});
facts.holding = crate::provider::agy::engine::Agy
.held_by()
.and_then(
|held| match crate::quiet::open(ctx, |one, _| (held.holds)(ctx.os, one)) {
crate::quiet::Open::No => Some(Vec::new()),
crate::quiet::Open::Yes(listed) => Some(crate::quiet::named(&listed)),
crate::quiet::Open::Unknown(_) => None,
},
);
facts
}
pub(super) fn judge_agy(facts: &AgyFacts) -> Vec<Check> {
let mut checks = Vec::new();
checks.push(match &facts.program {
Some(program) => ok("agy_program", "agy", program.display().to_string()),
None => warn(
"agy_program",
"agy",
"not found",
"Adding an Antigravity account runs agy's own sign-in. Install agy from \
https://antigravity.google/docs/getting-started/, or set PITBOARD_AGY to its path.",
),
});
checks.push(match &facts.login {
Ok(Some((email, kept))) => ok(
"agy_login",
"Antigravity login",
format!(
"{email}'s, kept {}",
match kept {
Backend::File => "in agy's token files",
_ => "in this computer's credential store",
}
),
),
Ok(None) => ok("agy_login", "Antigravity login", "nothing is signed in"),
Err(why) => warn(
"agy_login",
"Antigravity login",
why.clone(),
"Pitboard switches Antigravity only once its login reads. Unlock the store it is \
kept in, or sign in to Antigravity again.",
),
});
checks.push(match &facts.holding {
Some(holding) if holding.is_empty() => ok(
"agy_holding",
"what holds it",
"nothing of Antigravity runs",
),
Some(holding) => warn(
"agy_holding",
"what holds it",
format!("{} runs", crate::words::listed(holding.clone())),
"A switch waits until each is closed: quit Antigravity and its IDE, and every agy.",
),
None => warn(
"agy_holding",
"what holds it",
"the list of processes could not be read",
"A switch is refused until it can tell that nothing of Antigravity runs.",
),
});
checks.push(ok(
"agy_terms",
"Google's terms",
format!(
"{}: {} and {}",
if facts.terms_read || facts.enrolled > 0 {
"read before the first account"
} else {
"shown before the first account"
},
crate::words::ANTIGRAVITY_TERMS_URL,
crate::words::ANTIGRAVITY_FAQ_URL
),
));
checks
}
#[cfg(test)]
mod tests {
use super::*;
use crate::doctor::Level;
fn facts() -> AgyFacts {
AgyFacts {
program: Some(PathBuf::from("/home/x/.local/bin/agy")),
enrolled: 1,
login: Ok(Some(("work@example.com".into(), Backend::Keychain))),
holding: Some(Vec::new()),
terms_read: true,
}
}
#[test]
fn a_healthy_antigravity_says_its_program_login_holders_and_terms() {
let checks = judge_agy(&facts());
let codes: Vec<&str> = checks.iter().map(|c| c.code).collect();
assert_eq!(
codes,
["agy_program", "agy_login", "agy_holding", "agy_terms"]
);
assert!(checks.iter().all(|c| c.level == Level::Ok));
assert!(checks[1].detail.contains("work@example.com"));
assert!(
checks[3]
.detail
.contains("https://antigravity.google/terms")
);
}
#[test]
fn what_holds_it_and_a_missing_agy_are_warned_of() {
let checks = judge_agy(&AgyFacts {
program: None,
holding: Some(vec!["agy (pid 812)".into()]),
..facts()
});
assert_eq!(checks[0].level, Level::Warn);
assert_eq!(checks[2].level, Level::Warn);
assert!(checks[2].detail.contains("agy (pid 812)"));
let unknown = judge_agy(&AgyFacts {
holding: None,
..facts()
});
assert_eq!(unknown[2].level, Level::Warn);
}
#[test]
fn a_machine_without_antigravity_is_not_judged_on_it() {
let none = AgyFacts {
program: None,
enrolled: 0,
..facts()
};
assert!(!none.here());
assert!(facts().here());
}
}