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.

7.4 KB
Raw Blame
use ::activity::Action;
use ::activity::Actor;
use ::activity::EntityType;
use bot_id::BotId;
use chrono::Utc;
use macro_user_id::user_id::MacroUserIdStr;
use uuid::Uuid;

use macro_event_broker::Event;

use super::*;
use crate::domain::broker_events::{ChannelCreatedMetadata, ChannelParticipantAddedMetadata};
use crate::domain::models::ChannelType;
use messages::domain::events::MessagePostedMetadata;

fn user(id: &str) -> MacroUserIdStr<'static> {
    MacroUserIdStr::try_from(id.to_string()).expect("valid user id")
}

fn envelope(event: ChannelTopicEvent) -> Event<ChannelTopicEvent> {
    Event::with_event_id(Uuid::now_v7(), event)
}

const CHANNEL_ID: Uuid = Uuid::from_u128(7);

fn posted(parent: MessageParent, created_at: chrono::DateTime<Utc>) -> MessagePostedMetadata {
    MessagePostedMetadata {
        parent,
        message_id: Uuid::from_u128(8),
        thread_id: None,
        root_id: Uuid::from_u128(8),
        sender: Actor::new_from_bot(BotId::new_from_uuid(Uuid::from_u128(9))),
        triggered_by: Some("macro|[email protected]".to_string()),
        content: "hi".to_string(),
        mentions: vec![],
        attachments: vec![],
        created_at,
    }
}

#[test]
fn rename_preserves_names_and_skips_noops() {
    let metadata = crate::domain::broker_events::ChannelUpdatedMetadata {
        channel_id: CHANNEL_ID,
        actor: user("macro|[email protected]"),
        previous_name: Some("Before".into()),
        channel_name: Some("After".into()),
    };
    let event = envelope(ChannelTopicEvent::Updated(metadata.clone()));
    let Ingest::Insert(rows) = event.event.ingest(event.event_id) else {
        panic!("expected rename");
    };
    assert_eq!(
        rows[0].action,
        Action::Renamed(::activity::domain::models::NameChange {
            from: Some("Before".into()),
            to: Some("After".into()),
        })
    );
    let unchanged = envelope(ChannelTopicEvent::Updated(
        crate::domain::broker_events::ChannelUpdatedMetadata {
            channel_name: metadata.previous_name.clone(),
            ..metadata
        },
    ));
    assert_eq!(unchanged.event.ingest(unchanged.event_id), Ingest::Ignore);
}

#[test]
fn picture_changes_are_attributed_and_replay_stable() {
    let event = envelope(ChannelTopicEvent::PictureChanged(
        crate::domain::broker_events::ChannelPictureChangedMetadata {
            channel_id: CHANNEL_ID,
            actor: user("macro|[email protected]"),
        },
    ));
    let first = event.event.ingest(event.event_id);
    assert_eq!(first, event.event.ingest(event.event_id));
    let Ingest::Insert(rows) = first else {
        panic!("expected picture activity");
    };
    assert_eq!(rows[0].action, Action::PictureChanged);
    assert_eq!(rows[0].actor.as_ref(), "macro|[email protected]");
    assert_eq!(rows[0].entity_id, CHANNEL_ID.to_string());
}

#[test]
fn message_posted_maps_to_messaged_with_triggered_by_as_subject() {
    let created_at = Utc::now();
    let event_id = Uuid::now_v7();
    let event = MessageTopicEvent::Posted(posted(MessageParent::Channel(CHANNEL_ID), created_at));

    let Ingest::Insert(activities) = ingest_message_event(event_id, &event) else {
        panic!("expected activities");
    };
    assert_eq!(activities.len(), 1);
    assert_eq!(activities[0].action, Action::Messaged);
    // Delegation: the triggering user is the subject; the sender the actor.
    assert_eq!(activities[0].subject_id, "macro|[email protected]");
    assert_eq!(
        activities[0].actor.as_ref(),
        Actor::new_from_bot(BotId::new_from_uuid(Uuid::from_u128(9))).as_ref()
    );
    assert_eq!(activities[0].occurred_at, created_at);
    assert_eq!(activities[0].entity_id, CHANNEL_ID.to_string());
    assert_eq!(activities[0].entity_type, EntityType::Channel);
}

