pitboard-core 0.22.0

The engine behind Pitboard: switches Claude Code, Codex and Claude Desktop between accounts by parking and restoring their logins. Its API serves Pitboard's own front ends.
Documentation
//! A tool's terms, read before its first account is added.

use super::{Result, Settled};
use crate::provider::ProviderId;
use crate::state::{self, State};

/// Whether `which`'s terms are still to be read before an account of it is added: until the
/// person has gone on past them, and never while an account of it is enrolled.
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)
}

/// That the person went on past `which`'s terms, now.
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());
    }
}