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.

110 KB
Raw Blame
use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

use axum::http::{Request as HttpRequest, header};
use email::domain::models::{
    AttachmentDraft, AttachmentForwarded, CreateDraftInput, CreatedDraft, EmailErr, EmailFilter,
    EmailSyncStatus, EmailThreadMailCacheFacts, EmailThreadMailPreviews, EmailThreadMailProjection,
    EmailThreadMetadata, EnrichedEmailThreadPreview, GetEmailsRequest, LabelListVisibility,
    LabelType, Link, LinkLabel, Message, MessageAttachment, MessageListVisibility, ParsedMessage,
    ParsedThread, ResolvedSendingInbox, SenderPolicy, SendingInboxRequest,
    SetThreadsArchivedResult, Thread, UpdateThreadLabelsResult, UpsertEmailFilterInput,
    UserEmailLink, UserEmailLinkSettings, UserProvider,
};
use entity_access::domain::models::{
    AccessError, AccessLevel, BotAccessScope, BotId, CallChannelInfo, EditAccessLevel,
    EntityAccessReceipt, EntityPermission, EntityType, RequiredPermission, TeamRole, UserTeamInfo,
    ViewAccessLevel,
};
use graphql_common::GraphqlRequestParts;
use macro_authorization::{
    INTERNAL_API_KEY_HEADER, INTERNAL_MACRO_USER_ID_HEADER, InternalIdentityClaims,
    MacroAuthorizationError, MacroAuthorizationService, MacroAuthorizationState,
};
use macro_user_id::{
    email::EmailStr,
    lowercased::Lowercase,
    user_id::{MacroUserId, MacroUserIdStr},
};
use model_entity::EntityType as ModelEntityType;
use model_owner::Owner;
use model_user::UserContext;
use models_pagination::{Paginated, PaginatedCursor, SimpleSortMethod};
use models_soup::{
    chat::SoupChat,
    document::SoupDocument,
    email_thread::{SoupContact, SoupEmailThreadPreview, SoupEnrichedEmailThreadPreview},
    initiative::SoupInitiative,
    item::SoupItem,
    project::SoupProject,
};
use rootcause::Report;
use soup_realtime::domain::models::Patch;
use uuid::Uuid;

use super::*;

mod calendar;
mod crm_record;
mod database_activity;
mod database_row;
mod database_view_rows;
mod email_archive;
mod form_activity;
mod initiative;
mod notification_inbox;
mod registered_work;
mod scheduled_actions;
mod soup_patches;
mod stale_client;
mod team_activity;
mod work_feed;

const VALID_USER_ID: &str = "macro|[email protected]";
const INTERNAL_USER_ID: &str = "macro|[email protected]";
const VALID_INTERNAL_KEY: &str = "valid-internal-key";

#[derive(Clone)]
struct TestRealtimeSubscriptionService {
    receiver: Arc<Mutex<Option<tokio::sync::mpsc::Receiver<Patch<model_entity::Entity<'static>>>>>>,
    subscribed_user: Arc<Mutex<Option<MacroUserIdStr<'static>>>>,
}

impl SoupRealtimeSubscriptionService for TestRealtimeSubscriptionService {
    fn subscribe(
        &self,
        user_id: MacroUserIdStr<'static>,
    ) -> tokio::sync::mpsc::Receiver<Patch<model_entity::Entity<'static>>> {
        *self.subscribed_user.lock().expect("subscribed user lock") = Some(user_id);
        self.receiver
            .lock()
            .expect("subscription receiver lock")
            .take()
            .expect("test subscription is opened once")
    }
}

#[derive(Clone, Default)]
struct CountingSoupService {
    raw_calls: Arc<AtomicUsize>,
    raw_team_receipts: Arc<AtomicUsize>,
    return_empty_raw: bool,
    raw_response: Arc<Mutex<Option<Vec<SoupItem<()>>>>>,
    frecency_calls: Arc<AtomicUsize>,
    frecency_team_receipts: Arc<AtomicUsize>,
    grouped_calls: Arc<AtomicUsize>,
}

fn soup_document(id: Uuid) -> SoupItem<()> {
    grouped_document(id).map_extra(|_| ())
}

fn soup_agent_session(id: Uuid) -> SoupItem<()> {
    SoupItem::AgentSession(models_soup::agent_session::SoupAgentSession {
        id,
        name: "A session".to_string(),
        archived_by: None,
        archived_at: None,
        owner_id: Owner::from_principal_str(VALID_USER_ID).unwrap(),
        bot_id: Uuid::from_u128(99),
        harness: "claude".to_string(),
        repo_url: None,
        repo_branch: None,
        pull_request_url: None,
        working_branch: None,
        pull_request_state: None,
        pull_request_id: None,
        turn_state: None,
        thread_id: None,
        status: "no_messages".to_string(),
        created_at: Default::default(),
        updated_at: Default::default(),
        viewed_at: None,
        extra: (),
        source: models_soup::agent_session::AgentSessionSource::User,
        visibility: models_soup::agent_session::AgentSessionVisibility::Listed,
    })
}

fn soup_initiative(id: Uuid) -> SoupItem<()> {
    SoupItem::Initiative(SoupInitiative {
        id,
        name: "Launch project".to_string(),
        owner_id: Owner::from_principal_str(VALID_USER_ID).unwrap(),
        created_at: Default::default(),
        updated_at: Default::default(),
        viewed_at: None,
        extra: (),
    })
}

fn soup_project(id: Uuid) -> SoupItem<()> {
    SoupItem::Project(SoupProject {
        id,
        name: format!("Project {id}"),
        owner_id: Owner::from_principal_str(VALID_USER_ID).unwrap(),
        parent_id: None,
        created_at: Default::default(),
        updated_at: Default::default(),
        viewed_at: None,
        deleted_at: None,
        extra: (),
    })
}

fn soup_chat(id: Uuid) -> SoupItem<()> {
    SoupItem::Chat(SoupChat {
        id,
        name: format!("Chat {id}"),
        model: Some("openai/gpt-5.6".to_string()),
        owner_id: Owner::from_principal_str(VALID_USER_ID).unwrap(),
        project_id: None,
        is_persistent: true,
        created_at: Default::default(),
        updated_at: Default::default(),
        viewed_at: None,
        deleted_at: None,
        extra: (),
    })
}

fn grouped_document(id: Uuid) -> SoupItem<soup::domain::models::SoupPropertiesField> {
    SoupItem::Document(SoupDocument {
        id,
        document_version_id: 1,
        owner_id: Owner::from_principal_str(VALID_USER_ID).unwrap(),
        name: format!("Document {id}"),
        file_type: None,
        sha: None,
        project_id: None,
        branched_from_id: None,
        branched_from_version_id: None,
        document_family_id: None,
        created_at: Default::default(),
        updated_at: Default::default(),
        viewed_at: Default::default(),
        sub_type: None,
        deleted_at: None,
        extra: soup::domain::models::SoupPropertiesField::default(),
    })
}

fn test_soup_err() -> soup::domain::models::SoupErr {
    soup::domain::models::SoupErr::SoupDbErr(anyhow::anyhow!("counting Soup service"))
}

impl CountingSoupService {
    fn set_raw_response(&self, items: Vec<SoupItem<()>>) {
        *self.raw_response.lock().expect("raw response lock") = Some(items);
    }
}

impl SoupService for CountingSoupService {
    async fn get_user_soup<T>(
        &self,
        _req: soup::domain::models::SoupRequest<T>,
        team_receipt: Option<EntityAccessReceipt<entity_access::domain::models::MemberTeamRole>>,
    ) -> Result<soup::domain::ports::SoupOutput<T>, soup::domain::models::SoupErr>
    where
        soup::domain::models::SoupRequest<T>: soup::domain::models::IntoSoupReqAst,
        T: Clone + serde::Serialize + Send,
    {
        self.raw_calls.fetch_add(1, Ordering::SeqCst);
        if team_receipt.is_some() {
            self.raw_team_receipts.fetch_add(1, Ordering::SeqCst);
        }
        let raw_response = self.raw_response.lock().expect("raw response lock").clone();
        if self.return_empty_raw || raw_response.is_some() {
            let page: PaginatedCursor<SoupItem<()>, String, SimpleSortMethod, T> =
                Paginated::from_parts(raw_response.unwrap_or_default(), None);
            return Ok(soup::domain::ports::SoupOutput::Simple(page));
        }
        Err(test_soup_err())
    }

    async fn get_user_soup_with_projection<T>(
        &self,
        req: soup::domain::models::SoupRequest<T>,
        team_receipt: Option<EntityAccessReceipt<entity_access::domain::models::MemberTeamRole>>,
    ) -> Result<
        soup::domain::ports::SoupOutput<T, soup::domain::models::SoupProjectionHydration>,
        soup::domain::models::SoupErr,
    >
    where
        soup::domain::models::SoupRequest<T>: soup::domain::models::IntoSoupReqAst,
        T: Clone + serde::Serialize + Send,
    {
        self.get_user_soup(req, team_receipt).await.map(|output| {
            output.map(|item| {
                let document_server_facts = matches!(&item, SoupItem::Document(_)).then_some(
                    soup::domain::models::SoupDocumentServerFacts {
                        is_email_attachment: false,
                        is_important: true,
                        status_option_ids: Vec::new(),
                    },
                );
                soup::domain::models::SoupProjectionHydration {
                    sort_key: None,
                    item,
                    document_server_facts,
                }
            })
        })
    }

    async fn get_user_soup_with_properties<T>(
        &self,
        _req: soup::domain::models::SoupRequest<T>,
        _team_receipt: Option<EntityAccessReceipt<entity_access::domain::models::MemberTeamRole>>,
    ) -> Result<
        soup::domain::ports::SoupOutput<T, soup::domain::models::EnrichedSoupItem>,
        soup::domain::models::SoupErr,
    >
    where
        soup::domain::models::SoupRequest<T>: soup::domain::models::IntoSoupReqAst,
        T: Clone + serde::Serialize + Send,
    {
        Err(test_soup_err())
    }

    async fn get_user_soup_with_frecency<T>(
        &self,
        _req: soup::domain::models::SoupRequest<T>,
        team_receipt: Option<EntityAccessReceipt<entity_access::domain::models::MemberTeamRole>>,
    ) -> Result<
        soup::domain::ports::SoupOutput<T, soup::domain::models::EnrichedSoupItem>,
        soup::domain::models::SoupErr,
    >
    where
        soup::domain::models::SoupRequest<T>: soup::domain::models::IntoSoupReqAst,
        T: Clone + serde::Serialize + Send,
    {
        self.frecency_calls.fetch_add(1, Ordering::SeqCst);
        if team_receipt.is_some() {
            self.frecency_team_receipts.fetch_add(1, Ordering::SeqCst);
        }
        Err(test_soup_err())
    }

    async fn get_user_soup_with_properties_and_frecency<T>(
        &self,
        _req: soup::domain::models::SoupRequest<T>,
        _team_receipt: Option<EntityAccessReceipt<entity_access::domain::models::MemberTeamRole>>,
    ) -> Result<
        soup::domain::ports::SoupOutput<T, soup::domain::models::EnrichedSoupItem>,
        soup::domain::models::SoupErr,
    >
    where
        soup::domain::models::SoupRequest<T>: soup::domain::models::IntoSoupReqAst,
        T: Clone + serde::Serialize + Send,
    {
        Err(test_soup_err())
    }

    async fn get_user_soup_grouped(
        &self,
        _req: soup::domain::models::GroupedSortRequest<'_>,
    ) -> Result<
        impl Iterator<
            Item = soup::domain::models::grouping::ItemGroupingInfo<
                soup::domain::models::SoupPropertiesField,
            >,
        > + Send,
        soup::domain::models::SoupErr,
    > {
        self.grouped_calls.fetch_add(1, Ordering::SeqCst);
        Ok(vec![
            soup::domain::models::grouping::ItemGroupingInfo {
                sort_key: None,
                display_order: None,
                document_server_facts: Some(soup::domain::models::SoupDocumentServerFacts {
                    is_email_attachment: false,
                    is_important: true,
                    status_option_ids: Vec::new(),
                }),
                key: "document".to_string(),
                total_group_count: 3,
                index_in_group: 1,
                item: grouped_document(Uuid::from_u128(1)),
            },
            soup::domain::models::grouping::ItemGroupingInfo {
                sort_key: None,
                display_order: None,
                document_server_facts: None,
                key: "document".to_string(),
                total_group_count: 3,
                index_in_group: 2,
                item: grouped_document(Uuid::from_u128(2)),
            },
        ]
        .into_iter())
    }

    async fn group_labels<'a>(
        &'a self,
        _user_id: MacroUserIdStr<'a>,
        _group_by: &'a soup::domain::models::grouping::GroupByField,
        _keys: &'a [String],
    ) -> Result<soup::domain::models::grouping::GroupLabels, soup::domain::models::SoupErr> {
        Ok(Default::default())
    }

    async fn caller_tag_sets<'a>(
        &self,
        _user_id: MacroUserIdStr<'a>,
    ) -> Result<
        Vec<models_properties::service::property_definition_with_options::PropertyDefinitionWithOptions>,
        soup::domain::models::SoupErr,
    >{
        Err(test_soup_err())
    }
}

