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.

26.8 KB
Raw Blame
use super::*;
use models_properties::service::property_change_source::{GithubAccount, PropertyChangeSource};

// ---------------------------------------------------------------------------
// MacroTaskId::from_short_uuid
// ---------------------------------------------------------------------------

#[test]
fn from_short_uuid_valid() {
    let task_id = MacroTaskId::from_short_uuid("2BuyvtY3ae").unwrap();
    assert_eq!(task_id.short_uuid, "2BuyvtY3ae");
}

#[test]
fn from_short_uuid_rejects_empty() {
    assert!(MacroTaskId::from_short_uuid("").is_none());
}

#[test]
fn from_short_uuid_rejects_invalid_chars() {
    // 'O', 'I', 'l', '0' are not in Flickr base58
    assert!(MacroTaskId::from_short_uuid("OOOOO").is_none());
    assert!(MacroTaskId::from_short_uuid("IIIlll").is_none());
    assert!(MacroTaskId::from_short_uuid("000abc").is_none());
}

#[test]
fn from_short_uuid_rejects_too_long() {
    let long = "a".repeat(25);
    assert!(MacroTaskId::from_short_uuid(&long).is_none());
}

// ---------------------------------------------------------------------------
// MacroTaskId::from_uuid / to_uuid roundtrip
// ---------------------------------------------------------------------------

#[test]
fn roundtrip_uuid_conversion() {
    let uuid = uuid::Uuid::parse_str("0d0dc589-f301-43f1-8b11-4ab448ca4bb4").unwrap();
    let task_id = MacroTaskId::from_uuid(&uuid);
    assert_eq!(task_id.short_uuid, "2BuyvtY3aeEvHx4uG8iD51");

    let recovered = task_id.to_uuid().unwrap();
    assert_eq!(uuid, recovered);
}

#[test]
fn to_task_id_string() {
    let task_id = MacroTaskId::from_short_uuid("2BuyvtY3ae").unwrap();
    assert_eq!(task_id.to_task_id_string(), "MACRO-2BuyvtY3ae");
}

#[test]
fn display_impl() {
    let task_id = MacroTaskId::from_short_uuid("abc123").unwrap();
    assert_eq!(format!("{task_id}"), "MACRO-abc123");
}

// ---------------------------------------------------------------------------
// MacroTaskId::extract_from_text
// ---------------------------------------------------------------------------

const SHORT_UUID_A: &str = "2BuyvtY3aeEvHx4uG8iD51";
const SHORT_UUID_B: &str = "xoyQ8nrV6PNZFmpsWYMdyC";
const SHORT_UUID_C: &str = "2ZbZ7wJQfEMWyBSycKYTYr";

#[test]
fn extract_case_insensitive() {
    let text = format!(
        "fixes MACRO-{SHORT_UUID_A} and also macro-{SHORT_UUID_B} and Macro-{SHORT_UUID_C}"
    );
    let ids = MacroTaskId::extract_from_text(&text);
    assert_eq!(ids.len(), 3);
    assert_eq!(ids[0].short_uuid, SHORT_UUID_A);
    assert_eq!(ids[1].short_uuid, SHORT_UUID_B);
    assert_eq!(ids[2].short_uuid, SHORT_UUID_C);
}

#[test]
fn extract_deduplicates() {
    let text = format!("MACRO-{SHORT_UUID_A} and macro-{SHORT_UUID_A} again MACRO-{SHORT_UUID_A}");
    let ids = MacroTaskId::extract_from_text(&text);
    // Same short UUID captured, only first occurrence kept
    assert_eq!(ids.len(), 1);
    assert_eq!(ids[0].short_uuid, SHORT_UUID_A);
}

#[test]
fn extract_keeps_uuid_v7_task_ids() {
    let uuid = uuid::Uuid::parse_str("01a121dc-e36b-7d71-b4f1-d1d55b70820c").unwrap();
    let task_id = MacroTaskId::from_uuid(&uuid);
    let ids = MacroTaskId::extract_from_text(&format!("closes {task_id}"));
    assert_eq!(ids, [task_id]);
}

