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.

3.6 KB
Raw Blame
use model_entity::EntityType;
use model_owner::OwnerType;
use uuid::Uuid;

use super::*;

#[test]
fn registered_kinds_round_trip_and_match_check_spellings() {
    let expected = [
        (RegisteredEntityType::Project, "project"),
        (RegisteredEntityType::Document, "document"),
        (RegisteredEntityType::Chat, "chat"),
        (RegisteredEntityType::AgentSession, "agent_session"),
        (RegisteredEntityType::ScheduledAction, "scheduled_action"),
        (RegisteredEntityType::ExternalChat, "external_chat"),
    ];
    assert_eq!(
        RegisteredEntityType::ALL,
        [
            RegisteredEntityType::Project,
            RegisteredEntityType::Document,
            RegisteredEntityType::Chat,
            RegisteredEntityType::AgentSession,
            RegisteredEntityType::ScheduledAction,
            RegisteredEntityType::ExternalChat,
        ]
    );
    for (kind, spelling) in expected {
        assert_eq!(kind.as_str(), spelling);
        assert_eq!(kind.to_string(), spelling);
        let wide = EntityType::from(kind);
        assert_eq!(wide.as_ref(), spelling);
        assert_eq!(RegisteredEntityType::try_from(wide), Ok(kind));
    }
}

#[test]
fn try_from_rejects_every_unregistered_entity_type() {
    let unregistered = [
        EntityType::User,
        EntityType::Channel,
        EntityType::ChannelMessage,
        EntityType::EmailThread,
        EntityType::CalendarEvent,
        EntityType::Team,
        EntityType::Call,
        EntityType::ForeignEntity,
        EntityType::StaticFile,
        EntityType::CrmCompany,
        EntityType::CrmContact,
        EntityType::Reminder,
        EntityType::Skill,
        EntityType::Initiative,
    ];
    for ty in unregistered {
        assert_eq!(
            RegisteredEntityType::try_from(ty),
            Err(UnregisteredEntityType(ty))
        );
    }
}

#[test]
fn registered_kinds_serialize_as_their_entity_type_spelling() {
    let expected = [
        (RegisteredEntityType::Project, r#""project""#),
        (RegisteredEntityType::Document, r#""document""#),
        (RegisteredEntityType::Chat, r#""chat""#),
        (RegisteredEntityType::AgentSession, r#""agent_session""#),
        (
            RegisteredEntityType::ScheduledAction,
            r#""scheduled_action""#,
        ),
        (RegisteredEntityType::ExternalChat, r#""external_chat""#),
    ];
    for (kind, json) in expected {
        assert_eq!(serde_json::to_string(&kind).unwrap(), json);
        assert_eq!(
            serde_json::from_str::<RegisteredEntityType>(json).unwrap(),
            kind
        );
    }
}

#[test]
fn deserializing_an_unregistered_kind_fails() {
    let error = serde_json::from_str::<RegisteredEntityType>(r#""team""#).unwrap_err();
    assert_eq!(error.to_string(), "team is not a registered entity type");

    let error = serde_json::from_str::<RegisteredEntityType>(r#""Document""#).unwrap_err();
    assert!(
        error.to_string().starts_with("unknown variant `Document`"),
        "{error}"
    );
}

#[test]
fn try_new_accepts_chat_and_rejects_initiative() {
    let owner = Owner::parse(OwnerType::User, "macro|[email protected]").unwrap();
    let id = Uuid::from_u128(1);

    let record = NewEntityRecord::try_new(id, EntityType::Chat, owner.clone()).unwrap();
    assert_eq!(record.id, id);
    assert_eq!(record.entity_type, RegisteredEntityType::Chat);
    assert_eq!(record.owner, owner);
    assert_eq!(record.created_at, None);
    assert_eq!(record.updated_at, None);

    assert_eq!(
        NewEntityRecord::try_new(id, EntityType::Initiative, owner),
        Err(UnregisteredEntityType(EntityType::Initiative))
    );
}