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.

68.1 KB
Raw Blame
use super::*;
use crate::domain::{
    dm::ensure_dms_for_joining_member,
    events::ChannelEvent,
    models::{
        Activity, ActivityType, ChannelAttachment, ChannelAttachmentType, ChannelInfo,
        ChannelMetadata, ChannelParticipant, ChannelType, CreateChannelRequest,
        CreateEntityMentionOptions, CreatedChannel, EntityMention, GetOrCreateDmRequest,
        ParticipantRole, PatchChannelRequest, ReferencedShareItem, Sender,
    },
    ports::{
        ChannelEventDispatcher, ChannelReferenceSharePermissions, ChannelRepo, CommentParentAccess,
        MockChannelRepo,
    },
};
use chrono::Utc;
use entity_access::domain::models::{ParticipantRole as AccessRole, TeamRole};
use macro_user_id::{cowlike::CowLike, user_id::MacroUserIdStr};
use std::{
    collections::HashSet,
    sync::{Arc, Mutex},
};

mod deletion;

fn empty_repo() -> MockChannelRepo {
    let mut repo = MockChannelRepo::new();
    repo.expect_get_channel_attachments()
        .returning(|_, _, _, _| Box::pin(async { Ok(vec![]) }));
    repo.expect_get_channel_participants()
        .returning(|_| Box::pin(async { Ok(vec![]) }));
    repo
}

#[derive(Clone)]
struct FakeMutationRepo {
    picture_updates: Arc<Mutex<Vec<(Uuid, Option<Uuid>)>>>,
    state: Arc<Mutex<FakeMutationRepoState>>,
}

struct FakeMutationRepoState {
    channel_id: Uuid,
    channel_picture_id: Option<Uuid>,
    channel_name: Option<String>,
    channel_type: ChannelType,
    channel_team_id: Option<Uuid>,
    direct_agent: bool,
    team_roles: HashMap<(String, Uuid), TeamRole>,
    deleted_channels: Vec<Uuid>,
    user_team_id: Option<Uuid>,
    user_team_id_lookups: usize,
    join_code: Option<Uuid>,
    participants: Vec<ChannelParticipant>,
    participant_additions: usize,
    thread_participants: Vec<MacroUserIdStr<'static>>,
    activity_upserts: usize,
    touched_channel_ids: Vec<Uuid>,
    fail_channel_touches: bool,
    removed_participants: Vec<String>,
    channel_patches: Vec<(PatchChannelRequest, Option<Uuid>)>,
}

impl FakeMutationRepo {
    fn new(channel_id: Uuid, sender: &str) -> Self {
        let now = Utc::now();
        Self {
            picture_updates: Arc::default(),
            state: Arc::new(Mutex::new(FakeMutationRepoState {
                channel_id,
                channel_picture_id: None,
                channel_name: Some("Project".to_string()),
                channel_type: ChannelType::Private,
                channel_team_id: None,
                direct_agent: false,
                team_roles: HashMap::new(),
                deleted_channels: vec![],
                user_team_id: None,
                user_team_id_lookups: 0,
                join_code: None,
                participants: vec![
                    ChannelParticipant {
                        channel_id,
                        user_id: sender.to_string(),
                        role: ParticipantRole::Owner,
                        joined_at: now,
                        left_at: None,
                    },
                    ChannelParticipant {
                        channel_id,
                        user_id: "macro|[email protected]".to_string(),
                        role: ParticipantRole::Member,
                        joined_at: now,
                        left_at: None,
                    },
                ],
                participant_additions: 0,
                thread_participants: vec![
                    MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap(),
                ],
                activity_upserts: 0,
                touched_channel_ids: vec![],
                fail_channel_touches: false,
                removed_participants: vec![],
                channel_patches: vec![],
            })),
        }
    }
}

impl ChannelRepo for FakeMutationRepo {
    async fn set_channel_picture(
        &self,
        channel_id: Uuid,
        picture_id: Option<Uuid>,
    ) -> Result<bool, Self::Err> {
        self.picture_updates
            .lock()
            .unwrap()
            .push((channel_id, picture_id));
        let mut state = self.state.lock().unwrap();
        let changed = state.channel_picture_id != picture_id;
        state.channel_picture_id = picture_id;
        Ok(changed)
    }

    type Err = anyhow::Error;

    async fn get_channel_attachments(
        &self,
        _channel_id: Uuid,
        _query: &Query<Uuid, CreatedAt, ()>,
        _limit: u16,
        _attachment_type: Option<ChannelAttachmentType>,
    ) -> Result<Vec<ChannelAttachment>, Self::Err> {
        Ok(vec![])
    }

    async fn get_channel_participants(
        &self,
        _channel_id: Uuid,
    ) -> Result<Vec<ChannelParticipant>, Self::Err> {
        Ok(self.state.lock().unwrap().participants.clone())
    }

    async fn get_attachment_references(
        &self,
        _entity_type: &str,
        _entity_id: &str,
        _user_id: &str,
    ) -> Result<Vec<crate::domain::models::AttachmentEntityReference>, Self::Err> {
        Ok(vec![])
    }

    async fn get_channel_info(&self, channel_id: Uuid) -> Result<ChannelInfo, Self::Err> {
        let state = self.state.lock().unwrap();
        Ok(ChannelInfo {
            id: channel_id,
            name: state.channel_name.clone(),
            channel_type: state.channel_type,
            org_id: None,
            team_id: state.channel_team_id,
        })
    }

    async fn is_direct_agent_conversation(&self, _channel_id: Uuid) -> Result<bool, Self::Err> {
        Ok(self.state.lock().unwrap().direct_agent)
    }

    async fn get_or_create_channel_join_code(&self, _channel_id: Uuid) -> Result<Uuid, Self::Err> {
        let mut state = self.state.lock().unwrap();
        let join_code = *state.join_code.get_or_insert_with(Uuid::new_v4);
        Ok(join_code)
    }

    async fn get_channel_info_by_join_code(
        &self,
        join_code: Uuid,
    ) -> Result<Option<ChannelInfo>, Self::Err> {
        let state = self.state.lock().unwrap();
        Ok((state.join_code == Some(join_code)).then(|| ChannelInfo {
            id: state.channel_id,
            name: state.channel_name.clone(),
            channel_type: state.channel_type,
            org_id: None,
            team_id: state.channel_team_id,
        }))
    }

    async fn get_channel_metadata(
        &self,
        _channel_id: Uuid,
        _viewer_user_id: MacroUserIdStr<'static>,
    ) -> Result<ChannelMetadata, Self::Err> {
        Ok(ChannelMetadata {
            channel_type: ChannelType::Private,
            channel_name: "Project".to_string(),
        })
    }

    async fn batch_get_channel_previews(
        &self,
        _channel_ids: &[String],
        _viewer_user_id: &str,
        _org_id: Option<i64>,
    ) -> Result<Vec<crate::domain::models::ChannelPreviewRow>, Self::Err> {
        Ok(vec![])
    }

    async fn resolve_channel_name(
        &self,
        _info: &ChannelInfo,
        _viewer_user_id: MacroUserIdStr<'static>,
    ) -> Result<String, Self::Err> {
        Ok("Project".to_string())
    }

    async fn get_user_display_names(
        &self,
        _user_ids: &[MacroUserIdStr<'static>],
    ) -> Result<std::collections::HashMap<MacroUserIdStr<'static>, String>, Self::Err> {
        Ok(std::collections::HashMap::new())
    }

    async fn user_has_team(&self, _user_id: String, _team_id: Uuid) -> Result<bool, Self::Err> {
        Ok(true)
    }

    async fn get_user_team_id(
        &self,
        _user_id: &MacroUserIdStr<'_>,
    ) -> Result<Option<Uuid>, Self::Err> {
        let mut state = self.state.lock().unwrap();
        state.user_team_id_lookups += 1;
        Ok(state.user_team_id)
    }