#[test]
fn extract_ignores_short_ids_that_are_not_uuids() {
    // Each decodes to a small integer such as 00000000-0000-0000-0000-000166f86534.
    let ids = MacroTaskId::extract_from_text("fixes MACRO-abc123 and macro-2BuyvtY3ae");
    assert!(ids.is_empty());
}

#[test]
fn extract_ignores_macro_inc_org_in_github_urls() {
    let text = "See https://github.com/macro-inc/macro/pull/7882 for context";
    assert!(MacroTaskId::extract_from_text(text).is_empty());
}

#[test]
fn extract_ignores_team_task_numbers() {
    let text = "synoet/macro-3692-on-main";
    assert!(MacroTaskId::extract_from_text(text).is_empty());
    assert_eq!(
        TeamTaskReference::extract_from_text(text),
        vec![TeamTaskReference::new("macro", 3692).unwrap()]
    );
}

#[test]
fn extract_no_match() {
    let text = "no task ids here, just MACR-123 or MACRO- or MACRO";
    let ids = MacroTaskId::extract_from_text(text);
    assert!(ids.is_empty());
}

#[test]
fn extract_ignores_invalid_base58_chars() {
    // '0', 'O', 'I', 'l' are not in Flickr base58
    // "MACRO-000abc" -> regex captures "000abc" but from_short_uuid rejects it
    let text = "MACRO-000abc";
    let ids = MacroTaskId::extract_from_text(text);
    assert!(ids.is_empty());
}

#[test]
fn extract_from_branch_name() {
    let text = format!("feature/macro-{SHORT_UUID_A}");
    let ids = MacroTaskId::extract_from_text(&text);
    assert_eq!(ids.len(), 1);
    assert_eq!(ids[0].short_uuid, SHORT_UUID_A);
}

#[test]
fn extract_multiple_in_sentence() {
    let text = format!("closes MACRO-{SHORT_UUID_A} and MACRO-{SHORT_UUID_B}");
    let ids = MacroTaskId::extract_from_text(&text);
    assert_eq!(ids.len(), 2);
    assert_eq!(ids[0].short_uuid, SHORT_UUID_A);
    assert_eq!(ids[1].short_uuid, SHORT_UUID_B);
}

// ---------------------------------------------------------------------------
// TeamTaskReference::extract_from_text
// ---------------------------------------------------------------------------

#[test]
fn extract_team_task_reference_from_branch_name() {
    let refs = TeamTaskReference::extract_from_text("whutch/eng-123-fix-the-thing");
    assert_eq!(refs, vec![TeamTaskReference::new("eng", 123).unwrap()]);
}

#[test]
fn extract_team_task_reference_case_insensitive_and_dedupes() {
    let refs = TeamTaskReference::extract_from_text("ENG-123 eng-123 platform_api-7");
    assert_eq!(refs.len(), 2);
    assert_eq!(refs[0], TeamTaskReference::new("eng", 123).unwrap());
    assert_eq!(refs[1], TeamTaskReference::new("platform_api", 7).unwrap());
}

#[test]
fn extract_team_task_reference_rejects_invalid_numbers() {
    let refs = TeamTaskReference::extract_from_text("eng-0 eng-abc eng-999999999999999999999");
    assert!(refs.is_empty());
}

// ---------------------------------------------------------------------------
// ValidatedGithubWebhookEvent::pull_number / repo_owner / repo_name / installation_id
// ---------------------------------------------------------------------------

#[test]
fn pull_number_from_pull_request() {
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "pull_request": { "number": 42 }
        }),
    );
    assert_eq!(event.pull_number(), Some(42));
}

#[test]
fn pull_number_from_issue() {
    let event = ValidatedGithubWebhookEvent::new(
        "issue_comment".to_string(),
        serde_json::json!({
            "issue": { "number": 99 }
        }),
    );
    assert_eq!(event.pull_number(), Some(99));
}

#[test]
fn pull_number_prefers_pull_request_over_issue() {
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "pull_request": { "number": 1 },
            "issue": { "number": 2 }
        }),
    );
    assert_eq!(event.pull_number(), Some(1));
}

