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.

9.5 KB
Raw Blame
use super::*;
use crate::domain::ics::CalendarEventExportAccess;

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn event_export_resolves_own_copies_before_channel_shares(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]",
        "Offsite",
        1,
    );
    // Longer than the mention preview truncates to: the export carries it whole.
    let description = "a".repeat(MENTION_PREVIEW_DESCRIPTION_MAX_CHARS as usize + 10);
    author_copy.event.description = Some(description.clone());
    let recurrence_lines = author_copy.event.recurrence_lines.clone();
    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]",
        "Offsite, my copy",
        1,
    );
    repo.upsert_event_fixture(attendee_copy).await.unwrap();
    let mut private_copy = timed_upsert(
        author_id,
        author_link,
        author_provider,
        "[email protected]",
        "Private",
        1,
    );
    private_copy.event.visibility = EventVisibility::Private;
    let private_event_id = private_copy.event.id;
    repo.upsert_event_fixture(private_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 shared = repo
        .event_export(member_id, shared_event_id)
        .await
        .unwrap()
        .expect("a channel member sees the shared event");
    assert_eq!(shared.access, CalendarEventExportAccess::ChannelShared);
    assert_eq!(shared.owner_id, author_id);
    assert_eq!(shared.event_type, EventType::Default);
    assert_eq!(shared.ical_uid, "[email protected]");
    assert_eq!(shared.title, "Offsite");
    assert_eq!(shared.description.as_deref(), Some(description.as_str()));
    assert_eq!(shared.recurrence_lines, recurrence_lines);
    assert_eq!(
        shared.organizer_email.as_deref(),
        Some("[email protected]")
    );

    // Anyone holding a projection of the meeting exports their own copy.
    let own = repo
        .event_export(attendee_id, shared_event_id)
        .await
        .unwrap()
        .expect("an attendee sees their own copy");
    assert_eq!(own.access, CalendarEventExportAccess::OwnCopy);
    assert_eq!(own.owner_id, attendee_id);
    assert_eq!(own.title, "Offsite, my copy");
    let authored = repo
        .event_export(author_id, shared_event_id)
        .await
        .unwrap()
        .expect("the author sees their own copy");
    assert_eq!(authored.access, CalendarEventExportAccess::OwnCopy);
    assert_eq!(authored.title, "Offsite");

    // Private events stay hidden despite the grant; people who left the
    // channel and outsiders see nothing.
    for (requester, event_id) in [
        (member_id, private_event_id),
        (former_id, shared_event_id),
        (stranger_id, shared_event_id),
    ] {
        assert_eq!(
            repo.event_export(requester, event_id).await.unwrap(),
            None,
            "{requester}"
        );
    }

    sqlx::query!(
        "UPDATE calendar_events SET status = 'cancelled' WHERE id = $1",
        shared_event_id,
    )
    .execute(&pool)
    .await
    .unwrap();
    assert_eq!(
        repo.event_export(member_id, shared_event_id).await.unwrap(),
        None
    );
}

fn with_raw_payload(
    mut upsert: CalendarEventUpsert,
    raw: serde_json::Value,
) -> CalendarEventUpsert {
    if let CalendarEventSource::Google(source) = &mut upsert.source {
        source.raw_payload = raw;
    }
    upsert
}

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

    let mut outside = with_raw_payload(
        timed_upsert(
            owner_id,
            link,
            provider,
            "040000008200E000-outside",
            "Outside",
            1,
        ),
        serde_json::json!({ "privateCopy": true, "guestsCanInviteOthers": false }),
    );
    outside.event.conference_url = Some("https://teams.microsoft.com/l/meetup-join/abc".into());
    outside.event.conference_provider = Some(ConferenceProvider::MicrosoftTeams);
    let outside_id = outside.event.id;
    repo.upsert_event_fixture(outside).await.unwrap();

    let invited = with_raw_payload(
        timed_upsert(owner_id, link, provider, "[email protected]", "Invited", 1),
        serde_json::json!({ "guestsCanInviteOthers": false }),
    );
    let invited_id = invited.event.id;
    repo.upsert_event_fixture(invited).await.unwrap();

    let mut snapshot = with_raw_payload(
        timed_upsert(
            owner_id,
            link,
            provider,
            "[email protected]",
            "Snapshot",
            1,
        ),
        serde_json::json!({ "privateCopy": true }),
    );
    snapshot.event.is_snapshot = true;
    let snapshot_id = snapshot.event.id;
    repo.upsert_event_fixture(snapshot).await.unwrap();

    let outside = repo
        .forwarded_copy(owner_id, outside_id)
        .await
        .unwrap()
        .expect("the guest owns the copy");
    assert!(outside.google_private_copy);
    assert!(!outside.is_snapshot);
    assert_eq!(
        outside.join_link,
        Some(crate::domain::ics::JoinLink {
            url: "https://teams.microsoft.com/l/meetup-join/abc".to_string(),
            provider: Some(ConferenceProvider::MicrosoftTeams),
        })
    );

    let invited = repo
        .forwarded_copy(owner_id, invited_id)
        .await
        .unwrap()
        .expect("the guest owns the copy");
    assert!(!invited.google_private_copy);
    assert_eq!(invited.join_link, None);

    let snapshot = repo
        .forwarded_copy(owner_id, snapshot_id)
        .await
        .unwrap()
        .expect("the guest owns the copy");
    assert!(snapshot.google_private_copy);
    assert!(snapshot.is_snapshot);

    assert_eq!(
        repo.forwarded_copy("macro|[email protected]", outside_id)
            .await
            .unwrap(),
        None
    );
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_copy_is_private_only_when_every_calendar_holding_it_says_so(pool: PgPool) {
    let owner_id = "macro|[email protected]";
    let link = insert_link(&pool, owner_id).await;
    let repo = PgCalendarRepository::new(pool.clone());
    let (account_id, primary_id) = provider_ids(&repo, link).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();
    let primary = with_raw_payload(
        timed_upsert(
            owner_id,
            link,
            (account_id, primary_id),
            "040000008200E000-two-calendars",
            "Outside",
            1,
        ),
        serde_json::json!({ "privateCopy": true }),
    );
    let event_id = primary.event.id;
    repo.upsert_event_fixture(primary).await.unwrap();
    let mut team = with_raw_payload(
        timed_upsert(
            owner_id,
            link,
            (account_id, team_id),
            "040000008200E000-two-calendars",
            "Outside",
            1,
        ),
        serde_json::json!({}),
    );
    team.event.id = event_id;
    if let CalendarEventSource::Google(source) = &mut team.source {
        source.provider_event_id = "team-copy".to_string();
    }
    repo.upsert_event_fixture(team).await.unwrap();
    let sources = sqlx::query_scalar!(
        "SELECT count(*) FROM calendar_event_sources WHERE event_id = $1",
        event_id
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(sources, Some(2), "both calendars hold the one entity");

    let forwarded = repo
        .forwarded_copy(owner_id, event_id)
        .await
        .unwrap()
        .expect("the owner holds the copy");

    assert!(
        !forwarded.google_private_copy,
        "a calendar whose source lacks the flag is no evidence"
    );
}