    async fn create_channel(
        &self,
        owner_id: MacroUserIdStr<'_>,
        _org_id: Option<i64>,
        req: crate::domain::models::CreateChannelRequest,
    ) -> Result<CreatedChannel, Self::Err> {
        let state = self.state.lock().unwrap();
        let mut participant_user_ids = req.participants;
        participant_user_ids.insert(owner_id.into_owned());
        if req.auto_join_team {
            participant_user_ids.extend(
                state
                    .participants
                    .iter()
                    .map(|participant| macro_id(&participant.user_id)),
            );
        }
        let mut participant_user_ids: Vec<_> = participant_user_ids.into_iter().collect();
        participant_user_ids.sort_by(|a, b| a.as_ref().cmp(b.as_ref()));
        Ok(CreatedChannel {
            id: state.channel_id,
            participant_user_ids,
        })
    }

    async fn auto_join_by_team_id(
        &self,
        _team_id: &Uuid,
        _user_id: &MacroUserIdStr<'_>,
    ) -> Result<(), Self::Err> {
        Ok(())
    }

    async fn leave_by_team_id(
        &self,
        _team_id: &Uuid,
        _user_id: &MacroUserIdStr<'_>,
    ) -> Result<Vec<Uuid>, Self::Err> {
        Ok(Vec::new())
    }

    async fn restore_by_channel_ids(
        &self,
        _user_id: &MacroUserIdStr<'_>,
        _channel_ids: &[Uuid],
    ) -> Result<(), Self::Err> {
        Ok(())
    }

    async fn ensure_dm(
        &self,
        pair: crate::domain::dm::DmPair,
        creation: crate::domain::historical::DmCreation,
    ) -> Result<crate::domain::historical::EnsuredChannel, Self::Err> {
        creation.owner(&pair)?;
        Ok(crate::domain::historical::EnsuredChannel {
            id: self.state.lock().unwrap().channel_id,
            created: true,
        })
    }

    async fn maybe_get_dm(
        &self,
        _user_id: MacroUserIdStr<'_>,
        _recipient_id: MacroUserIdStr<'_>,
    ) -> Result<Option<Uuid>, Self::Err> {
        Ok(None)
    }

    async fn maybe_get_private_channel(
        &self,
        _participants: std::collections::HashSet<MacroUserIdStr<'_>>,
    ) -> Result<Option<Uuid>, Self::Err> {
        Ok(None)
    }

    async fn patch_channel(
        &self,
        _channel_id: Uuid,
        _user_id: String,
        team_id: Option<Uuid>,
        req: PatchChannelRequest,
    ) -> Result<(), Self::Err> {
        self.state
            .lock()
            .unwrap()
            .channel_patches
            .push((req, team_id));
        Ok(())
    }

    async fn user_is_team_admin(
        &self,
        user_id: &MacroUserIdStr<'_>,
        team_id: Uuid,
    ) -> Result<bool, Self::Err> {
        Ok(self
            .state
            .lock()
            .unwrap()
            .team_roles
            .get(&(user_id.to_string(), team_id))
            .is_some_and(|role| *role >= TeamRole::Admin))
    }

    async fn delete_channel(&self, channel_id: Uuid) -> Result<(), Self::Err> {
        self.state.lock().unwrap().deleted_channels.push(channel_id);
        Ok(())
    }

    async fn add_participant(
        &self,
        channel_id: Uuid,
        user_id: MacroUserIdStr<'_>,
        role: ParticipantRole,
    ) -> Result<bool, Self::Err> {
        let mut state = self.state.lock().unwrap();
        if let Some(participant) = state
            .participants
            .iter_mut()
            .find(|participant| participant.user_id == user_id.as_ref())
        {
            if participant.left_at.is_none() {
                return Ok(false);
            }
            participant.role = role;
            participant.joined_at = Utc::now();
            participant.left_at = None;
        } else {
            state.participants.push(ChannelParticipant {
                channel_id,
                user_id: user_id.as_ref().to_string(),
                role,
                joined_at: Utc::now(),
                left_at: None,
            });
        }
        state.participant_additions += 1;
        Ok(true)
    }

    async fn remove_participant(
        &self,
        _channel_id: Uuid,
        user_id: String,
    ) -> Result<(), Self::Err> {
        self.state
            .lock()
            .unwrap()
            .removed_participants
            .push(user_id);
        Ok(())
    }

    async fn touch_channel_updated_at(&self, channel_id: Uuid) -> Result<(), Self::Err> {
        let mut state = self.state.lock().unwrap();
        state.touched_channel_ids.push(channel_id);
        if state.fail_channel_touches {
            anyhow::bail!("channel touch failed");
        }
        Ok(())
    }

    async fn create_entity_mention(
        &self,
        _options: CreateEntityMentionOptions,
    ) -> Result<EntityMention, Self::Err> {
        anyhow::bail!("not implemented in test repo")
    }

    async fn get_entity_mention_by_id(
        &self,
        _id: Uuid,
    ) -> Result<Option<EntityMention>, Self::Err> {
        Ok(None)
    }

    async fn delete_entity_mention_by_id(
        &self,
        _id: Uuid,
    ) -> Result<Option<EntityMention>, Self::Err> {
        Ok(None)
    }

    async fn get_participants(
        &self,
        _channel_id: Uuid,
    ) -> Result<Vec<ChannelParticipant>, Self::Err> {
        Ok(self.state.lock().unwrap().participants.clone())
    }

    async fn get_thread_participants(
        &self,
        _thread_id: Uuid,
    ) -> Result<Vec<MacroUserIdStr<'static>>, Self::Err> {
        Ok(self.state.lock().unwrap().thread_participants.clone())
    }

    async fn upsert_activity(
        &self,
        _user_id: ChannelSender<'_>,
        _channel_id: Uuid,
    ) -> Result<(), Self::Err> {
        self.state.lock().unwrap().activity_upserts += 1;
        Ok(())
    }

    async fn get_activities(&self, _user_id: String) -> Result<Vec<Activity>, Self::Err> {
        Ok(Vec::new())
    }

    async fn set_activity(
        &self,
        user_id: String,
        channel_id: Uuid,
        _activity_type: ActivityType,
    ) -> Result<Activity, Self::Err> {
        self.state.lock().unwrap().activity_upserts += 1;
        Ok(Activity {
            id: Uuid::nil(),
            user_id,
            channel_id,
            created_at: Utc::now(),
            updated_at: Utc::now(),
            viewed_at: None,
            interacted_at: None,
        })
    }
}

#[derive(Clone, Default)]
struct FakeEvents {
    events: Arc<Mutex<Vec<ChannelEvent>>>,
}

impl ChannelEventDispatcher for FakeEvents {
    fn dispatch(&self, event: ChannelEvent) {
        self.events.lock().unwrap().push(event);
    }
}

#[derive(Clone, Default)]
struct FakeReferenceSharing {
    items: Arc<Mutex<Vec<ReferencedShareItem>>>,
}

impl ChannelReferenceSharePermissions for FakeReferenceSharing {
    type Err = anyhow::Error;

    async fn update_channel_share_permissions_for_referenced_items(
        &self,
        _actor: MacroUserIdStr<'static>,
        _channel_id: Uuid,
        items: Vec<ReferencedShareItem>,
    ) -> Result<Vec<crate::domain::models::ReferenceShareResult>, Self::Err> {
        let results = items
            .iter()
            .map(|item| crate::domain::models::ReferenceShareResult {
                entity_id: item.entity_id().to_string(),
                entity_type: item.entity_type(),
                outcome: crate::domain::models::ReferenceShareOutcome::Shared,
            })
            .collect();
        self.items.lock().unwrap().extend(items);
        Ok(results)
    }
}

fn mutation_service(
    repo: FakeMutationRepo,
    events: FakeEvents,
    share: FakeReferenceSharing,
) -> ChannelServiceImpl<FakeMutationRepo, FakeEvents, FakeReferenceSharing> {
    ChannelServiceImpl::with_dependencies(repo, events, share)
}

