2026-02-06 12:06:51 -05:00
|
|
|
pub mod action;
|
2026-02-06 13:07:34 -05:00
|
|
|
mod document;
|
2025-07-04 10:25:37 -04:00
|
|
|
mod message;
|
2025-12-24 17:54:25 -05:00
|
|
|
mod mtterror;
|
2026-02-18 13:08:47 -05:00
|
|
|
pub mod name;
|
2026-01-30 13:58:55 -05:00
|
|
|
mod queue;
|
2024-05-05 23:18:42 -04:00
|
|
|
|
2026-02-13 12:00:39 -05:00
|
|
|
use document::{Clock, CreateDoc, Session};
|
2026-03-20 09:31:56 -04:00
|
|
|
use isolang::Language;
|
2026-02-12 10:18:53 -05:00
|
|
|
use message::{wrapper::Message, MessageAction};
|
2026-01-30 13:58:55 -05:00
|
|
|
use queue::{
|
2026-02-22 00:21:57 -05:00
|
|
|
data_director::{RegMsg, Register},
|
2026-01-30 13:58:55 -05:00
|
|
|
router::Queue,
|
|
|
|
|
};
|
2026-02-19 14:26:29 -05:00
|
|
|
use std::{
|
2026-03-05 11:13:18 -05:00
|
|
|
sync::mpsc::{channel, Receiver, RecvTimeoutError, Sender},
|
2026-02-19 14:26:29 -05:00
|
|
|
time::Duration,
|
|
|
|
|
};
|
2025-04-19 07:57:16 -04:00
|
|
|
use uuid::Uuid;
|
2025-02-10 08:05:59 -05:00
|
|
|
|
2026-03-16 13:30:55 -04:00
|
|
|
pub use action::*;
|
|
|
|
|
pub use document::MissingTranslation;
|
2026-02-12 22:49:19 -05:00
|
|
|
pub use mtterror::{ErrorID, MTTError};
|
2026-02-09 19:28:22 -05:00
|
|
|
pub use name::{Name, NameType};
|
2026-02-22 00:21:57 -05:00
|
|
|
pub use queue::data_director::{Include, Path};
|
2026-02-09 19:28:22 -05:00
|
|
|
|
2025-12-24 17:54:25 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod support_tests {
|
2026-02-25 10:16:50 -05:00
|
|
|
use super::*;
|
2025-12-24 17:54:25 -05:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
pub static TIMEOUT: Duration = Duration::from_millis(500);
|
2026-02-25 10:16:50 -05:00
|
|
|
|
|
|
|
|
pub fn random_name() -> Name {
|
|
|
|
|
Name::english(Uuid::new_v4().to_string().as_str())
|
|
|
|
|
}
|
2025-12-24 17:54:25 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-19 14:26:29 -05:00
|
|
|
static TIMEOUT: Duration = Duration::from_secs(10);
|
|
|
|
|
|
2025-02-22 10:53:05 -05:00
|
|
|
#[derive(Clone)]
|
2025-03-27 07:18:19 -04:00
|
|
|
pub struct MoreThanText {
|
2025-12-17 12:14:06 -05:00
|
|
|
queue: Queue,
|
2025-03-27 07:18:19 -04:00
|
|
|
}
|
2025-02-22 10:53:05 -05:00
|
|
|
|
2025-03-26 17:42:34 -04:00
|
|
|
impl MoreThanText {
|
|
|
|
|
pub fn new() -> Self {
|
2025-12-23 22:15:49 -05:00
|
|
|
let queue = Queue::new();
|
2026-02-22 00:21:57 -05:00
|
|
|
let mut output = Self {
|
|
|
|
|
queue: queue.clone(),
|
|
|
|
|
};
|
2025-04-10 13:42:43 -04:00
|
|
|
Clock::start(queue.clone());
|
2025-12-17 12:14:06 -05:00
|
|
|
CreateDoc::start(queue.clone());
|
2026-02-22 00:21:57 -05:00
|
|
|
output
|
|
|
|
|
.create_document(Session::document_definition())
|
|
|
|
|
.unwrap();
|
2026-02-19 14:26:29 -05:00
|
|
|
output
|
2025-02-11 11:33:54 -05:00
|
|
|
}
|
|
|
|
|
|
2026-03-20 09:31:56 -04:00
|
|
|
fn new_session(lang: Option<Language>) -> ClientAction {
|
|
|
|
|
let mut output = Addition::new(Session::doc_names()[0].clone());
|
|
|
|
|
match lang {
|
|
|
|
|
Some(data) => {
|
|
|
|
|
let name = Session::language_field_names()[0].clone();
|
|
|
|
|
let field: Field = data.into();
|
|
|
|
|
output.add_field(name, field);
|
|
|
|
|
}
|
|
|
|
|
None => {}
|
|
|
|
|
}
|
|
|
|
|
output.into()
|
2026-02-19 08:59:48 -05:00
|
|
|
}
|
|
|
|
|
|
2026-03-20 09:31:56 -04:00
|
|
|
fn recursive_message_request<CA>(&mut self, action: CA, lang: Option<Language>) -> Uuid
|
2026-02-19 08:59:48 -05:00
|
|
|
where
|
2026-03-20 09:31:56 -04:00
|
|
|
CA: Into<ClientAction>,
|
2026-02-19 08:59:48 -05:00
|
|
|
{
|
|
|
|
|
match self.records(action) {
|
|
|
|
|
Ok(data) => {
|
2025-12-17 12:14:06 -05:00
|
|
|
if data.len() == 0 {
|
2026-03-20 09:31:56 -04:00
|
|
|
self.recursive_message_request(MoreThanText::new_session(lang), lang)
|
2025-12-17 12:14:06 -05:00
|
|
|
} else {
|
|
|
|
|
let rec = data.iter().last().unwrap();
|
2026-02-19 08:59:48 -05:00
|
|
|
match rec.get(Name::english("id")).unwrap() {
|
|
|
|
|
Field::Uuid(id) => id,
|
|
|
|
|
_ => unreachable!("should always return uuid"),
|
2025-12-17 12:14:06 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-20 09:31:56 -04:00
|
|
|
Err(_) => self.recursive_message_request(MoreThanText::new_session(lang), lang),
|
2025-04-19 07:57:16 -04:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-22 08:31:25 -04:00
|
|
|
|
2026-03-20 09:31:56 -04:00
|
|
|
pub fn validate_session(&mut self, session: Option<String>, lang: Option<Language>) -> Uuid {
|
2025-12-17 12:14:06 -05:00
|
|
|
let action = match session {
|
|
|
|
|
Some(data) => match Uuid::try_from(data.as_str()) {
|
|
|
|
|
Ok(id) => {
|
2026-02-09 19:28:22 -05:00
|
|
|
let mut query = Query::new(Session::doc_names()[0].clone());
|
2025-12-17 12:14:06 -05:00
|
|
|
let mut calc = Calculation::new(Operand::Equal);
|
2025-12-23 22:15:49 -05:00
|
|
|
calc.add_value(CalcValue::Existing(FieldType::Uuid))
|
|
|
|
|
.unwrap();
|
|
|
|
|
calc.add_value(id).unwrap();
|
2025-12-17 12:14:06 -05:00
|
|
|
query.add(Name::english("id"), calc);
|
|
|
|
|
query.into()
|
|
|
|
|
}
|
2026-03-20 09:31:56 -04:00
|
|
|
Err(_) => MoreThanText::new_session(lang),
|
2025-12-17 12:14:06 -05:00
|
|
|
},
|
2026-03-20 09:31:56 -04:00
|
|
|
None => MoreThanText::new_session(lang),
|
2025-12-17 12:14:06 -05:00
|
|
|
};
|
2026-03-20 09:31:56 -04:00
|
|
|
self.recursive_message_request(action, lang)
|
2025-12-17 12:14:06 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-18 12:00:39 -05:00
|
|
|
pub fn records<UA>(&mut self, request: UA) -> Result<Records, MTTError>
|
2026-02-06 12:06:51 -05:00
|
|
|
where
|
2026-03-10 10:54:29 -04:00
|
|
|
UA: Into<ClientAction>,
|
2026-02-06 12:06:51 -05:00
|
|
|
{
|
2026-02-18 12:00:39 -05:00
|
|
|
let req = request.into();
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
let sender_id = self.queue.add_sender(tx);
|
|
|
|
|
let doc_id = req.doc_name().clone();
|
|
|
|
|
let msg = Message::new(req);
|
|
|
|
|
let msg_id = msg.get_message_id();
|
|
|
|
|
let paths = [
|
|
|
|
|
Path::new(
|
|
|
|
|
Include::Just(msg_id.clone()),
|
|
|
|
|
Include::Just(doc_id.clone()),
|
|
|
|
|
Include::Just(Action::Records),
|
|
|
|
|
),
|
|
|
|
|
Path::new(
|
|
|
|
|
Include::Just(msg_id.clone()),
|
2026-02-25 13:44:43 -05:00
|
|
|
Include::All,
|
2026-02-18 12:00:39 -05:00
|
|
|
Include::Just(Action::Error),
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
for path in paths.iter() {
|
|
|
|
|
let reg_msg = Register::new(sender_id.clone(), RegMsg::AddRoute(path.clone()));
|
|
|
|
|
self.queue.send(Message::new(reg_msg));
|
2026-02-25 13:44:43 -05:00
|
|
|
let result = rx.recv().unwrap();
|
|
|
|
|
let action = result.get_action();
|
|
|
|
|
match action {
|
|
|
|
|
MsgAction::Register(status) => match status.get_msg() {
|
|
|
|
|
RegMsg::Error(err) => {
|
|
|
|
|
let mut error = err.clone();
|
|
|
|
|
error.add_parent(ErrorID::Document(msg.doc_name().clone()));
|
|
|
|
|
self.queue.remove_sender(&sender_id);
|
|
|
|
|
return Err(error);
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
2026-02-27 08:36:22 -05:00
|
|
|
},
|
2026-02-25 13:44:43 -05:00
|
|
|
_ => unreachable!("got {:?} should have been a registry message", action),
|
|
|
|
|
}
|
2026-02-18 12:00:39 -05:00
|
|
|
}
|
|
|
|
|
self.queue.send(msg);
|
2026-02-19 14:26:29 -05:00
|
|
|
let output = match rx.recv_timeout(TIMEOUT) {
|
|
|
|
|
Ok(data) => match data.get_action() {
|
|
|
|
|
MsgAction::Records(data) => Ok(data.clone()),
|
|
|
|
|
MsgAction::Error(err) => Err(err.clone()),
|
2026-02-22 00:21:57 -05:00
|
|
|
_ => unreachable!("should only receive records or errors"),
|
|
|
|
|
},
|
2026-02-25 10:16:50 -05:00
|
|
|
Err(_) => Err(MTTError::new(ErrorID::TimeOut)),
|
2026-02-19 14:26:29 -05:00
|
|
|
};
|
2026-02-18 12:00:39 -05:00
|
|
|
self.queue.remove_sender(&sender_id);
|
2026-02-19 14:26:29 -05:00
|
|
|
output
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn create_document(&mut self, docdef: DocDef) -> Result<(), MTTError> {
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
let sender_id = self.queue.add_sender(tx);
|
|
|
|
|
let msg = Message::new(docdef);
|
|
|
|
|
let msg_id = msg.get_message_id();
|
|
|
|
|
let paths = [
|
|
|
|
|
Path::new(
|
|
|
|
|
Include::Just(msg_id.clone()),
|
|
|
|
|
Include::All,
|
|
|
|
|
Include::Just(Action::DocumentCreated),
|
|
|
|
|
),
|
|
|
|
|
Path::new(
|
|
|
|
|
Include::Just(msg_id.clone()),
|
|
|
|
|
Include::All,
|
|
|
|
|
Include::Just(Action::Error),
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
for path in paths.iter() {
|
|
|
|
|
let reg_msg = Register::new(sender_id.clone(), RegMsg::AddRoute(path.clone()));
|
|
|
|
|
self.queue.send(Message::new(reg_msg));
|
|
|
|
|
rx.recv().unwrap(); // Wait for completion.
|
2026-02-18 12:00:39 -05:00
|
|
|
}
|
2026-02-19 14:26:29 -05:00
|
|
|
self.queue.send(msg);
|
|
|
|
|
let output = match rx.recv_timeout(TIMEOUT) {
|
|
|
|
|
Ok(data) => match data.get_action() {
|
|
|
|
|
MsgAction::DocumentCreated => Ok(()),
|
|
|
|
|
MsgAction::Error(err) => Err(err.clone()),
|
2026-02-22 00:21:57 -05:00
|
|
|
_ => unreachable!("should only receive records or errors"),
|
|
|
|
|
},
|
2026-02-25 10:16:50 -05:00
|
|
|
Err(_) => Err(MTTError::new(ErrorID::TimeOut)),
|
2026-02-19 14:26:29 -05:00
|
|
|
};
|
|
|
|
|
self.queue.remove_sender(&sender_id);
|
|
|
|
|
output
|
2026-02-06 12:06:51 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-03 11:37:58 -05:00
|
|
|
pub fn get_document(&self, name: &str, id: &str) -> Result<String, MTTError> {
|
|
|
|
|
if name == "page" {
|
|
|
|
|
Ok("something".to_string())
|
|
|
|
|
} else {
|
2026-02-25 10:16:50 -05:00
|
|
|
Err(MTTError::new(ErrorID::DocumentNotFound))
|
2025-04-25 14:02:40 -04:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-05 23:18:42 -04:00
|
|
|
}
|
2026-02-19 10:39:03 -05:00
|
|
|
|
|
|
|
|
pub struct TestMoreThanText {
|
|
|
|
|
mtt: MoreThanText,
|
|
|
|
|
queue: Queue,
|
2026-03-05 11:13:18 -05:00
|
|
|
channel: Option<Receiver<Message>>,
|
2026-02-19 10:39:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TestMoreThanText {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
let mut mtt = MoreThanText::new();
|
|
|
|
|
let queue = mtt.queue.clone();
|
|
|
|
|
Self {
|
|
|
|
|
mtt: mtt,
|
|
|
|
|
queue: queue,
|
2026-03-05 11:13:18 -05:00
|
|
|
channel: None,
|
2026-02-19 10:39:03 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 11:13:18 -05:00
|
|
|
pub fn get_morethantext(&self) -> MoreThanText {
|
|
|
|
|
self.mtt.clone()
|
2026-02-22 00:21:57 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-19 10:39:03 -05:00
|
|
|
pub fn send_time_pulse(&self) {
|
|
|
|
|
let msg = Clock::gen_message();
|
|
|
|
|
self.queue.send(msg);
|
|
|
|
|
}
|
2026-02-19 14:26:29 -05:00
|
|
|
|
2026-03-05 11:13:18 -05:00
|
|
|
pub fn register_channel(&mut self, paths: Vec<Path>) {
|
2026-02-22 00:21:57 -05:00
|
|
|
let mut queue = self.mtt.queue.clone();
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
let sender_id = queue.add_sender(tx);
|
|
|
|
|
for path in paths.iter() {
|
|
|
|
|
let reg_msg = Register::new(sender_id.clone(), RegMsg::AddRoute(path.clone()));
|
|
|
|
|
queue.send(Message::new(reg_msg));
|
|
|
|
|
rx.recv().unwrap(); // Wait for completion.
|
|
|
|
|
}
|
2026-03-05 11:13:18 -05:00
|
|
|
self.channel = Some(rx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn recv(&self) -> Result<Message, RecvTimeoutError> {
|
|
|
|
|
match &self.channel {
|
|
|
|
|
Some(rx) => rx.recv_timeout(Duration::from_millis(500)),
|
|
|
|
|
None => panic!("test environment does not have a channel setup"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_trigger_records(&self, action: Action) -> Records {
|
|
|
|
|
let msg = self.recv().unwrap();
|
|
|
|
|
let msg_action = msg.get_action();
|
|
|
|
|
if action == msg_action.clone().into() {
|
|
|
|
|
match msg_action {
|
|
|
|
|
MsgAction::OnAddition(data) => data.clone(),
|
|
|
|
|
MsgAction::OnDelete(data) => data.clone(),
|
|
|
|
|
MsgAction::OnQuery(data) => data.clone(),
|
|
|
|
|
MsgAction::OnUpdate(data) => data.clone(),
|
|
|
|
|
_ => panic!("{:?} is not a trigger", action),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
panic!("received {:?} instead of {:?} trigger", msg, action);
|
|
|
|
|
}
|
2026-02-22 00:21:57 -05:00
|
|
|
}
|
2026-02-19 10:39:03 -05:00
|
|
|
}
|