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.

241 KB
Raw Blame
#[cfg(feature = "outlook")]
mod outlook;
use super::*;
mod point_events;
mod snapshot_coverage;
mod team_refresh;
mod team_sharing;
mod team_sync_coverage;
use crate::domain::models::{
    GOOGLE_CALENDAR_SCOPES, GoogleCalendarSyncSnapshot, GoogleWatchChannel, OccurrenceException,
    OccurrenceListing, ProviderEventSource, REMINDER_METHOD_EMAIL, REMINDER_METHOD_POPUP,
};
use crate::domain::ports::GoogleCalendarSyncRepository;
use crate::domain::service::{CalendarService, GoogleCalendarBackfillFailureService};
use crate::domain::team::{CalendarTeamRepository, TeamCalendarSharing};
use crate::outbound::pg_team::PgCalendarTeamRepository;
use chrono::{Duration, SubsecRound, TimeZone};
use macro_db_migrator::MACRO_DB_MIGRATIONS;

fn complete_grant() -> GoogleScopeSet {
    GoogleScopeSet::parse(&GOOGLE_CALENDAR_SCOPES.join(" "))
}

async fn persist_complete_grant(pool: &PgPool, link_id: Uuid, grant_version: i64) {
    let scopes = GOOGLE_CALENDAR_SCOPES.map(str::to_owned);
    sqlx::query!(
        r#"
        INSERT INTO email_link_google_scopes (link_id, granted_scopes, grant_version)
        VALUES ($1, $2, $3)
        ON CONFLICT (link_id) DO UPDATE
        SET granted_scopes = EXCLUDED.granted_scopes,
            grant_version = EXCLUDED.grant_version,
            updated_at = now()
        "#,
        link_id,
        &scopes,
        grant_version,
    )
    .execute(pool)
    .await
    .unwrap();
}

async fn insert_link(pool: &PgPool, owner_id: &str) -> Uuid {
    let id = Uuid::now_v7();
    let email_address = format!("calendar-{id}@example.com");
    sqlx::query!(
        r#"
        INSERT INTO email_links (
            id, macro_id, fusionauth_user_id, email_address, provider
        )
        VALUES ($1, $2, $2, $3, 'GMAIL')
        "#,
        id,
        owner_id,
        email_address,
    )
    .execute(pool)
    .await
    .unwrap();
    id
}

async fn insert_user(pool: &PgPool, id: &str) {
    let macro_user_id = Uuid::now_v7();
    let stripe_customer_id = format!("cus_{macro_user_id}");
    sqlx::query!(
        r#"
        INSERT INTO macro_user (id, username, email, stripe_customer_id)
        VALUES ($1, $2, $3, $4)
        "#,
        macro_user_id,
        id,
        id,
        stripe_customer_id,
    )
    .execute(pool)
    .await
    .unwrap();
    let email = id.rsplit_once('|').map(|(_, email)| email).unwrap_or(id);
    sqlx::query!(
        r#"
        INSERT INTO "User" (id, email, macro_user_id)
        VALUES ($1, $2, $3)
        "#,
        id,
        email,
        macro_user_id,
    )
    .execute(pool)
    .await
    .unwrap();
}

