Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

11.1 KB
Raw Blame
use super::*;
use agent_trigger::domain::broker_events::{
    AgentAssignedToTaskEvent, AgentBotMentionedEvent, AgentMentionedEvent, ChannelEventMetadata,
    ExistingAgentSessionEvent, NewAgentSessionEvent, ThreadEventMetadata, ThreadMessageKind,
};
use channel_sender::ChannelSender;
use channels::domain::broker_events::ChannelMessagePostedMetadata;
use channels::domain::models::ChannelType;
use chrono::Utc;
use messages::domain::events::MessagePostedMetadata;
use messages::domain::models::MessageParent;
use std::sync::Mutex;

#[test]
fn repository_selection_survives_dispatch_and_older_events_still_decode() {
    let mut wire = serde_json::json!({"bot_id": bot_id::BotId::TEST_A, "session_id": test_session(), "owner": sender().to_string()});
    let old: AgentSessionRequestedEvent = serde_json::from_value(wire.clone()).unwrap();
    assert!(old.repo_url.is_none());
    wire["repo_url"] = serde_json::json!("https://github.com/org/project");
    let event: AgentSessionRequestedEvent = serde_json::from_value(wire).unwrap();
    let work = trigger_to_work(AgentTriggerTopicEvent::New(
        NewAgentSessionEvent::Requested(event),
    ))
    .unwrap();
    assert!(
        matches!(work, TriggerWork::OpenRequested { repo_url: Some(url), .. } if url == "https://github.com/org/project")
    );
}

fn test_session() -> AgentSessionId {
    AgentSessionId::new_from_uuid(Uuid::from_u128(0xA))
}

fn sender() -> MacroUserIdStr<'static> {
    MacroUserIdStr::try_from_email("[email protected]").unwrap()
}

/// A channel post in the channel-only shape channel triggers travel in.
fn message(content: &str, thread_id: Option<Uuid>) -> ChannelMessagePostedMetadata {
    ChannelMessagePostedMetadata {
        channel_id: Uuid::from_u128(1),
        message_id: Uuid::from_u128(2),
        thread_id,
        sender: ChannelSender::new_from_user(sender()),
        triggered_by: None,
        channel_type: ChannelType::Public,
        content: content.to_owned(),
        mentions: vec![],
        attachments: vec![],
        created_at: Utc::now(),
    }
}

fn document() -> MessageParent {
    MessageParent::parse("document", "doc-1").unwrap()
}

fn document_message(content: &str, thread_id: Option<Uuid>) -> MessagePostedMetadata {
    MessagePostedMetadata {
        parent: document(),
        message_id: Uuid::from_u128(2),
        thread_id,
        root_id: thread_id.unwrap_or(Uuid::from_u128(2)),
        sender: ChannelSender::new_from_user(sender()),
        triggered_by: None,
        content: content.to_owned(),
        mentions: vec![],
        attachments: vec![],
        created_at: Utc::now(),
    }
}

fn mention(content: &str) -> AgentTriggerTopicEvent {
    AgentTriggerTopicEvent::New(NewAgentSessionEvent::TopLevelMentioned(
        AgentBotMentionedEvent {
            bot_id: bot_id::BotId::TEST_A,
            message: message(content, None),
            context: None,
        },
    ))
}

fn follow_up(content: &str) -> AgentTriggerTopicEvent {
    AgentTriggerTopicEvent::Existing(ExistingAgentSessionEvent::Channel(ChannelEventMetadata {
        bot_id: bot_id::BotId::TEST_A,
        session_id: test_session(),
        kind: ThreadMessageKind::MentionThread,
        message: message(content, Some(Uuid::from_u128(7))),
        context: None,
    }))
}