/// Email service whose inbox lookups are counted, so tests can assert when
/// the lazy extraction actually runs.
#[derive(Clone, Default)]
struct CountingEmailService {
    thread_read_fails: Arc<Mutex<bool>>,
    thread_read_redirect: Arc<Mutex<Option<(Uuid, Uuid)>>>,
    thread_is_read: Arc<Mutex<bool>>,
    thread_archived: Arc<Mutex<bool>>,
    archive_mutation_calls: Arc<Mutex<Vec<(MacroUserIdStr<'static>, Uuid, bool)>>>,
    inbox_calls: Arc<AtomicUsize>,
    user_label_calls: Arc<AtomicUsize>,
    user_link_calls: Arc<AtomicUsize>,
    user_catalog_identities: Arc<Mutex<Vec<MacroUserIdStr<'static>>>>,
    seen_mutation_calls: Arc<Mutex<Vec<(MacroUserIdStr<'static>, Uuid)>>>,
    unread_mutation_calls: Arc<Mutex<Vec<(MacroUserIdStr<'static>, Uuid)>>>,
    label_mutation_calls: Arc<Mutex<Vec<(MacroUserIdStr<'static>, Uuid, Uuid, bool)>>>,
    thread_preview_receipts: Arc<Mutex<Vec<Vec<Uuid>>>>,
}

impl email::domain::ports::EmailThreadPreviewService for CountingEmailService {
    async fn get_email_thread_previews_by_receipts(
        &self,
        user_id: MacroUserIdStr<'static>,
        receipts: Vec<EntityAccessReceipt<ViewAccessLevel>>,
    ) -> Result<Vec<EnrichedEmailThreadPreview>, EmailErr> {
        assert_eq!(user_id.as_ref(), VALID_USER_ID);
        let mut thread_ids = receipts
            .iter()
            .map(|receipt| Uuid::parse_str(&receipt.entity().entity_id).unwrap())
            .collect::<Vec<_>>();
        thread_ids.sort_unstable();
        self.thread_preview_receipts
            .lock()
            .unwrap()
            .push(thread_ids);
        // Only the email crate constructs previews; this fake observes which
        // receipts reached the read.
        Ok(Vec::new())
    }
}

impl graphql_soup::EmailMutationThreadReader for CountingEmailService {
    async fn read(
        &self,
        user_id: MacroUserIdStr<'static>,
        thread_id: Uuid,
    ) -> async_graphql::Result<Option<SoupEnrichedEmailThreadPreview<()>>> {
        assert_eq!(user_id.as_ref(), VALID_USER_ID);
        let SoupItem::EmailThread(mut thread) =
            soup_email_thread_with_read_status(thread_id, *self.thread_is_read.lock().unwrap())
        else {
            unreachable!()
        };
        thread.thread.inbox_visible = !*self.thread_archived.lock().unwrap();
        Ok(Some(thread))
    }
}

fn test_email_err() -> EmailErr {
    EmailErr::RepoErr(anyhow::anyhow!("counting email service"))
}

impl email::domain::send_attempt::EmailSendService for CountingEmailService {
    async fn pending_email_sends(
        &self,
        _: MacroUserIdStr<'static>,
        _: Option<email::domain::send_attempt::SendAttemptCursor>,
    ) -> Result<email::domain::send_attempt::SendAttemptPage, EmailErr> {
        Err(test_email_err())
    }

    async fn send_email(
        &self,
        _: MacroUserIdStr<'static>,
        _: Uuid,
        _: email::domain::send_attempt::SendAttemptId,
        _: email::domain::send_attempt::SendSnapshot,
    ) -> Result<email::domain::send_attempt::SendAttempt, EmailErr> {
        Err(test_email_err())
    }
    async fn cancel_email_send(
        &self,
        _: MacroUserIdStr<'static>,
        _: Uuid,
        _: email::domain::send_attempt::SendAttemptId,
    ) -> Result<email::domain::send_attempt::SendAttempt, EmailErr> {
        Err(test_email_err())
    }
    async fn email_send_status(
        &self,
        _: MacroUserIdStr<'static>,
        _: Uuid,
        _: email::domain::send_attempt::SendAttemptId,
    ) -> Result<Option<email::domain::send_attempt::SendAttempt>, EmailErr> {
        Err(test_email_err())
    }
}

impl EmailUserService for CountingEmailService {
    async fn get_user_email_senders(
        &self,
        _user: MacroUserIdStr<'static>,
        _link: Uuid,
    ) -> Result<Vec<email::domain::senders::SendAsAddress>, EmailErr> {
        Ok(vec![])
    }

    async fn get_user_email_labels(
        &self,
        macro_id: MacroUserIdStr<'static>,
    ) -> Result<Vec<LinkLabel>, EmailErr> {
        self.user_label_calls.fetch_add(1, Ordering::SeqCst);
        self.user_catalog_identities
            .lock()
            .expect("user catalog identities lock")
            .push(macro_id);
        Ok(vec![LinkLabel {
            id: Uuid::from_u128(501),
            link_id: Uuid::from_u128(502),
            provider_label_id: "Label_501".to_owned(),
            name: "Customers".to_owned(),
            created_at: Default::default(),
            message_list_visibility: MessageListVisibility::Show,
            label_list_visibility: LabelListVisibility::LabelShow,
            type_: LabelType::User,
        }])
    }

    async fn get_user_email_links(
        &self,
        macro_id: MacroUserIdStr<'static>,
    ) -> Result<Vec<UserEmailLink>, EmailErr> {
        self.user_link_calls.fetch_add(1, Ordering::SeqCst);
        self.user_catalog_identities
            .lock()
            .expect("user catalog identities lock")
            .push(macro_id);
        Ok(vec![UserEmailLink {
            draft_is_signal: true,
            id: Uuid::from_u128(502),
            macro_id: MacroUserIdStr::try_from_email("[email protected]").unwrap(),
            email_address: EmailStr::try_from("[email protected]".to_owned()).unwrap(),
            photo_url: Some("https://example.com/inbox.png".to_owned()),
            provider: UserProvider::Gmail,
            is_sync_active: true,
            sync_status: EmailSyncStatus::UpToDate,
            needs_reauth: false,
            settings: UserEmailLinkSettings {
                signature_on_replies_forwards: true,
                signature: Some("<p>Regards</p>".to_owned()),
            },
            is_primary: true,
            needs_calendar_permission: false,
            calendar_disabled: false,
            has_calendar_data: true,
            created_at: Default::default(),
            updated_at: Default::default(),
        }])
    }
}

impl EmailService for CountingEmailService {
    async fn resolve_thread_read_id(
        &self,
        macro_id: MacroUserIdStr<'_>,
        thread_id: Uuid,
    ) -> Result<Uuid, EmailErr> {
        assert_eq!(macro_id.as_ref(), VALID_USER_ID);
        if *self.thread_read_fails.lock().unwrap() {
            return Err(anyhow::anyhow!("private repository diagnostic").into());
        }
        Ok(self
            .thread_read_redirect
            .lock()
            .unwrap()
            .filter(|(source, _)| *source == thread_id)
            .map(|(_, canonical)| canonical)
            .unwrap_or(thread_id))
    }

    async fn resolve_message_operation(
        &self,
        _actor: macro_user_id::user_id::MacroUserIdStr<'static>,
        _request: email::domain::models::mailbox_operation::MessageResolutionRequest,
    ) -> Result<Uuid, EmailErr> {
        Err(EmailErr::RepoErr(anyhow::anyhow!("unused test capability")))
    }

    async fn send_message_for_user(
        &self,
        _user: MacroUserIdStr<'static>,
        _link: Option<Uuid>,
        _input: email::domain::models::CreateDraftInput,
    ) -> Result<email::domain::models::CreatedDraft, EmailErr> {
        Err(test_email_err())
    }

    async fn set_thread_archived(
        &self,
        user_id: MacroUserIdStr<'static>,
        thread_id: Uuid,
        archived: bool,
    ) -> Result<(), EmailErr> {
        *self.thread_archived.lock().unwrap() = archived;
        self.archive_mutation_calls
            .lock()
            .unwrap()
            .push((user_id, thread_id, archived));
        Ok(())
    }

    async fn set_threads_archived(
        &self,
        user_id: MacroUserIdStr<'static>,
        thread_ids: Vec<Uuid>,
        archived: bool,
    ) -> Result<SetThreadsArchivedResult, EmailErr> {
        *self.thread_archived.lock().unwrap() = archived;
        self.archive_mutation_calls.lock().unwrap().extend(
            thread_ids
                .iter()
                .map(|thread_id| (user_id.clone(), *thread_id, archived)),
        );
        Ok(SetThreadsArchivedResult {
            updated_thread_ids: thread_ids,
            failed_thread_ids: vec![],
        })
    }

    async fn get_email_thread_previews(
        &self,
        _req: GetEmailsRequest,
    ) -> Result<PaginatedCursor<EnrichedEmailThreadPreview, Uuid, SimpleSortMethod, ()>, EmailErr>
    {
        Err(test_email_err())
    }

    async fn get_link_by_auth_id_and_macro_id(
        &self,
        _auth_id: &str,
        _macro_id: MacroUserIdStr<'_>,
    ) -> Result<Option<Link>, EmailErr> {
        Err(test_email_err())
    }

    async fn resolve_sending_inbox(
        &self,
        _macro_id: MacroUserIdStr<'_>,
        _request: SendingInboxRequest<'_>,
    ) -> Result<ResolvedSendingInbox, EmailErr> {
        Err(test_email_err())
    }

    async fn get_inboxes_for_macro_id(
        &self,
        _macro_id: MacroUserIdStr<'_>,
    ) -> Result<Vec<Link>, EmailErr> {
        self.inbox_calls.fetch_add(1, Ordering::SeqCst);
        Ok(Vec::new())
    }

    async fn get_owned_link_for_thread(
        &self,
        _macro_id: MacroUserIdStr<'_>,
        _thread_id: Uuid,
    ) -> Result<Option<Link>, EmailErr> {
        Err(test_email_err())
    }

    async fn get_thread_with_messages(
        &self,
        _receipt: EntityAccessReceipt<ViewAccessLevel>,
        _offset: i64,
        _limit: i64,
    ) -> Result<Option<Thread>, EmailErr> {
        Err(test_email_err())
    }

    async fn get_thread_parsed(
        &self,
        _receipt: EntityAccessReceipt<ViewAccessLevel>,
        _offset: i64,
        _limit: i64,
    ) -> Result<Option<ParsedThread>, EmailErr> {
        Err(test_email_err())
    }

    async fn create_draft(
        &self,
        _link: &Link,
        _accessible_inboxes: &[Link],
        _input: CreateDraftInput,
    ) -> Result<CreatedDraft, EmailErr> {
        Err(test_email_err())
    }

    async fn save_draft_for_user(
        &self,
        _macro_id: MacroUserIdStr<'_>,
        _link_id: Option<Uuid>,
        _input: CreateDraftInput,
    ) -> Result<email::domain::models::SavedUserDraft, EmailErr> {
        Err(test_email_err())
    }

    async fn delete_draft_for_user(
        &self,
        _macro_id: MacroUserIdStr<'_>,
        _draft_id: Uuid,
        _expected_generation: i32,
    ) -> Result<email::domain::models::DeletedUserDraft, EmailErr> {
        Err(test_email_err())
    }

    async fn send_message(
        &self,
        _link: &Link,
        _accessible_inboxes: &[Link],
        _input: CreateDraftInput,
    ) -> Result<CreatedDraft, EmailErr> {
        Err(test_email_err())
    }

    async fn list_labels(&self, _link: &Link) -> Result<Vec<LinkLabel>, EmailErr> {
        Err(test_email_err())
    }

    async fn update_thread_labels(
        &self,
        _link: &Link,
        _thread_id: Uuid,
        _label_id: Uuid,
        _add: bool,
    ) -> Result<UpdateThreadLabelsResult, EmailErr> {
        Err(test_email_err())
    }

    async fn mark_thread_seen(
        &self,
        macro_id: MacroUserIdStr<'static>,
        thread_id: Uuid,
    ) -> Result<(), EmailErr> {
        *self.thread_is_read.lock().unwrap() = true;
        self.seen_mutation_calls
            .lock()
            .expect("seen mutation calls lock")
            .push((macro_id, thread_id));
        Ok(())
    }

    async fn mark_thread_unread(
        &self,
        macro_id: MacroUserIdStr<'static>,
        thread_id: Uuid,
    ) -> Result<(), EmailErr> {
        *self.thread_is_read.lock().unwrap() = false;
        self.unread_mutation_calls
            .lock()
            .expect("unread mutation calls lock")
            .push((macro_id, thread_id));
        Ok(())
    }

    async fn update_thread_labels_for_user(
        &self,
        macro_id: MacroUserIdStr<'static>,
        thread_id: Uuid,
        label_id: Uuid,
        add: bool,
    ) -> Result<UpdateThreadLabelsResult, EmailErr> {
        self.label_mutation_calls
            .lock()
            .expect("label mutation calls lock")
            .push((macro_id, thread_id, label_id, add));
        Ok(UpdateThreadLabelsResult {
            successful_ids: Vec::new(),
            failed_ids: Vec::new(),
        })
    }

    async fn update_thread_project(
        &self,
        _thread_receipt: EntityAccessReceipt<EditAccessLevel>,
        _project_receipt: Option<EntityAccessReceipt<EditAccessLevel>>,
    ) -> Result<Option<String>, EmailErr> {
        Err(test_email_err())
    }

    async fn upsert_email_filter(
        &self,
        _link: &Link,
        _input: UpsertEmailFilterInput,
    ) -> Result<EmailFilter, EmailErr> {
        Err(test_email_err())
    }

    async fn set_sender_policy(
        &self,
        _actor: &macro_user_id::user_id::MacroUserIdStr<'_>,
        _link: &Link,
        _sender_email: &str,
        _policy: SenderPolicy,
    ) -> Result<(), EmailErr> {
        Err(test_email_err())
    }

    async fn delete_email_filter(&self, _link: &Link, _filter_id: Uuid) -> Result<bool, EmailErr> {
        Err(test_email_err())
    }

    async fn list_email_filters(&self, _link: &Link) -> Result<Vec<EmailFilter>, EmailErr> {
        Err(test_email_err())
    }
}

#[tokio::test]
async fn email_thread_redirect_reauthorizes_the_canonical_soup_entity() {
    let source = Uuid::from_u128(420);
    let canonical = Uuid::from_u128(421);
    for visible in [None, Some(source), Some(canonical)] {
        let harness = harness();
        *harness.email_service.thread_read_redirect.lock().unwrap() = Some((source, canonical));
        // Visibility of the retained source alone must never satisfy the
        // redirected lookup's canonical entity authorization.
        harness
            .thread_reader
            .set_raw_threads(visible.into_iter().map(soup_email_thread).collect());
        let response = harness
            .execute(&format!(
                r#"{{ user {{ emailThread(input: {{threadId: "{source}"}}) {{ id }} }} }}"#
            ))
            .await;
        if visible == Some(source) {
            // Even a malformed loader response containing the shared source
            // cannot stand in for the requested canonical entity.
            assert_eq!(response.errors.len(), 1);
            assert!(response.errors[0].message.contains("unrequested entity"));
        } else {
            assert!(response.errors.is_empty(), "{:?}", response.errors);
        }
        let data = response.data.into_json().unwrap();
        if visible == Some(canonical) {
            assert_eq!(data["user"]["emailThread"]["id"], canonical.to_string());
        } else {
            assert!(data["user"]["emailThread"].is_null());
        }
    }
}

#[derive(Clone, Default)]
struct RecordingEmailContentReader {
    calls: Arc<Mutex<Vec<Vec<graphql_email::EmailContentKey>>>>,
    metadata_calls: Arc<Mutex<Vec<Vec<Uuid>>>>,
    mail_projection_calls: Arc<Mutex<Vec<Vec<Uuid>>>>,
}

impl graphql_email::SoupEmailThreadMetadataEdgeReader for RecordingEmailContentReader {
    async fn get_email_thread_metadata(
        &self,
        _user_id: &MacroUserIdStr<'static>,
        thread_ids: Vec<Uuid>,
    ) -> HashMap<Uuid, graphql_email::EmailThreadMetadataLoad> {
        self.metadata_calls
            .lock()
            .expect("email metadata calls lock")
            .push(thread_ids.clone());
        thread_ids
            .into_iter()
            .map(|thread_id| {
                (
                    thread_id,
                    graphql_email::EmailThreadMetadataLoad::Found(EmailThreadMetadata {
                        thread_id,
                        link_id: Uuid::from_u128(900 + thread_id.as_u128()),
                        has_received_mail: true,
                        latest_inbound_message_ts: (thread_id.as_u128() % 2 == 1)
                            .then(Default::default),
                        latest_outbound_message_ts: (thread_id.as_u128() % 2 == 1)
                            .then(Default::default),
                        latest_non_spam_message_ts: Some(Default::default()),
                        reminder_returned_at: (thread_id.as_u128() % 2 == 0).then(Default::default),
                    }),
                )
            })
            .collect()
    }
}

impl graphql_email::SoupEmailThreadMailProjectionEdgeReader for RecordingEmailContentReader {
    async fn get_email_thread_mail_projections(
        &self,
        _user_id: &MacroUserIdStr<'static>,
        thread_ids: Vec<Uuid>,
    ) -> HashMap<Uuid, graphql_email::EmailThreadMailProjectionLoad> {
        self.mail_projection_calls
            .lock()
            .unwrap()
            .push(thread_ids.clone());
        thread_ids
            .into_iter()
            .map(|thread_id| {
                (
                    thread_id,
                    graphql_email::EmailThreadMailProjectionLoad::Found(Arc::new(
                        EmailThreadMailProjection {
                            thread_id,
                            cache_facts: EmailThreadMailCacheFacts {
                                latest_non_spam_message_ts: None,
                                latest_outbound_message_ts: None,
                                has_calendar_attachment: false,
                                has_thread_share: false,
                            },
                            draft_state: None,
                            previews: EmailThreadMailPreviews {
                                all: None,
                                draft: None,
                                sent: None,
                            },
                        },
                    )),
                )
            })
            .collect()
    }
}

impl graphql_email::SoupEmailContentEdgeReader for RecordingEmailContentReader {
    async fn get_email_content(
        &self,
        _user_id: &MacroUserIdStr<'static>,
        keys: Vec<graphql_email::EmailContentKey>,
    ) -> HashMap<graphql_email::EmailContentKey, graphql_email::EmailContentLoad> {
        self.calls
            .lock()
            .expect("email content calls lock")
            .push(keys.clone());
        keys.into_iter()
            .map(|key| {
                let thread_id = key.thread_id;
                let message = if key.requires_full_payload() {
                    full_message(thread_id).into()
                } else {
                    parsed_message(thread_id).into()
                };
                (key, graphql_email::EmailContentLoad::Found(vec![message]))
            })
            .collect()
    }
}

#[derive(Clone, Default)]
struct RecordingActivityReader {
    edge_calls: Arc<Mutex<Vec<Vec<graphql_activity::ActivityEdgeKey>>>>,
    feed_calls: Arc<Mutex<Vec<(String, Option<(chrono::DateTime<chrono::Utc>, Uuid)>, u32)>>>,
    overview_calls: Arc<Mutex<Vec<(String, activity::ActivityWindow)>>>,
    records: Arc<Mutex<Vec<activity::ActivityRecord>>>,
}

impl RecordingActivityReader {
    fn set_records(&self, records: Vec<activity::ActivityRecord>) {
        *self.records.lock().expect("activity records lock") = records;
    }
}

impl graphql_activity::SoupActivityEdgeReader for RecordingActivityReader {
    async fn entity_activity(
        &self,
        keys: Vec<graphql_activity::ActivityEdgeKey>,
    ) -> HashMap<graphql_activity::ActivityEdgeKey, graphql_activity::ActivityEdgeLoad> {
        self.edge_calls
            .lock()
            .expect("activity edge calls lock")
            .push(keys.clone());
        let records = self.records.lock().expect("activity records lock").clone();
        keys.into_iter()
            .map(|key| {
                let matching = records
                    .iter()
                    .filter(|record| {
                        record.entity_type == key.entity.entity_type
                            && record.entity_id == key.entity.entity_id.as_ref()
                    })
                    .take(key.limit as usize)
                    .cloned()
                    .collect();
                (key, graphql_activity::ActivityEdgeLoad::Found(matching))
            })
            .collect()
    }
}

impl graphql_activity::ActivityFeedReader for RecordingActivityReader {
    async fn subject_feed(
        &self,
        subject_id: &str,
        cursor: Option<(chrono::DateTime<chrono::Utc>, Uuid)>,
        limit: std::num::NonZeroU32,
    ) -> Result<activity::ActivityFeedPage, graphql_activity::ActivityReadFailed> {
        let limit = limit.get();
        self.feed_calls
            .lock()
            .expect("activity feed calls lock")
            .push((subject_id.to_owned(), cursor, limit));
        // Emulate the repo: subject-scoped, newest-first keyset order,
        // strictly after the cursor position, at most `limit` rows, with
        // `next` set whenever more rows remain past the page.
        let records = self.records.lock().expect("activity records lock").clone();
        let mut page: Vec<activity::ActivityRecord> = records
            .into_iter()
            .filter(|record| record.subject_id == subject_id)
            .filter(|record| {
                cursor.is_none_or(|(occurred_at, id)| {
                    (record.occurred_at, record.id) < (occurred_at, id)
                })
            })
            .collect();
        let has_more = page.len() > limit as usize;
        page.truncate(limit as usize);
        let next = has_more
            .then(|| page.last().map(|record| (record.occurred_at, record.id)))
            .flatten();
        Ok(activity::ActivityFeedPage {
            records: page,
            next,
        })
    }

    async fn teammate_feed(
        &self,
        viewer_id: &str,
        cursor: Option<(chrono::DateTime<chrono::Utc>, Uuid)>,
        limit: std::num::NonZeroU32,
    ) -> Result<activity::ActivityFeedPage, graphql_activity::ActivityReadFailed> {
        // Emulate the repo with everyone but the viewer as a teammate, in
        // the same keyset order and raw-row `next` as `subject_feed`.
        let limit = limit.get() as usize;
        let records = self.records.lock().expect("activity records lock").clone();
        let mut page: Vec<activity::ActivityRecord> = records
            .into_iter()
            .filter(|record| record.subject_id != viewer_id)
            .filter(|record| {
                cursor.is_none_or(|(occurred_at, id)| {
                    (record.occurred_at, record.id) < (occurred_at, id)
                })
            })
            .collect();
        let has_more = page.len() > limit;
        page.truncate(limit);
        let next = has_more
            .then(|| page.last().map(|record| (record.occurred_at, record.id)))
            .flatten();
        Ok(activity::ActivityFeedPage {
            records: page,
            next,
        })
    }

    async fn subject_overview(
        &self,
        subject_id: &str,
        window: activity::ActivityWindow,
    ) -> Result<activity::ActivityOverview, graphql_activity::ActivityReadFailed> {
        self.overview_calls
            .lock()
            .expect("activity overview calls lock")
            .push((subject_id.to_owned(), window.clone()));
        Ok(activity::ActivityOverview::new(
            window.clone(),
            vec![activity::DayCount {
                day: window.start,
                count: std::num::NonZeroU64::new(2).unwrap(),
            }],
            vec![activity::EntityRank {
                entity_type: ModelEntityType::Document,
                entity_id: "overview-doc".to_owned(),
                count: std::num::NonZeroU64::new(2).unwrap(),
            }],
        )
        .unwrap())
    }
}

fn parsed_message(thread_id: Uuid) -> ParsedMessage {
    ParsedMessage {
        draft_generation: 0,
        db_id: Uuid::from_u128(100),
        link_id: Uuid::from_u128(200),
        thread_db_id: thread_id,
        subject: Some("Direct thread subject".to_owned()),
        snippet: Some("Direct thread snippet".to_owned()),
        from: None,
        to: Vec::new(),
        cc: Vec::new(),
        bcc: Vec::new(),
        labels: Vec::new(),
        body_parsed: Some("Direct thread body".to_owned()),
        body_text: Some("Direct thread body".to_owned()),
        body_html_sanitized: None,
        body_macro: None,
        body_replyless: Some("Direct thread body".to_owned()),
        internal_date_ts: Some(Default::default()),
        sent_at: Some(Default::default()),
        is_read: true,
        is_starred: false,
        is_sent: false,
        is_draft: false,
        has_attachments: false,
        created_at: Default::default(),
        updated_at: Default::default(),
    }
}

fn full_message(thread_id: Uuid) -> Message {
    let message_id = Uuid::from_u128(100);
    let draft_attachment_id = Uuid::from_u128(102);
    Message {
        draft_generation: 0,
        operation_status: None,
        calendar_invitations: Default::default(),
        db_id: message_id,
        provider_id: Some("provider-message".to_owned()),
        thread_db_id: thread_id,
        provider_thread_id: Some("provider-thread".to_owned()),
        replying_to_id: Some(Uuid::from_u128(101)),
        global_id: Some("global-message".to_owned()),
        link_id: Uuid::from_u128(200),
        subject: Some("Direct thread subject".to_owned()),
        snippet: Some("Direct thread snippet".to_owned()),
        provider_history_id: None,
        internal_date_ts: Some(Default::default()),
        sent_at: Some(Default::default()),
        size_estimate: None,
        is_read: true,
        is_starred: false,
        is_sent: false,
        is_draft: true,
        has_attachments: true,
        scheduled_send_time: Some(Default::default()),
        scheduled_send_status: Some(email::domain::models::ScheduledSendStatus::Failed),
        from: None,
        to: Vec::new(),
        cc: Vec::new(),
        bcc: Vec::new(),
        labels: Vec::new(),
        body_text: Some("Direct thread body".to_owned()),
        body_html_sanitized: None,
        body_macro: None,
        body_replyless: Some("Direct thread body".to_owned()),
        attachments: vec![MessageAttachment {
            reference_url: Some("https://outlook.office.com/mail/message".into()),
            db_id: Uuid::from_u128(103),
            provider_id: Some("provider-attachment".to_owned()),
            filename: Some("provider.txt".to_owned()),
            mime_type: Some("text/plain".to_owned()),
            size_bytes: Some(10),
            sfs_id: Some(Uuid::from_u128(104)),
            content_id: Some("inline-content".to_owned()),
        }],
        attachments_draft: vec![AttachmentDraft {
            upload_pending: false,
            content_id: None,
            is_inline: false,
            id: draft_attachment_id,
            draft_id: message_id,
            file_name: "draft.txt".to_owned(),
            content_type: "text/plain".to_owned(),
            sha: "sha256".to_owned(),
            size: 20,
            s3_key: "draft/key".to_owned(),
        }],
        attachments_forwarded: vec![AttachmentForwarded {
            attachment_id: Uuid::from_u128(105),
            draft_id: message_id,
            provider_attachment_id: Some("forwarded-attachment".to_owned()),
            message_provider_id: "original-provider-message".to_owned(),
            filename: Some("forwarded.txt".to_owned()),
            mime_type: Some("text/plain".to_owned()),
            size_bytes: Some(30),
        }],
        headers_json: None,
        created_at: Default::default(),
        updated_at: Default::default(),
    }
}

/// Entity access service whose team lookups are counted and return a team,
/// and which grants View on the configured email threads only.
#[derive(Clone, Default)]
struct CountingEntityAccessService {
    team_calls: Arc<AtomicUsize>,
    viewable_email_threads: Arc<Mutex<std::collections::HashSet<Uuid>>>,
    email_thread_checks: Arc<Mutex<Vec<Uuid>>>,
}

impl EntityAccessService for CountingEntityAccessService {
    async fn generate_entity_access_receipt<T: RequiredPermission>(
        &self,
        _user_id: &MacroUserId<Lowercase<'_>>,
        _user_org_id: Option<i64>,
        entity_id: &str,
        entity_type: EntityType,
    ) -> Result<EntityAccessReceipt<T>, AccessError> {
        // The viewer views exactly one database, responds to one form (View),
        // edits another, and reaches nothing else. A level the grant does not
        // satisfy is refused as the real service refuses it.
        let grant = match (entity_type, entity_id) {
            (EntityType::Database, database_activity::VIEWABLE_DATABASE_ID)
            | (EntityType::Form, form_activity::RESPONDED_FORM_ID) => Some(AccessLevel::View),
            (EntityType::Form, form_activity::EDITABLE_FORM_ID) => Some(AccessLevel::Edit),
            (EntityType::Database | EntityType::Form, _) => None,
            (EntityType::Team, _) => {
                return EntityAccessReceipt::try_new_authenticated_user(
                    MacroUserIdStr::parse_from_str(VALID_USER_ID).unwrap(),
                    entity_access::domain::models::Entity {
                        entity_id: entity_id.to_owned(),
                        entity_type,
                    },
                    EntityPermission::TeamRole {
                        role: TeamRole::Member,
                    },
                );
            }
            (EntityType::EmailThread, _) => {
                let thread_id = Uuid::parse_str(entity_id).unwrap();
                self.email_thread_checks.lock().unwrap().push(thread_id);
                self.viewable_email_threads
                    .lock()
                    .unwrap()
                    .contains(&thread_id)
                    .then_some(AccessLevel::View)
            }
            _ => return Err(AccessError::internal("test access failure")),
        };
        if let Some(access_level) = grant {
            return EntityAccessReceipt::try_new_authenticated_user(
                MacroUserIdStr::parse_from_str(VALID_USER_ID).unwrap(),
                entity_access::domain::models::Entity {
                    entity_id: entity_id.to_owned(),
                    entity_type,
                },
                EntityPermission::AccessLevel { access_level },
            );
        }
        if matches!(
            entity_type,
            EntityType::Database | EntityType::Form | EntityType::EmailThread
        ) {
            return Err(AccessError::Unauthorized);
        }
        Err(AccessError::internal("test access failure"))
    }

    async fn generate_bot_entity_access_receipt<T: RequiredPermission>(
        &self,
        _bot_id: BotId,
        _scope: BotAccessScope,
        _entity_id: &str,
        _entity_type: EntityType,
    ) -> Result<EntityAccessReceipt<T>, AccessError> {
        Err(AccessError::internal("test access failure"))
    }

    async fn get_access_level(
        &self,
        _user_id: Option<&MacroUserId<Lowercase<'_>>>,
        _entity_id: &str,
        _entity_type: EntityType,
    ) -> Result<Option<AccessLevel>, AccessError> {
        Err(AccessError::internal("test access failure"))
    }

    async fn check_access(
        &self,
        _user_id: Option<&MacroUserId<Lowercase<'_>>>,
        _entity_id: &str,
        _entity_type: EntityType,
        _required_level: AccessLevel,
    ) -> Result<AccessLevel, AccessError> {
        Err(AccessError::internal("test access failure"))
    }

    async fn check_public_access(
        &self,
        _entity_id: &str,
        _entity_type: EntityType,
        _required_level: AccessLevel,
    ) -> Result<AccessLevel, AccessError> {
        Err(AccessError::internal("test access failure"))
    }

    async fn get_entity_permission(
        &self,
        _user_id: Option<&MacroUserId<Lowercase<'_>>>,
        _entity_id: &str,
        _entity_type: EntityType,
        _user_org_id: Option<i64>,
    ) -> Result<EntityPermission, AccessError> {
        Err(AccessError::internal("test access failure"))
    }

    async fn get_crm_entity_permission_with_team(
        &self,
        _user_id: Option<&MacroUserId<Lowercase<'_>>>,
        _entity_id: &str,
        _entity_type: EntityType,
    ) -> Result<(EntityPermission, Uuid, TeamRole), AccessError> {
        Err(AccessError::internal("test access failure"))
    }

    async fn get_users_by_entity(
        &self,
        _entity_id: &str,
        _entity_type: EntityType,
    ) -> Result<Vec<MacroUserIdStr<'static>>, AccessError> {
        Err(AccessError::internal("test access failure"))
    }

    async fn get_call_channel(
        &self,
        _call_id: &Uuid,
    ) -> Result<Option<CallChannelInfo>, AccessError> {
        Err(AccessError::internal("test access failure"))
    }

    async fn get_call_channel_by_channel_id(
        &self,
        _channel_id: &Uuid,
    ) -> Result<Option<CallChannelInfo>, AccessError> {
        Err(AccessError::internal("test access failure"))
    }

    async fn get_user_team(
        &self,
        _user_id: &MacroUserId<Lowercase<'_>>,
    ) -> Result<Option<UserTeamInfo>, AccessError> {
        self.team_calls.fetch_add(1, Ordering::SeqCst);
        Ok(Some(UserTeamInfo {
            team_id: Uuid::from_u128(42),
            role: TeamRole::Member,
        }))
    }
}

#[derive(Clone, Default)]
struct FakeAuthorizationService {
    authorization_calls: Arc<AtomicUsize>,
}

impl MacroAuthorizationService for FakeAuthorizationService {
    async fn authorize(&self, jwt: &str) -> Result<UserContext, Report<MacroAuthorizationError>> {
        self.authorization_calls.fetch_add(1, Ordering::SeqCst);

        match jwt {
            "valid" => Ok(user_context(VALID_USER_ID)),
            "expired" => Err(Report::new(MacroAuthorizationError::CredentialsExpired)),
            _ => Err(Report::new(MacroAuthorizationError::InvalidCredentials)),
        }
    }

    async fn authorize_identity(
        &self,
        jwt: &str,
    ) -> Result<macro_authorization::ValidatedCredential, Report<MacroAuthorizationError>> {
        self.authorize(jwt)
            .await
            .map(macro_authorization::ValidatedCredential::session)
    }

    async fn authorize_internal(
        &self,
        provided_key: &str,
        claims: InternalIdentityClaims,
    ) -> Result<Option<UserContext>, Report<MacroAuthorizationError>> {
        self.authorization_calls.fetch_add(1, Ordering::SeqCst);

        if provided_key != VALID_INTERNAL_KEY {
            return Err(Report::new(MacroAuthorizationError::InvalidCredentials));
        }

        Ok(claims.user_id.map(|user_id| user_context(&user_id)))
    }
}

fn user_context(user_id: &str) -> UserContext {
    UserContext {
        user_id: user_id.to_owned(),
        fusion_user_id: "fusion-user-id".to_owned(),
        permissions: None,
        organization_id: None,
    }
}

#[derive(Clone)]
struct TestState {
    authorization: MacroAuthorizationState<FakeAuthorizationService>,
    email: EmailRouterState<CountingEmailService>,
    entity_access: Arc<CountingEntityAccessService>,
}

impl FromRef<TestState> for MacroAuthorizationState<FakeAuthorizationService> {
    fn from_ref(state: &TestState) -> Self {
        state.authorization.clone()
    }
}

impl FromRef<TestState> for EmailRouterState<CountingEmailService> {
    fn from_ref(state: &TestState) -> Self {
        state.email.clone()
    }
}

impl FromRef<TestState> for Arc<CountingEntityAccessService> {
    fn from_ref(state: &TestState) -> Self {
        state.entity_access.clone()
    }
}

/// Soup email thread reader standing in for the replica-backed email reader.
#[derive(Clone, Default)]
struct RecordingThreadReader {
    reads: Arc<AtomicUsize>,
    return_unrequested_threads: Arc<AtomicBool>,
    threads: Arc<Mutex<Vec<SoupItem<()>>>>,
}

impl RecordingThreadReader {
    fn set_raw_threads(&self, threads: Vec<SoupItem<()>>) {
        self.set_threads(threads);
        self.return_unrequested_threads
            .store(true, Ordering::SeqCst);
    }

    fn set_threads(&self, threads: Vec<SoupItem<()>>) {
        *self.threads.lock().unwrap() = threads;
    }
}

impl graphql_soup::SoupEmailThreadReader for RecordingThreadReader {
    async fn read_threads(
        &self,
        user_id: MacroUserIdStr<'static>,
        thread_ids: Vec<Uuid>,
    ) -> Result<Vec<SoupEnrichedEmailThreadPreview<()>>, graphql_soup::SoupItemLoaderError> {
        assert_eq!(user_id.as_ref(), VALID_USER_ID);
        self.reads.fetch_add(1, Ordering::SeqCst);
        Ok(self
            .threads
            .lock()
            .unwrap()
            .iter()
            .filter_map(|item| match item {
                SoupItem::EmailThread(thread)
                    if self.return_unrequested_threads.load(Ordering::SeqCst)
                        || thread_ids.contains(&thread.thread.id) =>
                {
                    Some(thread.clone())
                }
                _ => None,
            })
            .collect())
    }
}

struct TestHarness {
    schema: SoupSchema<
        CountingSoupService,
        NoOpSoupRealtimeSubscriptionService,
        NoopWebSocketNotificationSubscriptionService,
        graphql_activity::NoOpActivitySubscriptionService,
        CountingEmailService,
        CountingEntityAccessService,
        FakeAuthorizationService,
        TestState,
        NoOpEntityPropertyWriter,
        UnavailableEntityMutationService,
        graphql_favorite::NoOpFavoriteMutationService,
        NoOpChannelActivityMutationService,
        NoOpNotificationMutationService,
        NoOpSoupNotificationEdgeReader,
        NoOpEntityPropertyReader,
        RecordingEmailContentReader,
        graphql_favorite::NoOpEntityFavoriteEdgeReader,
        graphql_permission::NoOpEntityPermissionEdgeReader,
        RecordingActivityReader,
    >,
    state: TestState,
    soup_service: CountingSoupService,
    /// Answers single agent-session lookups, as the primary does in DSS.
    primary_soup_service: CountingSoupService,
    email_service: CountingEmailService,
    email_content_reader: RecordingEmailContentReader,
    thread_reader: RecordingThreadReader,
    activity_reader: RecordingActivityReader,
    authorization_calls: Arc<AtomicUsize>,
    inbox_calls: Arc<AtomicUsize>,
    thread_reads: Arc<AtomicUsize>,
    user_label_calls: Arc<AtomicUsize>,
    user_link_calls: Arc<AtomicUsize>,
    user_catalog_identities: Arc<Mutex<Vec<MacroUserIdStr<'static>>>>,
    team_calls: Arc<AtomicUsize>,
    raw_soup_calls: Arc<AtomicUsize>,
    raw_soup_team_receipts: Arc<AtomicUsize>,
    frecency_soup_calls: Arc<AtomicUsize>,
    frecency_soup_team_receipts: Arc<AtomicUsize>,
    grouped_soup_calls: Arc<AtomicUsize>,
}

fn harness() -> TestHarness {
    let email = CountingEmailService::default();
    let entity_access = CountingEntityAccessService::default();
    let authorization = FakeAuthorizationService::default();
    let soup = CountingSoupService::default();
    let email_content_reader = RecordingEmailContentReader::default();
    let thread_reader = RecordingThreadReader::default();
    let activity_reader = RecordingActivityReader::default();
    let authorization_calls = Arc::clone(&authorization.authorization_calls);
    let inbox_calls = Arc::clone(&email.inbox_calls);
    let thread_reads = Arc::clone(&thread_reader.reads);
    let user_label_calls = Arc::clone(&email.user_label_calls);
    let user_link_calls = Arc::clone(&email.user_link_calls);
    let user_catalog_identities = Arc::clone(&email.user_catalog_identities);
    let team_calls = Arc::clone(&entity_access.team_calls);
    let raw_soup_calls = Arc::clone(&soup.raw_calls);
    let raw_soup_team_receipts = Arc::clone(&soup.raw_team_receipts);
    let frecency_soup_calls = Arc::clone(&soup.frecency_calls);
    let frecency_soup_team_receipts = Arc::clone(&soup.frecency_team_receipts);
    let grouped_soup_calls = Arc::clone(&soup.grouped_calls);
    TestHarness {
        schema: build_schema_with_service(soup.clone()),
        state: TestState {
            authorization: MacroAuthorizationState::new(Arc::new(authorization)),
            email: EmailRouterState::new(email.clone()),
            entity_access: Arc::new(entity_access),
        },
        soup_service: soup,
        primary_soup_service: CountingSoupService::default(),
        email_service: email,
        email_content_reader,
        thread_reader,
        activity_reader,
        authorization_calls,
        inbox_calls,
        thread_reads,
        user_label_calls,
        user_link_calls,
        user_catalog_identities,
        team_calls,
        raw_soup_calls,
        raw_soup_team_receipts,
        frecency_soup_calls,
        frecency_soup_team_receipts,
        grouped_soup_calls,
    }
}

fn authenticated_parts() -> axum::http::request::Parts {
    bearer_parts("valid")
}

fn bearer_parts(token: &str) -> axum::http::request::Parts {
    let request = HttpRequest::builder()
        .header(header::AUTHORIZATION, format!("Bearer {token}"))
        .body(())
        .unwrap();
    request.into_parts().0
}

fn internal_parts(user_id: &str) -> axum::http::request::Parts {
    let request = HttpRequest::builder()
        .header(INTERNAL_API_KEY_HEADER, VALID_INTERNAL_KEY)
        .header(INTERNAL_MACRO_USER_ID_HEADER, user_id)
        .body(())
        .unwrap();
    request.into_parts().0
}

impl TestHarness {
    async fn execute(&self, query: &str) -> async_graphql::Response {
        self.execute_with_parts(query, authenticated_parts()).await
    }

    async fn execute_with_parts(
        &self,
        query: &str,
        parts: axum::http::request::Parts,
    ) -> async_graphql::Response {
        self.schema.execute(self.request(query, parts)).await
    }

    async fn execute_authenticated_mutation(&self, mutation: &str) -> async_graphql::Response {
        let user_id = MacroUserIdStr::parse_from_str(VALID_USER_ID).unwrap();
        self.schema
            .execute(self.request(mutation, authenticated_parts()).data(user_id))
            .await
    }

    fn request(&self, query: &str, parts: axum::http::request::Parts) -> async_graphql::Request {
        let user_id = MacroUserIdStr::parse_from_str(VALID_USER_ID).unwrap();
        async_graphql::Request::new(query)
            .data(GraphqlRequestParts::new(parts))
            .data(self.state.clone())
            .data(self.state.email.service())
            .data(graphql_soup::EmailMutationThreadLoader::new(
                self.email_service.clone(),
            ))
            .data(graphql_soup::SoupItemDataLoader::new(
                graphql_soup::SoupItemLoader::new(
                    self.soup_service.clone(),
                    self.thread_reader.clone(),
                ),
            ))
            .data(graphql_soup::AgentSessionEntityLoader::new(
                self.primary_soup_service.clone(),
                Arc::new(self.email_service.clone()),
                self.state.entity_access.as_ref().clone(),
            ))
            .data(graphql_soup::PrimarySoupItemLoader::new(
                self.primary_soup_service.clone(),
                Arc::new(self.email_service.clone()),
                self.state.entity_access.as_ref().clone(),
            ))
            .data(graphql_properties::entity_properties_loader(
                user_id.clone(),
                NoOpEntityPropertyReader,
            ))
            .data(graphql_email::email_content_loader(
                user_id.clone(),
                self.email_content_reader.clone(),
            ))
            .data(graphql_email::email_thread_metadata_loader(
                user_id.clone(),
                None,
                self.email_content_reader.clone(),
            ))
            .data(graphql_email::email_thread_mail_projection_loader(
                user_id,
                None,
                self.email_content_reader.clone(),
            ))
            .data(NoOpEntityFavoriteEdgeReader)
            .data(graphql_soup::FoldersQuery::noop())
            .data(self.activity_reader.clone())
            .data(graphql_activity::entity_activity_loader(
                self.activity_reader.clone(),
            ))
    }
}

#[tokio::test]
async fn soup_updates_subscribes_as_the_authenticated_user() {
    use async_graphql::futures_util::{StreamExt as _, pin_mut};

    let user_id = MacroUserIdStr::parse_from_str(VALID_USER_ID).unwrap();
    let subscribed_user = Arc::new(Mutex::new(None));
    let (sender, receiver) = tokio::sync::mpsc::channel(2);
    let realtime = TestRealtimeSubscriptionService {
        receiver: Arc::new(Mutex::new(Some(receiver))),
        subscribed_user: Arc::clone(&subscribed_user),
    };
    let soup_service = CountingSoupService::default();
    let document_id = Uuid::from_u128(42);
    let deleted_document_id = Uuid::from_u128(43);
    soup_service.set_raw_response(vec![soup_document(document_id)]);
    let loader = graphql_soup::PrimarySoupItemLoader::new(
        soup_service.clone(),
        Arc::new(NoOpEmailService),
        NoOpEntityAccessService,
    );
    let schema: SoupSchema<
        CountingSoupService,
        TestRealtimeSubscriptionService,
        NoopWebSocketNotificationSubscriptionService,
        graphql_activity::NoOpActivitySubscriptionService,
        NoOpEmailService,
        NoOpEntityAccessService,
        SchemaOnlyAuthorizationService,
        SchemaOnlyState,
        NoOpEntityPropertyWriter,
        UnavailableEntityMutationService,
        NoOpFavoriteMutationService,
        NoOpChannelActivityMutationService,
        NoOpNotificationMutationService,
        NoOpSoupNotificationEdgeReader,
        NoOpEntityPropertyReader,
        NoOpSoupEmailContentEdgeReader,
        NoOpEntityFavoriteEdgeReader,
        NoOpEntityPermissionEdgeReader,
        NoOpActivityReader,
    > = build_schema_with_services(
        soup_service,
        realtime,
        NoopWebSocketNotificationSubscriptionService,
        graphql_activity::NoOpActivitySubscriptionService,
    );
    let request = async_graphql::Request::new(
        "subscription { soupUpdates { __typename ... on SoupUpdated { item { id cacheProjection } } ... on GraphqlCacheDeletion { graphqlTypeName entityId } } }",
    )
    .data(user_id.clone())
    .data(loader);
    let responses = schema.execute_stream(request);
    pin_mut!(responses);

    sender
        .send(Patch::Updated(
            ModelEntityType::Document.with_entity_string(document_id.to_string()),
        ))
        .await
        .expect("subscription remains open");
    sender
        .send(Patch::Deleted(
            ModelEntityType::Document.with_entity_string(deleted_document_id.to_string()),
        ))
        .await
        .expect("subscription remains open");
    let mut updates = Vec::new();
    while updates.len() < 2 {
        let response = responses.next().await.expect("subscription response");
        assert!(response.errors.is_empty(), "{:?}", response.errors);
        let data = response.data.into_json().expect("response data is JSON");
        updates.extend(
            data["soupUpdates"]
                .as_array()
                .expect("soupUpdates is a buffered list")
                .iter()
                .cloned(),
        );
    }

    assert_eq!(updates.len(), 2);
    assert_eq!(updates[0]["__typename"], "SoupUpdated");
    assert_eq!(updates[0]["item"]["id"], document_id.to_string());
    assert!(updates[0]["item"]["cacheProjection"].is_string());
    assert_eq!(updates[1]["__typename"], "GraphqlCacheDeletion");
    assert_eq!(updates[1]["graphqlTypeName"], "GraphqlSoupDocument");
    assert_eq!(updates[1]["entityId"], deleted_document_id.to_string());
    assert_eq!(
        subscribed_user
            .lock()
            .expect("subscribed user lock")
            .as_ref(),
        Some(&user_id)
    );
}

#[tokio::test]
async fn agent_session_is_read_from_the_primary_not_the_list_replica() {
    let harness = harness();
    let session_id = Uuid::from_u128(7);
    // Just created: the replica behind Soup lists has not seen the session yet.
    harness.soup_service.set_raw_response(Vec::new());
    harness
        .primary_soup_service
        .set_raw_response(vec![soup_agent_session(session_id)]);

    let response = harness
        .execute(&format!(
            r#"{{ user {{ agentSession(sessionId: "{session_id}") {{ id }} }} }}"#
        ))
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let data = response.data.into_json().unwrap();
    assert_eq!(data["user"]["agentSession"]["id"], session_id.to_string());
    assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);
}

#[tokio::test]
async fn agent_session_read_carries_the_viewer_team_for_its_linked_pr() {
    let harness = harness();
    let session_id = Uuid::from_u128(7);
    harness
        .primary_soup_service
        .set_raw_response(vec![soup_agent_session(session_id)]);

    let response = harness
        .execute(&format!(
            r#"{{ user {{ agentSession(sessionId: "{session_id}") {{ id }} }} }}"#
        ))
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    // Linked PRs are stored for the team; without its receipt the row's PR
    // state reads as unknown and overwrites the list's known state.
    assert_eq!(
        harness
            .primary_soup_service
            .raw_team_receipts
            .load(Ordering::SeqCst),
        1
    );
}

#[tokio::test]
async fn soup_entity_is_read_from_the_primary_not_the_list_replica() {
    let harness = harness();
    let document_id = Uuid::from_u128(8);
    // Just created: the replica behind Soup lists has not replayed the task yet.
    harness.soup_service.set_raw_response(Vec::new());
    harness
        .primary_soup_service
        .set_raw_response(vec![soup_document(document_id)]);

    let response = harness
        .execute(&format!(
            r#"{{ user {{ soupEntity(input: {{entityType: DOCUMENT, entityId: "{document_id}"}}) {{ __typename id }} }} }}"#
        ))
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let data = response.data.into_json().unwrap();
    assert_eq!(
        data["user"]["soupEntity"]["__typename"],
        "GraphqlSoupDocument"
    );
    assert_eq!(data["user"]["soupEntity"]["id"], document_id.to_string());
    assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);
}

#[tokio::test]
async fn soup_entity_is_null_when_the_viewer_cannot_see_it() {
    let harness = harness();
    harness.primary_soup_service.set_raw_response(Vec::new());

    let response = harness
        .execute(&format!(
            r#"{{ user {{ soupEntity(input: {{entityType: DOCUMENT, entityId: "{}"}}) {{ id }} }} }}"#,
            Uuid::from_u128(9)
        ))
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let data = response.data.into_json().unwrap();
    assert!(data["user"]["soupEntity"].is_null());
}

#[tokio::test]
async fn agent_session_log_appended_streams_runs_for_an_accessible_session() {
    use agent_runtime_protocol::domain::schema::v0::{SystemEvent, ToServerMessage};
    use agent_session::domain::model::{
        AgentSessionId, AgentSessionLog, Message, StoredAgentSessionLog,
    };
    use agent_session_realtime::domain::ports::AgentSessionLogSubscriptionService;
    use async_graphql::futures_util::{StreamExt as _, pin_mut};

    struct TestLogSubscriptions {
        receiver: Arc<Mutex<Option<tokio::sync::mpsc::Receiver<Vec<StoredAgentSessionLog>>>>>,
        subscribed: Arc<Mutex<Option<AgentSessionId>>>,
    }

    impl AgentSessionLogSubscriptionService for TestLogSubscriptions {
        fn subscribe(
            &self,
            session_id: AgentSessionId,
        ) -> tokio::sync::mpsc::Receiver<Vec<StoredAgentSessionLog>> {
            *self.subscribed.lock().expect("subscribed lock") = Some(session_id);
            self.receiver
                .lock()
                .expect("receiver lock")
                .take()
                .expect("subscribed once")
        }
    }

    let user_id = MacroUserIdStr::parse_from_str(VALID_USER_ID).unwrap();
    let session_id = Uuid::from_u128(7);
    let other_session_id = Uuid::from_u128(8);
    let (sender, receiver) = tokio::sync::mpsc::channel(2);
    let subscribed = Arc::new(Mutex::new(None));
    let subscriptions = crate::agent_session_log_subscriptions(TestLogSubscriptions {
        receiver: Arc::new(Mutex::new(Some(receiver))),
        subscribed: Arc::clone(&subscribed),
    });
    let soup_service = CountingSoupService::default();
    soup_service.set_raw_response(vec![soup_agent_session(session_id)]);
    let loader = graphql_soup::AgentSessionEntityLoader::new(
        soup_service.clone(),
        Arc::new(NoOpEmailService),
        NoOpEntityAccessService,
    );
    let schema = build_schema_with_service::<
        CountingSoupService,
        NoOpEmailService,
        NoOpEntityAccessService,
        SchemaOnlyAuthorizationService,
        SchemaOnlyState,
        NoOpEntityPropertyWriter,
        UnavailableEntityMutationService,
        NoOpFavoriteMutationService,
        NoOpChannelActivityMutationService,
        NoOpNotificationMutationService,
        NoOpSoupNotificationEdgeReader,
        NoOpEntityPropertyReader,
        NoOpSoupEmailContentEdgeReader,
        NoOpEntityFavoriteEdgeReader,
        NoOpEntityPermissionEdgeReader,
        NoOpActivityReader,
    >(soup_service);
    let query = format!(
        "subscription {{ agentSessionLogAppended(sessionId: \"{session_id}\") {{ id userId direction content }} }}"
    );

    // A session the viewer cannot see cannot be followed.
    let refused = schema
        .execute_stream(
            async_graphql::Request::new(format!(
                "subscription {{ agentSessionLogAppended(sessionId: \"{other_session_id}\") {{ id }} }}"
            ))
            .data(user_id.clone())
            .data(loader.clone())
            .data(subscriptions.clone()),
        )
        .next()
        .await
        .expect("one response");
    assert!(!refused.errors.is_empty());
    assert!(subscribed.lock().expect("subscribed lock").is_none());

    let responses = schema.execute_stream(
        async_graphql::Request::new(query)
            .data(user_id)
            .data(loader)
            .data(subscriptions),
    );
    pin_mut!(responses);
    sender
        .send(vec![StoredAgentSessionLog {
            id: Uuid::from_u128(1),
            created_at: Default::default(),
            entry: AgentSessionLog {
                agent_session_id: AgentSessionId::new_from_uuid(session_id),
                user_id: None,
                content: Message::ToServer(ToServerMessage::Event {
                    event: SystemEvent::AcpReady,
                }),
                logged_at: None,
            },
        }])
        .await
        .expect("subscription remains open");
    drop(sender);

    let response = responses.next().await.expect("a run");
    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let data = response.data.into_json().expect("response data is JSON");
    let run = data["agentSessionLogAppended"]
        .as_array()
        .expect("a run is a list");
    assert_eq!(run.len(), 1);
    assert_eq!(run[0]["id"], Uuid::from_u128(1).to_string());
    assert_eq!(run[0]["direction"], "to_server");
    assert!(run[0]["content"].is_object());
    assert_eq!(
        *subscribed.lock().expect("subscribed lock"),
        Some(AgentSessionId::new_from_uuid(session_id))
    );
    // The fan-out closing means rows were missed: the stream ends with an error.
    let closed = responses.next().await.expect("a closing response");
    assert!(!closed.errors.is_empty());
    assert!(responses.next().await.is_none());
}

#[tokio::test]
async fn activity_updates_subscribes_as_the_authenticated_user() {
    use activity::{
        ActivitySubscription, ActivitySubscriptionExit, ActivitySubscriptionService,
        ActivitySubscriptionUpdate,
    };
    use async_graphql::futures_util::{StreamExt as _, pin_mut};

    let user_id = MacroUserIdStr::parse_from_str(VALID_USER_ID).unwrap();
    let subscribed_user: Arc<Mutex<Option<MacroUserIdStr<'static>>>> = Arc::default();

    struct TestActivitySubscriptionService {
        subscribed_user: Arc<Mutex<Option<MacroUserIdStr<'static>>>>,
        subscription: Mutex<Option<ActivitySubscription>>,
    }

    impl ActivitySubscriptionService for TestActivitySubscriptionService {
        fn subscribe(&self, user_id: MacroUserIdStr<'static>) -> ActivitySubscription {
            *self.subscribed_user.lock().expect("subscribed user lock") = Some(user_id);
            self.subscription
                .lock()
                .expect("subscription lock")
                .take()
                .expect("subscription opened once")
        }
    }

    let (sender, receiver) = tokio::sync::mpsc::channel(2);
    let (exit_sender, exit_receiver) = tokio::sync::oneshot::channel();
    exit_sender
        .send(ActivitySubscriptionExit::Closed)
        .expect("exit receiver remains open");
    let service = TestActivitySubscriptionService {
        subscribed_user: Arc::clone(&subscribed_user),
        subscription: Mutex::new(Some(ActivitySubscription::from_parts(
            receiver,
            exit_receiver,
        ))),
    };

    let schema: SoupSchema<
        CountingSoupService,
        NoOpSoupRealtimeSubscriptionService,
        NoopWebSocketNotificationSubscriptionService,
        TestActivitySubscriptionService,
        NoOpEmailService,
        NoOpEntityAccessService,
        SchemaOnlyAuthorizationService,
        SchemaOnlyState,
        NoOpEntityPropertyWriter,
        UnavailableEntityMutationService,
        NoOpFavoriteMutationService,
        NoOpChannelActivityMutationService,
        NoOpNotificationMutationService,
        NoOpSoupNotificationEdgeReader,
        NoOpEntityPropertyReader,
        NoOpSoupEmailContentEdgeReader,
        NoOpEntityFavoriteEdgeReader,
        NoOpEntityPermissionEdgeReader,
        NoOpActivityReader,
    > = build_schema_with_services(
        CountingSoupService::default(),
        NoOpSoupRealtimeSubscriptionService,
        NoopWebSocketNotificationSubscriptionService,
        service,
    );

    let request = async_graphql::Request::new(
        "subscription { activityUpdates { __typename ... on GraphqlActivityEvent { id entityType entityId } } }",
    )
    .data(user_id.clone());
    let responses = schema.execute_stream(request);
    pin_mut!(responses);

    let record = {
        let activity = activity::Activity::common(
            Uuid::from_u128(7),
            0,
            activity::Actor::new_from_user(user_id.clone()),
            None,
            ModelEntityType::Document,
            "doc-1",
            activity::CommonAction::Edited,
            chrono::Utc::now(),
        );
        activity::ActivityRecord {
            agent_correlation_id: activity.agent_correlation_id,
            id: activity.id,
            actor: activity.actor.clone(),
            subject_id: activity.subject_id.clone(),
            entity_type: activity.entity_type,
            entity_id: activity.entity_id.clone(),
            action: activity::RecordedAction::Known(activity::Action::Edited),
            occurred_at: activity.occurred_at,
        }
    };
    let record_id = record.id;
    sender
        .send(ActivitySubscriptionUpdate::Updated(Arc::new(record)))
        .await
        .expect("subscription remains open");

    let response = responses.next().await.expect("subscription response");
    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let data = response.data.into_json().expect("response data is JSON");
    assert_eq!(data["activityUpdates"]["id"], record_id.to_string());
    assert_eq!(data["activityUpdates"]["entityType"], "DOCUMENT");
    assert_eq!(data["activityUpdates"]["entityId"], "doc-1");
    assert_eq!(
        subscribed_user
            .lock()
            .expect("subscribed user lock")
            .as_ref(),
        Some(&user_id)
    );
}

#[tokio::test]
async fn user_id_resolves_without_touching_services() {
    let harness = harness();

    let response = harness.execute("{ user { id } }").await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    assert_eq!(
        response.data.to_string(),
        r#"{user: {id: "macro|[email protected]"}}"#
    );
    assert_eq!(harness.authorization_calls.load(Ordering::SeqCst), 1);
    assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.user_label_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.user_link_calls.load(Ordering::SeqCst), 0);
    assert!(
        harness
            .user_catalog_identities
            .lock()
            .expect("user catalog identities lock")
            .is_empty()
    );
    assert_eq!(harness.team_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.frecency_soup_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.grouped_soup_calls.load(Ordering::SeqCst), 0);
}

