refactor(core): Initialize Engine Loop and Message Channel in main
This commit is contained in:
39
crates/app/cli/src/engine.rs
Normal file
39
crates/app/cli/src/engine.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use crate::messages::Message;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
/// The main background task that handles logic, API calls, and state updates.
|
||||
pub async fn run_engine_loop(mut rx: mpsc::Receiver<Message>) {
|
||||
while let Some(msg) = rx.recv().await {
|
||||
match msg {
|
||||
Message::UserAction(action) => {
|
||||
println!("Engine received action: {:?}", action);
|
||||
// TODO: Process action
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::messages::{Message, UserAction};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_engine_loop_structure() {
|
||||
let (tx, rx) = mpsc::channel(1);
|
||||
|
||||
// Spawn the engine loop
|
||||
let handle = tokio::spawn(async move {
|
||||
run_engine_loop(rx).await;
|
||||
});
|
||||
|
||||
// Send a message
|
||||
let msg = Message::UserAction(UserAction::Input("ping".to_string()));
|
||||
assert!(tx.send(msg).await.is_ok());
|
||||
|
||||
// Cleanup: dropping tx should close rx and terminate the loop
|
||||
drop(tx);
|
||||
assert!(handle.await.is_ok());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user