#[tokio::test]
async fn share_referenced_items_with_channel_forwards_to_the_share_port() {
    let share = FakeReferenceSharing::default();
    let seen = share.items.clone();
    let service = mutation_service(
        FakeMutationRepo::new(Uuid::now_v7(), "macro|[email protected]"),
        FakeEvents::default(),
        share,
    );
    let channel_id = Uuid::now_v7();
    let item = ReferencedShareItem::new(
        Uuid::now_v7().to_string(),
        crate::domain::models::ReferencedShareItemType::CalendarEvent,
    );
    let results = service
        .share_referenced_items_with_channel(
            macro_id("macro|[email protected]"),
            channel_id,
            vec![item.clone()],
        )
        .await
        .expect("share port accepts the item");

    assert_eq!(results.len(), 1);
    assert_eq!(results[0].entity_id, item.entity_id());
    assert_eq!(
        results[0].outcome,
        crate::domain::models::ReferenceShareOutcome::Shared
    );
    assert_eq!(seen.lock().unwrap().as_slice(), &[item]);
}

fn macro_id(user_id: &str) -> MacroUserIdStr<'static> {
    MacroUserIdStr::try_from(user_id.to_string()).unwrap()
}

fn sender(user_id: &str) -> Sender {
    Sender::new_from_user(macro_id(user_id))
}

fn patch_receipt(
    user_id: &str,
    channel_id: Uuid,
    role: AccessRole,
) -> EntityAccessReceipt<MemberParticipantRole> {
    use entity_access::domain::models::{Entity, EntityPermission};
    EntityAccessReceipt::<MemberParticipantRole>::try_new_authenticated_user(
        macro_id(user_id),
        Entity {
            entity_id: channel_id.to_string(),
            entity_type: EntityType::Channel,
        },
        EntityPermission::ChannelRole { role },
    )
    .unwrap()
}

#[tokio::test]
async fn returns_empty_attachments_page() {
    let svc = ChannelServiceImpl::new(empty_repo());
    let page = svc
        .get_channel_attachments(Uuid::nil(), Query::Sort(CreatedAt, ()), 50, None)
        .await
        .unwrap();

    assert!(page.items.is_empty());
    assert!(page.next_cursor.is_none());
}

#[tokio::test]
async fn returns_empty_participants_list() {
    let svc = ChannelServiceImpl::new(empty_repo());
    let participants = svc.get_channel_participants(Uuid::nil()).await.unwrap();

    assert!(participants.is_empty());
}

/// Grants parent access to an explicit list of `(parent type, parent id)` pairs.
struct FakeCommentParentAccess(Vec<(&'static str, &'static str)>);

#[async_trait::async_trait]
impl CommentParentAccess for FakeCommentParentAccess {
    async fn can_view_parent(
        &self,
        _viewer: &str,
        parent_entity_type: &str,
        parent_entity_id: &str,
    ) -> anyhow::Result<bool> {
        Ok(self.0.contains(&(parent_entity_type, parent_entity_id)))
    }
}

fn comment_reference(
    parent_entity_type: &str,
    parent_entity_id: &str,
) -> AttachmentEntityReference {
    AttachmentEntityReference::Comment(crate::domain::models::AttachmentCommentReference {
        parent_entity_type: parent_entity_type.to_string(),
        parent_entity_id: parent_entity_id.to_string(),
        message_id: Uuid::nil(),
        thread_id: None,
        sender_id: "macro|[email protected]".to_string(),
        message_content: "see the doc".to_string(),
        message_created_at: Utc::now(),
        attachment_created_at: Utc::now(),
    })
}

fn references_repo() -> MockChannelRepo {
    let mut repo = empty_repo();
    repo.expect_get_attachment_references()
        .returning(|_, _, _| {
            Box::pin(async {
                Ok(vec![
                    AttachmentEntityReference::Channel(
                        crate::domain::models::AttachmentChannelReference {
                            channel_id: Uuid::nil(),
                            channel_name: None,
                            message_id: Uuid::nil(),
                            thread_id: None,
                            sender_id: "macro|[email protected]".to_string(),
                            message_content: "in a channel".to_string(),
                            message_created_at: Utc::now(),
                            attachment_created_at: Utc::now(),
                        },
                    ),
                    comment_reference("initiative", "init-1"),
                    comment_reference("document", "doc-1"),
                    AttachmentEntityReference::Generic(
                        crate::domain::models::AttachmentGenericReference {
                            source_entity_type: "document".to_string(),
                            source_entity_id: "src-doc".to_string(),
                            entity_type: "document".to_string(),
                            entity_id: "target".to_string(),
                            user_id: None,
                            created_at: Utc::now(),
                        },
                    ),
                ])
            })
        });
    repo
}

#[tokio::test]
async fn attachment_references_keep_comments_only_on_viewable_parents() {
    let svc = ChannelServiceImpl::new(references_repo()).with_comment_parent_access(Arc::new(
        FakeCommentParentAccess(vec![("initiative", "init-1")]),
    ));
    let refs = svc
        .get_attachment_references(
            "document".to_string(),
            "target".to_string(),
            "macro|[email protected]".to_string(),
        )
        .await
        .unwrap();

    assert_eq!(refs.len(), 3);
    assert!(matches!(refs[0], AttachmentEntityReference::Channel(_)));
    assert!(matches!(
        &refs[1],
        AttachmentEntityReference::Comment(comment) if comment.parent_entity_id == "init-1"
    ));
    assert!(matches!(refs[2], AttachmentEntityReference::Generic(_)));
}

#[tokio::test]
async fn attachment_references_withhold_comments_without_parent_access() {
    let svc = ChannelServiceImpl::new(references_repo());
    let refs = svc
        .get_attachment_references(
            "document".to_string(),
            "target".to_string(),
            "macro|[email protected]".to_string(),
        )
        .await
        .unwrap();

    assert_eq!(refs.len(), 2);
    assert!(
        refs.iter()
            .all(|r| !matches!(r, AttachmentEntityReference::Comment(_)))
    );
}

// --- center_window tests ---

// --- get_channel_messages_around tests ---

#[tokio::test]
async fn add_participants_touches_channel_once_when_any_membership_changes() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let svc = mutation_service(
        repo.clone(),
        FakeEvents::default(),
        FakeReferenceSharing::default(),
    );

    svc.add_participants(
        sender("macro|[email protected]"),
        channel_id,
        AddParticipantsRequest {
            participants: HashSet::from([
                macro_id("macro|[email protected]"),
                macro_id("macro|[email protected]"),
            ]),
        },
    )
    .await
    .unwrap();

    let state = repo.state.lock().unwrap();
    assert_eq!(state.participant_additions, 1);
    assert_eq!(state.touched_channel_ids, vec![channel_id]);
}

#[tokio::test]
async fn add_participants_does_not_touch_channel_when_memberships_are_already_active() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let svc = mutation_service(
        repo.clone(),
        FakeEvents::default(),
        FakeReferenceSharing::default(),
    );

    svc.add_participants(
        sender("macro|[email protected]"),
        channel_id,
        AddParticipantsRequest {
            participants: HashSet::from([
                macro_id("macro|[email protected]"),
                macro_id("macro|[email protected]"),
            ]),
        },
    )
    .await
    .unwrap();

    let state = repo.state.lock().unwrap();
    assert_eq!(state.participant_additions, 0);
    assert!(state.touched_channel_ids.is_empty());
}

#[tokio::test]
async fn add_participants_propagates_channel_touch_errors() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    repo.state.lock().unwrap().fail_channel_touches = true;
    let svc = mutation_service(
        repo.clone(),
        FakeEvents::default(),
        FakeReferenceSharing::default(),
    );

    let error = svc
        .add_participants(
            sender("macro|[email protected]"),
            channel_id,
            AddParticipantsRequest {
                participants: HashSet::from([macro_id("macro|[email protected]")]),
            },
        )
        .await
        .unwrap_err();

    assert!(matches!(error, ChannelMutationErr::Repo(_)));
    assert_eq!(
        repo.state.lock().unwrap().touched_channel_ids,
        vec![channel_id]
    );
}

