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.

72.0 KB
Raw Blame
use super::*;
use crate::domain::{
    events::{
        WebhookCreatedMetadata, WebhookDeletedMetadata, WebhookTopicEvent, WebhookUpdatedMetadata,
        WebhookValidatedMetadata,
    },
    models::{
        CreateWebhookOutcome, CreateWebhookRequest, PatchWebhookRequest, Webhook,
        WebhookEventQueueMessage, WebhookStatus,
    },
    ports::{WebhookEventEnqueuer, WebhookRepo, WebhookWorkspaceResolver},
};
use call::domain::notification_events::{CallNotificationEvent, CallNotificationMetadata};
use channel_sender::ChannelSender;
use channels::domain::{
    broker_events::{
        ChannelCreatedMetadata, ChannelDeletedMetadata, ChannelMessagePostedMetadata,
        ChannelParticipantAddedMetadata, ChannelParticipantRemovedMetadata, ChannelUpdatedMetadata,
    },
    models::ChannelType,
};
use chrono::{DateTime, Utc};
use documents::domain::events::{
    DocumentContentUploadedMetadata, DocumentCopiedMetadata, DocumentCreatedMetadata,
    DocumentDeletedMetadata, DocumentPurgedMetadata, DocumentSyncContentUpdatedMetadata,
    DocumentUpdatedMetadata,
};
use entity_access::domain::models::{
    AccessLevel, BotAccessScope, BotId, CallChannelInfo, EntityAccessReceipt, EntityPermission,
    RequiredPermission, TeamRole, UserTeamInfo,
};
use macro_user_id::{lowercased::Lowercase, user_id::MacroUserId};
use messages::domain::events::{
    MessageAttachmentCreatedMetadata, MessageAttachmentRemovedMetadata, MessageDeletedMetadata,
    MessageMentionedMetadata, MessagePatchedMetadata, MessagePostedMetadata,
};
use messages::domain::models::{MessageParent, SimpleMention};
use model_owner::Owner;
use serde_json::Value;
use std::{
    collections::BTreeMap,
    sync::{Arc, Mutex, MutexGuard},
    time::Duration,
};

const DOCUMENT_ID: &str = "11111111-1111-1111-1111-111111111111";
const COPIED_DOCUMENT_ID: &str = "22222222-2222-2222-2222-222222222222";
const PERSONAL_WORKSPACE_ID: &str = "macro|[email protected]";
const BOT_PRINCIPAL_ID: &str = "bot|66666666-6666-6666-6666-666666666666";
const TEAM_WORKSPACE_ID: &str = "33333333-3333-3333-3333-333333333333";
const CALL_ID: &str = "0198a1b2-c3d4-7e5f-8061-728394a5b6c7";

#[tokio::test]
async fn private_call_invitation_reaches_only_the_recipient_workspace() {
    let call_id = Uuid::parse_str(CALL_ID).unwrap();
    let mut metadata = CallNotificationMetadata::new(call_id, None);
    metadata.recipient_user_id = Some(PERSONAL_WORKSPACE_ID.to_string());
    metadata.invited_by = Some("macro|[email protected]".to_string());
    let event = Event::new(CallNotificationEvent::Invited(metadata));
    let access = MockAccessService::with_users(Vec::new());
    let repository = MockRepository::new(
        vec![TEAM_WORKSPACE_ID.to_string()],
        vec![webhook("wh_recipient", PERSONAL_WORKSPACE_ID)],
    );
    let enqueuer = MockEnqueuer::default();

    service(access.clone(), repository.clone(), enqueuer.clone())
        .ingest_call_notification_event(event)
        .await
        .unwrap();

    assert!(lock(&access.calls).is_empty());
    let state = lock(&repository.state);
    assert!(state.workspace_calls.is_empty());
    assert_eq!(
        state.match_calls[0].workspace_ids,
        vec![PERSONAL_WORKSPACE_ID.to_string()]
    );
    drop(state);
    let messages = lock(&enqueuer.state);
    let delivered = &messages.attempted_messages[0].event;
    assert_eq!(delivered.event_name, "call.invited");
    assert_eq!(delivered.entity_id, CALL_ID);
    assert!(
        delivered.broker_envelope["metadata"]
            .get("recipient_user_id")
            .is_none()
    );
}

#[tokio::test]
async fn private_call_event_without_recipient_is_never_fanned_out() {
    let call_id = Uuid::parse_str(CALL_ID).unwrap();
    let event = Event::new(CallNotificationEvent::Invited(
        CallNotificationMetadata::new(call_id, None),
    ));
    let access = MockAccessService::with_users(vec![user_id(PERSONAL_WORKSPACE_ID)]);
    let repository = MockRepository::new(
        vec![PERSONAL_WORKSPACE_ID.to_string()],
        vec![webhook("wh_member", PERSONAL_WORKSPACE_ID)],
    );
    let enqueuer = MockEnqueuer::default();

    let result = service(access, repository.clone(), enqueuer.clone())
        .ingest_call_notification_event(event)
        .await;

    assert!(matches!(
        result,
        Err(WebhookEventIngestionError::MissingCallRecipient)
    ));
    assert!(lock(&repository.state).match_calls.is_empty());
    assert!(lock(&enqueuer.state).attempted_messages.is_empty());
}

#[tokio::test]
async fn public_call_event_resolves_current_call_accessors() {
    let call_id = Uuid::parse_str(CALL_ID).unwrap();
    let event = Event::new(CallNotificationEvent::Ended(CallNotificationMetadata::new(
        call_id, None,
    )));
    let access = MockAccessService::with_users(vec![user_id(PERSONAL_WORKSPACE_ID)]);
    let repository = MockRepository::new(
        vec![
            PERSONAL_WORKSPACE_ID.to_string(),
            TEAM_WORKSPACE_ID.to_string(),
        ],
        vec![webhook("wh_member", PERSONAL_WORKSPACE_ID)],
    );
    let enqueuer = MockEnqueuer::default();

    service(access.clone(), repository.clone(), enqueuer)
        .ingest_call_notification_event(event)
        .await
        .unwrap();

    assert_eq!(
        lock(&access.calls).as_slice(),
        &[(CALL_ID.to_string(), EntityType::Call)]
    );
    assert_eq!(lock(&repository.state).workspace_calls.len(), 1);
}

fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
    mutex.lock().expect("test mutex is not poisoned")
}

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

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

fn timestamp() -> DateTime<Utc> {
    DateTime::parse_from_rfc3339("2026-01-02T03:04:05Z")
        .expect("valid timestamp")
        .with_timezone(&Utc)
}

#[derive(Debug, Clone, Copy)]
enum AccessFailure {
    Internal,
}

#[derive(Clone)]
struct MockAccessService {
    users: Vec<MacroUserIdStr<'static>>,
    failure: Option<AccessFailure>,
    calls: Arc<Mutex<Vec<(String, EntityType)>>>,
}

impl MockAccessService {
    fn with_users(users: Vec<MacroUserIdStr<'static>>) -> Self {
        Self {
            users,
            failure: None,
            calls: Arc::default(),
        }
    }
}

impl EntityAccessService for MockAccessService {
    async fn generate_entity_access_receipt<T: RequiredPermission>(
        &self,
        _user_id: &MacroUserId<Lowercase<'_>>,
        _user_org_id: Option<i64>,
        _entity_id: &str,
        _entity_type: EntityType,
    ) -> Result<EntityAccessReceipt<T>, AccessError> {
        unimplemented!("not used by webhook event ingestion")
    }

    async fn generate_bot_entity_access_receipt<T: RequiredPermission>(
        &self,
        _bot_id: BotId,
        _scope: BotAccessScope,
        _entity_id: &str,
        _entity_type: EntityType,
    ) -> Result<EntityAccessReceipt<T>, AccessError> {
        unimplemented!("not used by webhook event ingestion")
    }

    async fn get_access_level(
        &self,
        _user_id: Option<&MacroUserId<Lowercase<'_>>>,
        _entity_id: &str,
        _entity_type: EntityType,
    ) -> Result<Option<AccessLevel>, AccessError> {
        unimplemented!("not used by webhook event ingestion")
    }

    async fn check_access(
        &self,
        _user_id: Option<&MacroUserId<Lowercase<'_>>>,
        _entity_id: &str,
        _entity_type: EntityType,
        _required_level: AccessLevel,
    ) -> Result<AccessLevel, AccessError> {
        unimplemented!("not used by webhook event ingestion")
    }

    async fn check_public_access(
        &self,
        _entity_id: &str,
        _entity_type: EntityType,
        _required_level: AccessLevel,
    ) -> Result<AccessLevel, AccessError> {
        unimplemented!("not used by webhook event ingestion")
    }

    async fn get_entity_permission(
        &self,
        _user_id: Option<&MacroUserId<Lowercase<'_>>>,
        _entity_id: &str,
        _entity_type: EntityType,
        _user_org_id: Option<i64>,
    ) -> Result<EntityPermission, AccessError> {
        unimplemented!("not used by webhook event ingestion")
    }

