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.

204 KB
Raw Blame
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};

use crate::domain::{
    models::{
        EnrichedGithubPullRequest, GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE, GitRef,
        GithubAppInstallationSource, GithubAuthenticatedUser, GithubError,
        GithubInstallationAccessToken, GithubKey, GithubPullRequestCheckRun,
        GithubPullRequestComment, GithubPullRequestDetails, GithubPullRequestStatus,
        GithubSetupAccessToken, GithubUserInstallation, MacroTaskId, ResolvedTeamTaskReference,
        TeamTaskReference, ValidatedGithubWebhookEvent,
    },
    ports::{GithubSyncClient, GithubSyncRepo, GithubSyncService},
};
use document_sub_type::DocumentSubType;
use documents::domain::models::EditDocumentServiceArgs;
use documents::domain::{
    content::{DocumentContent, DocumentContentLocation},
    models::{DocumentError, ImportEmailAttachmentRepoArgs, LocationQueryParams, NewDocument},
    ports::DocumentService,
    response::{
        CreateDocumentResponseData, DocumentMetadataWithContent, DocumentResponse,
        GetDocumentResponseData, LocationResponseV3,
    },
};
use entity_access::domain::models::{
    EditAccessLevel, EntityAccessReceipt, EntityType, MemberTeamRole, OwnerAccessLevel,
    ViewAccessLevel,
};
use foreign_entity::domain::{
    models::{
        CreateForeignEntity, ForeignEntity, ForeignEntityError, PatchForeignEntity, SourceId,
    },
    ports::{ForeignEntityListQuery, ForeignEntityService},
};
use github_pull_requests::domain::{
    models::{GithubPullRequestRow, GithubPullRequestWrite},
    ports::GithubPullRequestRepository,
    service::GithubPullRequestServiceImpl,
};
use macro_user_id::user_id::MacroUserIdStr;
use model::document::{DocumentBasic, DocumentMetadata};
use model_entity::Entity;
use model_owner::CreationPrincipal;
use models_permissions::share_permission::access_level::AccessLevel;
use models_properties::service::property_change_source::{GithubAccount, PropertyChangeSource};
use notification::domain::{
    models::{Notification, NotificationResult, SendNotificationRequest},
    service::{NotificationIngress, SendNotificationError},
};

use super::*;
use crate::domain::models::AppJwt;

/// UUID that corresponds to the short ID `2BuyvtY3aeEvHx4uG8iD51`.
const KNOWN_TASK_UUID: &str = "0d0dc589-f301-43f1-8b11-4ab448ca4bb4";

/// GitHub user id returned by [`StubSyncClient::get_authenticated_user`].
const TEST_GITHUB_USER_ID: u64 = 987654;

/// SAFETY: This is used for testing only
/// Minimal RSA private key used only for test JWT signing.
const TEST_PEM: &str = "-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAky4t+NMylQ8TEjJIKciwvjKWM+5EzSXDkvc+dlNN2g0/wRsr
CTkFE9tQdEpJASbUz8+TRnExM8rbAB3p0tAyhAino2UDYvMRCBH5tGIBxKAPejZ2
pEv63Gzk7xAlbIKyoOqdf/VUs5rNOsiB+l6/0Dbi2nBXFEjbQTNt33LOY6Smqu5f
tcvN9gxHMr+m+vhnuUraL39sP0AWEhml/aw+LLIPlO1Cfp/on0sxRGmd0bhqTVWa
o3fVqp8xqopQ3nCkZaYu6EUIzdg/ioktPEgY3kul/IS2QvJAfLAmi20/ahMLXJ+v
izWM11Qs4jwfjKDxtXBgU70bv3WMC4aaU6o7JQIDAQABAoIBAHXS5UiqQncj3z+U
80JIAH3y313pZDja/4s61U1CeTOTobNEvZofhJoV232NLo52eK14Xk1pNlthDRs1
10dGFvquNw3OQvzG256bTUyDnSi8fkd3LFlw3f3ySv+67ErHApth1v5l9w3lYmCp
vawih+n21nrKrlt1y9iRhGb6cJFBOsF8lmcFo9ijEzbRyaW+ou8J0ty9GNuwioET
RaimVOo0nct0lrN4A269C+LqHLRUpj2MdxYEH4+1ziSCRDhCIQhPxd0ylpcXVEYP
XubG5Kad8bueXn9HPtvkhxJJ0P9rD0M6+enPh5CdFPRg1qQchsoqSvRDxN4kwf5k
XzbLw8ECgYEAxDQrvwDaGDMpcMrNaxtyatUfLi4uuinDNYuK+45XqMSWKXehINMc
5bva0WBT3brKAdAoDRmZtfDiVvwc6Z59/WBSh+Zq29iLftazUhgCLejWFdIVO/SE
vAx6v3Ctyl0XgrkkV2wtKtpj9T8EU+8O9HnduP075VXrMmOwrh8/qbECgYEAwAkz
UG1fTs29BIbtAXauqhp14QM+J91viSQ7kzRIyElxp7S9IkAWWzei5K4piJGxBGBg
QwgviN0cpK8URtfFIXQijzcYMwKhf0RqPrX9Kwh+9FGHcK0SHCx3JMdzkhtNrkR3
1w+cjhP3VqsoZo/+reT7Wy6E4FlcrY6Rbo2qkbUCgYBZJiNibC6spEKGH3/q1NPO
Ovwp7Y4JxIQQRlFmL60g4AIi4VpzIbmVoR+x1wUEUKUM4dnw6drv0n3lbDRu6jbw
891MJqQTNHddsIxWFtaWqZ7s10ISte3BzCHR7o7ozheqrBkZJ+v19rlIa9O5l3vC
FcVrEpUuhTWS9b0HwOcaYQKBgCuOqq32cOS9876gIAfx9IIuyEgGZUXDizXvGvgz
psKPLhFdBH1NTgTYpMD74/3PFfipJ4xsweNoS8Pq1k2PSW5iGiij1YBUe28ThIm+
27K0FZ+zEmZzSyVKzKdx+fvM55y8ePY120u6qaJl5h8FUD3/LygqcAc3HbdcHA6Y
YXT1AoGAUyOZ7RPz8dLHWMA0+bRM4XGNxbyIjULKC/Fjf9bM3GIUWG8klxmBkCQJ
MEt9yPb3VfwFUyBSNJt4C6zDrnd+62oT+A9aJHJcUDUjqdBsmZamDu7xBAeLGxsn
sNRx7TF4iOEBkdJgBUoY4X/rZ+51FQOrdZGqeWo+8TjBhMQN7b4=
-----END RSA PRIVATE KEY-----";

/// Recorded update_task_status call.
#[derive(Debug, Clone)]
struct TaskStatusCall {
    entity_id: String,
    credited_user: Option<MacroUserIdStr<'static>>,
    source: Option<PropertyChangeSource>,
    status: String,
}

struct StubDocumentService {
    task_status_calls: Mutex<Vec<TaskStatusCall>>,
}

impl StubDocumentService {
    fn new() -> Self {
        Self {
            task_status_calls: Mutex::new(Vec::new()),
        }
    }

    fn task_status_calls(&self) -> Vec<TaskStatusCall> {
        self.task_status_calls.lock().unwrap().clone()
    }

    fn task_metadata(document_id: &str) -> DocumentMetadata {
        DocumentMetadata {
            document_id: document_id.to_string(),
            document_version_id: 1,
            owner: "macro|[email protected]".to_string().try_into().unwrap(),
            document_name: "My Task".to_string(),
            file_type: Some("md".to_string()),
            sha: None,
            project_id: None,
            project_name: None,
            branched_from_id: None,
            branched_from_version_id: None,
            document_family_id: None,
            document_bom: None,
            modification_data: None,
            created_at: None,
            updated_at: None,
            deleted_at: None,
            sub_type: Some(DocumentSubType::Task),
        }
    }
}

impl DocumentService for StubDocumentService {
    async fn internal_get_basic_document(
        &self,
        _document_id: &str,
    ) -> Result<DocumentBasic, DocumentError> {
        unimplemented!()
    }
    async fn internal_get_user_display_name(
        &self,
        _user_id: &str,
    ) -> Result<Option<String>, DocumentError> {
        unimplemented!()
    }
    async fn get_document_by_team_slug(
        &self,
        _team_receipt: EntityAccessReceipt<MemberTeamRole>,
        _slug: &str,
    ) -> Result<String, DocumentError> {
        unimplemented!()
    }
    async fn get_short_id(
        &self,
        _receipt: EntityAccessReceipt<ViewAccessLevel>,
    ) -> Result<String, DocumentError> {
        unimplemented!()
    }
    async fn get_task_branch_name(
        &self,
        _receipt: EntityAccessReceipt<ViewAccessLevel>,
        _document_name: String,
    ) -> Result<documents::domain::models::TaskBranchName, DocumentError> {
        unimplemented!()
    }
    async fn get_task_identity(
        &self,
        _receipt: EntityAccessReceipt<ViewAccessLevel>,
        _document_context: &DocumentBasic,
    ) -> Result<documents::domain::models::TaskIdentity, DocumentError> {
        unimplemented!()
    }
    async fn get_task_github_pull_requests(
        &self,
        _receipt: EntityAccessReceipt<ViewAccessLevel>,
        _document_context: &DocumentBasic,
    ) -> Result<documents::domain::models::GithubPullRequestsResponse, DocumentError> {
        unimplemented!()
    }
    async fn get_github_pull_request_tasks(
        &self,
        _user_id: &str,
        _github_keys: Vec<String>,
    ) -> Result<documents::domain::models::GithubPullRequestTasksResponse, DocumentError> {
        unimplemented!()
    }
    async fn get_project_children(
        &self,
        _project_id: &str,
    ) -> Result<Vec<Entity<'static>>, DocumentError> {
        unimplemented!()
    }
    async fn get_project_name(&self, _project_id: &str) -> Result<String, DocumentError> {
        unimplemented!()
    }
    async fn get_document(
        &self,
        receipt: EntityAccessReceipt<ViewAccessLevel>,
    ) -> Result<GetDocumentResponseData, DocumentError> {
        let document_id = receipt.entity().entity_id.clone();
        if document_id == KNOWN_TASK_UUID {
            Ok(GetDocumentResponseData {
                document_metadata: DocumentMetadataWithContent::new(
                    Self::task_metadata(&document_id),
                    DocumentContent::ready(DocumentContentLocation::SyncService),
                ),
                user_access_level: AccessLevel::Owner,
                view_location: None,
            })
        } else {
            Err(DocumentError::NotFound(document_id))
        }
    }
    async fn get_document_location(
        &self,
        _ctx: &DocumentBasic,
        _receipt: EntityAccessReceipt<ViewAccessLevel>,
        _params: LocationQueryParams,
    ) -> Result<LocationResponseV3, DocumentError> {
        unimplemented!()
    }
    async fn delete_document(
        &self,
        _receipt: EntityAccessReceipt<OwnerAccessLevel>,
        _project_id: Option<String>,
    ) -> Result<(), DocumentError> {
        unimplemented!()
    }
    async fn get_document_text(
        &self,
        _receipt: EntityAccessReceipt<ViewAccessLevel>,
    ) -> Result<String, DocumentError> {
        unimplemented!()
    }
    async fn create_document(
        &self,
        _principal: &CreationPrincipal,
        _document: NewDocument,
        _job_id: Option<String>,
    ) -> Result<CreateDocumentResponseData, DocumentError> {
        unimplemented!()
    }

    async fn import_email_attachment(
        &self,
        _args: ImportEmailAttachmentRepoArgs,
    ) -> Result<CreateDocumentResponseData, DocumentError> {
        unimplemented!()
    }

    async fn get_document_content(
        &self,
        _document_context: &DocumentBasic,
    ) -> Result<DocumentContent, DocumentError> {
        unimplemented!()
    }
    async fn update_task_status(
        &self,
        receipt: EntityAccessReceipt<EditAccessLevel>,
        credited_user: Option<MacroUserIdStr<'static>>,
        source: Option<PropertyChangeSource>,
        status: &str,
    ) -> Result<(), DocumentError> {
        self.task_status_calls.lock().unwrap().push(TaskStatusCall {
            entity_id: receipt.entity().entity_id.clone(),
            credited_user,
            source,
            status: status.to_string(),
        });
        Ok(())
    }

    async fn get_team_share(
        &self,
        _receipt: EntityAccessReceipt<ViewAccessLevel>,
    ) -> Result<documents::domain::models::DocumentTeamShareResponse, DocumentError> {
        unimplemented!()
    }

    async fn set_team_share(
        &self,
        _receipt: EntityAccessReceipt<EditAccessLevel>,
        _share: bool,
    ) -> Result<documents::domain::models::DocumentTeamShareResponse, DocumentError> {
        unimplemented!()
    }

    async fn edit_document(
        &self,
        _entity_access_receipt: EntityAccessReceipt<EditAccessLevel>,
        _document_basic: DocumentBasic,
        _request: EditDocumentServiceArgs,
    ) -> Result<(), DocumentError> {
        Ok(())
    }

    async fn copy_document(
        &self,
        _entity_access_receipt: EntityAccessReceipt<ViewAccessLevel>,
        _document_context: DocumentBasic,
        _principal: &CreationPrincipal,
        _document_name: String,
        _query_version_id: Option<i64>,
        _sync_version_id: Option<model::sync_service::SyncServiceVersionID>,
    ) -> Result<DocumentResponse, DocumentError> {
        unimplemented!()
    }

    async fn handle_task_properties(
        &self,
        _principal: &CreationPrincipal,
        _document_id: &str,
        _request: &documents::domain::models::CreateTaskRequest,
    ) -> Result<(), DocumentError> {
        unimplemented!()
    }

    async fn get_snapshot(&self, _document_id: &str) -> anyhow::Result<Option<Vec<u8>>> {
        unimplemented!()
    }

    async fn upload_snapshot(&self, _document_id: &str, _bytes: Vec<u8>) -> anyhow::Result<()> {
        unimplemented!()
    }

    async fn record_interaction(
        &self,
        _document_id: &str,
        _reason: documents::domain::events::InteractionReason,
    ) -> anyhow::Result<()> {
        unimplemented!()
    }

    async fn set_skill_description(
        &self,
        _entity_access_receipt: entity_access::domain::models::EntityAccessReceipt<
            models_permissions::share_permission::access_level::EditAccessLevel,
        >,
        _description: documents::domain::skill_description::SkillDescription,
    ) -> Result<(), documents::domain::models::DocumentError> {
        panic!("unexpected set_skill_description call")
    }

    async fn describe_new_skill(
        &self,
        _document_id: &str,
        _description: documents::domain::skill_description::SkillDescription,
    ) -> Result<(), documents::domain::models::DocumentError> {
        Ok(())
    }

    async fn skill_descriptions(
        &self,
        _document_ids: &[String],
    ) -> Result<
        std::collections::HashMap<String, documents::domain::skill_description::SkillDescription>,
        documents::domain::models::DocumentError,
    > {
        Ok(std::collections::HashMap::new())
    }
}

/// Stateful stub repo that tracks task IDs per github key.
struct StubSyncRepo {
    tasks: Mutex<HashMap<String, HashSet<String>>>,
    /// Maps (installation_id, normalized team_slug, team_task_id) -> matching
    /// (team_id, task ID) pairs. Multiple entries model a slug shared by
    /// several of the installation's teams.
    #[allow(clippy::type_complexity)]
    team_task_references: Mutex<HashMap<(String, String, i32), Vec<(uuid::Uuid, MacroTaskId)>>>,
    /// Maps github_user_id -> macro_ids for installation event lookups.
    ///
    /// A github_user_id may map to multiple Macro users because multiple Macro
    /// users can share one GitHub account.
    github_links: Mutex<HashMap<String, Vec<String>>>,
    /// Maps lowercase github login -> macro_ids for mention lookups.
    github_login_links: Mutex<HashMap<String, Vec<String>>>,
    /// Maps macro_id -> team_ids for installation event lookups.
    user_teams: Mutex<HashMap<String, Vec<uuid::Uuid>>>,
    /// Maps team_id -> Macro user IDs for notification recipient lookups.
    team_members: Mutex<HashMap<uuid::Uuid, Vec<MacroUserIdStr<'static>>>>,
    /// Current github_app_installation source rows keyed by installation id.
    installation_source_rows: Mutex<HashMap<String, HashSet<GithubAppInstallationSource>>>,
    /// Recorded installation source upserts: (installation_id, sources).
    installation_sources: Mutex<Vec<(String, Vec<GithubAppInstallationSource>)>>,
    /// Pending installation requests keyed by requester github user id.
    installation_requests: Mutex<HashMap<String, GithubAppInstallationSource>>,
}

impl StubSyncRepo {
    fn new() -> Self {
        Self {
            tasks: Mutex::new(HashMap::new()),
            team_task_references: Mutex::new(HashMap::new()),
            github_links: Mutex::new(HashMap::new()),
            github_login_links: Mutex::new(HashMap::new()),
            user_teams: Mutex::new(HashMap::new()),
            team_members: Mutex::new(HashMap::new()),
            installation_source_rows: Mutex::new(HashMap::new()),
            installation_sources: Mutex::new(Vec::new()),
            installation_requests: Mutex::new(HashMap::new()),
        }
    }

    fn with_github_link(self, github_user_id: &str, macro_id: &str) -> Self {
        self.github_links
            .lock()
            .unwrap()
            .entry(github_user_id.to_string())
            .or_default()
            .push(macro_id.to_string());
        self
    }

    fn with_github_login_link(self, github_login: &str, macro_id: &str) -> Self {
        self.github_login_links
            .lock()
            .unwrap()
            .entry(github_login.to_lowercase())
            .or_default()
            .push(macro_id.to_string());
        self
    }

    fn with_user_teams(self, macro_id: &str, team_ids: Vec<uuid::Uuid>) -> Self {
        self.user_teams
            .lock()
            .unwrap()
            .insert(macro_id.to_string(), team_ids);
        self
    }

    fn with_team_members(self, team_id: uuid::Uuid, member_ids: Vec<&str>) -> Self {
        let member_ids = member_ids
            .into_iter()
            .map(|member_id| MacroUserIdStr::try_from(member_id.to_string()).unwrap())
            .collect();
        self.team_members
            .lock()
            .unwrap()
            .insert(team_id, member_ids);
        self
    }

    fn with_team_task_reference(
        self,
        installation_id: &str,
        team_slug: &str,
        team_task_id: i32,
        team_id: uuid::Uuid,
        task_id: MacroTaskId,
    ) -> Self {
        self.team_task_references
            .lock()
            .unwrap()
            .entry((
                installation_id.to_string(),
                team_slug.to_ascii_lowercase(),
                team_task_id,
            ))
            .or_default()
            .push((team_id, task_id));
        self
    }

    fn with_installation_sources(
        self,
        installation_id: &str,
        sources: Vec<GithubAppInstallationSource>,
    ) -> Self {
        {
            let mut rows = self.installation_source_rows.lock().unwrap();
            let row_sources = rows.entry(installation_id.to_string()).or_default();
            row_sources.extend(sources);
        }
        self
    }

    fn installation_sources(&self) -> Vec<(String, Vec<GithubAppInstallationSource>)> {
        self.installation_sources.lock().unwrap().clone()
    }

    fn with_installation_request(
        self,
        github_user_id: &str,
        source: GithubAppInstallationSource,
    ) -> Self {
        self.installation_requests
            .lock()
            .unwrap()
            .insert(github_user_id.to_string(), source);
        self
    }

    fn installation_requests(&self) -> HashMap<String, GithubAppInstallationSource> {
        self.installation_requests.lock().unwrap().clone()
    }
}

impl GithubSyncRepo for StubSyncRepo {
    type Err = anyhow::Error;

    async fn get_task_ids(&self, github_key: GithubKey) -> Result<Vec<MacroTaskId>, Self::Err> {
        let tasks = self.tasks.lock().unwrap();
        let ids = tasks
            .get(github_key.as_ref())
            .map(|set| {
                set.iter()
                    .filter_map(|s| MacroTaskId::from_short_uuid(s))
                    .collect()
            })
            .unwrap_or_default();
        Ok(ids)
    }

    async fn upsert_task_ids(
        &self,
        github_key: GithubKey,
        task_ids: &[MacroTaskId],
    ) -> Result<(), Self::Err> {
        let mut tasks = self.tasks.lock().unwrap();
        let set = tasks.entry(github_key.as_ref().to_string()).or_default();
        for id in task_ids {
            set.insert(id.short_uuid.clone());
        }
        Ok(())
    }

