waybetter-for-x/apps/desktop/src-tauri/src/profiles.rs
2026-10-01 06:01:13 +07:00

45 lines
2.4 KiB
Rust

//! Account profiles (wayBetter host) with X's account rules: each X tab runs in one profile, every
//! non-default profile has its own WebView data directory, and the X login inside a profile is read
//! from X's own account button and validated with `site::SITE.accounts`.
pub use crate::site::valid_handle;
pub use waybetter_host::profiles::{
data_dir, orphan_dirs, store_identifier, valid_profile_id, AccountSeen, Profile, ProfileSettings, DEFAULT_PROFILE,
PROFILES_DIR,
};
/// The X account signed in inside a profile (`@handle` without the `@`, name, pbs.twimg.com avatar).
pub type XAccount = waybetter_host::profiles::Account;
#[cfg(test)]
mod tests {
use super::*;
use crate::site::SITE;
fn account(handle: &str, avatar: &str) -> XAccount {
XAccount { handle: handle.into(), name: "Fishies".into(), avatar: Some(avatar.into()), signed_in: false }
}
#[test]
fn x_logins_are_validated_with_x_rules() {
let mut settings = ProfileSettings::default();
for bad in ["", "has space", "waytoolonghandle_", "a/b", "a.b"] {
assert!(!settings.observe("default", AccountSeen::SignedIn(account(bad, "https://pbs.twimg.com/a.jpg")), &SITE.accounts), "{bad:?}");
}
assert!(settings.observe("default", AccountSeen::SignedIn(account("@play_fishies", "https://evil.test/x.png")), &SITE.accounts));
let seen = settings.profiles[0].account.clone().unwrap();
assert_eq!((seen.handle.as_str(), seen.avatar, seen.signed_in), ("play_fishies", None, true));
assert!(settings.observe("default", AccountSeen::SignedIn(account("play_fishies", "https://pbs.twimg.com/profile_images/1/a_x96.jpg")), &SITE.accounts));
assert!(settings.profiles[0].account.as_ref().unwrap().avatar.is_some());
}
#[test]
fn profile_json_shape_is_unchanged() {
// profiles.json written by earlier builds must keep loading.
let raw = r##"{"profiles":[{"id":"default","name":"Main","color":"#1d9bf0","account":{"handle":"a","name":"A","avatar":"https://pbs.twimg.com/x.jpg","signedIn":true}}],"active":"default"}"##;
let settings: ProfileSettings = serde_json::from_str(raw).unwrap();
let settings = settings.validated(&SITE.accounts).unwrap();
assert_eq!(serde_json::to_string(&settings).unwrap(), raw);
assert_eq!(ProfileSettings::default().profiles[0].color, "#1d9bf0");
}
}