#[tokio::test]
async fn favorites_are_nested_under_the_authenticated_user() {
    let harness = harness();

    let response = harness
        .execute("{ user { favorites { entityType entityId sortOrder } } }")
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    assert_eq!(response.data.to_string(), "{user: {favorites: []}}");
    assert_eq!(harness.authorization_calls.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn folders_are_nested_under_the_authenticated_user() {
    let harness = harness();

    let response = harness
        .execute("{ user { folders { id name parentId ownerId } } }")
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    assert_eq!(response.data.to_string(), "{user: {folders: []}}");
    assert_eq!(harness.authorization_calls.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn email_catalog_fields_use_the_authenticated_user_and_remain_direct_user_fields() {
    let harness = harness();

    let response = harness
        .execute(
            r#"{
                user {
                    emailLabels { __typename id linkId providerLabelId name }
                    emailLinks {
                        id macroId emailAddress photoUrl provider isSyncActive syncStatus
                        needsReauth settings { signatureOnRepliesForwards signature }
                        isPrimary createdAt updatedAt
                    }
                }
            }"#,
        )
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let data = response.data.into_json().unwrap();
    let label = &data["user"]["emailLabels"][0];
    assert_eq!(label["__typename"], "GraphqlSoupEmailLabel");
    assert_eq!(label["linkId"], Uuid::from_u128(502).to_string());
    let link = &data["user"]["emailLinks"][0];
    assert_eq!(link["emailAddress"], "[email protected]");
    assert_eq!(link["syncStatus"], "UP_TO_DATE");
    assert_eq!(link["settings"]["signature"], "<p>Regards</p>");
    assert_eq!(harness.user_label_calls.load(Ordering::SeqCst), 1);
    assert_eq!(harness.user_link_calls.load(Ordering::SeqCst), 1);
    assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 0);
    assert_eq!(
        *harness
            .user_catalog_identities
            .lock()
            .expect("user catalog identities lock"),
        vec![
            MacroUserIdStr::parse_from_str(VALID_USER_ID).unwrap(),
            MacroUserIdStr::parse_from_str(VALID_USER_ID).unwrap(),
        ]
    );
}

#[tokio::test]
async fn internal_authorization_uses_the_acting_user_claim() {
    let harness = harness();

    let response = harness
        .execute_with_parts("{ user { id } }", internal_parts(INTERNAL_USER_ID))
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    assert_eq!(
        response.data.to_string(),
        r#"{user: {id: "macro|[email protected]"}}"#
    );
    assert_eq!(harness.authorization_calls.load(Ordering::SeqCst), 1);
    assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.team_calls.load(Ordering::SeqCst), 0);
}

#[tokio::test]
async fn soup_passes_team_receipt_to_raw_path() {
    let harness = harness();

    let _response = harness
        .execute("{ user { soup(input: {initial: {}}) { nextCursor } } }")
        .await;

    assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 1);
    assert_eq!(harness.team_calls.load(Ordering::SeqCst), 1);
    assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 1);
    assert_eq!(harness.raw_soup_team_receipts.load(Ordering::SeqCst), 1);
    assert_eq!(harness.frecency_soup_calls.load(Ordering::SeqCst), 0);
    assert_eq!(
        harness.frecency_soup_team_receipts.load(Ordering::SeqCst),
        0
    );
}

