Got content added to new pages.

This commit is contained in:
Jeff Baskin 2025-05-02 18:04:17 -04:00
parent 57a09461f1
commit ea825c89eb
7 changed files with 47 additions and 14 deletions

2
Cargo.lock generated
View File

@ -555,6 +555,8 @@ dependencies = [
"axum", "axum",
"chrono", "chrono",
"clap", "clap",
"http-body-util",
"serde_json",
"tokio", "tokio",
"tower", "tower",
"tower-cookies", "tower-cookies",

View File

@ -9,9 +9,11 @@ edition = "2021"
axum = ">=0.8.0" axum = ">=0.8.0"
chrono = { version = ">=0.4.40", features = ["now"] } chrono = { version = ">=0.4.40", features = ["now"] }
clap = { version = ">=4.5.1", features = ["derive"] } clap = { version = ">=4.5.1", features = ["derive"] }
serde_json = ">=1.0.140"
tokio = { version = ">=1.36.0", features = ["full"] } tokio = { version = ">=1.36.0", features = ["full"] }
tower-cookies = ">=0.11.0" tower-cookies = ">=0.11.0"
uuid = { version = ">=1.8.0", features = ["v4"] } uuid = { version = ">=1.8.0", features = ["v4"] }
[dev-dependencies] [dev-dependencies]
http-body-util = ">=0.1.3"
tower = { version = ">=0.5.2", features = ["util"] } tower = { version = ">=0.5.2", features = ["util"] }

View File

@ -9,7 +9,12 @@ use std::{
}; };
use uuid::Uuid; use uuid::Uuid;
const RESPONS_TO: [MsgType; 4] = [MsgType::ActionOk, MsgType::Document, MsgType::Error, MsgType::SessionValidated]; const RESPONS_TO: [MsgType; 4] = [
MsgType::ActionOk,
MsgType::Document,
MsgType::Error,
MsgType::SessionValidated,
];
#[derive(Clone)] #[derive(Clone)]
pub struct ClientChannel { pub struct ClientChannel {

View File

@ -2,6 +2,7 @@ use crate::{
queue::{Message, MsgType, Queue}, queue::{Message, MsgType, Queue},
ActionType, ErrorType, ActionType, ErrorType,
}; };
use serde_json::Value;
use std::{ use std::{
collections::HashMap, collections::HashMap,
sync::mpsc::{channel, Receiver}, sync::mpsc::{channel, Receiver},
@ -51,8 +52,9 @@ impl Document {
fn add(&mut self, msg: Message) { fn add(&mut self, msg: Message) {
let name = msg.get_data("name").unwrap().to_string(); let name = msg.get_data("name").unwrap().to_string();
let doc = msg.get_data("doc").unwrap().to_string(); let doc: Value = serde_json::from_str(&msg.get_data("doc").unwrap().to_string()).unwrap();
self.data.insert(name, doc); self.data
.insert(name, doc["template"].as_str().unwrap().to_string());
self.queue.send(msg.reply(MsgType::ActionOk)).unwrap(); self.queue.send(msg.reply(MsgType::ActionOk)).unwrap();
} }
@ -66,7 +68,7 @@ impl Document {
let mut holder = msg.reply(MsgType::Document); let mut holder = msg.reply(MsgType::Document);
holder.add_data("doc", data.clone()); holder.add_data("doc", data.clone());
holder holder
}, }
None => { None => {
let mut holder = msg.reply(MsgType::Error); let mut holder = msg.reply(MsgType::Error);
holder.add_data("error_type", ErrorType::DocumentNotFound); holder.add_data("error_type", ErrorType::DocumentNotFound);
@ -80,6 +82,7 @@ impl Document {
#[cfg(test)] #[cfg(test)]
pub mod documents { pub mod documents {
use super::*; use super::*;
use serde_json::json;
use std::time::Duration; use std::time::Duration;
use uuid::Uuid; use uuid::Uuid;
@ -88,7 +91,10 @@ pub mod documents {
fn setup_document() -> (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, [MsgType::ActionOk, MsgType::Document, MsgType::Error].to_vec()); queue.add(
tx,
[MsgType::ActionOk, MsgType::Document, MsgType::Error].to_vec(),
);
Document::start(queue.clone()); Document::start(queue.clone());
(queue, rx) (queue, rx)
} }
@ -153,10 +159,13 @@ pub mod documents {
let (queue, rx) = setup_document(); let (queue, rx) = setup_document();
let name = format!("name-{}", Uuid::new_v4()); let name = format!("name-{}", Uuid::new_v4());
let content = format!("content-{}", Uuid::new_v4()); let content = format!("content-{}", Uuid::new_v4());
let input = json!({
"template": content.clone()
});
let mut msg1 = Message::new(MsgType::DocumentRequest); let mut msg1 = Message::new(MsgType::DocumentRequest);
msg1.add_data("name", name.clone()); msg1.add_data("name", name.clone());
msg1.add_data("action", ActionType::Add); msg1.add_data("action", ActionType::Add);
msg1.add_data("doc", content.clone()); msg1.add_data("doc", input.to_string());
queue.send(msg1.clone()).unwrap(); queue.send(msg1.clone()).unwrap();
let reply1 = rx.recv_timeout(TIMEOUT).unwrap(); let reply1 = rx.recv_timeout(TIMEOUT).unwrap();
assert_eq!(reply1.get_id(), msg1.get_id()); assert_eq!(reply1.get_id(), msg1.get_id());

View File

@ -282,7 +282,7 @@ mod fields {
let field: Field = Uuid::new_v4().into(); let field: Field = Uuid::new_v4().into();
match field.to_action() { match field.to_action() {
Ok(_) => unreachable!("should have returned an error"), Ok(_) => unreachable!("should have returned an error"),
Err(_) => {}, Err(_) => {}
} }
} }
} }

View File

@ -132,7 +132,13 @@ impl MoreThanText {
reply.get_data("sess_id").unwrap().to_uuid().unwrap() reply.get_data("sess_id").unwrap().to_uuid().unwrap()
} }
pub fn get_document<S>(&self, sess_id: Uuid, action: ActionType, doc_name: S) -> MTTReply pub fn get_document<S>(
&self,
sess_id: Uuid,
action: ActionType,
doc_name: S,
data: String,
) -> MTTReply
where where
S: Into<String>, S: Into<String>,
{ {
@ -140,7 +146,7 @@ impl MoreThanText {
msg.add_data("sess_id", sess_id); msg.add_data("sess_id", sess_id);
msg.add_data("action", action); msg.add_data("action", action);
msg.add_data("name", doc_name.into()); msg.add_data("name", doc_name.into());
msg.add_data("doc", "splat"); msg.add_data("doc", data);
let rx = self.client_channel.send(msg); let rx = self.client_channel.send(msg);
let reply = rx.recv().unwrap(); let reply = rx.recv().unwrap();
MTTReply::new(reply) MTTReply::new(reply)
@ -176,7 +182,7 @@ mod mtt {
fn get_root_document_with_str() { fn get_root_document_with_str() {
let mut mtt = MoreThanText::new(); let mut mtt = MoreThanText::new();
let id = mtt.validate_session(Some(Uuid::new_v4())); let id = mtt.validate_session(Some(Uuid::new_v4()));
let output = mtt.get_document(id, ActionType::Get, "root"); let output = mtt.get_document(id, ActionType::Get, "root", "".to_string());
assert!(output.get_error().is_none()); assert!(output.get_error().is_none());
} }
@ -184,7 +190,7 @@ mod mtt {
fn get_root_document_with_string() { fn get_root_document_with_string() {
let mut mtt = MoreThanText::new(); let mut mtt = MoreThanText::new();
let id = mtt.validate_session(Some(Uuid::new_v4())); let id = mtt.validate_session(Some(Uuid::new_v4()));
let output = mtt.get_document(id, ActionType::Get, "root".to_string()); let output = mtt.get_document(id, ActionType::Get, "root".to_string(), "".to_string());
assert!(output.get_error().is_none()); assert!(output.get_error().is_none());
} }
} }

View File

@ -85,6 +85,7 @@ async fn mtt_conn(
method: Method, method: Method,
path: Path<HashMap<String, String>>, path: Path<HashMap<String, String>>,
state: State<MoreThanText>, state: State<MoreThanText>,
body: String,
) -> impl IntoResponse { ) -> impl IntoResponse {
let (tx, mut rx) = channel(1); let (tx, mut rx) = channel(1);
let action = match method { let action = match method {
@ -97,7 +98,7 @@ async fn mtt_conn(
None => "root".to_string(), None => "root".to_string(),
}; };
spawn(async move { spawn(async move {
tx.send(state.get_document(sess_id.0, action, doc)) tx.send(state.get_document(sess_id.0, action, doc, body))
.await .await
.unwrap(); .unwrap();
}); });
@ -112,7 +113,6 @@ async fn mtt_conn(
#[cfg(test)] #[cfg(test)]
mod servers { mod servers {
use super::*; use super::*;
use std::time::Duration;
use axum::{ use axum::{
body::Body, body::Body,
http::{ http::{
@ -120,6 +120,9 @@ mod servers {
Method, Request, Method, Request,
}, },
}; };
use http_body_util::BodyExt;
use serde_json::json;
use std::time::Duration;
use tower::ServiceExt; use tower::ServiceExt;
#[tokio::test] #[tokio::test]
@ -197,6 +200,10 @@ mod servers {
async fn add_new_page() { async fn add_new_page() {
let base = "/something".to_string(); let base = "/something".to_string();
let api = format!("/api{}", &base); let api = format!("/api{}", &base);
let content = format!("content-{}", Uuid::new_v4());
let document = json!({
"template": content.clone()
});
let app = create_app(MoreThanText::new()).await; let app = create_app(MoreThanText::new()).await;
let response = app let response = app
.clone() .clone()
@ -204,7 +211,7 @@ mod servers {
Request::builder() Request::builder()
.method(Method::POST) .method(Method::POST)
.uri(&api) .uri(&api)
.body(Body::empty()) .body(document.to_string())
.unwrap(), .unwrap(),
) )
.await .await
@ -225,5 +232,7 @@ mod servers {
"failed to get ro {:?}", "failed to get ro {:?}",
base base
); );
let body = response.into_body().collect().await.unwrap().to_bytes();
assert_eq!(body, content);
} }
} }