#[tokio::test]
async fn remove_participants_rejects_removing_channel_owner() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let svc = mutation_service(
        repo.clone(),
        FakeEvents::default(),
        FakeReferenceSharing::default(),
    );

    let err = svc
        .remove_participants(
            sender("macro|[email protected]"),
            channel_id,
            RemoveParticipantsRequest {
                participants: vec![
                    "macro|[email protected]".to_string(),
                    "macro|[email protected]".to_string(),
                ],
            },
        )
        .await
        .unwrap_err();

    assert!(matches!(err, ChannelMutationErr::Unauthorized(_)));
    assert!(repo.state.lock().unwrap().removed_participants.is_empty());
}

#[tokio::test]
async fn remove_participants_allows_removing_non_owner() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let svc = mutation_service(
        repo.clone(),
        FakeEvents::default(),
        FakeReferenceSharing::default(),
    );

    svc.remove_participants(
        sender("macro|[email protected]"),
        channel_id,
        RemoveParticipantsRequest {
            participants: vec!["macro|[email protected]".to_string()],
        },
    )
    .await
    .unwrap();

    let state = repo.state.lock().unwrap();
    assert_eq!(
        state.removed_participants,
        vec!["macro|[email protected]".to_string()]
    );
    assert!(state.touched_channel_ids.is_empty());
}

#[tokio::test]
async fn create_system_channel_event_uses_system_actor() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let events = FakeEvents::default();
    let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());

    svc.create_system_channel(
        macro_id("macro|[email protected]"),
        crate::domain::models::CreateChannelRequest {
            name: Some("Macro Support x owner".to_string()),
            channel_type: ChannelType::Private,
            team_id: None,
            auto_join_team: false,
            participants: HashSet::from([macro_id("macro|[email protected]")]),
        },
    )
    .await
    .unwrap();

    let events = events.events.lock().unwrap();
    assert!(matches!(
        events.as_slice(),
        [ChannelEvent::ChannelCreated {
            actor,
            on_behalf_of: Some(owner),
            channel_name: Some(name),
            ..
        }] if actor == &Sender::new_from_bot(bot_id::MACRO_SYSTEM_BOT_ID)
            && owner.as_ref() == "macro|[email protected]"
            && name == "Macro Support x owner"
    ));
}

#[tokio::test]
async fn create_channel_on_behalf_attributes_created_to_the_bot() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let events = FakeEvents::default();
    let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());

    svc.create_channel_on_behalf(
        macro_id("macro|[email protected]"),
        bot_id::MACRO_AI_BOT_ID,
        crate::domain::models::CreateChannelRequest {
            name: Some("Planning".to_string()),
            channel_type: ChannelType::Private,
            team_id: None,
            auto_join_team: false,
            participants: HashSet::from([macro_id("macro|[email protected]")]),
        },
    )
    .await
    .unwrap();

    let events = events.events.lock().unwrap();
    assert!(matches!(
        events.as_slice(),
        [ChannelEvent::ChannelCreated {
            actor,
            on_behalf_of: Some(owner),
            channel_name: Some(name),
            ..
        }] if actor == &Sender::new_from_bot(bot_id::MACRO_AI_BOT_ID)
            && owner.as_ref() == "macro|[email protected]"
            && name == "Planning"
    ));
}

/// Signup on main called `create_channel(Sender::new_from_user(owner))`.
/// That is the path that made "Created # Macro Support x …" render as You.
#[tokio::test]
async fn signup_support_channel_via_user_create_channel_attributes_created_to_owner() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let events = FakeEvents::default();
    let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());

    svc.create_channel(
        sender("macro|[email protected]"),
        None,
        crate::domain::models::CreateChannelRequest {
            name: Some("Macro Support x owner".to_string()),
            channel_type: ChannelType::Private,
            team_id: None,
            auto_join_team: false,
            participants: HashSet::from([macro_id("macro|[email protected]")]),
        },
    )
    .await
    .unwrap();

    let events = events.events.lock().unwrap();
    assert!(matches!(
        events.as_slice(),
        [ChannelEvent::ChannelCreated { actor, channel_name: Some(name), .. }]
            if actor == &sender("macro|[email protected]")
                && name == "Macro Support x owner"
    ));
}

#[tokio::test]
async fn create_channel_event_carries_channel_name() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let events = FakeEvents::default();
    let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());

    svc.create_channel(
        sender("macro|[email protected]"),
        None,
        crate::domain::models::CreateChannelRequest {
            name: Some("general".to_string()),
            channel_type: ChannelType::Private,
            team_id: None,
            auto_join_team: false,
            participants: HashSet::from([macro_id("macro|[email protected]")]),
        },
    )
    .await
    .unwrap();

    let events = events.events.lock().unwrap();
    assert!(matches!(
        events.as_slice(),
        [ChannelEvent::ChannelCreated { channel_name: Some(name), .. }] if name == "general"
    ));
}

#[tokio::test]
async fn ensure_dms_dispatches_created_channel_once() {
    let channel_id = Uuid::new_v4();
    let joiner = macro_id("macro|[email protected]");
    let teammate = macro_id("macro|[email protected]");
    let repo = FakeMutationRepo::new(channel_id, joiner.as_ref());
    let events = FakeEvents::default();
    let service = mutation_service(repo, events.clone(), FakeReferenceSharing::default());

    let summary = service
        .ensure_dms(ensure_dms_for_joining_member(
            joiner.clone(),
            vec![joiner.clone(), teammate.clone()],
        ))
        .await
        .unwrap();

    assert_eq!(
        summary,
        EnsureDmsSummary {
            created: 1,
            existing: 0,
            failed: 0,
        }
    );
    let events = events.events.lock().unwrap();
    assert!(matches!(
        events.as_slice(),
        [ChannelEvent::ChannelCreated {
            channel_id: actual_channel_id,
            actor,
            on_behalf_of: None,
            channel_type: ChannelType::DirectMessage,
            channel_name: None,
            participant_user_ids,
        }] if actual_channel_id == &channel_id
            && actor.as_user() == Some(&joiner)
            && participant_user_ids.contains(&joiner)
            && participant_user_ids.contains(&teammate)
    ));
}

#[tokio::test]
async fn ensure_dms_does_not_dispatch_for_existing_channel() {
    let channel_id = Uuid::new_v4();
    let joiner = macro_id("macro|[email protected]");
    let teammate = macro_id("macro|[email protected]");
    let mut repo = MockChannelRepo::new();
    repo.expect_ensure_dm().once().returning(move |_, _| {
        Box::pin(async move {
            Ok(crate::domain::historical::EnsuredChannel {
                id: channel_id,
                created: false,
            })
        })
    });
    let events = FakeEvents::default();
    let service = ChannelServiceImpl::with_dependencies(
        repo,
        events.clone(),
        FakeReferenceSharing::default(),
    );

    let summary = service
        .ensure_dms(ensure_dms_for_joining_member(joiner, vec![teammate]))
        .await
        .unwrap();

    assert_eq!(
        summary,
        EnsureDmsSummary {
            created: 0,
            existing: 1,
            failed: 0,
        }
    );
    assert!(events.events.lock().unwrap().is_empty());
}

#[tokio::test]
async fn get_or_create_dm_rejects_self_pair() {
    let user = macro_id("macro|[email protected]");
    let repo = FakeMutationRepo::new(Uuid::new_v4(), user.as_ref());
    let service = mutation_service(repo, FakeEvents::default(), FakeReferenceSharing::default());

    let error = service
        .get_or_create_dm(
            Sender::new_from_user(user.clone()),
            GetOrCreateDmRequest {
                recipient_id: Sender::new_from_user(user),
            },
        )
        .await
        .unwrap_err();

    assert!(matches!(
        error,
        ChannelMutationErr::BadRequest(message)
            if message == "recipient_id cannot be the same as the user_id"
    ));
}