#[tokio::test]
async fn flat_soup_emits_server_fact_supplements_for_documents_and_mail() {
    let harness = harness();
    let document_id = Uuid::from_u128(88);
    let email_thread_id = Uuid::from_u128(89);
    let project_id = Uuid::from_u128(90);
    let chat_id = Uuid::from_u128(91);
    harness.soup_service.set_raw_response(vec![
        soup_document(document_id),
        soup_email_thread(email_thread_id),
        soup_project(project_id),
        soup_chat(chat_id),
    ]);

    let response = harness
        .execute(
            "{ user { soup(input: {initial: {}}) { items { __typename id cacheProjection } } } }",
        )
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let data = response.data.into_json().unwrap();
    let items = data["user"]["soup"]["items"].as_array().unwrap();
    let document = items
        .iter()
        .find(|item| item["__typename"] == "GraphqlSoupDocument")
        .unwrap();
    assert_eq!(document["id"], document_id.to_string());
    assert!(document["cacheProjection"].is_string());
    let email = items
        .iter()
        .find(|item| item["__typename"] == "GraphqlSoupEmailThread")
        .unwrap();
    assert_eq!(email["id"], email_thread_id.to_string());
    let mail_supplement = soup_filter_projection::decode_cache_projection_supplement(
        email["cacheProjection"].as_str().unwrap(),
    )
    .unwrap();
    assert_eq!(
        mail_supplement.record_key(),
        &predicate_index::RecordKey::new(format!("GraphqlSoupEmailThread:{email_thread_id}"))
            .unwrap()
    );
    assert!(mail_supplement.mail_facts().is_some());
    assert_eq!(
        mail_supplement.target_profile().token().as_str(),
        "soup-mail-v2"
    );
    let current = harness.schema.execute(
        harness.request("{ user { soup(input: {initial: {}}) { items { __typename cacheProjection } } } }", authenticated_parts())
            .data(crate::MailCacheProfile::from_advertised(Some("soup-mail-v5")))
    ).await;
    assert!(current.errors.is_empty(), "{:?}", current.errors);
    let current = current.data.into_json().unwrap();
    let current = current["user"]["soup"]["items"]
        .as_array()
        .unwrap()
        .iter()
        .find(|item| item["__typename"] == "GraphqlSoupEmailThread")
        .unwrap();
    let current = soup_filter_projection::decode_cache_projection_supplement(
        current["cacheProjection"].as_str().unwrap(),
    )
    .unwrap();
    assert_eq!(current.target_profile().token().as_str(), "soup-mail-v5");
    assert_eq!(current.mail_facts(), mail_supplement.mail_facts());
    for (typename, id) in [
        ("GraphqlSoupProject", project_id),
        ("GraphqlSoupChat", chat_id),
    ] {
        let item = items
            .iter()
            .find(|item| item["__typename"] == typename)
            .unwrap();
        assert_eq!(item["id"], id.to_string());
        assert!(item["cacheProjection"].is_null());
    }
}