/// Document triggers arrive in the parent-aware shape and become the same
/// work as channel ones, addressed to the document.
#[test]
fn document_triggers_become_work_on_the_document() {
    let opened =
        AgentTriggerTopicEvent::New(NewAgentSessionEvent::Mentioned(AgentMentionedEvent {
            bot_id: bot_id::BotId::TEST_A,
            message: document_message("summarize this", None),
            context: None,
        }));
    assert_eq!(
        trigger_to_work(opened).expect("a document mention is work"),
        TriggerWork::OpenAndPrompt {
            origin: Box::new(trigger_context::TriggerOrigin::Mentioned {
                by: trigger_context::origin::Caller::Person { user: sender() },
                message_id: Uuid::from_u128(2),
            }),
            reuse_origin_message: false,
            bot: bot_id::BotId::TEST_A,
            sender: sender(),
            parent: document(),
            thread_id: Uuid::from_u128(2),
            message_id: Uuid::from_u128(2),
            content: "summarize this".to_owned(),
            context: None,
        }
    );
    let followed =
        AgentTriggerTopicEvent::Existing(ExistingAgentSessionEvent::Thread(ThreadEventMetadata {
            bot_id: bot_id::BotId::TEST_A,
            session_id: test_session(),
            kind: ThreadMessageKind::Inferred,
            message: document_message("and this", Some(Uuid::from_u128(7))),
            context: None,
        }));
    assert_eq!(
        trigger_to_work(followed).expect("a document follow-up is work"),
        TriggerWork::PromptExisting {
            message_id: Uuid::from_u128(2),
            session: test_session(),
            sender: sender(),
            content: "and this".to_owned(),
            context: None,
        }
    );
}

#[test]
fn a_task_assignment_opens_and_prompts_in_the_task_discussion() {
    let event = AgentTriggerTopicEvent::New(NewAgentSessionEvent::AssignedToTask(
        AgentAssignedToTaskEvent {
            origin: trigger_context::TriggerOrigin::Unknown,
            bot_id: BotId::TEST_A,
            parent: document(),
            discussion_id: Uuid::from_u128(2),
            actor: sender(),
            prompt: "Complete the assigned task".to_owned(),
            context: None,
        },
    ));
    assert_eq!(
        trigger_to_work(event).expect("a task assignment is work"),
        TriggerWork::OpenAndPrompt {
            origin: Box::new(trigger_context::TriggerOrigin::Unknown),
            reuse_origin_message: true,
            bot: BotId::TEST_A,
            sender: sender(),
            parent: document(),
            thread_id: Uuid::from_u128(2),
            message_id: Uuid::from_u128(2),
            content: "Complete the assigned task".to_owned(),
            context: None,
        }
    );
}

#[test]
fn a_mention_becomes_open_and_prompt_rooting_its_own_thread() {
    let work = trigger_to_work(mention("fix the test")).expect("a mention is work");
    assert_eq!(
        work,
        TriggerWork::OpenAndPrompt {
            origin: Box::new(trigger_context::TriggerOrigin::Mentioned {
                by: trigger_context::origin::Caller::Person { user: sender() },
                message_id: Uuid::from_u128(2),
            }),
            reuse_origin_message: false,
            bot: bot_id::BotId::TEST_A,
            sender: sender(),
            parent: messages::domain::models::MessageParent::Channel(Uuid::from_u128(1)),
            thread_id: Uuid::from_u128(2),
            message_id: Uuid::from_u128(2),
            content: "fix the test".to_owned(),
            context: None,
        }
    );
}

#[test]
fn a_follow_up_becomes_prompt_existing() {
    let work = trigger_to_work(follow_up("keep going")).expect("a follow-up is work");
    assert_eq!(
        work,
        TriggerWork::PromptExisting {
            message_id: Uuid::from_u128(2),
            session: test_session(),
            sender: sender(),
            content: "keep going".to_owned(),
            context: None,
        }
    );
}

#[test]
fn an_unattributed_bot_message_is_skipped() {
    let mut event = mention("self talk");
    if let AgentTriggerTopicEvent::New(NewAgentSessionEvent::TopLevelMentioned(mentioned)) =
        &mut event
    {
        mentioned.message.sender = ChannelSender::new_from_bot(bot_id::BotId::TEST_B);
    }
    assert_eq!(trigger_to_work(event), Err(Skipped::NotFromUser));
}