    async fn filter_duplicate_tasks(
        &self,
        github_key: GithubKey,
        task_ids: &[MacroTaskId],
    ) -> Result<Vec<MacroTaskId>, Self::Err> {
        let tasks = self.tasks.lock().unwrap();
        let existing = tasks.get(github_key.as_ref());
        Ok(task_ids
            .iter()
            .filter(|t| {
                existing
                    .map(|set| !set.contains(&t.short_uuid))
                    .unwrap_or(true)
            })
            .cloned()
            .collect())
    }

    async fn resolve_team_task_references(
        &self,
        installation_id: &str,
        references: &[TeamTaskReference],
    ) -> Result<Vec<ResolvedTeamTaskReference>, Self::Err> {
        let team_task_references = self.team_task_references.lock().unwrap();
        let mut seen = HashSet::new();
        let mut resolved = Vec::new();

        for reference in references {
            let key = (
                installation_id.to_string(),
                reference.team_slug.to_ascii_lowercase(),
                reference.team_task_id,
            );
            for (team_id, task_id) in team_task_references.get(&key).into_iter().flatten() {
                if seen.insert((*team_id, task_id.clone())) {
                    resolved.push(ResolvedTeamTaskReference {
                        reference: reference.clone(),
                        team_id: *team_id,
                        task_id: task_id.clone(),
                    });
                }
            }
        }

        Ok(resolved)
    }

    async fn get_macro_ids_by_github_user_ids(
        &self,
        github_user_ids: &[String],
    ) -> Result<HashMap<String, Vec<String>>, Self::Err> {
        let links = self.github_links.lock().unwrap();
        Ok(github_user_ids
            .iter()
            .filter_map(|github_user_id| {
                let macro_ids = links.get(github_user_id)?.clone();
                Some((github_user_id.clone(), macro_ids))
            })
            .collect())
    }

    async fn get_macro_ids_by_github_logins(
        &self,
        github_logins: &[String],
    ) -> Result<HashMap<String, Vec<String>>, Self::Err> {
        let links = self.github_login_links.lock().unwrap();
        Ok(github_logins
            .iter()
            .filter_map(|login| {
                let login = login.to_lowercase();
                let macro_ids = links.get(&login)?.clone();
                Some((login, macro_ids))
            })
            .collect())
    }

    async fn get_user_team_ids(&self, macro_id: &str) -> Result<Vec<uuid::Uuid>, Self::Err> {
        Ok(self
            .user_teams
            .lock()
            .unwrap()
            .get(macro_id)
            .cloned()
            .unwrap_or_default())
    }

    async fn get_team_member_ids(
        &self,
        team_id: uuid::Uuid,
    ) -> Result<Vec<MacroUserIdStr<'static>>, Self::Err> {
        let mut member_ids = self
            .team_members
            .lock()
            .unwrap()
            .get(&team_id)
            .cloned()
            .unwrap_or_default();
        member_ids.sort_by(|left, right| left.as_ref().cmp(right.as_ref()));
        Ok(member_ids)
    }

    async fn get_installation_ids_for_sources(
        &self,
        macro_id: &str,
        team_ids: &[uuid::Uuid],
    ) -> Result<Vec<String>, Self::Err> {
        let rows = self.installation_source_rows.lock().unwrap();
        let mut installation_ids: Vec<String> = rows
            .iter()
            .filter(|(_, sources)| {
                sources.iter().any(|source| match source {
                    GithubAppInstallationSource::User(user) => user == macro_id,
                    GithubAppInstallationSource::Team(team) => team_ids.contains(team),
                })
            })
            .map(|(installation_id, _)| installation_id.clone())
            .collect();
        installation_ids.sort();
        Ok(installation_ids)
    }

    async fn get_installation_sources(
        &self,
        installation_id: &str,
    ) -> Result<Vec<GithubAppInstallationSource>, Self::Err> {
        Ok(self
            .installation_source_rows
            .lock()
            .unwrap()
            .get(installation_id)
            .map(|sources| sources.iter().cloned().collect())
            .unwrap_or_default())
    }

    async fn upsert_installation_sources(
        &self,
        installation_id: &str,
        sources: &[GithubAppInstallationSource],
    ) -> Result<(), Self::Err> {
        {
            let mut rows = self.installation_source_rows.lock().unwrap();
            let row_sources = rows.entry(installation_id.to_string()).or_default();
            row_sources.extend(sources.iter().cloned());
        }
        self.installation_sources
            .lock()
            .unwrap()
            .push((installation_id.to_string(), sources.to_vec()));
        Ok(())
    }

    async fn delete_installation_sources(&self, installation_id: &str) -> Result<(), Self::Err> {
        self.installation_source_rows
            .lock()
            .unwrap()
            .remove(installation_id);
        Ok(())
    }

    async fn upsert_installation_request(
        &self,
        github_user_id: &str,
        source: &GithubAppInstallationSource,
    ) -> Result<(), Self::Err> {
        self.installation_requests
            .lock()
            .unwrap()
            .insert(github_user_id.to_string(), source.clone());
        Ok(())
    }

    async fn get_installation_request(
        &self,
        github_user_id: &str,
    ) -> Result<Option<GithubAppInstallationSource>, Self::Err> {
        Ok(self
            .installation_requests
            .lock()
            .unwrap()
            .get(github_user_id)
            .cloned())
    }

    async fn delete_installation_request(&self, github_user_id: &str) -> Result<(), Self::Err> {
        self.installation_requests
            .lock()
            .unwrap()
            .remove(github_user_id);
        Ok(())
    }
}

#[tokio::test]
async fn test_get_team_member_ids_stub_returns_fixture_members() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = StubSyncRepo::new()
        .with_team_members(team_id, vec!["macro|[email protected]", "macro|[email protected]"]);

    let member_ids = repo.get_team_member_ids(team_id).await.unwrap();
    let member_ids: Vec<String> = member_ids.into_iter().map(String::from).collect();

    assert_eq!(
        member_ids,
        vec![
            "macro|[email protected]".to_string(),
            "macro|[email protected]".to_string(),
        ]
    );
    assert!(
        repo.get_team_member_ids(uuid::Uuid::nil())
            .await
            .unwrap()
            .is_empty()
    );
}

/// Recorded PR comment call.
#[derive(Debug, Clone)]
struct PrCommentCall {
    owner: String,
    repo: String,
    pull_number: u64,
    body: String,
}

/// Recorded pull request details call.
#[derive(Debug, Clone)]
struct PullRequestDetailsCall {
    owner: String,
    repo: String,
    number: u64,
}

struct StubSyncClient {
    setup_code_exchange_calls: Mutex<Vec<(String, String, String)>>,
    user_installation_list_calls: Mutex<Vec<String>>,
    user_installations: Mutex<Vec<GithubUserInstallation>>,
    authenticated_user_calls: Mutex<Vec<String>>,
    authenticated_user_id: Mutex<u64>,
    fail_setup_code_exchange: Mutex<bool>,
    fail_user_installation_list: Mutex<bool>,
    fail_get_authenticated_user: Mutex<bool>,
    pr_comments: Mutex<Vec<PrCommentCall>>,
    pull_request_details: Mutex<HashMap<String, GithubPullRequestDetails>>,
    pull_request_details_calls: Mutex<Vec<PullRequestDetailsCall>>,
    open_pull_requests: Mutex<Vec<EnrichedGithubPullRequest>>,
    list_open_pull_requests_calls: Mutex<Vec<String>>,
}

impl StubSyncClient {
    fn new() -> Self {
        Self {
            setup_code_exchange_calls: Mutex::new(Vec::new()),
            user_installation_list_calls: Mutex::new(Vec::new()),
            user_installations: Mutex::new(Vec::new()),
            authenticated_user_calls: Mutex::new(Vec::new()),
            authenticated_user_id: Mutex::new(TEST_GITHUB_USER_ID),
            fail_setup_code_exchange: Mutex::new(false),
            fail_user_installation_list: Mutex::new(false),
            fail_get_authenticated_user: Mutex::new(false),
            pr_comments: Mutex::new(Vec::new()),
            pull_request_details: Mutex::new(HashMap::new()),
            pull_request_details_calls: Mutex::new(Vec::new()),
            open_pull_requests: Mutex::new(Vec::new()),
            list_open_pull_requests_calls: Mutex::new(Vec::new()),
        }
    }

    fn set_user_installations(&self, installation_ids: &[u64]) {
        *self.user_installations.lock().unwrap() = installation_ids
            .iter()
            .map(|id| GithubUserInstallation { id: *id })
            .collect();
    }

    fn fail_setup_code_exchange(&self) {
        *self.fail_setup_code_exchange.lock().unwrap() = true;
    }

    fn fail_user_installation_list(&self) {
        *self.fail_user_installation_list.lock().unwrap() = true;
    }

    fn fail_get_authenticated_user(&self) {
        *self.fail_get_authenticated_user.lock().unwrap() = true;
    }

    fn authenticated_user_calls(&self) -> Vec<String> {
        self.authenticated_user_calls.lock().unwrap().clone()
    }

    fn setup_code_exchange_calls(&self) -> Vec<(String, String, String)> {
        self.setup_code_exchange_calls.lock().unwrap().clone()
    }

    fn user_installation_list_calls(&self) -> Vec<String> {
        self.user_installation_list_calls.lock().unwrap().clone()
    }

    fn pr_comments(&self) -> Vec<PrCommentCall> {
        self.pr_comments.lock().unwrap().clone()
    }

    fn pull_request_details_calls(&self) -> Vec<PullRequestDetailsCall> {
        self.pull_request_details_calls.lock().unwrap().clone()
    }

    fn list_open_pull_requests_calls(&self) -> Vec<String> {
        self.list_open_pull_requests_calls.lock().unwrap().clone()
    }

    fn set_open_pull_requests(&self, pull_requests: Vec<EnrichedGithubPullRequest>) {
        *self.open_pull_requests.lock().unwrap() = pull_requests;
    }

    fn set_pull_request_details(
        &self,
        owner: &str,
        repo: &str,
        number: u64,
        details: GithubPullRequestDetails,
    ) {
        self.pull_request_details
            .lock()
            .unwrap()
            .insert(Self::pull_request_details_key(owner, repo, number), details);
    }

    fn pull_request_details_key(owner: &str, repo: &str, number: u64) -> String {
        GithubKey::new(owner, repo, number).to_string()
    }
}

impl GithubSyncClient for StubSyncClient {
    async fn exchange_setup_code(
        &self,
        client_id: &str,
        client_secret: &str,
        code: &str,
    ) -> Result<GithubSetupAccessToken, GithubError> {
        self.setup_code_exchange_calls.lock().unwrap().push((
            client_id.to_string(),
            client_secret.to_string(),
            code.to_string(),
        ));
        if *self.fail_setup_code_exchange.lock().unwrap() {
            return Err(GithubError::Internal(anyhow::anyhow!("exchange failed")));
        }
        Ok(GithubSetupAccessToken::new("test-user-token".to_string()))
    }

    async fn list_user_installations(
        &self,
        access_token: &str,
    ) -> Result<Vec<GithubUserInstallation>, GithubError> {
        self.user_installation_list_calls
            .lock()
            .unwrap()
            .push(access_token.to_string());
        if *self.fail_user_installation_list.lock().unwrap() {
            return Err(GithubError::Internal(anyhow::anyhow!("listing failed")));
        }
        Ok(self.user_installations.lock().unwrap().clone())
    }

    async fn get_authenticated_user(
        &self,
        access_token: &str,
    ) -> Result<GithubAuthenticatedUser, GithubError> {
        self.authenticated_user_calls
            .lock()
            .unwrap()
            .push(access_token.to_string());
        if *self.fail_get_authenticated_user.lock().unwrap() {
            return Err(GithubError::Internal(anyhow::anyhow!(
                "authenticated user lookup failed"
            )));
        }
        Ok(GithubAuthenticatedUser {
            id: *self.authenticated_user_id.lock().unwrap(),
        })
    }

    async fn generate_installation_access_token(
        &self,
        _jwt: &AppJwt,
        _installation_id: u64,
    ) -> Result<GithubInstallationAccessToken, GithubError> {
        Ok(GithubInstallationAccessToken {
            token: "test-token".to_string(),
            expires_at: "2099-01-01T00:00:00Z".to_string(),
        })
    }

    async fn get_repository_installation(
        &self,
        _jwt: &AppJwt,
        _owner: &str,
        _repository: &str,
    ) -> Result<Option<u64>, GithubError> {
        unimplemented!("the sync service does not look installations up by repository")
    }

    async fn generate_scoped_installation_access_token(
        &self,
        _jwt: &AppJwt,
        _installation_id: u64,
        _repository: &str,
        _permissions: &[(&str, &str)],
    ) -> Result<GithubInstallationAccessToken, GithubError> {
        unimplemented!("the sync service mints unscoped installation tokens")
    }

    async fn create_pr_comment(
        &self,
        _access_token: &str,
        owner: &str,
        repo: &str,
        pull_number: u64,
        body: &str,
    ) -> Result<(), GithubError> {
        self.pr_comments.lock().unwrap().push(PrCommentCall {
            owner: owner.to_string(),
            repo: repo.to_string(),
            pull_number,
            body: body.to_string(),
        });
        Ok(())
    }

    async fn get_pull_request_details(
        &self,
        _access_token: &str,
        owner: &str,
        repo: &str,
        number: u64,
    ) -> Result<GithubPullRequestDetails, GithubError> {
        self.pull_request_details_calls
            .lock()
            .unwrap()
            .push(PullRequestDetailsCall {
                owner: owner.to_string(),
                repo: repo.to_string(),
                number,
            });

        self.pull_request_details
            .lock()
            .unwrap()
            .get(&Self::pull_request_details_key(owner, repo, number))
            .cloned()
            .ok_or_else(|| GithubError::Internal(anyhow::anyhow!("missing stub PR details")))
    }

    async fn list_open_pull_requests(
        &self,
        access_token: &str,
    ) -> Result<Vec<EnrichedGithubPullRequest>, GithubError> {
        self.list_open_pull_requests_calls
            .lock()
            .unwrap()
            .push(access_token.to_string());

        Ok(self.open_pull_requests.lock().unwrap().clone())
    }

    async fn list_repository_branches(
        &self,
        _access_token: &str,
        _owner: &str,
        _repository: &str,
    ) -> Result<Vec<String>, GithubError> {
        unimplemented!("the sync service does not list repository branches")
    }
}

fn foreign_entity_id_from_receipt(
    receipt: EntityAccessReceipt<ViewAccessLevel>,
) -> Result<uuid::Uuid, ForeignEntityError> {
    let entity = receipt.entity();
    if entity.entity_type != EntityType::ForeignEntity {
        return Err(ForeignEntityError::BadRequest(format!(
            "expected ForeignEntity receipt, got {:?}",
            entity.entity_type
        )));
    }

    uuid::Uuid::parse_str(&entity.entity_id).map_err(|_| {
        ForeignEntityError::BadRequest("foreign entity receipt id must be a valid UUID".to_string())
    })
}

#[derive(Clone)]
struct StubForeignEntityService {
    foreign_entities: Arc<Mutex<Vec<ForeignEntity>>>,
    create_calls: Arc<Mutex<Vec<CreateForeignEntity>>>,
    patch_calls: Arc<Mutex<Vec<(uuid::Uuid, PatchForeignEntity)>>>,
}

impl StubForeignEntityService {
    fn new() -> Self {
        Self {
            foreign_entities: Arc::new(Mutex::new(Vec::new())),
            create_calls: Arc::new(Mutex::new(Vec::new())),
            patch_calls: Arc::new(Mutex::new(Vec::new())),
        }
    }

    fn foreign_entities(&self) -> Vec<ForeignEntity> {
        self.foreign_entities.lock().unwrap().clone()
    }

    fn create_calls(&self) -> Vec<CreateForeignEntity> {
        self.create_calls.lock().unwrap().clone()
    }

    fn patch_calls(&self) -> Vec<(uuid::Uuid, PatchForeignEntity)> {
        self.patch_calls.lock().unwrap().clone()
    }
}

impl ForeignEntityService for StubForeignEntityService {
    async fn get_foreign_entity(
        &self,
        receipt: EntityAccessReceipt<ViewAccessLevel>,
    ) -> Result<ForeignEntity, ForeignEntityError> {
        let id = foreign_entity_id_from_receipt(receipt)?;
        self.get_foreign_entity_by_id(id).await
    }

    async fn get_foreign_entity_by_id(
        &self,
        id: uuid::Uuid,
    ) -> Result<ForeignEntity, ForeignEntityError> {
        self.foreign_entities
            .lock()
            .unwrap()
            .iter()
            .find(|entity| entity.id == id)
            .cloned()
            .ok_or(ForeignEntityError::NotFound(id))
    }

    async fn get_foreign_entities_by_foreign_entity_id(
        &self,
        foreign_entity_id: &str,
        foreign_entity_source: Option<&str>,
    ) -> Result<Vec<ForeignEntity>, ForeignEntityError> {
        Ok(self
            .foreign_entities
            .lock()
            .unwrap()
            .iter()
            .filter(|entity| entity.foreign_entity_id == foreign_entity_id)
            .filter(|entity| {
                foreign_entity_source
                    .map(|source| entity.foreign_entity_source == source)
                    .unwrap_or(true)
            })
            .cloned()
            .collect())
    }

    async fn get_foreign_entities_for_user(
        &self,
        _requesting_user: Option<String>,
        source_ids: Vec<SourceId>,
        limit: u32,
        _query: ForeignEntityListQuery,
    ) -> Result<Vec<ForeignEntity>, ForeignEntityError> {
        Ok(self
            .foreign_entities
            .lock()
            .unwrap()
            .iter()
            .filter(|entity| {
                source_ids.iter().any(|source_id| {
                    entity.stored_for_id.as_str() == source_id.id.as_str()
                        && entity.stored_for_auth_entity.as_str() == source_id.auth_entity.as_str()
                })
            })
            .take(limit as usize)
            .cloned()
            .collect())
    }

    async fn create_foreign_entity(
        &self,
        create: CreateForeignEntity,
    ) -> Result<ForeignEntity, ForeignEntityError> {
        let now = chrono::Utc::now();
        let entity = ForeignEntity {
            id: uuid::Uuid::new_v4(),
            foreign_entity_id: create.foreign_entity_id.clone(),
            foreign_entity_source: create.foreign_entity_source.clone(),
            metadata: create.metadata.clone(),
            stored_for_id: create.stored_for_id.clone(),
            stored_for_auth_entity: create.stored_for_auth_entity.clone(),
            created_at: now,
            updated_at: now,
        };

        self.create_calls.lock().unwrap().push(create);
        self.foreign_entities.lock().unwrap().push(entity.clone());
        Ok(entity)
    }

    async fn delete_foreign_entity(&self, id: uuid::Uuid) -> Result<(), ForeignEntityError> {
        let mut foreign_entities = self.foreign_entities.lock().unwrap();
        let original_len = foreign_entities.len();
        foreign_entities.retain(|entity| entity.id != id);

        if foreign_entities.len() == original_len {
            return Err(ForeignEntityError::NotFound(id));
        }

        Ok(())
    }

    async fn patch_foreign_entity(
        &self,
        id: uuid::Uuid,
        patch: PatchForeignEntity,
    ) -> Result<ForeignEntity, ForeignEntityError> {
        self.patch_calls.lock().unwrap().push((id, patch.clone()));

        let mut foreign_entities = self.foreign_entities.lock().unwrap();
        let Some(entity) = foreign_entities.iter_mut().find(|entity| entity.id == id) else {
            return Err(ForeignEntityError::NotFound(id));
        };

        if let Some(foreign_entity_id) = patch.foreign_entity_id {
            entity.foreign_entity_id = foreign_entity_id;
        }
        if let Some(foreign_entity_source) = patch.foreign_entity_source {
            entity.foreign_entity_source = foreign_entity_source;
        }
        if let Some(metadata) = patch.metadata {
            entity.metadata = metadata;
        }
        if let Some(stored_for_id) = patch.stored_for_id {
            entity.stored_for_id = stored_for_id;
        }
        if let Some(stored_for_auth_entity) = patch.stored_for_auth_entity {
            entity.stored_for_auth_entity = stored_for_auth_entity;
        }
        entity.updated_at = chrono::Utc::now();

        Ok(entity.clone())
    }
}

struct StubNotificationIngress {
    requests: Mutex<Vec<serde_json::Value>>,
    fail_sends: bool,
}

impl StubNotificationIngress {
    fn new() -> Self {
        Self {
            requests: Mutex::new(Vec::new()),
            fail_sends: false,
        }
    }

    fn failing() -> Self {
        Self {
            requests: Mutex::new(Vec::new()),
            fail_sends: true,
        }
    }