#[test]
fn pull_number_missing() {
    let event =
        ValidatedGithubWebhookEvent::new("ping".to_string(), serde_json::json!({"zen": "hello"}));
    assert_eq!(event.pull_number(), None);
}

#[test]
fn repo_owner_present() {
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "repository": { "owner": { "login": "my-org" }, "name": "my-repo" }
        }),
    );
    assert_eq!(event.repo_owner(), Some("my-org"));
}

#[test]
fn repo_owner_missing() {
    let event = ValidatedGithubWebhookEvent::new("pull_request".to_string(), serde_json::json!({}));
    assert_eq!(event.repo_owner(), None);
}

#[test]
fn repo_name_present() {
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "repository": { "name": "cool-repo", "owner": { "login": "x" } }
        }),
    );
    assert_eq!(event.repo_name(), Some("cool-repo"));
}

#[test]
fn repo_name_missing() {
    let event = ValidatedGithubWebhookEvent::new("pull_request".to_string(), serde_json::json!({}));
    assert_eq!(event.repo_name(), None);
}

#[test]
fn installation_id_present() {
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "installation": { "id": 12345 }
        }),
    );
    assert_eq!(event.installation_id(), Some(12345));
}

#[test]
fn installation_id_missing() {
    let event = ValidatedGithubWebhookEvent::new("pull_request".to_string(), serde_json::json!({}));
    assert_eq!(event.installation_id(), None);
}

// ---------------------------------------------------------------------------
// GithubWebhookEventType::from_event_header
// ---------------------------------------------------------------------------

#[test]
fn event_type_from_known_headers() {
    assert_eq!(
        GithubWebhookEventType::from_event_header("pull_request"),
        GithubWebhookEventType::PullRequest
    );
    assert_eq!(
        GithubWebhookEventType::from_event_header("issue_comment"),
        GithubWebhookEventType::IssueComment
    );
    assert_eq!(
        GithubWebhookEventType::from_event_header("pull_request_review"),
        GithubWebhookEventType::PullRequestReview
    );
    assert_eq!(
        GithubWebhookEventType::from_event_header("pull_request_review_comment"),
        GithubWebhookEventType::PullRequestReviewComment
    );
}

#[test]
fn event_type_unknown() {
    assert_eq!(
        GithubWebhookEventType::from_event_header("ping"),
        GithubWebhookEventType::Unknown("ping".to_string())
    );
    assert_eq!(
        GithubWebhookEventType::from_event_header("push"),
        GithubWebhookEventType::Unknown("push".to_string())
    );
}

// ---------------------------------------------------------------------------
// ValidatedGithubWebhookEvent::extract_searchable_text
// ---------------------------------------------------------------------------

#[test]
fn extract_text_pull_request() {
    let payload = serde_json::json!({
        "action": "opened",
        "pull_request": {
            "title": "fixes MACRO-abc123",
            "body": "This PR closes MACRO-def456",
            "head": {
                "ref": "feature/macro-ghi789"
            }
        }
    });
    let event = ValidatedGithubWebhookEvent::new("pull_request".to_string(), payload);
    let texts = event.extract_searchable_text();
    assert_eq!(texts.len(), 3);
    assert_eq!(texts[0], "fixes MACRO-abc123");
    assert_eq!(texts[1], "This PR closes MACRO-def456");
    assert_eq!(texts[2], "feature/macro-ghi789");
}

#[test]
fn extract_text_pull_request_null_body() {
    let payload = serde_json::json!({
        "action": "opened",
        "pull_request": {
            "title": "some title",
            "body": null,
            "head": {
                "ref": "main"
            }
        }
    });
    let event = ValidatedGithubWebhookEvent::new("pull_request".to_string(), payload);
    let texts = event.extract_searchable_text();
    assert_eq!(texts.len(), 2);
    assert_eq!(texts[0], "some title");
    assert_eq!(texts[1], "main");
}

