Files
gitea-pr-review/tests/render_markdown_tests.rs
T

141 lines
4.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use gitea_pr_review::model::{
CommentItem, CommentThread, CommitItem, DiffStat, FileStat, PrMeta, PrReviewDocument,
ReviewItem,
};
use gitea_pr_review::render::markdown::render_markdown;
#[test]
fn render_markdown_includes_expected_sections_and_preserves_comment_markdown() {
let doc = PrReviewDocument {
meta: PrMeta {
repo: "org/repo".into(),
pr_index: 7,
title: "Fix parser".into(),
description: Some("PR body".into()),
fetched_at: Some("2026-04-08T12:34:56Z".into()),
state: "open".into(),
author: "alice".into(),
base_branch: "main".into(),
head_branch: "feat/a".into(),
created_at: "2026-04-08T10:00:00Z".into(),
updated_at: "2026-04-08T11:00:00Z".into(),
merged_at: None,
},
commits: vec![CommitItem {
sha: "abcdef0123456789".into(),
short_sha: "abcdef0".into(),
title: "fix: parser".into(),
author: "alice".into(),
date: "2026-04-08T10:10:00Z".into(),
}],
diff_stat: DiffStat {
files_changed: 1,
additions: 3,
deletions: 1,
files: vec![FileStat {
path: "src/main.rs".into(),
additions: 3,
deletions: 1,
}],
},
reviews: vec![ReviewItem {
id: 1,
state: "COMMENT".into(),
reviewer: "bob".into(),
submitted_at: Some("2026-04-08T11:00:00Z".into()),
}],
threads: vec![CommentThread {
thread_id: "t1".into(),
file_path: Some("src/main.rs".into()),
line: Some(9),
root_comment: CommentItem {
id: 11,
review_id: Some(1),
user: "bob".into(),
created_at: "2026-04-08T11:01:00Z".into(),
path: Some("src/main.rs".into()),
line: Some(9),
body: "```rs\nlet x = 1;\n```".into(),
},
replies: vec![CommentItem {
id: 12,
review_id: Some(1),
user: "alice".into(),
created_at: "2026-04-08T11:02:00Z".into(),
path: Some("src/main.rs".into()),
line: Some(12),
body: "looks good".into(),
}],
}],
};
let md = render_markdown(&doc);
assert!(md.contains("# org/repo `#7` Fix parser"));
assert!(md.contains("## Metadata"));
assert!(md.contains("> fetched at: 2026-04-08T12:34:56Z"));
assert!(md.contains("## Commits"));
assert!(md.contains("## Diff Stat"));
assert!(md.contains("> 编号规则:Review `<pr>.<review>`Comment `<pr>.<review>.<comment>`Reply `<pr>.<review>.<comment>.<reply>`"));
assert!(md.contains("PR body"));
assert!(md.contains("## Review 7.1 (COMMENT)"));
assert!(md.contains("### Comment 7.1.1"));
assert!(md.contains("src/main.rs:9"));
assert!(md.contains("### Reply 7.1.1.1"));
assert!(md.contains("src/main.rs:12"));
let review_pos = md.find("## Review 7.1 (COMMENT)").unwrap();
let comment_pos = md.find("### Comment 7.1.1").unwrap();
assert!(comment_pos > review_pos);
assert!(!md.contains("## Comments (No Review)"));
assert!(md.contains("````md"));
assert!(md.contains("```rs\nlet x = 1;\n```"));
assert!(md.contains("looks good"));
}
#[test]
fn render_markdown_uses_minimal_fence_for_plain_text() {
let body = "plain text";
let doc = PrReviewDocument {
meta: PrMeta {
repo: "org/repo".into(),
pr_index: 1,
title: "T".into(),
description: None,
fetched_at: None,
state: "open".into(),
author: "alice".into(),
base_branch: "main".into(),
head_branch: "feat".into(),
created_at: "2026-04-08T10:00:00Z".into(),
updated_at: "2026-04-08T10:00:00Z".into(),
merged_at: None,
},
commits: vec![],
diff_stat: DiffStat {
files_changed: 0,
additions: 0,
deletions: 0,
files: vec![],
},
reviews: vec![],
threads: vec![CommentThread {
thread_id: "t1".into(),
file_path: None,
line: None,
root_comment: CommentItem {
id: 1,
review_id: None,
user: "bob".into(),
created_at: "2026-04-08T11:00:00Z".into(),
path: None,
line: None,
body: body.into(),
},
replies: vec![],
}],
};
let md = render_markdown(&doc);
assert!(md.contains("```md\nplain text\n```"));
}