    fn requests(&self) -> Vec<serde_json::Value> {
        self.requests.lock().unwrap().clone()
    }

    fn clear_requests(&self) {
        self.requests.lock().unwrap().clear();
    }
}

impl NotificationIngress for StubNotificationIngress {
    async fn send_notification<
        'a,
        T: Notification + Clone + 'static,
        U: serde::Serialize + Send + Sync + 'static,
    >(
        &'a self,
        request: SendNotificationRequest<'a, T, U>,
    ) -> Result<Option<NotificationResult<'a>>, rootcause::Report<SendNotificationError>> {
        let snapshot = serde_json::to_value(&request).unwrap();
        self.requests.lock().unwrap().push(snapshot);

        if self.fail_sends {
            return Err(rootcause::Report::new(SendNotificationError::Other));
        }

        Ok(None)
    }
}

type TestSyncServiceImpl = GithubSyncServiceImpl<
    StubDocumentService,
    StubSyncRepo,
    StubSyncClient,
    TestPullRequestService,
    StubNotificationIngress,
    StubRealtime,
>;

/// The sync service under test, with a handle on the foreign entities it stores.
struct TestGithubSyncService {
    service: TestSyncServiceImpl,
    foreign_entity_service: StubForeignEntityService,
}

impl std::ops::Deref for TestGithubSyncService {
    type Target = TestSyncServiceImpl;

    fn deref(&self) -> &Self::Target {
        &self.service
    }
}

impl std::ops::DerefMut for TestGithubSyncService {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.service
    }
}

type TestServiceWithForeignEntityService = (TestGithubSyncService, StubForeignEntityService);

type TestPullRequestService =
    GithubPullRequestServiceImpl<StubForeignEntityService, NoPullRequestRows>;

struct NoPullRequestRows;

impl GithubPullRequestRepository for NoPullRequestRows {
    type Err = std::convert::Infallible;

    async fn github_key_for(
        &self,
        _repository_id: i64,
        _number: i64,
    ) -> Result<Option<String>, Self::Err> {
        Ok(None)
    }

    async fn upsert_row(&self, _row: &GithubPullRequestWrite) -> Result<(), Self::Err> {
        Ok(())
    }

    async fn rename_row(&self, _from: &str, _to: &str) -> Result<(), Self::Err> {
        Ok(())
    }

    async fn pull_request_row(
        &self,
        _github_key: &str,
    ) -> Result<Option<GithubPullRequestRow>, Self::Err> {
        Ok(None)
    }
}

fn pull_request_service(
    foreign_entity_service: &StubForeignEntityService,
) -> Arc<TestPullRequestService> {
    Arc::new(GithubPullRequestServiceImpl::new(
        foreign_entity_service.clone(),
        NoPullRequestRows,
    ))
}

fn make_sync_service() -> TestGithubSyncService {
    make_sync_service_with_doc_service().0
}

fn make_sync_service_with_repo(repo: StubSyncRepo) -> TestGithubSyncService {
    make_sync_service_with_repo_and_notification_ingress(repo, StubNotificationIngress::new())
}

fn make_sync_service_with_repo_and_notification_ingress(
    repo: StubSyncRepo,
    notification_ingress: StubNotificationIngress,
) -> TestGithubSyncService {
    let doc_service = Arc::new(StubDocumentService::new());
    let foreign_entity_service = StubForeignEntityService::new();

    let service = GithubSyncServiceImpl::new(
        GithubSyncConfig {
            webhook_secret: "test-webhook-secret".to_string(),
            github_sync_app_url: "https://github.com/apps/test/installations/new?existing=1"
                .to_string(),
            sync_app_pem: TEST_PEM.to_string(),
            sync_app_client_id: "test-sync-app-client-id".to_string(),
            sync_app_client_secret: "test-sync-app-client-secret".to_string(),
            installation_state_secret: "test-installation-state-secret".to_string(),
        },
        doc_service,
        pull_request_service(&foreign_entity_service),
        notification_ingress,
        repo,
        StubSyncClient::new(),
        StubRealtime::default(),
    );
    TestGithubSyncService {
        service,
        foreign_entity_service,
    }
}

fn make_sync_service_with_doc_service() -> (TestGithubSyncService, Arc<StubDocumentService>) {
    make_sync_service_with_repo_and_doc_service(StubSyncRepo::new())
}

fn make_sync_service_with_repo_and_doc_service(
    repo: StubSyncRepo,
) -> (TestGithubSyncService, Arc<StubDocumentService>) {
    let doc_service = Arc::new(StubDocumentService::new());
    let foreign_entity_service = StubForeignEntityService::new();

    let service = GithubSyncServiceImpl::new(
        GithubSyncConfig {
            webhook_secret: "test-webhook-secret".to_string(),
            github_sync_app_url: "https://github.com/apps/test/installations/new?existing=1"
                .to_string(),
            sync_app_pem: TEST_PEM.to_string(),
            sync_app_client_id: "test-sync-app-client-id".to_string(),
            sync_app_client_secret: "test-sync-app-client-secret".to_string(),
            installation_state_secret: "test-installation-state-secret".to_string(),
        },
        doc_service.clone(),
        pull_request_service(&foreign_entity_service),
        StubNotificationIngress::new(),
        repo,
        StubSyncClient::new(),
        StubRealtime::default(),
    );
    (
        TestGithubSyncService {
            service,
            foreign_entity_service,
        },
        doc_service,
    )
}

fn make_sync_service_with_foreign_entity_service() -> TestServiceWithForeignEntityService {
    let repo = StubSyncRepo::new().with_installation_sources(
        "12345",
        vec![GithubAppInstallationSource::Team(
            "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap(),
        )],
    );
    let service = make_sync_service_with_repo(repo);
    let foreign_entity_service = service.foreign_entity_service.clone();

    (service, foreign_entity_service)
}

fn expected_pull_request_metadata(
    title: &str,
    status: GithubPullRequestStatus,
    additions: Option<u64>,
    deletions: Option<u64>,
) -> serde_json::Value {
    serde_json::to_value(EnrichedGithubPullRequest {
        github_key: "my-org/my-repo/pull/42".to_string(),
        owner: "my-org".to_string(),
        repo: "my-repo".to_string(),
        repository_id: None,
        number: 42,
        url: "https://github.com/my-org/my-repo/pull/42".to_string(),
        display_name: "my-org/my-repo#42".to_string(),
        name: Some(title.to_string()),
        status: Some(status),
        additions,
        deletions,
        author_login: None,
        author_id: None,
        description: None,
        comments: None,
        checks: None,
        participant_github_user_ids: None,
        draft: None,
        requested_reviewer_github_user_ids: None,
        github_updated_at: None,
        assignees: None,
        labels: None,
        reviews: None,
        base: None,
        head: Some(GitRef {
            name: Some("feature/some-branch".to_string()),
            sha: None,
        }),
    })
    .unwrap()
}

fn notification_request_content(request: &serde_json::Value) -> &serde_json::Value {
    request
        .pointer("/req/notification/content")
        .expect("notification request content")
}

fn notification_request_recipients(request: &serde_json::Value) -> Vec<String> {
    let mut recipient_ids: Vec<String> = request
        .pointer("/req/recipient_ids")
        .expect("notification recipients")
        .as_array()
        .expect("recipient_ids is an array")
        .iter()
        .map(|value| {
            value
                .as_str()
                .expect("recipient id is a string")
                .to_string()
        })
        .collect();
    recipient_ids.sort();
    recipient_ids
}

fn assert_github_pr_notification_realtime_enabled_apns_disabled(request: &serde_json::Value) {
    assert_github_notification_realtime_enabled_apns_disabled(request, "github_pr_status_changed");
}

fn assert_github_notification_realtime_enabled_apns_disabled(
    request: &serde_json::Value,
    tag: &str,
) {
    assert_eq!(
        request
            .pointer("/req/notification/tag")
            .and_then(|value| value.as_str()),
        Some(tag)
    );
    assert_eq!(
        request
            .pointer("/send_conn_gateway")
            .and_then(|value| value.as_bool()),
        Some(true)
    );
    assert!(
        request
            .pointer("/build_apns")
            .is_none_or(serde_json::Value::is_null),
        "GitHub PR notifications should not include APNS payloads"
    );
}

struct PullRequestWebhookParticipants<'a> {
    author: Option<(u64, &'a str)>,
    requested_reviewers: &'a [(u64, &'a str)],
    assignees: &'a [(u64, &'a str)],
}

impl<'a> PullRequestWebhookParticipants<'a> {
    fn empty() -> Self {
        Self {
            author: None,
            requested_reviewers: &[],
            assignees: &[],
        }
    }
}

fn github_webhook_user(id: u64, login: &str) -> serde_json::Value {
    serde_json::json!({ "id": id, "login": login })
}

fn github_webhook_users(users: &[(u64, &str)]) -> serde_json::Value {
    serde_json::Value::Array(
        users
            .iter()
            .map(|(id, login)| github_webhook_user(*id, *login))
            .collect(),
    )
}

fn notification_pull_request_event(
    action: &str,
    title: &str,
    state: &str,
    merged: bool,
    merged_at: Option<&str>,
    sender_id: u64,
    sender_login: &str,
) -> ValidatedGithubWebhookEvent {
    notification_pull_request_event_with_participants(
        action,
        title,
        state,
        merged,
        merged_at,
        sender_id,
        sender_login,
        PullRequestWebhookParticipants::empty(),
    )
}

fn notification_pull_request_event_with_participants(
    action: &str,
    title: &str,
    state: &str,
    merged: bool,
    merged_at: Option<&str>,
    sender_id: u64,
    sender_login: &str,
    participants: PullRequestWebhookParticipants<'_>,
) -> ValidatedGithubWebhookEvent {
    let mut pull_request = serde_json::json!({
        "number": 42,
        "title": title,
        "body": null,
        "head": { "ref": "feature/some-branch" },
        "base": { "ref": "main" },
        "state": state,
        "merged": merged,
        "merged_at": merged_at,
        "additions": 10,
        "deletions": 2
    });

    if let Some((id, login)) = participants.author {
        pull_request["user"] = github_webhook_user(id, login);
    }
    if !participants.requested_reviewers.is_empty() {
        pull_request["requested_reviewers"] =
            github_webhook_users(participants.requested_reviewers);
    }
    if !participants.assignees.is_empty() {
        pull_request["assignees"] = github_webhook_users(participants.assignees);
    }

    ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": action,
            "pull_request": pull_request,
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 },
            "sender": {
                "login": sender_login,
                "id": sender_id,
                "avatar_url": format!("https://avatars.example/{sender_login}.png")
            }
        }),
    )
}

fn backfilled_pull_request(title: &str) -> EnrichedGithubPullRequest {
    EnrichedGithubPullRequest {
        github_key: "my-org/my-repo/pull/42".to_string(),
        owner: "my-org".to_string(),
        repo: "my-repo".to_string(),
        repository_id: None,
        number: 42,
        url: "https://github.com/my-org/my-repo/pull/42".to_string(),
        display_name: "my-org/my-repo#42".to_string(),
        name: Some(title.to_string()),
        status: Some(GithubPullRequestStatus::Open),
        additions: None,
        deletions: None,
        author_login: None,
        author_id: None,
        description: None,
        comments: None,
        checks: None,
        participant_github_user_ids: None,
        draft: None,
        requested_reviewer_github_user_ids: None,
        github_updated_at: None,
        assignees: None,
        labels: None,
        reviews: None,
        base: None,
        head: None,
    }
}

fn expected_pull_request_metadata_from_details(
    details: &GithubPullRequestDetails,
) -> serde_json::Value {
    serde_json::to_value(EnrichedGithubPullRequest {
        github_key: "my-org/my-repo/pull/42".to_string(),
        owner: "my-org".to_string(),
        repo: "my-repo".to_string(),
        repository_id: None,
        number: 42,
        url: "https://github.com/my-org/my-repo/pull/42".to_string(),
        display_name: "my-org/my-repo#42".to_string(),
        name: Some(details.title.clone()),
        status: Some(details.status()),
        additions: Some(details.additions),
        deletions: Some(details.deletions),
        author_login: details.author_login.clone(),
        author_id: details.author_id,
        description: details.description.clone(),
        comments: details.comments.clone(),
        checks: details.checks.clone(),
        participant_github_user_ids: details.participant_github_user_ids.clone(),
        draft: None,
        requested_reviewer_github_user_ids: None,
        github_updated_at: None,
        assignees: None,
        labels: None,
        reviews: None,
        base: None,
        // The fixture's webhook supplies the head even when live details omit it.
        head: Some(GitRef {
            name: Some("feature/some-branch".to_string()),
            sha: None,
        }),
    })
    .unwrap()
}

fn pull_request_comment(id: u64, body: &str, source: &str) -> GithubPullRequestComment {
    GithubPullRequestComment {
        id,
        body: body.to_string(),
        author_id: None,
        author_login: Some("octocat".to_string()),
        author_association: Some("MEMBER".to_string()),
        url: Some(format!(
            "https://github.com/my-org/my-repo/pull/42#comment-{id}"
        )),
        created_at: None,
        updated_at: None,
        source: source.to_string(),
        in_reply_to_id: None,
        pull_request_review_id: None,
        path: None,
        line: None,
        original_line: None,
    }
}

fn pull_request_check_run(id: u64, name: &str, status: &str) -> GithubPullRequestCheckRun {
    GithubPullRequestCheckRun {
        id,
        name: name.to_string(),
        status: status.to_string(),
        conclusion: Some("success".to_string()),
        url: Some(format!("https://github.com/my-org/my-repo/runs/{id}")),
        started_at: None,
        completed_at: None,
    }
}

fn pull_request_details(
    title: &str,
    additions: u64,
    deletions: u64,
    comments: Option<Vec<GithubPullRequestComment>>,
    checks: Option<Vec<GithubPullRequestCheckRun>>,
) -> GithubPullRequestDetails {
    GithubPullRequestDetails {
        title: title.to_string(),
        state: "open".to_string(),
        repository_id: None,
        merged_at: None,
        additions,
        deletions,
        author_login: Some("octocat".to_string()),
        author_id: Some(583231),
        description: Some("Detailed pull request description".to_string()),
        comments,
        checks,
        participant_github_user_ids: None,
        draft: None,
        requested_reviewer_github_user_ids: None,
        github_updated_at: None,
        assignees: None,
        labels: None,
        reviews: None,
        base: None,
        head: None,
    }
}

fn seed_pull_request_details_with_participants(
    service: &TestGithubSyncService,
    participant_github_user_ids: &[&str],
) {
    let mut details = pull_request_details("Add GitHub notifications", 10, 2, None, None);
    details.participant_github_user_ids = Some(
        participant_github_user_ids
            .iter()
            .map(|id| (*id).to_string())
            .collect(),
    );
    service
        .client
        .set_pull_request_details("my-org", "my-repo", 42, details);
}

fn notification_check_run_event(
    action: &str,
    status: &str,
    conclusion: Option<&str>,
    pull_number: Option<u64>,
    sender_id: u64,
    sender_login: &str,
    sender_type: &str,
) -> ValidatedGithubWebhookEvent {
    let pull_requests = pull_number
        .map(|number| serde_json::json!([{ "number": number }]))
        .unwrap_or_else(|| serde_json::json!([]));
    let mut check_run = serde_json::json!({
        "id": 987_654_321,
        "name": "CI / tests",
        "status": status,
        "html_url": "https://github.com/my-org/my-repo/runs/987654321",
        "completed_at": "2026-05-25T19:01:02Z",
        "pull_requests": pull_requests,
    });
    if let Some(conclusion) = conclusion {
        check_run["conclusion"] = serde_json::json!(conclusion);
    }

    ValidatedGithubWebhookEvent::new(
        "check_run".to_string(),
        serde_json::json!({
            "action": action,
            "check_run": check_run,
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 },
            "sender": {
                "login": sender_login,
                "id": sender_id,
                "type": sender_type,
                "avatar_url": format!("https://avatars.example/{sender_login}.png")
            }
        }),
    )
}

#[tokio::test]
async fn pr_with_task_id_in_title() {
    let service = make_sync_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" }
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    let result = service.process_webhook_event(&event).await;
    assert!(result.is_ok());

    let comments = service.client.pr_comments();
    assert_eq!(comments.len(), 1);
    assert_eq!(comments[0].owner, "my-org");
    assert_eq!(comments[0].repo, "my-repo");
    assert_eq!(comments[0].pull_number, 42);
    assert_eq!(
        comments[0].body,
        format!("[My Task](https://macro.com/app/task/{KNOWN_TASK_UUID})")
    );
}

#[tokio::test]
async fn pr_with_task_id_in_branch_name() {
    let service = make_sync_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 7,
                "title": "some feature",
                "body": "no task ids here",
                "head": { "ref": "macro-2BuyvtY3aeEvHx4uG8iD51" }
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    let result = service.process_webhook_event(&event).await;
    assert!(result.is_ok());

    let comments = service.client.pr_comments();
    assert_eq!(comments.len(), 1);
    assert_eq!(comments[0].pull_number, 7);
}

#[tokio::test]
async fn pr_with_team_task_id_in_branch_name() {
    let task_id = MacroTaskId::from_uuid(&uuid::Uuid::parse_str(KNOWN_TASK_UUID).unwrap());
    let team_id = uuid::Uuid::parse_str("dddddddd-dddd-dddd-dddd-dddddddddddd").unwrap();
    let repo = StubSyncRepo::new().with_team_task_reference("12345", "eng", 123, team_id, task_id);
    let service = make_sync_service_with_repo(repo);

    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 7,
                "title": "some feature",
                "body": "no legacy task ids here",
                "head": { "ref": "whutch/eng-123-fix-some-bug" }
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    let result = service.process_webhook_event(&event).await;
    assert!(result.is_ok());

    let comments = service.client.pr_comments();
    assert_eq!(comments.len(), 1);
    assert_eq!(comments[0].pull_number, 7);
    assert_eq!(
        comments[0].body,
        format!("[My Task](https://macro.com/app/task/{KNOWN_TASK_UUID})")
    );
}

#[tokio::test]
async fn team_task_id_requires_installation_team_match() {
    let task_id = MacroTaskId::from_uuid(&uuid::Uuid::parse_str(KNOWN_TASK_UUID).unwrap());
    let team_id = uuid::Uuid::parse_str("dddddddd-dddd-dddd-dddd-dddddddddddd").unwrap();
    let repo = StubSyncRepo::new().with_team_task_reference("99999", "eng", 123, team_id, task_id);
    let service = make_sync_service_with_repo(repo);

    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 7,
                "title": "some feature",
                "body": null,
                "head": { "ref": "whutch/eng-123-fix-some-bug" }
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    let result = service.process_webhook_event(&event).await;
    assert!(result.is_ok());
    assert!(service.client.pr_comments().is_empty());
}

// Regression: a PR body/review quoting the Tailwind class `py-6` in
// markdown code must not link team PY's task 6.
#[tokio::test]
async fn team_task_reference_inside_markdown_code_links_nothing() {
    let task_id = MacroTaskId::from_uuid(&uuid::Uuid::parse_str(KNOWN_TASK_UUID).unwrap());
    let team_id = uuid::Uuid::parse_str("eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee").unwrap();
    let repo = StubSyncRepo::new().with_team_task_reference("12345", "py", 6, team_id, task_id);
    let service = make_sync_service_with_repo(repo);

    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 205,
                "title": "fix: adjust panel padding",
                "body": "Swap `pt-4` for `py-6` on the panel container.\n```tsx\n<aside class=\"py-6 px-4\">\n```",
                "head": { "ref": "someuser/adjust-panel-padding" }
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    let result = service.process_webhook_event(&event).await;
    assert!(result.is_ok());

    assert!(service.client.pr_comments().is_empty());
    let tracked = service
        .repo
        .get_task_ids(GithubKey::new("my-org", "my-repo", 205))
        .await
        .unwrap();
    assert!(tracked.is_empty());
}

#[tokio::test]
async fn ambiguous_team_task_reference_links_nothing() {
    // Two of the installation's teams share the slug "eng" (slugs are not
    // unique), so "eng-123" matches a different task in each team. Linking
    // either would risk attributing the PR to the wrong team's task, so the
    // reference must be skipped entirely.
    let task_a = MacroTaskId::from_uuid(&uuid::Uuid::parse_str(KNOWN_TASK_UUID).unwrap());
    let task_b = MacroTaskId::from_uuid(
        &uuid::Uuid::parse_str("11111111-1111-1111-1111-111111111111").unwrap(),
    );
    let team_a = uuid::Uuid::parse_str("dddddddd-dddd-dddd-dddd-dddddddddddd").unwrap();
    let team_b = uuid::Uuid::parse_str("eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee").unwrap();
    let repo = StubSyncRepo::new()
        .with_team_task_reference("12345", "eng", 123, team_a, task_a)
        .with_team_task_reference("12345", "eng", 123, team_b, task_b);
    let service = make_sync_service_with_repo(repo);

    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 7,
                "title": "some feature",
                "body": null,
                "head": { "ref": "whutch/eng-123-fix-some-bug" }
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    let result = service.process_webhook_event(&event).await;
    assert!(result.is_ok());

    assert!(service.client.pr_comments().is_empty());
    let tracked = service
        .repo
        .get_task_ids(GithubKey::new("my-org", "my-repo", 7))
        .await
        .unwrap();
    assert!(tracked.is_empty());
}

