use crate::model::{
AGENTTY_REVIEW_REPLY_MARKER_PREFIX, ForgeKind, ForgeRemote, ReviewComment,
ReviewCommentAnchorSide, ReviewCommentThread, ReviewRequestError, ReviewRequestState,
ReviewRequestSummary,
};
use crate::remote::detect_remote;
#[test]
fn ready_pr_marker_requires_open_github_request_and_exact_component() {
let mut summary = ReviewRequestSummary {
display_id: "#42".to_string(),
forge_kind: ForgeKind::GitHub,
source_branch: "feature".to_string(),
state: ReviewRequestState::Open,
status_summary: Some("Approved, Mergeable, PR ready".to_string()),
target_branch: "main".to_string(),
title: "Feature".to_string(),
web_url: "https://github.com/example/repo/pull/42".to_string(),
};
assert!(summary.is_github_pr_ready());
summary.status_summary = Some("Approved, PR readyish".to_string());
assert!(!summary.is_github_pr_ready());
summary.status_summary = Some("PR ready".to_string());
summary.state = ReviewRequestState::Merged;
assert!(!summary.is_github_pr_ready());
summary.state = ReviewRequestState::Open;
summary.forge_kind = ForgeKind::GitLab;
assert!(!summary.is_github_pr_ready());
summary.forge_kind = ForgeKind::GitHub;
summary.status_summary = None;
assert!(!summary.is_github_pr_ready());
}
#[test]
fn bypass_ready_pr_marker_is_distinct_from_clean_ready_marker() {
let mut summary = ReviewRequestSummary {
display_id: "#42".to_string(),
forge_kind: ForgeKind::GitHub,
source_branch: "feature".to_string(),
state: ReviewRequestState::Open,
status_summary: Some("Review required, Blocked, PR ready with bypass".to_string()),
target_branch: "main".to_string(),
title: "Feature".to_string(),
web_url: "https://github.com/example/repo/pull/42".to_string(),
};
assert!(summary.is_github_pr_bypass_ready());
assert!(!summary.is_github_pr_ready());
summary.state = ReviewRequestState::Closed;
assert!(!summary.is_github_pr_bypass_ready());
summary.state = ReviewRequestState::Open;
summary.status_summary = Some("Approved, Mergeable, PR ready".to_string());
assert!(!summary.is_github_pr_bypass_ready());
}
#[test]
fn review_comment_thread_is_actionable_until_agentty_addresses_latest_feedback() {
let actionable = review_comment_thread();
let mut resolved = review_comment_thread();
resolved.is_resolved = true;
let mut outdated = review_comment_thread();
outdated.is_outdated = Some(true);
let mut addressed = review_comment_thread();
addressed.comments.push(ReviewComment {
author: "agentty".to_string(),
authored_by_current_user: true,
body: [
"No change needed.\n\n",
AGENTTY_REVIEW_REPLY_MARKER_PREFIX,
"123e4567-e89b-12d3-a456-426614174000 -->",
]
.concat(),
});
let mut followed_up = addressed.clone();
followed_up.comments.push(ReviewComment {
author: "reviewer".to_string(),
authored_by_current_user: false,
body: "Please reconsider.".to_string(),
});
let mut reviewer_marker = review_comment_thread();
reviewer_marker.comments.push(ReviewComment {
author: "reviewer".to_string(),
authored_by_current_user: false,
body: [
"Please reconsider.\n\n",
AGENTTY_REVIEW_REPLY_MARKER_PREFIX,
"123e4567-e89b-12d3-a456-426614174000 -->",
]
.concat(),
});
assert!(actionable.is_actionable());
assert!(!resolved.is_actionable());
assert!(outdated.is_actionable());
assert!(addressed.is_addressed_by_agentty());
assert!(!addressed.is_actionable());
assert!(!followed_up.is_addressed_by_agentty());
assert!(followed_up.is_actionable());
assert!(!reviewer_marker.is_addressed_by_agentty());
assert!(reviewer_marker.is_actionable());
}
#[test]
fn review_comment_rejects_malformed_agentty_reply_markers() {
let malformed_comments = [
"Ordinary comment",
"No separator<!-- agentty review resolution:123e4567-e89b-12d3-a456-426614174000 -->",
"No terminator\n\n<!-- agentty review resolution:123e4567-e89b-12d3-a456-426614174000",
"Bad token\n\n<!-- agentty review resolution:not-a-uuid -->",
];
let results = malformed_comments.map(|body| ReviewComment {
author: "agentty".to_string(),
authored_by_current_user: true,
body: body.to_string(),
});
assert!(results.iter().all(|comment| !comment.is_agentty_reply()));
}
#[test]
fn forge_kind_round_trips_and_describes_each_forge() {
let cases = [
(
"GitHub",
ForgeKind::GitHub,
["gh", "gh auth login", "GitHub pull request", "PR"],
),
(
"GitLab",
ForgeKind::GitLab,
["glab", "glab auth login", "GitLab merge request", "MR"],
),
];
for (raw_forge_kind, expected_kind, expected_copy) in cases {
let forge_kind = raw_forge_kind
.parse::<ForgeKind>()
.expect("known forge kind should parse");
assert_eq!(forge_kind, expected_kind);
assert_eq!(forge_kind.to_string(), raw_forge_kind);
assert_eq!(
[
forge_kind.cli_name().to_string(),
forge_kind.auth_login_command().to_string(),
forge_kind.review_request_display_name(),
forge_kind.review_request_short_name().to_string(),
],
expected_copy.map(str::to_string)
);
}
assert_eq!(
"Gitea".parse::<ForgeKind>(),
Err("Unknown review-request forge: Gitea".to_string())
);
}
#[test]
fn review_request_state_round_trips_persisted_names() {
let cases = [
("Open", ReviewRequestState::Open),
("Merged", ReviewRequestState::Merged),
("Closed", ReviewRequestState::Closed),
];
for (raw_state, expected_state) in cases {
let state = raw_state
.parse::<ReviewRequestState>()
.expect("known state should parse");
assert_eq!(state, expected_state);
assert_eq!(state.to_string(), raw_state);
}
assert_eq!(
"Draft".parse::<ReviewRequestState>(),
Err("Unknown review-request state: Draft".to_string())
);
}
#[test]
fn detail_message_describes_non_authentication_failures() {
let cases = [
(
ReviewRequestError::CliNotInstalled {
forge_kind: ForgeKind::GitLab,
},
"GitLab review requests require the `glab` CLI.\nInstall `glab` and run `glab auth \
login`, then retry.",
),
(
ReviewRequestError::HostResolutionFailed {
forge_kind: ForgeKind::GitHub,
host: "github.example.com".to_string(),
},
"GitHub review requests could not reach `github.example.com`.\nCheck the repository \
remote host and your network or DNS setup, then retry.",
),
(
ReviewRequestError::UnsupportedRemote {
repo_url: "https://example.com/team/project.git".to_string(),
},
"Review requests are only supported for GitHub and GitLab remotes.\nThis repository \
remote is not supported: `https://example.com/team/project.git`.",
),
(
ReviewRequestError::OperationFailed {
forge_kind: ForgeKind::GitHub,
message: "HTTP 500".to_string(),
},
"GitHub review-request operation failed: HTTP 500",
),
];
for (error, expected_message) in cases {
let message = error.detail_message();
assert_eq!(message, expected_message);
}
}
#[test]
fn authentication_required_message_includes_original_cli_error_detail() {
let error = ReviewRequestError::AuthenticationRequired {
detail: Some("HTTP 401 Unauthorized. Run `gh auth login`.".to_string()),
forge_kind: ForgeKind::GitHub,
host: "github.com".to_string(),
};
let message = error.detail_message();
assert!(message.contains("GitHub review requests require local CLI authentication"));
assert!(message.contains("Run `gh auth login` and retry."));
assert!(message.contains("Original `gh` error:"));
assert!(message.ends_with("```text\nHTTP 401 Unauthorized. Run `gh auth login`.\n```"));
}
#[test]
fn authentication_required_message_omits_empty_original_cli_error_detail() {
let error = ReviewRequestError::AuthenticationRequired {
detail: Some(" \n".to_string()),
forge_kind: ForgeKind::GitHub,
host: "github.com".to_string(),
};
let message = error.detail_message();
assert!(message.contains("Run `gh auth login` and retry."));
assert!(!message.contains("Original `gh` error:"));
}
#[test]
fn review_request_creation_url_returns_github_compare_link() {
let remote = ForgeRemote {
command_working_directory: None,
forge_kind: ForgeKind::GitHub,
host: "github.com".to_string(),
namespace: "agentty-xyz".to_string(),
project: "agentty".to_string(),
repo_url: "git@github.com:agentty-xyz/agentty.git".to_string(),
web_url: "https://github.com/agentty-xyz/agentty".to_string(),
};
let url = remote
.review_request_creation_url("review/custom-branch", "main")
.expect("github compare URL should be created");
assert_eq!(
url,
"https://github.com/agentty-xyz/agentty/compare/main...review%2Fcustom-branch?expand=1"
);
}
#[test]
fn review_request_creation_url_rejects_invalid_web_url() {
let remote = ForgeRemote {
command_working_directory: None,
forge_kind: ForgeKind::GitHub,
host: "github.com".to_string(),
namespace: "agentty-xyz".to_string(),
project: "agentty".to_string(),
repo_url: "git@github.com:agentty-xyz/agentty.git".to_string(),
web_url: "not a url".to_string(),
};
let error = remote
.review_request_creation_url("review/custom-branch", "main")
.expect_err("invalid web URL should be rejected");
assert_eq!(
error,
ReviewRequestError::OperationFailed {
forge_kind: ForgeKind::GitHub,
message: "repository remote is missing a valid web URL: `not a url`".to_string(),
}
);
}
#[test]
fn review_request_creation_url_returns_gitlab_merge_request_link() {
let remote = ForgeRemote {
command_working_directory: None,
forge_kind: ForgeKind::GitLab,
host: "gitlab.com".to_string(),
namespace: "agentty-xyz".to_string(),
project: "agentty".to_string(),
repo_url: "git@gitlab.com:agentty-xyz/agentty.git".to_string(),
web_url: "https://gitlab.com/agentty-xyz/agentty".to_string(),
};
let url = remote
.review_request_creation_url("review/custom-branch", "main")
.expect("gitlab merge-request URL should be created");
assert_eq!(
url,
"https://gitlab.com/agentty-xyz/agentty/-/merge_requests/new?merge_request%5Bsource_branch%5D=review%2Fcustom-branch&merge_request%5Btarget_branch%5D=main"
);
}
fn review_comment_thread() -> ReviewCommentThread {
ReviewCommentThread {
anchor_side: ReviewCommentAnchorSide::New,
comments: Vec::new(),
id: "thread-1".to_string(),
is_outdated: Some(false),
is_resolved: false,
line: Some(1),
path: "src/lib.rs".to_string(),
start_line: None,
}
}
#[test]
fn review_request_creation_url_rejects_urls_without_path_segments() {
for forge_kind in [ForgeKind::GitHub, ForgeKind::GitLab] {
let mut remote =
detect_remote("https://github.com/owner/project").expect("valid remote should parse");
remote.forge_kind = forge_kind;
remote.web_url = "mailto:owner@example.com".to_string();
let error = remote
.review_request_creation_url("feature", "main")
.expect_err("opaque URL cannot carry review-request path segments");
assert!(
matches!(error, ReviewRequestError::OperationFailed { forge_kind: actual, .. }
if actual == forge_kind)
);
}
}
#[test]
fn github_creation_url_without_target_compares_source_branch() {
let remote =
detect_remote("https://github.com/owner/project").expect("valid remote should parse");
let url = remote
.review_request_creation_url("feature", " ")
.expect("empty target should use the source branch");
assert_eq!(
url,
"https://github.com/owner/project/compare/feature?expand=1"
);
}