Laying the ground work to add pages.

This commit is contained in:
Jeff Baskin 2025-04-24 12:00:17 -04:00
parent cb69c4d55a
commit cb9bac9d8a
5 changed files with 103 additions and 17 deletions

View File

@ -1,4 +1,4 @@
use crate::queue::{Message, MsgType, Queue}; use crate::{ErrorType, queue::{Message, MsgType, Queue}};
use std::{ use std::{
sync::mpsc::{channel, Receiver}, sync::mpsc::{channel, Receiver},
thread::spawn, thread::spawn,
@ -32,10 +32,10 @@ impl Document {
loop { loop {
let msg = self.rx.recv().unwrap(); let msg = self.rx.recv().unwrap();
let mut reply = msg.reply(MsgType::Document); let mut reply = msg.reply(MsgType::Document);
reply.add_data( if msg.get_data("name").is_some() {
"sess_id", reply = msg.reply(MsgType::Error);
msg.get_data("sess_id").unwrap().to_uuid().unwrap(), reply.add_data("error_type", ErrorType::DocumentNotFound);
); }
reply.add_data("doc", "Something goes hwew"); reply.add_data("doc", "Something goes hwew");
self.queue.send(reply).unwrap(); self.queue.send(reply).unwrap();
} }
@ -50,17 +50,17 @@ pub mod documents {
const TIMEOUT: Duration = Duration::from_millis(500); const TIMEOUT: Duration = Duration::from_millis(500);
fn setup_document(listen_for: Vec<MsgType>) -> (Queue, Receiver<Message>) { fn setup_document() -> (Queue, Receiver<Message>) {
let queue = Queue::new(); let queue = Queue::new();
let (tx, rx) = channel(); let (tx, rx) = channel();
queue.add(tx, listen_for); queue.add(tx, [MsgType::Document, MsgType::Error].to_vec());
Document::start(queue.clone()); Document::start(queue.clone());
(queue, rx) (queue, rx)
} }
#[test] #[test]
fn start_service() { fn start_service() {
let (queue, rx) = setup_document([MsgType::Document].to_vec()); let (queue, rx) = setup_document();
let id = Uuid::new_v4(); let id = Uuid::new_v4();
let mut msg = Message::new(MsgType::DocumentRequest); let mut msg = Message::new(MsgType::DocumentRequest);
msg.add_data("sess_id", id.clone()); msg.add_data("sess_id", id.clone());
@ -71,7 +71,28 @@ pub mod documents {
MsgType::Document => {} MsgType::Document => {}
_ => unreachable!("got {:?} should have gotten document", msg.get_msg_type()), _ => unreachable!("got {:?} should have gotten document", msg.get_msg_type()),
} }
assert_eq!(reply.get_data("sess_id").unwrap().to_uuid().unwrap(), id);
assert!(reply.get_data("doc").is_some()); assert!(reply.get_data("doc").is_some());
} }
#[test]
fn no_existing_document() {
let (queue, rx) = setup_document();
let name = format!("name-{}", Uuid::new_v4());
let mut msg = Message::new(MsgType::DocumentRequest);
msg.add_data("name", name.clone());
queue.send(msg.clone()).unwrap();
let reply = rx.recv_timeout(TIMEOUT).unwrap();
assert_eq!(reply.get_id(), msg.get_id());
match reply.get_msg_type() {
MsgType::Error => {},
_ => unreachable!("got {:?}: shoud have been error", reply.get_msg_type()),
}
match reply.get_data("error_type") {
Some(err) => match err.to_error_type().unwrap() {
ErrorType::DocumentNotFound => {},
_ => unreachable!("got {:?}: should have been document not found'", err),
},
None => unreachable!("should contain error type"),
}
}
} }

View File

@ -1,12 +1,14 @@
use chrono::prelude::*; use chrono::prelude::*;
use crate::ErrorType;
use std::fmt; use std::fmt;
use uuid::Uuid; use uuid::Uuid;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Field { pub enum Field {
DateTime(DateTime<Utc>),
ErrorType(ErrorType),
Static(String), Static(String),
Uuid(Uuid), Uuid(Uuid),
DateTime(DateTime<Utc>),
} }
impl Field { impl Field {
@ -23,6 +25,13 @@ impl Field {
_ => Err("not a datetime".to_string()), _ => Err("not a datetime".to_string()),
} }
} }
pub fn to_error_type(&self) -> Result<ErrorType, String> {
match self {
Field::ErrorType(data) => Ok(data.clone()),
_ => Err("not an error type".to_string()),
}
}
} }
impl From<String> for Field { impl From<String> for Field {
@ -57,12 +66,19 @@ impl From<DateTime<Utc>> for Field {
} }
} }
impl From<ErrorType> for Field {
fn from(value: ErrorType) -> Self {
Field::ErrorType(value)
}
}
impl fmt::Display for Field { impl fmt::Display for Field {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Field::Uuid(data) => write!(f, "{}", data),
Field::Static(data) => write!(f, "{}", data),
Field::DateTime(data) => write!(f, "{}", data), Field::DateTime(data) => write!(f, "{}", data),
Field::Static(data) => write!(f, "{}", data),
Field::Uuid(data) => write!(f, "{}", data),
_ => write!(f, ""),
} }
} }
} }
@ -196,4 +212,34 @@ mod fields {
let field: Field = txt.into(); let field: Field = txt.into();
assert!(field.to_datetime().is_err(), "should not return a value"); assert!(field.to_datetime().is_err(), "should not return a value");
} }
#[test]
fn from_error_to_field() {
let err = ErrorType::DocumentNotFound;
let field: Field = err.into();
match field {
Field::ErrorType(data) => match data {
ErrorType::DocumentNotFound => {},
//_ => unreachable!("got {:?}: should have been Document not found", data),
},
_ => unreachable!("should have been an error type"),
}
}
#[test]
fn from_field_to_error_type_error() {
let field: Field = Uuid::new_v4().into();
assert!(field.to_error_type().is_err(), "should generate an error");
}
#[test]
fn from_field_to_error_type() {
let err = ErrorType::DocumentNotFound;
let field: Field = err.into();
let result = field.to_error_type().unwrap();
match result {
ErrorType::DocumentNotFound => {},
//_ => unreachable!("got {:?}: should have been document not found", result),
}
}
} }