#[test]
fn extract_text_issue_comment() {
    let payload = serde_json::json!({
        "action": "created",
        "comment": {
            "body": "See MACRO-abc123 for details"
        }
    });
    let event = ValidatedGithubWebhookEvent::new("issue_comment".to_string(), payload);
    let texts = event.extract_searchable_text();
    assert_eq!(texts.len(), 1);
    assert_eq!(texts[0], "See MACRO-abc123 for details");
}

#[test]
fn extract_text_pull_request_review() {
    let payload = serde_json::json!({
        "action": "submitted",
        "review": {
            "body": "Looks good, relates to MACRO-xyz789"
        }
    });
    let event = ValidatedGithubWebhookEvent::new("pull_request_review".to_string(), payload);
    let texts = event.extract_searchable_text();
    assert_eq!(texts.len(), 1);
    assert_eq!(texts[0], "Looks good, relates to MACRO-xyz789");
}

#[test]
fn extract_text_pull_request_review_comment() {
    let payload = serde_json::json!({
        "action": "created",
        "comment": {
            "body": "This line relates to MACRO-abc123"
        }
    });
    let event =
        ValidatedGithubWebhookEvent::new("pull_request_review_comment".to_string(), payload);
    let texts = event.extract_searchable_text();
    assert_eq!(texts.len(), 1);
    assert_eq!(texts[0], "This line relates to MACRO-abc123");
}

#[test]
fn extract_text_unknown_event() {
    let payload = serde_json::json!({"zen": "Keep it logically awesome."});
    let event = ValidatedGithubWebhookEvent::new("ping".to_string(), payload);
    let texts = event.extract_searchable_text();
    assert!(texts.is_empty());
}

// ---------------------------------------------------------------------------
// strip_markdown_code
// ---------------------------------------------------------------------------

#[test]
fn strip_markdown_code_removes_inline_spans() {
    let stripped = strip_markdown_code("switch the sidebar to `py-6` for spacing");
    assert_eq!(stripped, "switch the sidebar to   for spacing");
    assert!(TeamTaskReference::extract_from_text(&stripped).is_empty());
}

#[test]
fn strip_markdown_code_removes_fenced_blocks() {
    let text = "Adjust padding:\n```css\n.sidebar { @apply py-6 px-4; }\n```\nDone.";
    let stripped = strip_markdown_code(text);
    assert_eq!(stripped, "Adjust padding:\nDone.");
    assert!(TeamTaskReference::extract_from_text(&stripped).is_empty());
}

#[test]
fn strip_markdown_code_removes_tilde_fences_and_language_tags() {
    let text = "before\n~~~diff\n- pt-4\n+ py-6\n~~~\nafter";
    assert_eq!(strip_markdown_code(text), "before\nafter");
}

#[test]
fn strip_markdown_code_unclosed_fence_swallows_rest() {
    let text = "intro\n```\npy-6 and db-27 live here";
    assert_eq!(strip_markdown_code(text), "intro");
}

#[test]
fn strip_markdown_code_double_backtick_spans() {
    let stripped = strip_markdown_code("use ``a `py-6` class`` here");
    assert_eq!(stripped, "use   here");
}

#[test]
fn strip_markdown_code_keeps_unmatched_backticks_and_prose_refs() {
    let stripped = strip_markdown_code("odd ` backtick, but PY-6 stays a reference");
    assert_eq!(stripped, "odd ` backtick, but PY-6 stays a reference");
    assert_eq!(
        TeamTaskReference::extract_from_text(&stripped),
        vec![TeamTaskReference::new("py", 6).unwrap()]
    );
}

#[test]
fn strip_markdown_code_plain_text_unchanged() {
    let text = "fixes py-6 and db-27 in prose";
    assert_eq!(strip_markdown_code(text), text);
}