#[tokio::test]
async fn issue_comment_with_task_id() {
    let service = make_sync_service();
    let event = ValidatedGithubWebhookEvent::new(
        "issue_comment".to_string(),
        serde_json::json!({
            "action": "created",
            "issue": {
                "number": 99,
                "title": "some issue",
                "body": null,
                "head": { "ref": "main" }
            },
            "comment": {
                "body": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51"
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    let result = service.process_webhook_event(&event).await;
    assert!(result.is_ok());

    let comments = service.client.pr_comments();
    assert_eq!(comments.len(), 1);
    assert_eq!(comments[0].pull_number, 99);
}

#[tokio::test]
async fn event_with_no_task_ids() {
    let service = make_sync_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "title": "just a normal PR",
                "body": "nothing special",
                "head": { "ref": "feature/no-task-id" }
            }
        }),
    );

    let result = service.process_webhook_event(&event).await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn unknown_event_type_skipped() {
    let service = make_sync_service();
    let event = ValidatedGithubWebhookEvent::new(
        "ping".to_string(),
        serde_json::json!({"zen": "Keep it logically awesome."}),
    );

    let result = service.process_webhook_event(&event).await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn multiple_task_ids_in_one_event() {
    let service = make_sync_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "title": "closes MACRO-abc123",
                "body": "also relates to MACRO-def456 and MACRO-ghi789",
                "head": { "ref": "main" }
            }
        }),
    );

    let result = service.process_webhook_event(&event).await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn pull_request_review_with_task_id() {
    let service = make_sync_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request_review".to_string(),
        serde_json::json!({
            "action": "submitted",
            "pull_request": {
                "number": 10,
                "title": "some PR",
                "body": null,
                "head": { "ref": "main" }
            },
            "review": {
                "body": "Approved, relates to MACRO-2BuyvtY3aeEvHx4uG8iD51"
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    let result = service.process_webhook_event(&event).await;
    assert!(result.is_ok());

    let comments = service.client.pr_comments();
    assert_eq!(comments.len(), 1);
    assert_eq!(comments[0].pull_number, 10);
}

#[tokio::test]
async fn pull_request_review_comment_with_task_id() {
    let service = make_sync_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request_review_comment".to_string(),
        serde_json::json!({
            "action": "created",
            "comment": {
                "body": "This line is related to MACRO-abc123"
            }
        }),
    );

    let result = service.process_webhook_event(&event).await;
    assert!(result.is_ok());
}

// ---------------------------------------------------------------------------
// Deduplication: repo tracks tasks already associated with a PR
// ---------------------------------------------------------------------------

#[tokio::test]
async fn duplicate_comment_not_posted_when_task_already_tracked() {
    let service = make_sync_service();

    let make_event = || {
        ValidatedGithubWebhookEvent::new(
            "pull_request".to_string(),
            serde_json::json!({
                "action": "opened",
                "pull_request": {
                    "number": 42,
                    "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                    "body": null,
                    "head": { "ref": "feature/some-branch" }
                },
                "repository": {
                    "name": "my-repo",
                    "owner": { "login": "my-org" }
                },
                "installation": { "id": 12345 }
            }),
        )
    };

    // First event — comment should be posted
    let event = make_event();
    service.process_webhook_event(&event).await.unwrap();
    assert_eq!(service.client.pr_comments().len(), 1);

    // Second event with same task ID — should NOT post a duplicate
    let event = make_event();
    service.process_webhook_event(&event).await.unwrap();
    assert_eq!(service.client.pr_comments().len(), 1);
}

// ---------------------------------------------------------------------------
// Deduplication: comment mentions task ID already in PR context
// ---------------------------------------------------------------------------

#[tokio::test]
async fn issue_comment_duplicate_task_id_skipped() {
    let service = make_sync_service();

    // First, open the PR with the task ID to populate the repo
    let pr_event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 99,
                "title": "fixes MACRO-abc123",
                "body": null,
                "head": { "ref": "main" }
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );
    service.process_webhook_event(&pr_event).await.unwrap();

    // Comment mentions the same task ID — should be skipped
    let comment_event = ValidatedGithubWebhookEvent::new(
        "issue_comment".to_string(),
        serde_json::json!({
            "action": "created",
            "issue": {
                "number": 99,
                "title": "fixes MACRO-abc123",
                "body": null,
                "head": { "ref": "main" }
            },
            "comment": {
                "body": "Fixes MACRO-abc123"
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    let result = service.process_webhook_event(&comment_event).await;
    assert!(result.is_ok());
    // No additional comment posted (PR open posted one, comment should not)
    assert_eq!(service.client.pr_comments().len(), 0);
}

#[tokio::test]
async fn issue_comment_new_task_id_not_skipped() {
    let service = make_sync_service();
    // Comment introduces a new task ID not previously tracked
    let event = ValidatedGithubWebhookEvent::new(
        "issue_comment".to_string(),
        serde_json::json!({
            "action": "created",
            "issue": {
                "title": "fixes MACRO-abc123",
                "body": null,
                "head": { "ref": "main" }
            },
            "comment": {
                "body": "Also fixes MACRO-def456"
            }
        }),
    );

    let result = service.process_webhook_event(&event).await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn review_duplicate_task_id_skipped_via_pr_context() {
    let service = make_sync_service();
    // PR title already has the task ID. The comment handler upserts PR context
    // tasks, so the review body's mention is considered a duplicate.
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request_review".to_string(),
        serde_json::json!({
            "action": "submitted",
            "pull_request": {
                "title": "MACRO-abc123 fix",
                "body": null,
                "head": { "ref": "main" }
            },
            "review": {
                "body": "Approved, relates to MACRO-abc123"
            }
        }),
    );

    let result = service.process_webhook_event(&event).await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn review_comment_mixed_new_and_duplicate() {
    let service = make_sync_service();
    // PR has MACRO-abc123 in branch (will be upserted as PR context),
    // comment mentions both abc123 (dup via context) and def456 (new)
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request_review_comment".to_string(),
        serde_json::json!({
            "action": "created",
            "pull_request": {
                "title": "some fix",
                "body": null,
                "head": { "ref": "feature/macro-abc123" }
            },
            "comment": {
                "body": "Relates to MACRO-abc123 and MACRO-def456"
            }
        }),
    );

    let result = service.process_webhook_event(&event).await;
    assert!(result.is_ok());
}

// ---------------------------------------------------------------------------
// Task status updates based on PR action
// ---------------------------------------------------------------------------

#[tokio::test]
async fn pr_opened_sets_task_status_in_review() {
    let (service, doc_service) = make_sync_service_with_doc_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "merged": false
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let status_calls = doc_service.task_status_calls();
    assert_eq!(status_calls.len(), 1);
    assert_eq!(status_calls[0].entity_id, KNOWN_TASK_UUID);
    assert_eq!(status_calls[0].status, "In Review");
}

#[tokio::test]
async fn pr_merged_sets_task_status_completed() {
    let (service, doc_service) = make_sync_service_with_doc_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "closed",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "merged": true
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let status_calls = doc_service.task_status_calls();
    assert_eq!(status_calls.len(), 1);
    assert_eq!(status_calls[0].entity_id, KNOWN_TASK_UUID);
    assert_eq!(status_calls[0].status, "Completed");
}

#[tokio::test]
async fn pr_merged_credits_the_linked_macro_user_who_merged() {
    let (service, doc_service) = make_sync_service_with_repo_and_doc_service(
        StubSyncRepo::new().with_github_link("333", "macro|[email protected]"),
    );
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "closed",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "merged": true
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 },
            "sender": { "login": "monalisa", "id": 333 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let status_calls = doc_service.task_status_calls();
    assert_eq!(status_calls.len(), 1);
    assert_eq!(status_calls[0].status, "Completed");
    assert_eq!(
        status_calls[0]
            .credited_user
            .as_ref()
            .map(ToString::to_string),
        Some("macro|[email protected]".to_string())
    );
}

#[tokio::test]
async fn pr_merged_by_an_unlinked_github_account_is_not_credited() {
    let (service, doc_service) = make_sync_service_with_repo_and_doc_service(
        StubSyncRepo::new().with_github_link("333", "macro|[email protected]"),
    );
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "closed",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "merged": true
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 },
            "sender": { "login": "someone-else", "id": 444 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let status_calls = doc_service.task_status_calls();
    assert_eq!(status_calls.len(), 1);
    assert_eq!(status_calls[0].status, "Completed");
    assert!(status_calls[0].credited_user.is_none());
}

#[tokio::test]
async fn pr_merged_by_an_unlinked_account_records_who_merged() {
    let (service, doc_service) = make_sync_service_with_doc_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "closed",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "merged": true,
                "html_url": "https://github.com/my-org/my-repo/pull/42",
                "merged_by": { "login": "someone-else", "id": 444 }
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 },
            "sender": { "login": "someone-else", "id": 444 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let status_calls = doc_service.task_status_calls();
    assert_eq!(status_calls.len(), 1);
    assert!(status_calls[0].credited_user.is_none());
    assert_eq!(
        status_calls[0].source,
        Some(PropertyChangeSource::GithubPullRequest {
            url: "https://github.com/my-org/my-repo/pull/42".to_string(),
            account: GithubAccount {
                id: "444".to_string(),
                login: "someone-else".to_string(),
            },
        })
    );
}

#[tokio::test]
async fn pr_closed_without_merge_sets_task_status_todo() {
    let (service, doc_service) = make_sync_service_with_doc_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "closed",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "merged": false
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let status_calls = doc_service.task_status_calls();
    assert_eq!(status_calls.len(), 1);
    assert_eq!(status_calls[0].entity_id, KNOWN_TASK_UUID);
    assert_eq!(status_calls[0].status, "Not Started");
}

#[tokio::test]
async fn pr_closed_without_merge_sets_previously_tracked_task_status_todo() {
    let (service, doc_service) = make_sync_service_with_doc_service();

    let opened_event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "merged": false
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );
    service.process_webhook_event(&opened_event).await.unwrap();

    let closed_event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "closed",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "merged": false
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );
    service.process_webhook_event(&closed_event).await.unwrap();

    let status_calls = doc_service.task_status_calls();
    assert_eq!(status_calls.len(), 2);
    assert_eq!(status_calls[0].entity_id, KNOWN_TASK_UUID);
    assert_eq!(status_calls[0].status, "In Review");
    assert_eq!(status_calls[1].entity_id, KNOWN_TASK_UUID);
    assert_eq!(status_calls[1].status, "Not Started");
}

#[tokio::test]
async fn issue_comment_on_open_pr_sets_task_status_in_review() {
    let (service, doc_service) = make_sync_service_with_doc_service();
    let event = ValidatedGithubWebhookEvent::new(
        "issue_comment".to_string(),
        serde_json::json!({
            "action": "created",
            "issue": {
                "number": 99,
                "title": "some issue",
                "body": null,
                "state": "open",
                "head": { "ref": "main" }
            },
            "comment": {
                "body": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51"
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let status_calls = doc_service.task_status_calls();
    assert_eq!(status_calls.len(), 1);
    assert_eq!(status_calls[0].entity_id, KNOWN_TASK_UUID);
    assert_eq!(status_calls[0].status, "In Review");
}

#[tokio::test]
async fn issue_comment_on_closed_pr_does_not_update_task_status() {
    let (service, doc_service) = make_sync_service_with_doc_service();
    let event = ValidatedGithubWebhookEvent::new(
        "issue_comment".to_string(),
        serde_json::json!({
            "action": "created",
            "issue": {
                "number": 99,
                "title": "some issue",
                "body": null,
                "state": "closed",
                "head": { "ref": "main" }
            },
            "comment": {
                "body": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51"
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let status_calls = doc_service.task_status_calls();
    assert!(
        status_calls.is_empty(),
        "issue_comment on closed PR should not update task status"
    );
}

#[tokio::test]
async fn pr_merged_updates_status_even_when_already_tracked() {
    let (service, doc_service) = make_sync_service_with_doc_service();

    // First event: PR opened — posts comment and sets "In Review"
    let opened_event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "merged": false
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );
    service.process_webhook_event(&opened_event).await.unwrap();
    assert_eq!(service.client.pr_comments().len(), 1);
    assert_eq!(doc_service.task_status_calls().len(), 1);
    assert_eq!(doc_service.task_status_calls()[0].status, "In Review");

    // Second event: PR merged — should NOT post a duplicate comment,
    // but SHOULD update status to "Completed"
    let merged_event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "closed",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "merged": true
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );
    service.process_webhook_event(&merged_event).await.unwrap();

    // Still only 1 comment (no duplicate)
    assert_eq!(service.client.pr_comments().len(), 1);

    // But status was updated twice: "In Review" then "Completed"
    let status_calls = doc_service.task_status_calls();
    assert_eq!(status_calls.len(), 2);
    assert_eq!(status_calls[1].entity_id, KNOWN_TASK_UUID);
    assert_eq!(status_calls[1].status, "Completed");
}

// ---------------------------------------------------------------------------
// PR foreign entity upserts
// ---------------------------------------------------------------------------

#[tokio::test]
async fn pr_opened_upserts_foreign_entity_for_installation_source() {
    let (service, foreign_entity_service) = make_sync_service_with_foreign_entity_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "state": "open",
                "merged": false,
                "additions": 10,
                "deletions": 2
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let foreign_entities = foreign_entity_service.foreign_entities();
    assert_eq!(foreign_entities.len(), 1);

    let foreign_entity = &foreign_entities[0];
    assert_eq!(foreign_entity.foreign_entity_id, "my-org/my-repo/pull/42");
    assert_eq!(
        foreign_entity.foreign_entity_source,
        GITHUB_PULL_REQUEST_FOREIGN_ENTITY_SOURCE
    );
    assert_eq!(
        foreign_entity.stored_for_id,
        "dddddddd-dddd-dddd-dddd-dddddddddddd"
    );
    assert_eq!(foreign_entity.stored_for_auth_entity, "team");
    assert_eq!(
        foreign_entity.metadata,
        expected_pull_request_metadata(
            "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
            GithubPullRequestStatus::Open,
            Some(10),
            Some(2),
        )
    );
    assert_eq!(foreign_entity_service.create_calls().len(), 1);
    assert!(foreign_entity_service.patch_calls().is_empty());
}

#[tokio::test]
async fn pr_opened_upserts_foreign_entity_for_user_installation_source() {
    let repo = StubSyncRepo::new().with_installation_sources(
        "77777",
        vec![GithubAppInstallationSource::User(
            "macro|[email protected]".to_string(),
        )],
    );
    let service = make_sync_service_with_repo(repo);
    let foreign_entity_service = service.foreign_entity_service.clone();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "state": "open",
                "merged": false,
                "additions": 10,
                "deletions": 2
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 77777 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let foreign_entities = foreign_entity_service.foreign_entities();
    assert_eq!(foreign_entities.len(), 1);
    assert_eq!(foreign_entities[0].stored_for_id, "macro|[email protected]");
    assert_eq!(foreign_entities[0].stored_for_auth_entity, "user");
}

#[tokio::test]
async fn github_pr_status_changed_opened_team_source_notifies_participant_team_members() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = StubSyncRepo::new()
        .with_installation_sources("12345", vec![GithubAppInstallationSource::Team(team_id)])
        .with_team_members(
            team_id,
            vec![
                "macro|[email protected]",
                "macro|[email protected]",
                "macro|[email protected]",
            ],
        )
        .with_github_link("111", "macro|[email protected]")
        .with_github_link("222", "macro|[email protected]")
        .with_github_link("333", "macro|[email protected]")
        .with_github_link("444", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_pull_request_event_with_participants(
        "opened",
        "Add GitHub notifications",
        "open",
        false,
        None,
        222,
        "octocat",
        PullRequestWebhookParticipants {
            author: Some((111, "author-gh")),
            requested_reviewers: &[(222, "alice-gh")],
            assignees: &[(333, "bob-gh")],
        },
    );

    service.process_webhook_event(&event).await.unwrap();

    let foreign_entity_id = service.foreign_entity_service.foreign_entities()[0].id;
    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);

    let request = &requests[0];
    assert_github_pr_notification_realtime_enabled_apns_disabled(request);
    assert_eq!(
        request
            .pointer("/req/notification_entity/entity_type")
            .and_then(|value| value.as_str()),
        Some("foreign_entity")
    );
    assert_eq!(
        request
            .pointer("/req/notification_entity/entity_id")
            .and_then(|value| value.as_str()),
        Some(foreign_entity_id.to_string().as_str())
    );
    assert_eq!(
        request
            .pointer("/req/sender_id")
            .and_then(|value| value.as_str()),
        Some("macro|[email protected]")
    );
    // Alice triggered the event, so she is not notified about her own activity.
    assert_eq!(
        notification_request_recipients(request),
        vec!["macro|[email protected]".to_string()]
    );

    let content = notification_request_content(request);
    assert_eq!(
        content
            .get("foreignEntityId")
            .and_then(|value| value.as_str()),
        Some(foreign_entity_id.to_string().as_str())
    );
    assert_eq!(
        content.get("githubKey").and_then(|value| value.as_str()),
        Some("my-org/my-repo/pull/42")
    );
    assert_eq!(
        content.get("owner").and_then(|value| value.as_str()),
        Some("my-org")
    );
    assert_eq!(
        content.get("repo").and_then(|value| value.as_str()),
        Some("my-repo")
    );
    assert_eq!(
        content.get("number").and_then(|value| value.as_u64()),
        Some(42)
    );
    assert_eq!(
        content.get("url").and_then(|value| value.as_str()),
        Some("https://github.com/my-org/my-repo/pull/42")
    );
    assert_eq!(
        content.get("displayName").and_then(|value| value.as_str()),
        Some("my-org/my-repo#42")
    );
    assert_eq!(
        content.get("title").and_then(|value| value.as_str()),
        Some("Add GitHub notifications")
    );
    assert_eq!(
        content.get("status").and_then(|value| value.as_str()),
        Some("open")
    );
    assert_eq!(
        content.get("action").and_then(|value| value.as_str()),
        Some("opened")
    );
    assert!(content.get("previousStatus").unwrap().is_null());
    assert_eq!(
        content
            .get("senderGithubLogin")
            .and_then(|value| value.as_str()),
        Some("octocat")
    );
    assert_eq!(
        content
            .get("senderGithubUserId")
            .and_then(|value| value.as_str()),
        Some("222")
    );
    assert_eq!(
        content
            .get("senderGithubAvatarUrl")
            .and_then(|value| value.as_str()),
        Some("https://avatars.example/octocat.png")
    );
    assert_eq!(
        content.get("headBranch").and_then(|value| value.as_str()),
        Some("feature/some-branch")
    );
    assert_eq!(
        content.get("baseBranch").and_then(|value| value.as_str()),
        Some("main")
    );
    assert!(content.get("mergedAt").unwrap().is_null());
}

#[tokio::test]
async fn github_pr_status_changed_merged_user_source_notifies_participant_user() {
    let repo = StubSyncRepo::new()
        .with_installation_sources(
            "12345",
            vec![GithubAppInstallationSource::User(
                "macro|[email protected]".to_string(),
            )],
        )
        .with_github_link("333", "macro|[email protected]")
        .with_github_link("444", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let opened_event = notification_pull_request_event_with_participants(
        "opened",
        "Add GitHub notifications",
        "open",
        false,
        None,
        333,
        "monalisa",
        PullRequestWebhookParticipants {
            author: None,
            requested_reviewers: &[(444, "reviewer-gh")],
            assignees: &[],
        },
    );
    service.process_webhook_event(&opened_event).await.unwrap();
    service.notification_ingress.clear_requests();

    let merged_event = notification_pull_request_event_with_participants(
        "closed",
        "Add GitHub notifications",
        "closed",
        true,
        Some("2026-05-27T19:00:00Z"),
        333,
        "monalisa",
        PullRequestWebhookParticipants {
            author: None,
            requested_reviewers: &[(444, "reviewer-gh")],
            assignees: &[],
        },
    );
    service.process_webhook_event(&merged_event).await.unwrap();

    let foreign_entity_id = service.foreign_entity_service.foreign_entities()[0].id;
    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);

    let request = &requests[0];
    assert_github_pr_notification_realtime_enabled_apns_disabled(request);
    assert_eq!(
        notification_request_recipients(request),
        vec!["macro|[email protected]".to_string()]
    );
    assert_eq!(
        request
            .pointer("/req/sender_id")
            .and_then(|value| value.as_str()),
        Some("macro|[email protected]")
    );
    assert_eq!(
        request
            .pointer("/req/notification_entity/entity_id")
            .and_then(|value| value.as_str()),
        Some(foreign_entity_id.to_string().as_str())
    );

    let content = notification_request_content(request);
    assert_eq!(
        content
            .get("foreignEntityId")
            .and_then(|value| value.as_str()),
        Some(foreign_entity_id.to_string().as_str())
    );
    assert_eq!(
        content.get("status").and_then(|value| value.as_str()),
        Some("merged")
    );
    assert_eq!(
        content
            .get("previousStatus")
            .and_then(|value| value.as_str()),
        Some("open")
    );
    assert_eq!(
        content.get("action").and_then(|value| value.as_str()),
        Some("closed")
    );
    assert_eq!(
        content.get("mergedAt").and_then(|value| value.as_str()),
        Some("2026-05-27T19:00:00Z")
    );
    assert_eq!(
        content
            .get("senderGithubLogin")
            .and_then(|value| value.as_str()),
        Some("monalisa")
    );
}

#[tokio::test]
async fn github_pr_status_changed_user_source_does_not_notify_nonparticipant() {
    let repo = StubSyncRepo::new()
        .with_installation_sources(
            "12345",
            vec![GithubAppInstallationSource::User(
                "macro|[email protected]".to_string(),
            )],
        )
        .with_github_link("222", "macro|[email protected]")
        .with_github_link("333", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_pull_request_event_with_participants(
        "opened",
        "Add GitHub notifications",
        "open",
        false,
        None,
        222,
        "octocat",
        PullRequestWebhookParticipants {
            author: Some((222, "octocat")),
            requested_reviewers: &[],
            assignees: &[],
        },
    );

    service.process_webhook_event(&event).await.unwrap();

    assert_eq!(service.foreign_entity_service.foreign_entities().len(), 1);
    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn github_pr_status_changed_does_not_notify_any_macro_user_linked_to_actor() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = StubSyncRepo::new()
        .with_installation_sources("12345", vec![GithubAppInstallationSource::Team(team_id)])
        .with_team_members(
            team_id,
            vec![
                "macro|[email protected]",
                "macro|[email protected]",
                "macro|[email protected]",
            ],
        )
        .with_github_link("222", "macro|[email protected]")
        .with_github_link("222", "macro|[email protected]")
        .with_github_link("333", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_pull_request_event_with_participants(
        "opened",
        "Add GitHub notifications",
        "open",
        false,
        None,
        222,
        "octocat",
        PullRequestWebhookParticipants {
            author: Some((222, "octocat")),
            requested_reviewers: &[(333, "bob-gh")],
            assignees: &[],
        },
    );

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);
    // The actor's GitHub account is linked to two Macro users; both are
    // excluded from recipients, not just the attributed sender.
    assert_eq!(
        requests[0]
            .pointer("/req/sender_id")
            .and_then(|value| value.as_str()),
        Some("macro|[email protected]")
    );
    assert_eq!(
        notification_request_recipients(&requests[0]),
        vec!["macro|[email protected]".to_string()]
    );
}

#[tokio::test]
async fn github_pr_status_changed_actor_as_only_participant_does_not_notify() {
    let repo = StubSyncRepo::new()
        .with_installation_sources(
            "12345",
            vec![GithubAppInstallationSource::User(
                "macro|[email protected]".to_string(),
            )],
        )
        .with_github_link("222", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_pull_request_event_with_participants(
        "opened",
        "Add GitHub notifications",
        "open",
        false,
        None,
        222,
        "octocat",
        PullRequestWebhookParticipants {
            author: Some((222, "octocat")),
            requested_reviewers: &[],
            assignees: &[],
        },
    );

    service.process_webhook_event(&event).await.unwrap();

    assert_eq!(service.foreign_entity_service.foreign_entities().len(), 1);
    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn github_pr_status_changed_missing_participants_does_not_notify_team_members() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = StubSyncRepo::new()
        .with_installation_sources("12345", vec![GithubAppInstallationSource::Team(team_id)])
        .with_team_members(team_id, vec!["macro|[email protected]", "macro|[email protected]"])
        .with_github_link("222", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_pull_request_event(
        "opened",
        "Add GitHub notifications",
        "open",
        false,
        None,
        222,
        "octocat",
    );

    service.process_webhook_event(&event).await.unwrap();

    assert_eq!(service.foreign_entity_service.foreign_entities().len(), 1);
    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn github_pr_status_changed_edited_does_not_notify() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = StubSyncRepo::new()
        .with_installation_sources("12345", vec![GithubAppInstallationSource::Team(team_id)])
        .with_team_members(team_id, vec!["macro|[email protected]"]);
    let service = make_sync_service_with_repo(repo);
    let event = notification_pull_request_event(
        "edited",
        "Update title",
        "open",
        false,
        None,
        222,
        "octocat",
    );

    service.process_webhook_event(&event).await.unwrap();

    assert_eq!(service.foreign_entity_service.foreign_entities().len(), 1);
    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn authenticated_installation_setup_backfill_does_not_notify() {
    let service = make_setup_sync_service();
    service.client.set_user_installations(&[12345]);
    service
        .client
        .set_open_pull_requests(vec![backfilled_pull_request("Backfilled PR")]);

    service
        .complete_installation_setup(
            &installation_setup_state(None, chrono::Utc::now().timestamp() + 60),
            Some("setup-code"),
            Some(12345),
            "install",
        )
        .await
        .unwrap();

    assert_eq!(service.foreign_entity_service.foreign_entities().len(), 1);
    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn github_pr_status_changed_unchanged_status_does_not_notify() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = StubSyncRepo::new()
        .with_installation_sources("12345", vec![GithubAppInstallationSource::Team(team_id)])
        .with_team_members(team_id, vec!["macro|[email protected]"]);
    let service = make_sync_service_with_repo(repo);
    let event = notification_pull_request_event(
        "opened",
        "Add GitHub notifications",
        "open",
        false,
        None,
        222,
        "octocat",
    );

    service.process_webhook_event(&event).await.unwrap();
    service.notification_ingress.clear_requests();
    service.process_webhook_event(&event).await.unwrap();

    assert_eq!(service.foreign_entity_service.foreign_entities().len(), 1);
    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn github_pr_status_changed_send_failure_does_not_fail_webhook_processing() {
    let repo = StubSyncRepo::new()
        .with_installation_sources(
            "12345",
            vec![GithubAppInstallationSource::User(
                "macro|[email protected]".to_string(),
            )],
        )
        .with_github_link("222", "macro|[email protected]");
    let service = make_sync_service_with_repo_and_notification_ingress(
        repo,
        StubNotificationIngress::failing(),
    );
    let event = notification_pull_request_event_with_participants(
        "opened",
        "Add GitHub notifications",
        "open",
        false,
        None,
        333,
        "other-dev",
        PullRequestWebhookParticipants {
            author: Some((222, "octocat")),
            requested_reviewers: &[],
            assignees: &[],
        },
    );

    let result = service.process_webhook_event(&event).await;

    assert!(result.is_ok());
    assert_eq!(service.foreign_entity_service.foreign_entities().len(), 1);

    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);
    assert_github_pr_notification_realtime_enabled_apns_disabled(&requests[0]);
}

#[tokio::test]
async fn github_pr_check_run_success_notifies_participant_team_members_from_bot_sender() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = StubSyncRepo::new()
        .with_installation_sources("12345", vec![GithubAppInstallationSource::Team(team_id)])
        .with_team_members(
            team_id,
            vec![
                "macro|[email protected]",
                "macro|[email protected]",
                "macro|[email protected]",
            ],
        )
        .with_github_link("222", "macro|[email protected]")
        .with_github_link("333", "macro|[email protected]")
        .with_github_link("444", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    seed_pull_request_details_with_participants(&service, &["333", "444", "999"]);
    let event = notification_check_run_event(
        "completed",
        "completed",
        Some("success"),
        Some(42),
        222,
        "macro-app",
        "Bot",
    );

    service.process_webhook_event(&event).await.unwrap();

    let foreign_entity_id = service.foreign_entity_service.foreign_entities()[0].id;
    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);

    let request = &requests[0];
    assert_github_notification_realtime_enabled_apns_disabled(request, "github_pr_check_run");
    assert_eq!(
        notification_request_recipients(request),
        vec![
            "macro|[email protected]".to_string(),
            "macro|[email protected]".to_string(),
        ]
    );
    assert_eq!(
        request
            .pointer("/req/sender_id")
            .and_then(|value| value.as_str()),
        Some("macro|[email protected]")
    );

    let content = notification_request_content(request);
    assert_eq!(
        content
            .get("foreignEntityId")
            .and_then(|value| value.as_str()),
        Some(foreign_entity_id.to_string().as_str())
    );
    assert_eq!(
        content.get("githubKey").and_then(|value| value.as_str()),
        Some("my-org/my-repo/pull/42")
    );
    assert_eq!(
        content.get("title").and_then(|value| value.as_str()),
        Some("Add GitHub notifications")
    );
    assert_eq!(
        content
            .get("checkRunGithubId")
            .and_then(|value| value.as_u64()),
        Some(987_654_321)
    );
    assert_eq!(
        content.get("checkName").and_then(|value| value.as_str()),
        Some("CI / tests")
    );
    assert_eq!(
        content.get("checkStatus").and_then(|value| value.as_str()),
        Some("completed")
    );
    assert_eq!(
        content.get("conclusion").and_then(|value| value.as_str()),
        Some("success")
    );
    assert_eq!(
        content.get("state").and_then(|value| value.as_str()),
        Some("completed")
    );
    assert_eq!(
        content.get("checkUrl").and_then(|value| value.as_str()),
        Some("https://github.com/my-org/my-repo/runs/987654321")
    );
    assert_eq!(
        content.get("completedAt").and_then(|value| value.as_str()),
        Some("2026-05-25T19:01:02Z")
    );
}

#[tokio::test]
async fn github_pr_check_run_failure_notifies_participant_user_source() {
    for conclusion in ["failure", "timed_out", "cancelled", "action_required"] {
        let repo = StubSyncRepo::new()
            .with_installation_sources(
                "12345",
                vec![GithubAppInstallationSource::User(
                    "macro|[email protected]".to_string(),
                )],
            )
            .with_github_link("222", "macro|[email protected]")
            .with_github_link("444", "macro|[email protected]");
        let service = make_sync_service_with_repo(repo);
        seed_pull_request_details_with_participants(&service, &["444"]);
        let event = notification_check_run_event(
            "completed",
            "completed",
            Some(conclusion),
            Some(42),
            222,
            "octocat",
            "User",
        );

        service.process_webhook_event(&event).await.unwrap();

        let requests = service.notification_ingress.requests();
        assert_eq!(requests.len(), 1, "expected notification for {conclusion}");
        assert_github_notification_realtime_enabled_apns_disabled(
            &requests[0],
            "github_pr_check_run",
        );
        assert_eq!(
            notification_request_recipients(&requests[0]),
            vec!["macro|[email protected]".to_string()]
        );

        let content = notification_request_content(&requests[0]);
        assert_eq!(
            content.get("conclusion").and_then(|value| value.as_str()),
            Some(conclusion)
        );
        assert_eq!(
            content.get("state").and_then(|value| value.as_str()),
            Some("failed")
        );
    }
}

#[tokio::test]
async fn github_pr_check_run_noncompleted_and_ignored_conclusions_do_not_notify() {
    let cases = [
        ("created action", "created", "completed", Some("success")),
        ("queued status", "completed", "queued", Some("success")),
        (
            "in_progress status",
            "completed",
            "in_progress",
            Some("success"),
        ),
        (
            "neutral conclusion",
            "completed",
            "completed",
            Some("neutral"),
        ),
        (
            "skipped conclusion",
            "completed",
            "completed",
            Some("skipped"),
        ),
        ("missing conclusion", "completed", "completed", None),
    ];

    for (case_name, action, status, conclusion) in cases {
        let repo = StubSyncRepo::new()
            .with_installation_sources(
                "12345",
                vec![GithubAppInstallationSource::User(
                    "macro|[email protected]".to_string(),
                )],
            )
            .with_github_link("444", "macro|[email protected]");
        let service = make_sync_service_with_repo(repo);
        seed_pull_request_details_with_participants(&service, &["444"]);
        let event = notification_check_run_event(
            action,
            status,
            conclusion,
            Some(42),
            222,
            "octocat",
            "User",
        );

        service.process_webhook_event(&event).await.unwrap();

        assert_eq!(service.foreign_entity_service.foreign_entities().len(), 1);
        assert!(
            service.notification_ingress.requests().is_empty(),
            "expected no notification for {case_name}"
        );
    }
}

#[tokio::test]
async fn github_pr_check_run_nonparticipant_recipient_does_not_notify() {
    let repo = StubSyncRepo::new()
        .with_installation_sources(
            "12345",
            vec![GithubAppInstallationSource::User(
                "macro|[email protected]".to_string(),
            )],
        )
        .with_github_link("333", "macro|[email protected]")
        .with_github_link("444", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    seed_pull_request_details_with_participants(&service, &["333"]);
    let event = notification_check_run_event(
        "completed",
        "completed",
        Some("success"),
        Some(42),
        222,
        "octocat",
        "User",
    );

    service.process_webhook_event(&event).await.unwrap();

    assert_eq!(service.foreign_entity_service.foreign_entities().len(), 1);
    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn github_pr_check_run_without_pull_request_does_not_notify_or_upsert() {
    let repo = StubSyncRepo::new().with_installation_sources(
        "12345",
        vec![GithubAppInstallationSource::User(
            "macro|[email protected]".to_string(),
        )],
    );
    let service = make_sync_service_with_repo(repo);
    let event = notification_check_run_event(
        "completed",
        "completed",
        Some("success"),
        None,
        222,
        "octocat",
        "User",
    );

    let result = service.process_webhook_event(&event).await;

    assert!(result.is_ok());
    assert!(service.foreign_entity_service.foreign_entities().is_empty());
    assert!(service.client.pull_request_details_calls().is_empty());
    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn github_pr_check_run_send_failure_does_not_fail_webhook_processing() {
    let repo = StubSyncRepo::new()
        .with_installation_sources(
            "12345",
            vec![GithubAppInstallationSource::User(
                "macro|[email protected]".to_string(),
            )],
        )
        .with_github_link("444", "macro|[email protected]");
    let service = make_sync_service_with_repo_and_notification_ingress(
        repo,
        StubNotificationIngress::failing(),
    );
    seed_pull_request_details_with_participants(&service, &["444"]);
    let event = notification_check_run_event(
        "completed",
        "completed",
        Some("success"),
        Some(42),
        222,
        "octocat",
        "User",
    );

    let result = service.process_webhook_event(&event).await;

    assert!(result.is_ok());
    assert_eq!(service.foreign_entity_service.foreign_entities().len(), 1);

    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);
    assert_github_notification_realtime_enabled_apns_disabled(&requests[0], "github_pr_check_run");
}

#[tokio::test]
async fn pr_edit_patches_existing_foreign_entity_metadata() {
    let (service, foreign_entity_service) = make_sync_service_with_foreign_entity_service();
    let opened_event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "state": "open",
                "merged": false,
                "additions": 10,
                "deletions": 2
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );
    service.process_webhook_event(&opened_event).await.unwrap();

    let edited_event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "edited",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51 with new title",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "state": "open",
                "merged": false,
                "additions": 25,
                "deletions": 7
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );
    service.process_webhook_event(&edited_event).await.unwrap();

    let expected_metadata = expected_pull_request_metadata(
        "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51 with new title",
        GithubPullRequestStatus::Open,
        Some(25),
        Some(7),
    );
    let foreign_entities = foreign_entity_service.foreign_entities();
    assert_eq!(foreign_entities.len(), 1);
    assert_eq!(foreign_entities[0].metadata, expected_metadata);
    assert_eq!(foreign_entity_service.create_calls().len(), 1);

    let patch_calls = foreign_entity_service.patch_calls();
    assert_eq!(patch_calls.len(), 1);
    assert_eq!(patch_calls[0].1.metadata, Some(expected_metadata));
}

#[tokio::test]
async fn pr_closed_upserts_merged_pull_request_metadata() {
    let (service, foreign_entity_service) = make_sync_service_with_foreign_entity_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "closed",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "state": "closed",
                "merged": true,
                "merged_at": "2026-05-27T19:00:00Z",
                "additions": 10,
                "deletions": 2
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let foreign_entities = foreign_entity_service.foreign_entities();
    assert_eq!(foreign_entities.len(), 1);
    assert_eq!(
        foreign_entities[0].metadata,
        expected_pull_request_metadata(
            "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
            GithubPullRequestStatus::Merged,
            Some(10),
            Some(2),
        )
    );
}

#[tokio::test]
async fn pr_event_extracts_participants_from_webhook_payload() {
    let (service, foreign_entity_service) = make_sync_service_with_foreign_entity_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "state": "open",
                "merged": false,
                "additions": 10,
                "deletions": 2,
                "user": { "login": "author", "id": 7 },
                "requested_reviewers": [
                    { "login": "reviewer", "id": 42 },
                    { "login": "author", "id": 7 }
                ],
                "assignees": [{ "login": "assignee", "id": 99 }]
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let foreign_entities = foreign_entity_service.foreign_entities();
    assert_eq!(foreign_entities.len(), 1);
    assert_eq!(
        foreign_entities[0].metadata.get("participantGithubUserIds"),
        Some(&serde_json::json!(["7", "42", "99"]))
    );
}

#[tokio::test]
async fn pr_event_without_valid_tasks_still_upserts_foreign_entity() {
    let (service, foreign_entity_service) = make_sync_service_with_foreign_entity_service();
    let unknown_task_id = MacroTaskId::from_uuid(
        &uuid::Uuid::parse_str("11111111-1111-1111-1111-111111111111").unwrap(),
    )
    .to_task_id_string();
    let title = format!("fixes {unknown_task_id}");
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": title.clone(),
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "state": "open",
                "merged": false,
                "additions": 10,
                "deletions": 2
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let foreign_entities = foreign_entity_service.foreign_entities();
    assert_eq!(foreign_entities.len(), 1);
    assert_eq!(
        foreign_entities[0].metadata,
        expected_pull_request_metadata(&title, GithubPullRequestStatus::Open, Some(10), Some(2))
    );
    assert_eq!(foreign_entity_service.create_calls().len(), 1);
    assert!(foreign_entity_service.patch_calls().is_empty());
}

#[tokio::test]
async fn pr_event_without_task_ids_still_upserts_foreign_entity() {
    let (service, foreign_entity_service) = make_sync_service_with_foreign_entity_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "just a normal PR",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "state": "open",
                "merged": false,
                "additions": 10,
                "deletions": 2
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let foreign_entities = foreign_entity_service.foreign_entities();
    assert_eq!(foreign_entities.len(), 1);
    assert_eq!(
        foreign_entities[0].metadata,
        expected_pull_request_metadata(
            "just a normal PR",
            GithubPullRequestStatus::Open,
            Some(10),
            Some(2),
        )
    );
    assert_eq!(foreign_entity_service.create_calls().len(), 1);
    assert!(foreign_entity_service.patch_calls().is_empty());
}

#[tokio::test]
async fn unhandled_pr_action_still_upserts_foreign_entity() {
    let (service, foreign_entity_service) = make_sync_service_with_foreign_entity_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "synchronize",
            "pull_request": {
                "number": 42,
                "title": "sync branch changes",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "state": "open",
                "merged": false,
                "additions": 12,
                "deletions": 3
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let foreign_entities = foreign_entity_service.foreign_entities();
    assert_eq!(foreign_entities.len(), 1);
    assert_eq!(
        foreign_entities[0].metadata,
        expected_pull_request_metadata(
            "sync branch changes",
            GithubPullRequestStatus::Open,
            Some(12),
            Some(3),
        )
    );
    assert_eq!(foreign_entity_service.create_calls().len(), 1);
    assert!(foreign_entity_service.patch_calls().is_empty());
}

#[tokio::test]
async fn foreign_entity_metadata_includes_comments_and_checks_from_sync_client() {
    let (service, foreign_entity_service) = make_sync_service_with_foreign_entity_service();
    let details = pull_request_details(
        "live pull request title",
        30,
        8,
        Some(vec![pull_request_comment(
            101,
            "Looks good",
            "issue_comment",
        )]),
        Some(vec![pull_request_check_run(201, "ci", "completed")]),
    );
    service
        .client
        .set_pull_request_details("my-org", "my-repo", 42, details.clone());

    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "webhook title",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "state": "open",
                "merged": false,
                "additions": 10,
                "deletions": 2
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    let foreign_entities = foreign_entity_service.foreign_entities();
    assert_eq!(foreign_entities.len(), 1);
    assert_eq!(
        foreign_entities[0].metadata,
        expected_pull_request_metadata_from_details(&details)
    );

    let detail_calls = service.client.pull_request_details_calls();
    assert_eq!(detail_calls.len(), 1);
    assert_eq!(detail_calls[0].owner, "my-org");
    assert_eq!(detail_calls[0].repo, "my-repo");
    assert_eq!(detail_calls[0].number, 42);
}

#[tokio::test]
async fn foreign_entity_metadata_comment_event_refreshes_without_task_id() {
    let (service, foreign_entity_service) = make_sync_service_with_foreign_entity_service();
    let initial_details = pull_request_details("initial title", 10, 2, None, None);
    service
        .client
        .set_pull_request_details("my-org", "my-repo", 42, initial_details);

    let opened_event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "initial title",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "state": "open",
                "merged": false,
                "additions": 10,
                "deletions": 2
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );
    service.process_webhook_event(&opened_event).await.unwrap();

    let refreshed_details = pull_request_details(
        "refreshed title",
        12,
        3,
        Some(vec![pull_request_comment(
            102,
            "A new comment",
            "issue_comment",
        )]),
        Some(vec![pull_request_check_run(202, "ci", "completed")]),
    );
    service
        .client
        .set_pull_request_details("my-org", "my-repo", 42, refreshed_details.clone());

    let comment_event = ValidatedGithubWebhookEvent::new(
        "issue_comment".to_string(),
        serde_json::json!({
            "action": "created",
            "issue": {
                "number": 42,
                "title": "initial title",
                "body": null,
                "state": "open",
                "pull_request": {
                    "url": "https://api.github.com/repos/my-org/my-repo/pulls/42"
                }
            },
            "comment": {
                "body": "No task reference in this comment"
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );
    service.process_webhook_event(&comment_event).await.unwrap();

    let expected_metadata = expected_pull_request_metadata_from_details(&refreshed_details);
    let foreign_entities = foreign_entity_service.foreign_entities();
    assert_eq!(foreign_entities.len(), 1);
    assert_eq!(foreign_entities[0].metadata, expected_metadata);
    assert!(service.client.pr_comments().is_empty());

    let patch_calls = foreign_entity_service.patch_calls();
    assert_eq!(patch_calls.len(), 1);
    assert_eq!(patch_calls[0].1.metadata, Some(expected_metadata));
}

#[tokio::test]
async fn foreign_entity_metadata_check_run_refreshes_pull_request() {
    let (service, foreign_entity_service) = make_sync_service_with_foreign_entity_service();
    let initial_details = pull_request_details("initial title", 10, 2, None, None);
    service
        .client
        .set_pull_request_details("my-org", "my-repo", 42, initial_details);

    let opened_event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "initial title",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "state": "open",
                "merged": false,
                "additions": 10,
                "deletions": 2
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );
    service.process_webhook_event(&opened_event).await.unwrap();

    let refreshed_details = pull_request_details(
        "initial title",
        10,
        2,
        None,
        Some(vec![pull_request_check_run(203, "lint", "completed")]),
    );
    service
        .client
        .set_pull_request_details("my-org", "my-repo", 42, refreshed_details.clone());

    let check_run_event = ValidatedGithubWebhookEvent::new(
        "check_run".to_string(),
        serde_json::json!({
            "action": "completed",
            "check_run": {
                "pull_requests": [
                    { "number": 42 }
                ]
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );
    service
        .process_webhook_event(&check_run_event)
        .await
        .unwrap();

    let expected_metadata = expected_pull_request_metadata_from_details(&refreshed_details);
    let foreign_entities = foreign_entity_service.foreign_entities();
    assert_eq!(foreign_entities.len(), 1);
    assert_eq!(foreign_entities[0].metadata, expected_metadata);

    let patch_calls = foreign_entity_service.patch_calls();
    assert_eq!(patch_calls.len(), 1);
    assert_eq!(patch_calls[0].1.metadata, Some(expected_metadata));
}

#[tokio::test]
async fn foreign_entity_metadata_preserves_existing_comments_when_refresh_omits_them() {
    let (service, foreign_entity_service) = make_sync_service_with_foreign_entity_service();
    let initial_details = pull_request_details(
        "initial title",
        10,
        2,
        Some(vec![pull_request_comment(
            103,
            "Keep this comment",
            "review",
        )]),
        Some(vec![pull_request_check_run(204, "ci", "completed")]),
    );
    service
        .client
        .set_pull_request_details("my-org", "my-repo", 42, initial_details.clone());

    let opened_event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "initial title",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "state": "open",
                "merged": false,
                "additions": 10,
                "deletions": 2
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );
    service.process_webhook_event(&opened_event).await.unwrap();

    let mut partial_details = pull_request_details(
        "partial refresh title",
        11,
        4,
        None,
        Some(vec![pull_request_check_run(205, "ci", "completed")]),
    );
    service
        .client
        .set_pull_request_details("my-org", "my-repo", 42, partial_details.clone());

    let edited_event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "edited",
            "pull_request": {
                "number": 42,
                "title": "partial refresh title",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "state": "open",
                "merged": false,
                "additions": 11,
                "deletions": 4
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );
    service.process_webhook_event(&edited_event).await.unwrap();

    partial_details.comments = initial_details.comments.clone();
    let expected_metadata = expected_pull_request_metadata_from_details(&partial_details);
    let foreign_entities = foreign_entity_service.foreign_entities();
    assert_eq!(foreign_entities.len(), 1);
    assert_eq!(foreign_entities[0].metadata, expected_metadata);
}

#[tokio::test]
async fn foreign_entity_metadata_non_pr_issue_comment_does_not_create_pull_request() {
    let (service, foreign_entity_service) = make_sync_service_with_foreign_entity_service();
    let event = ValidatedGithubWebhookEvent::new(
        "issue_comment".to_string(),
        serde_json::json!({
            "action": "created",
            "issue": {
                "number": 42,
                "title": "plain issue",
                "body": null,
                "state": "open"
            },
            "comment": {
                "body": "No task reference in this issue comment"
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    assert!(foreign_entity_service.foreign_entities().is_empty());
    assert!(service.client.pull_request_details_calls().is_empty());
}

// ---------------------------------------------------------------------------
// New behavioral tests
// ---------------------------------------------------------------------------

#[tokio::test]
async fn pr_close_does_not_post_comment() {
    let (service, doc_service) = make_sync_service_with_doc_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "closed",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "merged": true
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    // No comment posted on close
    assert!(
        service.client.pr_comments().is_empty(),
        "PR close should not post a new bot comment"
    );

    // But status should still be updated
    let status_calls = doc_service.task_status_calls();
    assert_eq!(status_calls.len(), 1);
    assert_eq!(status_calls[0].status, "Completed");
}

#[tokio::test]
async fn pr_open_does_not_search_existing_comments() {
    // On open, only PR title/body/branch are searched — not existing comments.
    // No tasks in the PR text, so nothing should happen.
    let (service, doc_service) = make_sync_service_with_doc_service();

    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "just a normal PR",
                "body": null,
                "head": { "ref": "feature/some-branch" }
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    assert!(service.client.pr_comments().is_empty());
    assert!(doc_service.task_status_calls().is_empty());
}

#[tokio::test]
async fn pr_close_picks_up_task_from_repo() {
    let (service, doc_service) = make_sync_service_with_doc_service();

    // First, open PR with the task to populate the repo
    let open_event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "merged": false
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );
    service.process_webhook_event(&open_event).await.unwrap();

    // Close with a different title (no task ID in text), but repo remembers it
    let close_event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "closed",
            "pull_request": {
                "number": 42,
                "title": "some feature",
                "body": null,
                "head": { "ref": "feature/some-branch" },
                "merged": true
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&close_event).await.unwrap();

    // No comment posted on close
    assert_eq!(service.client.pr_comments().len(), 1); // only from open

    // Status should be updated from repo-tracked task
    let status_calls = doc_service.task_status_calls();
    assert_eq!(status_calls.len(), 2); // "In Review" from open, "Completed" from close
    assert_eq!(status_calls[1].entity_id, KNOWN_TASK_UUID);
    assert_eq!(status_calls[1].status, "Completed");
}

#[tokio::test]
async fn comment_deduplicates_against_repo() {
    let (service, _doc_service) = make_sync_service_with_doc_service();

    // Open PR with a task — tracked in repo
    let pr_event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 99,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "head": { "ref": "main" }
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );
    service.process_webhook_event(&pr_event).await.unwrap();
    assert_eq!(service.client.pr_comments().len(), 1);

    // A comment mentions the same task ID — should be deduped by the repo
    let comment_event = ValidatedGithubWebhookEvent::new(
        "issue_comment".to_string(),
        serde_json::json!({
            "action": "created",
            "issue": {
                "number": 99,
                "title": "fixes MACRO-2BuyvtY3aeEvHx4uG8iD51",
                "body": null,
                "state": "open",
                "head": { "ref": "main" }
            },
            "comment": {
                "body": "Also see MACRO-2BuyvtY3aeEvHx4uG8iD51"
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&comment_event).await.unwrap();

    // No additional comment — task was already tracked in repo
    assert_eq!(
        service.client.pr_comments().len(),
        1,
        "comment should not re-trigger for task already tracked in repo"
    );
}

#[tokio::test]
async fn false_positive_macro_prefix_ignored() {
    // "macro-inc" matches the regex but does not correspond to a real task document.
    let (service, doc_service) = make_sync_service_with_doc_service();
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "number": 42,
                "title": "update macro-inc dependency",
                "body": null,
                "head": { "ref": "feature/update-deps" }
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap();

    assert!(
        service.client.pr_comments().is_empty(),
        "false positive macro- prefix should not trigger a comment"
    );
    assert!(
        doc_service.task_status_calls().is_empty(),
        "false positive macro- prefix should not trigger a status update"
    );
}

// ---------------------------------------------------------------------------
// installation created
// ---------------------------------------------------------------------------

fn installation_created_event(sender_id: u64, installation_id: u64) -> ValidatedGithubWebhookEvent {
    ValidatedGithubWebhookEvent::new(
        "installation".to_string(),
        serde_json::json!({
            "action": "created",
            "installation": { "id": installation_id },
            "sender": { "login": "testuser", "id": sender_id }
        }),
    )
}

/// An `installation.created` event emitted when an org admin approves another
/// user's installation request: the `sender` is the approving admin and the
/// `requester` is who originally asked.
fn approved_installation_created_event(
    requester_id: u64,
    installation_id: u64,
) -> ValidatedGithubWebhookEvent {
    ValidatedGithubWebhookEvent::new(
        "installation".to_string(),
        serde_json::json!({
            "action": "created",
            "installation": { "id": installation_id },
            "sender": { "login": "org-admin", "id": 111 },
            "requester": { "login": "org-member", "id": requester_id }
        }),
    )
}

#[tokio::test]
async fn installation_created_without_requester_is_noop() {
    // Direct installs are associated by the authenticated setup callback, not
    // the webhook, and the sender must never be used as an association
    // heuristic.
    let service = make_sync_service();
    let event = installation_created_event(12345, 99999);

    service.process_webhook_event(&event).await.unwrap();

    assert!(service.repo.installation_sources().is_empty());
    assert!(service.client.setup_code_exchange_calls().is_empty());
    assert!(service.client.user_installation_list_calls().is_empty());
    assert!(service.client.list_open_pull_requests_calls().is_empty());
    assert!(service.foreign_entity_service.foreign_entities().is_empty());
}

#[tokio::test]
async fn installation_created_with_requester_associates_pending_request_source() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = StubSyncRepo::new()
        .with_installation_request("12345", GithubAppInstallationSource::Team(team_id));
    let service = make_sync_service_with_repo(repo);
    let event = approved_installation_created_event(12345, 99999);

    service.process_webhook_event(&event).await.unwrap();

    assert_eq!(
        service.repo.installation_sources(),
        vec![(
            "99999".to_string(),
            vec![GithubAppInstallationSource::Team(team_id)]
        )]
    );
    // The pending request is consumed once the association lands.
    assert!(service.repo.installation_requests().is_empty());
}

#[tokio::test]
async fn installation_created_with_unknown_requester_is_noop() {
    let service = make_sync_service();
    let event = approved_installation_created_event(12345, 99999);

    service.process_webhook_event(&event).await.unwrap();

    assert!(service.repo.installation_sources().is_empty());
}

#[tokio::test]
async fn installation_created_redelivery_after_association_is_noop() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = StubSyncRepo::new()
        .with_installation_request("12345", GithubAppInstallationSource::Team(team_id));
    let service = make_sync_service_with_repo(repo);
    let event = approved_installation_created_event(12345, 99999);

    // GitHub retries webhooks: a redelivery after the pending request was
    // consumed must succeed without re-associating.
    service.process_webhook_event(&event).await.unwrap();
    service.process_webhook_event(&event).await.unwrap();

    assert_eq!(service.repo.installation_sources().len(), 1);
}

#[tokio::test]
async fn installation_created_missing_installation_id_errors() {
    let service = make_sync_service();
    let event = ValidatedGithubWebhookEvent::new(
        "installation".to_string(),
        serde_json::json!({
            "action": "created",
            "sender": { "login": "org-admin", "id": 111 },
            "requester": { "login": "org-member", "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap_err();
}

// ---------------------------------------------------------------------------
// installation deleted
// ---------------------------------------------------------------------------

fn installation_deleted_event(sender_id: u64, installation_id: u64) -> ValidatedGithubWebhookEvent {
    ValidatedGithubWebhookEvent::new(
        "installation".to_string(),
        serde_json::json!({
            "action": "deleted",
            "installation": { "id": installation_id },
            "sender": { "login": "testuser", "id": sender_id }
        }),
    )
}

#[tokio::test]
async fn installation_deleted_removes_only_installation_sources() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();

    let repo = StubSyncRepo::new()
        .with_installation_sources("99999", vec![GithubAppInstallationSource::Team(team_id)])
        .with_installation_sources(
            "88888",
            vec![GithubAppInstallationSource::User(
                "macro|[email protected]".to_string(),
            )],
        );

    let service = make_sync_service_with_repo(repo);
    let event = installation_deleted_event(12345, 99999);

    service.process_webhook_event(&event).await.unwrap();

    assert!(
        service
            .repo
            .get_installation_sources("99999")
            .await
            .unwrap()
            .is_empty()
    );
    // Other installations are untouched.
    assert_eq!(
        service
            .repo
            .get_installation_sources("88888")
            .await
            .unwrap(),
        vec![GithubAppInstallationSource::User(
            "macro|[email protected]".to_string()
        )]
    );
}

#[tokio::test]
async fn installation_deleted_unknown_installation_succeeds() {
    let service = make_sync_service();
    let event = installation_deleted_event(12345, 99999);

    // GitHub retries webhooks: deleting an installation we never recorded
    // (or already deleted) must succeed.
    service.process_webhook_event(&event).await.unwrap();
    service.process_webhook_event(&event).await.unwrap();

    assert!(
        service
            .repo
            .get_installation_sources("99999")
            .await
            .unwrap()
            .is_empty()
    );
}

#[tokio::test]
async fn installation_deleted_missing_installation_id_errors() {
    let service = make_sync_service();
    let event = ValidatedGithubWebhookEvent::new(
        "installation".to_string(),
        serde_json::json!({
            "action": "deleted",
            "sender": { "login": "testuser", "id": 12345 }
        }),
    );

    service.process_webhook_event(&event).await.unwrap_err();
}

// ---------------------------------------------------------------------------
// notify_review_requested
// ---------------------------------------------------------------------------

fn notification_review_requested_event(
    reviewer: Option<(u64, &str)>,
    sender_id: u64,
    sender_login: &str,
) -> ValidatedGithubWebhookEvent {
    let mut payload = serde_json::json!({
        "action": "review_requested",
        "pull_request": {
            "number": 42,
            "title": "Add GitHub notifications",
            "body": null,
            "head": { "ref": "feature/some-branch" },
            "base": { "ref": "main" },
            "state": "open",
            "merged": false,
            "merged_at": null,
            "additions": 10,
            "deletions": 2
        },
        "repository": {
            "name": "my-repo",
            "owner": { "login": "my-org" }
        },
        "installation": { "id": 12345 },
        "sender": {
            "login": sender_login,
            "id": sender_id,
            "avatar_url": format!("https://avatars.example/{sender_login}.png")
        }
    });
    match reviewer {
        Some((reviewer_id, reviewer_login)) => {
            payload["requested_reviewer"] = serde_json::json!({
                "id": reviewer_id,
                "login": reviewer_login,
            });
        }
        None => {
            payload["requested_team"] = serde_json::json!({
                "id": 9000,
                "slug": "platform",
            });
        }
    }

    ValidatedGithubWebhookEvent::new("pull_request".to_string(), payload)
}

#[tokio::test]
async fn review_requested_notifies_only_mapped_reviewer_in_team() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = StubSyncRepo::new()
        .with_installation_sources("12345", vec![GithubAppInstallationSource::Team(team_id)])
        .with_team_members(
            team_id,
            vec![
                "macro|[email protected]",
                "macro|[email protected]",
                "macro|[email protected]",
            ],
        )
        .with_github_link("222", "macro|[email protected]")
        .with_github_link("333", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_review_requested_event(Some((333, "bob-gh")), 222, "octocat");

    service.process_webhook_event(&event).await.unwrap();

    let foreign_entity_id = service.foreign_entity_service.foreign_entities()[0].id;
    let requests = service.notification_ingress.requests();
    assert_eq!(
        requests.len(),
        1,
        "expected only the review-requested notification"
    );

    let request = &requests[0];
    assert_github_notification_realtime_enabled_apns_disabled(request, "github_review_requested");
    assert_eq!(
        notification_request_recipients(request),
        vec!["macro|[email protected]".to_string()]
    );
    assert_eq!(
        request
            .pointer("/req/sender_id")
            .and_then(|value| value.as_str()),
        Some("macro|[email protected]")
    );

    let content = notification_request_content(request);
    assert_eq!(
        content
            .get("foreignEntityId")
            .and_then(|value| value.as_str()),
        Some(foreign_entity_id.to_string().as_str())
    );
    assert_eq!(
        content
            .get("requestedReviewerGithubLogin")
            .and_then(|value| value.as_str()),
        Some("bob-gh")
    );
    assert_eq!(
        content
            .get("requestedReviewerGithubUserId")
            .and_then(|value| value.as_str()),
        Some("333")
    );
    assert_eq!(
        content.get("displayName").and_then(|value| value.as_str()),
        Some("my-org/my-repo#42")
    );
}

#[tokio::test]
async fn review_requested_fans_out_to_all_macro_users_sharing_reviewer_github_account() {
    // The requested reviewer's GitHub account (id 333) is shared by two Macro
    // users, both of whom are members of the source team. The notification
    // should fan out to both of them.
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = StubSyncRepo::new()
        .with_installation_sources("12345", vec![GithubAppInstallationSource::Team(team_id)])
        .with_team_members(
            team_id,
            vec![
                "macro|[email protected]",
                "macro|[email protected]",
                "macro|[email protected]",
            ],
        )
        .with_github_link("222", "macro|[email protected]")
        .with_github_link("333", "macro|[email protected]")
        .with_github_link("333", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_review_requested_event(Some((333, "bob-gh")), 222, "octocat");

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    assert_eq!(
        requests.len(),
        1,
        "expected only the review-requested notification"
    );

    let request = &requests[0];
    assert_github_notification_realtime_enabled_apns_disabled(request, "github_review_requested");
    assert_eq!(
        notification_request_recipients(request),
        vec![
            "macro|[email protected]".to_string(),
            "macro|[email protected]".to_string(),
        ]
    );
}

#[tokio::test]
async fn review_requested_unmapped_reviewer_does_not_notify() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = StubSyncRepo::new()
        .with_installation_sources("12345", vec![GithubAppInstallationSource::Team(team_id)])
        .with_team_members(team_id, vec!["macro|[email protected]"]);
    let service = make_sync_service_with_repo(repo);
    let event = notification_review_requested_event(Some((999, "stranger")), 222, "octocat");

    service.process_webhook_event(&event).await.unwrap();

    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn review_requested_reviewer_outside_source_recipients_does_not_notify() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = StubSyncRepo::new()
        .with_installation_sources("12345", vec![GithubAppInstallationSource::Team(team_id)])
        .with_team_members(team_id, vec!["macro|[email protected]"])
        .with_github_link("333", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_review_requested_event(Some((333, "bob-gh")), 222, "octocat");

    service.process_webhook_event(&event).await.unwrap();

    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn review_requested_team_reviewer_does_not_notify() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = StubSyncRepo::new()
        .with_installation_sources("12345", vec![GithubAppInstallationSource::Team(team_id)])
        .with_team_members(team_id, vec!["macro|[email protected]"]);
    let service = make_sync_service_with_repo(repo);
    let event = notification_review_requested_event(None, 222, "octocat");

    service.process_webhook_event(&event).await.unwrap();

    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn review_requested_user_source_notifies_installed_reviewer() {
    let repo = StubSyncRepo::new()
        .with_installation_sources(
            "12345",
            vec![GithubAppInstallationSource::User(
                "macro|[email protected]".to_string(),
            )],
        )
        .with_github_link("333", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_review_requested_event(Some((333, "solo-gh")), 222, "octocat");

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);
    assert_github_notification_realtime_enabled_apns_disabled(
        &requests[0],
        "github_review_requested",
    );
    assert_eq!(
        notification_request_recipients(&requests[0]),
        vec!["macro|[email protected]".to_string()]
    );
}

// ---------------------------------------------------------------------------
// notify_pr_comment_and_mentions
// ---------------------------------------------------------------------------

fn notification_comment_event(
    event_type: &str,
    action: &str,
    comment_body: &str,
    sender_login: &str,
    sender_type: &str,
) -> ValidatedGithubWebhookEvent {
    let mut payload = serde_json::json!({
        "action": action,
        "comment": {
            "id": 555,
            "body": comment_body,
            "html_url": "https://github.com/my-org/my-repo/pull/42#issuecomment-555"
        },
        "repository": {
            "name": "my-repo",
            "owner": { "login": "my-org" }
        },
        "installation": { "id": 12345 },
        "sender": {
            "login": sender_login,
            "id": 222,
            "type": sender_type,
            "avatar_url": format!("https://avatars.example/{sender_login}.png")
        }
    });
    if event_type == "issue_comment" {
        payload["issue"] = serde_json::json!({
            "number": 42,
            "state": "open",
            "pull_request": { "url": "https://api.github.com/repos/my-org/my-repo/pulls/42" }
        });
    } else {
        payload["pull_request"] = serde_json::json!({
            "number": 42,
            "title": "Add GitHub notifications",
            "state": "open",
            "merged": false
        });
    }

    ValidatedGithubWebhookEvent::new(event_type.to_string(), payload)
}

fn comment_team_repo(team_id: uuid::Uuid) -> StubSyncRepo {
    StubSyncRepo::new()
        .with_installation_sources("12345", vec![GithubAppInstallationSource::Team(team_id)])
        .with_team_members(
            team_id,
            vec![
                "macro|[email protected]",
                "macro|[email protected]",
                "macro|[email protected]",
            ],
        )
        .with_github_link("222", "macro|[email protected]")
}

fn requests_with_tag(requests: &[serde_json::Value], tag: &str) -> Vec<serde_json::Value> {
    requests
        .iter()
        .filter(|request| {
            request
                .pointer("/req/notification/tag")
                .and_then(|value| value.as_str())
                == Some(tag)
        })
        .cloned()
        .collect()
}

#[tokio::test]
async fn issue_comment_notifies_participants_without_mentions() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id)
        .with_github_link("333", "macro|[email protected]")
        .with_github_link("444", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    seed_pull_request_details_with_participants(&service, &["333", "444", "999"]);
    let event = notification_comment_event(
        "issue_comment",
        "created",
        "Looks good overall",
        "octocat",
        "User",
    );

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);

    let request = &requests[0];
    assert_github_notification_realtime_enabled_apns_disabled(request, "github_pr_comment");
    assert_eq!(
        notification_request_recipients(request),
        vec![
            "macro|[email protected]".to_string(),
            "macro|[email protected]".to_string(),
        ]
    );

    let content = notification_request_content(request);
    assert_eq!(
        content.get("commentKind").and_then(|value| value.as_str()),
        Some("issue")
    );
    assert_eq!(
        content
            .get("commentSnippet")
            .and_then(|value| value.as_str()),
        Some("Looks good overall")
    );
    assert_eq!(
        content
            .get("commentGithubId")
            .and_then(|value| value.as_u64()),
        Some(555)
    );
    assert_eq!(
        content.get("commentUrl").and_then(|value| value.as_str()),
        Some("https://github.com/my-org/my-repo/pull/42#issuecomment-555")
    );
    assert_eq!(
        content.get("displayName").and_then(|value| value.as_str()),
        Some("my-org/my-repo#42")
    );
}

#[tokio::test]
async fn issue_comment_uses_existing_participant_metadata_when_live_details_are_missing() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id)
        .with_github_link("333", "macro|[email protected]")
        .with_github_link("444", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let opened_event = notification_pull_request_event_with_participants(
        "opened",
        "Add GitHub notifications",
        "open",
        false,
        None,
        222,
        "octocat",
        PullRequestWebhookParticipants {
            author: None,
            requested_reviewers: &[(333, "bob-gh")],
            assignees: &[(444, "carol-gh")],
        },
    );
    service.process_webhook_event(&opened_event).await.unwrap();
    service.notification_ingress.clear_requests();

    let foreign_entities = service.foreign_entity_service.foreign_entities();
    assert_eq!(foreign_entities.len(), 1);
    assert_eq!(
        foreign_entities[0].metadata.get("participantGithubUserIds"),
        Some(&serde_json::json!(["333", "444"]))
    );

    let event = notification_comment_event(
        "issue_comment",
        "created",
        "Live PR details were unavailable",
        "octocat",
        "User",
    );

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);
    assert_github_notification_realtime_enabled_apns_disabled(&requests[0], "github_pr_comment");
    assert_eq!(
        notification_request_recipients(&requests[0]),
        vec![
            "macro|[email protected]".to_string(),
            "macro|[email protected]".to_string(),
        ]
    );
}

#[tokio::test]
async fn issue_comment_missing_participants_does_not_notify_team_members() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let service = make_sync_service_with_repo(comment_team_repo(team_id));
    let event = notification_comment_event(
        "issue_comment",
        "created",
        "Looks good overall",
        "octocat",
        "User",
    );

    service.process_webhook_event(&event).await.unwrap();

    assert_eq!(service.foreign_entity_service.foreign_entities().len(), 1);
    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn issue_comment_mentioned_member_gets_mention_not_comment() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id)
        .with_github_link("444", "macro|[email protected]")
        .with_github_login_link("bob-gh", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    seed_pull_request_details_with_participants(&service, &["444"]);
    let event = notification_comment_event(
        "issue_comment",
        "created",
        "@bob-gh can you take a look?",
        "octocat",
        "User",
    );

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 2);

    let mentions = requests_with_tag(&requests, "github_pr_mention");
    assert_eq!(mentions.len(), 1);
    assert_eq!(
        notification_request_recipients(&mentions[0]),
        vec!["macro|[email protected]".to_string()]
    );
    let mention_content = notification_request_content(&mentions[0]);
    assert_eq!(
        mention_content
            .get("location")
            .and_then(|value| value.as_str()),
        Some("comment")
    );
    assert_eq!(
        mention_content
            .get("textSnippet")
            .and_then(|value| value.as_str()),
        Some("@bob-gh can you take a look?")
    );

    let comments = requests_with_tag(&requests, "github_pr_comment");
    assert_eq!(comments.len(), 1);
    assert_eq!(
        notification_request_recipients(&comments[0]),
        vec!["macro|[email protected]".to_string()]
    );
}

#[tokio::test]
async fn review_comment_uses_review_comment_kind_and_location() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id)
        .with_github_link("444", "macro|[email protected]")
        .with_github_login_link("bob-gh", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    seed_pull_request_details_with_participants(&service, &["444"]);
    let event = notification_comment_event(
        "pull_request_review_comment",
        "created",
        "@bob-gh this line is wrong",
        "octocat",
        "User",
    );

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    let mentions = requests_with_tag(&requests, "github_pr_mention");
    let comments = requests_with_tag(&requests, "github_pr_comment");
    assert_eq!(mentions.len(), 1);
    assert_eq!(comments.len(), 1);
    assert_eq!(
        notification_request_recipients(&mentions[0]),
        vec!["macro|[email protected]".to_string()]
    );
    assert_eq!(
        notification_request_recipients(&comments[0]),
        vec!["macro|[email protected]".to_string()]
    );
    assert_eq!(
        notification_request_content(&mentions[0])
            .get("location")
            .and_then(|value| value.as_str()),
        Some("review_comment")
    );
    assert_eq!(
        notification_request_content(&comments[0])
            .get("commentKind")
            .and_then(|value| value.as_str()),
        Some("review_comment")
    );
}

#[tokio::test]
async fn bot_comment_does_not_notify() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let service = make_sync_service_with_repo(comment_team_repo(team_id));
    let event = notification_comment_event(
        "issue_comment",
        "created",
        "Linked task: MACRO-abc123",
        "macro-app[bot]",
        "Bot",
    );

    service.process_webhook_event(&event).await.unwrap();

    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn edited_comment_does_not_notify() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let service = make_sync_service_with_repo(comment_team_repo(team_id));
    let event = notification_comment_event(
        "issue_comment",
        "edited",
        "Looks good overall (edited)",
        "octocat",
        "User",
    );

    service.process_webhook_event(&event).await.unwrap();

    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn mention_of_unlinked_login_falls_back_to_comment_for_participants() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id)
        .with_github_link("333", "macro|[email protected]")
        .with_github_link("444", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    seed_pull_request_details_with_participants(&service, &["333", "444"]);
    let event = notification_comment_event(
        "issue_comment",
        "created",
        "@stranger can you take a look?",
        "octocat",
        "User",
    );

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);
    assert_github_notification_realtime_enabled_apns_disabled(&requests[0], "github_pr_comment");
    assert_eq!(
        notification_request_recipients(&requests[0]),
        vec![
            "macro|[email protected]".to_string(),
            "macro|[email protected]".to_string(),
        ]
    );
}

#[tokio::test]
async fn mention_login_linked_to_multiple_users_notifies_all_in_team() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id)
        .with_github_login_link("shared-gh", "macro|[email protected]")
        .with_github_login_link("shared-gh", "macro|[email protected]")
        .with_github_login_link("shared-gh", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    seed_pull_request_details_with_participants(&service, &["222"]);
    let event = notification_comment_event(
        "issue_comment",
        "created",
        "@Shared-GH ping",
        "octocat",
        "User",
    );

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    let mentions = requests_with_tag(&requests, "github_pr_mention");
    let comments = requests_with_tag(&requests, "github_pr_comment");
    assert_eq!(mentions.len(), 1);
    assert_eq!(
        notification_request_recipients(&mentions[0]),
        vec![
            "macro|[email protected]".to_string(),
            "macro|[email protected]".to_string(),
        ]
    );
    // The only non-mentioned participant is Alice, who wrote the comment, so
    // no github_pr_comment notification goes out.
    assert_eq!(comments.len(), 0);
}

// ---------------------------------------------------------------------------
// notify_pr_review
// ---------------------------------------------------------------------------

fn notification_review_event(
    action: &str,
    state: &str,
    body: Option<&str>,
    author_github_id: u64,
    sender_login: &str,
    sender_type: &str,
) -> ValidatedGithubWebhookEvent {
    ValidatedGithubWebhookEvent::new(
        "pull_request_review".to_string(),
        serde_json::json!({
            "action": action,
            "review": {
                "id": 888,
                "state": state,
                "body": body,
                "html_url": "https://github.com/my-org/my-repo/pull/42#pullrequestreview-888"
            },
            "pull_request": {
                "number": 42,
                "title": "Add GitHub notifications",
                "state": "open",
                "merged": false,
                "user": { "id": author_github_id, "login": "pr-author" }
            },
            "repository": {
                "name": "my-repo",
                "owner": { "login": "my-org" }
            },
            "installation": { "id": 12345 },
            "sender": {
                "login": sender_login,
                "id": 222,
                "type": sender_type,
                "avatar_url": format!("https://avatars.example/{sender_login}.png")
            }
        }),
    )
}

#[tokio::test]
async fn approved_review_notifies_author_only() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id).with_github_link("444", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_review_event("submitted", "approved", None, 444, "octocat", "User");

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);

    let request = &requests[0];
    assert_github_notification_realtime_enabled_apns_disabled(request, "github_pr_review");
    assert_eq!(
        notification_request_recipients(request),
        vec!["macro|[email protected]".to_string()]
    );
    assert_eq!(
        request
            .pointer("/req/sender_id")
            .and_then(|value| value.as_str()),
        Some("macro|[email protected]")
    );

    let content = notification_request_content(request);
    assert_eq!(
        content.get("state").and_then(|value| value.as_str()),
        Some("approved")
    );
    assert_eq!(
        content
            .get("reviewGithubId")
            .and_then(|value| value.as_u64()),
        Some(888)
    );
    assert_eq!(
        content.get("reviewUrl").and_then(|value| value.as_str()),
        Some("https://github.com/my-org/my-repo/pull/42#pullrequestreview-888")
    );
    assert!(content.get("reviewSnippet").unwrap().is_null());
}

#[tokio::test]
async fn changes_requested_review_carries_snippet() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id).with_github_link("444", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_review_event(
        "submitted",
        "changes_requested",
        Some("Please add tests"),
        444,
        "octocat",
        "User",
    );

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);
    let content = notification_request_content(&requests[0]);
    assert_eq!(
        content.get("state").and_then(|value| value.as_str()),
        Some("changes_requested")
    );
    assert_eq!(
        content
            .get("reviewSnippet")
            .and_then(|value| value.as_str()),
        Some("Please add tests")
    );
}

#[tokio::test]
async fn unmapped_author_review_still_notifies_mentions() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo =
        comment_team_repo(team_id).with_github_login_link("carol-gh", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_review_event(
        "submitted",
        "approved",
        Some("@carol-gh should double-check the migration"),
        999,
        "octocat",
        "User",
    );

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);
    assert_github_notification_realtime_enabled_apns_disabled(&requests[0], "github_pr_mention");
    assert_eq!(
        notification_request_recipients(&requests[0]),
        vec!["macro|[email protected]".to_string()]
    );
    let content = notification_request_content(&requests[0]);
    assert_eq!(
        content.get("location").and_then(|value| value.as_str()),
        Some("review")
    );
}

#[tokio::test]
async fn empty_commented_review_does_not_notify() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id).with_github_link("444", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_review_event("submitted", "commented", None, 444, "octocat", "User");

    service.process_webhook_event(&event).await.unwrap();

    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn author_mentioned_in_review_gets_review_only() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id)
        .with_github_link("444", "macro|[email protected]")
        .with_github_login_link("bob-gh", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_review_event(
        "submitted",
        "commented",
        Some("@bob-gh nice work overall"),
        444,
        "octocat",
        "User",
    );

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);
    assert_github_notification_realtime_enabled_apns_disabled(&requests[0], "github_pr_review");
    assert_eq!(
        notification_request_recipients(&requests[0]),
        vec!["macro|[email protected]".to_string()]
    );
}

#[tokio::test]
async fn bot_review_does_not_notify() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id).with_github_link("444", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event =
        notification_review_event("submitted", "approved", None, 444, "review-bot[bot]", "Bot");

    service.process_webhook_event(&event).await.unwrap();

    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn dismissed_review_action_does_not_notify() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id).with_github_link("444", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_review_event("dismissed", "dismissed", None, 444, "octocat", "User");

    service.process_webhook_event(&event).await.unwrap();

    assert!(service.notification_ingress.requests().is_empty());
}

// ---------------------------------------------------------------------------
// notify_pr_body_mentions
// ---------------------------------------------------------------------------

fn notification_pr_body_event(
    action: &str,
    body: Option<&str>,
    previous_body: Option<&str>,
    sender_type: &str,
) -> ValidatedGithubWebhookEvent {
    let mut payload = serde_json::json!({
        "action": action,
        "pull_request": {
            "number": 42,
            "title": "Add GitHub notifications",
            "body": body,
            "html_url": "https://github.com/my-org/my-repo/pull/42",
            "head": { "ref": "feature/some-branch" },
            "base": { "ref": "main" },
            "state": "open",
            "merged": false
        },
        "repository": {
            "name": "my-repo",
            "owner": { "login": "my-org" }
        },
        "installation": { "id": 12345 },
        "sender": {
            "login": "octocat",
            "id": 222,
            "type": sender_type,
            "avatar_url": "https://avatars.example/octocat.png"
        }
    });
    if let Some(previous_body) = previous_body {
        payload["changes"] = serde_json::json!({ "body": { "from": previous_body } });
    }

    ValidatedGithubWebhookEvent::new("pull_request".to_string(), payload)
}

#[tokio::test]
async fn opened_pr_body_mention_notifies_mentioned_member() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id).with_github_login_link("bob-gh", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_pr_body_event(
        "opened",
        Some("Implements the thing. @bob-gh please review the approach."),
        None,
        "User",
    );

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();

    let mentions = requests_with_tag(&requests, "github_pr_mention");
    assert_eq!(mentions.len(), 1);
    assert_eq!(
        notification_request_recipients(&mentions[0]),
        vec!["macro|[email protected]".to_string()]
    );
    let content = notification_request_content(&mentions[0]);
    assert_eq!(
        content.get("location").and_then(|value| value.as_str()),
        Some("pr_body")
    );
    assert!(content.get("commentGithubId").unwrap().is_null());
    assert_eq!(
        content.get("commentUrl").and_then(|value| value.as_str()),
        Some("https://github.com/my-org/my-repo/pull/42")
    );
}

#[tokio::test]
async fn edited_pr_body_notifies_only_newly_added_mentions() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id)
        .with_github_login_link("bob-gh", "macro|[email protected]")
        .with_github_login_link("carol-gh", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_pr_body_event(
        "edited",
        Some("cc @bob-gh and now also @carol-gh"),
        Some("cc @bob-gh"),
        "User",
    );

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);
    assert_github_notification_realtime_enabled_apns_disabled(&requests[0], "github_pr_mention");
    assert_eq!(
        notification_request_recipients(&requests[0]),
        vec!["macro|[email protected]".to_string()]
    );
}

#[tokio::test]
async fn edited_pr_body_with_unchanged_mentions_does_not_notify() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id).with_github_login_link("bob-gh", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_pr_body_event(
        "edited",
        Some("cc @bob-gh (reworded description)"),
        Some("cc @bob-gh"),
        "User",
    );

    service.process_webhook_event(&event).await.unwrap();

    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn edited_pr_without_body_change_does_not_notify() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id).with_github_login_link("bob-gh", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    // Title-only edit: no changes.body.from in the payload.
    let event = notification_pr_body_event("edited", Some("cc @bob-gh"), None, "User");

    service.process_webhook_event(&event).await.unwrap();

    assert!(service.notification_ingress.requests().is_empty());
}

#[tokio::test]
async fn edited_pr_with_previously_blank_body_notifies_new_mentions() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id).with_github_login_link("bob-gh", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    // The PR had no description; the edit adds one containing a mention.
    let event = notification_pr_body_event("edited", Some("cc @bob-gh"), Some(""), "User");

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    assert_eq!(requests.len(), 1);
    assert_github_notification_realtime_enabled_apns_disabled(&requests[0], "github_pr_mention");
    assert_eq!(
        notification_request_recipients(&requests[0]),
        vec!["macro|[email protected]".to_string()]
    );
}

