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"));
}
#[test]
fn a_send_removes_the_words_it_deposited_and_never_the_ones_after_them() {
let mut drafts = Drafts::default();
let key = DraftKey::Message("c-1".into());
drafts.set(key.clone(), "ping".into());
drafts.sent(&key, "ping");
assert_eq!(drafts.text(&key), "", "the ordinary case: nothing is left");
drafts.set(key.clone(), "ping and hurry".into());
drafts.sent(&key, "ping");
assert_eq!(
drafts.text(&key),
" and hurry",
"what was typed after Enter is still theirs, verbatim"
);
drafts.set(key.clone(), "something else".into());
drafts.sent(&key, "ping");
assert_eq!(drafts.text(&key), "something else");
let empty = DraftKey::Message("c-2".into());
drafts.sent(&empty, "ping");
assert_eq!(drafts.text(&empty), "");
}
#[test]
fn a_draft_is_carried_to_the_key_its_conversation_acquired() {
let mut drafts = Drafts::default();
let bare = start_in("/w");
let named = DraftKey::Message("CourtyardRooftop".into());
let id = DraftKey::Message("c-2".into());
drafts.set(bare.clone(), "half a thought".into());
drafts.carry(&bare, &named);
assert_eq!(drafts.text(&named), "half a thought");
assert_eq!(drafts.text(&bare), "", "and it is not in two places");
drafts.carry(&bare, &named);
assert_eq!(drafts.text(&named), "half a thought");
drafts.carry(&named, &id);
assert_eq!(drafts.text(&id), "half a thought");
drafts.carry(&id, &id);
assert_eq!(drafts.text(&id), "half a thought");
drafts.set(named.clone(), "typed since".into());
drafts.carry(&named, &id);
assert_eq!(drafts.text(&id), "half a thought");
assert_eq!(drafts.text(&named), "typed since");
}