2026-04-12 13:11:23 +00:00
|
|
|
//! WhatsApp command handling — processes incoming WhatsApp messages as bot commands.
|
2026-03-27 10:49:39 +00:00
|
|
|
|
|
|
|
|
use super::WhatsAppWebhookContext;
|
2026-04-27 02:37:22 +00:00
|
|
|
use super::format::markdown_to_whatsapp;
|
2026-03-27 10:49:39 +00:00
|
|
|
use super::history::save_whatsapp_history;
|
2026-04-27 02:37:22 +00:00
|
|
|
use crate::chat::transport::matrix::RoomConversation;
|
2026-04-13 14:07:08 +00:00
|
|
|
use crate::chat::util::is_permission_approval;
|
|
|
|
|
use crate::http::context::PermissionDecision;
|
|
|
|
|
use crate::slog;
|
2026-03-27 10:49:39 +00:00
|
|
|
|
2026-04-27 02:37:22 +00:00
|
|
|
mod llm;
|
|
|
|
|
|
|
|
|
|
use llm::handle_llm_message;
|
|
|
|
|
|
2026-03-27 10:49:39 +00:00
|
|
|
/// Dispatch an incoming WhatsApp message to bot commands.
|
2026-04-13 14:07:08 +00:00
|
|
|
pub(super) async fn handle_incoming_message(
|
|
|
|
|
ctx: &WhatsAppWebhookContext,
|
|
|
|
|
sender: &str,
|
|
|
|
|
message: &str,
|
|
|
|
|
) {
|
2026-03-27 10:49:39 +00:00
|
|
|
use crate::chat::commands::{CommandDispatch, try_handle_command};
|
|
|
|
|
|
|
|
|
|
// Allowlist check: when configured, silently ignore unauthorized senders.
|
2026-04-13 14:07:08 +00:00
|
|
|
if !ctx.allowed_phones.is_empty() && !ctx.allowed_phones.iter().any(|p| p == sender) {
|
2026-03-27 10:49:39 +00:00
|
|
|
slog!("[whatsapp] Ignoring message from unauthorized sender: {sender}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Record this inbound message to keep the 24-hour window open.
|
|
|
|
|
ctx.window_tracker.record_message(sender);
|
|
|
|
|
|
|
|
|
|
// If there is a pending permission prompt for this sender, interpret the
|
|
|
|
|
// message as a yes/no response instead of starting a new command/LLM flow.
|
|
|
|
|
{
|
2026-04-25 19:47:49 +00:00
|
|
|
let mut pending = ctx.services.pending_perm_replies.lock().await;
|
2026-03-27 10:49:39 +00:00
|
|
|
if let Some(tx) = pending.remove(sender) {
|
|
|
|
|
let decision = if is_permission_approval(message) {
|
|
|
|
|
PermissionDecision::Approve
|
|
|
|
|
} else {
|
|
|
|
|
PermissionDecision::Deny
|
|
|
|
|
};
|
|
|
|
|
let _ = tx.send(decision);
|
|
|
|
|
let confirmation = if decision == PermissionDecision::Approve {
|
|
|
|
|
"Permission approved."
|
|
|
|
|
} else {
|
|
|
|
|
"Permission denied."
|
|
|
|
|
};
|
|
|
|
|
let formatted = markdown_to_whatsapp(confirmation);
|
|
|
|
|
let _ = ctx.transport.send_message(sender, &formatted, "").await;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let dispatch = CommandDispatch {
|
2026-04-25 20:37:10 +00:00
|
|
|
services: &ctx.services,
|
2026-04-25 19:47:49 +00:00
|
|
|
project_root: &ctx.services.project_root,
|
2026-04-25 20:37:10 +00:00
|
|
|
bot_user_id: &ctx.services.bot_user_id,
|
2026-03-27 10:49:39 +00:00
|
|
|
room_id: sender,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Some(response) = try_handle_command(&dispatch, message) {
|
|
|
|
|
slog!("[whatsapp] Sending command response to {sender}");
|
|
|
|
|
let formatted = markdown_to_whatsapp(&response);
|
|
|
|
|
if let Err(e) = ctx.transport.send_message(sender, &formatted, "").await {
|
|
|
|
|
slog!("[whatsapp] Failed to send reply to {sender}: {e}");
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for async commands (htop, delete).
|
|
|
|
|
if let Some(htop_cmd) = crate::chat::transport::matrix::htop::extract_htop_command(
|
|
|
|
|
message,
|
2026-04-25 19:47:49 +00:00
|
|
|
&ctx.services.bot_name,
|
|
|
|
|
&ctx.services.bot_user_id,
|
2026-03-27 10:49:39 +00:00
|
|
|
) {
|
|
|
|
|
use crate::chat::transport::matrix::htop::HtopCommand;
|
|
|
|
|
slog!("[whatsapp] Handling htop command from {sender}");
|
|
|
|
|
match htop_cmd {
|
|
|
|
|
HtopCommand::Stop => {
|
|
|
|
|
// htop stop — no-op on WhatsApp since there's no persistent
|
|
|
|
|
// editable message; just acknowledge.
|
|
|
|
|
let _ = ctx
|
|
|
|
|
.transport
|
|
|
|
|
.send_message(sender, "htop stopped.", "")
|
|
|
|
|
.await;
|
|
|
|
|
}
|
|
|
|
|
HtopCommand::Start { duration_secs } => {
|
|
|
|
|
// On WhatsApp, send a single snapshot instead of a live-updating
|
|
|
|
|
// dashboard since we can't edit messages.
|
|
|
|
|
let snapshot = crate::chat::transport::matrix::htop::build_htop_message(
|
2026-04-25 19:47:49 +00:00
|
|
|
&ctx.services.agents,
|
2026-03-27 10:49:39 +00:00
|
|
|
0,
|
|
|
|
|
duration_secs,
|
|
|
|
|
);
|
|
|
|
|
let _ = ctx.transport.send_message(sender, &snapshot, "").await;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(del_cmd) = crate::chat::transport::matrix::delete::extract_delete_command(
|
|
|
|
|
message,
|
2026-04-25 19:47:49 +00:00
|
|
|
&ctx.services.bot_name,
|
|
|
|
|
&ctx.services.bot_user_id,
|
2026-03-27 10:49:39 +00:00
|
|
|
) {
|
|
|
|
|
let response = match del_cmd {
|
|
|
|
|
crate::chat::transport::matrix::delete::DeleteCommand::Delete { story_number } => {
|
|
|
|
|
slog!("[whatsapp] Handling delete command from {sender}: story {story_number}");
|
|
|
|
|
crate::chat::transport::matrix::delete::handle_delete(
|
2026-04-25 19:47:49 +00:00
|
|
|
&ctx.services.bot_name,
|
2026-03-27 10:49:39 +00:00
|
|
|
&story_number,
|
2026-04-25 19:47:49 +00:00
|
|
|
&ctx.services.project_root,
|
|
|
|
|
&ctx.services.agents,
|
2026-03-27 10:49:39 +00:00
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
crate::chat::transport::matrix::delete::DeleteCommand::BadArgs => {
|
2026-04-25 19:47:49 +00:00
|
|
|
format!("Usage: `{} delete <number>`", ctx.services.bot_name)
|
2026-03-27 10:49:39 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let _ = ctx.transport.send_message(sender, &response, "").await;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(rmtree_cmd) = crate::chat::transport::matrix::rmtree::extract_rmtree_command(
|
|
|
|
|
message,
|
2026-04-25 19:47:49 +00:00
|
|
|
&ctx.services.bot_name,
|
|
|
|
|
&ctx.services.bot_user_id,
|
2026-03-27 10:49:39 +00:00
|
|
|
) {
|
|
|
|
|
let response = match rmtree_cmd {
|
|
|
|
|
crate::chat::transport::matrix::rmtree::RmtreeCommand::Rmtree { story_number } => {
|
|
|
|
|
slog!("[whatsapp] Handling rmtree command from {sender}: story {story_number}");
|
|
|
|
|
crate::chat::transport::matrix::rmtree::handle_rmtree(
|
2026-04-25 19:47:49 +00:00
|
|
|
&ctx.services.bot_name,
|
2026-03-27 10:49:39 +00:00
|
|
|
&story_number,
|
2026-04-25 19:47:49 +00:00
|
|
|
&ctx.services.project_root,
|
|
|
|
|
&ctx.services.agents,
|
2026-03-27 10:49:39 +00:00
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
crate::chat::transport::matrix::rmtree::RmtreeCommand::BadArgs => {
|
2026-04-25 19:47:49 +00:00
|
|
|
format!("Usage: `{} rmtree <number>`", ctx.services.bot_name)
|
2026-03-27 10:49:39 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let _ = ctx.transport.send_message(sender, &response, "").await;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if crate::chat::transport::matrix::reset::extract_reset_command(
|
|
|
|
|
message,
|
2026-04-25 19:47:49 +00:00
|
|
|
&ctx.services.bot_name,
|
|
|
|
|
&ctx.services.bot_user_id,
|
2026-03-27 10:49:39 +00:00
|
|
|
)
|
|
|
|
|
.is_some()
|
|
|
|
|
{
|
|
|
|
|
slog!("[whatsapp] Handling reset command from {sender}");
|
|
|
|
|
{
|
|
|
|
|
let mut guard = ctx.history.lock().await;
|
2026-04-13 14:07:08 +00:00
|
|
|
let conv = guard
|
|
|
|
|
.entry(sender.to_string())
|
|
|
|
|
.or_insert_with(RoomConversation::default);
|
2026-03-27 10:49:39 +00:00
|
|
|
conv.session_id = None;
|
|
|
|
|
conv.entries.clear();
|
2026-04-25 19:47:49 +00:00
|
|
|
save_whatsapp_history(&ctx.services.project_root, &guard);
|
2026-03-27 10:49:39 +00:00
|
|
|
}
|
|
|
|
|
let _ = ctx
|
|
|
|
|
.transport
|
|
|
|
|
.send_message(sender, "Session cleared.", "")
|
|
|
|
|
.await;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(start_cmd) = crate::chat::transport::matrix::start::extract_start_command(
|
|
|
|
|
message,
|
2026-04-25 19:47:49 +00:00
|
|
|
&ctx.services.bot_name,
|
|
|
|
|
&ctx.services.bot_user_id,
|
2026-03-27 10:49:39 +00:00
|
|
|
) {
|
|
|
|
|
let response = match start_cmd {
|
|
|
|
|
crate::chat::transport::matrix::start::StartCommand::Start {
|
|
|
|
|
story_number,
|
|
|
|
|
agent_hint,
|
|
|
|
|
} => {
|
|
|
|
|
slog!("[whatsapp] Handling start command from {sender}: story {story_number}");
|
|
|
|
|
crate::chat::transport::matrix::start::handle_start(
|
2026-04-25 19:47:49 +00:00
|
|
|
&ctx.services.bot_name,
|
2026-03-27 10:49:39 +00:00
|
|
|
&story_number,
|
|
|
|
|
agent_hint.as_deref(),
|
2026-04-25 19:47:49 +00:00
|
|
|
&ctx.services.project_root,
|
|
|
|
|
&ctx.services.agents,
|
2026-03-27 10:49:39 +00:00
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
crate::chat::transport::matrix::start::StartCommand::BadArgs => {
|
2026-04-25 19:47:49 +00:00
|
|
|
format!("Usage: `{} start <number>`", ctx.services.bot_name)
|
2026-03-27 10:49:39 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let _ = ctx.transport.send_message(sender, &response, "").await;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 15:07:15 +00:00
|
|
|
if let Some(assign_cmd) = crate::chat::transport::matrix::assign::extract_assign_command(
|
|
|
|
|
message,
|
2026-04-25 19:47:49 +00:00
|
|
|
&ctx.services.bot_name,
|
|
|
|
|
&ctx.services.bot_user_id,
|
2026-03-27 15:07:15 +00:00
|
|
|
) {
|
|
|
|
|
let response = match assign_cmd {
|
2026-04-13 14:07:08 +00:00
|
|
|
crate::chat::transport::matrix::assign::AssignCommand::Assign {
|
|
|
|
|
story_number,
|
|
|
|
|
model,
|
|
|
|
|
} => {
|
|
|
|
|
slog!(
|
|
|
|
|
"[whatsapp] Handling assign command from {sender}: story {story_number} model {model}"
|
|
|
|
|
);
|
2026-03-27 15:07:15 +00:00
|
|
|
crate::chat::transport::matrix::assign::handle_assign(
|
2026-04-25 19:47:49 +00:00
|
|
|
&ctx.services.bot_name,
|
2026-03-27 15:07:15 +00:00
|
|
|
&story_number,
|
|
|
|
|
&model,
|
2026-04-25 19:47:49 +00:00
|
|
|
&ctx.services.project_root,
|
|
|
|
|
&ctx.services.agents,
|
2026-03-27 15:07:15 +00:00
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
crate::chat::transport::matrix::assign::AssignCommand::BadArgs => {
|
2026-04-25 19:47:49 +00:00
|
|
|
format!("Usage: `{} assign <number> <model>`", ctx.services.bot_name)
|
2026-03-27 15:07:15 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let formatted = markdown_to_whatsapp(&response);
|
|
|
|
|
let _ = ctx.transport.send_message(sender, &formatted, "").await;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 10:49:39 +00:00
|
|
|
// No command matched — forward to LLM for conversational response.
|
|
|
|
|
slog!("[whatsapp] No command matched, forwarding to LLM for {sender}");
|
|
|
|
|
handle_llm_message(ctx, sender, message).await;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Forward a message to Claude Code and send the response back via WhatsApp.
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::super::WhatsAppWebhookContext;
|
2026-04-13 14:07:08 +00:00
|
|
|
use super::super::history::{MessagingWindowTracker, WhatsAppConversationHistory};
|
2026-03-27 10:49:39 +00:00
|
|
|
use super::*;
|
2026-04-13 14:07:08 +00:00
|
|
|
use crate::agents::AgentPool;
|
|
|
|
|
use crate::chat::transport::matrix::{ConversationEntry, ConversationRole, RoomConversation};
|
|
|
|
|
use crate::io::watcher::WatcherEvent;
|
2026-03-27 10:49:39 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use tokio::sync::Mutex as TokioMutex;
|
|
|
|
|
|
|
|
|
|
/// Build a minimal WhatsAppWebhookContext for allowlist tests.
|
2026-04-13 14:07:08 +00:00
|
|
|
fn make_ctx_with_allowlist(allowed_phones: Vec<String>) -> Arc<WhatsAppWebhookContext> {
|
2026-03-27 10:49:39 +00:00
|
|
|
struct NullTransport;
|
|
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
|
impl crate::chat::ChatTransport for NullTransport {
|
|
|
|
|
async fn send_message(
|
|
|
|
|
&self,
|
|
|
|
|
_room: &str,
|
|
|
|
|
_plain: &str,
|
|
|
|
|
_html: &str,
|
|
|
|
|
) -> Result<crate::chat::MessageId, String> {
|
|
|
|
|
Ok(String::new())
|
|
|
|
|
}
|
|
|
|
|
async fn edit_message(
|
|
|
|
|
&self,
|
|
|
|
|
_room: &str,
|
|
|
|
|
_id: &str,
|
|
|
|
|
_plain: &str,
|
|
|
|
|
_html: &str,
|
|
|
|
|
) -> Result<(), String> {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
async fn send_typing(&self, _room: &str, _typing: bool) -> Result<(), String> {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
|
|
|
let (tx, _rx) = tokio::sync::broadcast::channel::<WatcherEvent>(16);
|
|
|
|
|
let agents = Arc::new(AgentPool::new(3999, tx));
|
|
|
|
|
let tracker = Arc::new(MessagingWindowTracker::new());
|
|
|
|
|
let (_perm_tx, perm_rx) = tokio::sync::mpsc::unbounded_channel();
|
2026-04-25 19:47:49 +00:00
|
|
|
let services = Arc::new(crate::services::Services {
|
2026-03-27 10:49:39 +00:00
|
|
|
project_root: tmp.path().to_path_buf(),
|
2026-04-27 18:00:53 +00:00
|
|
|
status: agents.status_broadcaster(),
|
2026-03-27 10:49:39 +00:00
|
|
|
agents,
|
|
|
|
|
bot_name: "Bot".to_string(),
|
|
|
|
|
bot_user_id: "whatsapp-bot".to_string(),
|
|
|
|
|
ambient_rooms: Arc::new(std::sync::Mutex::new(Default::default())),
|
2026-04-25 19:47:49 +00:00
|
|
|
perm_rx: Arc::new(tokio::sync::Mutex::new(perm_rx)),
|
|
|
|
|
pending_perm_replies: Arc::new(tokio::sync::Mutex::new(Default::default())),
|
|
|
|
|
permission_timeout_secs: 120,
|
2026-05-15 11:57:00 +00:00
|
|
|
chat_dispatcher: Arc::new(crate::chat::dispatcher::ChatDispatcher::new(1_500)),
|
2026-04-25 19:47:49 +00:00
|
|
|
});
|
|
|
|
|
Arc::new(WhatsAppWebhookContext {
|
|
|
|
|
services,
|
|
|
|
|
verify_token: "tok".to_string(),
|
|
|
|
|
provider: "meta".to_string(),
|
|
|
|
|
transport: Arc::new(NullTransport),
|
2026-03-27 10:49:39 +00:00
|
|
|
history: Arc::new(tokio::sync::Mutex::new(Default::default())),
|
|
|
|
|
history_size: 20,
|
|
|
|
|
window_tracker: tracker,
|
|
|
|
|
allowed_phones,
|
2026-04-28 20:44:31 +00:00
|
|
|
app_secret: String::new(),
|
2026-04-28 23:12:31 +00:00
|
|
|
twilio_auth_token: String::new(),
|
2026-03-27 10:49:39 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 10:24:37 +00:00
|
|
|
// ── OAuth login link formatting ───────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn whatsapp_oauth_error_produces_plain_text_url() {
|
|
|
|
|
let err = "OAuth session expired or credentials missing. Please log in: http://localhost:3001/oauth/authorize";
|
|
|
|
|
let url = crate::llm::oauth::extract_login_url_from_error(err);
|
|
|
|
|
assert!(url.is_some(), "should extract URL from OAuth error");
|
2026-04-13 14:07:08 +00:00
|
|
|
let msg = format!(
|
|
|
|
|
"Authentication required. Log in to Claude here: {}",
|
|
|
|
|
url.unwrap()
|
|
|
|
|
);
|
2026-03-31 10:24:37 +00:00
|
|
|
assert!(msg.contains("http://localhost:3001/oauth/authorize"));
|
2026-04-13 14:07:08 +00:00
|
|
|
assert!(
|
|
|
|
|
!msg.contains('['),
|
|
|
|
|
"WhatsApp message should not use Markdown link syntax"
|
|
|
|
|
);
|
2026-03-31 10:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn whatsapp_non_oauth_error_not_formatted_as_link() {
|
|
|
|
|
let err = "Some unrelated error occurred during processing";
|
|
|
|
|
assert!(crate::llm::oauth::extract_login_url_from_error(err).is_none());
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 10:49:39 +00:00
|
|
|
// ── Allowlist tests ───────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn allowlist_blocks_unauthorized_sender() {
|
|
|
|
|
let allowed = vec!["+15551111111".to_string()];
|
|
|
|
|
let ctx = make_ctx_with_allowlist(allowed);
|
|
|
|
|
let unauthorized = "+15559999999";
|
|
|
|
|
|
|
|
|
|
handle_incoming_message(&ctx, unauthorized, "hello").await;
|
|
|
|
|
|
|
|
|
|
// window_tracker is only updated AFTER the allowlist check, so an
|
|
|
|
|
// unauthorized sender must leave the tracker untouched.
|
|
|
|
|
assert!(
|
|
|
|
|
!ctx.window_tracker.is_within_window(unauthorized),
|
|
|
|
|
"unauthorized sender should not have updated the window tracker"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn allowlist_empty_allows_all_senders() {
|
|
|
|
|
// Empty allowlist = open (backwards compatible).
|
|
|
|
|
let ctx = make_ctx_with_allowlist(vec![]);
|
|
|
|
|
let sender = "+15551234567";
|
|
|
|
|
|
|
|
|
|
handle_incoming_message(&ctx, sender, "hello").await;
|
|
|
|
|
|
|
|
|
|
// window_tracker.record_message is called right after the allowlist
|
|
|
|
|
// check passes, so the sender should be recorded.
|
|
|
|
|
assert!(
|
|
|
|
|
ctx.window_tracker.is_within_window(sender),
|
|
|
|
|
"sender should be recorded when allowlist is empty"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn allowlist_allows_listed_sender() {
|
|
|
|
|
let sender = "+15551111111";
|
|
|
|
|
let ctx = make_ctx_with_allowlist(vec![sender.to_string()]);
|
|
|
|
|
|
|
|
|
|
handle_incoming_message(&ctx, sender, "hello").await;
|
|
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
|
ctx.window_tracker.is_within_window(sender),
|
|
|
|
|
"listed sender should be recorded in the window tracker"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── reset command extraction ───────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn reset_command_extracted_from_plain_message() {
|
|
|
|
|
let result = crate::chat::transport::matrix::reset::extract_reset_command(
|
|
|
|
|
"reset",
|
|
|
|
|
"Timmy",
|
|
|
|
|
"@timmy:home.local",
|
|
|
|
|
);
|
|
|
|
|
assert!(result.is_some(), "plain 'reset' should be recognised");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn reset_command_extracted_with_bot_name_prefix() {
|
|
|
|
|
let result = crate::chat::transport::matrix::reset::extract_reset_command(
|
|
|
|
|
"Timmy reset",
|
|
|
|
|
"Timmy",
|
|
|
|
|
"@timmy:home.local",
|
|
|
|
|
);
|
|
|
|
|
assert!(result.is_some(), "'Timmy reset' should be recognised");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn reset_command_clears_whatsapp_session() {
|
|
|
|
|
let sender = "+15555550100";
|
|
|
|
|
let history: WhatsAppConversationHistory = Arc::new(TokioMutex::new({
|
|
|
|
|
let mut m = HashMap::new();
|
2026-04-13 14:07:08 +00:00
|
|
|
m.insert(
|
|
|
|
|
sender.to_string(),
|
|
|
|
|
RoomConversation {
|
|
|
|
|
session_id: Some("old-session".to_string()),
|
|
|
|
|
entries: vec![ConversationEntry {
|
|
|
|
|
role: ConversationRole::User,
|
|
|
|
|
sender: sender.to_string(),
|
|
|
|
|
content: "previous message".to_string(),
|
|
|
|
|
}],
|
|
|
|
|
},
|
|
|
|
|
);
|
2026-03-27 10:49:39 +00:00
|
|
|
m
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
2026-04-03 16:12:52 +01:00
|
|
|
let sk = tmp.path().join(".huskies");
|
2026-03-27 10:49:39 +00:00
|
|
|
std::fs::create_dir_all(&sk).unwrap();
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let mut guard = history.lock().await;
|
2026-04-13 14:07:08 +00:00
|
|
|
let conv = guard
|
|
|
|
|
.entry(sender.to_string())
|
|
|
|
|
.or_insert_with(RoomConversation::default);
|
2026-03-27 10:49:39 +00:00
|
|
|
conv.session_id = None;
|
|
|
|
|
conv.entries.clear();
|
|
|
|
|
save_whatsapp_history(tmp.path(), &guard);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let guard = history.lock().await;
|
|
|
|
|
let conv = guard.get(sender).unwrap();
|
|
|
|
|
assert!(conv.session_id.is_none(), "session_id should be cleared");
|
|
|
|
|
assert!(conv.entries.is_empty(), "entries should be cleared");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn start_command_extracted_from_plain_message() {
|
|
|
|
|
// WhatsApp messages arrive without a bot mention prefix.
|
|
|
|
|
// extract_start_command must recognise "start 42" by itself.
|
|
|
|
|
let result = crate::chat::transport::matrix::start::extract_start_command(
|
|
|
|
|
"start 42",
|
|
|
|
|
"Timmy",
|
|
|
|
|
"@timmy:home.local",
|
|
|
|
|
);
|
|
|
|
|
assert!(result.is_some(), "plain 'start 42' should be recognised");
|
|
|
|
|
assert_eq!(
|
|
|
|
|
result,
|
|
|
|
|
Some(crate::chat::transport::matrix::start::StartCommand::Start {
|
|
|
|
|
story_number: "42".to_string(),
|
|
|
|
|
agent_hint: None,
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn start_command_extracted_with_bot_name_prefix() {
|
|
|
|
|
let result = crate::chat::transport::matrix::start::extract_start_command(
|
|
|
|
|
"Timmy start 99",
|
|
|
|
|
"Timmy",
|
|
|
|
|
"@timmy:home.local",
|
|
|
|
|
);
|
|
|
|
|
assert!(result.is_some(), "'Timmy start 99' should be recognised");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn non_start_whatsapp_message_not_extracted() {
|
|
|
|
|
let result = crate::chat::transport::matrix::start::extract_start_command(
|
|
|
|
|
"help",
|
|
|
|
|
"Timmy",
|
|
|
|
|
"@timmy:home.local",
|
|
|
|
|
);
|
|
|
|
|
assert!(result.is_none(), "'help' should not be recognised as start");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── rmtree command extraction ──────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn rmtree_command_extracted_from_plain_message() {
|
|
|
|
|
// WhatsApp messages arrive without a bot mention prefix.
|
|
|
|
|
// extract_rmtree_command must recognise "rmtree 42" by itself.
|
|
|
|
|
let result = crate::chat::transport::matrix::rmtree::extract_rmtree_command(
|
|
|
|
|
"rmtree 42",
|
|
|
|
|
"Timmy",
|
|
|
|
|
"@timmy:home.local",
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
matches!(
|
|
|
|
|
result,
|
|
|
|
|
Some(crate::chat::transport::matrix::rmtree::RmtreeCommand::Rmtree { .. })
|
|
|
|
|
),
|
|
|
|
|
"plain 'rmtree 42' should be recognised"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn rmtree_command_extracted_with_bot_name_prefix() {
|
|
|
|
|
let result = crate::chat::transport::matrix::rmtree::extract_rmtree_command(
|
|
|
|
|
"Timmy rmtree 42",
|
|
|
|
|
"Timmy",
|
|
|
|
|
"@timmy:home.local",
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
matches!(
|
|
|
|
|
result,
|
|
|
|
|
Some(crate::chat::transport::matrix::rmtree::RmtreeCommand::Rmtree { .. })
|
|
|
|
|
),
|
|
|
|
|
"'Timmy rmtree 42' should be recognised"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn rmtree_command_returns_bad_args_without_number() {
|
|
|
|
|
let result = crate::chat::transport::matrix::rmtree::extract_rmtree_command(
|
|
|
|
|
"rmtree",
|
|
|
|
|
"Timmy",
|
|
|
|
|
"@timmy:home.local",
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
result,
|
|
|
|
|
Some(crate::chat::transport::matrix::rmtree::RmtreeCommand::BadArgs)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn non_rmtree_whatsapp_message_not_extracted() {
|
|
|
|
|
let result = crate::chat::transport::matrix::rmtree::extract_rmtree_command(
|
|
|
|
|
"status",
|
|
|
|
|
"Timmy",
|
|
|
|
|
"@timmy:home.local",
|
|
|
|
|
);
|
2026-04-13 14:07:08 +00:00
|
|
|
assert!(
|
|
|
|
|
result.is_none(),
|
|
|
|
|
"'status' should not be recognised as rmtree"
|
|
|
|
|
);
|
2026-03-27 10:49:39 +00:00
|
|
|
}
|
2026-03-27 15:07:15 +00:00
|
|
|
|
|
|
|
|
// ── assign command extraction ──────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn assign_command_extracted_from_plain_message() {
|
|
|
|
|
let result = crate::chat::transport::matrix::assign::extract_assign_command(
|
|
|
|
|
"assign 42 opus",
|
|
|
|
|
"Timmy",
|
|
|
|
|
"@timmy:home.local",
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
matches!(
|
|
|
|
|
result,
|
|
|
|
|
Some(crate::chat::transport::matrix::assign::AssignCommand::Assign { .. })
|
|
|
|
|
),
|
|
|
|
|
"plain 'assign 42 opus' should be recognised"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn assign_command_extracted_with_bot_name_prefix() {
|
|
|
|
|
let result = crate::chat::transport::matrix::assign::extract_assign_command(
|
|
|
|
|
"Timmy assign 42 sonnet",
|
|
|
|
|
"Timmy",
|
|
|
|
|
"@timmy:home.local",
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
matches!(
|
|
|
|
|
result,
|
|
|
|
|
Some(crate::chat::transport::matrix::assign::AssignCommand::Assign { .. })
|
|
|
|
|
),
|
|
|
|
|
"'Timmy assign 42 sonnet' should be recognised"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn assign_command_returns_bad_args_without_model() {
|
|
|
|
|
let result = crate::chat::transport::matrix::assign::extract_assign_command(
|
|
|
|
|
"assign 42",
|
|
|
|
|
"Timmy",
|
|
|
|
|
"@timmy:home.local",
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
result,
|
|
|
|
|
Some(crate::chat::transport::matrix::assign::AssignCommand::BadArgs)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn non_assign_whatsapp_message_not_extracted() {
|
|
|
|
|
let result = crate::chat::transport::matrix::assign::extract_assign_command(
|
|
|
|
|
"status",
|
|
|
|
|
"Timmy",
|
|
|
|
|
"@timmy:home.local",
|
|
|
|
|
);
|
2026-04-13 14:07:08 +00:00
|
|
|
assert!(
|
|
|
|
|
result.is_none(),
|
|
|
|
|
"'status' should not be recognised as assign"
|
|
|
|
|
);
|
2026-03-27 15:07:15 +00:00
|
|
|
}
|
2026-03-27 10:49:39 +00:00
|
|
|
}
|