use super::{DraftKey, Drafts};
use std::path::PathBuf;
fn start_in(path: &str) -> DraftKey {
DraftKey::NewConversation(Some(PathBuf::from(path)))
}
fn composer(path: &str, selected: Option<&str>) -> DraftKey {
DraftKey::composer(Some(PathBuf::from(path)), selected.map(str::to_owned))
}
#[test]
fn an_untyped_target_reads_empty() {
let drafts = Drafts::default();
assert!(drafts.is_empty());
assert_eq!(drafts.text(&start_in("/w")), "");
assert_eq!(drafts.text(&DraftKey::Message("c-1".into())), "");
}
#[test]
fn each_target_holds_its_own_draft() {
let mut drafts = Drafts::default();
let start = start_in("/w");
let other = start_in("/other");
let msg = DraftKey::Message("c-1".into());
drafts.set(start.clone(), "ship the goal".into());
assert_eq!(
drafts.text(&msg),
"",
"the message box is its own, and empty"
);
assert_eq!(drafts.text(&other), "", "so is another workspace's");
drafts.set(msg.clone(), "ping".into());
assert_eq!(
drafts.text(&start),
"ship the goal",
"switching back restores the goal"
);
assert_eq!(drafts.text(&msg), "ping");
}
#[test]
fn a_send_clears_only_its_own_key() {
let mut drafts = Drafts::default();
let start = start_in("/w");
let msg = DraftKey::Message("c-1".into());
drafts.set(start.clone(), "ship the goal".into());
drafts.set(msg.clone(), "ping".into());
drafts.set(msg.clone(), String::new());
assert_eq!(drafts.text(&msg), "");
assert_eq!(drafts.text(&start), "ship the goal");
assert!(!drafts.is_empty(), "the other target still holds its draft");
drafts.set(start, String::new());
assert!(drafts.is_empty());
}
#[test]
fn the_composer_key_follows_the_selection() {
assert_eq!(
composer("/w", Some("c-1")),
DraftKey::Message("c-1".into()),
"a selection is a message target"
);
assert_eq!(composer("/w", None), start_in("/w"));
assert_eq!(
DraftKey::composer(None, None),
DraftKey::NewConversation(None),
"the empty world is the general case with no workspace"
);
assert_ne!(composer("/w", None), composer("/other", None));
}
#[test]
fn drafts_clone_eq_and_debug() {
let mut drafts = Drafts::default();
let key = DraftKey::Message("c-1".into());
drafts.set(key.clone(), "ping".into());
assert_eq!(drafts.clone(), drafts);
assert!(format!("{drafts:?}").contains("ping"));
assert!(format!("{key:?}").contains("c-1"));
}