#[tokio::test]
async fn get_or_create_dm_returns_get_for_existing_pair() {
    let channel_id = Uuid::new_v4();
    let actor = macro_id("macro|[email protected]");
    let recipient = macro_id("macro|[email protected]");
    let mut repo = MockChannelRepo::new();
    repo.expect_ensure_dm().once().returning(move |_, _| {
        Box::pin(async move {
            Ok(crate::domain::historical::EnsuredChannel {
                id: channel_id,
                created: false,
            })
        })
    });
    let events = FakeEvents::default();
    let service = ChannelServiceImpl::with_dependencies(
        repo,
        events.clone(),
        FakeReferenceSharing::default(),
    );

    let response = service
        .get_or_create_dm(
            Sender::new_from_user(actor),
            GetOrCreateDmRequest {
                recipient_id: Sender::new_from_user(recipient),
            },
        )
        .await
        .unwrap();

    assert_eq!(response.channel_id, channel_id.to_string());
    assert_eq!(response.action, GetOrCreateAction::Get);
    assert!(events.events.lock().unwrap().is_empty());
}

#[tokio::test]
async fn create_private_channel_allows_no_invited_participants() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let events = FakeEvents::default();
    let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());

    svc.create_channel(
        sender("macro|[email protected]"),
        None,
        crate::domain::models::CreateChannelRequest {
            name: Some("private notes".to_string()),
            channel_type: ChannelType::Private,
            team_id: None,
            auto_join_team: false,
            participants: HashSet::new(),
        },
    )
    .await
    .unwrap();

    let events = events.events.lock().unwrap();
    assert!(matches!(
        events.as_slice(),
        [ChannelEvent::ChannelCreated { participant_user_ids, .. }]
            if participant_user_ids == &[macro_id("macro|[email protected]")]
    ));
}

#[tokio::test]
async fn create_auto_join_team_channel_event_includes_current_team_members() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let events = FakeEvents::default();
    let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());

    svc.create_channel(
        sender("macro|[email protected]"),
        None,
        CreateChannelRequest {
            name: Some("general".to_string()),
            channel_type: ChannelType::Team,
            team_id: Some(Uuid::new_v4()),
            auto_join_team: true,
            participants: HashSet::from([macro_id("macro|[email protected]")]),
        },
    )
    .await
    .unwrap();

    let events = events.events.lock().unwrap();
    assert!(matches!(
        events.as_slice(),
        [ChannelEvent::ChannelCreated { participant_user_ids, .. }]
            if participant_user_ids.len() == 2
                && participant_user_ids.contains(&macro_id("macro|[email protected]"))
                && participant_user_ids.contains(&macro_id("macro|[email protected]"))
    ));
}

#[test]
fn create_channel_request_defaults_auto_join_team_to_false() {
    let request: CreateChannelRequest = serde_json::from_value(serde_json::json!({
        "name": "general",
        "channel_type": "team",
        "team_id": Uuid::new_v4(),
        "participants": []
    }))
    .unwrap();

    assert!(!request.auto_join_team);
}

#[tokio::test]
async fn create_channel_rejects_auto_join_for_non_team_channel() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let svc = mutation_service(repo, FakeEvents::default(), FakeReferenceSharing::default());

    let err = svc
        .create_channel(
            sender("macro|[email protected]"),
            None,
            CreateChannelRequest {
                name: Some("private notes".to_string()),
                channel_type: ChannelType::Private,
                team_id: None,
                auto_join_team: true,
                participants: HashSet::new(),
            },
        )
        .await
        .unwrap_err();

    assert!(matches!(err, ChannelMutationErr::BadRequest(_)));
}

#[tokio::test]
async fn auto_join_by_team_id_does_not_touch_channel_recency() {
    let channel_id = Uuid::new_v4();
    let team_id = Uuid::new_v4();
    let user_id = macro_id("macro|[email protected]");
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let svc = mutation_service(
        repo.clone(),
        FakeEvents::default(),
        FakeReferenceSharing::default(),
    );

    svc.auto_join_by_team_id(&team_id, &user_id).await.unwrap();

    assert!(repo.state.lock().unwrap().touched_channel_ids.is_empty());
}

#[tokio::test]
async fn team_membership_operations_delegate_to_repo() {
    let team_id = Uuid::new_v4();
    let channel_ids = vec![Uuid::new_v4(), Uuid::new_v4()];
    let user_id = macro_id("macro|[email protected]");
    let mut repo = MockChannelRepo::new();
    repo.expect_auto_join_by_team_id()
        .withf({
            let user_id = user_id.clone();
            move |actual_team_id, actual_user_id| {
                actual_team_id == &team_id && actual_user_id == &user_id
            }
        })
        .once()
        .returning(|_, _| Box::pin(async { Ok(()) }));
    repo.expect_leave_by_team_id()
        .withf({
            let user_id = user_id.clone();
            move |actual_team_id, actual_user_id| {
                actual_team_id == &team_id && actual_user_id == &user_id
            }
        })
        .once()
        .returning({
            let channel_ids = channel_ids.clone();
            move |_, _| {
                let channel_ids = channel_ids.clone();
                Box::pin(async move { Ok(channel_ids) })
            }
        });
    repo.expect_restore_by_channel_ids()
        .withf({
            let user_id = user_id.clone();
            let channel_ids = channel_ids.clone();
            move |actual_user_id, actual_channel_ids| {
                actual_user_id == &user_id && actual_channel_ids == channel_ids
            }
        })
        .once()
        .returning(|_, _| Box::pin(async { Ok(()) }));
    let svc = ChannelServiceImpl::new(repo);

    svc.auto_join_by_team_id(&team_id, &user_id).await.unwrap();
    assert_eq!(
        svc.leave_by_team_id(&team_id, &user_id).await.unwrap(),
        channel_ids
    );
    svc.restore_by_channel_ids(&user_id, &channel_ids)
        .await
        .unwrap();
}

#[tokio::test]
async fn team_membership_operations_delegate_repo_errors() {
    let team_id = Uuid::new_v4();
    let user_id = macro_id("macro|[email protected]");
    let mut repo = MockChannelRepo::new();
    repo.expect_auto_join_by_team_id()
        .once()
        .returning(|_, _| Box::pin(async { Err(anyhow::anyhow!("join failed")) }));
    repo.expect_leave_by_team_id()
        .once()
        .returning(|_, _| Box::pin(async { Err(anyhow::anyhow!("leave failed")) }));
    repo.expect_restore_by_channel_ids()
        .once()
        .returning(|_, _| Box::pin(async { Err(anyhow::anyhow!("restore failed")) }));
    let svc = ChannelServiceImpl::new(repo);

    let join_err = svc
        .auto_join_by_team_id(&team_id, &user_id)
        .await
        .unwrap_err();
    let leave_err = svc.leave_by_team_id(&team_id, &user_id).await.unwrap_err();
    let restore_err = svc
        .restore_by_channel_ids(&user_id, &[Uuid::new_v4()])
        .await
        .unwrap_err();

    assert!(matches!(join_err, ChannelMutationErr::Repo(_)));
    assert!(matches!(leave_err, ChannelMutationErr::Repo(_)));
    assert!(matches!(restore_err, ChannelMutationErr::Repo(_)));
}

#[tokio::test]
async fn patch_channel_dispatches_channel_updated() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let events = FakeEvents::default();
    let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());

    svc.patch_channel(
        patch_receipt("macro|[email protected]", channel_id, AccessRole::Member),
        PatchChannelRequest {
            channel_name: Some("Renamed".to_string()),
            convert_to_team_channel: None,
            auto_join_team: None,
        },
    )
    .await
    .unwrap();

    let events = events.events.lock().unwrap();
    assert!(matches!(
        events.as_slice(),
        [ChannelEvent::ChannelUpdated { previous_name: Some(previous), channel_name: Some(new), actor, .. }]
            if previous == "Project" && new == "Renamed" && actor == &macro_id("macro|[email protected]")
    ));
}

