From 7dc4b9213fb1799cb9d34dde206abb8535ba264e Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 2 Dec 2023 16:09:59 +0800 Subject: [PATCH] add chat-test mini-app this now contains the UI tests as a stand-alone app --- Cargo.toml | 1 + apps/chat-test/Cargo.toml | 42 ++++++++++++++ apps/chat-test/README.md | 5 ++ apps/chat-test/src/api.rs | 32 +++++++++++ apps/chat-test/src/main.rs | 112 ++++++++++++++++++++++++++++++++++++ apps/chat-test/src/tests.rs | 30 ++++++++++ apps/manifest.json | 25 +++++--- 7 files changed, 239 insertions(+), 8 deletions(-) create mode 100644 apps/chat-test/Cargo.toml create mode 100644 apps/chat-test/README.md create mode 100644 apps/chat-test/src/api.rs create mode 100644 apps/chat-test/src/main.rs create mode 100644 apps/chat-test/src/tests.rs diff --git a/Cargo.toml b/Cargo.toml index a11afd5c7..6bc1c7802 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,6 +83,7 @@ members = [ "apps/vault", "apps/vault/tools/vaultbackup-rs", "apps/transientdisk", + "apps/chat-test", "services/libstd-test", "services/ffi-test", "services/tts", diff --git a/apps/chat-test/Cargo.toml b/apps/chat-test/Cargo.toml new file mode 100644 index 000000000..831a7399c --- /dev/null +++ b/apps/chat-test/Cargo.toml @@ -0,0 +1,42 @@ +[package] +name = "chat-test" +version = "0.1.0" +authors = ["bunnie "] +edition = "2021" +description = "Chat UI test routines" + +# Dependency versions enforced by Cargo.lock. +[dependencies] +log = "0.4.14" +num-derive = { version = "0.3.3", default-features = false} +num-traits = { version = "0.2.14", default-features = false} +xous = "0.9.54" +xous-ipc = "0.9.54" +log-server = { package = "xous-api-log", version = "0.1.50" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.50" } +xous-names = { package = "xous-api-names", version = "0.9.52" } +gam = { path = "../../services/gam" } +graphics-server = { path = "../../services/graphics-server" } +trng = { path = "../../services/trng"} +locales = { path = "../../locales"} +ime-plugin-api = { path = "../../services/ime-plugin-api"} +ime-plugin-shell = { path = "../../services/ime-plugin-shell"} +content-plugin-api = { path = "../../services/content-plugin-api"} # all content canvas providers must provide this API +codec = { path = "../../services/codec"} +com = { path = "../../services/com"} +com_rs = { git = "https://github.com/betrusted-io/com_rs", rev = "891bdd3ca8e41f81510d112483e178aea3e3a921" } +llio = { path = "../../services/llio"} +net = { path = "../../services/net" } +pddb = { path = "../../services/pddb" } +# new dependencies for mtxcli +chat = { path = "../../libs/chat" } +modals = { path = "../../services/modals" } +percent-encoding = "2.2" +rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} +serde = { version = "1.0", features = [ "derive" ] } +tls = { path = "../../libs/tls" } +ureq = { version = "2.7.1", features = ["json"] } +url = "2.2.2" + +[features] +default = [] diff --git a/apps/chat-test/README.md b/apps/chat-test/README.md new file mode 100644 index 000000000..6802197e1 --- /dev/null +++ b/apps/chat-test/README.md @@ -0,0 +1,5 @@ +# chat-test + +Routines for testing aspects of the chat application APIs. + +Not suitable for human consumption, side effects include confusion, frustration, and panic attacks. \ No newline at end of file diff --git a/apps/chat-test/src/api.rs b/apps/chat-test/src/api.rs new file mode 100644 index 000000000..84995111f --- /dev/null +++ b/apps/chat-test/src/api.rs @@ -0,0 +1,32 @@ +#[allow(dead_code)] +pub(crate) const SERVER_NAME_MTXCHAT: &str = "_Matrix chat_"; + +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub enum ChatTestOp { + /// chat ui event + Event = 0, + /// menu item click + Menu, + /// chat ui user post + Post, + /// chat ui keystroke + Rawkeys, + /// exit the application + Quit, +} + +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub enum MenuOp { + Login, + Logout, + Noop, + Room, +} + +#[allow(dead_code)] +pub struct Msg { + pub type_: String, + pub body: Option, + pub sender: Option, + pub ts: Option, +} diff --git a/apps/chat-test/src/main.rs b/apps/chat-test/src/main.rs new file mode 100644 index 000000000..797bbc83e --- /dev/null +++ b/apps/chat-test/src/main.rs @@ -0,0 +1,112 @@ +#![cfg_attr(target_os = "none", no_std)] +#![cfg_attr(target_os = "none", no_main)] + +mod api; +mod tests; + +use api::*; +use chat::{Chat, Event, POST_TEXT_MAX}; +use num_traits::*; +use xous_ipc::Buffer; + +fn main() -> ! { + let stack_size = 1024 * 1024; + std::thread::Builder::new() + .stack_size(stack_size) + .spawn(wrapped_main) + .unwrap() + .join() + .unwrap() +} + +fn wrapped_main() -> ! { + log_server::init_wait().unwrap(); + log::set_max_level(log::LevelFilter::Info); + log::info!("my PID is {}", xous::process::id()); + + const HEAP_LARGER_LIMIT: usize = 2048 * 1024; + let new_limit = HEAP_LARGER_LIMIT; + let result = xous::rsyscall(xous::SysCall::AdjustProcessLimit( + xous::Limits::HeapMaximum as usize, + 0, + new_limit, + )); + + if let Ok(xous::Result::Scalar2(1, current_limit)) = result { + xous::rsyscall(xous::SysCall::AdjustProcessLimit( + xous::Limits::HeapMaximum as usize, + current_limit, + new_limit, + )) + .unwrap(); + log::info!("Heap limit increased to: {}", new_limit); + } else { + panic!("Unsupported syscall!"); + } + + let xns = xous_names::XousNames::new().unwrap(); + let sid = xns + .register_name(SERVER_NAME_MTXCHAT, None) + .expect("can't register server"); + log::trace!("registered with NS -- {:?}", sid); + + let chat = Chat::new( + gam::APP_NAME_CHAT_TEST, + gam::APP_MENU_0_CHAT_TEST, + Some(xous::connect(sid).unwrap()), + Some(ChatTestOp::Post as usize), + Some(ChatTestOp::Event as usize), + Some(ChatTestOp::Rawkeys as usize), + ); + // not used yet, but probably will use in the future + let _cid = xous::connect(sid).unwrap(); + + tests::test_ui(&chat); + loop { + let msg = xous::receive_message(sid).unwrap(); + log::debug!("got message {:?}", msg); + match FromPrimitive::from_usize(msg.body.id()) { + Some(ChatTestOp::Event) => { + log::info!("got Chat UI Event"); + xous::msg_scalar_unpack!(msg, event_code, _, _, _, { + match FromPrimitive::from_usize(event_code) { + Some(Event::Focus) => { + chat.redraw(); + } + _ => (), + } + }); + } + Some(ChatTestOp::Menu) => { + log::info!("got Chat Menu Click"); + xous::msg_scalar_unpack!(msg, menu_code, _, _, _, { + let code: Option = FromPrimitive::from_usize(menu_code); + log::info!("Got menu code {:?}", code); + }); + } + Some(ChatTestOp::Post) => { + let buffer = + unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let s = buffer + .to_original::, _>() + .unwrap(); + if s.len() > 0 { + // capture input instead of calling here, so message can drop and calling server is released + log::info!("got Post {:?}", s.to_string()); + } + } + Some(ChatTestOp::Rawkeys) => log::info!("got rawkeys"), + Some(ChatTestOp::Quit) => { + log::error!("got Quit"); + break; + } + _ => (), + } + } + // clean up our program + log::error!("main loop exit, destroying servers"); + xns.unregister_server(sid).unwrap(); + xous::destroy_server(sid).unwrap(); + log::trace!("quitting"); + xous::terminate_process(0) +} diff --git a/apps/chat-test/src/tests.rs b/apps/chat-test/src/tests.rs new file mode 100644 index 000000000..283688f07 --- /dev/null +++ b/apps/chat-test/src/tests.rs @@ -0,0 +1,30 @@ +use chat::Chat; + +pub fn test_ui(chat: &Chat) { + let pddb = pddb::Pddb::new(); + // nuke any existing test dictionary, if it exists + pddb.delete_dict("tests.ui", None).ok(); + pddb.sync().ok(); + + // re-create the test room from scratch every time + chat.dialogue_set("tests.ui", Some("test_room")).expect("couldn't set dialog"); + + // generate a list of test posts that are repeatable each time + chat.post_add("alice", 1_700_000_000, "hello world!", None).ok(); + chat.post_add("bob", 1_700_000_002, "hi alice!", None).ok(); + for i in 0..5 { + chat.post_add("alice", 1_700_000_005 + i*4, &format!("alice sez {}", i), None).ok(); + chat.post_add("bob", 1_700_000_006 + i*4, &format!("bob sez {}", i), None).ok(); + chat.post_add("trent", 1_700_000_007 + i*4, &format!("trent sez {}", i), None).ok(); + } + chat.post_add("alice", 1_700_001_000, "eom", None).ok(); + + log::info!("triggering save"); + xous::send_message( + chat.cid(), + xous::Message::new_scalar(chat::ChatOp::DialogueSave as usize, 0, 0, 0, 0), + ) + .expect("failed to send new inbound msgs"); + + log::info!("test setup done"); +} diff --git a/apps/manifest.json b/apps/manifest.json index ec4552148..9a160de59 100644 --- a/apps/manifest.json +++ b/apps/manifest.json @@ -1,13 +1,13 @@ { "app-loader": { - "context_name": "App Loader", - "menu_name": { - "appmenu.app-loader": { - "en": "App Loader", - "en-tts": "App Loader" - } - }, - "submenu": 2 + "context_name": "App Loader", + "menu_name": { + "appmenu.app-loader": { + "en": "App Loader", + "en-tts": "App Loader" + } + }, + "submenu": 2 }, "ball": { "context_name": "ball demo app", @@ -54,6 +54,15 @@ }, "submenu": 1 }, + "chat-test": { + "context_name": "Chat UI test", + "menu_name": { + "appmenu.mtxchat": { + "en": "Chat UI test" + } + }, + "submenu": 1 + }, "repl": { "context_name": "repl demo app", "menu_name": {