use crate::event::ReplEvent;
use crate::model::{CancelReply, CommandDescriptor, PermissionAnswer, PickerRequest};
use anyhow::Result;
use tokio::sync::mpsc::UnboundedSender;
#[async_trait::async_trait]
pub trait TuiEngine: Send + Sync {
async fn handle_input(&self, line: String, tx: UnboundedSender<ReplEvent>) -> Result<bool>;
async fn setup(&self, tx: UnboundedSender<ReplEvent>) -> Result<()>;
async fn cancel_session(&self) -> Result<()> {
Ok(())
}
async fn cancel_session_reply(&self) -> CancelReply {
match self.cancel_session().await {
Ok(()) => CancelReply::Stopped,
Err(e) => CancelReply::Failed {
error: format!("{e:#}"),
},
}
}
async fn respond_permission(&self, request_id: String, answer: PermissionAnswer) -> Result<()> {
let _ = (request_id, answer);
Ok(())
}
async fn subscribe_workstream_events(&self, tx: UnboundedSender<ReplEvent>) -> Result<()> {
let _ = tx;
Ok(())
}
fn commands(&self) -> Vec<CommandDescriptor> {
Vec::new()
}
fn picker(&self, name: &str) -> Option<PickerRequest> {
let _ = name;
None
}
async fn shutdown(&self) -> Result<()> {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::CommandRouting;
struct BareEngine;
#[async_trait::async_trait]
impl TuiEngine for BareEngine {
async fn handle_input(
&self,
_line: String,
_tx: UnboundedSender<ReplEvent>,
) -> Result<bool> {
Ok(true)
}
async fn setup(&self, _tx: UnboundedSender<ReplEvent>) -> Result<()> {
Ok(())
}
}
struct RichEngine;
#[async_trait::async_trait]
impl TuiEngine for RichEngine {
async fn handle_input(
&self,
_line: String,
_tx: UnboundedSender<ReplEvent>,
) -> Result<bool> {
Ok(true)
}
async fn setup(&self, _tx: UnboundedSender<ReplEvent>) -> Result<()> {
Ok(())
}
fn commands(&self) -> Vec<CommandDescriptor> {
vec![CommandDescriptor {
name: "workstream".to_string(),
summary: "List or activate a workstream".to_string(),
routing: CommandRouting::Engine,
args_hint: Some("activate <id>".to_string()),
}]
}
fn picker(&self, name: &str) -> Option<PickerRequest> {
(name == "model").then(|| PickerRequest {
title: "Select a model".to_string(),
items: Vec::new(),
dispatch_command: "/model".to_string(),
})
}
}
#[test]
fn commands_defaults_to_empty() {
let engine = BareEngine;
assert!(engine.commands().is_empty());
}
#[test]
fn picker_defaults_to_none() {
let engine = BareEngine;
assert!(engine.picker("model").is_none());
assert!(engine.picker("anything").is_none());
}
#[tokio::test]
async fn respond_permission_defaults_to_ok() {
let engine = BareEngine;
assert!(
engine
.respond_permission("req-1".to_string(), PermissionAnswer::Deny)
.await
.is_ok()
);
}
struct FailingCancelEngine;
#[async_trait::async_trait]
impl TuiEngine for FailingCancelEngine {
async fn handle_input(
&self,
_line: String,
_tx: UnboundedSender<ReplEvent>,
) -> Result<bool> {
Ok(true)
}
async fn setup(&self, _tx: UnboundedSender<ReplEvent>) -> Result<()> {
Ok(())
}
async fn cancel_session(&self) -> Result<()> {
Err(anyhow::anyhow!("socket closed"))
}
}
#[tokio::test]
async fn cancel_session_reply_defaults_to_stopped() {
assert_eq!(
BareEngine.cancel_session_reply().await,
CancelReply::Stopped
);
}
#[tokio::test]
async fn cancel_session_reply_default_reports_an_error_as_failed() {
assert_eq!(
FailingCancelEngine.cancel_session_reply().await,
CancelReply::Failed {
error: "socket closed".to_string()
}
);
}
#[test]
fn commands_and_picker_overrides_take_effect() {
let engine = RichEngine;
let commands = engine.commands();
assert_eq!(commands.len(), 1);
assert_eq!(commands[0].name, "workstream");
assert_eq!(commands[0].routing, CommandRouting::Engine);
let picker = engine.picker("model").expect("model picker present");
assert_eq!(picker.title, "Select a model");
assert_eq!(picker.dispatch_command, "/model");
assert!(engine.picker("provider").is_none());
}
}