#[tokio::test]
async fn noop_patch_channel_dispatches_nothing() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let events = FakeEvents::default();
    let svc = mutation_service(
        repo.clone(),
        events.clone(),
        FakeReferenceSharing::default(),
    );

    svc.patch_channel(
        patch_receipt("macro|[email protected]", channel_id, AccessRole::Member),
        PatchChannelRequest {
            channel_name: None,
            convert_to_team_channel: None,
            auto_join_team: None,
        },
    )
    .await
    .unwrap();

    assert!(events.events.lock().unwrap().is_empty());
    let state = repo.state.lock().unwrap();
    assert!(state.channel_patches.is_empty());
    assert!(state.touched_channel_ids.is_empty());
}

#[tokio::test]
async fn patch_channel_conversion_uses_the_users_team() {
    let channel_id = Uuid::new_v4();
    let team_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    repo.state.lock().unwrap().user_team_id = Some(team_id);
    let svc = mutation_service(
        repo.clone(),
        FakeEvents::default(),
        FakeReferenceSharing::default(),
    );

    svc.patch_channel(
        patch_receipt("macro|[email protected]", channel_id, AccessRole::Admin),
        PatchChannelRequest {
            channel_name: None,
            convert_to_team_channel: Some(true),
            auto_join_team: None,
        },
    )
    .await
    .unwrap();

    let state = repo.state.lock().unwrap();
    assert_eq!(state.user_team_id_lookups, 1);
    assert_eq!(state.channel_patches.len(), 1);
    assert_eq!(state.channel_patches[0].1, Some(team_id));
    assert_eq!(
        state.channel_patches[0].0.convert_to_team_channel,
        Some(true)
    );
}

#[tokio::test]
async fn patch_channel_conversion_names_an_unnamed_private_channel() {
    let channel_id = Uuid::new_v4();
    let team_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    {
        let mut state = repo.state.lock().unwrap();
        state.channel_name = None;
        state.user_team_id = Some(team_id);
    }
    let svc = mutation_service(
        repo.clone(),
        FakeEvents::default(),
        FakeReferenceSharing::default(),
    );

    svc.patch_channel(
        patch_receipt("macro|[email protected]", channel_id, AccessRole::Admin),
        PatchChannelRequest {
            channel_name: None,
            convert_to_team_channel: Some(true),
            auto_join_team: None,
        },
    )
    .await
    .unwrap();

    let state = repo.state.lock().unwrap();
    assert_eq!(state.channel_patches.len(), 1);
    assert_eq!(
        state.channel_patches[0].0.channel_name.as_deref(),
        Some("Project")
    );
}

#[tokio::test]
async fn patch_team_channel_conversion_to_private_clears_team_settings() {
    let channel_id = Uuid::new_v4();
    let team_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    {
        let mut state = repo.state.lock().unwrap();
        state.channel_type = ChannelType::Team;
        state.channel_team_id = Some(team_id);
    }
    let svc = mutation_service(
        repo.clone(),
        FakeEvents::default(),
        FakeReferenceSharing::default(),
    );

    svc.patch_channel(
        patch_receipt("macro|[email protected]", channel_id, AccessRole::Admin),
        PatchChannelRequest {
            channel_name: None,
            convert_to_team_channel: Some(false),
            auto_join_team: Some(true),
        },
    )
    .await
    .unwrap();

    let state = repo.state.lock().unwrap();
    assert_eq!(state.user_team_id_lookups, 0);
    assert_eq!(state.channel_patches.len(), 1);
    assert_eq!(state.channel_patches[0].1, None);
    assert_eq!(
        state.channel_patches[0].0.convert_to_team_channel,
        Some(false)
    );
    assert_eq!(state.channel_patches[0].0.auto_join_team, Some(false));
}

#[tokio::test]
async fn patch_channel_refuses_to_make_an_agent_dm_a_team_channel() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    {
        let mut state = repo.state.lock().unwrap();
        state.channel_type = ChannelType::DirectMessage;
        state.direct_agent = true;
        state.user_team_id = Some(Uuid::new_v4());
    }
    let svc = mutation_service(
        repo.clone(),
        FakeEvents::default(),
        FakeReferenceSharing::default(),
    );

    let err = svc
        .patch_channel(
            patch_receipt("macro|[email protected]", channel_id, AccessRole::Owner),
            PatchChannelRequest {
                channel_name: None,
                convert_to_team_channel: Some(true),
                auto_join_team: None,
            },
        )
        .await
        .unwrap_err();

    assert!(matches!(err, ChannelMutationErr::BadRequest(_)));
    let state = repo.state.lock().unwrap();
    assert_eq!(state.user_team_id_lookups, 0);
    assert!(state.channel_patches.is_empty());
}

#[tokio::test]
async fn patch_channel_conversion_requires_the_user_to_have_a_team() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let svc = mutation_service(
        repo.clone(),
        FakeEvents::default(),
        FakeReferenceSharing::default(),
    );

    let err = svc
        .patch_channel(
            patch_receipt("macro|[email protected]", channel_id, AccessRole::Admin),
            PatchChannelRequest {
                channel_name: None,
                convert_to_team_channel: Some(true),
                auto_join_team: None,
            },
        )
        .await
        .unwrap_err();

    assert!(matches!(err, ChannelMutationErr::BadRequest(_)));
    let state = repo.state.lock().unwrap();
    assert_eq!(state.user_team_id_lookups, 1);
    assert!(state.channel_patches.is_empty());
}

#[tokio::test]
async fn patch_channel_rejects_enabling_auto_join_on_a_non_team_channel() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let svc = mutation_service(
        repo.clone(),
        FakeEvents::default(),
        FakeReferenceSharing::default(),
    );

    let err = svc
        .patch_channel(
            patch_receipt("macro|[email protected]", channel_id, AccessRole::Admin),
            PatchChannelRequest {
                channel_name: None,
                convert_to_team_channel: None,
                auto_join_team: Some(true),
            },
        )
        .await
        .unwrap_err();

    assert!(matches!(err, ChannelMutationErr::BadRequest(_)));
    let state = repo.state.lock().unwrap();
    assert_eq!(state.user_team_id_lookups, 0);
    assert!(state.channel_patches.is_empty());
}

#[tokio::test]
async fn patch_team_channel_auto_join_uses_its_existing_team() {
    let channel_id = Uuid::new_v4();
    let team_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    {
        let mut state = repo.state.lock().unwrap();
        state.channel_type = ChannelType::Team;
        state.channel_team_id = Some(team_id);
    }
    let svc = mutation_service(
        repo.clone(),
        FakeEvents::default(),
        FakeReferenceSharing::default(),
    );

    svc.patch_channel(
        patch_receipt("macro|[email protected]", channel_id, AccessRole::Admin),
        PatchChannelRequest {
            channel_name: None,
            convert_to_team_channel: None,
            auto_join_team: Some(true),
        },
    )
    .await
    .unwrap();

    let state = repo.state.lock().unwrap();
    assert_eq!(state.user_team_id_lookups, 0);
    assert_eq!(state.channel_patches.len(), 1);
    assert_eq!(state.channel_patches[0].1, Some(team_id));
    assert_eq!(state.channel_patches[0].0.auto_join_team, Some(true));
}

#[tokio::test]
async fn patch_channel_allows_disabling_auto_join_without_a_team() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let svc = mutation_service(
        repo.clone(),
        FakeEvents::default(),
        FakeReferenceSharing::default(),
    );

    svc.patch_channel(
        patch_receipt("macro|[email protected]", channel_id, AccessRole::Admin),
        PatchChannelRequest {
            channel_name: None,
            convert_to_team_channel: None,
            auto_join_team: Some(false),
        },
    )
    .await
    .unwrap();

    let state = repo.state.lock().unwrap();
    assert_eq!(state.user_team_id_lookups, 0);
    assert_eq!(state.channel_patches.len(), 1);
    assert_eq!(state.channel_patches[0].1, None);
    assert_eq!(state.channel_patches[0].0.auto_join_team, Some(false));
}