#[tokio::test]
async fn soup_input_rejects_initial_and_continuation_together() {
    let harness = harness();

    let response = harness
        .execute(
            r#"{ user { soup(input: {initial: {}, continuation: {cursor: "invalid"}}) { nextCursor } } }"#,
        )
        .await;

    assert!(!response.errors.is_empty());
    assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.frecency_soup_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.team_calls.load(Ordering::SeqCst), 0);
}

#[tokio::test]
async fn soup_returns_the_saved_chat_model() {
    let harness = harness();
    harness
        .soup_service
        .set_raw_response(vec![soup_chat(Uuid::from_u128(91))]);
    let response = harness.execute(
        "{ user { soup(input: {initial: {}}) { items { ... on GraphqlSoupChat { model } } } } }"
    ).await;
    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let data = response.data.into_json().unwrap();
    assert_eq!(data["user"]["soup"]["items"][0]["model"], "openai/gpt-5.6");
}

#[tokio::test]
async fn soup_passes_team_receipt_to_frecency_enriched_path() {
    let harness = harness();

    let _response = harness
        .execute("{ user { soup(input: {initial: {}}) { items { frecencyScore } } } }")
        .await;

    assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.raw_soup_team_receipts.load(Ordering::SeqCst), 0);
    assert_eq!(harness.frecency_soup_calls.load(Ordering::SeqCst), 1);
    assert_eq!(
        harness.frecency_soup_team_receipts.load(Ordering::SeqCst),
        1
    );
    assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 1);
    assert_eq!(harness.team_calls.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn group_soup_nests_items_in_bins_and_reuses_cached_authorization() {
    let harness = harness();

    let response = harness
        .execute(
            "{ user { id groupSoup(input: {initial: {groupBy: {field: ENTITY_TYPE}}}) { bins { key totalCount displayOrder nextCursor items { id entityType cacheProjection } } } } }",
        )
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let data = response.data.into_json().unwrap();
    let bin = &data["user"]["groupSoup"]["bins"][0];
    assert_eq!(bin["key"], "document");
    assert_eq!(bin["totalCount"], 3);
    assert_eq!(harness.authorization_calls.load(Ordering::SeqCst), 1);
    let cursor = bin["nextCursor"].as_str().unwrap();
    assert_eq!(bin["items"].as_array().unwrap().len(), 2);
    assert_eq!(bin["displayOrder"], 0);
    let items = bin["items"].as_array().unwrap();
    let supplement = soup_filter_projection::decode_cache_projection_supplement(
        items[0]["cacheProjection"].as_str().unwrap(),
    )
    .unwrap();
    assert_eq!(
        supplement.record_key(),
        &predicate_index::RecordKey::new(format!("GraphqlSoupDocument:{}", Uuid::from_u128(1)))
            .unwrap()
    );
    assert!(items[1]["cacheProjection"].is_null());

    let continuation = format!(
        "{{ user {{ groupSoup(input: {{continuation: {{groupBy: {{field: ENTITY_TYPE}}, groupKey: \"document\", cursor: \"{cursor}\"}}}}) {{ bins {{ key }} }} }} }}"
    );
    let response = harness.execute(&continuation).await;
    assert!(response.errors.is_empty(), "{:?}", response.errors);

    assert_eq!(harness.authorization_calls.load(Ordering::SeqCst), 2);
    assert_eq!(harness.grouped_soup_calls.load(Ordering::SeqCst), 2);
    assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.team_calls.load(Ordering::SeqCst), 0);
}