// Regression: a review quoting a Tailwind class list (`py-6`) must not
// produce a task reference for a team with slug PY, while the reference in
// the branch name still resolves.
#[test]
fn extract_text_strips_code_from_review_body() {
    let payload = serde_json::json!({
        "action": "submitted",
        "review": {
            "body": "Consider tightening the spacing:\n```tsx\n<aside class=\"py-6 px-4\">\n```\nOtherwise LGTM."
        }
    });
    let event = ValidatedGithubWebhookEvent::new("pull_request_review".to_string(), payload);
    let texts = event.extract_searchable_text();
    assert_eq!(texts.len(), 1);
    let refs = TeamTaskReference::extract_from_text(&texts.join(" "));
    assert!(refs.is_empty());
}

#[test]
fn extract_text_strips_code_from_pr_body_but_keeps_branch() {
    let payload = serde_json::json!({
        "action": "opened",
        "pull_request": {
            "title": "fix: adjust panel padding",
            "body": "Swap `pt-4` for `py-6` on the panel container.",
            "head": { "ref": "someuser/db-27-adjust-panel-padding" }
        }
    });
    let event = ValidatedGithubWebhookEvent::new("pull_request".to_string(), payload);
    let texts = event.extract_searchable_text();
    let refs = TeamTaskReference::extract_from_text(&texts.join(" "));
    assert_eq!(refs, vec![TeamTaskReference::new("db", 27).unwrap()]);
}

// ---------------------------------------------------------------------------
// ValidatedGithubWebhookEvent::extract_pr_context_text
// ---------------------------------------------------------------------------

#[test]
fn pr_context_empty_for_pull_request_event() {
    let payload = serde_json::json!({
        "action": "opened",
        "pull_request": {
            "title": "fixes MACRO-abc123",
            "body": "body text",
            "head": { "ref": "feature/macro-abc123" }
        }
    });
    let event = ValidatedGithubWebhookEvent::new("pull_request".to_string(), payload);
    assert!(event.extract_pr_context_text().is_empty());
}

#[test]
fn pr_context_from_issue_comment() {
    let payload = serde_json::json!({
        "action": "created",
        "issue": {
            "title": "PR title with MACRO-abc123",
            "body": "PR body with MACRO-def456",
            "pull_request": {},
            "head": { "ref": "feature/macro-ghi789" }
        },
        "comment": {
            "body": "Fixes MACRO-abc123"
        }
    });
    let event = ValidatedGithubWebhookEvent::new("issue_comment".to_string(), payload);
    let texts = event.extract_pr_context_text();
    assert_eq!(texts.len(), 3);
    assert_eq!(texts[0], "PR title with MACRO-abc123");
    assert_eq!(texts[1], "PR body with MACRO-def456");
    assert_eq!(texts[2], "feature/macro-ghi789");
}

#[test]
fn pr_context_from_review() {
    let payload = serde_json::json!({
        "action": "submitted",
        "pull_request": {
            "title": "MACRO-abc123 fix",
            "body": null,
            "head": { "ref": "main" }
        },
        "review": {
            "body": "Relates to MACRO-abc123"
        }
    });
    let event = ValidatedGithubWebhookEvent::new("pull_request_review".to_string(), payload);
    let texts = event.extract_pr_context_text();
    assert_eq!(texts.len(), 2);
    assert_eq!(texts[0], "MACRO-abc123 fix");
    assert_eq!(texts[1], "main");
}

#[test]
fn pr_context_from_review_comment() {
    let payload = serde_json::json!({
        "action": "created",
        "pull_request": {
            "title": "MACRO-abc123 fix",
            "body": "details",
            "head": { "ref": "feature/macro-abc123" }
        },
        "comment": {
            "body": "This line relates to MACRO-abc123"
        }
    });
    let event =
        ValidatedGithubWebhookEvent::new("pull_request_review_comment".to_string(), payload);
    let texts = event.extract_pr_context_text();
    assert_eq!(texts.len(), 3);
    assert_eq!(texts[0], "MACRO-abc123 fix");
    assert_eq!(texts[1], "details");
    assert_eq!(texts[2], "feature/macro-abc123");
}

#[test]
fn pr_context_empty_for_unknown_event() {
    let payload = serde_json::json!({"zen": "Keep it logically awesome."});
    let event = ValidatedGithubWebhookEvent::new("ping".to_string(), payload);
    assert!(event.extract_pr_context_text().is_empty());
}

