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.

4.4 KB
Raw Blame
use ::activity::Action;
use chrono::Utc;
use macro_user_id::user_id::MacroUserIdStr;
use uuid::Uuid;

use macro_event_broker::Event;

use super::*;
use crate::domain::events::{CallRecordDeletedMetadata, CallStartedMetadata};

fn archived(
    channel_id: Option<Uuid>,
    duration_ms: Option<i64>,
    ended_at: chrono::DateTime<Utc>,
) -> Event<CallTopicEvent> {
    Event::with_event_id(
        Uuid::now_v7(),
        CallTopicEvent::RecordArchived(crate::domain::events::CallRecordArchivedMetadata {
            call_id: Uuid::from_u128(5),
            channel_id,
            created_by: "macro|[email protected]".to_string().try_into().unwrap(),
            started_at: ended_at - chrono::Duration::minutes(8),
            ended_at,
            duration_ms,
            participant_count: 2,
            participants: vec![
                "macro|[email protected]".to_string().try_into().unwrap(),
                "macro|[email protected]".to_string().try_into().unwrap(),
            ],
            has_recording: false,
            archive_reason: crate::domain::events::CallArchiveReason::LastParticipantLeft,
            channel_thread: false,
        }),
    )
}

#[test]
fn a_channel_thread_calls_ending_stays_out_of_the_channels_timeline() {
    let mut event = archived(Some(Uuid::from_u128(9)), Some(480_000), Utc::now());
    let CallTopicEvent::RecordArchived(metadata) = &mut event.event else {
        panic!("expected an archived call");
    };
    metadata.channel_thread = true;

    let Ingest::Insert(activities) = event.event.ingest(event.event_id) else {
        panic!("expected the ending to be recorded");
    };

    assert_eq!(activities.len(), 1);
    assert_eq!(activities[0].entity_type, EntityType::Call);
    assert_eq!(activities[0].entity_id, Uuid::from_u128(5).to_string());
}

#[test]
fn archived_calls_without_a_channel_have_no_timeline() {
    let event = archived(None, Some(480_000), Utc::now());
    assert!(matches!(event.event.ingest(event.event_id), Ingest::Ignore));
}

#[test]
fn archived_calls_record_duration_without_requiring_a_recording() {
    for duration_ms in [Some(480_000), None, Some(-1)] {
        let ended_at = Utc::now();
        let event = archived(Some(Uuid::from_u128(6)), duration_ms, ended_at);
        let Ingest::Insert(rows) = event.event.ingest(event.event_id) else {
            panic!("expected completed call");
        };
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].occurred_at, ended_at);
        assert_eq!(
            rows[0].action,
            Action::CallEnded(::activity::domain::models::CallEnd {
                call_id: Uuid::from_u128(5).to_string(),
                duration_ms: duration_ms.unwrap_or(480_000).max(0),
            })
        );
    }
}

#[test]
fn call_started_yields_channel_and_call_activities() {
    let call_id = Uuid::from_u128(5);
    let channel_id = Uuid::from_u128(6);
    let created_at = Utc::now();
    let event = Event::with_event_id(
        Uuid::now_v7(),
        CallTopicEvent::Started(CallStartedMetadata {
            call_id,
            channel_id: Some(channel_id),
            created_by: MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap(),
            created_at,
            recording_enabled: false,
        }),
    );

    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_eq!(activities[0].entity_type, EntityType::Channel);
    assert_eq!(activities[0].entity_id, channel_id.to_string());
    assert_eq!(
        activities[0].action,
        Action::CallStarted(::activity::CallStart {
            call_id: call_id.to_string()
        })
    );

    assert_eq!(activities[1].entity_type, EntityType::Call);
    assert_eq!(activities[1].entity_id, call_id.to_string());
    assert_eq!(activities[1].action, Action::Created);
    assert!(activities.iter().all(|a| a.occurred_at == created_at));
}

#[test]
fn record_deletion_purges_the_call() {
    let call_id = Uuid::from_u128(5);
    let event = Event::with_event_id(
        Uuid::now_v7(),
        CallTopicEvent::RecordDeleted(CallRecordDeletedMetadata {
            call_id,
            channel_id: Some(Uuid::from_u128(6)),
            actor_user_id: None,
        }),
    );

    assert_eq!(
        event.event.ingest(event.event_id),
        Ingest::Purge(vec![(EntityType::Call, call_id.to_string())])
    );
}