#[tokio::test]
async fn crm_scoped_soup_resolves_team_membership_lazily() {
    let harness = harness();

    // The membership/role authorization itself lives in the soup domain
    // and CRM service (covered by their tests); this asserts CRM-scoped
    // input still receives the always-resolved team receipt.
    let response = harness
        .execute(
            r#"{ user { soup(input: {initial: {filters: {emailFilter: {crmScope: {domains: ["example.com"]}}}}}) { nextCursor } } }"#,
        )
        .await;

    assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 1);
    assert_eq!(harness.team_calls.load(Ordering::SeqCst), 1);
    assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 1);
    assert_eq!(harness.frecency_soup_calls.load(Ordering::SeqCst), 0);
    assert!(!response.errors.is_empty());
}

#[tokio::test]
async fn unauthenticated_request_fails_at_the_resolver() {
    let harness = harness();
    let parts = HttpRequest::new(()).into_parts().0;

    let response = harness.execute_with_parts("{ user { id } }", parts).await;

    assert_eq!(response.errors.len(), 1);
    assert_eq!(response.errors[0].message, "authentication required");
    assert_eq!(harness.authorization_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.team_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.frecency_soup_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.grouped_soup_calls.load(Ordering::SeqCst), 0);
}

#[tokio::test]
async fn invalid_credentials_return_the_safe_authorization_error() {
    let harness = harness();

    let response = harness
        .execute_with_parts("{ user { id } }", bearer_parts("invalid"))
        .await;

    assert_eq!(response.errors.len(), 1);
    assert_eq!(response.errors[0].message, "unauthorized");
    assert_eq!(harness.authorization_calls.load(Ordering::SeqCst), 1);
    assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.team_calls.load(Ordering::SeqCst), 0);
}