#[tokio::test]
async fn patch_channel_member_can_rename() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let events = FakeEvents::default();
    let svc = mutation_service(
        repo.clone(),
        events.clone(),
        FakeReferenceSharing::default(),
    );

    svc.patch_channel(
        patch_receipt("macro|[email protected]", channel_id, AccessRole::Member),
        PatchChannelRequest {
            channel_name: Some("Member Name".to_string()),
            convert_to_team_channel: None,
            auto_join_team: None,
        },
    )
    .await
    .unwrap();

    let state = repo.state.lock().unwrap();
    assert_eq!(state.channel_patches.len(), 1);
    assert_eq!(
        state.channel_patches[0].0.channel_name.as_deref(),
        Some("Member Name")
    );
    assert_eq!(state.channel_patches[0].0.convert_to_team_channel, None);
    assert_eq!(state.channel_patches[0].0.auto_join_team, None);
    let events = events.events.lock().unwrap();
    assert!(matches!(
        events.as_slice(),
        [ChannelEvent::ChannelUpdated { channel_name: Some(name), .. }]
            if name == "Member Name"
    ));
}

#[tokio::test]
async fn patch_channel_member_cannot_convert_or_change_auto_join() {
    let requests = [
        PatchChannelRequest {
            channel_name: None,
            convert_to_team_channel: Some(true),
            auto_join_team: None,
        },
        PatchChannelRequest {
            channel_name: Some("Still Member".to_string()),
            convert_to_team_channel: None,
            auto_join_team: Some(false),
        },
    ];
    for req in requests {
        let channel_id = Uuid::new_v4();
        let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
        repo.state.lock().unwrap().user_team_id = Some(Uuid::new_v4());
        let events = FakeEvents::default();
        let svc = mutation_service(
            repo.clone(),
            events.clone(),
            FakeReferenceSharing::default(),
        );

        let err = svc
            .patch_channel(
                patch_receipt("macro|[email protected]", channel_id, AccessRole::Member),
                req,
            )
            .await
            .unwrap_err();

        assert!(matches!(
            err,
            ChannelMutationErr::Forbidden(message)
                if message == "converting a channel or changing auto-join requires channel admin access"
        ));
        let state = repo.state.lock().unwrap();
        assert!(state.channel_patches.is_empty());
        assert_eq!(state.user_team_id_lookups, 0);
        assert!(events.events.lock().unwrap().is_empty());
    }
}

#[tokio::test]
async fn remove_participants_dispatches_participants_removed() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let events = FakeEvents::default();
    let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());

    svc.remove_participants(
        sender("macro|[email protected]"),
        channel_id,
        RemoveParticipantsRequest {
            participants: vec!["macro|[email protected]".to_string()],
        },
    )
    .await
    .unwrap();

    let events = events.events.lock().unwrap();
    assert!(matches!(
        events.as_slice(),
        [ChannelEvent::ParticipantsRemoved { actor, removed_user_ids, .. }]
            if actor == &macro_id("macro|[email protected]")
                && removed_user_ids == &vec![macro_id("macro|[email protected]")]
    ));
}

#[tokio::test]
async fn leave_channel_dispatches_participants_removed_for_self() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    // A private channel needs more than 2 participants to allow leaving.
    repo.state
        .lock()
        .unwrap()
        .participants
        .push(ChannelParticipant {
            channel_id,
            user_id: "macro|[email protected]".to_string(),
            role: ParticipantRole::Member,
            joined_at: Utc::now(),
            left_at: None,
        });
    let events = FakeEvents::default();
    let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());

    svc.leave_channel(sender("macro|[email protected]"), channel_id)
        .await
        .unwrap();

    let events = events.events.lock().unwrap();
    assert!(matches!(
        events.as_slice(),
        [ChannelEvent::ParticipantsRemoved { actor, removed_user_ids, .. }]
            if actor == &macro_id("macro|[email protected]")
                && removed_user_ids == &vec![macro_id("macro|[email protected]")]
    ));
}

#[tokio::test]
async fn join_channel_touches_channel_when_membership_changes() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let events = FakeEvents::default();
    let svc = mutation_service(
        repo.clone(),
        events.clone(),
        FakeReferenceSharing::default(),
    );

    svc.join_channel(sender("macro|[email protected]"), channel_id)
        .await
        .unwrap();

    assert_eq!(
        repo.state.lock().unwrap().touched_channel_ids,
        vec![channel_id]
    );
    assert!(matches!(
        events.events.lock().unwrap().as_slice(),
        [ChannelEvent::ParticipantJoined { channel_id: event_channel_id, .. }]
            if event_channel_id == &channel_id
    ));
}

#[tokio::test]
async fn join_channel_propagates_channel_touch_errors() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    repo.state.lock().unwrap().fail_channel_touches = true;
    let events = FakeEvents::default();
    let svc = mutation_service(
        repo.clone(),
        events.clone(),
        FakeReferenceSharing::default(),
    );

    let error = svc
        .join_channel(sender("macro|[email protected]"), channel_id)
        .await
        .unwrap_err();

    assert!(matches!(error, ChannelMutationErr::Repo(_)));
    assert_eq!(
        repo.state.lock().unwrap().touched_channel_ids,
        vec![channel_id]
    );
    assert!(events.events.lock().unwrap().is_empty());
}

#[tokio::test]
async fn private_channel_join_code_is_reused() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let svc = mutation_service(repo, FakeEvents::default(), FakeReferenceSharing::default());

    let first = svc.get_channel_join_code(channel_id).await.unwrap();
    let second = svc.get_channel_join_code(channel_id).await.unwrap();

    assert_eq!(first, second);
}

#[tokio::test]
async fn join_code_generation_is_forbidden_for_non_private_channels() {
    for channel_type in [
        ChannelType::Public,
        ChannelType::DirectMessage,
        ChannelType::Team,
    ] {
        let channel_id = Uuid::new_v4();
        let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
        repo.state.lock().unwrap().channel_type = channel_type;
        let svc = mutation_service(
            repo.clone(),
            FakeEvents::default(),
            FakeReferenceSharing::default(),
        );

        let error = svc.get_channel_join_code(channel_id).await.unwrap_err();

        assert!(matches!(error, ChannelMutationErr::Forbidden(_)));
        assert!(repo.state.lock().unwrap().join_code.is_none());
    }
}

#[tokio::test]
async fn unknown_join_code_returns_not_found() {
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let svc = mutation_service(repo, FakeEvents::default(), FakeReferenceSharing::default());

    let error = svc
        .join_channel_by_code(sender("macro|[email protected]"), Uuid::new_v4())
        .await
        .unwrap_err();

    assert!(matches!(error, ChannelMutationErr::NotFound(_)));
}

#[tokio::test]
async fn join_by_code_rejects_non_private_channel() {
    let channel_id = Uuid::new_v4();
    let join_code = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    {
        let mut state = repo.state.lock().unwrap();
        state.join_code = Some(join_code);
        state.channel_type = ChannelType::Public;
    }
    let svc = mutation_service(repo, FakeEvents::default(), FakeReferenceSharing::default());

    let error = svc
        .join_channel_by_code(sender("macro|[email protected]"), join_code)
        .await
        .unwrap_err();

    assert!(matches!(error, ChannelMutationErr::Forbidden(_)));
}

#[tokio::test]
async fn join_by_code_adds_participant_and_dispatches_event() {
    let channel_id = Uuid::new_v4();
    let join_code = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    repo.state.lock().unwrap().join_code = Some(join_code);
    let events = FakeEvents::default();
    let svc = mutation_service(
        repo.clone(),
        events.clone(),
        FakeReferenceSharing::default(),
    );

    svc.join_channel_by_code(sender("macro|[email protected]"), join_code)
        .await
        .unwrap();

    let state = repo.state.lock().unwrap();
    assert_eq!(state.participant_additions, 1);
    assert_eq!(state.touched_channel_ids, vec![channel_id]);
    assert!(
        state
            .participants
            .iter()
            .any(|participant| participant.user_id == "macro|[email protected]")
    );
    drop(state);
    assert!(matches!(
        events.events.lock().unwrap().as_slice(),
        [ChannelEvent::ParticipantJoined { channel_id: event_channel_id, .. }]
            if event_channel_id == &channel_id
    ));
}

