use super::*;
use crate::{PreferencePatch, PreferencesAnswer};
fn tagged(room: &str, began: u64, tags: &[&str]) -> SessionTags {
SessionTags {
city: Address::parse("harbour").unwrap(),
room: Address::parse(room).unwrap(),
began: Seq::new(began),
tags: tags.iter().map(|tag| Tag::parse(tag).unwrap()).collect(),
}
}
#[test]
fn a_tag_is_one_word_of_letters_digits_dash_or_underscore() {
for good in [
"bug",
"重构",
"v2",
"follow-up",
"two_words",
&"x".repeat(TAG_MAX),
] {
assert!(Tag::parse(good).is_ok(), "{good}");
}
for bad in [
"",
"two words",
"a/b",
"#x",
"tab\t",
&"x".repeat(TAG_MAX + 1),
] {
assert!(Tag::parse(bad).is_err(), "{bad:?}");
}
assert!(serde_json::from_str::<Tag>(r#""two words""#).is_err());
}
#[test]
fn a_tags_patch_replaces_one_sessions_set_and_keeps_the_list_ordered() {
let mut held = PreferencesAnswer::default();
held.apply(PreferencePatch::Tags(tagged("lab/b", 9, &["x"])));
held.apply(PreferencePatch::Tags(tagged(
"hall/mayor",
3,
&["pin", "bug", "pin"],
)));
held.apply(PreferencePatch::Tags(tagged("lab/b", 9, &["y"])));
assert_eq!(
held.tags,
vec![
tagged("hall/mayor", 3, &["bug", "pin"]),
tagged("lab/b", 9, &["y"])
]
);
held.apply(PreferencePatch::Tags(tagged("hall/mayor", 3, &[])));
assert_eq!(held.tags, vec![tagged("lab/b", 9, &["y"])]);
}
#[test]
fn the_city_is_part_of_a_sessions_name() {
let mut held = PreferencesAnswer::default();
let other = SessionTags {
city: Address::parse("fjord").unwrap(),
..tagged("hall/mayor", 3, &["b"])
};
held.apply(PreferencePatch::Tags(tagged("hall/mayor", 3, &["a"])));
held.apply(PreferencePatch::Tags(other.clone()));
assert_eq!(held.tags, vec![other, tagged("hall/mayor", 3, &["a"])]);
}