/// Provision the account and calendar every event source now requires.
async fn provider_ids(repo: &PgCalendarRepository, link_id: Uuid) -> (Uuid, Uuid) {
    let account_id = repo.upsert_google_account(link_id).await.unwrap();
    let calendar_id = repo
        .upsert_calendar_fixture(
            account_id,
            ProviderCalendar {
                provider_calendar_id: "primary".to_string(),
                name: "Primary".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("owner".to_string()),
                is_primary: true,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap();
    (account_id, calendar_id)
}

fn timed_upsert(
    owner_id: &str,
    link_id: Uuid,
    provider: (Uuid, Uuid),
    uid: &str,
    title: &str,
    sequence: u32,
) -> CalendarEventUpsert {
    let (account_id, calendar_id) = provider;
    let id = Uuid::now_v7();
    let starts_at = Utc.with_ymd_and_hms(2026, 7, 24, 14, 0, 0).unwrap();
    let ends_at = starts_at + Duration::hours(1);
    let second_start = starts_at + Duration::days(1);
    CalendarEventUpsert {
        event: CalendarEvent {
            id,
            owner_id: owner_id.to_string(),
            ical_uid: uid.to_string(),
            calendar_id: Some(calendar_id),
            sources: Vec::new(),
            title: title.to_string(),
            description: None,
            location: None,
            status: EventStatus::Confirmed,
            visibility: EventVisibility::Default,
            transparency: EventTransparency::Opaque,
            event_type: EventType::Default,
            time: EventTime::Timed {
                starts_at,
                ends_at,
                time_zone: Some("UTC".to_string()),
            },
            recurrence_lines: vec!["RRULE:FREQ=DAILY;COUNT=2".to_string()],
            organizer_email: Some("[email protected]".to_string()),
            organizer_name: Some("Organizer".to_string()),
            creator_email: Some("[email protected]".to_string()),
            creator_name: Some("Creator".to_string()),
            conference_url: None,
            conference_provider: None,
            sequence,
            is_read_only: true,
            attendees: vec![CalendarAttendee {
                email: "[email protected]".to_string(),
                display_name: Some("Guest".to_string()),
                response_status: AttendeeResponseStatus::Accepted,
                is_organizer: false,
                is_optional: false,
                is_self: false,
                comment: None,
            }],
            reminders: EventReminders::default(),
            guest_policy: GuestPolicy::default(),
            is_snapshot: false,
            viewer_can: ViewerCapabilities::NONE,
            created_at: starts_at,
            updated_at: starts_at + Duration::minutes(i64::from(sequence)),
        },
        source: CalendarEventSource::Google(ProviderEventSource {
            automatic_decline: None,
            binding: None,
            observed_access_role: Some("owner".to_owned()),
            email_link_id: link_id,
            account_id,
            calendar_id,
            provider_event_id: format!("provider-{uid}"),
            provider_recurring_event_id: None,
            provider_etag: None,
            raw_payload: serde_json::json!({}),
        }),
        overrides: Vec::new(),
        occurrences: vec![
            CalendarOccurrence {
                event_id: id,
                occurrence_key: starts_at.to_rfc3339(),
                recurrence_id: None,
                time: EventTime::Timed {
                    starts_at,
                    ends_at,
                    time_zone: Some("UTC".to_string()),
                },
                is_cancelled: false,
            },
            CalendarOccurrence {
                event_id: id,
                occurrence_key: second_start.to_rfc3339(),
                recurrence_id: None,
                time: EventTime::Timed {
                    starts_at: second_start,
                    ends_at: second_start + Duration::hours(1),
                    time_zone: Some("UTC".to_string()),
                },
                is_cancelled: false,
            },
        ],
    }
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn applying_grant_recreates_missing_side_table_state_from_version_zero(pool: PgPool) {
    let link_id = insert_link(&pool, "macro|[email protected]").await;
    let sentinel_updated_at = Utc.with_ymd_and_hms(2020, 1, 2, 3, 4, 5).unwrap();
    sqlx::query!(
        "UPDATE email_links SET updated_at = $2 WHERE id = $1",
        link_id,
        sentinel_updated_at,
    )
    .execute(&pool)
    .await
    .unwrap();
    sqlx::query!(
        "DELETE FROM email_link_google_scopes WHERE link_id = $1",
        link_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    let grant = PgCalendarRepository::new(pool.clone())
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    assert!(grant.changed);
    assert_eq!(grant.grant_version, 1);

    let persisted = sqlx::query!(
        r#"
        SELECT
            l.updated_at AS "link_updated_at!",
            g.granted_scopes AS "side_scopes!",
            g.grant_version AS "side_version!"
        FROM email_links l
        JOIN email_link_google_scopes g ON g.link_id = l.id
        WHERE l.id = $1
        "#,
        link_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(persisted.link_updated_at, sentinel_updated_at);
    assert_eq!(
        GoogleScopeSet::from_scopes(persisted.side_scopes),
        complete_grant()
    );
    assert_eq!(persisted.side_version, 1);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn calendar_capability_transition_schedules_once_and_failed_jobs_can_retry(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());

    let partial = repo
        .apply_google_grant(
            link_id,
            GoogleScopeSet::parse("https://www.googleapis.com/auth/gmail.modify"),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    assert!(partial.changed);
    assert!(partial.jobs.is_empty());

    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    assert!(enabled.changed);
    assert_eq!(enabled.jobs.len(), 1);

    let duplicate = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    assert!(!duplicate.changed);
    assert!(duplicate.jobs.is_empty());

    let failed_job = enabled.jobs[0].id;
    sqlx::query!(
        "UPDATE calendar_backfill_jobs SET status = 'failed' WHERE id = $1",
        failed_job
    )
    .execute(&pool)
    .await
    .unwrap();
    sqlx::query!(
        "UPDATE calendar_sync_outbox SET published_at = now() WHERE backfill_job_id = $1",
        failed_job
    )
    .execute(&pool)
    .await
    .unwrap();

    let retried = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    assert!(!retried.changed);
    assert_eq!(retried.jobs.len(), 1);
    assert_eq!(retried.jobs[0].id, failed_job);

    let status = sqlx::query_scalar!(
        "SELECT status FROM calendar_backfill_jobs WHERE id = $1",
        failed_job
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    let published_at = sqlx::query_scalar!(
        "SELECT published_at FROM calendar_sync_outbox WHERE backfill_job_id = $1",
        failed_job
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(status, "pending");
    assert!(published_at.is_none());
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn reaper_rearms_a_dead_lettered_pending_job(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let job_id = enabled.jobs[0].id;

    // The drain published the delivery; the deterministic failure then sat the
    // job back to pending without touching the outbox — exactly the wedge.
    sqlx::query!(
        "UPDATE calendar_sync_outbox SET published_at = now() WHERE backfill_job_id = $1",
        job_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    // A job still inside its SQS retry budget keeps getting touched, so the
    // reaper leaves it alone.
    assert_eq!(
        repo.reap_wedged_google_syncs(Utc::now() - Duration::minutes(15))
            .await
            .unwrap(),
        0
    );

    // The job reached a worker once (started_at set) and a deterministic
    // failure sat it back to pending; once the delivery dead-letters nothing
    // touches it again.
    sqlx::query!(
        "UPDATE calendar_backfill_jobs SET started_at = now() - interval '25 minutes', updated_at = now() - interval '20 minutes' WHERE id = $1",
        job_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    assert_eq!(
        repo.reap_wedged_google_syncs(Utc::now() - Duration::minutes(15))
            .await
            .unwrap(),
        1
    );
    let published_at = sqlx::query_scalar!(
        "SELECT published_at FROM calendar_sync_outbox WHERE backfill_job_id = $1",
        job_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert!(
        published_at.is_none(),
        "the reaper republishes the outbox row"
    );

    // Republished rows are no longer published, so a second pass is a no-op.
    assert_eq!(
        repo.reap_wedged_google_syncs(Utc::now() - Duration::minutes(15))
            .await
            .unwrap(),
        0
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn reaper_leaves_a_never_delivered_queued_job_alone(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let job_id = enabled.jobs[0].id;

    // The drain published the delivery, but a backed-up queue means it has not
    // reached a worker yet: started_at is still NULL though the outbox row and
    // the job both look old.
    sqlx::query!(
        "UPDATE calendar_sync_outbox SET published_at = now() - interval '30 minutes' WHERE backfill_job_id = $1",
        job_id,
    )
    .execute(&pool)
    .await
    .unwrap();
    sqlx::query!(
        "UPDATE calendar_backfill_jobs SET updated_at = now() - interval '30 minutes' WHERE id = $1",
        job_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    // Republishing would only duplicate the delivery still in flight, so the
    // reaper leaves an un-claimed job alone.
    assert_eq!(
        repo.reap_wedged_google_syncs(Utc::now() - Duration::minutes(15))
            .await
            .unwrap(),
        0
    );
    let published_at = sqlx::query_scalar!(
        "SELECT published_at FROM calendar_sync_outbox WHERE backfill_job_id = $1",
        job_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert!(
        published_at.is_some(),
        "the queued delivery stays published, not re-armed"
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn reaper_rearms_a_running_job_behind_a_dead_lease(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let job_id = enabled.jobs[0].id;
    let key = CalendarBackfillJobKey {
        job_id,
        email_link_id: link_id,
    };

    let CalendarBackfillClaim::Claimed { .. } = repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("the freshly granted job should be claimable");
    };
    sqlx::query!(
        "UPDATE calendar_sync_outbox SET published_at = now() WHERE backfill_job_id = $1",
        job_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    // A live lease means a worker is on it, so the reaper leaves it alone.
    assert_eq!(
        repo.reap_wedged_google_syncs(Utc::now() - Duration::minutes(15))
            .await
            .unwrap(),
        0
    );

    // The worker died: its lease expired long ago and no longer renews.
    sqlx::query!(
        "UPDATE calendar_backfill_jobs SET lease_expires_at = now() - interval '20 minutes' WHERE id = $1",
        job_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    assert_eq!(
        repo.reap_wedged_google_syncs(Utc::now() - Duration::minutes(15))
            .await
            .unwrap(),
        1
    );
    let row = sqlx::query!(
        r#"
        SELECT job.status, job.lease_token, outbox.published_at
        FROM calendar_backfill_jobs job
        JOIN calendar_sync_outbox outbox ON outbox.backfill_job_id = job.id
        WHERE job.id = $1
        "#,
        job_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(row.status, "pending");
    assert!(row.lease_token.is_none());
    assert!(row.published_at.is_none());
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn removing_calendar_scope_disables_sources_and_fences_the_running_job(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let google_job = enabled
        .jobs
        .iter()
        .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
        .unwrap();
    let account_id = google_job.account_id.unwrap();
    let key = CalendarBackfillJobKey {
        job_id: google_job.id,
        email_link_id: link_id,
    };
    let CalendarBackfillClaim::Claimed { lease_token, .. } =
        repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("Google job should be claimable");
    };
    let calendar_id = repo
        .upsert_google_calendar(
            key,
            lease_token,
            account_id,
            ProviderCalendar {
                provider_calendar_id: "primary".to_string(),
                name: "Primary".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("owner".to_string()),
                is_primary: true,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap()
        .id;
    let mut google = timed_upsert(
        owner_id,
        link_id,
        (account_id, calendar_id),
        "[email protected]",
        "Removed with scope",
        1,
    );
    google.source = CalendarEventSource::Google(ProviderEventSource {
        automatic_decline: None,
        binding: None,
        observed_access_role: Some("owner".to_owned()),
        email_link_id: link_id,
        account_id,
        calendar_id,
        provider_event_id: "provider-event".to_string(),
        provider_recurring_event_id: None,
        provider_etag: None,
        raw_payload: serde_json::json!({}),
    });
    let event_id = repo
        .upsert_event(CalendarEventWrite::GoogleBackfill {
            key,
            lease_token,
            upsert: google,
        })
        .await
        .unwrap()
        .event_id;

    let downgraded = repo
        .apply_google_grant(
            link_id,
            GoogleScopeSet::parse("https://www.googleapis.com/auth/gmail.modify"),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    assert!(downgraded.changed);
    assert!(downgraded.jobs.is_empty());

    let state = sqlx::query!(
        r#"
        SELECT
            account.sync_status,
            calendar.is_deleted,
            google_job.status AS google_job_status,
            google_job.lease_token,
            (SELECT count(*) FROM calendar_event_sources WHERE event_id = $3) AS "source_count!",
            (SELECT count(*) FROM calendar_events WHERE id = $3) AS "event_count!"
        FROM calendar_accounts account
        JOIN calendars calendar ON calendar.account_id = account.id
        JOIN calendar_backfill_jobs google_job ON google_job.id = $2
        WHERE account.id = $1
        "#,
        account_id,
        google_job.id,
        event_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(state.sync_status, "disabled");
    assert!(state.is_deleted);
    assert_eq!(state.google_job_status, "failed");
    assert!(state.lease_token.is_none());
    assert_eq!(state.source_count, 0);
    assert_eq!(state.event_count, 0);
    assert!(matches!(
        repo.claim_google_backfill(key).await.unwrap(),
        CalendarBackfillClaim::Failed
    ));

    let reenabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    assert_eq!(reenabled.jobs.len(), 1);
    assert_eq!(
        sqlx::query_scalar!(
            r#"SELECT sync_status FROM calendar_accounts WHERE id = $1"#,
            account_id
        )
        .fetch_one(&pool)
        .await
        .unwrap(),
        "pending"
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn completed_google_job_is_rearmed_and_reuses_calendar_sync_state(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let google_job = enabled
        .jobs
        .into_iter()
        .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
        .unwrap();
    let account_id = google_job.account_id.unwrap();
    let key = CalendarBackfillJobKey {
        job_id: google_job.id,
        email_link_id: link_id,
    };
    let CalendarBackfillClaim::Claimed { lease_token, .. } =
        repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("Google job should be claimable");
    };
    let provider_calendar = ProviderCalendar {
        provider_calendar_id: "primary".to_string(),
        name: "Primary".to_string(),
        description: None,
        time_zone: Some("UTC".to_string()),
        color: None,
        access_role: Some("owner".to_string()),
        is_primary: true,
        is_selected: true,
        default_reminders: Vec::new(),
    };
    let calendar_id = repo
        .upsert_google_calendar(key, lease_token, account_id, provider_calendar.clone())
        .await
        .unwrap()
        .id;
    // Postgres stores timestamptz at microsecond precision, so the
    // round-tripped range only compares equal from a truncated instant.
    let range = OccurrenceRange::historical_sync(Utc::now().trunc_subsecs(6));
    repo.commit_google_calendar_sync(
        key,
        lease_token,
        account_id,
        GoogleCalendarSyncSnapshot {
            calendar_id,
            next_sync_token: "next-sync-token".to_string(),
            observed_provider_event_ids: Some(Vec::new()),
            materialized_range: Some(range.clone()),
            cancelled_provider_event_ids: Vec::new(),
        },
        3,
    )
    .await
    .unwrap();
    assert_eq!(
        sqlx::query_scalar!(
            r#"SELECT extracted_count FROM calendar_backfill_jobs WHERE id = $1"#,
            key.job_id,
        )
        .fetch_one(&pool)
        .await
        .unwrap(),
        3
    );
    repo.reconcile_google_calendar_list(key, lease_token, account_id, vec![calendar_id])
        .await
        .unwrap();
    repo.complete_google_backfill(key, lease_token)
        .await
        .unwrap();
    sqlx::query!(
        r#"
        UPDATE calendar_accounts
        SET last_synced_at = now() - interval '10 minutes'
        WHERE id = $1
        "#,
        account_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    assert_eq!(
        repo.schedule_due_google_syncs(Utc::now() - Duration::minutes(5))
            .await
            .unwrap(),
        1
    );
    assert_eq!(
        repo.schedule_due_google_syncs(Utc::now() - Duration::minutes(5))
            .await
            .unwrap(),
        0
    );
    let CalendarBackfillClaim::Claimed {
        lease_token: next_lease,
        ..
    } = repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("scheduled Google job should be claimable");
    };
    let stored = repo
        .upsert_google_calendar(key, next_lease, account_id, provider_calendar)
        .await
        .unwrap();
    assert_eq!(stored.sync_token.as_deref(), Some("next-sync-token"));
    assert_eq!(stored.materialized_range, Some(range));
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn google_claim_rejects_a_job_without_its_grant_provisioned_account(pool: PgPool) {
    let link_id = insert_link(&pool, "macro|[email protected]").await;
    let job_id = Uuid::now_v7();
    sqlx::query!(
        r#"
        INSERT INTO calendar_backfill_jobs (
            id, email_link_id, kind, grant_version, status
        )
        VALUES ($1, $2, 'google_calendar', 1, 'pending')
        "#,
        job_id,
        link_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    let result = PgCalendarRepository::new(pool.clone())
        .claim_google_backfill(CalendarBackfillJobKey {
            job_id,
            email_link_id: link_id,
        })
        .await;
    assert!(result.is_err());

    let state = sqlx::query!(
        r#"
        SELECT status, lease_token
        FROM calendar_backfill_jobs
        WHERE id = $1
        "#,
        job_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(state.status, "pending");
    assert!(state.lease_token.is_none());
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn canonical_precedence_uses_the_selected_source_clock(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let source_clock = Utc.with_ymd_and_hms(2026, 7, 24, 14, 0, 0).unwrap();
    let later_clock = source_clock + Duration::hours(3);
    let winning_clock = source_clock + Duration::hours(2);

    repo.apply_google_grant(
        link_id,
        complete_grant(),
        CalendarGrantIntent::CalendarRequested,
    )
    .await
    .unwrap();
    let provider = provider_ids(&repo, link_id).await;

    let mut first = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Earlier sequence, later clock",
        1,
    );
    first.event.updated_at = later_clock;
    let event_id = repo.upsert_event_fixture(first).await.unwrap();

    // A higher sequence wins regardless of clock, so the canonical clock moves
    // backwards while the entity's own updated_at keeps the latest it has seen.
    let mut second = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Later sequence, earlier clock",
        2,
    );
    second.event.updated_at = winning_clock;
    repo.upsert_event_fixture(second).await.unwrap();

    let canonical = sqlx::query!(
        r#"
        SELECT title, schedule_updated_at, updated_at
        FROM calendar_events
        WHERE id = $1
        "#,
        event_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(canonical.title, "Later sequence, earlier clock");
    assert_eq!(canonical.schedule_updated_at, winning_clock);
    assert_eq!(canonical.updated_at, later_clock);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn fenced_google_snapshot_removes_deleted_events_and_calendars(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let account_id = repo.upsert_google_account(link_id).await.unwrap();
    let calendar_id = repo
        .upsert_calendar_fixture(
            account_id,
            ProviderCalendar {
                provider_calendar_id: "removed-calendar".to_string(),
                name: "Removed Calendar".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("owner".to_string()),
                is_primary: false,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap();
    let mut google = timed_upsert(
        owner_id,
        link_id,
        (account_id, calendar_id),
        "[email protected]",
        "Deleted Google event",
        1,
    );
    google.source = CalendarEventSource::Google(ProviderEventSource {
        automatic_decline: None,
        binding: None,
        observed_access_role: Some("owner".to_owned()),
        email_link_id: link_id,
        account_id,
        calendar_id,
        provider_event_id: "deleted-provider-event".to_string(),
        provider_recurring_event_id: None,
        provider_etag: Some("\"old\"".to_string()),
        raw_payload: serde_json::json!({}),
    });
    let event_id = repo.upsert_event_fixture(google).await.unwrap();
    let google_job = enabled
        .jobs
        .into_iter()
        .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
        .unwrap();
    let key = CalendarBackfillJobKey {
        job_id: google_job.id,
        email_link_id: link_id,
    };
    let CalendarBackfillClaim::Claimed {
        lease_token,
        account_id: claimed_account_id,
    } = repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("Google job should be claimable");
    };
    assert_eq!(claimed_account_id, account_id);
    assert!(
        repo.reconcile_google_calendar_list(key, Uuid::new_v4(), account_id, Vec::new())
            .await
            .is_err()
    );
    assert_eq!(
        sqlx::query_scalar!(
            r#"SELECT count(*) AS "count!" FROM calendar_events WHERE id = $1"#,
            event_id,
        )
        .fetch_one(&pool)
        .await
        .unwrap(),
        1
    );

    repo.reconcile_google_calendar_list(key, lease_token, account_id, Vec::new())
        .await
        .unwrap();
    assert_eq!(
        sqlx::query_scalar!(
            r#"SELECT count(*) AS "count!" FROM calendar_events WHERE id = $1"#,
            event_id,
        )
        .fetch_one(&pool)
        .await
        .unwrap(),
        0
    );
    assert!(
        sqlx::query_scalar!(
            r#"SELECT is_deleted AS "is_deleted!" FROM calendars WHERE id = $1"#,
            calendar_id,
        )
        .fetch_one(&pool)
        .await
        .unwrap()
    );

    let outside_start = Utc.with_ymd_and_hms(2026, 8, 2, 0, 0, 0).unwrap();
    let outside_end = Utc.with_ymd_and_hms(2026, 8, 3, 0, 0, 0).unwrap();
    let occurrences = repo
        .list_occurrences(
            owner_id,
            OccurrenceRange {
                starts_at: outside_start,
                ends_at: outside_end,
                start_date: outside_start.date_naive(),
                end_date: outside_end.date_naive(),
            },
            None,
            100,
        )
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();
    assert!(occurrences.is_empty());
    assert_eq!(
        repo.sync_status(owner_id).await.unwrap(),
        CalendarSyncStatus::Syncing
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn expired_google_worker_cannot_resurrect_reconciled_provider_data(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let account_id = repo.upsert_google_account(link_id).await.unwrap();
    let google_job = enabled
        .jobs
        .into_iter()
        .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
        .unwrap();
    let key = CalendarBackfillJobKey {
        job_id: google_job.id,
        email_link_id: link_id,
    };
    let CalendarBackfillClaim::Claimed {
        lease_token: stale_lease,
        ..
    } = repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("Google job should be claimable");
    };
    let provider_calendar = ProviderCalendar {
        provider_calendar_id: "primary".to_string(),
        name: "Primary".to_string(),
        description: None,
        time_zone: Some("UTC".to_string()),
        color: None,
        access_role: Some("owner".to_string()),
        is_primary: true,
        is_selected: true,
        default_reminders: Vec::new(),
    };
    let calendar_id = repo
        .upsert_google_calendar(key, stale_lease, account_id, provider_calendar.clone())
        .await
        .unwrap()
        .id;
    let mut google = timed_upsert(
        owner_id,
        link_id,
        (account_id, calendar_id),
        "[email protected]",
        "Stale worker event",
        1,
    );
    google.source = CalendarEventSource::Google(ProviderEventSource {
        automatic_decline: None,
        binding: None,
        observed_access_role: Some("owner".to_owned()),
        email_link_id: link_id,
        account_id,
        calendar_id,
        provider_event_id: "stale-worker-provider-event".to_string(),
        provider_recurring_event_id: None,
        provider_etag: Some("\"stale\"".to_string()),
        raw_payload: serde_json::json!({}),
    });
    repo.upsert_event(CalendarEventWrite::GoogleBackfill {
        key,
        lease_token: stale_lease,
        upsert: google.clone(),
    })
    .await
    .unwrap();

    sqlx::query!(
        r#"
        UPDATE calendar_backfill_jobs
        SET lease_expires_at = now() - interval '1 second'
        WHERE id = $1
        "#,
        key.job_id,
    )
    .execute(&pool)
    .await
    .unwrap();
    let CalendarBackfillClaim::Claimed {
        lease_token: current_lease,
        ..
    } = repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("expired Google job should be reclaimable");
    };
    repo.reconcile_google_calendar_list(key, current_lease, account_id, Vec::new())
        .await
        .unwrap();
    repo.complete_google_backfill(key, current_lease)
        .await
        .unwrap();

    assert!(
        repo.mark_google_account_syncing(key, stale_lease)
            .await
            .is_err()
    );
    assert_eq!(
        sqlx::query_scalar!(
            r#"SELECT sync_status FROM calendar_accounts WHERE id = $1"#,
            account_id,
        )
        .fetch_one(&pool)
        .await
        .unwrap(),
        "ready"
    );
    assert!(
        repo.upsert_google_calendar(key, stale_lease, account_id, provider_calendar)
            .await
            .is_err()
    );
    assert!(
        repo.upsert_event(CalendarEventWrite::GoogleBackfill {
            key,
            lease_token: stale_lease,
            upsert: google,
        })
        .await
        .is_err()
    );
    assert!(
        sqlx::query_scalar!(
            r#"SELECT is_deleted AS "is_deleted!" FROM calendars WHERE id = $1"#,
            calendar_id,
        )
        .fetch_one(&pool)
        .await
        .unwrap()
    );
    assert_eq!(
        sqlx::query_scalar!(
            r#"
            SELECT count(*) AS "count!"
            FROM calendar_events
            WHERE owner_id = $1
              AND ical_uid = '[email protected]'
            "#,
            owner_id,
        )
        .fetch_one(&pool)
        .await
        .unwrap(),
        0
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn google_snapshot_deletion_removes_an_event_without_a_surviving_source(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let (account_id, calendar_id) = provider_ids(&repo, link_id).await;
    let mut google = timed_upsert(
        owner_id,
        link_id,
        (account_id, calendar_id),
        "[email protected]",
        "Google canonical",
        2,
    );
    google.source = CalendarEventSource::Google(ProviderEventSource {
        automatic_decline: None,
        binding: None,
        observed_access_role: Some("owner".to_owned()),
        email_link_id: link_id,
        account_id,
        calendar_id,
        provider_event_id: "google-fallback-event".to_string(),
        provider_recurring_event_id: None,
        provider_etag: Some("\"etag\"".to_string()),
        raw_payload: serde_json::json!({}),
    });
    let event_id = repo.upsert_event_fixture(google).await.unwrap();

    let google_job = enabled
        .jobs
        .into_iter()
        .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
        .unwrap();
    let key = CalendarBackfillJobKey {
        job_id: google_job.id,
        email_link_id: link_id,
    };
    let CalendarBackfillClaim::Claimed { lease_token, .. } =
        repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("Google job should be claimable");
    };
    repo.commit_google_calendar_sync(
        key,
        lease_token,
        account_id,
        GoogleCalendarSyncSnapshot {
            calendar_id,
            next_sync_token: "next".to_string(),
            observed_provider_event_ids: Some(Vec::new()),
            materialized_range: Some(OccurrenceRange::historical_sync(Utc::now())),
            cancelled_provider_event_ids: Vec::new(),
        },
        0,
    )
    .await
    .unwrap();
    repo.reconcile_google_calendar_list(key, lease_token, account_id, vec![calendar_id])
        .await
        .unwrap();

    let remaining = sqlx::query!(
        r#"
        SELECT
            (SELECT count(*) FROM calendar_events WHERE id = $1) AS "event_count!",
            (SELECT count(*) FROM calendar_event_sources WHERE event_id = $1) AS "source_count!"
        "#,
        event_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!((remaining.event_count, remaining.source_count), (0, 0));
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn incremental_cancellation_tombstones_retire_sources_without_a_snapshot(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let (account_id, calendar_id) = provider_ids(&repo, link_id).await;
    let mut google = timed_upsert(
        owner_id,
        link_id,
        (account_id, calendar_id),
        "[email protected]",
        "Google canonical",
        2,
    );
    google.source = CalendarEventSource::Google(ProviderEventSource {
        automatic_decline: None,
        binding: None,
        observed_access_role: Some("owner".to_owned()),
        email_link_id: link_id,
        account_id,
        calendar_id,
        provider_event_id: "cancelled-event".to_string(),
        provider_recurring_event_id: None,
        provider_etag: Some("\"etag\"".to_string()),
        raw_payload: serde_json::json!({}),
    });
    let event_id = repo.upsert_event_fixture(google).await.unwrap();
    let mut instance = timed_upsert(
        owner_id,
        link_id,
        (account_id, calendar_id),
        "[email protected]",
        "Series instance",
        1,
    );
    instance.source = CalendarEventSource::Google(ProviderEventSource {
        automatic_decline: None,
        binding: None,
        observed_access_role: Some("owner".to_owned()),
        email_link_id: link_id,
        account_id,
        calendar_id,
        provider_event_id: "series-instance".to_string(),
        provider_recurring_event_id: Some("cancelled-master".to_string()),
        provider_etag: Some("\"etag\"".to_string()),
        raw_payload: serde_json::json!({}),
    });
    let instance_event_id = repo.upsert_event_fixture(instance).await.unwrap();

    let google_job = enabled
        .jobs
        .into_iter()
        .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
        .unwrap();
    let key = CalendarBackfillJobKey {
        job_id: google_job.id,
        email_link_id: link_id,
    };
    let CalendarBackfillClaim::Claimed { lease_token, .. } =
        repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("Google job should be claimable");
    };
    repo.commit_google_calendar_sync(
        key,
        lease_token,
        account_id,
        GoogleCalendarSyncSnapshot {
            calendar_id,
            next_sync_token: "incremental-next".to_string(),
            observed_provider_event_ids: None,
            materialized_range: None,
            cancelled_provider_event_ids: vec![
                "cancelled-event".to_string(),
                "cancelled-master".to_string(),
            ],
        },
        0,
    )
    .await
    .unwrap();

    // The tombstone retires the only source, so the entity goes with it.
    let retired = sqlx::query!(
        r#"
        SELECT
            (SELECT count(*) FROM calendar_events WHERE id = $1) AS "event_count!",
            (SELECT count(*) FROM calendar_event_sources WHERE event_id = $1) AS "source_count!"
        "#,
        event_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!((retired.event_count, retired.source_count), (0, 0));

    assert_eq!(
        sqlx::query_scalar!(
            r#"SELECT count(*) AS "count!" FROM calendar_events WHERE id = $1"#,
            instance_event_id,
        )
        .fetch_one(&pool)
        .await
        .unwrap(),
        0
    );
    assert_eq!(
        sqlx::query_scalar!(
            r#"SELECT sync_token FROM calendars WHERE id = $1"#,
            calendar_id,
        )
        .fetch_one(&pool)
        .await
        .unwrap()
        .as_deref(),
        Some("incremental-next")
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn occurrence_range_uses_overlap_indexes_and_preserves_attendees(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool);
    let provider = provider_ids(&repo, link_id).await;
    let upsert = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Range test",
        1,
    );
    repo.upsert_event_fixture(upsert).await.unwrap();

    let starts_at = Utc.with_ymd_and_hms(2026, 7, 24, 14, 30, 0).unwrap();
    let ends_at = Utc.with_ymd_and_hms(2026, 7, 26, 0, 0, 0).unwrap();
    let result = repo
        .list_occurrences(
            owner_id,
            OccurrenceRange {
                starts_at,
                ends_at,
                start_date: starts_at.date_naive(),
                end_date: ends_at.date_naive(),
            },
            None,
            100,
        )
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();

    assert_eq!(result.len(), 2);
    assert!(result.iter().all(|(event, _)| event.attendees.len() == 1));
    assert!(result.iter().all(|(event, _)| {
        event.creator_email.as_deref() == Some("[email protected]")
            && event.creator_name.as_deref() == Some("Creator")
    }));
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn guest_policy_follows_the_canonical_copy(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool);
    let provider = provider_ids(&repo, link_id).await;
    let locked_down = GuestPolicy {
        can_modify: false,
        can_invite_others: false,
        can_see_other_guests: false,
    };
    let range = OccurrenceRange {
        starts_at: Utc.with_ymd_and_hms(2026, 7, 24, 0, 0, 0).unwrap(),
        ends_at: Utc.with_ymd_and_hms(2026, 7, 26, 0, 0, 0).unwrap(),
        start_date: NaiveDate::from_ymd_opt(2026, 7, 24).unwrap(),
        end_date: NaiveDate::from_ymd_opt(2026, 7, 26).unwrap(),
    };
    let mut first = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Planning",
        1,
    );
    first.event.guest_policy = locked_down;
    repo.upsert_event_fixture(first).await.unwrap();

    let listed = repo
        .list_occurrences(owner_id, range.clone(), None, 100)
        .await
        .unwrap();
    assert!(!listed.is_empty());
    assert!(
        listed
            .iter()
            .all(|listing| listing.event.guest_policy == locked_down)
    );

    // The organizer restores Google's defaults; the next sync carries no
    // switch at all and the entity follows.
    let second = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Planning",
        2,
    );
    repo.upsert_event_fixture(second).await.unwrap();

    let listed = repo
        .list_occurrences(owner_id, range, None, 100)
        .await
        .unwrap();
    assert!(
        listed
            .iter()
            .all(|listing| listing.event.guest_policy == GuestPolicy::default())
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_recorded_guest_policy_reaches_the_entity_and_every_copy(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = provider_ids(&repo, link_id).await;
    let event_id = repo
        .upsert_event_fixture(timed_upsert(
            owner_id,
            link_id,
            provider,
            "[email protected]",
            "Planning",
            1,
        ))
        .await
        .unwrap();
    let hidden = GuestPolicy {
        can_modify: false,
        can_invite_others: true,
        can_see_other_guests: false,
    };

    repo.record_guest_policy(event_id, hidden).await.unwrap();

    let entity = sqlx::query!(
        r#"
        SELECT guests_can_modify, guests_can_invite_others, guests_can_see_other_guests
        FROM calendar_events
        WHERE id = $1
        "#,
        event_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert!(!entity.guests_can_modify);
    assert!(entity.guests_can_invite_others);
    assert!(!entity.guests_can_see_other_guests);
    let stored = sqlx::query_scalar!(
        "SELECT normalized_payload FROM calendar_event_sources WHERE event_id = $1",
        event_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    let projection: StoredSourceProjection = serde_json::from_value(stored).unwrap();
    assert_eq!(
        projection.event.guest_policy, hidden,
        "a restore from this copy keeps the recorded policy"
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn source_projections_store_neither_viewer_capabilities_nor_a_default_policy(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = provider_ids(&repo, link_id).await;
    let mut upsert = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Planning",
        1,
    );
    upsert.event.viewer_can = ViewerCapabilities {
        rsvp: true,
        ..ViewerCapabilities::NONE
    };
    let event_id = repo.upsert_event_fixture(upsert).await.unwrap();

    let stored = sqlx::query_scalar!(
        "SELECT normalized_payload FROM calendar_event_sources WHERE event_id = $1",
        event_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();

    // Projections stored before these fields existed lack their keys, and an
    // unchanged event must keep comparing equal to them.
    assert!(stored["event"].get("viewerCan").is_none());
    assert!(stored["event"].get("guestPolicy").is_none());
    assert!(stored["event"].get("isSnapshot").is_none());
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_snapshot_stays_marked_on_reads_and_mutation_targets(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = provider_ids(&repo, link_id).await;
    let mut upsert = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Shared meeting",
        1,
    );
    upsert.event.is_snapshot = true;
    let event_id = repo.upsert_event_fixture(upsert).await.unwrap();

    let listed = repo
        .list_occurrences(
            owner_id,
            OccurrenceRange {
                starts_at: Utc.with_ymd_and_hms(2026, 7, 24, 0, 0, 0).unwrap(),
                ends_at: Utc.with_ymd_and_hms(2026, 7, 26, 0, 0, 0).unwrap(),
                start_date: NaiveDate::from_ymd_opt(2026, 7, 24).unwrap(),
                end_date: NaiveDate::from_ymd_opt(2026, 7, 26).unwrap(),
            },
            None,
            100,
        )
        .await
        .unwrap();
    assert!(!listed.is_empty());
    assert!(listed.iter().all(|listing| listing.event.is_snapshot));
    let target = repo
        .get_event_mutation_target(owner_id, event_id, None)
        .await
        .unwrap()
        .expect("the owner may address their snapshot");
    assert!(target.is_snapshot);
    let stored = sqlx::query_scalar!(
        "SELECT normalized_payload FROM calendar_event_sources WHERE event_id = $1",
        event_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    let projection: StoredSourceProjection = serde_json::from_value(stored).unwrap();
    assert!(
        projection.event.is_snapshot,
        "a restore from this copy keeps it a snapshot"
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_snapshot_an_invitation_reached_stays_its_owners_seat(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = provider_ids(&repo, link_id).await;
    let copy = |sequence, is_snapshot| {
        let mut upsert = timed_upsert(
            owner_id,
            link_id,
            provider,
            "[email protected]",
            "Shared meeting",
            sequence,
        );
        upsert.event.is_snapshot = is_snapshot;
        upsert
    };
    let is_snapshot = |event_id| {
        let repo = &repo;
        async move {
            repo.get_event_mutation_target(owner_id, event_id, None)
                .await
                .unwrap()
                .expect("the owner may address their copy")
                .is_snapshot
        }
    };

    let event_id = repo.upsert_event_fixture(copy(1, true)).await.unwrap();
    repo.upsert_event_fixture(copy(2, true)).await.unwrap();
    assert!(is_snapshot(event_id).await, "a later sync keeps a snapshot");

    // The organizer's invitation lands on the snapshot's Google event.
    repo.upsert_event_fixture(copy(3, false)).await.unwrap();
    assert!(!is_snapshot(event_id).await);
    // Answering can leave Google's copy looking like the import again.
    repo.upsert_event_fixture(copy(4, true)).await.unwrap();
    assert!(!is_snapshot(event_id).await, "the seat stays a seat");
    let entity_is_snapshot = sqlx::query_scalar!(
        "SELECT is_snapshot FROM calendar_events WHERE id = $1",
        event_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert!(!entity_is_snapshot);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_snapshot_beside_the_real_meeting_stays_a_snapshot_when_addressed(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_id) = grant_and_provider_ids(&repo, link_id).await;
    let team_id = repo
        .upsert_calendar_fixture(
            account_id,
            ProviderCalendar {
                provider_calendar_id: "team".to_string(),
                name: "Team".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("writer".to_string()),
                is_primary: false,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap();
    // The real invitation on the primary calendar is the canonical copy.
    let event_id = repo
        .upsert_event(CalendarEventWrite::UserMutation(timed_upsert(
            owner_id,
            link_id,
            (account_id, primary_id),
            "[email protected]",
            "Shared meeting",
            2,
        )))
        .await
        .unwrap()
        .event_id;
    // A snapshot of the same meeting imported into another calendar shares
    // the entity.
    let mut snapshot = timed_upsert(
        owner_id,
        link_id,
        (account_id, team_id),
        "[email protected]",
        "Shared meeting",
        1,
    );
    snapshot.event.is_snapshot = true;
    snapshot.source = CalendarEventSource::Google(ProviderEventSource {
        automatic_decline: None,
        binding: None,
        observed_access_role: Some("writer".to_owned()),
        email_link_id: link_id,
        account_id,
        calendar_id: team_id,
        provider_event_id: "snapshot-id".to_string(),
        provider_recurring_event_id: None,
        provider_etag: None,
        raw_payload: serde_json::json!({}),
    });
    repo.upsert_event(CalendarEventWrite::UserMutation(snapshot))
        .await
        .unwrap();

    let canonical = repo
        .get_event_mutation_target(owner_id, event_id, None)
        .await
        .unwrap()
        .expect("the owner may address the meeting");
    let addressed = repo
        .get_event_mutation_target(owner_id, event_id, Some(team_id))
        .await
        .unwrap()
        .expect("the owner may address their snapshot");

    assert!(
        !canonical.is_snapshot,
        "the real invitation stays answerable"
    );
    assert_eq!(addressed.provider_event_id, "snapshot-id");
    assert!(
        addressed.is_snapshot,
        "an answer addressed to the snapshot's calendar must not slip through"
    );
}

/// A Google out-of-office auto-decline records the decline on the exception
/// instance and never on the series master, so the occurrence carrying that
/// exception must project the exception's attendees while its siblings keep
/// the series answer.
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn listing_keeps_the_series_event_beside_the_exception_and_its_link(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool);
    let provider = provider_ids(&repo, link_id).await;
    let mut upsert = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Series title",
        1,
    );
    let attendee = CalendarAttendee {
        email: "[email protected]".to_string(),
        display_name: None,
        response_status: AttendeeResponseStatus::Accepted,
        is_organizer: false,
        is_optional: false,
        is_self: false,
        comment: None,
    };
    upsert.event.attendees.push(attendee.clone());
    let edited_start = Utc.with_ymd_and_hms(2026, 7, 25, 14, 0, 0).unwrap();
    let recurrence_id = edited_start.to_rfc3339();
    upsert.occurrences[1].recurrence_id = Some(recurrence_id.clone());
    upsert.overrides = vec![CalendarEventOverride {
        reminders: None,
        automatic_decline: None,
        visibility: None,
        transparency: None,
        sequence: None,
        source_updated_at: None,
        recurrence_id: recurrence_id.clone(),
        original_time: EventStart::Timed(edited_start),
        time: EventTime::Timed {
            starts_at: edited_start,
            ends_at: edited_start + Duration::hours(1),
            time_zone: Some("UTC".to_string()),
        },
        title: Some("Edited title".to_string()),
        description: None,
        location: None,
        status: Some(EventStatus::Tentative),
        attendees: Some(vec![CalendarAttendee {
            response_status: AttendeeResponseStatus::Declined,
            ..attendee
        }]),
    }];
    repo.upsert_event_fixture(upsert).await.unwrap();

    let starts_at = Utc.with_ymd_and_hms(2026, 7, 24, 0, 0, 0).unwrap();
    let ends_at = Utc.with_ymd_and_hms(2026, 7, 26, 0, 0, 0).unwrap();
    let listings = repo
        .list_occurrences(
            owner_id,
            OccurrenceRange {
                starts_at,
                ends_at,
                start_date: starts_at.date_naive(),
                end_date: ends_at.date_naive(),
            },
            None,
            100,
        )
        .await
        .unwrap();

    assert_eq!(listings.len(), 2);
    for listing in &listings {
        assert_eq!(listing.link_id, link_id);
        assert_eq!(listing.event.title, "Series title");
        assert_eq!(listing.event.status, EventStatus::Confirmed);
        assert_eq!(
            listing.event.attendees[0].response_status,
            AttendeeResponseStatus::Accepted,
            "the series event is never rewritten by an exception"
        );
    }
    let exception = listings
        .iter()
        .find(|listing| listing.occurrence.occurrence_key == recurrence_id)
        .map(|listing| &listing.exception)
        .expect("the exception occurrence is listed");
    assert_eq!(exception.title.as_deref(), Some("Edited title"));
    assert_eq!(exception.status, Some(EventStatus::Tentative));
    assert_eq!(
        exception
            .attendees
            .as_ref()
            .map(|attendees| attendees[0].response_status),
        Some(AttendeeResponseStatus::Declined)
    );
    let sibling = listings
        .iter()
        .find(|listing| listing.occurrence.occurrence_key != recurrence_id)
        .map(|listing| &listing.exception)
        .expect("the sibling occurrence is listed");
    assert_eq!(*sibling, OccurrenceException::default());
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn occurrence_attendee_override_shadows_the_series_response(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool);
    let provider = provider_ids(&repo, link_id).await;
    let mut upsert = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Declined instance",
        1,
    );
    let self_attendee = CalendarAttendee {
        email: "[email protected]".to_string(),
        display_name: Some("Self".to_string()),
        response_status: AttendeeResponseStatus::Accepted,
        is_organizer: false,
        is_optional: false,
        is_self: true,
        comment: None,
    };
    upsert.event.attendees.push(self_attendee.clone());

    // The second occurrence is the exception: same event, declined for us.
    let declined_start = Utc.with_ymd_and_hms(2026, 7, 25, 14, 0, 0).unwrap();
    let recurrence_id = declined_start.to_rfc3339();
    upsert.occurrences[1].recurrence_id = Some(recurrence_id.clone());
    upsert.overrides = vec![CalendarEventOverride {
        reminders: None,
        automatic_decline: None,
        visibility: None,
        transparency: None,
        sequence: None,
        source_updated_at: None,
        recurrence_id: recurrence_id.clone(),
        original_time: EventStart::Timed(declined_start),
        time: EventTime::Timed {
            starts_at: declined_start,
            ends_at: declined_start + Duration::hours(1),
            time_zone: Some("UTC".to_string()),
        },
        title: None,
        description: None,
        location: None,
        status: Some(EventStatus::Confirmed),
        attendees: Some(vec![CalendarAttendee {
            response_status: AttendeeResponseStatus::Declined,
            comment: Some("Declined because I am out of office".to_string()),
            ..self_attendee
        }]),
    }];
    repo.upsert_event_fixture(upsert).await.unwrap();

    let starts_at = Utc.with_ymd_and_hms(2026, 7, 24, 0, 0, 0).unwrap();
    let ends_at = Utc.with_ymd_and_hms(2026, 7, 26, 0, 0, 0).unwrap();
    let result = repo
        .list_occurrences(
            owner_id,
            OccurrenceRange {
                starts_at,
                ends_at,
                start_date: starts_at.date_naive(),
                end_date: ends_at.date_naive(),
            },
            None,
            100,
        )
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();

    assert_eq!(result.len(), 2);
    let response_at = |key: &str| {
        result
            .iter()
            .find(|(_, occurrence)| occurrence.occurrence_key == key)
            .and_then(|(event, _)| event.attendees.iter().find(|attendee| attendee.is_self))
            .map(|attendee| attendee.response_status)
    };
    assert_eq!(
        response_at(&recurrence_id),
        Some(AttendeeResponseStatus::Declined)
    );
    let series_start = Utc.with_ymd_and_hms(2026, 7, 24, 14, 0, 0).unwrap();
    assert_eq!(
        response_at(&series_start.to_rfc3339()),
        Some(AttendeeResponseStatus::Accepted)
    );
}

/// A single-occurrence edit lands on the exception, never on the master, so
/// the occurrence carrying it must read the exception's content — on the
/// entity and on the calendar copy a client shows — while its siblings keep
/// the series content.
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn occurrence_content_override_shadows_the_series_content(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool);
    let provider = provider_ids(&repo, link_id).await;
    let mut upsert = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Series title",
        1,
    );
    upsert.event.description = Some("Series description".to_string());
    upsert.event.location = Some("Series room".to_string());

    let edited_start = Utc.with_ymd_and_hms(2026, 7, 25, 14, 0, 0).unwrap();
    let recurrence_id = edited_start.to_rfc3339();
    upsert.occurrences[1].recurrence_id = Some(recurrence_id.clone());
    upsert.overrides = vec![CalendarEventOverride {
        reminders: None,
        automatic_decline: None,
        visibility: None,
        transparency: None,
        sequence: None,
        source_updated_at: None,
        recurrence_id: recurrence_id.clone(),
        original_time: EventStart::Timed(edited_start),
        time: EventTime::Timed {
            starts_at: edited_start,
            ends_at: edited_start + Duration::hours(1),
            time_zone: Some("UTC".to_string()),
        },
        title: Some("Edited title".to_string()),
        description: Some("Only this occurrence changed".to_string()),
        location: None,
        status: Some(EventStatus::Tentative),
        attendees: None,
    }];
    repo.upsert_event_fixture(upsert).await.unwrap();

    let starts_at = Utc.with_ymd_and_hms(2026, 7, 24, 0, 0, 0).unwrap();
    let ends_at = Utc.with_ymd_and_hms(2026, 7, 26, 0, 0, 0).unwrap();
    let result = repo
        .list_occurrences(
            owner_id,
            OccurrenceRange {
                starts_at,
                ends_at,
                start_date: starts_at.date_naive(),
                end_date: ends_at.date_naive(),
            },
            None,
            100,
        )
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();

    assert_eq!(result.len(), 2);
    let content_at = |key: &str| {
        let (event, _) = result
            .iter()
            .find(|(_, occurrence)| occurrence.occurrence_key == key)
            .expect("the occurrence is listed");
        let copy = event
            .sources
            .first()
            .expect("the listing carries the calendar copy");
        (
            event.title.as_str(),
            event.description.as_deref(),
            event.location.as_deref(),
            event.status,
            copy.title.as_str(),
            copy.description.as_deref(),
            copy.location.as_deref(),
        )
    };
    assert_eq!(
        content_at(&recurrence_id),
        (
            "Edited title",
            Some("Only this occurrence changed"),
            Some("Series room"),
            EventStatus::Tentative,
            "Edited title",
            Some("Only this occurrence changed"),
            Some("Series room"),
        ),
        "the exception's content replaces the series content on the entity and its copy, \
         and a field the exception leaves unset inherits the series value"
    );
    let series_start = Utc.with_ymd_and_hms(2026, 7, 24, 14, 0, 0).unwrap();
    assert_eq!(
        content_at(&series_start.to_rfc3339()),
        (
            "Series title",
            Some("Series description"),
            Some("Series room"),
            EventStatus::Confirmed,
            "Series title",
            Some("Series description"),
            Some("Series room"),
        ),
        "a sibling occurrence keeps the series content"
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn mention_preview_shows_the_previewed_occurrences_content(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool);
    let provider = provider_ids(&repo, link_id).await;
    let mut upsert = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Series title",
        1,
    );
    upsert.event.description = Some("Series description".to_string());
    upsert.event.location = Some("Series room".to_string());
    let event_id = upsert.event.id;
    let edited_start = Utc.with_ymd_and_hms(2026, 7, 25, 14, 0, 0).unwrap();
    let recurrence_id = edited_start.to_rfc3339();
    upsert.occurrences[1].recurrence_id = Some(recurrence_id.clone());
    upsert.overrides = vec![CalendarEventOverride {
        reminders: None,
        automatic_decline: None,
        visibility: None,
        transparency: None,
        sequence: None,
        source_updated_at: None,
        recurrence_id: recurrence_id.clone(),
        original_time: EventStart::Timed(edited_start),
        time: EventTime::Timed {
            starts_at: edited_start,
            ends_at: edited_start + Duration::hours(1),
            time_zone: Some("UTC".to_string()),
        },
        title: Some("Edited title".to_string()),
        description: Some("Only this occurrence changed".to_string()),
        location: None,
        status: None,
        attendees: None,
    }];
    repo.upsert_event_fixture(upsert).await.unwrap();

    let now = Utc.with_ymd_and_hms(2026, 7, 23, 0, 0, 0).unwrap();
    let series_key = Utc
        .with_ymd_and_hms(2026, 7, 24, 14, 0, 0)
        .unwrap()
        .to_rfc3339();
    let previews = repo
        .mention_previews(
            owner_id,
            [recurrence_id.clone(), series_key]
                .map(|key| CalendarMentionRequestItem {
                    event_id,
                    occurrence_key: Some(key),
                })
                .to_vec(),
            now,
        )
        .await
        .unwrap();
    let content = |preview: &CalendarMentionPreview| match preview {
        CalendarMentionPreview::Accessible(event) => (
            event.title.clone(),
            event.description.clone(),
            event.location.clone(),
        ),
        other => panic!("expected an accessible preview: {other:?}"),
    };

    assert_eq!(
        content(&previews[0]),
        (
            "Edited title".to_string(),
            Some("Only this occurrence changed".to_string()),
            Some("Series room".to_string()),
        ),
        "the exception's content replaces the series content, and an unset field inherits it"
    );
    assert_eq!(
        content(&previews[1]),
        (
            "Series title".to_string(),
            Some("Series description".to_string()),
            Some("Series room".to_string()),
        ),
    );
}

/// An exception that explicitly replaces the attendee list with an empty one
/// must project no attendees for that occurrence — not fall back to the
/// series list, which only an exception without an attendee list inherits.
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn explicitly_empty_override_attendees_do_not_inherit_the_series_list(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool);
    let provider = provider_ids(&repo, link_id).await;
    let mut upsert = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Emptied instance",
        1,
    );

    let emptied_start = Utc.with_ymd_and_hms(2026, 7, 25, 14, 0, 0).unwrap();
    let recurrence_id = emptied_start.to_rfc3339();
    upsert.occurrences[1].recurrence_id = Some(recurrence_id.clone());
    upsert.overrides = vec![CalendarEventOverride {
        reminders: None,
        automatic_decline: None,
        visibility: None,
        transparency: None,
        sequence: None,
        source_updated_at: None,
        recurrence_id: recurrence_id.clone(),
        original_time: EventStart::Timed(emptied_start),
        time: EventTime::Timed {
            starts_at: emptied_start,
            ends_at: emptied_start + Duration::hours(1),
            time_zone: Some("UTC".to_string()),
        },
        title: None,
        description: None,
        location: None,
        status: Some(EventStatus::Confirmed),
        attendees: Some(Vec::new()),
    }];
    repo.upsert_event_fixture(upsert).await.unwrap();

    let starts_at = Utc.with_ymd_and_hms(2026, 7, 24, 0, 0, 0).unwrap();
    let ends_at = Utc.with_ymd_and_hms(2026, 7, 26, 0, 0, 0).unwrap();
    let result = repo
        .list_occurrences(
            owner_id,
            OccurrenceRange {
                starts_at,
                ends_at,
                start_date: starts_at.date_naive(),
                end_date: ends_at.date_naive(),
            },
            None,
            100,
        )
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();

    assert_eq!(result.len(), 2);
    let attendees_at = |key: &str| {
        result
            .iter()
            .find(|(_, occurrence)| occurrence.occurrence_key == key)
            .map(|(event, _)| event.attendees.len())
    };
    let series_start = Utc.with_ymd_and_hms(2026, 7, 24, 14, 0, 0).unwrap();
    assert_eq!(attendees_at(&series_start.to_rfc3339()), Some(1));
    assert_eq!(attendees_at(&recurrence_id), Some(0));
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn occurrence_cursor_is_stable_when_occurrences_share_a_start(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool);
    let provider = provider_ids(&repo, link_id).await;
    for ordinal in 1..=3 {
        repo.upsert_event_fixture(timed_upsert(
            owner_id,
            link_id,
            provider,
            &format!("pagination-{ordinal}@example.com"),
            &format!("Pagination event {ordinal}"),
            ordinal,
        ))
        .await
        .unwrap();
    }

    let starts_at = Utc.with_ymd_and_hms(2026, 7, 24, 0, 0, 0).unwrap();
    let ends_at = Utc.with_ymd_and_hms(2026, 7, 25, 0, 0, 0).unwrap();
    let range = OccurrenceRange {
        starts_at,
        ends_at,
        start_date: starts_at.date_naive(),
        end_date: ends_at.date_naive(),
    };
    let first_page = repo
        .list_occurrences(owner_id, range.clone(), None, 2)
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();
    assert_eq!(first_page.len(), 2);
    let cursor = CalendarOccurrenceCursor::from_occurrence(&first_page[1].1);
    let second_page = repo
        .list_occurrences(owner_id, range, Some(cursor), 2)
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();
    assert_eq!(second_page.len(), 1);

    let mut event_ids: Vec<_> = first_page
        .iter()
        .chain(&second_page)
        .map(|(event, _)| event.id)
        .collect();
    event_ids.sort_unstable();
    event_ids.dedup();
    assert_eq!(event_ids.len(), 3);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn watch_channels_round_trip_from_recording_to_targeted_rearm(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let account_id = repo.upsert_google_account(link_id).await.unwrap();
    let google_job = enabled
        .jobs
        .into_iter()
        .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
        .unwrap();
    let key = CalendarBackfillJobKey {
        job_id: google_job.id,
        email_link_id: link_id,
    };
    let CalendarBackfillClaim::Claimed { lease_token, .. } =
        repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("Google job should be claimable");
    };
    let provider_calendar = ProviderCalendar {
        provider_calendar_id: "primary".to_string(),
        name: "Primary".to_string(),
        description: None,
        time_zone: Some("UTC".to_string()),
        color: None,
        access_role: Some("owner".to_string()),
        is_primary: true,
        is_selected: true,
        default_reminders: Vec::new(),
    };
    let calendar_id = repo
        .upsert_google_calendar(key, lease_token, account_id, provider_calendar.clone())
        .await
        .unwrap()
        .id;

    assert!(
        repo.record_watch_unsupported(key, Uuid::new_v4(), account_id, calendar_id)
            .await
            .is_err(),
        "a stale lease must not record push refusals"
    );
    repo.record_watch_unsupported(key, lease_token, account_id, calendar_id)
        .await
        .unwrap();
    let refused = repo
        .upsert_google_calendar(key, lease_token, account_id, provider_calendar.clone())
        .await
        .unwrap();
    assert!(refused.watch_unsupported_at.is_some());
    assert!(!refused.needs_watch_renewal(Utc::now()));

    let channel = GoogleWatchChannel {
        channel_id: Uuid::new_v4(),
        resource_id: "resource-1".to_string(),
        expires_at: (Utc::now() + Duration::days(6)).trunc_subsecs(6),
    };
    assert!(
        repo.record_watch_channel(
            key,
            Uuid::new_v4(),
            account_id,
            calendar_id,
            channel.clone()
        )
        .await
        .is_err(),
        "a stale lease must not record channels"
    );
    repo.record_watch_channel(key, lease_token, account_id, calendar_id, channel.clone())
        .await
        .unwrap();

    // The stored expiry feeds the renewal decision on the next run.
    let stored = repo
        .upsert_google_calendar(key, lease_token, account_id, provider_calendar)
        .await
        .unwrap();
    assert_eq!(stored.watch_expires_at, Some(channel.expires_at));
    assert_eq!(
        stored.watch_unsupported_at, None,
        "an opened channel clears an earlier push refusal"
    );

    // Notifications resolve the channel to its inbox; unknown or mismatched
    // identifiers resolve to nothing.
    assert_eq!(
        repo.find_watch_target(&channel.channel_id.to_string(), "resource-1")
            .await
            .unwrap(),
        Some(link_id)
    );
    assert_eq!(
        repo.find_watch_target(&channel.channel_id.to_string(), "other-resource")
            .await
            .unwrap(),
        None
    );

    // A completed job re-arms exactly once per notification burst.
    repo.commit_google_calendar_sync(
        key,
        lease_token,
        account_id,
        GoogleCalendarSyncSnapshot {
            calendar_id,
            next_sync_token: "next".to_string(),
            observed_provider_event_ids: Some(Vec::new()),
            materialized_range: Some(OccurrenceRange::historical_sync(Utc::now())),
            cancelled_provider_event_ids: Vec::new(),
        },
        0,
    )
    .await
    .unwrap();
    repo.reconcile_google_calendar_list(key, lease_token, account_id, vec![calendar_id])
        .await
        .unwrap();
    repo.complete_google_backfill(key, lease_token)
        .await
        .unwrap();
    assert!(repo.schedule_google_sync_for_link(link_id).await.unwrap());
    assert!(
        !repo.schedule_google_sync_for_link(link_id).await.unwrap(),
        "a pending job absorbs further notifications"
    );

    // Disabled accounts stop resolving notifications.
    sqlx::query!(
        "UPDATE calendar_accounts SET sync_status = 'disabled' WHERE id = $1",
        account_id,
    )
    .execute(&pool)
    .await
    .unwrap();
    assert_eq!(
        repo.find_watch_target(&channel.channel_id.to_string(), "resource-1")
            .await
            .unwrap(),
        None
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn sync_status_reflects_visible_account_ingestion_state(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let repo = PgCalendarRepository::new(pool.clone());
    assert_eq!(
        repo.sync_status(owner_id).await.unwrap(),
        CalendarSyncStatus::Ready
    );

    let link_id = insert_link(&pool, owner_id).await;
    repo.apply_google_grant(
        link_id,
        complete_grant(),
        CalendarGrantIntent::CalendarRequested,
    )
    .await
    .unwrap();
    let account_id = repo.upsert_google_account(link_id).await.unwrap();
    assert_eq!(
        repo.sync_status(owner_id).await.unwrap(),
        CalendarSyncStatus::Syncing
    );

    sqlx::query!(
        "UPDATE calendar_accounts SET sync_status = 'ready' WHERE id = $1",
        account_id,
    )
    .execute(&pool)
    .await
    .unwrap();
    assert_eq!(
        repo.sync_status(owner_id).await.unwrap(),
        CalendarSyncStatus::Ready
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn only_a_first_google_import_reports_syncing(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let (account_id, _) = provider_ids(&repo, link_id).await;
    let google_job = enabled
        .jobs
        .into_iter()
        .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
        .unwrap();
    let key = CalendarBackfillJobKey {
        job_id: google_job.id,
        email_link_id: link_id,
    };

    let CalendarBackfillClaim::Claimed { lease_token, .. } =
        repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("the first import should be claimable");
    };
    repo.mark_google_account_syncing(key, lease_token)
        .await
        .unwrap();
    assert_eq!(
        repo.sync_status(owner_id).await.unwrap(),
        CalendarSyncStatus::Syncing
    );
    repo.reconcile_google_calendar_list(key, lease_token, account_id, Vec::new())
        .await
        .unwrap();
    repo.complete_google_backfill(key, lease_token)
        .await
        .unwrap();

    assert!(repo.schedule_google_sync_for_link(link_id).await.unwrap());
    let CalendarBackfillClaim::Claimed { lease_token, .. } =
        repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("the routine run should be claimable");
    };
    repo.mark_google_account_syncing(key, lease_token)
        .await
        .unwrap();
    assert_eq!(
        repo.sync_status(owner_id).await.unwrap(),
        CalendarSyncStatus::Ready
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn retiring_sources_raises_the_team_hint_only_for_out_of_office_ones(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let (account_id, calendar_id) = provider_ids(&repo, link_id).await;
    let provider = (account_id, calendar_id);
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);
    let out_of_office = repo
        .upsert_event_fixture(member_primary_upsert(
            owner_id,
            link_id,
            provider,
            "[email protected]",
            starts_at,
        ))
        .await
        .unwrap();
    let meeting = repo
        .upsert_event_fixture(timed_upsert(
            owner_id,
            link_id,
            provider,
            "[email protected]",
            "Weekly sync",
            1,
        ))
        .await
        .unwrap();
    let google_job = enabled
        .jobs
        .into_iter()
        .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
        .unwrap();
    let key = CalendarBackfillJobKey {
        job_id: google_job.id,
        email_link_id: link_id,
    };
    let CalendarBackfillClaim::Claimed { lease_token, .. } =
        repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("Google job should be claimable");
    };

    let retired = repo
        .commit_google_calendar_sync(
            key,
            lease_token,
            account_id,
            GoogleCalendarSyncSnapshot {
                calendar_id,
                next_sync_token: "next".to_string(),
                observed_provider_event_ids: None,
                materialized_range: None,
                cancelled_provider_event_ids: vec![
                    "[email protected]".to_string(),
                    "[email protected]".to_string(),
                ],
            },
            0,
        )
        .await
        .unwrap();

    let hint_of = |event_id: Uuid| {
        retired
            .iter()
            .find(|outcome| outcome.event_id == event_id)
            .map(|outcome| outcome.team_ooo_changed)
    };
    assert_eq!(hint_of(out_of_office), Some(true));
    assert_eq!(hint_of(meeting), Some(false));
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn unchanged_google_projection_skips_the_write_path(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    repo.apply_google_grant(
        link_id,
        complete_grant(),
        CalendarGrantIntent::CalendarRequested,
    )
    .await
    .unwrap();
    let account_id = repo.upsert_google_account(link_id).await.unwrap();
    let calendar_id = repo
        .upsert_calendar_fixture(
            account_id,
            ProviderCalendar {
                provider_calendar_id: "primary".to_string(),
                name: "Primary".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("owner".to_string()),
                is_primary: true,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap();

    let build = |title: &str| {
        let mut upsert = timed_upsert(
            owner_id,
            link_id,
            (account_id, calendar_id),
            "[email protected]",
            title,
            1,
        );
        upsert.source = CalendarEventSource::Google(ProviderEventSource {
            automatic_decline: None,
            binding: None,
            observed_access_role: Some("owner".to_owned()),
            email_link_id: link_id,
            account_id,
            calendar_id,
            provider_event_id: "stable-event".to_string(),
            provider_recurring_event_id: None,
            provider_etag: Some("\"etag\"".to_string()),
            raw_payload: serde_json::json!({}),
        });
        upsert
    };

    let first_id = repo
        .upsert_event_fixture(build("Same title"))
        .await
        .unwrap();
    let generated_before = sqlx::query_scalar!(
        r#"SELECT max(generated_at) AS "max!" FROM calendar_event_occurrences WHERE event_id = $1"#,
        first_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();

    // A rebuild re-mints the proposed entity id, so a fresh mapping of the
    // same provider state must be recognized as unchanged and skipped.
    let second_id = repo
        .upsert_event_fixture(build("Same title"))
        .await
        .unwrap();
    assert_eq!(second_id, first_id);
    let generated_after = sqlx::query_scalar!(
        r#"SELECT max(generated_at) AS "max!" FROM calendar_event_occurrences WHERE event_id = $1"#,
        first_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(generated_after, generated_before);

    // A real change still writes through.
    repo.upsert_event_fixture(build("New title")).await.unwrap();
    let title = sqlx::query_scalar!("SELECT title FROM calendar_events WHERE id = $1", first_id,)
        .fetch_one(&pool)
        .await
        .unwrap();
    assert_eq!(title, "New title");
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn delegated_inbox_source_grants_calendar_visibility(pool: PgPool) {
    let child_id = "macro|[email protected]";
    let primary_id = "macro|[email protected]";
    insert_user(&pool, child_id).await;
    insert_user(&pool, primary_id).await;
    let link_id = insert_link(&pool, child_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = provider_ids(&repo, link_id).await;
    repo.upsert_event_fixture(timed_upsert(
        child_id,
        link_id,
        provider,
        "[email protected]",
        "Delegated event",
        1,
    ))
    .await
    .unwrap();
    sqlx::query!(
        r#"
        INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)
        VALUES ($1, $2, $3)
        "#,
        primary_id,
        child_id,
        link_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    let starts_at = Utc.with_ymd_and_hms(2026, 7, 24, 0, 0, 0).unwrap();
    let ends_at = Utc.with_ymd_and_hms(2026, 7, 26, 0, 0, 0).unwrap();
    let range = OccurrenceRange {
        starts_at,
        ends_at,
        start_date: starts_at.date_naive(),
        end_date: ends_at.date_naive(),
    };
    assert_eq!(
        repo.list_occurrences(primary_id, range.clone(), None, 100)
            .await
            .unwrap()
            .into_iter()
            .map(OccurrenceListing::into_occurrence_event)
            .collect::<Vec<_>>()
            .len(),
        2
    );
    assert!(
        repo.list_occurrences("macro|[email protected]", range, None, 100)
            .await
            .unwrap()
            .into_iter()
            .map(OccurrenceListing::into_occurrence_event)
            .collect::<Vec<_>>()
            .is_empty()
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn identical_uids_from_distinct_inboxes_remain_distinct_and_link_scoped(pool: PgPool) {
    let child_id = "macro|[email protected]";
    let primary_id = "macro|[email protected]";
    insert_user(&pool, child_id).await;
    insert_user(&pool, primary_id).await;
    let delegated_link_id = insert_link(&pool, child_id).await;
    let private_link_id = insert_link(&pool, child_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let delegated_provider = provider_ids(&repo, delegated_link_id).await;
    let private_provider = provider_ids(&repo, private_link_id).await;

    repo.upsert_event_fixture(timed_upsert(
        child_id,
        delegated_link_id,
        delegated_provider,
        "[email protected]",
        "Delegated inbox",
        1,
    ))
    .await
    .unwrap();
    repo.upsert_event_fixture(timed_upsert(
        child_id,
        private_link_id,
        private_provider,
        "[email protected]",
        "Private inbox",
        1,
    ))
    .await
    .unwrap();
    sqlx::query!(
        r#"
        INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)
        VALUES ($1, $2, $3)
        "#,
        primary_id,
        child_id,
        delegated_link_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    let starts_at = Utc.with_ymd_and_hms(2026, 7, 24, 0, 0, 0).unwrap();
    let ends_at = Utc.with_ymd_and_hms(2026, 7, 26, 0, 0, 0).unwrap();
    let result = repo
        .list_occurrences(
            primary_id,
            OccurrenceRange {
                starts_at,
                ends_at,
                start_date: starts_at.date_naive(),
                end_date: ends_at.date_naive(),
            },
            None,
            100,
        )
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();

    assert_eq!(result.len(), 2);
    assert!(
        result
            .iter()
            .all(|(event, _)| event.title == "Delegated inbox")
    );
    let event_count = sqlx::query_scalar!(
        r#"SELECT count(*) AS "count!" FROM calendar_events WHERE owner_id = $1"#,
        child_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(event_count, 2);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn equal_sequence_cannot_replace_a_newer_projection(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = provider_ids(&repo, link_id).await;
    let mut latest = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Latest",
        4,
    );
    latest.event.updated_at += Duration::days(2);
    let event_id = repo.upsert_event_fixture(latest).await.unwrap();

    let mut stale = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Stale",
        4,
    );
    stale.event.updated_at -= Duration::days(2);
    repo.upsert_event_fixture(stale).await.unwrap();

    let title = sqlx::query_scalar!("SELECT title FROM calendar_events WHERE id = $1", event_id)
        .fetch_one(&pool)
        .await
        .unwrap();
    assert_eq!(title, "Latest");
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn stale_google_source_projection_cannot_resurface_during_reconciliation(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let account_id = repo.upsert_google_account(link_id).await.unwrap();
    let primary_calendar_id = repo
        .upsert_calendar_fixture(
            account_id,
            ProviderCalendar {
                provider_calendar_id: "primary".to_string(),
                name: "Primary".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("owner".to_string()),
                is_primary: true,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap();
    let sibling_calendar_id = repo
        .upsert_calendar_fixture(
            account_id,
            ProviderCalendar {
                provider_calendar_id: "sibling".to_string(),
                name: "Sibling".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("owner".to_string()),
                is_primary: false,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap();

    let mut latest = timed_upsert(
        owner_id,
        link_id,
        (account_id, primary_calendar_id),
        "[email protected]",
        "Latest",
        4,
    );
    latest.event.updated_at += Duration::days(2);
    latest.source = CalendarEventSource::Google(ProviderEventSource {
        automatic_decline: None,
        binding: None,
        observed_access_role: Some("owner".to_owned()),
        email_link_id: link_id,
        account_id,
        calendar_id: primary_calendar_id,
        provider_event_id: "primary-event".to_string(),
        provider_recurring_event_id: None,
        provider_etag: Some("\"latest\"".to_string()),
        raw_payload: serde_json::json!({}),
    });
    let event_id = repo.upsert_event_fixture(latest).await.unwrap();

    let mut sibling = timed_upsert(
        owner_id,
        link_id,
        (account_id, sibling_calendar_id),
        "[email protected]",
        "Sibling",
        1,
    );
    sibling.source = CalendarEventSource::Google(ProviderEventSource {
        automatic_decline: None,
        binding: None,
        observed_access_role: Some("owner".to_owned()),
        email_link_id: link_id,
        account_id,
        calendar_id: sibling_calendar_id,
        provider_event_id: "sibling-event".to_string(),
        provider_recurring_event_id: None,
        provider_etag: Some("\"sibling\"".to_string()),
        raw_payload: serde_json::json!({}),
    });
    repo.upsert_event_fixture(sibling).await.unwrap();

    let mut stale = timed_upsert(
        owner_id,
        link_id,
        (account_id, primary_calendar_id),
        "[email protected]",
        "Stale",
        4,
    );
    stale.event.updated_at -= Duration::days(2);
    stale.source = CalendarEventSource::Google(ProviderEventSource {
        automatic_decline: None,
        binding: None,
        observed_access_role: Some("owner".to_owned()),
        email_link_id: link_id,
        account_id,
        calendar_id: primary_calendar_id,
        provider_event_id: "primary-event".to_string(),
        provider_recurring_event_id: None,
        provider_etag: Some("\"stale\"".to_string()),
        raw_payload: serde_json::json!({}),
    });
    repo.upsert_event_fixture(stale).await.unwrap();

    let google_job = enabled
        .jobs
        .into_iter()
        .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
        .unwrap();
    let key = CalendarBackfillJobKey {
        job_id: google_job.id,
        email_link_id: link_id,
    };
    let CalendarBackfillClaim::Claimed { lease_token, .. } =
        repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("Google job should be claimable");
    };
    repo.commit_google_calendar_sync(
        key,
        lease_token,
        account_id,
        GoogleCalendarSyncSnapshot {
            calendar_id: primary_calendar_id,
            next_sync_token: "primary-next".to_string(),
            observed_provider_event_ids: Some(vec!["primary-event".to_string()]),
            materialized_range: Some(OccurrenceRange::historical_sync(Utc::now())),
            cancelled_provider_event_ids: Vec::new(),
        },
        0,
    )
    .await
    .unwrap();
    repo.commit_google_calendar_sync(
        key,
        lease_token,
        account_id,
        GoogleCalendarSyncSnapshot {
            calendar_id: sibling_calendar_id,
            next_sync_token: "sibling-next".to_string(),
            observed_provider_event_ids: Some(Vec::new()),
            materialized_range: Some(OccurrenceRange::historical_sync(Utc::now())),
            cancelled_provider_event_ids: Vec::new(),
        },
        0,
    )
    .await
    .unwrap();
    repo.reconcile_google_calendar_list(
        key,
        lease_token,
        account_id,
        vec![primary_calendar_id, sibling_calendar_id],
    )
    .await
    .unwrap();

    let restored = sqlx::query!(
        r#"
        SELECT title,
               (SELECT count(*) FROM calendar_event_sources WHERE event_id = $1) AS "source_count!"
        FROM calendar_events
        WHERE id = $1
        "#,
        event_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(restored.title, "Latest");
    assert_eq!(restored.source_count, 1);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn unclaimed_google_reauth_failure_is_atomic_and_edge_triggered(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    persist_complete_grant(&pool, link_id, 1).await;
    let account_id = Uuid::now_v7();
    let job_id = Uuid::now_v7();
    sqlx::query!(
        r#"
        INSERT INTO calendar_accounts (
            id, owner_id, email_link_id, provider, provider_account_id
        )
        VALUES ($1, $2, $3, 'google', $4)
        "#,
        account_id,
        owner_id,
        link_id,
        "[email protected]",
    )
    .execute(&pool)
    .await
    .unwrap();
    sqlx::query!(
        r#"
        INSERT INTO calendar_backfill_jobs (
            id, email_link_id, account_id, kind, grant_version, status
        )
        VALUES ($1, $2, $3, 'google_calendar', 1, 'pending')
        "#,
        job_id,
        link_id,
        account_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    let repo = PgCalendarRepository::new(pool.clone());
    let key = CalendarBackfillJobKey {
        job_id,
        email_link_id: link_id,
    };
    let failure_service = GoogleCalendarBackfillFailureService::new(repo);
    let outcome = failure_service
        .fail_unclaimed(
            key,
            CalendarBackfillFailureDisposition::ReauthRequired,
            "grant expired",
        )
        .await
        .unwrap();
    assert!(outcome.job_transitioned);
    assert!(outcome.link_reauth_transitioned);

    let state = sqlx::query!(
        r#"
        SELECT
            job.status AS job_status,
            account.sync_status AS account_status,
            link.needs_reauth,
            link.last_sync_error_at
        FROM calendar_backfill_jobs job
        JOIN calendar_accounts account ON account.id = job.account_id
        JOIN email_links link ON link.id = job.email_link_id
        WHERE job.id = $1
        "#,
        job_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(state.job_status, "failed");
    assert_eq!(state.account_status, "reauth_required");
    assert!(state.needs_reauth);
    assert!(state.last_sync_error_at.is_some());

    let duplicate = failure_service
        .fail_unclaimed(
            key,
            CalendarBackfillFailureDisposition::ReauthRequired,
            "duplicate",
        )
        .await
        .unwrap();
    assert!(!duplicate.job_transitioned);
    assert!(!duplicate.link_reauth_transitioned);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn calendar_permission_failure_does_not_mark_the_gmail_link_for_reauth(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let job = enabled
        .jobs
        .into_iter()
        .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
        .unwrap();
    let outcome = GoogleCalendarBackfillFailureService::new(repo)
        .fail_unclaimed(
            CalendarBackfillJobKey {
                job_id: job.id,
                email_link_id: link_id,
            },
            CalendarBackfillFailureDisposition::CalendarPermissionRequired,
            "calendar consent is missing",
        )
        .await
        .unwrap();

    assert!(outcome.job_transitioned);
    assert!(!outcome.link_reauth_transitioned);
    let state = sqlx::query!(
        r#"
        SELECT account.sync_status, link.needs_reauth
        FROM calendar_accounts account
        JOIN email_links link ON link.id = account.email_link_id
        WHERE account.id = $1
        "#,
        job.account_id.unwrap(),
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(state.sync_status, "reauth_required");
    assert!(!state.needs_reauth);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn unclaimed_permanent_google_failure_marks_account_error(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    persist_complete_grant(&pool, link_id, 1).await;
    let account_id = Uuid::now_v7();
    let job_id = Uuid::now_v7();
    sqlx::query!(
        r#"
        INSERT INTO calendar_accounts (
            id, owner_id, email_link_id, provider, provider_account_id
        )
        VALUES ($1, $2, $3, 'google', $4)
        "#,
        account_id,
        owner_id,
        link_id,
        "[email protected]",
    )
    .execute(&pool)
    .await
    .unwrap();
    sqlx::query!(
        r#"
        INSERT INTO calendar_backfill_jobs (
            id, email_link_id, account_id, kind, grant_version, status
        )
        VALUES ($1, $2, $3, 'google_calendar', 1, 'pending')
        "#,
        job_id,
        link_id,
        account_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    let outcome =
        GoogleCalendarBackfillFailureService::new(PgCalendarRepository::new(pool.clone()))
            .fail_unclaimed(
                CalendarBackfillJobKey {
                    job_id,
                    email_link_id: link_id,
                },
                CalendarBackfillFailureDisposition::Permanent,
                "provider rejected data",
            )
            .await
            .unwrap();
    assert!(outcome.job_transitioned);
    assert!(!outcome.link_reauth_transitioned);

    let account = sqlx::query!(
        "SELECT sync_status, last_sync_error FROM calendar_accounts WHERE id = $1",
        account_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(account.sync_status, "error");
    assert_eq!(
        account.last_sync_error.as_deref(),
        Some("provider rejected data")
    );
}

async fn grant_and_provider_ids(repo: &PgCalendarRepository, link_id: Uuid) -> (Uuid, Uuid) {
    repo.apply_google_grant(
        link_id,
        complete_grant(),
        CalendarGrantIntent::CalendarRequested,
    )
    .await
    .unwrap();
    provider_ids(repo, link_id).await
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn user_mutation_write_persists_a_google_echo_without_a_lease(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = grant_and_provider_ids(&repo, link_id).await;

    let upsert = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Google event",
        1,
    );
    let event_id = repo
        .upsert_event(CalendarEventWrite::UserMutation(upsert))
        .await
        .unwrap()
        .event_id;

    let row = sqlx::query!(
        r#"
        SELECT
            (SELECT title FROM calendar_events WHERE id = $1) AS "title!",
            (SELECT count(*) FROM calendar_event_occurrences WHERE event_id = $1) AS "occurrences!",
            (SELECT count(*) FROM calendar_event_sources WHERE event_id = $1 AND source_kind = 'google') AS "sources!"
        "#,
        event_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(
        (row.title.as_str(), row.occurrences, row.sources),
        ("Google event", 2, 1)
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn mutation_target_resolves_only_for_visible_requesters(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let delegate_id = "macro|[email protected]";
    let stranger_id = "macro|[email protected]";
    insert_user(&pool, owner_id).await;
    insert_user(&pool, delegate_id).await;
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = grant_and_provider_ids(&repo, link_id).await;
    let event_id = repo
        .upsert_event(CalendarEventWrite::UserMutation(timed_upsert(
            owner_id,
            link_id,
            provider,
            "[email protected]",
            "Target event",
            1,
        )))
        .await
        .unwrap()
        .event_id;
    sqlx::query!(
        r#"
        INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)
        VALUES ($1, $2, $3)
        "#,
        delegate_id,
        owner_id,
        link_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    let target = repo
        .get_event_mutation_target(owner_id, event_id, None)
        .await
        .unwrap()
        .expect("owner sees the mutation target");
    assert_eq!(target.provider_event_id, "[email protected]");
    assert_eq!(
        target.master_provider_event_id(),
        "[email protected]"
    );
    assert_eq!(target.account_id, provider.0);
    assert_eq!(target.calendar_id, provider.1);
    assert_eq!(target.provider_calendar_id, "primary");
    assert_eq!(target.owner_id, owner_id);
    let inbox = format!("calendar-{link_id}@example.com");
    assert_eq!(target.token_identity.provider, CalendarProvider::Google);
    assert_eq!(target.token_identity.email_address, inbox);
    assert!(
        target
            .actor
            .as_ref()
            .is_some_and(|actor| actor.matches(&inbox))
    );

    let delegated = repo
        .get_event_mutation_target(delegate_id, event_id, None)
        .await
        .unwrap()
        .expect("delegate sees the mutation target");
    assert_eq!(delegated.token_identity.email_address, inbox);
    assert!(
        delegated.actor.is_none(),
        "a delegate without their own inbox has no actor"
    );

    let hidden = repo
        .get_event_mutation_target(stranger_id, event_id, None)
        .await
        .unwrap();
    assert!(hidden.is_none(), "stranger cannot see the mutation target");
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn primary_time_zone_is_the_primary_calendars(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, _) = grant_and_provider_ids(&repo, link_id).await;
    repo.upsert_calendar_fixture(
        account_id,
        ProviderCalendar {
            provider_calendar_id: "primary".to_string(),
            name: "Primary".to_string(),
            description: None,
            time_zone: Some("America/New_York".to_string()),
            color: None,
            access_role: Some("owner".to_string()),
            is_primary: true,
            is_selected: true,
            default_reminders: Vec::new(),
        },
    )
    .await
    .unwrap();

    let zone = repo.primary_time_zone(owner_id).await.unwrap();
    assert_eq!(zone.as_deref(), Some("America/New_York"));

    let stranger = repo
        .primary_time_zone("macro|[email protected]")
        .await
        .unwrap();
    assert!(stranger.is_none(), "a stranger has no primary calendar");
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn creation_target_actor_is_owned_inboxes_not_the_calendar_inbox(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let delegate_id = "macro|[email protected]";
    insert_user(&pool, owner_id).await;
    insert_user(&pool, delegate_id).await;
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    grant_and_provider_ids(&repo, link_id).await;
    sqlx::query!(
        r#"
        INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)
        VALUES ($1, $2, $3)
        "#,
        delegate_id,
        owner_id,
        link_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    let inbox = format!("calendar-{link_id}@example.com");
    let owner = repo
        .get_creation_target(owner_id, None, None)
        .await
        .unwrap()
        .expect("owner resolves a creation target");
    assert_eq!(owner.token_identity.email_address, inbox);
    assert!(
        owner
            .actor
            .as_ref()
            .is_some_and(|actor| actor.matches(&inbox))
    );

    let delegated = repo
        .get_creation_target(delegate_id, None, None)
        .await
        .unwrap()
        .expect("delegate resolves a creation target");
    assert_eq!(delegated.token_identity.email_address, inbox);
    assert!(
        delegated.actor.is_none(),
        "a delegate without their own inbox has no actor"
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn creation_target_prefers_the_requesters_own_primary_inbox(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, calendar_id) = grant_and_provider_ids(&repo, link_id).await;

    let target = repo
        .get_creation_target(owner_id, None, None)
        .await
        .unwrap()
        .expect("owner resolves a creation target");
    assert_eq!(target.account_id, account_id);
    assert_eq!(target.calendar_id, calendar_id);
    assert_eq!(target.owner_id, owner_id);
    assert!(!target.is_read_only);
    assert!(
        target.is_primary,
        "resolving without a calendar id lands on the primary calendar"
    );

    let explicit = repo
        .get_creation_target(owner_id, Some(link_id), None)
        .await
        .unwrap();
    assert!(explicit.is_some());

    let unknown_link = repo
        .get_creation_target(owner_id, Some(Uuid::now_v7()), None)
        .await
        .unwrap();
    assert!(unknown_link.is_none());

    let stranger = repo
        .get_creation_target("macro|[email protected]", None, None)
        .await
        .unwrap();
    assert!(stranger.is_none());

    let team_id = repo
        .upsert_calendar_fixture(
            account_id,
            ProviderCalendar {
                provider_calendar_id: "team".to_string(),
                name: "Team".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: Some("#33b679".to_string()),
                access_role: Some("writer".to_string()),
                is_primary: false,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap();
    let picked = repo
        .get_creation_target(owner_id, None, Some(team_id))
        .await
        .unwrap()
        .expect("an explicit calendar overrides the primary default");
    assert_eq!(picked.calendar_id, team_id);
    assert_eq!(picked.provider_calendar_id, "team");
    assert!(!picked.is_read_only);

    let foreign_calendar = repo
        .get_creation_target(
            "macro|[email protected]",
            None,
            Some(team_id),
        )
        .await
        .unwrap();
    assert!(
        foreign_calendar.is_none(),
        "calendar picks stay requester-scoped"
    );

    let calendars = repo.list_visible_calendars(owner_id).await.unwrap();
    assert_eq!(
        calendars
            .iter()
            .map(|calendar| (
                calendar.name.as_str(),
                calendar.is_primary,
                calendar.is_writable
            ))
            .collect::<Vec<_>>(),
        vec![("Primary", true, true), ("Team", false, true)]
    );
    assert!(
        repo.list_visible_calendars("macro|[email protected]")
            .await
            .unwrap()
            .is_empty()
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_persistently_failing_calendar_badges_then_clears_on_recovery(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let google_job = enabled
        .jobs
        .iter()
        .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
        .unwrap();
    let account_id = google_job.account_id.unwrap();
    let key = CalendarBackfillJobKey {
        job_id: google_job.id,
        email_link_id: link_id,
    };
    let CalendarBackfillClaim::Claimed { lease_token, .. } =
        repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("Google job should be claimable");
    };
    let primary = ProviderCalendar {
        provider_calendar_id: "primary".to_string(),
        name: "Primary".to_string(),
        description: None,
        time_zone: Some("UTC".to_string()),
        color: None,
        access_role: Some("owner".to_string()),
        is_primary: true,
        is_selected: true,
        default_reminders: Vec::new(),
    };
    let calendar_id = repo
        .upsert_google_calendar(key, lease_token, account_id, primary.clone())
        .await
        .unwrap()
        .id;

    let message = "Precondition check failed. (reasons: conditionNotMet)";
    let sync_error = || async {
        repo.list_visible_calendars(owner_id).await.unwrap()[0]
            .sync_error
            .clone()
    };

    // A couple of isolated failures stay below the badge threshold.
    for _ in 0..2 {
        repo.record_google_calendar_sync_error(key, lease_token, account_id, calendar_id, message)
            .await
            .unwrap();
    }
    assert_eq!(sync_error().await, None);

    // A third failure crosses the threshold and surfaces to the user.
    repo.record_google_calendar_sync_error(key, lease_token, account_id, calendar_id, message)
        .await
        .unwrap();
    assert_eq!(sync_error().await.as_deref(), Some(message));

    // A successful sync clears the badge and resets the failure counter.
    repo.commit_google_calendar_sync(
        key,
        lease_token,
        account_id,
        GoogleCalendarSyncSnapshot {
            calendar_id,
            next_sync_token: "recovered".to_string(),
            observed_provider_event_ids: Some(Vec::new()),
            materialized_range: Some(OccurrenceRange::historical_sync(
                Utc::now().trunc_subsecs(6),
            )),
            cancelled_provider_event_ids: Vec::new(),
        },
        0,
    )
    .await
    .unwrap();
    assert_eq!(sync_error().await, None);

    // A fresh failure after recovery starts the count over, so one blip does
    // not immediately re-badge.
    repo.record_google_calendar_sync_error(key, lease_token, account_id, calendar_id, message)
        .await
        .unwrap();
    assert_eq!(sync_error().await, None);

    // A badged calendar that drops off the provider list and later returns
    // starts clean rather than resurrecting its old badge.
    for _ in 0..2 {
        repo.record_google_calendar_sync_error(key, lease_token, account_id, calendar_id, message)
            .await
            .unwrap();
    }
    assert_eq!(sync_error().await.as_deref(), Some(message));
    repo.reconcile_google_calendar_list(key, lease_token, account_id, Vec::new())
        .await
        .unwrap();
    assert!(
        repo.list_visible_calendars(owner_id)
            .await
            .unwrap()
            .is_empty()
    );
    let resurrected_id = repo
        .upsert_google_calendar(key, lease_token, account_id, primary)
        .await
        .unwrap()
        .id;
    assert_eq!(resurrected_id, calendar_id);
    assert_eq!(sync_error().await, None);
    repo.record_google_calendar_sync_error(key, lease_token, account_id, calendar_id, message)
        .await
        .unwrap();
    assert_eq!(sync_error().await, None);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn removing_a_google_source_restores_the_surviving_calendar_copy(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_id) = grant_and_provider_ids(&repo, link_id).await;
    let secondary_id = repo
        .upsert_calendar_fixture(
            account_id,
            ProviderCalendar {
                provider_calendar_id: "team".to_string(),
                name: "Team".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("writer".to_string()),
                is_primary: false,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap();

    // The same iCalUID observed through two calendars of one inbox: two
    // sources, one canonical entity.
    let mut primary_copy = timed_upsert(
        owner_id,
        link_id,
        (account_id, primary_id),
        "[email protected]",
        "Primary copy",
        2,
    );
    primary_copy.event.is_read_only = false;
    let event_id = repo
        .upsert_event(CalendarEventWrite::UserMutation(primary_copy))
        .await
        .unwrap()
        .event_id;
    let mut team_copy = timed_upsert(
        owner_id,
        link_id,
        (account_id, secondary_id),
        "[email protected]",
        "Team copy",
        1,
    );
    team_copy.source = CalendarEventSource::Google(ProviderEventSource {
        automatic_decline: None,
        binding: None,
        observed_access_role: Some("writer".to_owned()),
        email_link_id: link_id,
        account_id,
        calendar_id: secondary_id,
        provider_event_id: "team-copy-id".to_string(),
        provider_recurring_event_id: None,
        provider_etag: None,
        raw_payload: serde_json::json!({}),
    });
    repo.upsert_event(CalendarEventWrite::UserMutation(team_copy))
        .await
        .unwrap();

    repo.remove_provider_source(account_id, primary_id, "[email protected]")
        .await
        .unwrap();
    let row = sqlx::query!(
        r#"
        SELECT
            (SELECT title FROM calendar_events WHERE id = $1) AS "title!",
            (SELECT calendar_id FROM calendar_event_sources WHERE event_id = $1) AS "calendar_id",
            (SELECT count(*) FROM calendar_event_sources WHERE event_id = $1) AS "sources!"
        "#,
        event_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(
        (row.title.as_str(), row.calendar_id, row.sources),
        ("Team copy", Some(secondary_id), 1),
        "the surviving calendar's copy is promoted back to canonical"
    );

    repo.remove_provider_source(account_id, secondary_id, "team-copy-id")
        .await
        .unwrap();
    let remaining = sqlx::query_scalar!(
        r#"SELECT count(*) AS "count!" FROM calendar_events WHERE id = $1"#,
        event_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(remaining, 0, "an event with no surviving source is deleted");
}

/// One-occurrence event fixture for reminder scheduling tests.
fn reminder_upsert(
    owner_id: &str,
    link_id: Uuid,
    provider: (Uuid, Uuid),
    uid: &str,
    starts_at: chrono::DateTime<Utc>,
    reminders: EventReminders,
) -> CalendarEventUpsert {
    let (account_id, calendar_id) = provider;
    let id = Uuid::now_v7();
    let ends_at = starts_at + Duration::hours(1);
    CalendarEventUpsert {
        event: CalendarEvent {
            id,
            owner_id: owner_id.to_string(),
            ical_uid: uid.to_string(),
            calendar_id: Some(calendar_id),
            sources: Vec::new(),
            title: "Reminder subject".to_string(),
            description: None,
            location: None,
            status: EventStatus::Confirmed,
            visibility: EventVisibility::Default,
            transparency: EventTransparency::Opaque,
            event_type: EventType::Default,
            time: EventTime::Timed {
                starts_at,
                ends_at,
                time_zone: Some("UTC".to_string()),
            },
            recurrence_lines: Vec::new(),
            organizer_email: None,
            organizer_name: None,
            creator_email: None,
            creator_name: None,
            conference_url: None,
            conference_provider: None,
            sequence: 0,
            is_read_only: false,
            attendees: Vec::new(),
            reminders,
            guest_policy: GuestPolicy::default(),
            is_snapshot: false,
            viewer_can: ViewerCapabilities::NONE,
            created_at: starts_at - Duration::days(1),
            updated_at: starts_at - Duration::days(1),
        },
        source: CalendarEventSource::Google(ProviderEventSource {
            automatic_decline: None,
            binding: None,
            observed_access_role: Some("owner".to_owned()),
            email_link_id: link_id,
            account_id,
            calendar_id,
            provider_event_id: format!("provider-{uid}"),
            provider_recurring_event_id: None,
            provider_etag: None,
            raw_payload: serde_json::json!({}),
        }),
        overrides: Vec::new(),
        occurrences: vec![CalendarOccurrence {
            event_id: id,
            occurrence_key: starts_at.to_rfc3339(),
            recurrence_id: None,
            time: EventTime::Timed {
                starts_at,
                ends_at,
                time_zone: Some("UTC".to_string()),
            },
            is_cancelled: false,
        }],
    }
}

fn reminder_attendee(email: &str, declined: bool, is_self: bool) -> CalendarAttendee {
    CalendarAttendee {
        email: email.to_string(),
        display_name: None,
        response_status: if declined {
            AttendeeResponseStatus::Declined
        } else {
            AttendeeResponseStatus::Accepted
        },
        is_organizer: false,
        is_optional: false,
        is_self,
        comment: None,
    }
}

fn popup_reminders(minutes: &[u32]) -> EventReminders {
    EventReminders {
        use_default: false,
        overrides: minutes
            .iter()
            .map(|minutes| EventReminderOverride {
                method: REMINDER_METHOD_POPUP.to_string(),
                minutes: *minutes,
            })
            .collect(),
    }
}

async fn scheduled_firings(pool: &PgPool, event_id: Uuid) -> Vec<(String, i32, DateTime<Utc>)> {
    sqlx::query!(
        r#"
        SELECT occurrence_key, minutes_before, fire_at
        FROM calendar_event_reminder_firings
        WHERE event_id = $1
        ORDER BY fire_at, minutes_before
        "#,
        event_id,
    )
    .fetch_all(pool)
    .await
    .unwrap()
    .into_iter()
    .map(|row| (row.occurrence_key, row.minutes_before, row.fire_at))
    .collect()
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn upsert_materializes_popup_reminder_firings(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = provider_ids(&repo, link_id).await;
    let starts_at = (Utc::now() + Duration::hours(2)).trunc_subsecs(0);

    let mut reminders = popup_reminders(&[10]);
    reminders.overrides.push(EventReminderOverride {
        method: REMINDER_METHOD_EMAIL.to_string(),
        minutes: 5,
    });
    let upsert = reminder_upsert(owner_id, link_id, provider, "alarms", starts_at, reminders);
    let event_id = repo.upsert_event_fixture(upsert.clone()).await.unwrap();

    assert_eq!(
        scheduled_firings(&pool, event_id).await,
        vec![(
            starts_at.to_rfc3339(),
            10,
            starts_at - Duration::minutes(10)
        )],
        "only popup reminders fire Macro notifications"
    );

    // Removing the overrides (back to empty defaults) clears the schedule.
    let mut cleared = upsert;
    cleared.event.reminders = EventReminders::default();
    let same_event = repo.upsert_event_fixture(cleared).await.unwrap();
    assert_eq!(same_event, event_id);
    assert_eq!(scheduled_firings(&pool, event_id).await, Vec::new());
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn calendar_default_reminders_fan_out_to_use_default_events(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, calendar_id) = provider_ids(&repo, link_id).await;
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    let follows_defaults = repo
        .upsert_event_fixture(reminder_upsert(
            owner_id,
            link_id,
            (account_id, calendar_id),
            "defaults",
            starts_at,
            EventReminders::default(),
        ))
        .await
        .unwrap();
    let has_overrides = repo
        .upsert_event_fixture(reminder_upsert(
            owner_id,
            link_id,
            (account_id, calendar_id),
            "overridden",
            starts_at,
            popup_reminders(&[10]),
        ))
        .await
        .unwrap();
    assert_eq!(
        scheduled_firings(&pool, follows_defaults).await,
        Vec::new(),
        "a calendar with no default reminders schedules nothing"
    );

    let updated = repo
        .upsert_calendar_fixture(
            account_id,
            ProviderCalendar {
                provider_calendar_id: "primary".to_string(),
                name: "Primary".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("owner".to_string()),
                is_primary: true,
                is_selected: true,
                default_reminders: vec![EventReminderOverride {
                    method: REMINDER_METHOD_POPUP.to_string(),
                    minutes: 30,
                }],
            },
        )
        .await
        .unwrap();
    assert_eq!(updated, calendar_id);

    assert_eq!(
        scheduled_firings(&pool, follows_defaults).await,
        vec![(
            starts_at.to_rfc3339(),
            30,
            starts_at - Duration::minutes(30)
        )],
        "new calendar defaults reschedule useDefault events"
    );
    assert_eq!(
        scheduled_firings(&pool, has_overrides).await,
        vec![(
            starts_at.to_rfc3339(),
            10,
            starts_at - Duration::minutes(10)
        )],
        "explicit overrides are untouched by a defaults change"
    );
}

/// Status-style events (working location, out of office, focus time,
/// birthdays) never resolve the calendar's default reminders — Google's
/// clients offer no notification setting on them — while explicit overrides
/// still schedule firings.
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn status_events_ignore_calendar_default_reminders(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, calendar_id) = provider_ids(&repo, link_id).await;
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);
    let primary_with_defaults = |minutes: &[u32]| ProviderCalendar {
        provider_calendar_id: "primary".to_string(),
        name: "Primary".to_string(),
        description: None,
        time_zone: Some("UTC".to_string()),
        color: None,
        access_role: Some("owner".to_string()),
        is_primary: true,
        is_selected: true,
        default_reminders: minutes
            .iter()
            .map(|minutes| EventReminderOverride {
                method: REMINDER_METHOD_POPUP.to_string(),
                minutes: *minutes,
            })
            .collect(),
    };
    let updated = repo
        .upsert_calendar_fixture(account_id, primary_with_defaults(&[10]))
        .await
        .unwrap();
    assert_eq!(updated, calendar_id);

    let mut working_location = reminder_upsert(
        owner_id,
        link_id,
        (account_id, calendar_id),
        "office",
        starts_at,
        EventReminders::default(),
    );
    working_location.event.event_type = EventType::WorkingLocation;
    let follows_defaults = repo.upsert_event_fixture(working_location).await.unwrap();
    let meeting = repo
        .upsert_event_fixture(reminder_upsert(
            owner_id,
            link_id,
            (account_id, calendar_id),
            "meeting",
            starts_at,
            EventReminders::default(),
        ))
        .await
        .unwrap();
    assert_eq!(
        scheduled_firings(&pool, follows_defaults).await,
        Vec::new(),
        "useDefault resolves to nothing on a status event"
    );
    assert_eq!(
        scheduled_firings(&pool, meeting).await,
        vec![(
            starts_at.to_rfc3339(),
            10,
            starts_at - Duration::minutes(10)
        )],
        "an ordinary event on the same calendar still follows the defaults"
    );

    let mut with_override = reminder_upsert(
        owner_id,
        link_id,
        (account_id, calendar_id),
        "office-override",
        starts_at,
        popup_reminders(&[5]),
    );
    with_override.event.event_type = EventType::WorkingLocation;
    let overridden = repo.upsert_event_fixture(with_override).await.unwrap();
    assert_eq!(
        scheduled_firings(&pool, overridden).await,
        vec![(starts_at.to_rfc3339(), 5, starts_at - Duration::minutes(5))],
        "an explicit override on a status event still fires"
    );

    repo.upsert_calendar_fixture(account_id, primary_with_defaults(&[30]))
        .await
        .unwrap();
    assert_eq!(
        scheduled_firings(&pool, follows_defaults).await,
        Vec::new(),
        "a defaults change never schedules a status event"
    );
    assert_eq!(
        scheduled_firings(&pool, meeting).await,
        vec![(
            starts_at.to_rfc3339(),
            30,
            starts_at - Duration::minutes(30)
        )],
        "the same defaults change still fans out to ordinary events"
    );
}

/// One event can hold sources on several calendars of one inbox. Only the
/// canonical source (highest sequence/updated_at/seen/id) may drive the
/// firing schedule: a defaults change on a secondary calendar must not
/// delete the canonical schedule and rebuild it from the secondary's
/// defaults.
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn secondary_calendar_changes_leave_the_canonical_schedule_alone(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, canonical_calendar) = provider_ids(&repo, link_id).await;
    let secondary = |minutes: &[u32]| ProviderCalendar {
        provider_calendar_id: "secondary".to_string(),
        name: "Secondary".to_string(),
        description: None,
        time_zone: Some("UTC".to_string()),
        color: None,
        access_role: Some("reader".to_string()),
        is_primary: false,
        is_selected: true,
        default_reminders: minutes
            .iter()
            .map(|minutes| EventReminderOverride {
                method: REMINDER_METHOD_POPUP.to_string(),
                minutes: *minutes,
            })
            .collect(),
    };
    let secondary_calendar = repo
        .upsert_calendar_fixture(account_id, secondary(&[]))
        .await
        .unwrap();

    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);
    let mut canonical_source = reminder_upsert(
        owner_id,
        link_id,
        (account_id, canonical_calendar),
        "shared",
        starts_at,
        popup_reminders(&[10]),
    );
    canonical_source.event.sequence = 1;
    let event_id = repo.upsert_event_fixture(canonical_source).await.unwrap();

    let mut secondary_source = reminder_upsert(
        owner_id,
        link_id,
        (account_id, secondary_calendar),
        "shared",
        starts_at,
        popup_reminders(&[10]),
    );
    secondary_source.event.sequence = 0;
    let same_event = repo.upsert_event_fixture(secondary_source).await.unwrap();
    assert_eq!(same_event, event_id, "both sources attach to one event");

    let canonical_schedule = vec![(
        starts_at.to_rfc3339(),
        10,
        starts_at - Duration::minutes(10),
    )];
    assert_eq!(scheduled_firings(&pool, event_id).await, canonical_schedule);

    repo.upsert_calendar_fixture(account_id, secondary(&[30]))
        .await
        .unwrap();

    assert_eq!(
        scheduled_firings(&pool, event_id).await,
        canonical_schedule,
        "a secondary calendar's defaults change leaves the canonical schedule alone"
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn cancelled_events_and_occurrences_schedule_no_firings(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = provider_ids(&repo, link_id).await;
    let starts_at = (Utc::now() + Duration::hours(2)).trunc_subsecs(0);

    let mut cancelled_event = reminder_upsert(
        owner_id,
        link_id,
        provider,
        "cancelled-event",
        starts_at,
        popup_reminders(&[10]),
    );
    cancelled_event.event.status = EventStatus::Cancelled;
    let event_id = repo.upsert_event_fixture(cancelled_event).await.unwrap();
    assert_eq!(scheduled_firings(&pool, event_id).await, Vec::new());

    let mut cancelled_occurrence = reminder_upsert(
        owner_id,
        link_id,
        provider,
        "cancelled-occurrence",
        starts_at,
        popup_reminders(&[10]),
    );
    cancelled_occurrence.occurrences[0].is_cancelled = true;
    let event_id = repo
        .upsert_event_fixture(cancelled_occurrence)
        .await
        .unwrap();
    assert_eq!(scheduled_firings(&pool, event_id).await, Vec::new());
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn dispatch_repo_sweeps_claims_and_completes(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = provider_ids(&repo, link_id).await;
    // Ten minutes from now with a ten-minute reminder: due right now.
    let starts_at = (Utc::now() + Duration::minutes(10)).trunc_subsecs(0);

    let event_id = repo
        .upsert_event_fixture(reminder_upsert(
            owner_id,
            link_id,
            provider,
            "due",
            starts_at,
            popup_reminders(&[10]),
        ))
        .await
        .unwrap();

    let now = Utc::now();
    let due = repo.due_reminder_firings(now, None, 100).await.unwrap();
    assert_eq!(due.len(), 1);
    let firing = &due[0];
    assert_eq!(
        (firing.event_id, firing.minutes_before, firing.fire_at),
        (event_id, 10, starts_at - Duration::minutes(10)),
    );

    let resolved = repo.find_due_reminder(firing).await.unwrap().unwrap();
    assert_eq!(resolved.owner_id, owner_id);
    assert_eq!(resolved.title, "Reminder subject");
    assert_eq!(resolved.display_time_zone.as_deref(), Some("UTC"));
    assert!(!resolved.declined);

    let retry_before = now - Duration::minutes(5);
    assert!(
        repo.claim_reminder_delivery(firing, retry_before)
            .await
            .unwrap()
    );
    assert!(
        !repo
            .claim_reminder_delivery(firing, retry_before)
            .await
            .unwrap(),
        "a fresh claim is not taken over"
    );

    repo.release_reminder_delivery(firing).await.unwrap();
    assert!(
        repo.claim_reminder_delivery(firing, retry_before)
            .await
            .unwrap(),
        "a released claim can be retaken"
    );

    repo.complete_reminder_delivery(firing).await.unwrap();
    assert_eq!(
        repo.due_reminder_firings(Utc::now(), None, 100)
            .await
            .unwrap(),
        Vec::new(),
        "a completed delivery stops the firing being due"
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn stale_and_declined_firings_resolve_safely(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = provider_ids(&repo, link_id).await;
    let starts_at = (Utc::now() + Duration::minutes(10)).trunc_subsecs(0);

    let mut declined = reminder_upsert(
        owner_id,
        link_id,
        provider,
        "declined",
        starts_at,
        popup_reminders(&[10]),
    );
    declined.event.attendees = vec![CalendarAttendee {
        email: format!("calendar-{link_id}@example.com"),
        display_name: None,
        response_status: AttendeeResponseStatus::Declined,
        is_organizer: false,
        is_optional: false,
        is_self: true,
        comment: None,
    }];
    repo.upsert_event_fixture(declined).await.unwrap();

    let due = repo
        .due_reminder_firings(Utc::now(), None, 100)
        .await
        .unwrap();
    assert_eq!(due.len(), 1);
    let firing = &due[0];
    let resolved = repo.find_due_reminder(firing).await.unwrap().unwrap();
    assert!(resolved.declined, "a declined occurrence must not alert");

    // A firing whose schedule row moved on (the event was rescheduled)
    // resolves to nothing rather than alerting at the old time.
    let mut moved = firing.clone();
    moved.fire_at += Duration::minutes(1);
    assert!(repo.find_due_reminder(&moved).await.unwrap().is_none());

    // Firings staler than the sweep grace are silently dropped.
    let past = repo
        .due_reminder_firings(Utc::now() + Duration::hours(2), None, 100)
        .await
        .unwrap();
    assert_eq!(past, Vec::new());
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn reminder_decline_follows_owner_inbox_not_calendar_self(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let owner_inbox = format!("calendar-{link_id}@example.com");
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = provider_ids(&repo, link_id).await;
    let starts_at = (Utc::now() + Duration::minutes(10)).trunc_subsecs(0);

    let mut coworker_declined = reminder_upsert(
        owner_id,
        link_id,
        provider,
        "coworker-declined",
        starts_at,
        popup_reminders(&[10]),
    );
    coworker_declined.event.attendees = vec![
        reminder_attendee("[email protected]", true, true),
        reminder_attendee(&owner_inbox, false, false),
    ];
    let coworker_event_id = repo.upsert_event_fixture(coworker_declined).await.unwrap();

    let mut owner_declined = reminder_upsert(
        owner_id,
        link_id,
        provider,
        "owner-declined",
        starts_at,
        popup_reminders(&[10]),
    );
    owner_declined.event.attendees = vec![
        reminder_attendee("[email protected]", false, true),
        reminder_attendee(&owner_inbox, true, false),
    ];
    let owner_event_id = repo.upsert_event_fixture(owner_declined).await.unwrap();

    let due = repo
        .due_reminder_firings(Utc::now(), None, 100)
        .await
        .unwrap();
    let firing_for = |event_id| {
        due.iter()
            .find(|firing| firing.event_id == event_id)
            .expect("event has a due firing")
            .clone()
    };

    let coworker = repo
        .find_due_reminder(&firing_for(coworker_event_id))
        .await
        .unwrap()
        .unwrap();
    assert!(
        !coworker.declined,
        "a coworker's declined self row must not suppress the owner's reminder"
    );

    let owner = repo
        .find_due_reminder(&firing_for(owner_event_id))
        .await
        .unwrap()
        .unwrap();
    assert!(
        owner.declined,
        "the owner's own declined inbox must suppress the reminder even when is_self is on a coworker"
    );
}

/// The provider classification has to survive the write and come back on the
/// read path: it is what tells the product a conference is one Macro may
/// detach, so losing it in persistence would put a third-party conference at
/// risk of being destroyed by an edit.
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn conference_provider_round_trips_through_persistence(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = grant_and_provider_ids(&repo, link_id).await;

    let mut upsert = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Meeting with a Meet",
        1,
    );
    upsert.event.conference_url = Some("https://meet.google.com/abc-defg-hij".to_string());
    upsert.event.conference_provider = Some(ConferenceProvider::GoogleMeet);
    repo.upsert_event(CalendarEventWrite::UserMutation(upsert))
        .await
        .unwrap();

    let starts_at = Utc.with_ymd_and_hms(2026, 7, 24, 0, 0, 0).unwrap();
    let ends_at = starts_at + Duration::days(2);
    let occurrences = repo
        .list_occurrences(
            owner_id,
            OccurrenceRange {
                starts_at,
                ends_at,
                start_date: starts_at.date_naive(),
                end_date: ends_at.date_naive(),
            },
            None,
            100,
        )
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();

    let (event, _) = occurrences.first().expect("the event is in range");
    assert_eq!(
        event.conference_url.as_deref(),
        Some("https://meet.google.com/abc-defg-hij")
    );
    assert_eq!(
        event.conference_provider,
        Some(ConferenceProvider::GoogleMeet)
    );
}

/// A grant that also carries Gmail, so a disconnect can be shown to take the
/// calendar scopes and nothing else.
fn gmail_and_calendar_grant() -> GoogleScopeSet {
    GoogleScopeSet::parse(&format!(
        "https://www.googleapis.com/auth/gmail.modify {}",
        GOOGLE_CALENDAR_SCOPES.join(" ")
    ))
}

/// Bring one inbox's calendar fully to life: account, calendar, open push
/// channel, and a synced event with an outstanding reminder delivery claim.
async fn connected_calendar(
    pool: &PgPool,
    repo: &PgCalendarRepository,
    owner_id: &str,
    link_id: Uuid,
) -> (Uuid, Uuid, Uuid, Uuid) {
    let enabled = repo
        .apply_google_grant(
            link_id,
            gmail_and_calendar_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let job = enabled
        .jobs
        .iter()
        .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
        .unwrap();
    let account_id = job.account_id.unwrap();
    let key = CalendarBackfillJobKey {
        job_id: job.id,
        email_link_id: link_id,
    };
    let CalendarBackfillClaim::Claimed { lease_token, .. } =
        repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("Google job should be claimable");
    };
    let calendar_id = repo
        .upsert_google_calendar(
            key,
            lease_token,
            account_id,
            ProviderCalendar {
                provider_calendar_id: "primary".to_string(),
                name: "Primary".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("owner".to_string()),
                is_primary: true,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap()
        .id;
    let channel_id = Uuid::new_v4();
    repo.record_watch_channel(
        key,
        lease_token,
        account_id,
        calendar_id,
        GoogleWatchChannel {
            channel_id,
            resource_id: "resource-1".to_string(),
            expires_at: (Utc::now() + Duration::days(6)).trunc_subsecs(6),
        },
    )
    .await
    .unwrap();
    let event_id = repo
        .upsert_event(CalendarEventWrite::GoogleBackfill {
            key,
            lease_token,
            upsert: timed_upsert(
                owner_id,
                link_id,
                (account_id, calendar_id),
                "[email protected]",
                "Removed by disconnect",
                1,
            ),
        })
        .await
        .unwrap()
        .event_id;
    sqlx::query!(
        r#"
        INSERT INTO calendar_event_reminder_deliveries (
            id, event_id, occurrence_key, minutes_before, fire_at
        )
        VALUES ($1, $2, 'key', 10, now())
        "#,
        Uuid::now_v7(),
        event_id,
    )
    .execute(pool)
    .await
    .unwrap();
    (account_id, calendar_id, event_id, channel_id)
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn disconnecting_calendar_removes_the_data_and_records_the_opt_out(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, _calendar_id, event_id, channel_id) =
        connected_calendar(&pool, &repo, owner_id, link_id).await;

    let disconnected = repo
        .disconnect_provider_calendar(owner_id, link_id)
        .await
        .unwrap()
        .expect("the owner's inbox is disconnectable");

    assert_eq!(
        disconnected.watch_channels,
        vec![CalendarWatchRelease {
            channel_id: channel_id.to_string(),
            resource_id: "resource-1".to_string(),
        }]
    );
    assert_eq!(disconnected.token_identity.fusionauth_user_id, owner_id);
    assert_eq!(
        disconnected.token_identity.provider,
        CalendarProvider::Google
    );

    let remaining = sqlx::query!(
        r#"
        SELECT
            (SELECT count(*) FROM calendar_accounts WHERE email_link_id = $1) AS "accounts!",
            (SELECT count(*) FROM calendars WHERE account_id = $2) AS "calendars!",
            (SELECT count(*) FROM calendar_events WHERE source_link_id = $1) AS "events!",
            (SELECT count(*) FROM calendar_event_sources WHERE source_link_id = $1) AS "sources!",
            (SELECT count(*) FROM calendar_event_occurrences WHERE event_id = $3) AS "occurrences!",
            (SELECT count(*) FROM calendar_event_reminder_deliveries WHERE event_id = $3) AS "deliveries!",
            (SELECT count(*) FROM calendar_backfill_jobs WHERE email_link_id = $1) AS "jobs!"
        "#,
        link_id,
        account_id,
        event_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(remaining.accounts, 0);
    assert_eq!(remaining.calendars, 0);
    assert_eq!(remaining.events, 0);
    assert_eq!(remaining.sources, 0);
    assert_eq!(remaining.occurrences, 0);
    assert_eq!(remaining.deliveries, 0);
    assert_eq!(remaining.jobs, 0);

    // A notification for the closed channel no longer resolves to the inbox.
    assert_eq!(
        repo.find_watch_target(&channel_id.to_string(), "resource-1")
            .await
            .unwrap(),
        None
    );

    let grant = sqlx::query!(
        r#"
        SELECT
            granted_scopes,
            grant_version,
            (calendar_disabled_at IS NOT NULL) AS "opted_out!"
        FROM email_link_google_scopes
        WHERE link_id = $1
        "#,
        link_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(
        GoogleScopeSet::from_scopes(grant.granted_scopes),
        GoogleScopeSet::parse("https://www.googleapis.com/auth/gmail.modify"),
        "Gmail keeps working; only the calendar scopes leave the grant"
    );
    assert_eq!(grant.grant_version, 2);
    assert!(grant.opted_out);
}

/// Consent requests carry `include_granted_scopes=true`, so a later Gmail-only
/// connect reports the calendar scopes Google still holds. That must not
/// resurrect calendar; only a flow that asked for calendar turns it back on.
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_reissued_grant_keeps_calendar_off_until_it_is_requested_again(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    connected_calendar(&pool, &repo, owner_id, link_id).await;
    repo.disconnect_provider_calendar(owner_id, link_id)
        .await
        .unwrap()
        .unwrap();

    let incidental = repo
        .apply_google_grant(
            link_id,
            gmail_and_calendar_grant(),
            CalendarGrantIntent::Incidental,
        )
        .await
        .unwrap();
    assert!(!incidental.changed);
    assert!(incidental.jobs.is_empty());
    let held = sqlx::query!(
        r#"
        SELECT granted_scopes, (calendar_disabled_at IS NOT NULL) AS "opted_out!"
        FROM email_link_google_scopes WHERE link_id = $1
        "#,
        link_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert!(
        !GoogleScopeSet::from_scopes(held.granted_scopes).has_calendar_capability(),
        "a scope that merely rode along must not be recorded"
    );
    assert!(held.opted_out);
    assert_eq!(
        sqlx::query_scalar!(
            "SELECT count(*) AS \"count!\" FROM calendar_accounts WHERE email_link_id = $1",
            link_id
        )
        .fetch_one(&pool)
        .await
        .unwrap(),
        0
    );

    let requested = repo
        .apply_google_grant(
            link_id,
            gmail_and_calendar_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    assert!(requested.changed);
    assert_eq!(requested.jobs.len(), 1);
    let restored = sqlx::query!(
        r#"
        SELECT granted_scopes, (calendar_disabled_at IS NOT NULL) AS "opted_out!"
        FROM email_link_google_scopes WHERE link_id = $1
        "#,
        link_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert!(GoogleScopeSet::from_scopes(restored.granted_scopes).has_calendar_capability());
    assert!(!restored.opted_out);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn only_the_owner_can_disconnect_an_inbox_calendar(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    connected_calendar(&pool, &repo, owner_id, link_id).await;

    assert!(
        repo.disconnect_provider_calendar("macro|[email protected]", link_id)
            .await
            .unwrap()
            .is_none()
    );
    assert_eq!(
        sqlx::query_scalar!(
            "SELECT count(*) AS \"count!\" FROM calendar_accounts WHERE email_link_id = $1",
            link_id
        )
        .fetch_one(&pool)
        .await
        .unwrap(),
        1
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn mention_previews_resolve_through_the_shared_uid(pool: PgPool) {
    let author_id = "macro|[email protected]";
    let attendee_id = "macro|[email protected]";
    let author_link = insert_link(&pool, author_id).await;
    let attendee_link = insert_link(&pool, attendee_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let author_provider = provider_ids(&repo, author_link).await;
    let attendee_provider = provider_ids(&repo, attendee_link).await;

    let author_copy = timed_upsert(
        author_id,
        author_link,
        author_provider,
        "[email protected]",
        "Smart Macro Discussion",
        1,
    );
    let author_event_id = author_copy.event.id;
    repo.upsert_event_fixture(author_copy).await.unwrap();
    let attendee_copy = timed_upsert(
        attendee_id,
        attendee_link,
        attendee_provider,
        "[email protected]",
        "Smart Macro Discussion",
        1,
    );
    let attendee_event_id = attendee_copy.event.id;
    repo.upsert_event_fixture(attendee_copy).await.unwrap();
    let private_copy = timed_upsert(
        author_id,
        author_link,
        author_provider,
        "[email protected]",
        "Author only",
        1,
    );
    let private_event_id = private_copy.event.id;
    repo.upsert_event_fixture(private_copy).await.unwrap();
    let mut cancelled_copy = timed_upsert(
        author_id,
        author_link,
        author_provider,
        "[email protected]",
        "Cancelled",
        1,
    );
    cancelled_copy.event.status = EventStatus::Cancelled;
    let cancelled_event_id = cancelled_copy.event.id;
    repo.upsert_event_fixture(cancelled_copy).await.unwrap();

    let now = Utc.with_ymd_and_hms(2026, 7, 23, 0, 0, 0).unwrap();
    let request = |event_id| CalendarMentionRequestItem {
        event_id,
        occurrence_key: None,
    };

    let previews = repo
        .mention_previews(
            author_id,
            vec![request(author_event_id), request(Uuid::now_v7())],
            now,
        )
        .await
        .unwrap();
    let CalendarMentionPreview::Accessible(own) = &previews[0] else {
        panic!("author preview should be accessible: {:?}", previews[0]);
    };
    assert_eq!(own.viewer_event_id, Some(author_event_id));
    assert_eq!(own.title, "Smart Macro Discussion");
    assert!(own.is_recurring);
    assert_eq!(own.attendee_count, 1);
    assert_eq!(previews[1], CalendarMentionPreview::DoesNotExist);

    // The attendee resolves the author's row to their own projection, and
    // never sees the author-only or cancelled meetings.
    let previews = repo
        .mention_previews(
            attendee_id,
            vec![
                request(author_event_id),
                request(private_event_id),
                request(cancelled_event_id),
            ],
            now,
        )
        .await
        .unwrap();
    let CalendarMentionPreview::Accessible(resolved) = &previews[0] else {
        panic!("attendee preview should be accessible: {:?}", previews[0]);
    };
    assert_eq!(resolved.viewer_event_id, Some(attendee_event_id));
    assert_eq!(previews[1], CalendarMentionPreview::NoAccess);
    assert_eq!(previews[2], CalendarMentionPreview::DoesNotExist);
}

async fn share_with_channel(pool: &PgPool, event_id: Uuid, channel_id: Uuid) {
    sqlx::query!(
        r#"
        INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level)
        VALUES ($1, 'calendar_event', $2::uuid::text, 'channel', 'view')
        "#,
        event_id,
        channel_id,
    )
    .execute(pool)
    .await
    .unwrap();
}

async fn insert_channel(pool: &PgPool, owner_id: &str, members: &[&str]) -> Uuid {
    let channel_id = Uuid::now_v7();
    sqlx::query!(
        "INSERT INTO comms_channels (id, channel_type, owner_id) VALUES ($1, 'private', $2)",
        channel_id,
        owner_id,
    )
    .execute(pool)
    .await
    .unwrap();
    for member in members {
        sqlx::query!(
            "INSERT INTO comms_channel_participants (channel_id, user_id, role) VALUES ($1, $2, 'member')",
            channel_id,
            *member,
        )
        .execute(pool)
        .await
        .unwrap();
    }
    channel_id
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn mention_previews_fall_back_to_a_channel_shared_projection(pool: PgPool) {
    let author_id = "macro|[email protected]";
    let attendee_id = "macro|[email protected]";
    let member_id = "macro|[email protected]";
    let former_id = "macro|[email protected]";
    let stranger_id = "macro|[email protected]";
    let author_link = insert_link(&pool, author_id).await;
    let attendee_link = insert_link(&pool, attendee_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let author_provider = provider_ids(&repo, author_link).await;
    let attendee_provider = provider_ids(&repo, attendee_link).await;

    let mut author_copy = timed_upsert(
        author_id,
        author_link,
        author_provider,
        "[email protected]",
        "Pilates",
        1,
    );
    author_copy.event.description = Some("Bring a mat".to_string());
    let shared_event_id = author_copy.event.id;
    repo.upsert_event_fixture(author_copy).await.unwrap();
    let attendee_copy = timed_upsert(
        attendee_id,
        attendee_link,
        attendee_provider,
        "[email protected]",
        "Pilates",
        1,
    );
    let attendee_event_id = attendee_copy.event.id;
    repo.upsert_event_fixture(attendee_copy).await.unwrap();
    let mut private_copy = timed_upsert(
        author_id,
        author_link,
        author_provider,
        "[email protected]",
        "Doctor",
        1,
    );
    private_copy.event.visibility = EventVisibility::Private;
    let private_event_id = private_copy.event.id;
    repo.upsert_event_fixture(private_copy).await.unwrap();
    let unshared_copy = timed_upsert(
        author_id,
        author_link,
        author_provider,
        "[email protected]",
        "Unshared",
        1,
    );
    let unshared_event_id = unshared_copy.event.id;
    repo.upsert_event_fixture(unshared_copy).await.unwrap();

    let channel_id = insert_channel(
        &pool,
        author_id,
        &[author_id, attendee_id, member_id, former_id],
    )
    .await;
    sqlx::query!(
        "UPDATE comms_channel_participants SET left_at = now() WHERE channel_id = $1 AND user_id = $2",
        channel_id,
        former_id,
    )
    .execute(&pool)
    .await
    .unwrap();
    share_with_channel(&pool, shared_event_id, channel_id).await;
    share_with_channel(&pool, private_event_id, channel_id).await;

    let now = Utc.with_ymd_and_hms(2026, 7, 23, 0, 0, 0).unwrap();
    let request = |event_id| CalendarMentionRequestItem {
        event_id,
        occurrence_key: None,
    };

    // A member without a copy sees the shared projection read-only.
    let previews = repo
        .mention_previews(
            member_id,
            vec![
                request(shared_event_id),
                request(private_event_id),
                request(unshared_event_id),
            ],
            now,
        )
        .await
        .unwrap();
    let CalendarMentionPreview::Accessible(shared) = &previews[0] else {
        panic!("member preview should be accessible: {:?}", previews[0]);
    };
    assert_eq!(shared.viewer_event_id, None);
    assert_eq!(shared.title, "Pilates");
    assert_eq!(shared.description.as_deref(), Some("Bring a mat"));
    assert!(shared.occurrence_key.is_some());
    // Private events stay hidden despite the grant, and a grant covers only
    // the event it names.
    assert_eq!(previews[1], CalendarMentionPreview::NoAccess);
    assert_eq!(previews[2], CalendarMentionPreview::NoAccess);

    // A member with their own copy keeps resolving to it.
    let previews = repo
        .mention_previews(attendee_id, vec![request(shared_event_id)], now)
        .await
        .unwrap();
    let CalendarMentionPreview::Accessible(own) = &previews[0] else {
        panic!("attendee preview should be accessible: {:?}", previews[0]);
    };
    assert_eq!(own.viewer_event_id, Some(attendee_event_id));

    // So does the owner, who can open the private event they shared.
    let previews = repo
        .mention_previews(author_id, vec![request(private_event_id)], now)
        .await
        .unwrap();
    let CalendarMentionPreview::Accessible(owned) = &previews[0] else {
        panic!("owner preview should be accessible: {:?}", previews[0]);
    };
    assert_eq!(owned.viewer_event_id, Some(private_event_id));

    // Nonparticipants and people who left the channel get nothing.
    for outsider in [stranger_id, former_id] {
        let previews = repo
            .mention_previews(outsider, vec![request(shared_event_id)], now)
            .await
            .unwrap();
        assert_eq!(previews[0], CalendarMentionPreview::NoAccess, "{outsider}");
    }

    // A cancelled event no longer previews for anyone.
    sqlx::query!(
        "UPDATE calendar_events SET status = 'cancelled' WHERE id = $1",
        shared_event_id,
    )
    .execute(&pool)
    .await
    .unwrap();
    let previews = repo
        .mention_previews(member_id, vec![request(shared_event_id)], now)
        .await
        .unwrap();
    assert_eq!(previews[0], CalendarMentionPreview::DoesNotExist);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn mention_preview_picks_the_requested_or_nearest_occurrence(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link_id = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = provider_ids(&repo, link_id).await;
    let upsert = timed_upsert(
        owner_id,
        link_id,
        provider,
        "[email protected]",
        "Series",
        1,
    );
    let event_id = upsert.event.id;
    let first_start = Utc.with_ymd_and_hms(2026, 7, 24, 14, 0, 0).unwrap();
    let second_start = first_start + Duration::days(1);
    repo.upsert_event_fixture(upsert).await.unwrap();

    let preview_time = |preview: &CalendarMentionPreview| match preview {
        CalendarMentionPreview::Accessible(event) => {
            (event.time.clone(), event.occurrence_key.clone())
        }
        other => panic!("expected an accessible preview: {other:?}"),
    };

    // Before the series: the next upcoming instance.
    let before = Utc.with_ymd_and_hms(2026, 7, 23, 0, 0, 0).unwrap();
    let previews = repo
        .mention_previews(
            owner_id,
            vec![CalendarMentionRequestItem {
                event_id,
                occurrence_key: None,
            }],
            before,
        )
        .await
        .unwrap();
    let (time, key) = preview_time(&previews[0]);
    assert_eq!(key.as_deref(), Some(first_start.to_rfc3339().as_str()));
    assert!(matches!(time, EventTime::Timed { starts_at, .. } if starts_at == first_start));

    // Between instances: still the next upcoming one.
    let between = Utc.with_ymd_and_hms(2026, 7, 24, 18, 0, 0).unwrap();
    let previews = repo
        .mention_previews(
            owner_id,
            vec![CalendarMentionRequestItem {
                event_id,
                occurrence_key: None,
            }],
            between,
        )
        .await
        .unwrap();
    let (_, key) = preview_time(&previews[0]);
    assert_eq!(key.as_deref(), Some(second_start.to_rfc3339().as_str()));

    // After the series: the latest past instance.
    let after = Utc.with_ymd_and_hms(2026, 8, 1, 0, 0, 0).unwrap();
    let previews = repo
        .mention_previews(
            owner_id,
            vec![CalendarMentionRequestItem {
                event_id,
                occurrence_key: None,
            }],
            after,
        )
        .await
        .unwrap();
    let (_, key) = preview_time(&previews[0]);
    assert_eq!(key.as_deref(), Some(second_start.to_rfc3339().as_str()));

    // A requested instance wins regardless of the clock.
    let previews = repo
        .mention_previews(
            owner_id,
            vec![CalendarMentionRequestItem {
                event_id,
                occurrence_key: Some(first_start.to_rfc3339()),
            }],
            after,
        )
        .await
        .unwrap();
    let (_, key) = preview_time(&previews[0]);
    assert_eq!(key.as_deref(), Some(first_start.to_rfc3339().as_str()));
}

async fn insert_team(pool: &PgPool, owner_id: &str, member_ids: &[&str]) -> Uuid {
    for member_id in member_ids {
        insert_user(pool, member_id).await;
    }
    let team_id = Uuid::now_v7();
    sqlx::query!(
        r#"INSERT INTO team (id, name, owner_id) VALUES ($1, 'Team', $2)"#,
        team_id,
        owner_id,
    )
    .execute(pool)
    .await
    .unwrap();
    for member_id in member_ids {
        sqlx::query!(
            r#"INSERT INTO team_user (user_id, team_id, team_role) VALUES ($1, $2, 'member')"#,
            member_id,
            team_id,
        )
        .execute(pool)
        .await
        .unwrap();
    }
    team_id
}

fn ooo_upsert(
    owner_id: &str,
    link_id: Uuid,
    provider: (Uuid, Uuid),
    uid: &str,
    title: &str,
) -> CalendarEventUpsert {
    let mut upsert = timed_upsert(owner_id, link_id, provider, uid, title, 1);
    upsert.event.event_type = EventType::OutOfOffice;
    upsert.event.attendees.clear();
    upsert.event.recurrence_lines.clear();
    upsert
}

fn july_2026_range() -> OccurrenceRange {
    let starts_at = Utc.with_ymd_and_hms(2026, 7, 24, 0, 0, 0).unwrap();
    let ends_at = Utc.with_ymd_and_hms(2026, 7, 27, 0, 0, 0).unwrap();
    OccurrenceRange {
        starts_at,
        ends_at,
        start_date: starts_at.date_naive(),
        end_date: ends_at.date_naive(),
    }
}

/// An explicit detail grant and successful provider snapshot for OOO fixtures.
async fn enable_team_details(repo: &PgCalendarRepository, users: &[&str]) {
    sqlx::query!(
        "INSERT INTO calendar_team_sharing(user_id,sharing) SELECT unnest($1::text[]),'all' ON CONFLICT(user_id) DO UPDATE SET sharing='all'",
        users as &[&str]
    ).execute(&repo.pool).await.unwrap();
    mark_team_sources_verified(repo, users).await;
}

/// A successful provider snapshot for OOO fixtures, leaving sharing untouched.
async fn mark_team_sources_verified(repo: &PgCalendarRepository, users: &[&str]) {
    sqlx::query!(
        "UPDATE calendar_accounts SET sync_status='ready',last_synced_at=now(),last_sync_error=NULL WHERE owner_id=ANY($1::text[]) AND sync_status<>'disabled'",
        users as &[&str]
    ).execute(&repo.pool).await.unwrap();
    sqlx::query!(
        "UPDATE calendars SET synced_at=now() WHERE account_id IN (SELECT id FROM calendar_accounts WHERE owner_id=ANY($1::text[]))",
        users as &[&str]
    ).execute(&repo.pool).await.unwrap();
    sqlx::query!(
        "UPDATE calendars SET snapshot_normalization_version=1 WHERE account_id IN (SELECT id FROM calendar_accounts WHERE owner_id=ANY($1::text[]))",
        users as &[&str]
    ).execute(&repo.pool).await.unwrap();
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn team_out_of_office_returns_teammates_status_only(pool: PgPool) {
    let requester = "macro|[email protected]";
    let teammate = "macro|[email protected]";
    let outsider = "macro|[email protected]";
    insert_team(&pool, requester, &[requester, teammate]).await;
    insert_team(&pool, outsider, &[outsider]).await;
    let requester_link = insert_link(&pool, requester).await;
    let teammate_link = insert_link(&pool, teammate).await;
    let outsider_link = insert_link(&pool, outsider).await;
    let repo = PgCalendarRepository::new(pool);
    let requester_provider = provider_ids(&repo, requester_link).await;
    let teammate_provider = provider_ids(&repo, teammate_link).await;
    let outsider_provider = provider_ids(&repo, outsider_link).await;
    repo.upsert_event_fixture(ooo_upsert(
        requester,
        requester_link,
        requester_provider,
        "[email protected]",
        "My own OOO",
    ))
    .await
    .unwrap();
    repo.upsert_event_fixture(ooo_upsert(
        teammate,
        teammate_link,
        teammate_provider,
        "[email protected]",
        "Vacation",
    ))
    .await
    .unwrap();
    repo.upsert_event_fixture(timed_upsert(
        teammate,
        teammate_link,
        teammate_provider,
        "[email protected]",
        "Regular meeting",
        1,
    ))
    .await
    .unwrap();
    repo.upsert_event_fixture(ooo_upsert(
        outsider,
        outsider_link,
        outsider_provider,
        "[email protected]",
        "Elsewhere",
    ))
    .await
    .unwrap();

    enable_team_details(&repo, &[teammate]).await;

    let rows = repo
        .list_team_out_of_office(requester, july_2026_range(), 100)
        .await
        .unwrap();

    assert_eq!(rows.len(), 2);
    assert!(rows.iter().all(|row| row.owner_id == teammate));
    assert!(
        rows.iter()
            .all(|row| row.title.as_deref() == Some("Vacation"))
    );
    let starts: Vec<_> = rows
        .iter()
        .map(|row| match row.time {
            EventTime::Timed { starts_at, .. } => starts_at,
            EventTime::AllDay { .. } => panic!("out-of-office fixtures are timed"),
        })
        .collect();
    assert!(starts.windows(2).all(|pair| pair[0] <= pair[1]));

    let empty = repo
        .list_team_out_of_office(outsider, july_2026_range(), 100)
        .await
        .unwrap();
    assert!(empty.is_empty());
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn team_out_of_office_requires_an_active_primary_calendar_source(pool: PgPool) {
    let requester = "macro|[email protected]";
    let subscribed = "macro|[email protected]";
    let disabled = "macro|[email protected]";
    insert_team(&pool, requester, &[requester, subscribed, disabled]).await;
    let subscribed_link = insert_link(&pool, subscribed).await;
    let disabled_link = insert_link(&pool, disabled).await;
    let repo = PgCalendarRepository::new(pool.clone());

    // An out-of-office event on a calendar the teammate merely subscribes to
    // is someone else's status and must not surface as theirs.
    let subscribed_account = repo.upsert_google_account(subscribed_link).await.unwrap();
    let subscribed_calendar = repo
        .upsert_calendar_fixture(
            subscribed_account,
            ProviderCalendar {
                provider_calendar_id: "someone-else".to_string(),
                name: "Someone else".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("reader".to_string()),
                is_primary: false,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap();
    repo.upsert_event_fixture(ooo_upsert(
        subscribed,
        subscribed_link,
        (subscribed_account, subscribed_calendar),
        "[email protected]",
        "Not their own",
    ))
    .await
    .unwrap();

    let disabled_provider = provider_ids(&repo, disabled_link).await;
    repo.upsert_event_fixture(ooo_upsert(
        disabled,
        disabled_link,
        disabled_provider,
        "[email protected]",
        "Stale",
    ))
    .await
    .unwrap();
    sqlx::query!(
        r#"UPDATE calendar_accounts SET sync_status = 'disabled' WHERE email_link_id = $1"#,
        disabled_link,
    )
    .execute(&pool)
    .await
    .unwrap();

    enable_team_details(&repo, &[subscribed, disabled]).await;

    let rows = repo
        .list_team_out_of_office(requester, july_2026_range(), 100)
        .await
        .unwrap();

    assert!(rows.is_empty());
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn team_out_of_office_hides_private_status_even_with_detail_sharing(pool: PgPool) {
    let requester = "macro|[email protected]";
    let teammate = "macro|[email protected]";
    insert_team(&pool, requester, &[requester, teammate]).await;
    let first_link = insert_link(&pool, teammate).await;
    let second_link = insert_link(&pool, teammate).await;
    let repo = PgCalendarRepository::new(pool);
    let first_provider = provider_ids(&repo, first_link).await;
    let second_provider = provider_ids(&repo, second_link).await;
    let mut private_ooo = ooo_upsert(
        teammate,
        first_link,
        first_provider,
        "[email protected]",
        "Surgery",
    );
    private_ooo.event.visibility = EventVisibility::Private;
    repo.upsert_event_fixture(private_ooo).await.unwrap();
    let mut duplicate = ooo_upsert(
        teammate,
        second_link,
        second_provider,
        "[email protected]",
        "Surgery",
    );
    duplicate.event.visibility = EventVisibility::Private;
    repo.upsert_event_fixture(duplicate).await.unwrap();

    enable_team_details(&repo, &[teammate]).await;
    let service = CalendarService::new(repo).with_team_sharing_enabled(true);
    let rows = service
        .list_team_out_of_office(requester, july_2026_range(), 100)
        .await
        .unwrap();

    assert!(
        rows.is_empty(),
        "the OOO endpoint must not reveal the category of private busy time"
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn team_out_of_office_shows_busy_only_status_without_its_title(pool: PgPool) {
    let viewer = "macro|[email protected]";
    let unset = "macro|[email protected]";
    let busy = "macro|[email protected]";
    let hidden = "macro|[email protected]";
    insert_team(&pool, viewer, &[viewer, unset, busy, hidden]).await;
    let repo = PgCalendarRepository::new(pool.clone());
    for (owner, uid) in [
        (unset, "[email protected]"),
        (busy, "[email protected]"),
        (hidden, "[email protected]"),
    ] {
        let link = insert_link(&pool, owner).await;
        let provider = provider_ids(&repo, link).await;
        repo.upsert_event_fixture(ooo_upsert(owner, link, provider, uid, "Beach trip"))
            .await
            .unwrap();
    }
    mark_team_sources_verified(&repo, &[unset, busy, hidden]).await;
    let team = PgCalendarTeamRepository::new(pool);
    team.set_sharing(busy, TeamCalendarSharing::BusyOnly)
        .await
        .unwrap();
    team.set_sharing(hidden, TeamCalendarSharing::None)
        .await
        .unwrap();

    let rows = CalendarService::new(repo)
        .with_team_sharing_enabled(true)
        .list_team_out_of_office(viewer, july_2026_range(), 100)
        .await
        .unwrap();

    // A member who never chose a setting shares busy blocks by default.
    let mut owners: Vec<_> = rows.iter().map(|row| row.owner_id.as_str()).collect();
    owners.sort_unstable();
    owners.dedup();
    assert_eq!(owners, [busy, unset]);
    assert!(
        rows.iter().all(|row| row.title.is_none()),
        "busy-only sharing discloses the status, never the title"
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn team_out_of_office_reads_each_sources_own_event_type(pool: PgPool) {
    let viewer = "macro|[email protected]";
    let teammate = "macro|[email protected]";
    insert_team(&pool, viewer, &[viewer, teammate]).await;
    let link = insert_link(&pool, teammate).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let provider = provider_ids(&repo, link).await;
    repo.upsert_event_fixture(ooo_upsert(
        teammate,
        link,
        provider,
        "[email protected]",
        "Parental leave",
    ))
    .await
    .unwrap();
    // The entity's stored type follows its canonical source, which can be a
    // non-out-of-office copy of the same event; the primary calendar's own
    // source still says out of office.
    sqlx::query!(
        "UPDATE calendar_events SET event_type = 'default' WHERE owner_id = $1",
        teammate,
    )
    .execute(&pool)
    .await
    .unwrap();
    enable_team_details(&repo, &[teammate]).await;

    let rows = repo
        .list_team_out_of_office(viewer, july_2026_range(), 100)
        .await
        .unwrap();

    assert_eq!(rows.len(), 2);
    assert!(
        rows.iter()
            .all(|row| row.owner_id == teammate && row.title.as_deref() == Some("Parental leave"))
    );
}

fn shift_upsert_days(upsert: &mut CalendarEventUpsert, days: i64) {
    let delta = Duration::days(days);
    if let EventTime::Timed {
        starts_at, ends_at, ..
    } = &mut upsert.event.time
    {
        *starts_at += delta;
        *ends_at += delta;
    }
    for occurrence in &mut upsert.occurrences {
        if let EventTime::Timed {
            starts_at, ends_at, ..
        } = &mut occurrence.time
        {
            *starts_at += delta;
            *ends_at += delta;
        }
        occurrence.occurrence_key = occurrence.time.occurrence_key();
    }
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn team_out_of_office_deduplicates_before_the_limit(pool: PgPool) {
    let requester = "macro|[email protected]";
    let teammate = "macro|[email protected]";
    insert_team(&pool, requester, &[requester, teammate]).await;
    let first_link = insert_link(&pool, teammate).await;
    let second_link = insert_link(&pool, teammate).await;
    let repo = PgCalendarRepository::new(pool);
    let first_provider = provider_ids(&repo, first_link).await;
    let second_provider = provider_ids(&repo, second_link).await;

    // The earliest event exists on both of the teammate's inboxes, so its
    // occurrences arrive first and duplicated.
    repo.upsert_event_fixture(ooo_upsert(
        teammate,
        first_link,
        first_provider,
        "[email protected]",
        "Duplicated",
    ))
    .await
    .unwrap();
    repo.upsert_event_fixture(ooo_upsert(
        teammate,
        second_link,
        second_provider,
        "[email protected]",
        "Duplicated",
    ))
    .await
    .unwrap();
    let mut later = ooo_upsert(
        teammate,
        first_link,
        first_provider,
        "[email protected]",
        "Later",
    );
    shift_upsert_days(&mut later, 1);
    repo.upsert_event_fixture(later).await.unwrap();

    enable_team_details(&repo, &[teammate]).await;

    let rows = repo
        .list_team_out_of_office(requester, july_2026_range(), 3)
        .await
        .unwrap();

    // Duplicates collapse before the limit: the page fills with distinct
    // occurrences and the later unique event still claims a slot, so a
    // full page keeps signalling truncation accurately.
    assert_eq!(rows.len(), 3);
    let mut identities: Vec<_> = rows
        .iter()
        .map(|row| (row.ical_uid.as_str(), row.occurrence_key.as_str()))
        .collect();
    identities.sort_unstable();
    identities.dedup();
    assert_eq!(identities.len(), 3);
    assert!(
        rows.iter()
            .any(|row| row.ical_uid == "[email protected]")
    );
}

/// A shared calendar an account subscribes to, on which Google's
/// vacation-calendar script re-imports members' out-of-office events under
/// the same UID with a bracketed title and the type flattened to `default`.
async fn insert_shared_calendar(
    repo: &PgCalendarRepository,
    account_id: Uuid,
    access_role: &str,
    default_reminders: &[u32],
) -> Uuid {
    repo.upsert_calendar_fixture(
        account_id,
        ProviderCalendar {
            provider_calendar_id: "[email protected]".to_string(),
            name: "Macro Vacation".to_string(),
            description: None,
            time_zone: Some("UTC".to_string()),
            color: None,
            access_role: Some(access_role.to_string()),
            is_primary: false,
            is_selected: true,
            default_reminders: default_reminders
                .iter()
                .map(|minutes| EventReminderOverride {
                    method: REMINDER_METHOD_POPUP.to_string(),
                    minutes: *minutes,
                })
                .collect(),
        },
    )
    .await
    .unwrap()
}

/// The member's own out-of-office event on their primary calendar, carrying
/// distinctive values for every per-copy field so a copy that overwrites one
/// is caught: editable, busy, private, with an explicit 30-minute reminder,
/// created by the member.
fn member_primary_upsert(
    owner_id: &str,
    link_id: Uuid,
    provider: (Uuid, Uuid),
    uid: &str,
    starts_at: DateTime<Utc>,
) -> CalendarEventUpsert {
    let mut upsert = reminder_upsert(
        owner_id,
        link_id,
        provider,
        uid,
        starts_at,
        popup_reminders(&[30]),
    );
    upsert.event.title = "OOO".to_string();
    upsert.event.event_type = EventType::OutOfOffice;
    upsert.event.is_read_only = false;
    upsert.event.transparency = EventTransparency::Opaque;
    upsert.event.visibility = EventVisibility::Private;
    upsert.event.creator_email = Some("[email protected]".to_string());
    upsert.event.creator_name = Some("Teo".to_string());
    upsert
}

/// The shared calendar's re-imported copy of the member's event: the same
/// provider event id, the same sequence, a provider `updated` stamp one hour
/// newer than the member's own copy, and every per-copy field reframed the
/// way Google records a copy — bracketed title, `default` type, reader
/// access, marked free, default visibility, its own reminder, the script's
/// creator.
fn shared_copy_upsert(
    owner_id: &str,
    link_id: Uuid,
    provider: (Uuid, Uuid),
    uid: &str,
    starts_at: DateTime<Utc>,
) -> CalendarEventUpsert {
    let (account_id, calendar_id) = provider;
    let mut upsert = reminder_upsert(
        owner_id,
        link_id,
        provider,
        uid,
        starts_at,
        popup_reminders(&[5]),
    );
    upsert.event.title = "[teo] OOO".to_string();
    upsert.event.event_type = EventType::Default;
    upsert.event.is_read_only = true;
    upsert.event.transparency = EventTransparency::Transparent;
    upsert.event.visibility = EventVisibility::Default;
    upsert.event.creator_email = Some("[email protected]".to_string());
    upsert.event.creator_name = Some("Vacation script".to_string());
    upsert.event.updated_at += Duration::hours(1);
    upsert.source = CalendarEventSource::Google(ProviderEventSource {
        automatic_decline: None,
        binding: None,
        observed_access_role: Some("reader".to_owned()),
        email_link_id: link_id,
        account_id,
        calendar_id,
        provider_event_id: format!("provider-{uid}"),
        provider_recurring_event_id: None,
        provider_etag: None,
        raw_payload: serde_json::json!({}),
    });
    upsert
}

struct EntityContent {
    title: String,
    event_type: String,
    is_read_only: bool,
    transparency: String,
    visibility: String,
    reminders_use_default: bool,
    reminder_overrides: serde_json::Value,
    creator_email: Option<String>,
    sequence: i32,
    schedule_updated_at: DateTime<Utc>,
}

async fn entity_content(pool: &PgPool, event_id: Uuid) -> EntityContent {
    sqlx::query_as!(
        EntityContent,
        r#"
        SELECT title, event_type, is_read_only, transparency, visibility,
               reminders_use_default, reminder_overrides, creator_email,
               sequence, schedule_updated_at
        FROM calendar_events WHERE id = $1
        "#,
        event_id,
    )
    .fetch_one(pool)
    .await
    .unwrap()
}

/// Assert the entity carries the [`member_primary_upsert`] values rather than
/// a shared copy's.
fn assert_primary_content(content: &EntityContent) {
    assert_eq!(content.title, "OOO", "title");
    assert_eq!(content.event_type, "out_of_office", "event_type");
    assert!(!content.is_read_only, "is_read_only");
    assert_eq!(content.transparency, "opaque", "transparency");
    assert_eq!(content.visibility, "private", "visibility");
    assert!(!content.reminders_use_default, "reminders_use_default");
    assert_eq!(
        content.reminder_overrides,
        serde_json::json!([{ "method": REMINDER_METHOD_POPUP, "minutes": 30 }]),
        "reminder_overrides",
    );
    assert_eq!(content.creator_email.as_deref(), Some("[email protected]"));
}

fn range_around(starts_at: DateTime<Utc>) -> OccurrenceRange {
    let range_start = starts_at - Duration::days(1);
    let range_end = starts_at + Duration::days(1);
    OccurrenceRange {
        starts_at: range_start,
        ends_at: range_end,
        start_date: range_start.date_naive(),
        end_date: range_end.date_naive(),
    }
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn shared_calendar_copy_records_its_content_without_touching_the_primary_entity(
    pool: PgPool,
) {
    let member = "macro|[email protected]";
    let viewer = "macro|[email protected]";
    insert_team(&pool, member, &[member, viewer]).await;
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_calendar_id) = provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "reader", &[]).await;
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    // The member's own copy syncs first. The shared copy arrives an hour
    // later with the same sequence and a newer provider stamp.
    let event_id = repo
        .upsert_event_fixture(member_primary_upsert(
            member,
            link_id,
            (account_id, primary_calendar_id),
            uid,
            starts_at,
        ))
        .await
        .unwrap();
    let outcome = repo
        .upsert_event(CalendarEventWrite::Fixture(shared_copy_upsert(
            member,
            link_id,
            (account_id, shared_calendar_id),
            uid,
            starts_at,
        )))
        .await
        .unwrap();
    assert_eq!(
        outcome.event_id, event_id,
        "both copies attach to one entity"
    );
    assert_eq!(
        outcome.change,
        CalendarEventChange::Updated,
        "a new copy is a change readers must refetch"
    );

    assert_primary_content(&entity_content(&pool, event_id).await);

    // The listing carries both copies, the primary first, and attributes the
    // event to the primary calendar.
    let rows = repo
        .list_occurrences(member, range_around(starts_at), None, 10)
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();
    let (event, _) = rows.first().expect("the merged event lists once");
    assert_eq!(rows.len(), 1);
    assert_eq!(event.calendar_id, Some(primary_calendar_id));
    assert_eq!(event.title, "OOO");
    assert_eq!(event.event_type, EventType::OutOfOffice);
    let copies: Vec<_> = event
        .sources
        .iter()
        .map(|source| {
            (
                source.calendar_id,
                source.title.as_str(),
                source.event_type,
                source.is_read_only,
                source.transparency,
                source.reminders.popup_minutes(&[]),
                source.creator_email.as_deref(),
            )
        })
        .collect();
    assert_eq!(
        copies,
        vec![
            (
                primary_calendar_id,
                "OOO",
                EventType::OutOfOffice,
                false,
                EventTransparency::Opaque,
                vec![30],
                Some("[email protected]"),
            ),
            (
                shared_calendar_id,
                "[teo] OOO",
                EventType::Default,
                true,
                EventTransparency::Transparent,
                vec![5],
                Some("[email protected]"),
            ),
        ]
    );

    // The semantic team overlay cannot reveal the primary copy's private OOO
    // category, even when the member explicitly shares ordinary event details.
    enable_team_details(&repo, &[member]).await;
    let team = repo
        .list_team_out_of_office(viewer, range_around(starts_at), 100)
        .await
        .unwrap();
    assert!(team.is_empty(), "private OOO remains generic availability");

    // Reminders fire once, per the member's own 30-minute setting.
    assert_eq!(
        scheduled_firings(&pool, event_id).await,
        vec![(
            starts_at.to_rfc3339(),
            30,
            starts_at - Duration::minutes(30)
        )]
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_late_primary_sync_reclaims_the_entity_from_a_shared_copy(pool: PgPool) {
    let member = "macro|[email protected]";
    let viewer = "macro|[email protected]";
    insert_team(&pool, member, &[member, viewer]).await;
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_calendar_id) = provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "reader", &[]).await;
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    // The shared copy backfills first, so the entity starts life as the copy.
    let event_id = repo
        .upsert_event_fixture(shared_copy_upsert(
            member,
            link_id,
            (account_id, shared_calendar_id),
            uid,
            starts_at,
        ))
        .await
        .unwrap();
    let seeded = entity_content(&pool, event_id).await;
    assert_eq!(
        (seeded.title.as_str(), seeded.event_type.as_str()),
        ("[teo] OOO", "default")
    );
    assert!(
        {
            enable_team_details(&repo, &[member]).await;
            repo.list_team_out_of_office(viewer, range_around(starts_at), 100)
                .await
                .unwrap()
                .is_empty()
        },
        "a copy on a subscribed calendar is not the member's own status"
    );

    // The member's primary copy syncs later with an older provider stamp. It
    // loses the freshness comparison but owns the entity outright.
    let outcome = repo
        .upsert_event(CalendarEventWrite::Fixture(member_primary_upsert(
            member,
            link_id,
            (account_id, primary_calendar_id),
            uid,
            starts_at,
        )))
        .await
        .unwrap();
    assert_eq!(outcome.event_id, event_id);
    assert_eq!(outcome.change, CalendarEventChange::Updated);

    let content = entity_content(&pool, event_id).await;
    assert_primary_content(&content);
    assert_eq!(
        content.schedule_updated_at,
        starts_at - Duration::days(1),
        "the schedule stamp follows the primary copy"
    );
    let rows = repo
        .list_occurrences(member, range_around(starts_at), None, 10)
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();
    assert_eq!(rows[0].0.calendar_id, Some(primary_calendar_id));
    assert_eq!(
        rows[0]
            .0
            .sources
            .iter()
            .map(|source| source.calendar_id)
            .collect::<Vec<_>>(),
        vec![primary_calendar_id, shared_calendar_id]
    );
    assert!(
        repo.list_team_out_of_office(viewer, range_around(starts_at), 100)
            .await
            .unwrap()
            .is_empty(),
        "a private primary copy must not reveal the member's OOO category"
    );
    assert_eq!(
        scheduled_firings(&pool, event_id).await,
        vec![(
            starts_at.to_rfc3339(),
            30,
            starts_at - Duration::minutes(30)
        )]
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn an_hourly_shared_copy_reimport_never_reclaims_the_entity(pool: PgPool) {
    let member = "macro|[email protected]";
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_calendar_id) = provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "reader", &[]).await;
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    let event_id = repo
        .upsert_event_fixture(member_primary_upsert(
            member,
            link_id,
            (account_id, primary_calendar_id),
            uid,
            starts_at,
        ))
        .await
        .unwrap();

    // Every import carries the original's sequence and a later provider
    // `updated`.
    let mut copy = shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at,
    );
    for _ in 0..3 {
        copy.event.updated_at += Duration::hours(1);
        repo.upsert_event_fixture(copy.clone()).await.unwrap();
        assert_primary_content(&entity_content(&pool, event_id).await);
    }
    let copy_title = sqlx::query_scalar!(
        r#"SELECT title FROM calendar_event_sources WHERE event_id = $1 AND calendar_id = $2"#,
        event_id,
        shared_calendar_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(
        copy_title, "[teo] OOO",
        "the copy's own row keeps its content"
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_primary_edit_propagates_over_a_fresher_shared_copy(pool: PgPool) {
    let member = "macro|[email protected]";
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_calendar_id) = provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "reader", &[]).await;
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    let event_id = repo
        .upsert_event_fixture(member_primary_upsert(
            member,
            link_id,
            (account_id, primary_calendar_id),
            uid,
            starts_at,
        ))
        .await
        .unwrap();
    repo.upsert_event_fixture(shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at,
    ))
    .await
    .unwrap();

    // The member renames their event and drops the reminder; the echo carries
    // a higher sequence than the copy and rewrites the entity.
    let mut edited = member_primary_upsert(
        member,
        link_id,
        (account_id, primary_calendar_id),
        uid,
        starts_at,
    );
    edited.event.sequence = 1;
    edited.event.title = "Out of office".to_string();
    edited.event.reminders = EventReminders::default();
    edited.event.updated_at += Duration::hours(2);
    repo.upsert_event(CalendarEventWrite::UserMutation(edited))
        .await
        .unwrap();

    let content = entity_content(&pool, event_id).await;
    assert_eq!(content.title, "Out of office");
    assert_eq!(content.sequence, 1);
    assert!(content.reminders_use_default);
    assert_eq!(
        scheduled_firings(&pool, event_id).await,
        Vec::new(),
        "an out-of-office event following calendar defaults schedules nothing"
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn mutation_target_addresses_the_primary_by_default_and_the_named_calendar_copy(
    pool: PgPool,
) {
    let member = "macro|[email protected]";
    insert_user(&pool, member).await;
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_calendar_id) = grant_and_provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "reader", &[]).await;
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    repo.upsert_event_fixture(member_primary_upsert(
        member,
        link_id,
        (account_id, primary_calendar_id),
        uid,
        starts_at,
    ))
    .await
    .unwrap();
    let event_id = repo
        .upsert_event_fixture(shared_copy_upsert(
            member,
            link_id,
            (account_id, shared_calendar_id),
            uid,
            starts_at,
        ))
        .await
        .unwrap();

    let default_target = repo
        .get_event_mutation_target(member, event_id, None)
        .await
        .unwrap()
        .expect("owner sees the mutation target");
    assert_eq!(default_target.calendar_id, primary_calendar_id);
    assert_eq!(default_target.provider_calendar_id, "primary");
    assert_eq!(
        default_target.observed_access_role.as_deref(),
        Some("owner")
    );
    assert!(!default_target.is_read_only);

    let copy_target = repo
        .get_event_mutation_target(member, event_id, Some(shared_calendar_id))
        .await
        .unwrap()
        .expect("the shared copy is addressable by its calendar");
    assert_eq!(copy_target.calendar_id, shared_calendar_id);
    assert_eq!(copy_target.observed_access_role.as_deref(), Some("reader"));
    assert_eq!(
        copy_target.provider_calendar_id,
        "[email protected]"
    );
    assert!(
        copy_target.is_read_only,
        "the copy's own access role decides"
    );

    let elsewhere = repo
        .get_event_mutation_target(member, event_id, Some(Uuid::now_v7()))
        .await
        .unwrap();
    assert!(
        elsewhere.is_none(),
        "a calendar without a copy of the event is not a target"
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn calendar_default_reminder_changes_follow_the_primary_calendar(pool: PgPool) {
    let member = "macro|[email protected]";
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_calendar_id) = provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "reader", &[5]).await;
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    // A regular meeting following calendar defaults on the member's primary,
    // then the shared calendar's copy with a newer stamp.
    let event_id = repo
        .upsert_event_fixture(reminder_upsert(
            member,
            link_id,
            (account_id, primary_calendar_id),
            uid,
            starts_at,
            EventReminders::default(),
        ))
        .await
        .unwrap();
    let mut copy = reminder_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at,
        EventReminders::default(),
    );
    copy.event.updated_at += Duration::hours(1);
    repo.upsert_event_fixture(copy).await.unwrap();
    assert_eq!(
        scheduled_firings(&pool, event_id).await,
        Vec::new(),
        "the primary calendar has no default reminders"
    );

    // Changing the shared calendar's defaults must not reach an event it
    // merely holds a copy of.
    insert_shared_calendar(&repo, account_id, "reader", &[15]).await;
    assert_eq!(scheduled_firings(&pool, event_id).await, Vec::new());

    // Changing the primary calendar's defaults must.
    repo.upsert_calendar_fixture(
        account_id,
        ProviderCalendar {
            provider_calendar_id: "primary".to_string(),
            name: "Primary".to_string(),
            description: None,
            time_zone: Some("UTC".to_string()),
            color: None,
            access_role: Some("owner".to_string()),
            is_primary: true,
            is_selected: true,
            default_reminders: vec![EventReminderOverride {
                method: REMINDER_METHOD_POPUP.to_string(),
                minutes: 10,
            }],
        },
    )
    .await
    .unwrap();
    assert_eq!(
        scheduled_firings(&pool, event_id).await,
        vec![(
            starts_at.to_rfc3339(),
            10,
            starts_at - Duration::minutes(10)
        )]
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn retiring_the_primary_copy_promotes_the_shared_copy(pool: PgPool) {
    let member = "macro|[email protected]";
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_calendar_id) = provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "reader", &[]).await;
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    let event_id = repo
        .upsert_event_fixture(member_primary_upsert(
            member,
            link_id,
            (account_id, primary_calendar_id),
            uid,
            starts_at,
        ))
        .await
        .unwrap();
    repo.upsert_event_fixture(shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at,
    ))
    .await
    .unwrap();

    let retired = repo
        .remove_provider_source(account_id, primary_calendar_id, &format!("provider-{uid}"))
        .await
        .unwrap();
    assert_eq!(
        retired,
        vec![RetiredCalendarEvent {
            event_id,
            owner_id: member.to_string(),
            deleted: false,
            team_ooo_changed: true,
        }]
    );
    let content = entity_content(&pool, event_id).await;
    assert_eq!(
        (
            content.title.as_str(),
            content.event_type.as_str(),
            content.is_read_only
        ),
        ("[teo] OOO", "default", true),
        "the surviving copy becomes canonical"
    );
    assert_eq!(
        scheduled_firings(&pool, event_id).await,
        vec![(starts_at.to_rfc3339(), 5, starts_at - Duration::minutes(5))],
        "the schedule follows the surviving copy's reminders"
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_user_edit_through_the_shared_copy_moves_the_schedule_but_keeps_primary_content(
    pool: PgPool,
) {
    let member = "macro|[email protected]";
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_calendar_id) = provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "writer", &[]).await;
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    let event_id = repo
        .upsert_event_fixture(member_primary_upsert(
            member,
            link_id,
            (account_id, primary_calendar_id),
            uid,
            starts_at,
        ))
        .await
        .unwrap();
    repo.upsert_event_fixture(shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at,
    ))
    .await
    .unwrap();

    // The member drags the event while viewing the shared calendar. Google
    // confirms the move on that copy; the entity's schedule must follow it
    // even though the copy is not canonical.
    let moved_start = starts_at + Duration::hours(2);
    let mut echo = shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        moved_start,
    );
    echo.event.sequence = 1;
    echo.event.updated_at = starts_at + Duration::hours(1);
    let outcome = repo
        .upsert_event(CalendarEventWrite::UserMutation(echo))
        .await
        .unwrap();
    assert_eq!(outcome.change, CalendarEventChange::Updated);

    let content = entity_content(&pool, event_id).await;
    assert_primary_content(&content);
    let rows = repo
        .list_occurrences(member, range_around(moved_start), None, 10)
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();
    let (event, occurrence) = rows.first().expect("the moved occurrence lists");
    assert_eq!(rows.len(), 1);
    assert_eq!(
        occurrence.time,
        EventTime::Timed {
            starts_at: moved_start,
            ends_at: moved_start + Duration::hours(1),
            time_zone: None,
        }
    );
    assert_eq!(
        event.title, "OOO",
        "content still comes from the primary copy"
    );
    assert_eq!(
        scheduled_firings(&pool, event_id).await,
        vec![(
            moved_start.to_rfc3339(),
            30,
            moved_start - Duration::minutes(30)
        )],
        "reminders follow the new time with the primary copy's offset"
    );

    // A plain sync of the shared copy with the same schedule stays a
    // non-canonical write and leaves everything alone.
    let mut resync = shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        moved_start,
    );
    resync.event.sequence = 1;
    resync.event.updated_at = starts_at + Duration::hours(2);
    repo.upsert_event_fixture(resync).await.unwrap();
    assert_primary_content(&entity_content(&pool, event_id).await);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn canonical_selection_skips_copies_on_deleted_calendars(pool: PgPool) {
    let member = "macro|[email protected]";
    insert_user(&pool, member).await;
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_calendar_id) = grant_and_provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "reader", &[]).await;
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    let event_id = repo
        .upsert_event_fixture(member_primary_upsert(
            member,
            link_id,
            (account_id, primary_calendar_id),
            uid,
            starts_at,
        ))
        .await
        .unwrap();
    repo.upsert_event_fixture(shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at,
    ))
    .await
    .unwrap();

    // The primary calendar disappears while its source row still exists.
    // Reads already ignore it, so writes must too: the next shared-copy sync
    // promotes the live copy instead of freezing the entity on the dead one.
    sqlx::query!(
        "UPDATE calendars SET is_deleted = true WHERE id = $1",
        primary_calendar_id,
    )
    .execute(&pool)
    .await
    .unwrap();
    let mut resync = shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at,
    );
    resync.event.updated_at += Duration::hours(1);
    repo.upsert_event_fixture(resync).await.unwrap();

    let content = entity_content(&pool, event_id).await;
    assert_eq!(
        (
            content.title.as_str(),
            content.event_type.as_str(),
            content.is_read_only
        ),
        ("[teo] OOO", "default", true)
    );
    let target = repo
        .get_event_mutation_target(member, event_id, None)
        .await
        .unwrap()
        .expect("the live copy is the mutation target");
    assert_eq!(target.calendar_id, shared_calendar_id);
    let rows = repo
        .list_occurrences(member, range_around(starts_at), None, 10)
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();
    assert_eq!(rows[0].0.calendar_id, Some(shared_calendar_id));
    assert_eq!(
        rows[0]
            .0
            .sources
            .iter()
            .map(|source| source.calendar_id)
            .collect::<Vec<_>>(),
        vec![shared_calendar_id]
    );

    // Retiring the shared copy now finds no live source and removes the entity
    // rather than resurrecting the deleted calendar's copy.
    let retired = repo
        .remove_provider_source(account_id, shared_calendar_id, &format!("provider-{uid}"))
        .await
        .unwrap();
    assert_eq!(
        retired,
        vec![RetiredCalendarEvent {
            event_id,
            owner_id: member.to_string(),
            deleted: true,
            team_ooo_changed: false,
        }]
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn an_older_canonical_resync_cannot_undo_a_user_edit_made_through_another_copy(pool: PgPool) {
    let member = "macro|[email protected]";
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_calendar_id) = provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "writer", &[]).await;
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    let event_id = repo
        .upsert_event_fixture(member_primary_upsert(
            member,
            link_id,
            (account_id, primary_calendar_id),
            uid,
            starts_at,
        ))
        .await
        .unwrap();
    repo.upsert_event_fixture(shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at,
    ))
    .await
    .unwrap();

    // The member moves the event through the shared calendar; Google stamps
    // that copy an hour after the primary's last update.
    let moved_start = starts_at + Duration::hours(2);
    let mut echo = shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        moved_start,
    );
    echo.event.sequence = 1;
    echo.event.updated_at = starts_at + Duration::hours(1);
    repo.upsert_event(CalendarEventWrite::UserMutation(echo))
        .await
        .unwrap();

    // The primary copy syncs a change that is older than the edit and keeps
    // its own sequence — a description Google recorded before the move. Its
    // content lands, its stale schedule does not.
    let mut stale_primary = member_primary_upsert(
        member,
        link_id,
        (account_id, primary_calendar_id),
        uid,
        starts_at,
    );
    stale_primary.event.description = Some("Dentist, then lunch".to_string());
    stale_primary.event.updated_at = starts_at + Duration::minutes(30);
    repo.upsert_event_fixture(stale_primary).await.unwrap();

    let rows = repo
        .list_occurrences(member, range_around(moved_start), None, 10)
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();
    let (event, occurrence) = rows.first().expect("the moved occurrence lists");
    assert_eq!(rows.len(), 1);
    assert_eq!(
        occurrence.time,
        EventTime::Timed {
            starts_at: moved_start,
            ends_at: moved_start + Duration::hours(1),
            time_zone: None,
        },
        "an older canonical state leaves the user's move in place"
    );
    assert_eq!(event.description.as_deref(), Some("Dentist, then lunch"));
    assert_eq!(
        scheduled_firings(&pool, event_id).await,
        vec![(
            moved_start.to_rfc3339(),
            30,
            moved_start - Duration::minutes(30)
        )]
    );

    // The primary copy itself changes, advancing its sequence. Its provider
    // stamp is still older than the edit's, but a change to the canonical copy
    // owns the schedule again regardless of the clock.
    let mut fresh_primary = member_primary_upsert(
        member,
        link_id,
        (account_id, primary_calendar_id),
        uid,
        moved_start + Duration::hours(1),
    );
    fresh_primary.event.sequence = 1;
    fresh_primary.event.updated_at = starts_at + Duration::minutes(45);
    repo.upsert_event_fixture(fresh_primary).await.unwrap();
    let rows = repo
        .list_occurrences(member, range_around(moved_start), None, 10)
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();
    assert_eq!(
        rows[0].1.time,
        EventTime::Timed {
            starts_at: moved_start + Duration::hours(1),
            ends_at: moved_start + Duration::hours(2),
            time_zone: None,
        }
    );
}

/// The member's own copy with the guests and conference only the primary
/// carries, so a copy that drops either is caught.
fn member_primary_meeting(
    owner_id: &str,
    link_id: Uuid,
    provider: (Uuid, Uuid),
    uid: &str,
    starts_at: DateTime<Utc>,
) -> CalendarEventUpsert {
    let mut upsert = member_primary_upsert(owner_id, link_id, provider, uid, starts_at);
    upsert.event.event_type = EventType::Default;
    upsert.event.attendees = vec![reminder_attendee("[email protected]", false, false)];
    upsert.event.conference_url = Some("https://meet.google.com/abc-defg-hij".to_string());
    upsert.event.conference_provider = Some(ConferenceProvider::GoogleMeet);
    upsert
}

async fn listed_event(
    repo: &PgCalendarRepository,
    member: &str,
    around: DateTime<Utc>,
) -> (CalendarEvent, CalendarOccurrence) {
    let rows = repo
        .list_occurrences(member, range_around(around), None, 10)
        .await
        .unwrap()
        .into_iter()
        .map(OccurrenceListing::into_occurrence_event)
        .collect::<Vec<_>>();
    assert_eq!(rows.len(), 1, "one occurrence lists");
    rows.into_iter().next().unwrap()
}

fn timed(starts_at: DateTime<Utc>) -> EventTime {
    EventTime::Timed {
        starts_at,
        ends_at: starts_at + Duration::hours(1),
        time_zone: None,
    }
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_primary_copy_arriving_after_a_shared_copy_takes_the_schedule(pool: PgPool) {
    let member = "macro|[email protected]";
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_calendar_id) = provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "reader", &[]).await;
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    // The shared copy seeds the entity with a flattened schedule and a newer
    // provider stamp. The primary copy arrives afterwards with an older stamp,
    // guests, a conference, and a different time.
    let event_id = repo
        .upsert_event_fixture(shared_copy_upsert(
            member,
            link_id,
            (account_id, shared_calendar_id),
            uid,
            starts_at,
        ))
        .await
        .unwrap();
    let primary_start = starts_at + Duration::hours(1);
    repo.upsert_event_fixture(member_primary_meeting(
        member,
        link_id,
        (account_id, primary_calendar_id),
        uid,
        primary_start,
    ))
    .await
    .unwrap();

    let (event, occurrence) = listed_event(&repo, member, primary_start).await;
    assert_eq!(event.id, event_id);
    assert_eq!(occurrence.time, timed(primary_start));
    assert_eq!(event.attendees.len(), 1);
    assert_eq!(
        event.conference_url.as_deref(),
        Some("https://meet.google.com/abc-defg-hij")
    );
    assert_eq!(event.calendar_id, Some(primary_calendar_id));
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_user_edit_through_the_shared_copy_keeps_the_primary_guests_and_conference(pool: PgPool) {
    let member = "macro|[email protected]";
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_calendar_id) = provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "writer", &[]).await;
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    repo.upsert_event_fixture(member_primary_meeting(
        member,
        link_id,
        (account_id, primary_calendar_id),
        uid,
        starts_at,
    ))
    .await
    .unwrap();
    repo.upsert_event_fixture(shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at,
    ))
    .await
    .unwrap();

    let moved_start = starts_at + Duration::hours(2);
    let mut echo = shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        moved_start,
    );
    echo.event.sequence = 1;
    echo.event.updated_at = starts_at + Duration::hours(1);
    repo.upsert_event(CalendarEventWrite::UserMutation(echo))
        .await
        .unwrap();

    let (event, occurrence) = listed_event(&repo, member, moved_start).await;
    assert_eq!(occurrence.time, timed(moved_start));
    assert_eq!(
        event.attendees.len(),
        1,
        "the copy's empty guest list does not replace the primary's"
    );
    assert_eq!(
        event.conference_url.as_deref(),
        Some("https://meet.google.com/abc-defg-hij")
    );
    assert_eq!(event.title, "OOO");
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn echoes_landing_out_of_order_keep_the_newer_schedule(pool: PgPool) {
    let member = "macro|[email protected]";
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_calendar_id) = provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "writer", &[]).await;
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    repo.upsert_event_fixture(member_primary_upsert(
        member,
        link_id,
        (account_id, primary_calendar_id),
        uid,
        starts_at,
    ))
    .await
    .unwrap();
    repo.upsert_event_fixture(shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at,
    ))
    .await
    .unwrap();

    // Two edits race: one through the shared copy stamped T1, one through the
    // primary stamped T2. Google applied them in that order, but the primary's
    // echo persists first.
    let later_start = starts_at + Duration::hours(3);
    let mut primary_echo = member_primary_upsert(
        member,
        link_id,
        (account_id, primary_calendar_id),
        uid,
        later_start,
    );
    primary_echo.event.sequence = 1;
    primary_echo.event.updated_at = starts_at + Duration::hours(2);
    repo.upsert_event(CalendarEventWrite::UserMutation(primary_echo))
        .await
        .unwrap();
    let mut shared_echo = shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at + Duration::hours(2),
    );
    shared_echo.event.sequence = 1;
    shared_echo.event.updated_at = starts_at + Duration::hours(1);
    repo.upsert_event(CalendarEventWrite::UserMutation(shared_echo))
        .await
        .unwrap();

    let (_, occurrence) = listed_event(&repo, member, later_start).await;
    assert_eq!(
        occurrence.time,
        timed(later_start),
        "the older echo does not roll the schedule back"
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn the_copy_that_wrote_the_schedule_keeps_writing_it(pool: PgPool) {
    let member = "macro|[email protected]";
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_calendar_id) = provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "writer", &[]).await;
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    let event_id = repo
        .upsert_event_fixture(member_primary_upsert(
            member,
            link_id,
            (account_id, primary_calendar_id),
            uid,
            starts_at,
        ))
        .await
        .unwrap();
    repo.upsert_event_fixture(shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at,
    ))
    .await
    .unwrap();

    // The member moves the shared copy, then the script that maintains that
    // calendar moves it back. The shared copy wrote the schedule, so its own
    // later sync follows through, while the primary's content stays.
    let moved_start = starts_at + Duration::hours(2);
    let mut echo = shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        moved_start,
    );
    echo.event.sequence = 1;
    echo.event.updated_at = starts_at + Duration::hours(1);
    repo.upsert_event(CalendarEventWrite::UserMutation(echo))
        .await
        .unwrap();
    let mut reset = shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at,
    );
    reset.event.sequence = 1;
    reset.event.updated_at = starts_at + Duration::hours(2);
    repo.upsert_event_fixture(reset).await.unwrap();

    let (event, occurrence) = listed_event(&repo, member, starts_at).await;
    assert_eq!(event.id, event_id);
    assert_eq!(occurrence.time, timed(starts_at));
    assert_primary_content(&entity_content(&pool, event_id).await);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_writable_copy_outranks_a_fresher_reader_copy_without_a_primary(pool: PgPool) {
    let member = "macro|[email protected]";
    insert_user(&pool, member).await;
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, _primary_calendar_id) = grant_and_provider_ids(&repo, link_id).await;
    let secondary_calendar_id = repo
        .upsert_calendar_fixture(
            account_id,
            ProviderCalendar {
                provider_calendar_id: "secondary".to_string(),
                name: "Projects".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("owner".to_string()),
                is_primary: false,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap();
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "reader", &[]).await;
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    let mut own_copy = reminder_upsert(
        member,
        link_id,
        (account_id, secondary_calendar_id),
        uid,
        starts_at,
        EventReminders::default(),
    );
    own_copy.event.title = "Planning".to_string();
    own_copy.event.is_read_only = false;
    let event_id = repo.upsert_event_fixture(own_copy).await.unwrap();
    let mut reader_copy = shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at,
    );
    reader_copy.event.title = "[teo] Planning".to_string();
    repo.upsert_event_fixture(reader_copy).await.unwrap();

    let content = entity_content(&pool, event_id).await;
    assert_eq!(content.title, "Planning");
    assert!(!content.is_read_only, "the writable copy is canonical");
    let target = repo
        .get_event_mutation_target(member, event_id, None)
        .await
        .unwrap()
        .expect("owner sees the mutation target");
    assert_eq!(target.calendar_id, secondary_calendar_id);
    assert!(!target.is_read_only);
    let (event, _) = listed_event(&repo, member, starts_at).await;
    assert_eq!(event.calendar_id, Some(secondary_calendar_id));
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_writable_copy_outranks_a_fresher_copy_whose_calendar_role_is_unknown(pool: PgPool) {
    let member = "macro|[email protected]";
    insert_user(&pool, member).await;
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, _primary_calendar_id) = grant_and_provider_ids(&repo, link_id).await;
    let secondary_calendar_id = repo
        .upsert_calendar_fixture(
            account_id,
            ProviderCalendar {
                provider_calendar_id: "secondary".to_string(),
                name: "Projects".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("owner".to_string()),
                is_primary: false,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap();
    let unknown_role_calendar_id = repo
        .upsert_calendar_fixture(
            account_id,
            ProviderCalendar {
                provider_calendar_id: "[email protected]".to_string(),
                name: "Macro Vacation".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: None,
                is_primary: false,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap();
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    let mut own_copy = reminder_upsert(
        member,
        link_id,
        (account_id, secondary_calendar_id),
        uid,
        starts_at,
        EventReminders::default(),
    );
    own_copy.event.title = "Planning".to_string();
    own_copy.event.is_read_only = false;
    let event_id = repo.upsert_event_fixture(own_copy).await.unwrap();
    let mut unknown_role_copy = shared_copy_upsert(
        member,
        link_id,
        (account_id, unknown_role_calendar_id),
        uid,
        starts_at,
    );
    unknown_role_copy.event.title = "[teo] Planning".to_string();
    repo.upsert_event_fixture(unknown_role_copy).await.unwrap();

    let content = entity_content(&pool, event_id).await;
    assert_eq!(content.title, "Planning");
    assert!(!content.is_read_only, "the writable copy is canonical");
    let target = repo
        .get_event_mutation_target(member, event_id, None)
        .await
        .unwrap()
        .expect("owner sees the mutation target");
    assert_eq!(target.calendar_id, secondary_calendar_id);
    let (event, _) = listed_event(&repo, member, starts_at).await;
    assert_eq!(event.calendar_id, Some(secondary_calendar_id));
    assert_eq!(
        event
            .sources
            .iter()
            .map(|copy| copy.calendar_id)
            .collect::<Vec<_>>(),
        vec![secondary_calendar_id, unknown_role_calendar_id]
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_calendar_role_change_moves_the_entity_to_the_new_canonical_copy_on_reconcile(
    pool: PgPool,
) {
    let member = "macro|[email protected]";
    insert_user(&pool, member).await;
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let google_job = enabled
        .jobs
        .into_iter()
        .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
        .unwrap();
    let key = CalendarBackfillJobKey {
        job_id: google_job.id,
        email_link_id: link_id,
    };
    let CalendarBackfillClaim::Claimed { lease_token, .. } =
        repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("Google job should be claimable");
    };
    let (account_id, primary_calendar_id) = provider_ids(&repo, link_id).await;
    let secondary_calendar_id = repo
        .upsert_calendar_fixture(
            account_id,
            ProviderCalendar {
                provider_calendar_id: "secondary".to_string(),
                name: "Projects".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("owner".to_string()),
                is_primary: false,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap();
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "reader", &[]).await;
    let dropped_calendar_id = repo
        .upsert_calendar_fixture(
            account_id,
            ProviderCalendar {
                provider_calendar_id: "[email protected]".to_string(),
                name: "Team".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("reader".to_string()),
                is_primary: false,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap();
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    let mut own_copy = reminder_upsert(
        member,
        link_id,
        (account_id, secondary_calendar_id),
        uid,
        starts_at,
        EventReminders::default(),
    );
    own_copy.event.title = "Planning".to_string();
    own_copy.event.is_read_only = false;
    let event_id = repo.upsert_event_fixture(own_copy).await.unwrap();
    let mut reader_copy = shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at,
    );
    reader_copy.event.title = "[teo] Planning".to_string();
    repo.upsert_event_fixture(reader_copy).await.unwrap();
    repo.upsert_event_fixture(shared_copy_upsert(
        member,
        link_id,
        (account_id, dropped_calendar_id),
        uid,
        starts_at,
    ))
    .await
    .unwrap();
    assert_eq!(entity_content(&pool, event_id).await.title, "Planning");

    sqlx::query!(
        "UPDATE calendars SET access_role = 'writer' WHERE id = $1",
        shared_calendar_id,
    )
    .execute(&pool)
    .await
    .unwrap();
    let announced = repo
        .reconcile_google_calendar_list(
            key,
            lease_token,
            account_id,
            vec![
                primary_calendar_id,
                secondary_calendar_id,
                shared_calendar_id,
            ],
        )
        .await
        .unwrap();
    assert_eq!(
        announced
            .iter()
            .map(|outcome| (outcome.event_id, outcome.deleted))
            .collect::<Vec<_>>(),
        vec![(event_id, false)],
        "losing a copy and unlocking another announce the event once"
    );

    let content = entity_content(&pool, event_id).await;
    assert_eq!(
        content.title, "[teo] Planning",
        "the fresher copy is canonical once its calendar is writable"
    );
    assert!(
        !content.is_read_only,
        "the copy's read-only flag follows the calendar's new role"
    );
    let mirrors_canonical = sqlx::query_scalar!(
        r#"
        SELECT content_source_id = calendar_event_canonical_source_id(id) AS "mirrors!"
        FROM calendar_events
        WHERE id = $1
        "#,
        event_id,
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert!(mirrors_canonical);
    let target = repo
        .get_event_mutation_target(member, event_id, None)
        .await
        .unwrap()
        .expect("owner sees the mutation target");
    assert_eq!(target.calendar_id, shared_calendar_id);
    assert!(!target.is_read_only);
    let (event, _) = listed_event(&repo, member, starts_at).await;
    assert_eq!(event.calendar_id, Some(shared_calendar_id));
    assert_eq!(
        event
            .sources
            .iter()
            .map(|copy| (copy.calendar_id, copy.is_read_only))
            .collect::<Vec<_>>(),
        vec![(shared_calendar_id, false), (secondary_calendar_id, false)]
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_calendar_role_change_refreshes_the_read_only_flag_of_its_only_copy(pool: PgPool) {
    let member = "macro|[email protected]";
    insert_user(&pool, member).await;
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let enabled = repo
        .apply_google_grant(
            link_id,
            complete_grant(),
            CalendarGrantIntent::CalendarRequested,
        )
        .await
        .unwrap();
    let google_job = enabled
        .jobs
        .into_iter()
        .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
        .unwrap();
    let key = CalendarBackfillJobKey {
        job_id: google_job.id,
        email_link_id: link_id,
    };
    let CalendarBackfillClaim::Claimed { lease_token, .. } =
        repo.claim_google_backfill(key).await.unwrap()
    else {
        panic!("Google job should be claimable");
    };
    let (account_id, primary_calendar_id) = provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "reader", &[]).await;
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);
    let event_id = repo
        .upsert_event_fixture(shared_copy_upsert(
            member,
            link_id,
            (account_id, shared_calendar_id),
            "[email protected]",
            starts_at,
        ))
        .await
        .unwrap();
    assert!(entity_content(&pool, event_id).await.is_read_only);

    sqlx::query!(
        "UPDATE calendars SET access_role = 'writer' WHERE id = $1",
        shared_calendar_id,
    )
    .execute(&pool)
    .await
    .unwrap();
    let announced = repo
        .reconcile_google_calendar_list(
            key,
            lease_token,
            account_id,
            vec![primary_calendar_id, shared_calendar_id],
        )
        .await
        .unwrap();

    assert!(!entity_content(&pool, event_id).await.is_read_only);
    assert_eq!(
        announced
            .iter()
            .map(|outcome| (outcome.event_id, outcome.deleted))
            .collect::<Vec<_>>(),
        vec![(event_id, false)],
        "the unlocked event announces itself so search and clients refresh"
    );
    let target = repo
        .get_event_mutation_target(member, event_id, None)
        .await
        .unwrap()
        .expect("owner sees the mutation target");
    assert_eq!(target.calendar_id, shared_calendar_id);
    assert!(!target.is_read_only);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn retiring_an_unrelated_copy_keeps_a_fresher_schedule_written_through_another(pool: PgPool) {
    let member = "macro|[email protected]";
    let link_id = insert_link(&pool, member).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_calendar_id) = provider_ids(&repo, link_id).await;
    let shared_calendar_id = insert_shared_calendar(&repo, account_id, "writer", &[]).await;
    let third_calendar_id = repo
        .upsert_calendar_fixture(
            account_id,
            ProviderCalendar {
                provider_calendar_id: "[email protected]".to_string(),
                name: "Team".to_string(),
                description: None,
                time_zone: Some("UTC".to_string()),
                color: None,
                access_role: Some("reader".to_string()),
                is_primary: false,
                is_selected: true,
                default_reminders: Vec::new(),
            },
        )
        .await
        .unwrap();
    let uid = "[email protected]";
    let starts_at = (Utc::now() + Duration::hours(3)).trunc_subsecs(0);

    let event_id = repo
        .upsert_event_fixture(member_primary_upsert(
            member,
            link_id,
            (account_id, primary_calendar_id),
            uid,
            starts_at,
        ))
        .await
        .unwrap();
    repo.upsert_event_fixture(shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        starts_at,
    ))
    .await
    .unwrap();
    let mut third = shared_copy_upsert(
        member,
        link_id,
        (account_id, third_calendar_id),
        uid,
        starts_at,
    );
    third.source = CalendarEventSource::Google(ProviderEventSource {
        automatic_decline: None,
        binding: None,
        observed_access_role: Some("reader".to_owned()),
        email_link_id: link_id,
        account_id,
        calendar_id: third_calendar_id,
        provider_event_id: format!("third-{uid}"),
        provider_recurring_event_id: None,
        provider_etag: None,
        raw_payload: serde_json::json!({}),
    });
    repo.upsert_event_fixture(third).await.unwrap();

    let moved_start = starts_at + Duration::hours(2);
    let mut echo = shared_copy_upsert(
        member,
        link_id,
        (account_id, shared_calendar_id),
        uid,
        moved_start,
    );
    echo.event.sequence = 1;
    echo.event.updated_at = starts_at + Duration::hours(1);
    repo.upsert_event(CalendarEventWrite::UserMutation(echo))
        .await
        .unwrap();

    // Retiring the third copy re-derives the entity from the primary. The
    // shared copy that wrote the schedule survives with a fresher stamp, so
    // the move stays while the content is the primary's.
    repo.remove_provider_source(account_id, third_calendar_id, &format!("third-{uid}"))
        .await
        .unwrap();
    let (event, occurrence) = listed_event(&repo, member, moved_start).await;
    assert_eq!(event.id, event_id);
    assert_eq!(occurrence.time, timed(moved_start));
    assert_primary_content(&entity_content(&pool, event_id).await);
}

mod changes;
mod channel_grants;
mod details;
mod event_labels;
mod export;
mod guest_policy;
mod invitations;
mod join_requests;
mod meeting_copies;
mod meeting_uid;
mod mention_join_link;
mod organizer_calendar;
mod source_access;

mod replacement;

async fn link_email(pool: &PgPool, link_id: Uuid) -> String {
    sqlx::query_scalar!(
        r#"SELECT email_address::text AS "email_address!" FROM email_links WHERE id = $1"#,
        link_id,
    )
    .fetch_one(pool)
    .await
    .unwrap()
}

fn attendee(email: &str, is_organizer: bool) -> CalendarAttendee {
    CalendarAttendee {
        email: email.to_string(),
        display_name: None,
        response_status: AttendeeResponseStatus::Accepted,
        is_organizer,
        is_optional: false,
        is_self: false,
        comment: None,
    }
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn mention_previews_say_how_the_viewer_holds_the_meeting(pool: PgPool) {
    let organizer_id = "macro|[email protected]";
    let guest_id = "macro|[email protected]";
    let holder_id = "macro|[email protected]";
    let member_id = "macro|[email protected]";
    let organizer_link = insert_link(&pool, organizer_id).await;
    let guest_link = insert_link(&pool, guest_id).await;
    let holder_link = insert_link(&pool, holder_id).await;
    let organizer_email = link_email(&pool, organizer_link).await;
    let guest_email = link_email(&pool, guest_link).await;
    let holder_email = link_email(&pool, holder_link).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let uid = "[email protected]";

    let copy = |owner_id: &str, link_id, provider, attendees: Vec<CalendarAttendee>| {
        let mut upsert = timed_upsert(owner_id, link_id, provider, uid, "Planning", 1);
        upsert.event.organizer_email = Some(organizer_email.clone());
        upsert.event.attendees = attendees;
        upsert
    };
    let organizer_copy = copy(
        organizer_id,
        organizer_link,
        provider_ids(&repo, organizer_link).await,
        vec![
            attendee(&organizer_email, true),
            attendee(&guest_email, false),
        ],
    );
    let shared_event_id = organizer_copy.event.id;
    repo.upsert_event_fixture(organizer_copy).await.unwrap();
    let guest_copy = copy(
        guest_id,
        guest_link,
        provider_ids(&repo, guest_link).await,
        vec![
            attendee(&organizer_email, true),
            attendee(&guest_email, false),
        ],
    );
    repo.upsert_event_fixture(guest_copy).await.unwrap();
    // A snapshot lists its holder as the only guest so Google accepts it.
    let mut snapshot = copy(
        holder_id,
        holder_link,
        provider_ids(&repo, holder_link).await,
        vec![attendee(&holder_email, false)],
    );
    snapshot.event.is_snapshot = true;
    repo.upsert_event_fixture(snapshot).await.unwrap();
    let channel_id = insert_channel(
        &pool,
        organizer_id,
        &[organizer_id, guest_id, holder_id, member_id],
    )
    .await;
    share_with_channel(&pool, shared_event_id, channel_id).await;

    let now = Utc.with_ymd_and_hms(2026, 7, 23, 0, 0, 0).unwrap();
    let held_as = |viewer: &'static str| {
        let repo = &repo;
        async move {
            let previews = repo
                .mention_previews(
                    viewer,
                    vec![CalendarMentionRequestItem {
                        event_id: shared_event_id,
                        occurrence_key: None,
                    }],
                    now,
                )
                .await
                .unwrap();
            let CalendarMentionPreview::Accessible(event) = &previews[0] else {
                panic!("{viewer} preview should be accessible: {:?}", previews[0]);
            };
            event.viewer_copy
        }
    };
    assert_eq!(
        held_as(organizer_id).await,
        Some(CalendarMentionViewerCopy::Event)
    );
    assert_eq!(
        held_as(guest_id).await,
        Some(CalendarMentionViewerCopy::Invitation)
    );
    assert_eq!(
        held_as(holder_id).await,
        Some(CalendarMentionViewerCopy::Snapshot)
    );
    assert_eq!(held_as(member_id).await, None);
}