#[tokio::test]
async fn join_by_code_is_idempotent_for_active_participant() {
    let channel_id = Uuid::new_v4();
    let join_code = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    repo.state.lock().unwrap().join_code = Some(join_code);
    let events = FakeEvents::default();
    let svc = mutation_service(
        repo.clone(),
        events.clone(),
        FakeReferenceSharing::default(),
    );

    svc.join_channel_by_code(sender("macro|[email protected]"), join_code)
        .await
        .unwrap();

    let state = repo.state.lock().unwrap();
    assert_eq!(state.participant_additions, 0);
    assert!(state.touched_channel_ids.is_empty());
    assert!(events.events.lock().unwrap().is_empty());
}

#[derive(Clone)]
struct FakePictureFiles {
    file: Option<ChannelPictureFile>,
    fail: bool,
}

impl ChannelPictureFiles for FakePictureFiles {
    async fn get_picture_file(&self, _file_id: Uuid) -> anyhow::Result<Option<ChannelPictureFile>> {
        if self.fail {
            anyhow::bail!("static-file service unavailable");
        }
        Ok(self.file.clone())
    }
}

fn owned_picture_file() -> ChannelPictureFile {
    ChannelPictureFile {
        owner_id: "macro|[email protected]".into(),
        is_uploaded: true,
        content_type: "image/png".into(),
    }
}

#[tokio::test]
async fn channel_picture_can_be_set_replaced_and_removed_by_any_participant() {
    use entity_access::domain::models::{
        Entity, EntityPermission, MemberParticipantRole, ParticipantRole as AccessRole,
    };
    for role in [AccessRole::Member, AccessRole::Admin, AccessRole::Owner] {
        let channel_id = Uuid::new_v4();
        let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
        let events = FakeEvents::default();
        let svc = mutation_service(
            repo.clone(),
            events.clone(),
            FakeReferenceSharing::default(),
        )
        .with_picture_files(FakePictureFiles {
            file: Some(owned_picture_file()),
            fail: false,
        });
        let pictures = [Some(Uuid::new_v4()), Some(Uuid::new_v4()), None];
        for picture in pictures {
            let access = EntityAccessReceipt::<MemberParticipantRole>::try_new_authenticated_user(
                macro_id("macro|[email protected]"),
                Entity {
                    entity_id: channel_id.to_string(),
                    entity_type: EntityType::Channel,
                },
                EntityPermission::ChannelRole { role },
            )
            .unwrap();
            svc.set_channel_picture(access, picture).await.unwrap();
        }
        assert_eq!(
            *repo.picture_updates.lock().unwrap(),
            pictures.map(|picture| (channel_id, picture))
        );
        let events = events.events.lock().unwrap();
        assert_eq!(events.len(), 3);
        assert!(events.iter().all(|event| matches!(event,
            ChannelEvent::PictureChanged { channel_id: id, recipients, actor }
                if *id == channel_id && recipients.contains(&macro_id("macro|[email protected]"))
                    && actor == &macro_id("macro|[email protected]")
        )));
    }
}

#[tokio::test]
async fn repeated_channel_picture_updates_do_not_publish_false_activity() {
    use entity_access::domain::models::MemberParticipantRole;
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let events = FakeEvents::default();
    let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default())
        .with_picture_files(FakePictureFiles {
            file: Some(owned_picture_file()),
            fail: false,
        });
    let picture = Some(Uuid::new_v4());
    for (value, expected_events) in [(None, 0), (picture, 1), (picture, 1), (None, 2), (None, 2)] {
        let access =
            EntityAccessReceipt::<MemberParticipantRole>::dangerously_assert_authenticated_user(
                macro_id("macro|[email protected]"),
                &channel_id.to_string(),
                EntityType::Channel,
            );
        svc.set_channel_picture(access, value).await.unwrap();
        assert_eq!(events.events.lock().unwrap().len(), expected_events);
    }
}

#[test]
fn channel_picture_write_access_requires_an_active_participant_role() {
    use entity_access::domain::models::{
        Entity, EntityPermission, MemberParticipantRole, ParticipantRole as AccessRole,
    };
    let entity = || Entity {
        entity_id: Uuid::new_v4().to_string(),
        entity_type: EntityType::Channel,
    };
    assert!(
        EntityAccessReceipt::<MemberParticipantRole>::try_new_authenticated_user(
            macro_id("macro|[email protected]"),
            entity(),
            EntityPermission::ChannelRole {
                role: AccessRole::Member
            },
        )
        .is_ok()
    );
    // A viewer of a team channel they have not joined cannot edit its picture.
    assert!(
        EntityAccessReceipt::<MemberParticipantRole>::try_new_authenticated_user(
            macro_id("macro|[email protected]"),
            entity(),
            EntityPermission::ChannelViewOnly,
        )
        .is_err()
    );
}

#[tokio::test]
async fn channel_picture_rejects_direct_messages_and_non_channel_receipts() {
    use entity_access::domain::models::MemberParticipantRole;
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    repo.state.lock().unwrap().channel_type = ChannelType::DirectMessage;
    let svc = ChannelServiceImpl::new(repo.clone());
    for entity_type in [EntityType::Channel, EntityType::Document] {
        let access =
            EntityAccessReceipt::<MemberParticipantRole>::dangerously_assert_authenticated_user(
                macro_id("macro|[email protected]"),
                &channel_id.to_string(),
                entity_type,
            );
        assert!(matches!(
            svc.set_channel_picture(access, Some(Uuid::new_v4())).await,
            Err(ChannelMutationErr::BadRequest(_))
        ));
    }
    assert!(repo.picture_updates.lock().unwrap().is_empty());
}

#[tokio::test]
async fn channel_picture_rejects_foreign_missing_pending_non_image_and_unavailable_files() {
    use entity_access::domain::models::MemberParticipantRole;
    let files = [
        FakePictureFiles {
            file: Some(ChannelPictureFile {
                owner_id: "macro|[email protected]".into(),
                ..owned_picture_file()
            }),
            fail: false,
        },
        FakePictureFiles {
            file: None,
            fail: false,
        },
        FakePictureFiles {
            file: Some(ChannelPictureFile {
                is_uploaded: false,
                ..owned_picture_file()
            }),
            fail: false,
        },
        FakePictureFiles {
            file: Some(ChannelPictureFile {
                content_type: "text/html".into(),
                ..owned_picture_file()
            }),
            fail: false,
        },
        FakePictureFiles {
            file: Some(owned_picture_file()),
            fail: true,
        },
    ];
    for file in files {
        let channel_id = Uuid::new_v4();
        let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
        let events = FakeEvents::default();
        let svc = mutation_service(
            repo.clone(),
            events.clone(),
            FakeReferenceSharing::default(),
        )
        .with_picture_files(file);
        let access =
            EntityAccessReceipt::<MemberParticipantRole>::dangerously_assert_authenticated_user(
                macro_id("macro|[email protected]"),
                &channel_id.to_string(),
                EntityType::Channel,
            );
        assert!(
            svc.set_channel_picture(access, Some(Uuid::new_v4()))
                .await
                .is_err()
        );
        assert!(repo.picture_updates.lock().unwrap().is_empty());
        assert!(events.events.lock().unwrap().is_empty());
    }
}

#[tokio::test]
async fn channel_picture_removal_does_not_require_static_file_availability() {
    use entity_access::domain::models::MemberParticipantRole;
    let channel_id = Uuid::new_v4();
    let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
    let svc = ChannelServiceImpl::new(repo.clone());
    let access =
        EntityAccessReceipt::<MemberParticipantRole>::dangerously_assert_authenticated_user(
            macro_id("macro|[email protected]"),
            &channel_id.to_string(),
            EntityType::Channel,
        );
    svc.set_channel_picture(access, None).await.unwrap();
    assert_eq!(*repo.picture_updates.lock().unwrap(), [(channel_id, None)]);
}