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.

12.0 KB
Raw Blame
use std::sync::{Arc, Mutex};

use async_graphql::{Context, EmptySubscription, Object, Schema, SimpleObject, value};
use entity_mutation::EntityMutationActor;
use favorites::domain::models::FavoritesMutationActor;
use graphql_soup::SoupEntityEdges;
use model_entity::EntityType;
use uuid::Uuid;

use super::*;

/// Minimal query root for the isolated mutation schema.
struct QueryRoot;

#[Object]
impl QueryRoot {
    async fn health(&self) -> bool {
        true
    }
}

/// Minimal composed edge object needed by the shared mutation result.
#[derive(Clone, SimpleObject)]
struct TestSoupEdges {
    available: bool,
}

/// Minimal email edge object needed by the Soup edge trait.
#[derive(Clone, SimpleObject)]
struct TestEmailEdges {
    available: bool,
}

/// Minimal agent-session edge object needed by the Soup edge trait.
#[derive(Clone, SimpleObject)]
struct TestAgentSessionEdges {
    available: bool,
}

impl SoupEntityEdges for TestSoupEdges {
    type Property = String;
    type Notification = String;
    type NotificationFilter = String;
    type ActivityEvent = String;
    type EmailThreadEdges = TestEmailEdges;
    type CrmRecordEdges = TestEmailEdges;
    type InitiativeEdges = TestEmailEdges;
    type AgentSessionEdges = TestAgentSessionEdges;

    fn from_entity(_entity: Entity<'static>) -> Self {
        Self { available: true }
    }

    fn initiative_edges(_initiative_id: Uuid) -> Self::InitiativeEdges {
        TestEmailEdges { available: true }
    }

    fn email_thread_edges(_email_thread_id: Uuid) -> Self::EmailThreadEdges {
        TestEmailEdges { available: true }
    }

    fn crm_record_edges(_record: model_entity::Entity<'static>) -> Self::CrmRecordEdges {
        TestEmailEdges { available: true }
    }

    async fn resolve_email_cache_projection(
        &self,
        _ctx: &Context<'_>,
        _email_thread_id: Uuid,
    ) -> async_graphql::Result<Option<String>> {
        Ok(None)
    }

    fn agent_session_edges(_session_id: Uuid, _bot_id: Uuid) -> Self::AgentSessionEdges {
        TestAgentSessionEdges { available: true }
    }

    async fn resolve_properties(
        &self,
        _ctx: &Context<'_>,
    ) -> async_graphql::Result<Vec<Self::Property>> {
        Ok(Vec::new())
    }

    async fn resolve_notifications(
        &self,
        _ctx: &Context<'_>,
        _filter: Option<String>,
        _limit: Option<i32>,
    ) -> async_graphql::Result<Vec<Self::Notification>> {
        Ok(Vec::new())
    }

    async fn resolve_is_favorited(&self, _ctx: &Context<'_>) -> async_graphql::Result<bool> {
        Ok(true)
    }

    async fn resolve_viewer_permission(
        &self,
        _ctx: &Context<'_>,
    ) -> async_graphql::Result<Option<graphql_permission::GraphqlEntityPermission>> {
        Ok(None)
    }