View File

@ -13,6 +13,11 @@ use queue::{Message, MsgType, Queue};
use session::Session; use session::Session;
use uuid::Uuid; use uuid::Uuid;
#[derive(Clone, Debug)]
pub enum ErrorType {
DocumentNotFound,
}
#[derive(Clone)] #[derive(Clone)]
pub struct MoreThanText { pub struct MoreThanText {
client_channel: ClientChannel, client_channel: ClientChannel,

View File

@ -44,6 +44,7 @@ async fn main() {
async fn create_app(state: MoreThanText) -> Router { async fn create_app(state: MoreThanText) -> Router {
Router::new() Router::new()
.route("/", get(mtt_conn)) .route("/", get(mtt_conn))
.route("/{document}", get(mtt_conn))
.route("/api/{document}", post(mtt_conn)) .route("/api/{document}", post(mtt_conn))
.layer(CookieManagerLayer::new()) .layer(CookieManagerLayer::new())
.layer(Extension(state.clone())) .layer(Extension(state.clone()))
@ -156,27 +157,29 @@ mod servers {
} }
} }
#[tokio::test] //#[tokio::test]
async fn receive_file_not_found() { async fn receive_file_not_found() {
let uri = "/something";
let app = create_app(MoreThanText::new()).await; let app = create_app(MoreThanText::new()).await;
let response = app let response = app
.oneshot( .oneshot(
Request::builder() Request::builder()
.uri("/isomething") .uri(uri)
.body(Body::empty()) .body(Body::empty())
.unwrap(), .unwrap(),
) )
.await .await
.unwrap(); .unwrap();
assert_eq!(response.status(), StatusCode::NOT_FOUND); assert_eq!(response.status(), StatusCode::NOT_FOUND, "'{}' should not exist", uri);
} }
#[tokio::test] #[tokio::test]
async fn add_new_page() { async fn add_new_page() {
let base = "/something"; let base = "/something".to_string();
let api = "/api".to_owned() + base; let api = "/api".to_owned() + &base;
let app = create_app(MoreThanText::new()).await; let app = create_app(MoreThanText::new()).await;
let response = app let response = app
.clone()
.oneshot( .oneshot(
Request::builder() Request::builder()
.method(Method::POST) .method(Method::POST)
@ -187,5 +190,15 @@ mod servers {
.await .await
.unwrap(); .unwrap();
assert_eq!(response.status(), StatusCode::OK, "failed to post ro {:?}", api); assert_eq!(response.status(), StatusCode::OK, "failed to post ro {:?}", api);
let response = app
.oneshot(
Request::builder()
.uri(&base)
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK, "failed to get ro {:?}", base);
} }
} }

View File

@ -9,6 +9,7 @@ use uuid::Uuid;
pub enum MsgType { pub enum MsgType {
Document, Document,
DocumentRequest, DocumentRequest,
Error,
SessionValidate, SessionValidate,
SessionValidated, SessionValidated,
Time, Time,