#[tokio::test]
async fn expired_credentials_return_the_safe_authorization_error() {
    let harness = harness();

    let response = harness
        .execute_with_parts("{ user { id } }", bearer_parts("expired"))
        .await;

    assert_eq!(response.errors.len(), 1);
    assert_eq!(response.errors[0].message, "jwt expired");
    assert_eq!(harness.authorization_calls.load(Ordering::SeqCst), 1);
    assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.team_calls.load(Ordering::SeqCst), 0);
}

fn soup_email_thread(thread_id: Uuid) -> SoupItem<()> {
    soup_email_thread_with_read_status(thread_id, false)
}

fn soup_email_thread_with_read_status(thread_id: Uuid, is_read: bool) -> SoupItem<()> {
    SoupItem::EmailThread(SoupEnrichedEmailThreadPreview {
        thread: SoupEmailThreadPreview {
            id: thread_id,
            provider_id: Some("provider-thread".to_owned()),
            owner_id: MacroUserIdStr::parse_from_str(VALID_USER_ID).unwrap(),
            inbox_visible: true,
            is_read,
            is_draft: false,
            is_important: true,
            is_signal: true,
            name: Some("Direct thread".to_owned()),
            snippet: Some("Direct thread snippet".to_owned()),
            sender_email: Some("[email protected]".to_owned()),
            sender_name: Some("Sender".to_owned()),
            sender_photo_url: None,
            sort_ts: Default::default(),
            created_at: Default::default(),
            updated_at: Default::default(),
            viewed_at: None,
            project_id: None,
        },
        attachments: Vec::new(),
        participants: vec![SoupContact {
            id: Uuid::from_u128(300),
            link_id: Uuid::from_u128(200),
            name: Some("Sender".to_owned()),
            email_address: Some("[email protected]".to_owned()),
            sfs_photo_url: None,
        }],
        labels: Vec::new(),
        extra: (),
    })
}

#[tokio::test]
async fn email_thread_returns_the_canonical_soup_type_and_forwards_message_pagination() {
    let harness = harness();
    let thread_id = Uuid::from_u128(42);
    harness
        .thread_reader
        .set_threads(vec![soup_email_thread(thread_id)]);

    let response = harness
        .execute(&format!(
            r#"{{ user {{ emailThread(input: {{threadId: "{thread_id}"}}) {{ __typename id linkId latestInboundMessageTs inboxVisible isRead messages(offset: 7, limit: 20) {{ id threadId bodyParsed }} }} }} }}"#
        ))
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let data = response.data.into_json().unwrap();
    let thread = &data["user"]["emailThread"];
    assert_eq!(thread["__typename"], "GraphqlSoupEmailThread");
    assert_eq!(thread["id"], thread_id.to_string());
    assert_eq!(thread["linkId"], Uuid::from_u128(942).to_string());
    assert!(thread["latestInboundMessageTs"].is_null());
    assert_eq!(thread["inboxVisible"], true);
    assert_eq!(thread["isRead"], false);
    assert_eq!(thread["messages"][0]["threadId"], thread_id.to_string());
    assert_eq!(thread["messages"][0]["bodyParsed"], "Direct thread body");
    assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.thread_reads.load(Ordering::SeqCst), 1);
    assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 0);
    assert_eq!(
        *harness
            .email_content_reader
            .metadata_calls
            .lock()
            .expect("email metadata calls lock"),
        vec![vec![thread_id]]
    );
    assert_eq!(
        *harness
            .email_content_reader
            .calls
            .lock()
            .expect("email content calls lock"),
        vec![vec![graphql_email::EmailContentKey::page(thread_id, 7, 20)]]
    );
}

#[tokio::test]
async fn email_thread_metadata_is_lazy_and_batches_across_threads() {
    let harness = harness();
    let first_id = Uuid::from_u128(51);
    let second_id = Uuid::from_u128(52);
    harness.soup_service.set_raw_response(vec![
        soup_email_thread(first_id),
        soup_email_thread(second_id),
    ]);

    let without_metadata = harness
        .execute(
            r#"{ user { soup(input: {initial: {}}) { items { ... on GraphqlSoupEmailThread { id inboxVisible } } } } }"#,
        )
        .await;
    assert!(
        without_metadata.errors.is_empty(),
        "{:?}",
        without_metadata.errors
    );
    assert!(
        harness
            .email_content_reader
            .metadata_calls
            .lock()
            .expect("email metadata calls lock")
            .is_empty()
    );

    let with_metadata = harness
        .execute(
            r#"{ user { soup(input: {initial: {}}) { items { ... on GraphqlSoupEmailThread { id linkId latestInboundMessageTs latestOutboundMessageTs latestNonSpamMessageTs reminderReturnedAt } } } } }"#,
        )
        .await;
    assert!(
        with_metadata.errors.is_empty(),
        "{:?}",
        with_metadata.errors
    );
    let mut calls = harness
        .email_content_reader
        .metadata_calls
        .lock()
        .expect("email metadata calls lock")
        .clone();
    assert_eq!(calls.len(), 1);
    calls[0].sort();
    assert_eq!(calls[0], vec![first_id, second_id]);

    let items = &with_metadata.data.into_json().unwrap()["user"]["soup"]["items"];
    assert_eq!(items[0]["linkId"], Uuid::from_u128(951).to_string());
    assert_eq!(items[1]["linkId"], Uuid::from_u128(952).to_string());
    assert!(items[0]["latestInboundMessageTs"].as_str().is_some());
    assert!(items[1]["latestInboundMessageTs"].is_null());
    assert!(items[0]["reminderReturnedAt"].is_null());
    assert_eq!(items[1]["reminderReturnedAt"], "1970-01-01T00:00:00+00:00");
    assert!(items[0]["latestOutboundMessageTs"].as_str().is_some());
    assert!(items[1]["latestOutboundMessageTs"].is_null());
    assert_eq!(
        items[0]["latestNonSpamMessageTs"],
        "1970-01-01T00:00:00+00:00"
    );
    assert_eq!(
        items[1]["latestNonSpamMessageTs"],
        "1970-01-01T00:00:00+00:00"
    );
}

#[tokio::test]
async fn mail_previews_and_capsules_share_a_lazy_batched_reader() {
    let harness = harness();
    let first_id = Uuid::from_u128(51);
    let second_id = Uuid::from_u128(52);
    harness.soup_service.set_raw_response(vec![
        soup_email_thread(first_id),
        soup_email_thread(second_id),
    ]);
    let metadata = harness
        .execute("{ user { soup(input: {initial: {}}) { items { ... on GraphqlSoupEmailThread { id linkId latestInboundMessageTs } } } } }")
        .await;
    assert!(metadata.errors.is_empty(), "{:?}", metadata.errors);
    assert!(
        harness
            .email_content_reader
            .mail_projection_calls
            .lock()
            .unwrap()
            .is_empty()
    );

    let response = harness
        .execute("{ user { soup(input: {initial: {}}) { items { id cacheProjection ... on GraphqlSoupEmailThread { mailAllPreview { id } mailDraftPreview { id } mailSentPreview { id } } } } } }")
        .await;
    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let mut calls = harness
        .email_content_reader
        .mail_projection_calls
        .lock()
        .unwrap()
        .clone();
    assert_eq!(calls.len(), 1);
    calls[0].sort();
    assert_eq!(calls[0], vec![first_id, second_id]);
}

fn activity_record(
    id: u128,
    entity_type: ModelEntityType,
    entity_id: &str,
    action: activity::RecordedAction,
    occurred_at_secs: i64,
) -> activity::ActivityRecord {
    activity::ActivityRecord {
        agent_correlation_id: None,
        id: Uuid::from_u128(id),
        actor: activity::Actor::new_from_user(
            MacroUserIdStr::parse_from_str(VALID_USER_ID).unwrap(),
        ),
        subject_id: VALID_USER_ID.to_owned(),
        entity_type,
        entity_id: entity_id.to_owned(),
        action,
        occurred_at: chrono::DateTime::from_timestamp(occurred_at_secs, 0)
            .expect("valid timestamp"),
    }
}

#[tokio::test]
async fn entity_activity_is_lazy_and_batches_across_entities() {
    let harness = harness();
    let first_id = Uuid::from_u128(61);
    let second_id = Uuid::from_u128(62);
    harness.soup_service.set_raw_response(vec![
        soup_email_thread(first_id),
        soup_email_thread(second_id),
    ]);
    harness.activity_reader.set_records(vec![activity_record(
        1,
        ModelEntityType::EmailThread,
        &first_id.to_string(),
        activity::RecordedAction::Known(activity::Action::Edited),
        300,
    )]);

    let without_activity = harness
        .execute(
            r#"{ user { soup(input: {initial: {}}) { items { ... on GraphqlSoupEmailThread { id inboxVisible } } } } }"#,
        )
        .await;
    assert!(
        without_activity.errors.is_empty(),
        "{:?}",
        without_activity.errors
    );
    assert!(
        harness
            .activity_reader
            .edge_calls
            .lock()
            .expect("activity edge calls lock")
            .is_empty(),
        "unselected activity edge must not touch the reader"
    );

    let with_activity = harness
        .execute(
            r#"{ user { soup(input: {initial: {}}) { items { ... on GraphqlSoupEmailThread { id activity { occurredAt action { __typename } } } } } } }"#,
        )
        .await;
    assert!(
        with_activity.errors.is_empty(),
        "{:?}",
        with_activity.errors
    );

    let calls = harness
        .activity_reader
        .edge_calls
        .lock()
        .expect("activity edge calls lock")
        .clone();
    assert_eq!(calls.len(), 1, "both entities load in one batched call");
    let mut requested: Vec<(String, u32)> = calls[0]
        .iter()
        .map(|key| (key.entity.entity_id.to_string(), key.limit))
        .collect();
    requested.sort();
    assert_eq!(
        requested,
        vec![(first_id.to_string(), 10), (second_id.to_string(), 10)]
    );

    let items = &with_activity.data.into_json().unwrap()["user"]["soup"]["items"];
    assert_eq!(
        items[0]["activity"][0]["action"]["__typename"],
        "GraphqlActivityEdited"
    );
    assert!(items[0]["activity"][0]["occurredAt"].as_str().is_some());
    assert_eq!(
        items[1]["activity"].as_array().map(Vec::len),
        Some(0),
        "entity with no activity resolves an empty timeline"
    );
}