    async fn resolve_activity(
        &self,
        _ctx: &Context<'_>,
        _limit: Option<i32>,
    ) -> async_graphql::Result<Vec<Self::ActivityEvent>> {
        Ok(Vec::new())
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct SetCall {
    actor_user_id: String,
    organization_id: Option<i64>,
    entity_type: EntityType,
    entity_id: String,
    favorite: bool,
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct ReorderCall {
    user_id: String,
    entities: Vec<(EntityType, String)>,
}

#[derive(Default)]
struct CapturingService {
    set_call: Mutex<Option<SetCall>>,
    reorder_call: Mutex<Option<ReorderCall>>,
    set_error: Mutex<Option<FavoritesError>>,
}

impl FavoritesMutationService for CapturingService {
    async fn set_favorite(
        &self,
        actor: FavoritesMutationActor,
        entity: Entity<'static>,
        favorite: bool,
    ) -> Result<SetFavoriteResult, FavoritesError> {
        *self.set_call.lock().expect("set call lock poisoned") = Some(SetCall {
            actor_user_id: actor.user_id.to_string(),
            organization_id: actor.organization_id,
            entity_type: entity.entity_type,
            entity_id: entity.entity_id.to_string(),
            favorite,
        });
        if let Some(error) = self
            .set_error
            .lock()
            .expect("set error lock poisoned")
            .take()
        {
            return Err(error);
        }
        let persisted_favorite = favorite.then(|| Favorite {
            entity_type: entity.entity_type,
            entity_id: entity.entity_id.to_string(),
            sort_order: 2.0,
            created_at: chrono::Utc::now(),
            file_type: Some("md".to_string()),
            document_sub_type: None,
            channel_type: None,
            channel_id: None,
        });
        Ok(SetFavoriteResult {
            entity,
            favorite: persisted_favorite,
        })
    }

    async fn reorder_favorites(
        &self,
        user_id: MacroUserIdStr<'static>,
        ordered: Vec<Entity<'static>>,
    ) -> Result<Vec<Favorite>, FavoritesError> {
        *self
            .reorder_call
            .lock()
            .expect("reorder call lock poisoned") = Some(ReorderCall {
            user_id: user_id.to_string(),
            entities: ordered
                .iter()
                .map(|entity| (entity.entity_type, entity.entity_id.to_string()))
                .collect(),
        });
        Ok(ordered
            .into_iter()
            .enumerate()
            .map(|(index, entity)| Favorite {
                entity_type: entity.entity_type,
                entity_id: entity.entity_id.into_owned(),
                sort_order: index as f64,
                created_at: chrono::Utc::now(),
                file_type: None,
                document_sub_type: None,
                channel_type: None,
                channel_id: None,
            })
            .collect())
    }
}

fn user_id() -> MacroUserIdStr<'static> {
    MacroUserIdStr::parse_from_str("macro|[email protected]").expect("valid user id")
}

fn schema(
    service: Arc<CapturingService>,
) -> Schema<QueryRoot, FavoriteMutationRoot<CapturingService, TestSoupEdges>, EmptySubscription> {
    let user_id = user_id();
    Schema::build(
        QueryRoot,
        FavoriteMutationRoot::<CapturingService, TestSoupEdges>::new(),
        EmptySubscription,
    )
    .data(service)
    .data(user_id.clone())
    .data(FavoritesMutationActor {
        user_id: user_id.clone(),
        organization_id: Some(42),
    })
    .data(EntityMutationActor {
        user_id,
        organization_id: Some(42),
    })
    .finish()
}

#[tokio::test]
async fn set_entity_favorite_preserves_the_toggle_and_delegates_to_favorites() {
    // This isolated favorites schema has no Soup loader. Hydrated mutation
    // effects are covered by complete_graph's composed-schema tests.
    let service = Arc::new(CapturingService::default());
    let response = schema(service.clone())
        .execute(
            r#"
            mutation {
              setFavorite(
                entity: { type: DOCUMENT, id: "document-1" }
                favorite: true
              ) {
                result {
                  __typename
                }
                favorite {
                  id
                  entityType
                  entityId
                  sortOrder
                  fileType
                }
              }
            }
            "#,
        )
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    assert_eq!(
        response.data,
        value!({
            "setFavorite": {
                "result": {
                    "__typename": "GraphqlMutationSuccess",
                },
                "favorite": {
                    "id": "document:document-1",
                    "entityType": "DOCUMENT",
                    "entityId": "document-1",
                    "sortOrder": 2.0,
                    "fileType": "md",
                },
            }
        })
    );
    assert_eq!(
        service
            .set_call
            .lock()
            .expect("set call lock poisoned")
            .clone(),
        Some(SetCall {
            actor_user_id: "macro|[email protected]".to_string(),
            organization_id: Some(42),
            entity_type: EntityType::Document,
            entity_id: "document-1".to_string(),
            favorite: true,
        })
    );
}

#[tokio::test]
async fn set_entity_favorite_retains_the_legacy_result_shape() {
    let service = Arc::new(CapturingService::default());
    let response = schema(service)
        .execute(
            r#"
            mutation {
              setEntityFavorite(
                entity: { type: DOCUMENT, id: "document-1" }
                favorite: false
              ) {
                __typename
              }
            }
            "#,
        )
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    assert_eq!(
        response.data,
        value!({
            "setEntityFavorite": {
                "__typename": "GraphqlMutationSuccess",
            }
        })
    );
}

#[tokio::test]
async fn set_favorite_failures_are_transport_errors_for_durable_replay() {
    for error in [
        FavoritesError::Unauthorized,
        FavoritesError::NotFound,
        FavoritesError::BadRequest("collection is full".to_string()),
        FavoritesError::UnsupportedEntityType(EntityType::User),
    ] {
        let service = Arc::new(CapturingService {
            set_error: Mutex::new(Some(error)),
            ..Default::default()
        });
        let response = schema(service)
            .execute(
                r#"mutation {
                    setFavorite(entity: { type: DOCUMENT, id: "document-1" }, favorite: true) {
                        result { __typename }
                        favorite { id }
                    }
                }"#,
            )
            .await;

        assert_eq!(response.errors.len(), 1, "{response:?}");
        assert_eq!(response.data, value!(null));
    }
}

#[tokio::test]
async fn set_entity_favorite_retains_the_legacy_error_union() {
    let service = Arc::new(CapturingService {
        set_error: Mutex::new(Some(FavoritesError::Unauthorized)),
        ..Default::default()
    });
    let response = schema(service)
        .execute(
            r#"mutation {
                setEntityFavorite(entity: { type: DOCUMENT, id: "document-1" }, favorite: true) {
                    __typename
                }
            }"#,
        )
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    assert_eq!(
        response.data,
        value!({ "setEntityFavorite": { "__typename": "GraphqlMutationError" } })
    );
}

#[tokio::test]
async fn reorder_favorites_delegates_and_returns_authoritative_order() {
    let service = Arc::new(CapturingService::default());
    let response = schema(service.clone())
        .execute(
            r#"
            mutation {
              reorderFavorites(input: {
                favorites: [
                  { type: PROJECT, id: "project-1" }
                  { type: DOCUMENT, id: "document-1" }
                ]
              }) {
                entityType
                entityId
                sortOrder
              }
            }
            "#,
        )
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    assert_eq!(
        response.data,
        value!({
            "reorderFavorites": [
                {
                    "entityType": "PROJECT",
                    "entityId": "project-1",
                    "sortOrder": 0.0,
                },
                {
                    "entityType": "DOCUMENT",
                    "entityId": "document-1",
                    "sortOrder": 1.0,
                },
            ]
        })
    );
    assert_eq!(
        service
            .reorder_call
            .lock()
            .expect("reorder call lock poisoned")
            .clone(),
        Some(ReorderCall {
            user_id: "macro|[email protected]".to_string(),
            entities: vec![
                (EntityType::Project, "project-1".to_string()),
                (EntityType::Document, "document-1".to_string()),
            ],
        })
    );
}