#[test]
fn trigger_filters_name_this_bot_and_every_trigger_event() {
    let filters = trigger_filters([bot_id::BotId::TEST_A]);
    assert_eq!(filters.len(), 1);
    let filter = &filters[0];
    assert_eq!(
        filter.events,
        vec![
            "agent_trigger.new".to_owned(),
            "agent_trigger.existing".to_owned()
        ]
    );
    assert_eq!(
        filter.ids.as_deref(),
        Some([bot_id::BotId::TEST_A.to_string()].as_slice())
    );
}

/// Records executed work instead of doing anything.
#[derive(Default)]
struct RecordingExecutor {
    executed: Mutex<Vec<TriggerWork>>,
    fail: bool,
}

impl WorkExecutor for std::sync::Arc<RecordingExecutor> {
    async fn execute(&self, work: TriggerWork) -> Result<(), crate::dispatch::DispatchError> {
        self.executed.lock().unwrap().push(work);
        if self.fail {
            return Err(crate::dispatch::DispatchError::Api(
                crate::outbound::agent_session::ApiError::Refused {
                    status: reqwest::StatusCode::INTERNAL_SERVER_ERROR,
                    message: "boom".to_owned(),
                },
            ));
        }
        Ok(())
    }
}

#[tokio::test]
async fn a_mention_event_executes() {
    let executor = std::sync::Arc::new(RecordingExecutor::default());

    handle_event(Event::new(mention("fix it")), &executor)
        .await
        .expect("a mention is work");

    assert_eq!(executor.executed.lock().unwrap().len(), 1);
}

#[tokio::test]
async fn a_skipped_event_is_not_an_error() {
    let executor = std::sync::Arc::new(RecordingExecutor::default());
    let mut event = mention("self talk");
    if let AgentTriggerTopicEvent::New(NewAgentSessionEvent::TopLevelMentioned(mentioned)) =
        &mut event
    {
        mentioned.message.sender = ChannelSender::new_from_bot(bot_id::BotId::TEST_B);
    }

    handle_event(Event::new(event), &executor)
        .await
        .expect("skipped events are not errors");

    assert!(executor.executed.lock().unwrap().is_empty());
}

#[tokio::test]
async fn failed_work_is_returned() {
    let executor = std::sync::Arc::new(RecordingExecutor {
        fail: true,
        ..Default::default()
    });

    assert!(
        handle_event(Event::new(mention("fix it")), &executor)
            .await
            .is_err()
    );
}

#[test]
fn attributed_bot_mentions_open_and_prompt_with_the_invoking_user() {
    let mut open = mention("fix the test");
    if let AgentTriggerTopicEvent::New(NewAgentSessionEvent::TopLevelMentioned(event)) = &mut open {
        event.message.sender = ChannelSender::new_from_bot(bot_id::MACRO_NEW_BOT_ID);
        event.message.triggered_by = Some(sender().to_string());
    }
    assert!(
        matches!(trigger_to_work(open).unwrap(), TriggerWork::OpenAndPrompt { sender: actor, .. } if actor == sender())
    );
    let mut follow_up = follow_up("continue");
    if let AgentTriggerTopicEvent::Existing(ExistingAgentSessionEvent::Channel(event)) =
        &mut follow_up
    {
        event.message.sender = ChannelSender::new_from_bot(bot_id::MACRO_NEW_BOT_ID);
        event.message.triggered_by = Some(sender().to_string());
    }
    assert!(
        matches!(trigger_to_work(follow_up).unwrap(), TriggerWork::PromptExisting { sender: actor, .. } if actor == sender())
    );
}

#[test]
fn a_mention_tells_the_runtime_what_opened_its_session() {
    let Ok(TriggerWork::OpenAndPrompt { origin, .. }) = trigger_to_work(mention("hello")) else {
        panic!("expected a session to open");
    };

    assert!(matches!(
        *origin,
        trigger_context::TriggerOrigin::Mentioned { .. }
    ));
}