2026-03-23 14:08:45 -04:00
|
|
|
use isolang::Language;
|
|
|
|
|
use morethantext::{Action, Field, Include, MoreThanText, Name, Path, TestMoreThanText};
|
|
|
|
|
use std::{collections::HashSet, sync::mpsc::RecvTimeoutError};
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
fn doc_name() -> Name {
|
|
|
|
|
Name::english("session")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn lang_name() -> Name {
|
|
|
|
|
Name::english("language")
|
|
|
|
|
}
|
2026-03-22 13:57:12 -04:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn are_session_ids_unique() {
|
|
|
|
|
let count = 10;
|
|
|
|
|
let mtt = MoreThanText::new();
|
|
|
|
|
let mut ids: HashSet<String> = HashSet::new();
|
|
|
|
|
for _ in 0..count {
|
|
|
|
|
let client = mtt.client();
|
|
|
|
|
ids.insert(client.session_id());
|
|
|
|
|
}
|
|
|
|
|
assert_eq!(ids.len(), count, "ids = {:?}", ids);
|
|
|
|
|
}
|
2026-03-23 14:08:45 -04:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn can_existing_sessions_be_used() {
|
|
|
|
|
let mtt = MoreThanText::new();
|
|
|
|
|
let client1 = mtt.client();
|
|
|
|
|
let id = client1.session_id();
|
|
|
|
|
drop(client1);
|
|
|
|
|
let client2 = mtt.client_with_session(id.clone(), None);
|
|
|
|
|
assert_eq!(client2.session_id(), id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn does_expired_session_ids_return_new() {
|
|
|
|
|
let id = Uuid::new_v4().to_string();
|
|
|
|
|
let mtt = MoreThanText::new();
|
|
|
|
|
let client = mtt.client_with_session(id.clone(), None);
|
|
|
|
|
assert_ne!(client.session_id(), id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn does_bad_id_string_get_new() {
|
|
|
|
|
let id = "Not uuid".to_string();
|
|
|
|
|
let mtt = MoreThanText::new();
|
|
|
|
|
let client = mtt.client_with_session(id.clone(), None);
|
|
|
|
|
assert_ne!(client.session_id(), id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn can_new_clients_set_langauge() {
|
|
|
|
|
let lang = Language::from_639_1("fr").unwrap();
|
|
|
|
|
let mut test_env = TestMoreThanText::new();
|
|
|
|
|
let mtt = test_env.get_morethantext();
|
|
|
|
|
let path = Path::new(
|
|
|
|
|
Include::All,
|
|
|
|
|
Include::Just(doc_name().into()),
|
|
|
|
|
Include::Just(Action::OnAddition),
|
|
|
|
|
);
|
|
|
|
|
test_env.register_channel(vec![path]);
|
|
|
|
|
mtt.client_with_language(lang.clone());
|
|
|
|
|
let result = test_env.get_trigger_records(Action::OnAddition);
|
|
|
|
|
let rec = result.iter().last().unwrap();
|
|
|
|
|
assert_eq!(rec.get(&lang_name()).unwrap(), lang.into());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn is_lanaguage_set_for_expired_session() {
|
|
|
|
|
let lang = Language::from_639_1("fr").unwrap();
|
|
|
|
|
let mut test_env = TestMoreThanText::new();
|
|
|
|
|
let mtt = test_env.get_morethantext();
|
|
|
|
|
let path = Path::new(
|
|
|
|
|
Include::All,
|
|
|
|
|
Include::Just(doc_name().into()),
|
|
|
|
|
Include::Just(Action::OnAddition),
|
|
|
|
|
);
|
|
|
|
|
test_env.register_channel(vec![path]);
|
|
|
|
|
mtt.client_with_session(Uuid::new_v4().to_string(), Some(lang));
|
|
|
|
|
let result = test_env.get_trigger_records(Action::OnAddition);
|
|
|
|
|
let rec = result.iter().last().unwrap();
|
|
|
|
|
assert_eq!(rec.get(&lang_name()).unwrap(), lang.into());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn is_lanaguage_set_for_bad_session() {
|
|
|
|
|
let lang = Language::from_639_1("de").unwrap();
|
|
|
|
|
let mut test_env = TestMoreThanText::new();
|
|
|
|
|
let mtt = test_env.get_morethantext();
|
|
|
|
|
let path = Path::new(
|
|
|
|
|
Include::All,
|
|
|
|
|
Include::Just(doc_name().into()),
|
|
|
|
|
Include::Just(Action::OnAddition),
|
|
|
|
|
);
|
|
|
|
|
test_env.register_channel(vec![path]);
|
|
|
|
|
mtt.client_with_session("bad".to_string(), Some(lang));
|
|
|
|
|
let result = test_env.get_trigger_records(Action::OnAddition);
|
|
|
|
|
let rec = result.iter().last().unwrap();
|
|
|
|
|
assert_eq!(rec.get(&lang_name()).unwrap(), lang.into());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn do_existing_sessions_keep_language_unchanged() {
|
|
|
|
|
let lang1 = Language::from_639_1("de").unwrap();
|
|
|
|
|
let lang2 = Language::from_639_1("fr").unwrap();
|
|
|
|
|
let mut test_env = TestMoreThanText::new();
|
|
|
|
|
let mtt = test_env.get_morethantext();
|
|
|
|
|
let client = mtt.client_with_language(lang1);
|
|
|
|
|
let id = client.session_id();
|
|
|
|
|
drop(client);
|
|
|
|
|
let path = Path::new(
|
|
|
|
|
Include::All,
|
|
|
|
|
Include::Just(doc_name().into()),
|
|
|
|
|
Include::Just(Action::OnUpdate),
|
|
|
|
|
);
|
|
|
|
|
test_env.register_channel(vec![path]);
|
|
|
|
|
mtt.client_with_session(id.clone(), Some(lang2));
|
|
|
|
|
let result = test_env.get_trigger_records(Action::OnUpdate);
|
|
|
|
|
let rec = result.iter().last().unwrap();
|
|
|
|
|
assert_eq!(rec.get(&lang_name()).unwrap(), lang1.into());
|
|
|
|
|
}
|