use super::{Result, Settled};
use crate::provider::ProviderId;
use crate::state::{self, State};
pub(crate) fn due(state: &State, which: ProviderId) -> bool {
!which.terms().is_empty()
&& !state.terms_read.contains_key(which.code())
&& !state.accounts.iter().any(|a| a.provider() == which)
}
pub(crate) fn read(settled: Settled, which: ProviderId) -> Result<()> {
let Settled {
_exclusive,
mut state,
ctx,
permit,
} = settled;
state.terms_read.insert(which.code().to_owned(), ctx.now());
state::save(&ctx, permit, &state)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::state::{Account, Detail};
fn an_antigravity_account() -> Account {
Account {
label: "work".into(),
id: "agy_0011223344556677".into(),
account_uuid: "work@example.com".into(),
email: "work@example.com".into(),
parked: None,
last_used_at: None,
replaced_at: None,
plan: None,
detail: Detail::Agy {},
}
}
#[test]
fn only_antigravitys_terms_are_read_and_only_before_its_first_account() {
let mut state = State::default();
for which in [ProviderId::Claude, ProviderId::Codex, ProviderId::Desktop] {
assert!(!due(&state, which), "{which}");
}
assert!(due(&state, ProviderId::Agy));
state.terms_read.insert("agy".into(), 1);
assert!(!due(&state, ProviderId::Agy), "read");
state.terms_read.clear();
state.accounts.push(an_antigravity_account());
assert!(!due(&state, ProviderId::Agy), "an account is enrolled");
}
#[test]
fn what_was_read_survives_a_round_trip_and_an_older_file_reads() {
let mut state = State::default();
state.terms_read.insert("agy".into(), 1_800_000_000);
let text = serde_json::to_string(&state).unwrap();
let back: State = serde_json::from_str(&text).unwrap();
assert_eq!(back.terms_read.get("agy"), Some(&1_800_000_000));
let older = serde_json::to_string(&State::default()).unwrap();
assert!(!older.contains("terms_read"));
let back: State = serde_json::from_str(&older).unwrap();
assert!(back.terms_read.is_empty());
}
}