refactor(core): Initialize Engine Loop and Message Channel in main

This commit is contained in:
2025-12-26 19:09:16 +01:00
parent 7c66dfd36b
commit 9648ddd69d
2 changed files with 49 additions and 0 deletions

View 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());
}
}

View File

@@ -1,5 +1,6 @@
mod commands;
mod messages;
mod engine;
use clap::{Parser, ValueEnum};
use color_eyre::eyre::{Result, eyre};
@@ -333,6 +334,15 @@ async fn main() -> Result<()> {
let session_id = generate_session_id();
let output_format = args.output_format;
// Initialize async engine infrastructure
let (tx, rx) = tokio::sync::mpsc::channel::<messages::Message>(100);
// Spawn the Engine Loop
tokio::spawn(async move {
engine::run_engine_loop(rx).await;
});
// Keep tx for future use (will be passed to UI/REPL)
let _tx = tx;
if let Some(cmd) = args.cmd {
match cmd {
Cmd::Read { path } => {