#[tokio::test]
async fn bot_opened_pr_body_mention_does_not_notify_mention() {
    let team_id: uuid::Uuid = "dddddddd-dddd-dddd-dddd-dddddddddddd".parse().unwrap();
    let repo = comment_team_repo(team_id).with_github_login_link("bob-gh", "macro|[email protected]");
    let service = make_sync_service_with_repo(repo);
    let event = notification_pr_body_event("opened", Some("automated PR cc @bob-gh"), None, "Bot");

    service.process_webhook_event(&event).await.unwrap();

    let requests = service.notification_ingress.requests();
    assert!(requests_with_tag(&requests, "github_pr_mention").is_empty());
}

fn installation_setup_user() -> MacroUserIdStr<'static> {
    MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap()
}

/// Repo where the setup user has linked the stub client's GitHub account,
/// which `complete_installation_setup` requires of whoever finishes the flow.
fn installation_setup_repo() -> StubSyncRepo {
    StubSyncRepo::new().with_github_link(
        &TEST_GITHUB_USER_ID.to_string(),
        installation_setup_user().as_ref(),
    )
}

fn make_setup_sync_service() -> TestGithubSyncService {
    make_sync_service_with_repo(installation_setup_repo())
}

fn installation_setup_state(team_id: Option<uuid::Uuid>, exp: i64) -> String {
    sign_installation_state(
        &InstallationState {
            macro_user_id: installation_setup_user(),
            team_id,
            exp,
        },
        b"test-installation-state-secret",
    )
    .unwrap()
}

