Skip to main content

fallow_output/
ci_output.rs

1//! Shared CI comment output contracts for CLI and programmatic consumers.
2
3use std::fmt::Write as _;
4
5use crate::{
6    CodeClimateIssue, CodeClimateSeverity, DiffIndex, GitHubReviewComment, GitHubReviewSide,
7    GitLabReviewComment, GitLabReviewPosition, GitLabReviewPositionType, ReviewCheckConclusion,
8    ReviewComment, ReviewEnvelopeEvent, ReviewEnvelopeMeta, ReviewEnvelopeOutput,
9    ReviewEnvelopeSchema, ReviewEnvelopeSummary, ReviewProvider, default_marker_regex,
10    default_marker_regex_flags,
11};
12use serde_json::Value;
13
14/// Supported CI review providers for generated comments.
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum CiProvider {
17    Github,
18    Gitlab,
19}
20
21impl CiProvider {
22    #[must_use]
23    pub const fn name(self) -> &'static str {
24        match self {
25            Self::Github => "GitHub",
26            Self::Gitlab => "GitLab",
27        }
28    }
29}
30
31/// Normalized CodeClimate issue used by CI comment renderers.
32#[derive(Clone, Debug, PartialEq, Eq)]
33pub struct CiIssue {
34    pub rule_id: String,
35    pub description: String,
36    pub severity: String,
37    pub path: String,
38    pub line: u64,
39    pub fingerprint: String,
40}
41
42/// Inputs for rendering a sticky PR/MR summary comment.
43pub struct PrCommentRenderInput<'a> {
44    pub command: &'a str,
45    pub provider: CiProvider,
46    pub issues: &'a [CiIssue],
47    pub marker_id: String,
48    pub max_comments: usize,
49    pub category_for_rule: &'a dyn Fn(&str) -> &'static str,
50}
51
52/// GitLab diff refs for a review-envelope position.
53#[derive(Clone, Debug, PartialEq, Eq)]
54pub struct ReviewGitlabDiffRefs {
55    pub base_sha: String,
56    pub start_sha: String,
57    pub head_sha: String,
58}
59
60/// Truncation signals produced while rendering a review envelope.
61#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
62pub struct ReviewEnvelopeTruncation {
63    pub body: bool,
64    pub comment_limit: bool,
65}
66
67/// Rendered review envelope plus side-channel signals for CLI telemetry.
68#[derive(Debug)]
69pub struct ReviewEnvelopeRenderResult {
70    pub envelope: ReviewEnvelopeOutput,
71    pub truncation: ReviewEnvelopeTruncation,
72}
73
74/// Inputs for rendering a GitHub/GitLab review envelope.
75pub struct ReviewEnvelopeRenderInput<'a> {
76    pub command: &'a str,
77    pub provider: CiProvider,
78    pub issues: &'a [CiIssue],
79    pub diff_index: Option<&'a DiffIndex>,
80    pub max_comments: usize,
81    pub gitlab_diff_refs: Option<&'a ReviewGitlabDiffRefs>,
82    pub include_guidance: bool,
83    pub suggestion_block: &'a dyn Fn(CiProvider, &CiIssue) -> Option<String>,
84    pub guidance_block: &'a dyn Fn(&CiIssue) -> Option<String>,
85}
86
87/// Marker prefix appended to every v2 review-comment body.
88pub const MARKER_PREFIX_V2: &str = "<!-- fallow-fingerprint:v2: ";
89
90/// Closing of the v2 marker, after the fingerprint string.
91pub const MARKER_SUFFIX_V2: &str = " -->";
92
93pub const MAX_COMMENT_BODY_BYTES: usize = 65_536;
94const TRUNCATION_SUFFIX: &str = "\n\n<!-- fallow-truncated -->\n> Body truncated by fallow.";
95
96#[must_use]
97pub fn issues_from_codeclimate(value: &Value) -> Vec<CiIssue> {
98    let mut issues = value
99        .as_array()
100        .into_iter()
101        .flatten()
102        .filter_map(issue_from_codeclimate)
103        .collect::<Vec<_>>();
104    sort_ci_issues(&mut issues);
105    issues
106}
107
108#[must_use]
109pub fn issues_from_codeclimate_issues(issues: &[CodeClimateIssue]) -> Vec<CiIssue> {
110    let mut issues = issues
111        .iter()
112        .map(issue_from_codeclimate_issue)
113        .collect::<Vec<_>>();
114    sort_ci_issues(&mut issues);
115    issues
116}
117
118fn issue_from_codeclimate(value: &Value) -> Option<CiIssue> {
119    let path = value.pointer("/location/path")?.as_str()?.to_string();
120    let line = value
121        .pointer("/location/lines/begin")
122        .and_then(Value::as_u64)
123        .unwrap_or(1);
124    Some(CiIssue {
125        rule_id: value
126            .get("check_name")
127            .and_then(Value::as_str)
128            .unwrap_or("fallow/finding")
129            .to_string(),
130        description: value
131            .get("description")
132            .and_then(Value::as_str)
133            .unwrap_or("Fallow finding")
134            .to_string(),
135        severity: value
136            .get("severity")
137            .and_then(Value::as_str)
138            .unwrap_or("minor")
139            .to_string(),
140        fingerprint: value
141            .get("fingerprint")
142            .and_then(Value::as_str)
143            .unwrap_or("")
144            .to_string(),
145        path,
146        line,
147    })
148}
149
150fn issue_from_codeclimate_issue(issue: &CodeClimateIssue) -> CiIssue {
151    CiIssue {
152        rule_id: issue.check_name.clone(),
153        description: issue.description.clone(),
154        severity: codeclimate_severity_label(issue.severity).to_owned(),
155        path: issue.location.path.clone(),
156        line: u64::from(issue.location.lines.begin),
157        fingerprint: issue.fingerprint.clone(),
158    }
159}
160
161const fn codeclimate_severity_label(severity: CodeClimateSeverity) -> &'static str {
162    match severity {
163        CodeClimateSeverity::Info => "info",
164        CodeClimateSeverity::Minor => "minor",
165        CodeClimateSeverity::Major => "major",
166        CodeClimateSeverity::Critical => "critical",
167        CodeClimateSeverity::Blocker => "blocker",
168    }
169}
170
171fn sort_ci_issues(issues: &mut [CiIssue]) {
172    issues
173        .sort_by(|a, b| (&a.path, a.line, &a.fingerprint).cmp(&(&b.path, b.line, &b.fingerprint)));
174}
175
176fn fingerprint_hash(parts: &[&str]) -> String {
177    crate::codeclimate_fingerprint_hash(parts)
178}
179
180#[must_use]
181#[expect(clippy::expect_used, reason = "formatting into String is infallible")]
182pub fn render_pr_comment(input: &PrCommentRenderInput<'_>) -> String {
183    let marker = format!("<!-- fallow-id: {} -->", input.marker_id);
184    let title = command_title(input.command);
185    let count = input.issues.len();
186    let noun = if count == 1 { "finding" } else { "findings" };
187
188    let mut out = String::new();
189    out.push_str(&marker);
190    out.push('\n');
191    write!(&mut out, "### Fallow {title}\n\n").expect("write to string");
192    if count == 0 {
193        writeln!(
194            &mut out,
195            "No {provider} PR/MR findings.",
196            provider = input.provider.name()
197        )
198        .expect("write to string");
199    } else {
200        write!(&mut out, "Found **{count}** {noun}.\n\n").expect("write to string");
201        let groups = group_by_category(input.issues, input.category_for_rule);
202        if let [(_, group_issues)] = groups.as_slice() {
203            render_findings_table(&mut out, group_issues, input.max_comments, "Details");
204        } else {
205            for (category, group_issues) in &groups {
206                let summary_label = summary_label(category, group_issues.len(), input.max_comments);
207                render_findings_table(&mut out, group_issues, input.max_comments, &summary_label);
208            }
209        }
210    }
211    out.push_str("\nGenerated by fallow.");
212    out
213}
214
215/// Rule ids whose findings describe project-wide config state rather than a
216/// change touching a specific source line.
217pub const PROJECT_LEVEL_RULE_IDS: &[&str] = &[
218    "fallow/unused-catalog-entry",
219    "fallow/empty-catalog-group",
220    "fallow/unresolved-catalog-reference",
221    "fallow/unused-dependency-override",
222    "fallow/misconfigured-dependency-override",
223    "fallow/unused-dependency",
224    "fallow/unused-dev-dependency",
225    "fallow/unused-optional-dependency",
226    "fallow/type-only-dependency",
227    "fallow/test-only-dependency",
228];
229
230#[must_use]
231pub fn is_project_level_rule(rule_id: &str) -> bool {
232    PROJECT_LEVEL_RULE_IDS.contains(&rule_id)
233}
234
235const CATEGORY_ORDER: [&str; 6] = [
236    "Dead code",
237    "Dependencies",
238    "Duplication",
239    "Health",
240    "Architecture",
241    "Suppressions",
242];
243
244fn group_by_category<'a>(
245    issues: &'a [CiIssue],
246    category_for_rule: &dyn Fn(&str) -> &'static str,
247) -> Vec<(&'static str, Vec<&'a CiIssue>)> {
248    let mut buckets: std::collections::BTreeMap<&'static str, Vec<&CiIssue>> =
249        std::collections::BTreeMap::new();
250    for issue in issues {
251        let category = category_for_rule(&issue.rule_id);
252        buckets.entry(category).or_default().push(issue);
253    }
254    let mut ordered: Vec<(&'static str, Vec<&CiIssue>)> = Vec::with_capacity(buckets.len());
255    for category in CATEGORY_ORDER {
256        if let Some(items) = buckets.remove(category) {
257            ordered.push((category, items));
258        }
259    }
260    for (category, items) in buckets {
261        ordered.push((category, items));
262    }
263    ordered
264}
265
266#[must_use]
267pub fn summary_label(category: &str, total: usize, max: usize) -> String {
268    if total > max {
269        format!("{category} ({total}, showing {max})")
270    } else {
271        format!("{category} ({total})")
272    }
273}
274
275#[expect(clippy::expect_used, reason = "formatting into String is infallible")]
276fn render_findings_table(out: &mut String, issues: &[&CiIssue], max: usize, summary: &str) {
277    writeln!(out, "<details>\n<summary>{summary}</summary>\n").expect("write to string");
278    out.push_str("| Severity | Rule | Location | Description |\n");
279    out.push_str("| --- | --- | --- | --- |\n");
280    for issue in issues.iter().take(max) {
281        writeln!(
282            out,
283            "| {} | `{}` | `{}`:{} | {} |",
284            escape_md(&issue.severity),
285            escape_md(&issue.rule_id),
286            escape_md(&issue.path),
287            issue.line,
288            escape_md(&issue.description),
289        )
290        .expect("write to string");
291    }
292    if issues.len() > max {
293        writeln!(
294            out,
295            "\nShowing {max} of {} findings. Run fallow locally or inspect the CI output for the full report.",
296            issues.len(),
297        )
298        .expect("write to string");
299    }
300    out.push_str("\n</details>\n\n");
301}
302
303#[must_use]
304pub fn command_title(command: &str) -> &'static str {
305    match command {
306        "dead-code" | "check" => "dead-code report",
307        "dupes" => "duplication report",
308        "health" => "health report",
309        "audit" => "audit report",
310        "" | "combined" => "combined report",
311        _ => "report",
312    }
313}
314
315/// Escape a string for inclusion in a Markdown table cell.
316#[must_use]
317pub fn escape_md(value: &str) -> String {
318    let collapsed = value.replace('\n', " ");
319    let mut out = String::with_capacity(collapsed.len());
320    for ch in collapsed.chars() {
321        if matches!(
322            ch,
323            '\\' | '`'
324                | '*'
325                | '_'
326                | '['
327                | ']'
328                | '('
329                | ')'
330                | '!'
331                | '<'
332                | '>'
333                | '#'
334                | '|'
335                | '~'
336                | '&'
337        ) {
338            out.push('\\');
339        }
340        out.push(ch);
341    }
342    out.trim().to_owned()
343}
344
345/// Render a provider-specific review envelope from typed CI issues.
346#[must_use]
347pub fn render_review_envelope(input: &ReviewEnvelopeRenderInput<'_>) -> ReviewEnvelopeRenderResult {
348    let grouped = group_review_issues_by_path_line(input.issues, input.max_comments);
349
350    let comments: Vec<ReviewComment> = grouped
351        .groups
352        .iter()
353        .map(|group| {
354            render_review_comment_for_group(&ReviewCommentRenderInput {
355                provider: input.provider,
356                group,
357                gitlab_diff_refs: input.gitlab_diff_refs,
358                diff_index: input.diff_index,
359                include_guidance: input.include_guidance,
360                suggestion_block: input.suggestion_block,
361                guidance_block: input.guidance_block,
362            })
363        })
364        .collect();
365
366    let summary_text = format!(
367        "### Fallow {}\n\n{} inline finding{} selected for {} review.\n\n<!-- fallow-review -->",
368        command_title(input.command),
369        comments.len(),
370        if comments.len() == 1 { "" } else { "s" },
371        input.provider.name(),
372    );
373    let summary_fp = summary_fingerprint(&summary_text);
374    let summary_marker = format!("\n\n{MARKER_PREFIX_V2}{summary_fp}{MARKER_SUFFIX_V2}");
375    let body = format!("{summary_text}{summary_marker}");
376    let summary = ReviewEnvelopeSummary {
377        body: body.clone(),
378        fingerprint: summary_fp,
379    };
380
381    let truncation = ReviewEnvelopeTruncation {
382        body: comments.iter().any(review_comment_truncated),
383        comment_limit: grouped.truncated,
384    };
385
386    ReviewEnvelopeRenderResult {
387        envelope: build_review_envelope_output(
388            input.provider,
389            body,
390            summary,
391            comments,
392            input.issues,
393        ),
394        truncation,
395    }
396}
397
398#[derive(Debug, PartialEq, Eq)]
399pub struct GroupedReviewIssues<'a> {
400    pub groups: Vec<Vec<&'a CiIssue>>,
401    pub truncated: bool,
402}
403
404/// Group consecutive same-(path, line) issues. Input is already sorted by
405/// `(path, line, fingerprint)` so a single linear pass collects runs.
406#[must_use]
407pub fn group_review_issues_by_path_line(
408    issues: &[CiIssue],
409    max_groups: usize,
410) -> GroupedReviewIssues<'_> {
411    if max_groups == 0 {
412        return GroupedReviewIssues {
413            groups: Vec::new(),
414            truncated: !issues.is_empty(),
415        };
416    }
417    let mut groups: Vec<Vec<&CiIssue>> = Vec::with_capacity(max_groups.min(issues.len()));
418    let mut current: Vec<&CiIssue> = Vec::new();
419    let mut current_key: Option<(&str, u64)> = None;
420    for issue in issues {
421        let key = (issue.path.as_str(), issue.line);
422        if Some(key) != current_key {
423            if !current.is_empty() {
424                groups.push(std::mem::take(&mut current));
425                if groups.len() == max_groups {
426                    return GroupedReviewIssues {
427                        groups,
428                        truncated: true,
429                    };
430                }
431            }
432            current_key = Some(key);
433        }
434        current.push(issue);
435    }
436    if !current.is_empty() && groups.len() < max_groups {
437        groups.push(current);
438    }
439    GroupedReviewIssues {
440        groups,
441        truncated: false,
442    }
443}
444
445fn review_comment_truncated(comment: &ReviewComment) -> bool {
446    match comment {
447        ReviewComment::GitHub(comment) => comment.truncated,
448        ReviewComment::GitLab(comment) => comment.truncated,
449    }
450}
451
452pub struct ReviewCommentRenderInput<'a, 'group> {
453    pub provider: CiProvider,
454    pub group: &'a [&'group CiIssue],
455    pub gitlab_diff_refs: Option<&'a ReviewGitlabDiffRefs>,
456    pub diff_index: Option<&'a DiffIndex>,
457    pub include_guidance: bool,
458    pub suggestion_block: &'a dyn Fn(CiProvider, &CiIssue) -> Option<String>,
459    pub guidance_block: &'a dyn Fn(&CiIssue) -> Option<String>,
460}
461
462/// Render one comment from a group of issues sharing the same `(path, line)`.
463#[must_use]
464pub fn render_review_comment_for_group(input: &ReviewCommentRenderInput<'_, '_>) -> ReviewComment {
465    assert!(
466        !input.group.is_empty(),
467        "group_review_issues_by_path_line never yields empty"
468    );
469    let representative = input.group[0];
470    let fingerprint = if input.group.len() == 1 {
471        representative.fingerprint.clone()
472    } else {
473        let constituents: Vec<&str> = input.group.iter().map(|i| i.fingerprint.as_str()).collect();
474        composite_fingerprint(&constituents)
475    };
476
477    let content = build_merged_comment_content(input);
478    let marker_line = format!("\n\n{MARKER_PREFIX_V2}{fingerprint}{MARKER_SUFFIX_V2}");
479    let (body, truncated) = cap_body_with_marker(&content, &marker_line);
480
481    build_review_comment(ReviewCommentInput {
482        provider: input.provider,
483        representative,
484        gitlab_diff_refs: input.gitlab_diff_refs,
485        diff_index: input.diff_index,
486        body,
487        fingerprint,
488        truncated,
489    })
490}
491
492#[expect(clippy::expect_used, reason = "formatting into String is infallible")]
493fn build_merged_comment_content(input: &ReviewCommentRenderInput<'_, '_>) -> String {
494    let mut content = String::new();
495    for (index, issue) in input.group.iter().enumerate() {
496        let label = review_label_from_codeclimate(&issue.severity);
497        if index > 0 {
498            content.push_str("\n\n");
499        }
500        write!(
501            content,
502            "**{}** `{}`: {}",
503            label,
504            escape_md(&issue.rule_id),
505            escape_md(&issue.description)
506        )
507        .expect("write to String is infallible");
508        if let Some(suggestion) = (input.suggestion_block)(input.provider, issue) {
509            content.push_str(&suggestion);
510        }
511        if input.include_guidance
512            && let Some(guidance) = (input.guidance_block)(issue)
513        {
514            content.push_str(&guidance);
515        }
516    }
517    content
518}
519
520struct ReviewCommentInput<'a> {
521    provider: CiProvider,
522    representative: &'a CiIssue,
523    gitlab_diff_refs: Option<&'a ReviewGitlabDiffRefs>,
524    diff_index: Option<&'a DiffIndex>,
525    body: String,
526    fingerprint: String,
527    truncated: bool,
528}
529
530fn build_review_comment(input: ReviewCommentInput<'_>) -> ReviewComment {
531    let ReviewCommentInput {
532        provider,
533        representative,
534        gitlab_diff_refs,
535        diff_index,
536        body,
537        fingerprint,
538        truncated,
539    } = input;
540    match provider {
541        CiProvider::Github => ReviewComment::GitHub(GitHubReviewComment {
542            path: representative.path.clone(),
543            line: u32::try_from(representative.line).unwrap_or(u32::MAX),
544            side: GitHubReviewSide::Right,
545            body,
546            fingerprint,
547            truncated,
548        }),
549        CiProvider::Gitlab => {
550            let new_path = representative.path.clone();
551            let old_path = diff_index
552                .and_then(|di| di.old_path_for(&new_path))
553                .map_or_else(|| new_path.clone(), str::to_owned);
554            let position = GitLabReviewPosition {
555                base_sha: gitlab_diff_refs.map(|r| r.base_sha.clone()),
556                start_sha: gitlab_diff_refs.map(|r| r.start_sha.clone()),
557                head_sha: gitlab_diff_refs.map(|r| r.head_sha.clone()),
558                position_type: GitLabReviewPositionType::Text,
559                old_path,
560                new_path,
561                new_line: u32::try_from(representative.line).unwrap_or(u32::MAX),
562            };
563            ReviewComment::GitLab(GitLabReviewComment {
564                body,
565                position,
566                fingerprint,
567                truncated,
568            })
569        }
570    }
571}
572
573#[must_use]
574pub fn cap_body_with_marker(content: &str, marker_line: &str) -> (String, bool) {
575    let intact_len = content.len() + marker_line.len();
576    if intact_len <= MAX_COMMENT_BODY_BYTES {
577        let mut out = String::with_capacity(intact_len);
578        out.push_str(content);
579        out.push_str(marker_line);
580        return (out, false);
581    }
582    let reserved = marker_line.len() + TRUNCATION_SUFFIX.len();
583    let budget = MAX_COMMENT_BODY_BYTES.saturating_sub(reserved);
584    let mut cut = budget.min(content.len());
585    while cut > 0 && !content.is_char_boundary(cut) {
586        cut -= 1;
587    }
588    let mut out = String::with_capacity(MAX_COMMENT_BODY_BYTES);
589    out.push_str(&content[..cut]);
590    out.push_str(TRUNCATION_SUFFIX);
591    out.push_str(marker_line);
592    (out, true)
593}
594
595#[must_use]
596pub const fn review_label_from_codeclimate(severity_name: &str) -> &'static str {
597    match severity_name.as_bytes() {
598        b"major" | b"critical" | b"blocker" => "error",
599        _ => "warn",
600    }
601}
602
603#[must_use]
604pub fn github_check_conclusion(issues: &[CiIssue]) -> ReviewCheckConclusion {
605    if issues
606        .iter()
607        .any(|issue| matches!(issue.severity.as_str(), "major" | "critical" | "blocker"))
608    {
609        ReviewCheckConclusion::Failure
610    } else if issues.is_empty() {
611        ReviewCheckConclusion::Success
612    } else {
613        ReviewCheckConclusion::Neutral
614    }
615}
616
617fn build_review_envelope_output(
618    provider: CiProvider,
619    body: String,
620    summary: ReviewEnvelopeSummary,
621    comments: Vec<ReviewComment>,
622    issues: &[CiIssue],
623) -> ReviewEnvelopeOutput {
624    match provider {
625        CiProvider::Github => ReviewEnvelopeOutput {
626            event: Some(ReviewEnvelopeEvent::Comment),
627            body,
628            summary,
629            comments,
630            marker_regex: default_marker_regex(),
631            marker_regex_flags: default_marker_regex_flags(),
632            meta: ReviewEnvelopeMeta {
633                schema: ReviewEnvelopeSchema::V2,
634                provider: ReviewProvider::Github,
635                check_conclusion: Some(github_check_conclusion(issues)),
636            },
637        },
638        CiProvider::Gitlab => ReviewEnvelopeOutput {
639            event: None,
640            body,
641            summary,
642            comments,
643            marker_regex: default_marker_regex(),
644            marker_regex_flags: default_marker_regex_flags(),
645            meta: ReviewEnvelopeMeta {
646                schema: ReviewEnvelopeSchema::V2,
647                provider: ReviewProvider::Gitlab,
648                check_conclusion: None,
649            },
650        },
651    }
652}
653
654#[must_use]
655pub fn summary_fingerprint(body: &str) -> String {
656    fingerprint_hash(&[body])
657}
658
659#[must_use]
660pub fn composite_fingerprint(constituents: &[&str]) -> String {
661    let mut sorted: Vec<&str> = constituents.to_vec();
662    sorted.sort_unstable();
663    let joined = sorted.join(":");
664    format!("merged:{}", fingerprint_hash(&[joined.as_str()]))
665}
666
667#[cfg(test)]
668mod tests {
669    use super::*;
670    use crate::{CodeClimateIssueKind, CodeClimateLines, CodeClimateLocation};
671
672    fn category_for_rule(rule_id: &str) -> &'static str {
673        match rule_id {
674            "fallow/code-duplication" => "Duplication",
675            "fallow/high-complexity" => "Health",
676            "fallow/unused-dependency" => "Dependencies",
677            _ => "Dead code",
678        }
679    }
680
681    #[test]
682    fn extracts_issues_from_codeclimate() {
683        let value = serde_json::json!([{
684            "check_name": "fallow/unused-export",
685            "description": "Export x is never imported",
686            "severity": "minor",
687            "fingerprint": "abc",
688            "location": { "path": "src/a.ts", "lines": { "begin": 7 } }
689        }]);
690        let issues = issues_from_codeclimate(&value);
691        assert_eq!(issues.len(), 1);
692        assert_eq!(issues[0].path, "src/a.ts");
693        assert_eq!(issues[0].line, 7);
694    }
695
696    #[test]
697    fn typed_codeclimate_issues_extract_like_json_codeclimate() {
698        let severities = [
699            (CodeClimateSeverity::Info, "info"),
700            (CodeClimateSeverity::Minor, "minor"),
701            (CodeClimateSeverity::Major, "major"),
702            (CodeClimateSeverity::Critical, "critical"),
703            (CodeClimateSeverity::Blocker, "blocker"),
704        ];
705        let typed = severities
706            .iter()
707            .enumerate()
708            .map(|(index, (severity, _))| CodeClimateIssue {
709                kind: CodeClimateIssueKind::Issue,
710                check_name: format!("fallow/rule-{index}"),
711                description: format!("Finding {index}"),
712                categories: vec!["Complexity".to_owned()],
713                severity: *severity,
714                fingerprint: format!("fp-{index}"),
715                location: CodeClimateLocation {
716                    path: format!("src/{index}.ts"),
717                    lines: CodeClimateLines {
718                        begin: u32::try_from(index + 1).expect("small fixture index"),
719                    },
720                },
721                owner: None,
722                group: None,
723            })
724            .collect::<Vec<_>>();
725        let value = serde_json::to_value(&typed).expect("typed fixture serializes");
726
727        assert_eq!(
728            issues_from_codeclimate_issues(&typed),
729            issues_from_codeclimate(&value)
730        );
731        let typed_labels = issues_from_codeclimate_issues(&typed)
732            .into_iter()
733            .map(|issue| issue.severity)
734            .collect::<Vec<_>>();
735        let expected_labels = severities
736            .iter()
737            .map(|(_, label)| (*label).to_owned())
738            .collect::<Vec<_>>();
739        assert_eq!(typed_labels, expected_labels);
740    }
741
742    #[test]
743    fn renders_default_empty_comment() {
744        let body = render_pr_comment(&PrCommentRenderInput {
745            command: "check",
746            provider: CiProvider::Github,
747            issues: &[],
748            marker_id: "fallow-results".to_owned(),
749            max_comments: 50,
750            category_for_rule: &category_for_rule,
751        });
752        assert!(body.contains("<!-- fallow-id: fallow-results"));
753        assert!(body.contains("No GitHub PR/MR findings."));
754    }
755
756    #[test]
757    fn escape_md_escapes_inline_commonmark_specials() {
758        let raw = "foo*bar_baz [a](u) `c` <h> #x !i ~s | p";
759        let escaped = escape_md(raw);
760        for ch in [
761            '*', '_', '[', ']', '(', ')', '`', '<', '>', '#', '!', '~', '|',
762        ] {
763            let raw_count = raw.chars().filter(|c| c == &ch).count();
764            let escaped_count = escaped.matches(&format!("\\{ch}")).count();
765            assert_eq!(
766                raw_count, escaped_count,
767                "char {ch:?}: raw {raw_count} occurrences, escaped {escaped_count} in {escaped:?}"
768            );
769        }
770    }
771
772    #[test]
773    fn escape_md_escapes_ampersand_to_block_numeric_entity_bypass() {
774        let raw = "value &#42;suspicious&#42; here";
775        let escaped = escape_md(raw);
776        assert!(escaped.contains(r"\&"), "got: {escaped}");
777        assert!(escaped.contains(r"\#"), "got: {escaped}");
778        assert!(!escaped.contains(" *suspicious"), "got: {escaped}");
779    }
780
781    #[test]
782    fn summary_label_foreshadows_truncation() {
783        assert_eq!(
784            summary_label("Duplication", 160, 50),
785            "Duplication (160, showing 50)"
786        );
787        assert_eq!(summary_label("Health", 12, 50), "Health (12)");
788        assert_eq!(summary_label("Dependencies", 50, 50), "Dependencies (50)");
789    }
790
791    #[test]
792    fn escape_md_does_not_escape_block_only_markers() {
793        let raw = "fallow/test-only-dependency package.json:12";
794        let escaped = escape_md(raw);
795        assert!(!escaped.contains("\\-"), "should not escape `-`");
796        assert!(!escaped.contains("\\."), "should not escape `.`");
797        assert_eq!(escaped, raw);
798    }
799
800    #[test]
801    fn escape_md_collapses_newlines_to_spaces() {
802        let raw = "first\nsecond\nthird";
803        assert_eq!(escape_md(raw), "first second third");
804    }
805
806    #[test]
807    fn escape_md_leaves_safe_chars_unchanged() {
808        let raw = "Export 'helperFn' is never imported by other modules";
809        assert_eq!(
810            escape_md(raw),
811            r"Export 'helperFn' is never imported by other modules"
812        );
813    }
814
815    #[test]
816    fn is_project_level_rule_covers_config_anchored_dependency_findings() {
817        for rule_id in PROJECT_LEVEL_RULE_IDS {
818            assert!(
819                is_project_level_rule(rule_id),
820                "{rule_id} must be project-level"
821            );
822        }
823        for rule_id in [
824            "fallow/unused-file",
825            "fallow/unused-export",
826            "fallow/unused-type",
827            "fallow/unused-enum-member",
828            "fallow/unused-class-member",
829            "fallow/unused-store-member",
830            "fallow/unresolved-import",
831            "fallow/unlisted-dependency",
832            "fallow/duplicate-export",
833            "fallow/circular-dependency",
834            "fallow/re-export-cycle",
835            "fallow/boundary-violation",
836            "fallow/stale-suppression",
837            "fallow/private-type-leak",
838            "fallow/high-complexity",
839            "fallow/high-crap-score",
840        ] {
841            assert!(
842                !is_project_level_rule(rule_id),
843                "{rule_id} must NOT be project-level"
844            );
845        }
846    }
847
848    #[test]
849    fn escape_md_double_apply_is_safe() {
850        let raw = "code with `backticks` and *stars*";
851        let once = escape_md(raw);
852        let twice = escape_md(&once);
853        assert!(twice.contains(r"\\"));
854    }
855}