144 lines
4.9 KiB
Rust
144 lines
4.9 KiB
Rust
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 {
|
||
version: "v1".into(),
|
||
meta: PrMeta {
|
||
repo: "org/repo".into(),
|
||
pr_index: 7,
|
||
title: "Fix parser".into(),
|
||
description: Some("PR body".into()),
|
||
fetched_at: "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("> version: v1"));
|
||
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 {
|
||
version: "v1".into(),
|
||
meta: PrMeta {
|
||
repo: "org/repo".into(),
|
||
pr_index: 1,
|
||
title: "T".into(),
|
||
description: None,
|
||
fetched_at: "2026-04-08T12:34:56Z".into(),
|
||
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```"));
|
||
}
|