use crate::report::Finding;
pub const MAX_SCORE: f32 = 10.0;
#[must_use]
pub fn score_findings(findings: &[Finding]) -> f32 {
if findings.is_empty() {
return 0.0;
}
let total_weight: f32 = findings.iter().map(|f| f.severity.weight()).sum();
let max_severity_weight = findings
.iter()
.map(|f| f.severity.weight())
.fold(0.0_f32, f32::max);
let bonus = (1.0 + total_weight - max_severity_weight).ln().max(0.0);
let raw = max_severity_weight + bonus;
raw.clamp(0.0, MAX_SCORE)
}
#[must_use]
pub fn exceeds(score: f32, threshold: f32) -> bool {
score >= threshold
}
#[cfg(test)]
mod tests {
use super::*;
use crate::report::{Finding, RiskCategory, Severity};
fn finding(sev: Severity) -> Finding {
Finding {
category: RiskCategory::Other,
severity: sev,
file: "x.rs".to_string(),
line: Some(1),
message: "test".to_string(),
}
}
#[test]
fn empty_is_zero() {
assert_eq!(score_findings(&[]), 0.0);
}
#[test]
fn single_critical_exceeds_threshold_seven() {
let s = score_findings(&[finding(Severity::Critical)]);
assert!(s >= 7.0, "critical must exceed threshold 7, got {s}");
}
#[test]
fn single_low_below_threshold_seven() {
let s = score_findings(&[finding(Severity::Low)]);
assert!(s < 7.0, "single low must be below threshold 7, got {s}");
}
#[test]
fn one_critical_beats_many_mediums() {
let crit = score_findings(&[finding(Severity::Critical)]);
let mediums = score_findings(&[
finding(Severity::Medium),
finding(Severity::Medium),
finding(Severity::Medium),
]);
assert!(crit > mediums);
}
#[test]
fn score_clamped_to_max() {
let flood: Vec<Finding> = (0..50).map(|_| finding(Severity::Critical)).collect();
assert!(score_findings(&flood) <= MAX_SCORE);
}
#[test]
fn more_findings_never_lowers_score() {
let one = score_findings(&[finding(Severity::High)]);
let two = score_findings(&[finding(Severity::High), finding(Severity::Low)]);
assert!(two >= one);
}
}