    async fn get_crm_entity_permission_with_team(
        &self,
        _user_id: Option<&MacroUserId<Lowercase<'_>>>,
        _entity_id: &str,
        _entity_type: EntityType,
    ) -> Result<(EntityPermission, Uuid, TeamRole), AccessError> {
        unimplemented!("not used by webhook event ingestion")
    }

    async fn get_users_by_entity(
        &self,
        entity_id: &str,
        entity_type: EntityType,
    ) -> Result<Vec<MacroUserIdStr<'static>>, AccessError> {
        lock(&self.calls).push((entity_id.to_string(), entity_type));
        match self.failure {
            Some(AccessFailure::Internal) => Err(AccessError::internal("mock access failure")),
            None => Ok(self.users.clone()),
        }
    }

    async fn get_call_channel(
        &self,
        _call_id: &Uuid,
    ) -> Result<Option<CallChannelInfo>, AccessError> {
        unimplemented!("not used by webhook event ingestion")
    }

    async fn get_call_channel_by_channel_id(
        &self,
        _channel_id: &Uuid,
    ) -> Result<Option<CallChannelInfo>, AccessError> {
        unimplemented!("not used by webhook event ingestion")
    }

    async fn get_user_team(
        &self,
        _user_id: &MacroUserId<Lowercase<'_>>,
    ) -> Result<Option<UserTeamInfo>, AccessError> {
        unimplemented!("not used by webhook event ingestion")
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct MatchCall {
    workspace_ids: Vec<String>,
    event_name: String,
    entity_id: String,
}

struct MockRepositoryState {
    resolved_workspace_ids: Vec<String>,
    matching_webhooks: Vec<Webhook>,
    fail_workspace_resolution: bool,
    fail_matching: bool,
    workspace_calls: Vec<Vec<MacroUserIdStr<'static>>>,
    match_calls: Vec<MatchCall>,
}

#[derive(Clone)]
struct MockRepository {
    state: Arc<Mutex<MockRepositoryState>>,
}

impl MockRepository {
    fn new(resolved_workspace_ids: Vec<String>, matching_webhooks: Vec<Webhook>) -> Self {
        Self {
            state: Arc::new(Mutex::new(MockRepositoryState {
                resolved_workspace_ids,
                matching_webhooks,
                fail_workspace_resolution: false,
                fail_matching: false,
                workspace_calls: Vec::new(),
                match_calls: Vec::new(),
            })),
        }
    }
}

impl WebhookWorkspaceResolver for MockRepository {
    type Err = anyhow::Error;

    async fn resolve_workspace_ids(
        &self,
        people: Vec<MacroUserIdStr<'static>>,
    ) -> Result<Vec<String>, Self::Err> {
        let mut state = lock(&self.state);
        state.workspace_calls.push(people);
        if state.fail_workspace_resolution {
            anyhow::bail!("workspace resolver unavailable");
        }
        Ok(state.resolved_workspace_ids.clone())
    }

    async fn get_user_team_workspace_id(
        &self,
        _user_id: MacroUserIdStr<'static>,
    ) -> Result<Option<String>, Self::Err> {
        Ok(None)
    }
}

impl WebhookRepo for MockRepository {
    type Err = anyhow::Error;

    async fn create_webhook(
        &self,
        _created_by_user_id: MacroUserIdStr<'static>,
        _workspace_id: String,
        _request: CreateWebhookRequest,
        _signing_secret: String,
        _headers: Value,
    ) -> Result<CreateWebhookOutcome, Self::Err> {
        unimplemented!("not used by webhook event ingestion")
    }

    async fn get_webhook(&self, _webhook_id: String) -> Result<Option<Webhook>, Self::Err> {
        unimplemented!("not used by webhook event ingestion")
    }

    async fn list_webhooks_for_workspaces(
        &self,
        _workspace_ids: Vec<String>,
    ) -> Result<Vec<Webhook>, Self::Err> {
        unimplemented!("not used by webhook event ingestion")
    }

    async fn list_active_webhooks_matching_event(
        &self,
        workspace_ids: Vec<String>,
        event_name: String,
        entity_id: String,
    ) -> Result<Vec<Webhook>, Self::Err> {
        let mut state = lock(&self.state);
        state.match_calls.push(MatchCall {
            workspace_ids,
            event_name,
            entity_id,
        });
        if state.fail_matching {
            anyhow::bail!("webhook repository unavailable");
        }
        Ok(state.matching_webhooks.clone())
    }

    async fn patch_webhook(
        &self,
        _webhook_id: String,
        _request: PatchWebhookRequest,
    ) -> Result<Option<Webhook>, Self::Err> {
        unimplemented!("not used by webhook event ingestion")
    }

    async fn delete_webhook(&self, _webhook_id: String) -> Result<Option<Webhook>, Self::Err> {
        unimplemented!("not used by webhook event ingestion")
    }

    async fn set_webhook_validity(
        &self,
        _webhook_id: String,
        _is_valid: bool,
    ) -> Result<Option<Webhook>, Self::Err> {
        unimplemented!("not used by webhook event ingestion")
    }

    async fn get_user_team_workspace_id(
        &self,
        _user_id: MacroUserIdStr<'static>,
    ) -> Result<Option<String>, Self::Err> {
        unimplemented!("not used by webhook event ingestion")
    }
}

#[derive(Default)]
struct MockEnqueuerState {
    attempted_messages: Vec<WebhookEventQueueMessage>,
    completed_webhook_ids: Vec<String>,
    failing_webhook_id: Option<String>,
    delayed_webhook_id: Option<String>,
}

#[derive(Clone, Default)]
struct MockEnqueuer {
    state: Arc<Mutex<MockEnqueuerState>>,
}

impl WebhookEventEnqueuer for MockEnqueuer {
    type Err = anyhow::Error;

    async fn enqueue(&self, message: WebhookEventQueueMessage) -> Result<(), Self::Err> {
        let (should_fail, should_delay) = {
            let mut state = lock(&self.state);
            let should_fail = state.failing_webhook_id.as_ref() == Some(&message.webhook_id);
            let should_delay = state.delayed_webhook_id.as_ref() == Some(&message.webhook_id);
            state.attempted_messages.push(message.clone());
            (should_fail, should_delay)
        };

        if should_delay {
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
        lock(&self.state)
            .completed_webhook_ids
            .push(message.webhook_id.clone());

        if should_fail {
            anyhow::bail!("queue unavailable");
        }
        Ok(())
    }
}

type TestService =
    WebhookEventIngestionServiceImpl<MockAccessService, MockRepository, MockEnqueuer>;

fn service(
    access: MockAccessService,
    repository: MockRepository,
    enqueuer: MockEnqueuer,
) -> TestService {
    WebhookEventIngestionServiceImpl::new(Arc::new(access), repository, enqueuer)
}

fn webhook(id: &str, workspace_id: &str) -> Webhook {
    Webhook {
        id: id.to_string(),
        workspace_id: workspace_id.to_string(),
        namespace: id.to_string(),
        name: id.to_string(),
        endpoint_url: "https://example.com/webhook".to_string(),
        signing_secret: "not-queued".to_string(),
        headers: BTreeMap::from([("X-Custom".to_string(), "not-queued".to_string())]),
        status: WebhookStatus::Active,
        is_valid: true,
        created_by_user_id: PERSONAL_WORKSPACE_ID.to_string(),
        created_at: timestamp(),
        updated_at: timestamp(),
        deleted_at: None,
        filters: Vec::new(),
    }
}

#[derive(Clone)]
enum TestBrokerEvent {
    Document(Event<DocumentTopicEvent>),
    Channel(Event<ChannelTopicEvent>),
    Message(Event<MessageTopicEvent>),
}

impl TestBrokerEvent {
    async fn ingest(&self, service: &TestService) -> Result<(), WebhookEventIngestionError> {
        match self {
            Self::Document(event) => service.ingest_document_event(event.clone()).await,
            Self::Channel(event) => service.ingest_channel_event(event.clone()).await,
            Self::Message(event) => service.ingest_message_event(event.clone()).await,
        }
    }

    fn envelope(&self) -> Value {
        match self {
            Self::Document(event) => serde_json::to_value(event).expect("serializable event"),
            Self::Channel(event) => serde_json::to_value(event).expect("serializable event"),
            Self::Message(event) => serde_json::to_value(event).expect("serializable event"),
        }
    }

    fn event_id(&self) -> Uuid {
        match self {
            Self::Document(event) => event.event_id,
            Self::Channel(event) => event.event_id,
            Self::Message(event) => event.event_id,
        }
    }

    fn schema_version(&self) -> u8 {
        match self {
            Self::Document(event) => event.schema_version,
            Self::Message(event) => event.schema_version,
            Self::Channel(event) => event.schema_version,
        }
    }
}

struct EventCase {
    event: TestBrokerEvent,
    event_name: &'static str,
    entity_type: EntityType,
    normalized_entity_type: &'static str,
    entity_id: String,
}

impl EventCase {
    fn new(
        event: TestBrokerEvent,
        event_name: &'static str,
        entity_type: EntityType,
        normalized_entity_type: &'static str,
        entity_id: String,
    ) -> Self {
        Self {
            event,
            event_name,
            entity_type,
            normalized_entity_type,
            entity_id,
        }
    }
}

fn document_event_cases() -> Vec<EventCase> {
    vec![
        EventCase::new(
            TestBrokerEvent::Document(Event::with_schema_version(
                DocumentTopicEvent::Created(DocumentCreatedMetadata {
                    document_id: DOCUMENT_ID.to_string(),
                    owner: Owner::User(user_id("macro|[email protected]")),
                    actor: None,
                    on_behalf_of: None,
                    document_name: "notes".to_string(),
                    file_type: None,
                    project_id: None,
                    sub_type: None,
                    created_at: Some(timestamp()),
                }),
                2,
            )),
            "document.created",
            EntityType::Document,
            DOCUMENT_ENTITY_TYPE,
            DOCUMENT_ID.to_string(),
        ),
        EventCase::new(
            TestBrokerEvent::Document(Event::with_schema_version(
                DocumentTopicEvent::Updated(DocumentUpdatedMetadata {
                    document_id: DOCUMENT_ID.to_string(),
                    owner: Owner::User(user_id("macro|[email protected]")),
                    actor_user_id: Some(user_id("macro|[email protected]")),
                    actor: None,
                    on_behalf_of: None,
                    document_name: Some("renamed".to_string()),
                    previous_project_id: None,
                    project_id: None,
                    file_type: None,
                    share_permission_updated: false,
                }),
                2,
            )),
            "document.updated",
            EntityType::Document,
            DOCUMENT_ENTITY_TYPE,
            DOCUMENT_ID.to_string(),
        ),
        EventCase::new(
            TestBrokerEvent::Document(Event::with_schema_version(
                DocumentTopicEvent::Deleted(DocumentDeletedMetadata {
                    sub_type: None,
                    document_id: DOCUMENT_ID.to_string(),
                    actor_user_id: Some(user_id("macro|[email protected]")),
                    actor: None,
                    on_behalf_of: None,
                    project_id: None,
                }),
                2,
            )),
            "document.deleted",
            EntityType::Document,
            DOCUMENT_ENTITY_TYPE,
            DOCUMENT_ID.to_string(),
        ),
        EventCase::new(
            TestBrokerEvent::Document(Event::with_schema_version(
                DocumentTopicEvent::Copied(DocumentCopiedMetadata {
                    document_id: COPIED_DOCUMENT_ID.to_string(),
                    source_document_id: DOCUMENT_ID.to_string(),
                    source_version_id: None,
                    owner: Owner::User(user_id("macro|[email protected]")),
                    actor: None,
                    on_behalf_of: None,
                    document_name: "notes (copy)".to_string(),
                    file_type: None,
                    project_id: None,
                    sub_type: None,
                }),
                2,
            )),
            "document.copied",
            EntityType::Document,
            DOCUMENT_ENTITY_TYPE,
            COPIED_DOCUMENT_ID.to_string(),
        ),
    ]
}

fn search_only_document_event_cases() -> Vec<(&'static str, Event<DocumentTopicEvent>)> {
    vec![
        (
            "document.content_uploaded",
            Event::new(DocumentTopicEvent::ContentUploaded(
                DocumentContentUploadedMetadata {
                    document_id: DOCUMENT_ID.to_string(),
                    owner: Owner::User(user_id("macro|[email protected]")),
                    file_type: "pdf".parse().expect("valid file type"),
                    document_version_id: Some("convert".to_string()),
                },
            )),
        ),
        (
            "document.sync_content_updated",
            Event::new(DocumentTopicEvent::SyncContentUpdated(
                DocumentSyncContentUpdatedMetadata {
                    editors: Vec::new(),
                    document_id: DOCUMENT_ID.to_string(),
                    file_type: "md".parse().expect("valid file type"),
                    document_version_id: None,
                    actor: None,
                    on_behalf_of: None,
                },
            )),
        ),
        (
            "document.purged",
            Event::new(DocumentTopicEvent::Purged(DocumentPurgedMetadata {
                document_id: DOCUMENT_ID.to_string(),
            })),
        ),
    ]
}

fn channel_event_cases() -> Vec<EventCase> {
    let channel_id = Uuid::parse_str("44444444-4444-4444-4444-444444444444").unwrap();
    let owner = "macro|[email protected]";
    let member = "macro|[email protected]";

    vec![
        EventCase::new(
            TestBrokerEvent::Channel(Event::with_schema_version(
                ChannelTopicEvent::Created(ChannelCreatedMetadata {
                    channel_id,
                    actor: sender(owner),
                    on_behalf_of: None,
                    channel_type: ChannelType::Team,
                    channel_name: Some("general".to_string()),
                    participant_user_ids: vec![user_id(owner)],
                }),
                3,
            )),
            "channel.created",
            EntityType::Channel,
            CHANNEL_ENTITY_TYPE,
            channel_id.to_string(),
        ),
        EventCase::new(
            TestBrokerEvent::Channel(Event::with_schema_version(
                ChannelTopicEvent::Updated(ChannelUpdatedMetadata {
                    channel_id,
                    actor: user_id(owner),
                    previous_name: Some("general".to_string()),
                    channel_name: Some("announcements".to_string()),
                }),
                3,
            )),
            "channel.updated",
            EntityType::Channel,
            CHANNEL_ENTITY_TYPE,
            channel_id.to_string(),
        ),
        EventCase::new(
            TestBrokerEvent::Channel(Event::with_schema_version(
                ChannelTopicEvent::Deleted(ChannelDeletedMetadata {
                    channel_id,
                    actor: sender(owner),
                }),
                3,
            )),
            "channel.deleted",
            EntityType::Channel,
            CHANNEL_ENTITY_TYPE,
            channel_id.to_string(),
        ),
        EventCase::new(
            TestBrokerEvent::Channel(Event::with_schema_version(
                ChannelTopicEvent::ParticipantAdded(ChannelParticipantAddedMetadata {
                    channel_id,
                    channel_type: ChannelType::Team,
                    added_by: sender(owner),
                    added_user_ids: vec![user_id(member)],
                }),
                3,
            )),
            "channel.participant_added",
            EntityType::Channel,
            CHANNEL_ENTITY_TYPE,
            channel_id.to_string(),
        ),
        EventCase::new(
            TestBrokerEvent::Channel(Event::with_schema_version(
                ChannelTopicEvent::ParticipantRemoved(ChannelParticipantRemovedMetadata {
                    channel_id,
                    channel_type: ChannelType::Team,
                    removed_by: user_id(owner),
                    removed_user_ids: vec![user_id(member)],
                }),
                3,
            )),
            "channel.participant_removed",
            EntityType::Channel,
            CHANNEL_ENTITY_TYPE,
            channel_id.to_string(),
        ),
    ]
}

fn message_event_cases() -> Vec<EventCase> {
    let channel_id = Uuid::parse_str("44444444-4444-4444-4444-444444444444").unwrap();
    let message_id = Uuid::parse_str("55555555-5555-5555-5555-555555555555").unwrap();
    let member = "macro|[email protected]";
    let channel = MessageParent::Channel(channel_id);
    let document = MessageParent::parse("document", DOCUMENT_ID).unwrap();
    let posted = |parent: MessageParent| MessagePostedMetadata {
        parent,
        message_id,
        thread_id: None,
        root_id: message_id,
        sender: sender(member),
        triggered_by: None,
        content: "hello".to_string(),
        mentions: vec![],
        attachments: vec![],
        created_at: timestamp(),
    };

    vec![
        EventCase::new(
            TestBrokerEvent::Message(Event::with_schema_version(
                MessageTopicEvent::Posted(posted(channel.clone())),
                1,
            )),
            "message.posted",
            EntityType::Channel,
            CHANNEL_ENTITY_TYPE,
            channel_id.to_string(),
        ),
        // A document discussion resolves access on the document.
        EventCase::new(
            TestBrokerEvent::Message(Event::with_schema_version(
                MessageTopicEvent::Posted(posted(document.clone())),
                1,
            )),
            "message.posted",
            EntityType::Document,
            DOCUMENT_ENTITY_TYPE,
            DOCUMENT_ID.to_string(),
        ),
        EventCase::new(
            TestBrokerEvent::Message(Event::with_schema_version(
                MessageTopicEvent::Patched(MessagePatchedMetadata {
                    completed_reply: None,
                    parent: channel.clone(),
                    message_id,
                    thread_id: None,
                    root_id: message_id,
                    actor: sender(member),
                    content: "hello (edited)".to_string(),
                    edited_at: Some(timestamp()),
                    updated_at: timestamp(),
                }),
                1,
            )),
            "message.patched",
            EntityType::Channel,
            CHANNEL_ENTITY_TYPE,
            channel_id.to_string(),
        ),
        EventCase::new(
            TestBrokerEvent::Message(Event::with_schema_version(
                MessageTopicEvent::Deleted(MessageDeletedMetadata {
                    parent: channel.clone(),
                    message_id,
                    thread_id: None,
                    root_id: message_id,
                    actor: sender(member),
                    deleted_at: Some(timestamp()),
                }),
                1,
            )),
            "message.deleted",
            EntityType::Channel,
            CHANNEL_ENTITY_TYPE,
            channel_id.to_string(),
        ),
        EventCase::new(
            TestBrokerEvent::Message(Event::with_schema_version(
                MessageTopicEvent::AttachmentCreated(MessageAttachmentCreatedMetadata {
                    parent: channel.clone(),
                    message_id,
                    thread_id: None,
                    root_id: message_id,
                    actor: sender(member),
                    attachments: vec![],
                }),
                1,
            )),
            "message.attachment_created",
            EntityType::Channel,
            CHANNEL_ENTITY_TYPE,
            channel_id.to_string(),
        ),
        EventCase::new(
            TestBrokerEvent::Message(Event::with_schema_version(
                MessageTopicEvent::AttachmentRemoved(MessageAttachmentRemovedMetadata {
                    parent: channel.clone(),
                    message_id,
                    thread_id: None,
                    root_id: message_id,
                    actor: sender(member),
                    attachments: vec![],
                }),
                1,
            )),
            "message.attachment_removed",
            EntityType::Channel,
            CHANNEL_ENTITY_TYPE,
            channel_id.to_string(),
        ),
        // Like every message event, mentions match and resolve access on the
        // parent; the mentioned entity is payload for consumers to filter on.
        EventCase::new(
            TestBrokerEvent::Message(Event::with_schema_version(
                MessageTopicEvent::Mentioned(MessageMentionedMetadata {
                    parent: channel,
                    message_id,
                    thread_id: None,
                    root_id: message_id,
                    sender: sender(member),
                    content: "hello bot".to_string(),
                    mentioned: SimpleMention {
                        entity_type: "bot".to_string(),
                        entity_id: BOT_PRINCIPAL_ID.to_string(),
                    },
                    created_at: timestamp(),
                }),
                1,
            )),
            "message.mentioned",
            EntityType::Channel,
            CHANNEL_ENTITY_TYPE,
            channel_id.to_string(),
        ),
    ]
}

struct WebhookEventCase {
    event: Event<WebhookTopicEvent>,
    event_name: &'static str,
    webhook_id: &'static str,
    workspace_id: &'static str,
}

fn webhook_event_cases() -> Vec<WebhookEventCase> {
    vec![
        WebhookEventCase {
            event: Event::with_event_id_and_schema_version(
                Uuid::parse_str("01998a30-1a2b-7c3d-9e4f-5a6b7c8d9e0f").unwrap(),
                2,
                WebhookTopicEvent::Created(WebhookCreatedMetadata {
                    webhook_id: "wh_created".to_string(),
                    workspace_id: PERSONAL_WORKSPACE_ID.to_string(),
                    namespace: "created-webhook".to_string(),
                    created_by_user_id: user_id("macro|[email protected]"),
                    name: "Created webhook".to_string(),
                    endpoint_url: "https://example.com/created".to_string(),
                    status: WebhookStatus::Active,
                    is_valid: false,
                    filters: vec![],
                    header_names: vec!["X-Customer".to_string()],
                    created_at: timestamp(),
                }),
            ),
            event_name: "webhook.created",
            webhook_id: "wh_created",
            workspace_id: PERSONAL_WORKSPACE_ID,
        },
        WebhookEventCase {
            event: Event::with_event_id_and_schema_version(
                Uuid::parse_str("01998a30-2b3c-7d4e-8f50-6b7c8d9e0f1a").unwrap(),
                3,
                WebhookTopicEvent::Updated(WebhookUpdatedMetadata {
                    webhook_id: "wh_updated".to_string(),
                    workspace_id: TEAM_WORKSPACE_ID.to_string(),
                    actor_user_id: user_id("macro|[email protected]"),
                    name: Some("Updated webhook".to_string()),
                    endpoint_url: None,
                    filters: None,
                    headers_updated: true,
                    status: Some(WebhookStatus::Paused),
                    previous_status: Some(WebhookStatus::Active),
                    is_valid: false,
                    updated_at: timestamp(),
                }),
            ),
            event_name: "webhook.updated",
            webhook_id: "wh_updated",
            workspace_id: TEAM_WORKSPACE_ID,
        },
        WebhookEventCase {
            event: Event::with_event_id_and_schema_version(
                Uuid::parse_str("01998a30-3c4d-7e5f-8051-7c8d9e0f1a2b").unwrap(),
                4,
                WebhookTopicEvent::Deleted(WebhookDeletedMetadata {
                    webhook_id: "wh_deleted".to_string(),
                    workspace_id: PERSONAL_WORKSPACE_ID.to_string(),
                    actor_user_id: user_id("macro|[email protected]"),
                }),
            ),
            event_name: "webhook.deleted",
            webhook_id: "wh_deleted",
            workspace_id: PERSONAL_WORKSPACE_ID,
        },
        WebhookEventCase {
            event: Event::with_event_id_and_schema_version(
                Uuid::parse_str("01998a30-4d5e-7f60-8152-8d9e0f1a2b3c").unwrap(),
                5,
                WebhookTopicEvent::Validated(WebhookValidatedMetadata {
                    webhook_id: "wh_validated".to_string(),
                    workspace_id: TEAM_WORKSPACE_ID.to_string(),
                    actor_user_id: user_id("macro|[email protected]"),
                    is_valid: true,
                    response_status: Some(204),
                    message: None,
                }),
            ),
            event_name: "webhook.validated",
            webhook_id: "wh_validated",
            workspace_id: TEAM_WORKSPACE_ID,
        },
    ]
}

#[tokio::test]
async fn normalizes_and_matches_all_fifteen_event_variants() {
    let event_cases = document_event_cases()
        .into_iter()
        .chain(channel_event_cases())
        .chain(message_event_cases())
        .collect::<Vec<_>>();
    assert_eq!(event_cases.len(), 16);

    for event_case in event_cases {
        let access = MockAccessService::with_users(vec![user_id(PERSONAL_WORKSPACE_ID)]);
        let repository = MockRepository::new(
            vec![PERSONAL_WORKSPACE_ID.to_string()],
            vec![webhook("wh_match", PERSONAL_WORKSPACE_ID)],
        );
        let enqueuer = MockEnqueuer::default();
        let service = service(access.clone(), repository.clone(), enqueuer.clone());
        let expected_envelope = event_case.event.envelope();
        let before_ingestion = Utc::now();

        event_case
            .event
            .ingest(&service)
            .await
            .unwrap_or_else(|error| {
                panic!("{} should be ingested: {error}", event_case.event_name)
            });
        let after_ingestion = Utc::now();

        assert_eq!(
            lock(&access.calls).as_slice(),
            &[(event_case.entity_id.clone(), event_case.entity_type)],
            "entity access mapping for {}",
            event_case.event_name
        );
        let repository_state = lock(&repository.state);
        assert_eq!(
            repository_state.match_calls.as_slice(),
            &[MatchCall {
                workspace_ids: vec![PERSONAL_WORKSPACE_ID.to_string()],
                event_name: event_case.event_name.to_string(),
                entity_id: event_case.entity_id.clone(),
            }],
            "repository matching for {}",
            event_case.event_name
        );
        drop(repository_state);

        let enqueuer_state = lock(&enqueuer.state);
        assert_eq!(enqueuer_state.attempted_messages.len(), 1);
        let message = &enqueuer_state.attempted_messages[0];
        assert_eq!(message.webhook_id, "wh_match");
        assert_eq!(
            message.event.event_id,
            event_case.event.event_id().to_string()
        );
        assert_eq!(
            message.event.schema_version,
            event_case.event.schema_version()
        );
        assert_eq!(message.event.event_name, event_case.event_name);
        assert_eq!(message.event.entity_type, event_case.normalized_entity_type);
        assert_eq!(message.event.entity_id, event_case.entity_id);
        assert_eq!(message.event.ordering_key, event_case.entity_id);
        assert_eq!(message.event.broker_envelope, expected_envelope);
        assert_eq!(
            message.event.broker_envelope["event_type"],
            event_case.event_name
        );
        assert!(message.event.occurred_at >= before_ingestion);
        assert!(message.event.occurred_at <= after_ingestion);
    }
}

#[tokio::test]
async fn search_only_document_events_skip_webhook_delivery() {
    let access = MockAccessService {
        users: vec![user_id(PERSONAL_WORKSPACE_ID)],
        failure: Some(AccessFailure::Internal),
        calls: Arc::default(),
    };
    let repository = MockRepository::new(
        vec![PERSONAL_WORKSPACE_ID.to_string()],
        vec![webhook("wh_match", PERSONAL_WORKSPACE_ID)],
    );
    {
        let mut state = lock(&repository.state);
        state.fail_workspace_resolution = true;
        state.fail_matching = true;
    }
    let enqueuer = MockEnqueuer::default();
    let service = service(access.clone(), repository.clone(), enqueuer.clone());

    for (event_name, event) in search_only_document_event_cases() {
        assert!(
            normalized_document_event(&event)
                .unwrap_or_else(|error| panic!("{event_name} normalization failed: {error}"))
                .is_none(),
            "{event_name} should not normalize to a public webhook event"
        );
        service
            .ingest_document_event(event)
            .await
            .unwrap_or_else(|error| panic!("{event_name} ingestion failed: {error}"));
    }

    assert!(lock(&access.calls).is_empty());
    let repository_state = lock(&repository.state);
    assert!(repository_state.workspace_calls.is_empty());
    assert!(repository_state.match_calls.is_empty());
    assert!(lock(&enqueuer.state).attempted_messages.is_empty());
}

#[tokio::test]
async fn normalizes_all_webhook_variants_with_direct_workspace_ownership() {
    for event_case in webhook_event_cases() {
        let access = MockAccessService {
            users: vec![user_id("macro|[email protected]")],
            failure: Some(AccessFailure::Internal),
            calls: Arc::default(),
        };
        let repository = MockRepository::new(
            vec!["workspace-that-must-not-be-used".to_string()],
            vec![webhook("wh_match", event_case.workspace_id)],
        );
        lock(&repository.state).fail_workspace_resolution = true;
        let enqueuer = MockEnqueuer::default();
        let service = service(access.clone(), repository.clone(), enqueuer.clone());
        let expected_envelope = serde_json::to_value(&event_case.event).unwrap();
        let before_ingestion = Utc::now();

        service
            .ingest_webhook_event(event_case.event.clone())
            .await
            .unwrap_or_else(|error| {
                panic!("{} should be ingested: {error}", event_case.event_name)
            });
        let after_ingestion = Utc::now();

        assert!(
            lock(&access.calls).is_empty(),
            "entity access must not be called for {}",
            event_case.event_name
        );
        let repository_state = lock(&repository.state);
        assert!(
            repository_state.workspace_calls.is_empty(),
            "workspace resolution must not be called for {}",
            event_case.event_name
        );
        assert_eq!(
            repository_state.match_calls,
            vec![MatchCall {
                workspace_ids: vec![event_case.workspace_id.to_string()],
                event_name: event_case.event_name.to_string(),
                entity_id: event_case.webhook_id.to_string(),
            }],
            "strict workspace matching for {}",
            event_case.event_name
        );
        drop(repository_state);

        let enqueuer_state = lock(&enqueuer.state);
        assert_eq!(enqueuer_state.attempted_messages.len(), 1);
        let message = &enqueuer_state.attempted_messages[0];
        assert_eq!(message.webhook_id, "wh_match");
        assert_eq!(
            message.event.event_id,
            event_case.event.event_id.to_string()
        );
        assert_eq!(
            message.event.schema_version,
            event_case.event.schema_version
        );
        assert_eq!(message.event.event_name, event_case.event_name);
        assert_eq!(message.event.entity_type, WEBHOOK_ENTITY_TYPE);
        assert_eq!(message.event.entity_id, event_case.webhook_id);
        assert_eq!(message.event.ordering_key, event_case.webhook_id);
        assert_eq!(message.event.broker_envelope, expected_envelope);
        assert_eq!(
            message.event.broker_envelope["event_type"],
            event_case.event_name
        );
        assert!(message.event.occurred_at >= before_ingestion);
        assert!(message.event.occurred_at <= after_ingestion);
    }
}

#[tokio::test]
async fn webhook_event_fans_out_complete_envelope_to_every_match() {
    let event_case = webhook_event_cases().remove(1);
    let expected_envelope = serde_json::to_value(&event_case.event).unwrap();
    let access = MockAccessService::with_users(Vec::new());
    let repository = MockRepository::new(
        Vec::new(),
        vec![
            webhook("wh_first_match", event_case.workspace_id),
            webhook("wh_second_match", event_case.workspace_id),
        ],
    );
    let enqueuer = MockEnqueuer::default();
    let service = service(access.clone(), repository.clone(), enqueuer.clone());

    service
        .ingest_webhook_event(event_case.event)
        .await
        .expect("webhook fan-out succeeds");

    assert!(lock(&access.calls).is_empty());
    let repository_state = lock(&repository.state);
    assert!(repository_state.workspace_calls.is_empty());
    assert_eq!(
        repository_state.match_calls,
        vec![MatchCall {
            workspace_ids: vec![TEAM_WORKSPACE_ID.to_string()],
            event_name: "webhook.updated".to_string(),
            entity_id: "wh_updated".to_string(),
        }]
    );
    drop(repository_state);

    let enqueuer_state = lock(&enqueuer.state);
    let mut webhook_ids = enqueuer_state
        .attempted_messages
        .iter()
        .map(|message| message.webhook_id.as_str())
        .collect::<Vec<_>>();
    webhook_ids.sort_unstable();
    assert_eq!(webhook_ids, vec!["wh_first_match", "wh_second_match"]);
    assert_eq!(enqueuer_state.completed_webhook_ids.len(), 2);
    for message in &enqueuer_state.attempted_messages {
        assert_eq!(message.event.broker_envelope, expected_envelope);
    }
}

#[tokio::test]
async fn malformed_webhook_ids_are_permanent_and_skip_all_resolution() {
    let access = MockAccessService::with_users(vec![user_id(PERSONAL_WORKSPACE_ID)]);
    let repository = MockRepository::new(
        vec![PERSONAL_WORKSPACE_ID.to_string()],
        vec![webhook("wh_match", PERSONAL_WORKSPACE_ID)],
    );
    let enqueuer = MockEnqueuer::default();
    let service = service(access.clone(), repository.clone(), enqueuer.clone());

    for invalid_id in ["", "webhook_without_prefix"] {
        let event = Event::new(WebhookTopicEvent::Deleted(WebhookDeletedMetadata {
            webhook_id: invalid_id.to_string(),
            workspace_id: PERSONAL_WORKSPACE_ID.to_string(),
            actor_user_id: user_id("macro|[email protected]"),
        }));

        let error = service
            .ingest_webhook_event(event)
            .await
            .expect_err("malformed webhook ids are rejected");

        assert!(matches!(
            &error,
            WebhookEventIngestionError::InvalidEntityId {
                entity_type: WEBHOOK_ENTITY_TYPE,
                entity_id,
            } if entity_id == invalid_id
        ));
        assert!(!error.is_transient());
    }

    assert!(lock(&access.calls).is_empty());
    let repository_state = lock(&repository.state);
    assert!(repository_state.workspace_calls.is_empty());
    assert!(repository_state.match_calls.is_empty());
    assert!(lock(&enqueuer.state).attempted_messages.is_empty());
}

#[tokio::test]
async fn no_accessors_resolves_no_workspaces_and_enqueues_nothing() {
    let access = MockAccessService::with_users(Vec::new());
    let repository = MockRepository::new(Vec::new(), Vec::new());
    let enqueuer = MockEnqueuer::default();
    let service = service(access, repository.clone(), enqueuer.clone());
    let event = document_event_cases().remove(0).event;

    event.ingest(&service).await.expect("no access is a no-op");

    let repository_state = lock(&repository.state);
    assert_eq!(repository_state.workspace_calls, vec![Vec::new()]);
    assert_eq!(
        repository_state.match_calls,
        vec![MatchCall {
            workspace_ids: Vec::new(),
            event_name: "document.created".to_string(),
            entity_id: DOCUMENT_ID.to_string(),
        }]
    );
    assert!(lock(&enqueuer.state).attempted_messages.is_empty());
}

#[tokio::test]
async fn no_matching_webhooks_enqueues_nothing_after_workspace_expansion() {
    let reader = user_id(PERSONAL_WORKSPACE_ID);
    let access = MockAccessService::with_users(vec![reader.clone()]);
    let repository = MockRepository::new(
        vec![
            PERSONAL_WORKSPACE_ID.to_string(),
            TEAM_WORKSPACE_ID.to_string(),
        ],
        Vec::new(),
    );
    let enqueuer = MockEnqueuer::default();
    let service = service(access, repository.clone(), enqueuer.clone());
    let event = document_event_cases().remove(0).event;

    event.ingest(&service).await.expect("no matches is a no-op");

    let repository_state = lock(&repository.state);
    assert_eq!(repository_state.workspace_calls, vec![vec![reader]]);
    assert_eq!(
        repository_state.match_calls[0].workspace_ids,
        vec![
            PERSONAL_WORKSPACE_ID.to_string(),
            TEAM_WORKSPACE_ID.to_string(),
        ]
    );
    assert!(lock(&enqueuer.state).attempted_messages.is_empty());
}

#[tokio::test]
async fn fans_out_exact_envelope_once_per_personal_and_team_webhook() {
    let event = document_event_cases().remove(3).event;
    let expected_envelope = event.envelope();
    let reader = user_id(PERSONAL_WORKSPACE_ID);
    let access = MockAccessService::with_users(vec![reader.clone()]);
    let repository = MockRepository::new(
        vec![
            PERSONAL_WORKSPACE_ID.to_string(),
            TEAM_WORKSPACE_ID.to_string(),
        ],
        vec![
            webhook("wh_personal", PERSONAL_WORKSPACE_ID),
            webhook("wh_team", TEAM_WORKSPACE_ID),
        ],
    );
    let enqueuer = MockEnqueuer::default();
    let service = service(access, repository.clone(), enqueuer.clone());

    event.ingest(&service).await.expect("fan-out succeeds");

    let repository_state = lock(&repository.state);
    assert_eq!(repository_state.workspace_calls, vec![vec![reader]]);
    assert_eq!(
        repository_state.match_calls,
        vec![MatchCall {
            workspace_ids: vec![
                PERSONAL_WORKSPACE_ID.to_string(),
                TEAM_WORKSPACE_ID.to_string(),
            ],
            event_name: "document.copied".to_string(),
            entity_id: COPIED_DOCUMENT_ID.to_string(),
        }]
    );
    drop(repository_state);

    let enqueuer_state = lock(&enqueuer.state);
    let mut webhook_ids = enqueuer_state
        .attempted_messages
        .iter()
        .map(|message| message.webhook_id.as_str())
        .collect::<Vec<_>>();
    webhook_ids.sort_unstable();
    assert_eq!(webhook_ids, vec!["wh_personal", "wh_team"]);
    for message in &enqueuer_state.attempted_messages {
        assert_eq!(message.event.broker_envelope, expected_envelope);
        assert_eq!(message.event.entity_id, COPIED_DOCUMENT_ID);
    }
}

#[tokio::test]
async fn enqueue_failure_is_transient_after_every_send_completes() {
    let access = MockAccessService::with_users(vec![user_id(PERSONAL_WORKSPACE_ID)]);
    let repository = MockRepository::new(
        vec![PERSONAL_WORKSPACE_ID.to_string()],
        vec![
            webhook("wh_fail", PERSONAL_WORKSPACE_ID),
            webhook("wh_slow", PERSONAL_WORKSPACE_ID),
        ],
    );
    let enqueuer = MockEnqueuer::default();
    {
        let mut state = lock(&enqueuer.state);
        state.failing_webhook_id = Some("wh_fail".to_string());
        state.delayed_webhook_id = Some("wh_slow".to_string());
    }
    let service = service(access, repository, enqueuer.clone());
    let event = document_event_cases().remove(0).event;

    let error = event
        .ingest(&service)
        .await
        .expect_err("one failed send fails ingestion");

    assert!(matches!(error, WebhookEventIngestionError::Enqueue(_)));
    assert!(error.is_transient());
    let mut completed_webhook_ids = lock(&enqueuer.state).completed_webhook_ids.clone();
    completed_webhook_ids.sort_unstable();
    assert_eq!(completed_webhook_ids, vec!["wh_fail", "wh_slow"]);
}

#[tokio::test]
async fn malformed_document_id_is_permanent_and_skips_access_resolution() {
    let access = MockAccessService::with_users(vec![user_id(PERSONAL_WORKSPACE_ID)]);
    let repository = MockRepository::new(Vec::new(), Vec::new());
    let enqueuer = MockEnqueuer::default();
    let service = service(access.clone(), repository, enqueuer);
    let event = Event::new(DocumentTopicEvent::Deleted(DocumentDeletedMetadata {
        sub_type: None,
        document_id: "not-a-uuid".to_string(),
        actor_user_id: None,
        actor: None,
        on_behalf_of: None,
        project_id: None,
    }));

    let error = service
        .ingest_document_event(event)
        .await
        .expect_err("malformed ids are rejected");

    assert!(matches!(
        error,
        WebhookEventIngestionError::InvalidEntityId { .. }
    ));
    assert!(!error.is_transient());
    assert!(lock(&access.calls).is_empty());
}

#[test]
fn classifies_adapter_and_contract_errors() {
    let transient_db_error =
        WebhookEventIngestionError::EntityAccess(AccessError::from(sqlx::Error::PoolTimedOut));
    assert!(transient_db_error.is_transient());
    let permanent_db_error =
        WebhookEventIngestionError::EntityAccess(AccessError::from(sqlx::Error::RowNotFound));
    assert!(!permanent_db_error.is_transient());
    assert!(!WebhookEventIngestionError::EntityAccess(AccessError::internal("bug")).is_transient());
    assert!(!WebhookEventIngestionError::EntityAccess(AccessError::Unauthorized).is_transient());

    let serialization_error = serde_json::from_str::<Value>("{").expect_err("invalid json");
    assert!(!WebhookEventIngestionError::Serialization(serialization_error).is_transient());
    assert!(WebhookEventIngestionError::WorkspaceResolution(anyhow::anyhow!("db")).is_transient());
    assert!(WebhookEventIngestionError::Repository(anyhow::anyhow!("db")).is_transient());
    assert!(WebhookEventIngestionError::Enqueue(anyhow::anyhow!("queue")).is_transient());
}

#[tokio::test]
async fn entity_access_internal_error_is_not_transient() {
    let access = MockAccessService {
        users: Vec::new(),
        failure: Some(AccessFailure::Internal),
        calls: Arc::default(),
    };
    let repository = MockRepository::new(Vec::new(), Vec::new());
    let enqueuer = MockEnqueuer::default();
    let service = service(access, repository, enqueuer);
    let event = document_event_cases().remove(0).event;

    let error = event
        .ingest(&service)
        .await
        .expect_err("access adapter fails");

    assert!(matches!(
        error,
        WebhookEventIngestionError::EntityAccess(AccessError::Internal(_))
    ));
    assert!(!error.is_transient());
}

fn agent_trigger_new_event() -> Event<agent_trigger::domain::broker_events::AgentTriggerTopicEvent>
{
    use agent_trigger::domain::broker_events::{
        AgentBotMentionedEvent, AgentTriggerTopicEvent, NewAgentSessionEvent,
    };

    Event::new(AgentTriggerTopicEvent::New(
        NewAgentSessionEvent::TopLevelMentioned(AgentBotMentionedEvent {
            bot_id: bot_id::BotId::new_from_uuid(uuid::Uuid::from_u128(0xB07)),
            message: ChannelMessagePostedMetadata {
                channel_id: uuid::Uuid::from_u128(1),
                message_id: uuid::Uuid::from_u128(2),
                thread_id: None,
                sender: sender("macro|[email protected]"),
                triggered_by: None,
                channel_type: ChannelType::Public,
                content: "fix the flaky test".to_owned(),
                mentions: vec![],
                attachments: vec![],
                created_at: timestamp(),
            },
            context: None,
        }),
    ))
}

fn agent_trigger_requested_event()
-> Event<agent_trigger::domain::broker_events::AgentTriggerTopicEvent> {
    use agent_trigger::domain::broker_events::{
        AgentSessionRequestedEvent, AgentTriggerTopicEvent, NewAgentSessionEvent,
    };

    Event::new(AgentTriggerTopicEvent::New(
        NewAgentSessionEvent::Requested(AgentSessionRequestedEvent {
            origin: trigger_context::TriggerOrigin::Unknown,
            repo_url: None,
            bot_id: bot_id::BotId::new_from_uuid(uuid::Uuid::from_u128(0xB07)),
            session_id: agent_session::domain::model::AgentSessionId::new_from_uuid(
                uuid::Uuid::from_u128(0x5E55),
            ),
            owner: "macro|[email protected]".to_owned(),
            context: None,
        }),
    ))
}

fn agent_trigger_document_event()
-> Event<agent_trigger::domain::broker_events::AgentTriggerTopicEvent> {
    use agent_trigger::domain::broker_events::{
        AgentMentionedEvent, AgentTriggerTopicEvent, NewAgentSessionEvent,
    };

    Event::new(AgentTriggerTopicEvent::New(
        NewAgentSessionEvent::Mentioned(AgentMentionedEvent {
            bot_id: bot_id::BotId::new_from_uuid(uuid::Uuid::from_u128(0xB07)),
            message: messages::domain::events::MessagePostedMetadata {
                parent: messages::domain::models::MessageParent::parse("document", "doc-1")
                    .unwrap(),
                message_id: uuid::Uuid::from_u128(2),
                thread_id: None,
                root_id: uuid::Uuid::from_u128(2),
                sender: sender("macro|[email protected]"),
                triggered_by: None,
                content: "summarize this".to_owned(),
                mentions: vec![],
                attachments: vec![],
                created_at: timestamp(),
            },
            context: None,
        }),
    ))
}

fn agent_trigger_existing_event()
-> Event<agent_trigger::domain::broker_events::AgentTriggerTopicEvent> {
    use agent_trigger::domain::broker_events::{
        AgentTriggerTopicEvent, ChannelEventMetadata, ExistingAgentSessionEvent,
    };

    Event::new(AgentTriggerTopicEvent::Existing(
        ExistingAgentSessionEvent::Channel(ChannelEventMetadata {
            bot_id: bot_id::BotId::new_from_uuid(uuid::Uuid::from_u128(0xB07)),
            session_id: agent_session::domain::model::AgentSessionId::TEST_A,
            kind: agent_trigger::domain::broker_events::ThreadMessageKind::MentionThread,
            message: ChannelMessagePostedMetadata {
                channel_id: uuid::Uuid::from_u128(1),
                message_id: uuid::Uuid::from_u128(2),
                thread_id: None,
                sender: sender("macro|[email protected]"),
                triggered_by: None,
                channel_type: ChannelType::Public,
                content: "and now the other one".to_owned(),
                mentions: vec![],
                attachments: vec![],
                created_at: timestamp(),
            },
            context: None,
        }),
    ))
}

/// A document trigger is gated by who may read the document.
#[tokio::test]
async fn a_document_trigger_is_gated_by_its_document() {
    let access = MockAccessService::with_users(vec![user_id(PERSONAL_WORKSPACE_ID)]);
    let repository = MockRepository::new(
        vec![PERSONAL_WORKSPACE_ID.to_string()],
        vec![webhook("wh_agent_feed", PERSONAL_WORKSPACE_ID)],
    );
    let service = service(access.clone(), repository.clone(), MockEnqueuer::default());

    service
        .ingest_agent_trigger_event(agent_trigger_document_event())
        .await
        .expect("document agent trigger events are ingested");

    assert_eq!(
        lock(&access.calls).as_slice(),
        &[("doc-1".to_owned(), EntityType::Document)],
    );
}

fn agent_trigger_task_assignment_event() -> Event<AgentTriggerTopicEvent> {
    use agent_trigger::domain::broker_events::{AgentAssignedToTaskEvent, NewAgentSessionEvent};

    Event::new(AgentTriggerTopicEvent::New(
        NewAgentSessionEvent::AssignedToTask(AgentAssignedToTaskEvent {
            origin: trigger_context::TriggerOrigin::Unknown,
            bot_id: bot_id::BotId::TEST_A,
            parent: messages::domain::models::MessageParent::parse("document", DOCUMENT_ID)
                .unwrap(),
            discussion_id: uuid::Uuid::from_u128(2),
            actor: user_id("macro|[email protected]"),
            prompt: "Complete the assigned task".to_owned(),
            context: None,
        }),
    ))
}

#[tokio::test]
async fn a_task_assignment_is_gated_by_the_task_and_delivered_under_the_bot() {
    let access = MockAccessService::with_users(vec![user_id(PERSONAL_WORKSPACE_ID)]);
    let repository = MockRepository::new(
        vec![PERSONAL_WORKSPACE_ID.to_string()],
        vec![webhook("wh_agent_feed", PERSONAL_WORKSPACE_ID)],
    );
    let enqueuer = MockEnqueuer::default();
    let service = service(access.clone(), repository.clone(), enqueuer.clone());
    let event = agent_trigger_task_assignment_event();

    service
        .ingest_agent_trigger_event(event.clone())
        .await
        .expect("task assignments are ingested");

    assert_eq!(
        lock(&access.calls).as_slice(),
        &[(DOCUMENT_ID.to_owned(), EntityType::Document)],
    );
    let repository_state = lock(&repository.state);
    assert_eq!(
        repository_state.workspace_calls,
        vec![vec![user_id(PERSONAL_WORKSPACE_ID)]],
    );
    assert_eq!(repository_state.match_calls.len(), 1);
    assert_eq!(
        repository_state.match_calls[0].entity_id,
        bot_id::BotId::TEST_A.to_string(),
    );
    assert_eq!(
        repository_state.match_calls[0].event_name,
        "agent_trigger.new"
    );
    drop(repository_state);
    let enqueuer_state = lock(&enqueuer.state);
    assert_eq!(enqueuer_state.attempted_messages.len(), 1);
    assert_eq!(
        enqueuer_state.attempted_messages[0].event.broker_envelope,
        serde_json::to_value(event).expect("serialize assignment envelope"),
    );
}

#[tokio::test]
async fn a_task_assignment_does_not_grant_the_assigner_access_to_the_trigger() {
    let access = MockAccessService::with_users(vec![]);
    let repository = MockRepository::new(vec![], vec![]);
    let enqueuer = MockEnqueuer::default();
    let service = service(access.clone(), repository.clone(), enqueuer.clone());

    service
        .ingest_agent_trigger_event(agent_trigger_task_assignment_event())
        .await
        .expect("an assignment without any readers is ingested");

    assert_eq!(
        lock(&access.calls).as_slice(),
        &[(DOCUMENT_ID.to_owned(), EntityType::Document)],
    );
    let repository_state = lock(&repository.state);
    assert_eq!(repository_state.workspace_calls, vec![vec![]]);
    assert!(repository_state.match_calls[0].workspace_ids.is_empty());
    assert!(lock(&enqueuer.state).attempted_messages.is_empty());
}

/// An existing session does not grant a webhook access to a new message parent.
#[tokio::test]
async fn an_existing_session_trigger_is_gated_by_its_message_parent() {
    let access = MockAccessService::with_users(vec![user_id(PERSONAL_WORKSPACE_ID)]);
    let repository = MockRepository::new(
        vec![PERSONAL_WORKSPACE_ID.to_string()],
        vec![webhook("wh_agent_feed", PERSONAL_WORKSPACE_ID)],
    );
    let service = service(access.clone(), repository.clone(), MockEnqueuer::default());

    service
        .ingest_agent_trigger_event(agent_trigger_existing_event())
        .await
        .expect("agent trigger events are ingested");

    assert_eq!(
        lock(&access.calls).as_slice(),
        &[(uuid::Uuid::from_u128(1).to_string(), EntityType::Channel)],
    );
}

/// A session asked for from the composer was posted nowhere, so there is no
/// parent whose readers decide who sees it: the requester alone does, and
/// their workspaces are where the bot's runtime subscribes from.
#[tokio::test]
async fn a_requested_session_is_scoped_to_its_requester_and_named_by_the_bot() {
    let access = MockAccessService::with_users(vec![]);
    let repository = MockRepository::new(
        vec![PERSONAL_WORKSPACE_ID.to_string()],
        vec![webhook("wh_agent_feed", PERSONAL_WORKSPACE_ID)],
    );
    let enqueuer = MockEnqueuer::default();
    let service = service(access.clone(), repository.clone(), enqueuer.clone());
    let event = agent_trigger_requested_event();

    service
        .ingest_agent_trigger_event(event.clone())
        .await
        .expect("requested sessions are ingested");

    // Nobody's access was consulted: the requester is the audience.
    assert!(lock(&access.calls).is_empty());
    let bot_id = bot_id::BotId::new_from_uuid(uuid::Uuid::from_u128(0xB07)).to_string();
    let repository_state = lock(&repository.state);
    assert_eq!(repository_state.match_calls.len(), 1);
    assert_eq!(repository_state.match_calls[0].entity_id, bot_id);
    assert_eq!(
        repository_state.match_calls[0].event_name,
        "agent_trigger.new"
    );
    drop(repository_state);
    let enqueuer_state = lock(&enqueuer.state);
    assert_eq!(enqueuer_state.attempted_messages.len(), 1);
    assert_eq!(
        enqueuer_state.attempted_messages[0].event.broker_envelope,
        serde_json::to_value(&event).expect("a serializable envelope"),
    );
}

#[tokio::test]
async fn agent_trigger_events_are_scoped_by_the_channel_but_named_by_the_bot() {
    let access = MockAccessService::with_users(vec![user_id(PERSONAL_WORKSPACE_ID)]);
    let repository = MockRepository::new(
        vec![PERSONAL_WORKSPACE_ID.to_string()],
        vec![webhook("wh_agent_feed", PERSONAL_WORKSPACE_ID)],
    );
    let enqueuer = MockEnqueuer::default();
    let service = service(access.clone(), repository.clone(), enqueuer.clone());
    let event = agent_trigger_new_event();

    service
        .ingest_agent_trigger_event(event.clone())
        .await
        .expect("agent trigger events are ingested");

    let bot_id = bot_id::BotId::new_from_uuid(uuid::Uuid::from_u128(0xB07)).to_string();
    // Who may see it is decided by the channel the mention sits in - the same
    // question a channel event asks - not by the bot.
    assert_eq!(
        lock(&access.calls).as_slice(),
        &[(uuid::Uuid::from_u128(1).to_string(), EntityType::Channel)],
    );
    let repository_state = lock(&repository.state);
    // ...but the entity matched on is the bot, so a filter's `ids` scopes a
    // subscriber to exactly one bot's triggers.
    assert_eq!(repository_state.match_calls.len(), 1);
    assert_eq!(repository_state.match_calls[0].entity_id, bot_id);
    assert_eq!(
        repository_state.match_calls[0].event_name,
        "agent_trigger.new"
    );
    assert_eq!(
        repository_state.match_calls[0].workspace_ids,
        vec![PERSONAL_WORKSPACE_ID.to_string()]
    );
    drop(repository_state);

    // The delivered body is the broker envelope, verbatim: exactly what the
    // daemon on the other end decodes.
    let enqueuer_state = lock(&enqueuer.state);
    assert_eq!(enqueuer_state.attempted_messages.len(), 1);
    let normalized = &enqueuer_state.attempted_messages[0].event;
    assert_eq!(normalized.event_name, "agent_trigger.new");
    assert_eq!(normalized.entity_type, "bot");
    assert_eq!(normalized.entity_id, bot_id);
    assert_eq!(normalized.ordering_key, bot_id);
    assert_eq!(
        normalized.broker_envelope,
        serde_json::to_value(&event).expect("a serializable envelope"),
    );
}

fn agent_session_lifecycle_event(
    event: fn(
        agent_session::domain::events::SessionIdentity,
    ) -> agent_session::domain::events::AgentSessionLifecycleEvent,
) -> Event<agent_session::domain::events::AgentSessionLifecycleEvent> {
    use agent_session::domain::events::{SessionIdentity, ThreadOrigin};
    use macro_user_id::cowlike::CowLike as _;

    Event::new(event(SessionIdentity {
        session_id: agent_session::domain::model::AgentSessionId::TEST_A,
        session_name: "Fix the flaky test".to_owned(),
        bot_id: bot_id::BotId::new_from_uuid(uuid::Uuid::from_u128(0xB07)),
        bot_name: "Macro Coder".to_owned(),
        owner_id: macro_user_id::user_id::MacroUserIdStr::parse_from_str("macro|[email protected]")
            .expect("valid user id")
            .into_owned(),
        origin: Some(ThreadOrigin::new(
            messages::domain::models::MessageParent::Channel(uuid::Uuid::from_u128(1)),
            uuid::Uuid::from_u128(2),
            uuid::Uuid::from_u128(3),
        )),
        audience: Vec::new(),
        source: agent_session::domain::model::SessionSource::User,
    }))
}

/// A lifecycle fact is about one session: the session is the entity, the
/// ordering key, and whose grants gate it - not the bot, unlike a trigger.
#[tokio::test]
async fn agent_session_lifecycle_events_are_scoped_and_named_by_the_session() {
    use agent_session::domain::events::{
        AgentSessionLifecycleEvent, SessionSettledMetadata, TurnSummary,
    };

    let access = MockAccessService::with_users(vec![user_id(PERSONAL_WORKSPACE_ID)]);
    let repository = MockRepository::new(
        vec![PERSONAL_WORKSPACE_ID.to_string()],
        vec![webhook("wh_agent_feed", PERSONAL_WORKSPACE_ID)],
    );
    let enqueuer = MockEnqueuer::default();
    let service = service(access.clone(), repository.clone(), enqueuer.clone());
    let event = agent_session_lifecycle_event(|identity| {
        AgentSessionLifecycleEvent::Settled(SessionSettledMetadata {
            identity,
            last_turn: Some(TurnSummary {
                turn: agent_fold::domain::model::TurnId(0),
                action_id: agent_runtime_protocol::domain::action::AgentActionId::mint(),
                actor: None,
                announcement_message_id: None,
                stop_reason: "end_turn".to_owned(),
                excerpt: Some("Done.".to_owned()),
            }),
        })
    });
    let session_id = agent_session::domain::model::AgentSessionId::TEST_A.to_string();

    service
        .ingest_agent_session_lifecycle_event(event.clone())
        .await
        .expect("lifecycle events are ingested");

    assert_eq!(
        lock(&access.calls).as_slice(),
        &[(session_id.clone(), EntityType::AgentSession)],
    );
    let repository_state = lock(&repository.state);
    assert_eq!(repository_state.match_calls.len(), 1);
    assert_eq!(repository_state.match_calls[0].entity_id, session_id);
    assert_eq!(
        repository_state.match_calls[0].event_name,
        "agent_session.settled"
    );
    drop(repository_state);

    let enqueuer_state = lock(&enqueuer.state);
    assert_eq!(enqueuer_state.attempted_messages.len(), 1);
    let normalized = &enqueuer_state.attempted_messages[0].event;
    assert_eq!(normalized.event_name, "agent_session.settled");
    assert_eq!(normalized.entity_type, "agent_session");
    assert_eq!(normalized.entity_id, session_id);
    assert_eq!(normalized.ordering_key, session_id);
    assert_eq!(
        normalized.broker_envelope,
        serde_json::to_value(&event).expect("a serializable envelope"),
    );
}

#[tokio::test]
async fn every_agent_session_lifecycle_variant_is_named_by_its_wire_tag() {
    use agent_session::domain::events::{
        AgentSessionLifecycleEvent, SessionDeletedMetadata, WaitingForInputMetadata,
    };

    let access = MockAccessService::with_users(vec![user_id(PERSONAL_WORKSPACE_ID)]);
    let repository = MockRepository::new(
        vec![PERSONAL_WORKSPACE_ID.to_string()],
        vec![webhook("wh_agent_feed", PERSONAL_WORKSPACE_ID)],
    );
    let service = service(access, repository.clone(), MockEnqueuer::default());

    service
        .ingest_agent_session_lifecycle_event(agent_session_lifecycle_event(|identity| {
            AgentSessionLifecycleEvent::WaitingForInput(WaitingForInputMetadata {
                identity,
                turn: agent_fold::domain::model::TurnId(1),
                action_id: agent_runtime_protocol::domain::action::AgentActionId::mint(),
                announcement_message_id: None,
                question: "Which approach?".to_owned(),
            })
        }))
        .await
        .expect("lifecycle events are ingested");
    service
        .ingest_agent_session_lifecycle_event(agent_session_lifecycle_event(|identity| {
            AgentSessionLifecycleEvent::Deleted(SessionDeletedMetadata { identity })
        }))
        .await
        .expect("lifecycle events are ingested");

    let names: Vec<String> = lock(&repository.state)
        .match_calls
        .iter()
        .map(|call| call.event_name.clone())
        .collect();
    assert_eq!(
        names,
        ["agent_session.waiting_for_input", "agent_session.deleted"]
    );
}

/// A deleted session has no access rows left to ask, so its audience comes
/// from the event: the owner, plus the channel it was opened from.
#[tokio::test]
async fn a_deleted_session_is_delivered_to_its_owner_and_origin_channel() {
    use agent_session::domain::events::{AgentSessionLifecycleEvent, SessionDeletedMetadata};

    let access = MockAccessService::with_users(vec![user_id("macro|[email protected]")]);
    let repository = MockRepository::new(
        vec![PERSONAL_WORKSPACE_ID.to_string()],
        vec![webhook("wh_agent_feed", PERSONAL_WORKSPACE_ID)],
    );
    let enqueuer = MockEnqueuer::default();
    let service = service(access.clone(), repository.clone(), enqueuer.clone());

    service
        .ingest_agent_session_lifecycle_event(agent_session_lifecycle_event(|identity| {
            AgentSessionLifecycleEvent::Deleted(SessionDeletedMetadata { identity })
        }))
        .await
        .expect("lifecycle events are ingested");

    // The session is never asked about; the origin channel is.
    assert_eq!(
        lock(&access.calls).as_slice(),
        &[(uuid::Uuid::from_u128(1).to_string(), EntityType::Channel)],
    );
    let repository_state = lock(&repository.state);
    assert_eq!(repository_state.match_calls.len(), 1);
    assert_eq!(
        repository_state.match_calls[0].event_name,
        "agent_session.deleted"
    );
    assert_eq!(
        repository_state.workspace_calls.last().map(Vec::len),
        Some(2),
        "the owner and the channel's member both resolve to workspaces"
    );
}

#[tokio::test]
async fn a_deleted_session_without_an_origin_is_its_owners_alone() {
    use agent_session::domain::events::{AgentSessionLifecycleEvent, SessionDeletedMetadata};

    let access = MockAccessService::with_users(vec![user_id("macro|[email protected]")]);
    let repository = MockRepository::new(
        vec![PERSONAL_WORKSPACE_ID.to_string()],
        vec![webhook("wh_agent_feed", PERSONAL_WORKSPACE_ID)],
    );
    let service = service(access.clone(), repository.clone(), MockEnqueuer::default());

    let mut event = agent_session_lifecycle_event(|identity| {
        AgentSessionLifecycleEvent::Deleted(SessionDeletedMetadata { identity })
    });
    if let AgentSessionLifecycleEvent::Deleted(deleted) = &mut event.event {
        deleted.identity.origin = None;
    }

    service
        .ingest_agent_session_lifecycle_event(event)
        .await
        .expect("lifecycle events are ingested");

    assert!(lock(&access.calls).is_empty(), "nothing is looked up");
    assert_eq!(
        lock(&repository.state).workspace_calls.last().map(Vec::len),
        Some(1)
    );
}

/// The live stream mirrors the persisted fan-out: a departed session's owner
/// keeps ownership, so the stream audience is the union of the owner's
/// workspace and the origin parent, not the parent alone.
#[test]
fn a_deleted_session_streams_to_both_its_owner_and_origin_parent() {
    use crate::domain::ingestion::stream::agent_session_lifecycle_stream_candidate;
    use crate::domain::stream::StreamAudience;
    use agent_session::domain::events::{AgentSessionLifecycleEvent, SessionDeletedMetadata};

    let event = agent_session_lifecycle_event(|identity| {
        AgentSessionLifecycleEvent::Deleted(SessionDeletedMetadata { identity })
    });

    let candidate =
        agent_session_lifecycle_stream_candidate(&event).expect("a deleted session streams");

    assert_eq!(
        candidate.audience,
        StreamAudience::Any(vec![
            StreamAudience::Workspace {
                workspace_id: "macro|[email protected]".to_owned(),
            },
            StreamAudience::Entity {
                entity_id: uuid::Uuid::from_u128(1).to_string(),
                entity_type: EntityType::Channel,
            },
        ]),
    );
}