#[tokio::test]
async fn begin_team_installation_setup_preserves_query_and_signs_team() {
    let user = installation_setup_user();
    let team_id = uuid::Uuid::new_v4();
    let service = make_sync_service_with_repo(
        StubSyncRepo::new().with_user_teams(user.as_ref(), vec![team_id]),
    );

    let setup_url = service
        .begin_installation_setup(&user, Some(team_id))
        .await
        .unwrap();
    let url = url::Url::parse(&setup_url).unwrap();
    let query: HashMap<_, _> = url.query_pairs().into_owned().collect();
    let state = verify_installation_state(
        query.get("state").unwrap(),
        b"test-installation-state-secret",
        chrono::Utc::now().timestamp(),
    )
    .unwrap();

    assert_eq!(url.path(), "/apps/test/installations/new");
    assert_eq!(query.get("existing").map(String::as_str), Some("1"));
    assert_eq!(state.macro_user_id, user);
    assert_eq!(state.team_id, Some(team_id));
    assert!(state.exp <= chrono::Utc::now().timestamp() + 60 * 60);
}

#[tokio::test]
async fn begin_installation_setup_normalizes_app_profile_url() {
    let user = installation_setup_user();
    let mut service = make_sync_service();
    service.config.github_sync_app_url = "https://github.com/apps/test?existing=1".to_string();

    let setup_url = service.begin_installation_setup(&user, None).await.unwrap();
    let url = url::Url::parse(&setup_url).unwrap();
    let query: HashMap<_, _> = url.query_pairs().into_owned().collect();

    assert_eq!(url.path(), "/apps/test/installations/new");
    assert_eq!(query.get("existing").map(String::as_str), Some("1"));
    assert!(query.contains_key("state"));
}

