use std::future::Future;
use std::pin::Pin;
use crate::context::CommandContext;
use crate::{CommandError, CommandHandler, CommandOutput, SlashCategory};
pub struct ConvCommand;
impl CommandHandler<CommandContext<'_>> for ConvCommand {
fn name(&self) -> &'static str {
"/conv"
}
fn description(&self) -> &'static str {
"List, inspect, resume, or fork durable conversation-sessions"
}
fn args_hint(&self) -> &'static str {
"[list | show <id> | resume <id> | fork <id>]"
}
fn category(&self) -> SlashCategory {
SlashCategory::Session
}
fn feature_gate(&self) -> Option<&'static str> {
Some("session")
}
fn requires_auth(&self) -> bool {
true
}
fn handle<'a>(
&'a self,
ctx: &'a mut CommandContext<'_>,
args: &'a str,
) -> Pin<Box<dyn Future<Output = Result<CommandOutput, CommandError>> + Send + 'a>> {
use tracing::Instrument as _;
let span = tracing::info_span!("commands.conv.handle");
Box::pin(
async move {
let result = ctx.agent.handle_conv(args).await?;
Ok(CommandOutput::Message(result))
}
.instrument(span),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::CommandRegistry;
use crate::handlers::test_helpers::{MockDebug, MockMessages, MockSession, make_ctx};
use crate::sink::NullSink;
#[test]
fn conv_name_and_description() {
assert_eq!(ConvCommand.name(), "/conv");
assert!(!ConvCommand.description().is_empty());
}
#[tokio::test]
async fn conv_not_supported_returns_ok_message() {
let mut sink = NullSink;
let mut debug = MockDebug;
let mut messages = MockMessages;
let session = MockSession;
let mut agent = crate::NullAgent;
let mut ctx = make_ctx(&mut sink, &mut debug, &mut messages, &session, &mut agent);
let result = ConvCommand.handle(&mut ctx, "").await;
assert!(result.is_ok());
if let Ok(CommandOutput::Message(msg)) = result {
assert!(!msg.is_empty());
}
}
#[tokio::test]
async fn conv_dispatch_allowed_when_trusted() {
let mut sink = NullSink;
let mut debug = MockDebug;
let mut messages = MockMessages;
let session = MockSession;
let mut agent = crate::NullAgent;
let mut ctx = make_ctx(&mut sink, &mut debug, &mut messages, &session, &mut agent);
let mut reg: CommandRegistry<CommandContext<'_>> = CommandRegistry::new();
reg.register(ConvCommand);
let result = reg.dispatch(&mut ctx, "/conv list", true).await;
assert!(result.unwrap().is_ok());
}
#[tokio::test]
async fn conv_dispatch_rejected_when_untrusted() {
let mut sink = NullSink;
let mut debug = MockDebug;
let mut messages = MockMessages;
let session = MockSession;
let mut agent = crate::NullAgent;
let mut ctx = make_ctx(&mut sink, &mut debug, &mut messages, &session, &mut agent);
let mut reg: CommandRegistry<CommandContext<'_>> = CommandRegistry::new();
reg.register(ConvCommand);
let result = reg.dispatch(&mut ctx, "/conv list", false).await;
let err = result.unwrap().unwrap_err();
assert!(err.0.contains("trusted"));
}
}