// ---------------------------------------------------------------------------
// extract_github_mentions
// ---------------------------------------------------------------------------

#[test]
fn github_mentions_extracts_unique_lowercased_logins() {
    let mentions = extract_github_mentions(
        "@Alice please review, cc @bob-smith and @alice again.\n@carol99: thoughts?",
    );
    assert_eq!(mentions, vec!["alice", "bob-smith", "carol99"]);
}

#[test]
fn github_mentions_ignores_emails_and_bare_at_signs() {
    let mentions = extract_github_mentions("contact me at [email protected] or @ (nothing)");
    assert!(mentions.is_empty());
}

#[test]
fn github_mentions_matches_at_start_and_after_punctuation() {
    let mentions = extract_github_mentions("@lead-dev: see (@helper) and [@docs-team]");
    assert_eq!(mentions, vec!["docs-team", "helper", "lead-dev"]);
}

#[test]
fn github_mentions_does_not_capture_trailing_hyphen() {
    let mentions = extract_github_mentions("ping @user- and @-nobody");
    assert_eq!(mentions, vec!["user"]);
}

// ---------------------------------------------------------------------------
// pull_request_task_reference
// ---------------------------------------------------------------------------

#[test]
fn pull_request_task_reference_prefers_the_team_reference() {
    let task = MacroTaskId::from_short_uuid("2BuyvtY3ae").unwrap();
    let team = TeamTaskReference::new("ENG_CORE", 42).unwrap();
    let reference = pull_request_task_reference(&task, Some(&team));
    assert_eq!(reference, "ENG_CORE-42");
    assert_eq!(TeamTaskReference::extract_from_text(&reference), [team]);
}

#[test]
fn pull_request_task_reference_falls_back_to_the_macro_id() {
    let task = MacroTaskId::from_short_uuid(SHORT_UUID_A).unwrap();
    assert_eq!(
        pull_request_task_reference(&task, None),
        format!("MACRO-{SHORT_UUID_A}")
    );
    // A slug the webhook's parser would not read back cannot be the reference.
    let unreadable = TeamTaskReference::new("A_VERY_LONG_TEAM_SLUG_NAME", 7).unwrap();
    let reference = pull_request_task_reference(&task, Some(&unreadable));
    assert_eq!(reference, format!("MACRO-{SHORT_UUID_A}"));
    assert_eq!(MacroTaskId::extract_from_text(&reference), [task]);
}

#[test]
fn task_status_source_names_who_merged_over_the_webhook_sender() {
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "closed",
            "pull_request": {
                "merged": true,
                "html_url": "https://github.com/my-org/my-repo/pull/42",
                "merged_by": { "login": "merger", "id": 333 }
            },
            "sender": { "login": "automation", "id": 444 }
        }),
    );

    assert_eq!(
        event.task_status_source(),
        Some(PropertyChangeSource::GithubPullRequest {
            url: "https://github.com/my-org/my-repo/pull/42".to_string(),
            account: GithubAccount {
                id: "333".to_string(),
                login: "merger".to_string(),
            },
        })
    );
}

#[test]
fn task_status_source_falls_back_to_the_sender_on_an_open_pull_request() {
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "opened",
            "pull_request": {
                "html_url": "https://github.com/my-org/my-repo/pull/42",
                "merged_by": null
            },
            "sender": { "login": "opener", "id": 444 }
        }),
    );

    assert_eq!(
        event.task_status_source(),
        Some(PropertyChangeSource::GithubPullRequest {
            url: "https://github.com/my-org/my-repo/pull/42".to_string(),
            account: GithubAccount {
                id: "444".to_string(),
                login: "opener".to_string(),
            },
        })
    );
}