#[tokio::test]
async fn begin_personal_installation_setup_does_not_infer_teams() {
    let user = installation_setup_user();
    let service = make_sync_service_with_repo(
        StubSyncRepo::new().with_user_teams(user.as_ref(), vec![uuid::Uuid::new_v4()]),
    );

    let setup_url = service.begin_installation_setup(&user, None).await.unwrap();
    let url = url::Url::parse(&setup_url).unwrap();
    let signed_state = url
        .query_pairs()
        .find_map(|(key, value)| (key == "state").then(|| value.into_owned()))
        .unwrap();
    let state = verify_installation_state(
        &signed_state,
        b"test-installation-state-secret",
        chrono::Utc::now().timestamp(),
    )
    .unwrap();

    assert_eq!(state.team_id, None);
}

#[tokio::test]
async fn begin_installation_setup_rejects_non_member() {
    let error = make_sync_service()
        .begin_installation_setup(&installation_setup_user(), Some(uuid::Uuid::new_v4()))
        .await
        .unwrap_err();

    assert!(matches!(error, GithubError::Forbidden));
}

#[tokio::test]
async fn complete_installation_setup_associates_team_personal_and_update_sources() {
    let team_id = uuid::Uuid::new_v4();
    let team_service = make_setup_sync_service();
    team_service.client.set_user_installations(&[41]);
    team_service
        .complete_installation_setup(
            &installation_setup_state(Some(team_id), chrono::Utc::now().timestamp() + 60),
            Some("team-code"),
            Some(41),
            "install",
        )
        .await
        .unwrap();
    assert_eq!(
        team_service.repo.installation_sources(),
        vec![(
            "41".to_string(),
            vec![GithubAppInstallationSource::Team(team_id)]
        )]
    );

    let personal_service = make_setup_sync_service();
    personal_service.client.set_user_installations(&[42]);
    personal_service
        .complete_installation_setup(
            &installation_setup_state(None, chrono::Utc::now().timestamp() + 60),
            Some("personal-code"),
            Some(42),
            "update",
        )
        .await
        .unwrap();
    assert_eq!(
        personal_service.repo.installation_sources(),
        vec![(
            "42".to_string(),
            vec![GithubAppInstallationSource::User(
                installation_setup_user().into()
            )]
        )]
    );
    assert_eq!(
        personal_service.client.setup_code_exchange_calls(),
        vec![(
            "test-sync-app-client-id".to_string(),
            "test-sync-app-client-secret".to_string(),
            "personal-code".to_string()
        )]
    );
    assert_eq!(
        personal_service.client.user_installation_list_calls(),
        vec!["test-user-token".to_string()]
    );
}

