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.3 KB
Raw Blame
use ::activity::{Action, Actor, activity_id};
use chrono::{TimeZone as _, Utc};
use macro_event_broker::Event;
use macro_user_id::user_id::MacroUserIdStr;
use model_entity::EntityType;
use models_databases::TableId;
use uuid::Uuid;

use super::*;
use crate::domain::events::{
    Attribution as EventAttribution, DatabaseCreatedMetadata, DatabasePurgedMetadata,
    DatabaseRenamedMetadata, DatabaseRestoredMetadata, DatabaseSharingChangedMetadata,
    DatabaseTablesChangedMetadata, DatabaseTrashedMetadata, TableVersionChange,
};
use crate::domain::models::TableVersion;

const DATABASE_ID: &str = "11111111-1111-1111-1111-111111111111";

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

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

fn single_activity(ingest: Ingest) -> Activity {
    match ingest {
        Ingest::Insert(mut activities) => {
            assert_eq!(activities.len(), 1);
            activities.pop().unwrap()
        }
        other => panic!("expected a single activity, got {other:?}"),
    }
}

fn by_user(id: &str) -> Option<EventAttribution> {
    Some(EventAttribution::user(user(id)))
}

#[test]
fn created_maps_to_a_created_activity_at_the_repository_timestamp() {
    let created_at = Utc.with_ymd_and_hms(2026, 9, 1, 12, 0, 0).unwrap();
    let event = envelope(DatabaseTopicEvent::Created(DatabaseCreatedMetadata {
        database_id: DATABASE_ID.parse().unwrap(),
        owner: user("macro|[email protected]"),
        name: "Roadmap".to_string(),
        created_at,
        attribution: EventAttribution::user(user("macro|[email protected]")),
    }));

    let activity = single_activity(event.event.ingest(event.event_id));
    assert_eq!(activity.action, Action::Created);
    assert_eq!(activity.entity_type, EntityType::Database);
    assert_eq!(activity.entity_id, DATABASE_ID);
    assert_eq!(activity.subject_id, "macro|[email protected]");
    assert_eq!(activity.actor.as_ref(), "macro|[email protected]");
    assert_eq!(activity.occurred_at, created_at);
    assert_eq!(activity.id, activity_id(event.event_id, 0));
}

#[test]
fn renamed_restored_and_tables_changed_map_to_edited() {
    let events = [
        DatabaseTopicEvent::Renamed(DatabaseRenamedMetadata {
            database_id: DATABASE_ID.parse().unwrap(),
            attribution: by_user("macro|[email protected]"),
            name: "Renamed".to_string(),
        }),
        DatabaseTopicEvent::Restored(DatabaseRestoredMetadata {
            database_id: DATABASE_ID.parse().unwrap(),
            attribution: by_user("macro|[email protected]"),
        }),
        DatabaseTopicEvent::TablesChanged(DatabaseTablesChangedMetadata {
            database_id: DATABASE_ID.parse().unwrap(),
            attribution: by_user("macro|[email protected]"),
            tables: vec![TableVersionChange {
                table_id: TableId::new(),
                version: TableVersion(3),
            }],
        }),
    ];

    for event in events {
        let event = envelope(event);
        let activity = single_activity(event.event.ingest(event.event_id));
        assert_eq!(activity.action, Action::Edited, "{:?}", event.event);
        assert_eq!(activity.entity_type, EntityType::Database);
        assert_eq!(activity.entity_id, DATABASE_ID);
        assert_eq!(activity.subject_id, "macro|[email protected]");
    }
}

#[test]
fn an_agent_creation_lands_on_the_owners_feed_as_the_agent() {
    let created_at = Utc.with_ymd_and_hms(2026, 9, 1, 12, 0, 0).unwrap();
    let bot = Actor::try_from("bot|00000000-0000-0000-0000-00000000a1a1".to_string())
        .expect("valid bot actor");
    let event = envelope(DatabaseTopicEvent::Created(DatabaseCreatedMetadata {
        database_id: DATABASE_ID.parse().unwrap(),
        owner: user("macro|[email protected]"),
        name: "Roadmap".to_string(),
        created_at,
        attribution: EventAttribution {
            actor: bot.clone(),
            on_behalf_of: Some(user("macro|[email protected]")),
        },
    }));

    let activity = single_activity(event.event.ingest(event.event_id));
    assert_eq!(activity.action, Action::Created);
    assert_eq!(activity.actor, bot);
    assert_eq!(activity.subject_id, "macro|[email protected]");
    assert_eq!(activity.occurred_at, created_at);
}

#[test]
fn sharing_changed_maps_to_edited() {
    let event = envelope(DatabaseTopicEvent::SharingChanged(
        DatabaseSharingChangedMetadata {
            database_id: DATABASE_ID.parse().unwrap(),
            attribution: by_user("macro|[email protected]"),
        },
    ));

    let activity = single_activity(event.event.ingest(event.event_id));
    assert_eq!(activity.action, Action::Edited);
    assert_eq!(activity.entity_type, EntityType::Database);
    assert_eq!(activity.entity_id, DATABASE_ID);
    assert_eq!(activity.subject_id, "macro|[email protected]");
    assert_eq!(activity.id, activity_id(event.event_id, 0));
}

#[test]
fn trashed_maps_to_deleted() {
    let event = envelope(DatabaseTopicEvent::Trashed(DatabaseTrashedMetadata {
        database_id: DATABASE_ID.parse().unwrap(),
        attribution: by_user("macro|[email protected]"),
    }));

    let activity = single_activity(event.event.ingest(event.event_id));
    assert_eq!(activity.action, Action::Deleted);
    assert_eq!(activity.subject_id, "macro|[email protected]");
}

#[test]
fn delegated_writes_land_on_the_subjects_feed() {
    let bot = Actor::try_from("bot|00000000-0000-0000-0000-000000000042".to_string())
        .expect("valid bot actor");
    let event = envelope(DatabaseTopicEvent::TablesChanged(
        DatabaseTablesChangedMetadata {
            database_id: DATABASE_ID.parse().unwrap(),
            attribution: Some(EventAttribution {
                actor: bot.clone(),
                on_behalf_of: Some(user("macro|[email protected]")),
            }),
            tables: vec![],
        },
    ));

    let activity = single_activity(event.event.ingest(event.event_id));
    assert_eq!(activity.actor, bot);
    assert_eq!(activity.subject_id, "macro|[email protected]");
}

#[test]
fn unattributed_mutations_are_ignored() {
    let event = envelope(DatabaseTopicEvent::Renamed(DatabaseRenamedMetadata {
        database_id: DATABASE_ID.parse().unwrap(),
        attribution: None,
        name: "Internal".to_string(),
    }));

    assert_eq!(event.event.ingest(event.event_id), Ingest::Ignore);
}

#[test]
fn purged_purges_the_database() {
    let event = envelope(DatabaseTopicEvent::Purged(DatabasePurgedMetadata {
        database_id: DATABASE_ID.parse().unwrap(),
    }));

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

#[test]
fn events_round_trip_through_json() {
    let event = DatabaseTopicEvent::TablesChanged(DatabaseTablesChangedMetadata {
        database_id: DATABASE_ID.parse().unwrap(),
        attribution: by_user("macro|[email protected]"),
        tables: vec![TableVersionChange {
            table_id: TableId::new(),
            version: TableVersion(7),
        }],
    });

    let json = serde_json::to_value(&event).expect("serializes");
    assert_eq!(json["event_type"], "database.tables_changed");
    let decoded: DatabaseTopicEvent = serde_json::from_value(json).expect("deserializes");
    assert_eq!(decoded, event);
}