#[test]
fn task_status_source_reads_the_pull_request_from_a_comment_event() {
    let event = ValidatedGithubWebhookEvent::new(
        "issue_comment".to_string(),
        serde_json::json!({
            "action": "created",
            "issue": {
                "html_url": "https://github.com/my-org/my-repo/pull/42",
                "pull_request": { "url": "https://api.github.com/repos/my-org/my-repo/pulls/42" }
            },
            "sender": { "login": "commenter", "id": 555 }
        }),
    );

    assert_eq!(
        event.task_status_source(),
        Some(PropertyChangeSource::GithubPullRequest {
            url: "https://github.com/my-org/my-repo/pull/42".to_string(),
            account: GithubAccount {
                id: "555".to_string(),
                login: "commenter".to_string(),
            },
        })
    );
}

#[test]
fn task_status_source_is_none_for_a_comment_on_a_plain_issue() {
    let event = ValidatedGithubWebhookEvent::new(
        "issue_comment".to_string(),
        serde_json::json!({
            "action": "created",
            "issue": {
                "html_url": "https://github.com/my-org/my-repo/issues/42",
                "state": "open"
            },
            "sender": { "login": "commenter", "id": 555 }
        }),
    );

    assert_eq!(event.task_status_for_event(), Some("In Review"));
    assert_eq!(event.task_status_source(), None);
}

#[test]
fn task_status_source_names_the_editor_of_an_already_merged_pull_request() {
    let event = ValidatedGithubWebhookEvent::new(
        "pull_request".to_string(),
        serde_json::json!({
            "action": "edited",
            "pull_request": {
                "merged": true,
                "html_url": "https://github.com/my-org/my-repo/pull/42",
                "merged_by": { "login": "merger", "id": 333 }
            },
            "sender": { "login": "editor", "id": 444 }
        }),
    );

    assert_eq!(
        event.task_status_source(),
        Some(PropertyChangeSource::GithubPullRequest {
            url: "https://github.com/my-org/my-repo/pull/42".to_string(),
            account: GithubAccount {
                id: "444".to_string(),
                login: "editor".to_string(),
            },
        })
    );
}

#[test]
fn a_delivery_keeps_its_meaning_and_drops_api_links() {
    let body = serde_json::json!({
        "action": "completed",
        "installation": { "id": 77, "node_id": "x" },
        "repository": {
            "id": 42,
            "full_name": "macro-inc/macro",
            "url": "https://api.github.com/repos/macro-inc/macro",
            "html_url": "https://github.com/macro-inc/macro",
            "hooks_url": "https://api.github.com/repos/macro-inc/macro/hooks",
        },
        "sender": { "login": "dependabot[bot]", "type": "Bot", "avatar_url": "https://x" },
        "workflow_run": { "name": "CI", "conclusion": "failure", "jobs_url": "https://x" },
    });

    let delivery = GithubWebhookReceived::from_delivery(
        Some("d-1".to_owned()),
        "workflow_run".to_owned(),
        &body,
    )
    .unwrap();

    assert_eq!(delivery.installation_id, 77);
    assert_eq!(delivery.action.as_deref(), Some("completed"));
    assert_eq!(delivery.repository.as_ref().unwrap().id, 42);
    assert!(delivery.sender.as_ref().unwrap().bot);
    assert_eq!(
        delivery.payload,
        Some(serde_json::json!({
            "action": "completed",
            "repository": {
                "id": 42,
                "full_name": "macro-inc/macro",
                "html_url": "https://github.com/macro-inc/macro",
            },
            "sender": { "login": "dependabot[bot]", "type": "Bot" },
            "workflow_run": { "name": "CI", "conclusion": "failure" },
        }))
    );
}

#[test]
fn a_delivery_without_an_installation_is_nobodys() {
    let body =
        serde_json::json!({ "action": "revoked", "sender": { "login": "a", "type": "User" } });

    assert!(
        GithubWebhookReceived::from_delivery(None, "github_app_authorization".to_owned(), &body)
            .is_none()
    );
}

#[test]
fn an_oversized_payload_travels_without_it() {
    let body = serde_json::json!({
        "installation": { "id": 1 },
        "commits": [{ "message": "x".repeat(MAX_WEBHOOK_PAYLOAD_BYTES) }],
    });

    let delivery = GithubWebhookReceived::from_delivery(None, "push".to_owned(), &body).unwrap();

    assert_eq!(delivery.payload, None);
}