#[tokio::test]
async fn update_associates_an_existing_installation_with_one_new_source() {
    let existing_team = uuid::Uuid::new_v4();
    let requested_team = uuid::Uuid::new_v4();
    let service =
        make_sync_service_with_repo(installation_setup_repo().with_installation_sources(
            "43",
            vec![GithubAppInstallationSource::Team(existing_team)],
        ));
    service.client.set_user_installations(&[43]);

    service
        .complete_installation_setup(
            &installation_setup_state(Some(requested_team), chrono::Utc::now().timestamp() + 60),
            Some("update-code"),
            Some(43),
            "update",
        )
        .await
        .unwrap();

    let sources: HashSet<_> = service
        .repo
        .get_installation_sources("43")
        .await
        .unwrap()
        .into_iter()
        .collect();
    assert_eq!(
        sources,
        HashSet::from([
            GithubAppInstallationSource::Team(existing_team),
            GithubAppInstallationSource::Team(requested_team),
        ])
    );
    assert_eq!(
        service.repo.installation_sources(),
        vec![(
            "43".to_string(),
            vec![GithubAppInstallationSource::Team(requested_team)]
        )]
    );
}

#[tokio::test]
async fn complete_installation_setup_rejects_expired_and_tampered_state() {
    let service = make_sync_service();
    let expired = installation_setup_state(None, chrono::Utc::now().timestamp());
    assert!(matches!(
        service
            .complete_installation_setup(&expired, Some("code"), Some(1), "install")
            .await,
        Err(GithubError::InvalidInstallationState)
    ));

    let mut tampered = installation_setup_state(None, chrono::Utc::now().timestamp() + 60);
    tampered.push('x');
    assert!(matches!(
        service
            .complete_installation_setup(&tampered, Some("code"), Some(1), "install")
            .await,
        Err(GithubError::InvalidInstallationState)
    ));
    assert!(service.client.setup_code_exchange_calls().is_empty());
}

#[tokio::test]
async fn complete_installation_setup_rejects_foreign_installation() {
    let service = make_setup_sync_service();
    service.client.set_user_installations(&[1, 2]);
    let result = service
        .complete_installation_setup(
            &installation_setup_state(None, chrono::Utc::now().timestamp() + 60),
            Some("code"),
            Some(3),
            "install",
        )
        .await;

    assert!(matches!(result, Err(GithubError::InstallationNotOwned)));
    assert!(service.repo.installation_sources().is_empty());
}

#[tokio::test]
async fn complete_installation_setup_fails_closed_on_exchange_or_listing_failure() {
    let exchange_service = make_setup_sync_service();
    exchange_service.client.fail_setup_code_exchange();
    let state = installation_setup_state(None, chrono::Utc::now().timestamp() + 60);
    assert!(
        exchange_service
            .complete_installation_setup(&state, Some("code"), Some(1), "install")
            .await
            .is_err()
    );
    assert!(
        exchange_service
            .client
            .user_installation_list_calls()
            .is_empty()
    );
    assert!(exchange_service.repo.installation_sources().is_empty());

    let listing_service = make_setup_sync_service();
    listing_service.client.fail_user_installation_list();
    assert!(
        listing_service
            .complete_installation_setup(&state, Some("code"), Some(1), "install")
            .await
            .is_err()
    );
    assert!(listing_service.repo.installation_sources().is_empty());
}

#[tokio::test]
async fn complete_installation_setup_accepts_installation_from_complete_paginated_result() {
    let service = make_setup_sync_service();
    let installation_ids: Vec<u64> = (1..=150).collect();
    service.client.set_user_installations(&installation_ids);

    service
        .complete_installation_setup(
            &installation_setup_state(None, chrono::Utc::now().timestamp() + 60),
            Some("code"),
            Some(150),
            "install",
        )
        .await
        .unwrap();

    assert_eq!(service.repo.installation_sources().len(), 1);
}

#[tokio::test]
async fn complete_installation_setup_request_parks_team_pending_request() {
    let team_id = uuid::Uuid::new_v4();
    let service = make_setup_sync_service();
    service
        .complete_installation_setup(
            &installation_setup_state(Some(team_id), chrono::Utc::now().timestamp() + 60),
            Some("request-code"),
            None,
            "request",
        )
        .await
        .unwrap();

    // No installation exists until an org admin approves, so nothing is
    // associated yet...
    assert!(service.repo.installation_sources().is_empty());
    assert!(service.client.user_installation_list_calls().is_empty());
    // ...but the requested source is parked, keyed by the requester's GitHub
    // identity, for the installation.created webhook to complete.
    assert_eq!(
        service.repo.installation_requests(),
        HashMap::from([(
            TEST_GITHUB_USER_ID.to_string(),
            GithubAppInstallationSource::Team(team_id)
        )])
    );
    assert_eq!(
        service.client.authenticated_user_calls(),
        vec!["test-user-token".to_string()]
    );
}

#[tokio::test]
async fn complete_installation_setup_request_parks_personal_pending_request() {
    let service = make_setup_sync_service();
    service
        .complete_installation_setup(
            &installation_setup_state(None, chrono::Utc::now().timestamp() + 60),
            Some("request-code"),
            None,
            "request",
        )
        .await
        .unwrap();

    assert_eq!(
        service.repo.installation_requests(),
        HashMap::from([(
            TEST_GITHUB_USER_ID.to_string(),
            GithubAppInstallationSource::User(installation_setup_user().into())
        )])
    );
}

#[tokio::test]
async fn complete_installation_setup_request_rejects_invalid_state_and_missing_code() {
    let service = make_sync_service();

    assert!(matches!(
        service
            .complete_installation_setup("invalid", Some("code"), None, "request")
            .await,
        Err(GithubError::InvalidInstallationState)
    ));

    assert!(matches!(
        service
            .complete_installation_setup(
                &installation_setup_state(None, chrono::Utc::now().timestamp() + 60),
                None,
                None,
                "request",
            )
            .await,
        Err(GithubError::MissingInstallationSetupField("code"))
    ));

    assert!(service.client.setup_code_exchange_calls().is_empty());
    assert!(service.repo.installation_requests().is_empty());
}

#[tokio::test]
async fn complete_installation_setup_request_fails_closed_on_identity_failure() {
    let service = make_setup_sync_service();
    service.client.fail_get_authenticated_user();

    assert!(
        service
            .complete_installation_setup(
                &installation_setup_state(None, chrono::Utc::now().timestamp() + 60),
                Some("request-code"),
                None,
                "request",
            )
            .await
            .is_err()
    );
    assert!(service.repo.installation_requests().is_empty());
}

#[tokio::test]
async fn requested_install_is_associated_after_admin_approval() {
    let team_id = uuid::Uuid::new_v4();
    let service = make_setup_sync_service();

    // A team member requests the org install: no installation exists yet.
    service
        .complete_installation_setup(
            &installation_setup_state(Some(team_id), chrono::Utc::now().timestamp() + 60),
            Some("request-code"),
            None,
            "request",
        )
        .await
        .unwrap();
    assert!(service.repo.installation_sources().is_empty());

    // Days later an org admin approves, which creates the installation and
    // emits installation.created with the original requester.
    let event = approved_installation_created_event(TEST_GITHUB_USER_ID, 555);
    service.process_webhook_event(&event).await.unwrap();

    assert_eq!(
        service.repo.installation_sources(),
        vec![(
            "555".to_string(),
            vec![GithubAppInstallationSource::Team(team_id)]
        )]
    );
    assert!(service.repo.installation_requests().is_empty());
}

#[tokio::test]
async fn complete_installation_setup_rejects_missing_fields_and_unknown_action() {
    let service = make_sync_service();
    let state = installation_setup_state(None, chrono::Utc::now().timestamp() + 60);

    assert!(matches!(
        service
            .complete_installation_setup(&state, None, Some(1), "install")
            .await,
        Err(GithubError::MissingInstallationSetupField("code"))
    ));
    assert!(matches!(
        service
            .complete_installation_setup(&state, Some("code"), None, "update")
            .await,
        Err(GithubError::MissingInstallationSetupField(
            "installation_id"
        ))
    ));
    assert!(matches!(
        service
            .complete_installation_setup(&state, None, None, "other")
            .await,
        Err(GithubError::InvalidInstallationSetupAction)
    ));
}

#[tokio::test]
async fn complete_installation_setup_rejects_unlinked_completer() {
    // A signed state alone must not be honored: whoever finishes the flow has
    // to be linked to the Macro user the state was minted for, or a leaked or
    // attacker-minted state could bind an installation to a foreign source.
    let service = make_sync_service();
    service.client.set_user_installations(&[9]);
    let state = installation_setup_state(None, chrono::Utc::now().timestamp() + 60);

    assert!(matches!(
        service
            .complete_installation_setup(&state, Some("code"), Some(9), "install")
            .await,
        Err(GithubError::SetupUserNotLinked)
    ));
    assert!(matches!(
        service
            .complete_installation_setup(&state, Some("code"), None, "request")
            .await,
        Err(GithubError::SetupUserNotLinked)
    ));

    assert!(service.repo.installation_sources().is_empty());
    assert!(service.repo.installation_requests().is_empty());
    // The identity check happens before the ownership listing.
    assert!(service.client.user_installation_list_calls().is_empty());
}

#[tokio::test]
async fn complete_installation_setup_rejects_completer_linked_to_other_user() {
    let service = make_sync_service_with_repo(StubSyncRepo::new().with_github_link(
        &TEST_GITHUB_USER_ID.to_string(),
        "macro|[email protected]",
    ));
    service.client.set_user_installations(&[9]);

    assert!(matches!(
        service
            .complete_installation_setup(
                &installation_setup_state(None, chrono::Utc::now().timestamp() + 60),
                Some("code"),
                Some(9),
                "install",
            )
            .await,
        Err(GithubError::SetupUserNotLinked)
    ));
    assert!(service.repo.installation_sources().is_empty());
}

#[tokio::test]
async fn repeated_installation_association_is_idempotent() {
    let service = make_setup_sync_service();
    service.client.set_user_installations(&[77]);
    let state = installation_setup_state(None, chrono::Utc::now().timestamp() + 60);

    for _ in 0..2 {
        service
            .complete_installation_setup(&state, Some("code"), Some(77), "install")
            .await
            .unwrap();
    }

    let rows = service.repo.get_installation_sources("77").await.unwrap();
    assert_eq!(
        rows,
        vec![GithubAppInstallationSource::User(
            installation_setup_user().into()
        )]
    );
}

#[derive(Default)]
struct StubRealtime {
    fail_sends: bool,
    events: std::sync::Mutex<Vec<(Vec<MacroUserIdStr<'static>>, ForeignEntity)>>,
}

impl crate::domain::ports::GithubSyncRealtime for StubRealtime {
    async fn publish_pull_request(
        &self,
        recipients: &[MacroUserIdStr<'static>],
        entity: &ForeignEntity,
    ) -> Result<(), GithubError> {
        if self.fail_sends {
            return Err(GithubError::Internal(anyhow::anyhow!(
                "gateway unavailable"
            )));
        }
        self.events
            .lock()
            .unwrap()
            .push((recipients.to_vec(), entity.clone()));
        Ok(())
    }
}

#[tokio::test]
async fn realtime_publishes_saved_entities_only_to_their_source_members() {
    let team = macro_uuid::generate_uuid_v7();
    let repo = StubSyncRepo::new()
        .with_team_members(team, vec!["macro|[email protected]", "macro|[email protected]"]);
    let service = make_sync_service_with_repo(repo);
    let pull_request: EnrichedGithubPullRequest = serde_json::from_value(
        expected_pull_request_metadata("PR", GithubPullRequestStatus::Open, None, None),
    )
    .unwrap();
    let sources = vec![
        GithubAppInstallationSource::Team(team),
        GithubAppInstallationSource::User("macro|[email protected]".to_string()),
    ];
    service
        .upsert_enriched_pull_request_foreign_entities(pull_request.clone(), &sources)
        .await;
    let stored = service.foreign_entity_service.foreign_entities();
    {
        let events = service.realtime.events.lock().unwrap();
        assert_eq!(events.len(), 2);
        assert_eq!(events[0].1, stored[0]);
        assert_eq!(events[1].1, stored[1]);
        let recipients: HashSet<_> = events[0].0.iter().map(|id| id.as_ref()).collect();
        assert_eq!(
            recipients,
            HashSet::from(["macro|[email protected]", "macro|[email protected]"])
        );
        assert_eq!(
            events[1].0.iter().map(|id| id.as_ref()).collect::<Vec<_>>(),
            vec!["macro|[email protected]"]
        );
    }
    let mut merged = pull_request;
    merged.status = Some(GithubPullRequestStatus::Merged);
    service
        .upsert_enriched_pull_request_foreign_entities(merged, &sources)
        .await;
    let events = service.realtime.events.lock().unwrap();
    assert_eq!(events.len(), 4);
    assert_eq!(events[2].1.id, stored[0].id);
    assert_eq!(events[2].1.metadata["status"], "merged");
}

#[tokio::test]
async fn realtime_does_not_publish_to_an_empty_or_invalid_source() {
    let service = make_sync_service();
    let pull_request: EnrichedGithubPullRequest = serde_json::from_value(
        expected_pull_request_metadata("PR", GithubPullRequestStatus::Open, None, None),
    )
    .unwrap();
    service
        .upsert_enriched_pull_request_foreign_entities(
            pull_request,
            &[
                GithubAppInstallationSource::Team(macro_uuid::generate_uuid_v7()),
                GithubAppInstallationSource::User("invalid".to_string()),
            ],
        )
        .await;
    assert!(service.realtime.events.lock().unwrap().is_empty());
}

#[tokio::test]
async fn realtime_delivery_failure_keeps_the_saved_mapping() {
    let mut service = make_sync_service();
    service.realtime.fail_sends = true;
    let pull_request: EnrichedGithubPullRequest = serde_json::from_value(
        expected_pull_request_metadata("PR", GithubPullRequestStatus::Open, None, None),
    )
    .unwrap();
    let upserts = service
        .upsert_enriched_pull_request_foreign_entities(
            pull_request,
            &[GithubAppInstallationSource::User(
                "macro|[email protected]".to_string(),
            )],
        )
        .await;
    assert_eq!(upserts.len(), 1);
    assert_eq!(service.foreign_entity_service.foreign_entities().len(), 1);
}

struct RecordingPublisher(Arc<Mutex<Vec<crate::domain::models::GithubWebhookReceived>>>);

impl crate::domain::ports::GithubWebhookPublisher for RecordingPublisher {
    fn publish_received(
        &self,
        delivery: crate::domain::models::GithubWebhookReceived,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), GithubError>> + Send + '_>>
    {
        self.0.lock().unwrap().push(delivery);
        Box::pin(async { Ok(()) })
    }
}

#[tokio::test]
async fn every_installation_delivery_is_published_even_events_sync_ignores() {
    let TestGithubSyncService { service, .. } = make_sync_service();
    let published = Arc::new(Mutex::new(Vec::new()));
    let service = service.with_webhook_publisher(RecordingPublisher(Arc::clone(&published)));

    let workflow_run = ValidatedGithubWebhookEvent::new(
        "workflow_run".to_string(),
        serde_json::json!({
            "action": "completed",
            "installation": { "id": 12345 },
            "workflow_run": { "name": "CI", "conclusion": "failure" },
        }),
    )
    .with_delivery_id(Some("delivery-1".to_string()));
    let app_level = ValidatedGithubWebhookEvent::new(
        "github_app_authorization".to_string(),
        serde_json::json!({ "action": "revoked" }),
    );

    service.process_webhook_event(&workflow_run).await.unwrap();
    service.process_webhook_event(&app_level).await.unwrap();

    let published = published.lock().unwrap();
    assert_eq!(published.len(), 1);
    assert_eq!(published[0].event, "workflow_run");
    assert_eq!(published[0].delivery_id.as_deref(), Some("delivery-1"));
    assert_eq!(published[0].installation_id, 12345);
}