#[tokio::test]
async fn activity_feed_pages_by_cursor_and_carries_unknown_actions() {
    let harness = harness();
    let doc = Uuid::from_u128(71).to_string();
    harness.activity_reader.set_records(vec![
        activity_record(
            3,
            ModelEntityType::Document,
            &doc,
            activity::RecordedAction::Unknown {
                tag: "transmogrified".to_owned(),
                payload: None,
            },
            300,
        ),
        activity_record(
            2,
            ModelEntityType::Document,
            &doc,
            activity::RecordedAction::Known(activity::Action::Edited),
            200,
        ),
        activity_record(
            1,
            ModelEntityType::Document,
            &doc,
            activity::RecordedAction::Known(activity::Action::Created),
            100,
        ),
    ]);

    let first_page = harness
        .execute(
            r#"{ user { activity(input: {limit: 2}) { items { entityId entityType action { __typename ... on GraphqlActivityUnknownAction { tag } } } nextCursor } } }"#,
        )
        .await;
    assert!(first_page.errors.is_empty(), "{:?}", first_page.errors);
    let data = first_page.data.into_json().unwrap();
    let page = &data["user"]["activity"];
    assert_eq!(page["items"].as_array().map(Vec::len), Some(2));
    assert_eq!(
        page["items"][0]["action"]["__typename"],
        "GraphqlActivityUnknownAction"
    );
    assert_eq!(page["items"][0]["action"]["tag"], "transmogrified");
    assert_eq!(page["items"][0]["entityId"], doc);
    assert_eq!(page["items"][0]["entityType"], "DOCUMENT");
    assert_eq!(
        page["items"][1]["action"]["__typename"],
        "GraphqlActivityEdited"
    );
    let cursor = page["nextCursor"].as_str().expect("more rows exist");

    let second_page = harness
        .execute(&format!(
            r#"{{ user {{ activity(input: {{limit: 2, cursor: "{cursor}"}}) {{ items {{ action {{ __typename }} }} nextCursor }} }} }}"#
        ))
        .await;
    assert!(second_page.errors.is_empty(), "{:?}", second_page.errors);
    let data = second_page.data.into_json().unwrap();
    let page = &data["user"]["activity"];
    assert_eq!(page["items"].as_array().map(Vec::len), Some(1));
    assert_eq!(
        page["items"][0]["action"]["__typename"],
        "GraphqlActivityCreated"
    );
    assert!(page["nextCursor"].is_null());

    let feed_calls = harness
        .activity_reader
        .feed_calls
        .lock()
        .expect("activity feed calls lock")
        .clone();
    // The resolver binds the viewer's principal string as the subject and
    // passes the client limit through; the has-more probe lives in storage.
    assert_eq!(feed_calls.len(), 2);
    assert_eq!(feed_calls[0].0, VALID_USER_ID);
    assert_eq!(feed_calls[0].2, 2);
    let (cursor_at, cursor_id) = feed_calls[1].1.expect("second page carries the cursor");
    assert_eq!(cursor_at, chrono::DateTime::from_timestamp(200, 0).unwrap());
    assert_eq!(cursor_id, Uuid::from_u128(2));
}

#[tokio::test]
async fn activity_overview_uses_the_authenticated_subject_and_requested_zone() {
    let harness = harness();

    let response = harness
        .execute(
            r#"{ user { activityOverview(input: {timeZone: "America/Havana"}) { from to timeZone total days { date count } topEntities { entityType entityId count } } } }"#,
        )
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let calls = harness
        .activity_reader
        .overview_calls
        .lock()
        .expect("activity overview calls lock")
        .clone();
    assert_eq!(calls.len(), 1);
    assert_eq!(calls[0].0, VALID_USER_ID);
    assert_eq!(calls[0].1.zone.name(), "America/Havana");

    let data = response.data.into_json().unwrap();
    let overview = &data["user"]["activityOverview"];
    assert_eq!(overview["from"], calls[0].1.start.to_string());
    assert_eq!(overview["to"], calls[0].1.end.to_string());
    assert_eq!(overview["timeZone"], "America/Havana");
    assert_eq!(overview["total"], 2);
    assert_eq!(overview["days"][0]["date"], calls[0].1.start.to_string());
    assert_eq!(overview["days"][0]["count"], 2);
    assert_eq!(overview["topEntities"][0]["entityType"], "DOCUMENT");
    assert_eq!(overview["topEntities"][0]["entityId"], "overview-doc");
    assert_eq!(overview["topEntities"][0]["count"], 2);
}

#[tokio::test]
async fn email_message_delivery_status_alone_requests_the_full_edge_payload() {
    let harness = harness();
    let thread_id = Uuid::from_u128(43);
    harness
        .thread_reader
        .set_threads(vec![soup_email_thread(thread_id)]);
    let response = harness
        .execute(&format!(
            r#"{{ user {{ emailThread(input: {{threadId: "{thread_id}"}}) {{ messages(offset: 0, limit: 1) {{ scheduledSendStatus }} }} }} }}"#
        ))
        .await;
    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let data = response.data.into_json().unwrap();
    assert_eq!(
        data["user"]["emailThread"]["messages"][0]["scheduledSendStatus"],
        "FAILED"
    );
}

#[tokio::test]
async fn email_message_full_fields_request_the_full_edge_payload() {
    let harness = harness();
    let thread_id = Uuid::from_u128(43);
    harness
        .thread_reader
        .set_threads(vec![soup_email_thread(thread_id)]);

    let response = harness
        .execute(&format!(
            r#"{{ user {{ emailThread(input: {{threadId: "{thread_id}"}}) {{ messages(offset: 2, limit: 4) {{ providerId replyingToId scheduledSendTime scheduledSendStatus bodyParsed attachments {{ id providerId sfsId referenceUrl }} attachmentsDraft {{ id draftId fileName }} attachmentsForwarded {{ attachmentId draftId providerAttachmentId }} }} }} }} }}"#
        ))
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let data = response.data.into_json().unwrap();
    let message = &data["user"]["emailThread"]["messages"][0];
    assert_eq!(message["providerId"], "provider-message");
    assert_eq!(message["replyingToId"], Uuid::from_u128(101).to_string());
    assert!(message["scheduledSendTime"].as_str().is_some());
    assert_eq!(message["scheduledSendStatus"], "FAILED");
    assert_eq!(message["bodyParsed"], "Direct thread body");
    assert_eq!(
        message["attachments"][0]["sfsId"],
        Uuid::from_u128(104).to_string()
    );
    assert_eq!(
        message["attachments"][0]["referenceUrl"],
        "https://outlook.office.com/mail/message"
    );
    assert_eq!(message["attachmentsDraft"][0]["fileName"], "draft.txt");
    assert_eq!(
        message["attachmentsForwarded"][0]["providerAttachmentId"],
        "forwarded-attachment"
    );
    assert_eq!(
        *harness
            .email_content_reader
            .calls
            .lock()
            .expect("email content calls lock"),
        vec![vec![graphql_email::EmailContentKey::page_full(
            thread_id, 2, 4
        )]]
    );
}

#[tokio::test]
async fn latest_email_message_full_fields_request_the_full_edge_payload() {
    let harness = harness();
    let thread_id = Uuid::from_u128(44);
    harness
        .thread_reader
        .set_threads(vec![soup_email_thread(thread_id)]);

    let response = harness
        .execute(&format!(
            r#"{{ user {{ emailThread(input: {{threadId: "{thread_id}"}}) {{ latestContentMessage {{ providerId attachments {{ sfsId }} }} }} }} }}"#
        ))
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let message =
        &response.data.into_json().unwrap()["user"]["emailThread"]["latestContentMessage"];
    assert_eq!(message["providerId"], "provider-message");
    assert_eq!(
        message["attachments"][0]["sfsId"],
        Uuid::from_u128(104).to_string()
    );
    assert_eq!(
        *harness
            .email_content_reader
            .calls
            .lock()
            .expect("email content calls lock"),
        vec![vec![graphql_email::EmailContentKey::latest_full(thread_id)]]
    );
}

#[tokio::test]
async fn email_mutations_return_primary_state_even_when_the_soup_replica_is_stale() {
    let harness = harness();
    let thread_id = Uuid::from_u128(44);
    let label_id = Uuid::from_u128(45);
    harness
        .soup_service
        .set_raw_response(vec![soup_email_thread_with_read_status(thread_id, false)]);

    let seen_response = harness
        .execute_authenticated_mutation(&format!(
            r#"mutation {{ markEmailThreadSeen(input: {{threadId: "{thread_id}"}}) {{ __typename id isRead }} }}"#
        ))
        .await;
    assert!(
        seen_response.errors.is_empty(),
        "{:?}",
        seen_response.errors
    );
    let seen_thread = &seen_response.data.into_json().unwrap()["markEmailThreadSeen"];
    assert_eq!(seen_thread["__typename"], "GraphqlSoupEmailThread");
    assert_eq!(seen_thread["id"], thread_id.to_string());
    assert_eq!(seen_thread["isRead"], true);

    let label_response = harness
        .execute_authenticated_mutation(&format!(
            r#"mutation {{ updateEmailThreadLabel(input: {{threadId: "{thread_id}", labelId: "{label_id}", value: true}}) {{ __typename id isRead }} }}"#
        ))
        .await;
    assert!(
        label_response.errors.is_empty(),
        "{:?}",
        label_response.errors
    );
    let label_thread = &label_response.data.into_json().unwrap()["updateEmailThreadLabel"];
    assert_eq!(label_thread["__typename"], "GraphqlSoupEmailThread");
    assert_eq!(label_thread["id"], thread_id.to_string());
    assert_eq!(label_thread["isRead"], true);

    harness
        .soup_service
        .set_raw_response(vec![soup_email_thread_with_read_status(thread_id, true)]);
    let unread_response = harness
        .execute_authenticated_mutation(&format!(
            r#"mutation {{ markEmailThreadUnread(input: {{threadId: "{thread_id}"}}) {{ __typename id isRead }} }}"#
        ))
        .await;
    assert!(
        unread_response.errors.is_empty(),
        "{:?}",
        unread_response.errors
    );
    let unread_thread = &unread_response.data.into_json().unwrap()["markEmailThreadUnread"];
    assert_eq!(unread_thread["__typename"], "GraphqlSoupEmailThread");
    assert_eq!(unread_thread["id"], thread_id.to_string());
    assert_eq!(unread_thread["isRead"], false);
    assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);

    let expected_user = MacroUserIdStr::parse_from_str(VALID_USER_ID).unwrap();
    assert_eq!(
        *harness.email_service.unread_mutation_calls.lock().unwrap(),
        vec![(expected_user.clone(), thread_id)]
    );
    assert_eq!(
        *harness
            .email_service
            .seen_mutation_calls
            .lock()
            .expect("seen mutation calls lock"),
        vec![(expected_user.clone(), thread_id)]
    );
    assert_eq!(
        *harness
            .email_service
            .label_mutation_calls
            .lock()
            .expect("label mutation calls lock"),
        vec![(expected_user, thread_id, label_id, true)]
    );
}

#[tokio::test]
async fn email_thread_returns_null_when_the_viewer_cannot_read_the_thread() {
    let harness = harness();
    let thread_id = Uuid::from_u128(43);

    let response = harness
        .execute(&format!(
            r#"{{ user {{ emailThread(input: {{threadId: "{thread_id}"}}) {{ id messages {{ id }} }} }} }}"#
        ))
        .await;

    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let data = response.data.into_json().unwrap();
    assert!(data["user"]["emailThread"].is_null());
    assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.thread_reads.load(Ordering::SeqCst), 1);
    assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 0);
    assert!(
        harness
            .email_content_reader
            .calls
            .lock()
            .expect("email content calls lock")
            .is_empty()
    );
}

/// The lookup mints a view receipt through entity access, the rule the REST
/// thread route applies, so a thread shared through a team's CRM or a project
/// reaches the email read while an inaccessible one never does.
#[tokio::test]
async fn email_thread_lookups_settle_access_before_reading_the_email_service() {
    let harness = harness();
    let viewable_id = Uuid::from_u128(45);
    let hidden_id = Uuid::from_u128(46);
    harness
        .state
        .entity_access
        .viewable_email_threads
        .lock()
        .unwrap()
        .insert(viewable_id);
    let loader = graphql_soup::soup_item_loader(
        harness.soup_service.clone(),
        Arc::new(harness.email_service.clone()),
        harness.state.entity_access.as_ref().clone(),
    );

    for thread_id in [viewable_id, hidden_id] {
        let request = harness
            .request(
                &format!(
                    r#"{{ user {{ emailThread(input: {{threadId: "{thread_id}"}}) {{ id }} }} }}"#
                ),
                authenticated_parts(),
            )
            .data(loader.clone());
        let response = harness.schema.execute(request).await;
        assert!(response.errors.is_empty(), "{:?}", response.errors);
        assert!(response.data.into_json().unwrap()["user"]["emailThread"].is_null());
    }

    assert_eq!(
        *harness
            .state
            .entity_access
            .email_thread_checks
            .lock()
            .unwrap(),
        vec![viewable_id, hidden_id]
    );
    assert_eq!(
        *harness
            .email_service
            .thread_preview_receipts
            .lock()
            .unwrap(),
        vec![vec![viewable_id]]
    );
    assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);
}

#[tokio::test]
async fn email_thread_rejects_an_invalid_thread_id_before_loading_soup() {
    let harness = harness();

    let response = harness
        .execute(r#"{ user { emailThread(input: {threadId: "not-a-uuid"}) { id } } }"#)
        .await;

    assert_eq!(response.errors.len(), 1);
    assert!(
        response.errors[0]
            .message
            .starts_with("invalid threadId UUID")
    );
    assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);
    assert_eq!(harness.thread_reads.load(Ordering::SeqCst), 0);
    assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 0);
}

#[tokio::test]
async fn initiatives_are_returned_by_soup_with_shared_edges() {
    let harness = harness();
    let id = Uuid::from_u128(42);
    harness
        .soup_service
        .set_raw_response(vec![soup_initiative(id)]);
    let response = harness
        .execute(
            r#"{
        user { soup(input: {initial: {filters: {initiativeFilter: {literal: {include: true}}}}}) {
            items {
                __typename id entityType displayName
                metadata { ownerId ownerType createdAt updatedAt }
                properties { id }
            }
        } }
    }"#,
        )
        .await;
    assert!(response.errors.is_empty(), "{:?}", response.errors);
    let data = response.data.into_json().unwrap();
    let item = &data["user"]["soup"]["items"][0];
    assert_eq!(item["__typename"], "GraphqlSoupInitiative");
    assert_eq!(item["id"], id.to_string());
    assert_eq!(item["entityType"], "INITIATIVE");
    assert_eq!(item["displayName"], "Launch project");
    assert_eq!(item["metadata"]["ownerId"], VALID_USER_ID);
    assert!(item["properties"].is_array());
    assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 1);
}