#[test]
fn a_message_from_an_account_manager_stays_the_senders_activity() {
    let mut posted = posted(MessageParent::Channel(CHANNEL_ID), Utc::now());
    posted.sender = Actor::new_from_user(user("macro|[email protected]"));
    posted.triggered_by = Some("macro|[email protected]".to_string());

    let Ingest::Insert(activities) =
        ingest_message_event(Uuid::now_v7(), &MessageTopicEvent::Posted(posted))
    else {
        panic!("expected activities");
    };

    assert_eq!(activities[0].subject_id, "macro|[email protected]");
}

#[test]
fn document_discussion_posts_record_no_activity() {
    let event = MessageTopicEvent::Posted(posted(
        MessageParent::parse("document", "doc-1").unwrap(),
        Utc::now(),
    ));
    assert!(matches!(
        ingest_message_event(Uuid::now_v7(), &event),
        Ingest::Ignore
    ));
}

#[test]
fn participant_added_yields_one_activity_per_user_with_stable_ordinals() {
    let event = envelope(ChannelTopicEvent::ParticipantAdded(
        ChannelParticipantAddedMetadata {
            channel_id: CHANNEL_ID,
            channel_type: ChannelType::Public,
            added_by: Actor::new_from_user(user("macro|[email protected]")),
            added_user_ids: vec![user("macro|[email protected]"), user("macro|[email protected]")],
        },
    ));

    let Ingest::Insert(activities) = event.event.ingest(event.event_id) else {
        panic!("expected activities");
    };
    assert_eq!(activities.len(), 2);
    assert_ne!(activities[0].id, activities[1].id);
    assert!(
        activities
            .iter()
            .all(|a| a.subject_id == "macro|[email protected]")
    );
    assert_eq!(
        activities[0].action,
        Action::ParticipantAdded(::activity::ParticipantChange {
            participant: Actor::new_from_user(user("macro|[email protected]"))
        })
    );

    // Replay derives identical ids.
    let Ingest::Insert(replayed) = event.event.ingest(event.event_id) else {
        panic!("expected activities");
    };
    assert_eq!(activities[0].id, replayed[0].id);
    assert_eq!(activities[1].id, replayed[1].id);
}

#[test]
fn created_maps_to_created_by_the_actor() {
    let event = envelope(ChannelTopicEvent::Created(ChannelCreatedMetadata {
        channel_id: CHANNEL_ID,
        actor: Actor::new_from_user(user("macro|[email protected]")),
        on_behalf_of: None,
        channel_type: ChannelType::Public,
        channel_name: Some("general".to_string()),
        participant_user_ids: vec![user("macro|[email protected]")],
    }));

    let Ingest::Insert(activities) = event.event.ingest(event.event_id) else {
        panic!("expected activities");
    };
    assert_eq!(activities[0].action, Action::Created);
    assert_eq!(activities[0].actor.as_ref(), "macro|[email protected]");
    assert_eq!(activities[0].subject_id, "macro|[email protected]");
}

#[test]
fn created_by_system_stays_on_the_owner_feed() {
    let event = envelope(ChannelTopicEvent::Created(ChannelCreatedMetadata {
        channel_id: CHANNEL_ID,
        actor: Actor::new_from_bot(bot_id::MACRO_SYSTEM_BOT_ID),
        on_behalf_of: Some(user("macro|[email protected]")),
        channel_type: ChannelType::Private,
        channel_name: Some("Macro Support x owner".to_string()),
        participant_user_ids: vec![user("macro|[email protected]")],
    }));

    let Ingest::Insert(activities) = event.event.ingest(event.event_id) else {
        panic!("expected activities");
    };
    assert_eq!(activities[0].action, Action::Created);
    assert_eq!(
        activities[0].actor.as_ref(),
        Actor::new_from_bot(bot_id::MACRO_SYSTEM_BOT_ID).as_ref()
    );
    assert_eq!(activities[0].subject_id, "macro|[email protected]");
}