|
| 1 | +//! Integration coverage for `socket_patch_cli::output` helpers. |
| 2 | +//! The pub `format_severity` and `color` functions are widely used |
| 3 | +//! by `commands/scan.rs` + `commands/list.rs` for human-mode display, |
| 4 | +//! but the integration test suite runs all its scan/list tests in |
| 5 | +//! `--json` mode (which suppresses the colour wrappers entirely), so |
| 6 | +//! every ANSI branch was uncovered. These tests drive each branch |
| 7 | +//! directly via the lib's pub API. |
| 8 | +
|
| 9 | +use socket_patch_cli::output::{color, format_severity}; |
| 10 | + |
| 11 | +#[test] |
| 12 | +fn format_severity_no_color_returns_input_verbatim() { |
| 13 | + assert_eq!(format_severity("critical", false), "critical"); |
| 14 | + assert_eq!(format_severity("high", false), "high"); |
| 15 | + assert_eq!(format_severity("medium", false), "medium"); |
| 16 | + assert_eq!(format_severity("low", false), "low"); |
| 17 | + assert_eq!(format_severity("unknown", false), "unknown"); |
| 18 | +} |
| 19 | + |
| 20 | +#[test] |
| 21 | +fn format_severity_critical_wraps_in_red() { |
| 22 | + let out = format_severity("critical", true); |
| 23 | + assert!(out.contains("\x1b[31m"), "expected red ANSI 31m; got {out:?}"); |
| 24 | + assert!(out.ends_with("\x1b[0m")); |
| 25 | + assert!(out.contains("critical")); |
| 26 | +} |
| 27 | + |
| 28 | +#[test] |
| 29 | +fn format_severity_high_wraps_in_bright_red() { |
| 30 | + let out = format_severity("high", true); |
| 31 | + assert!(out.contains("\x1b[91m"), "expected bright-red 91m; got {out:?}"); |
| 32 | +} |
| 33 | + |
| 34 | +#[test] |
| 35 | +fn format_severity_medium_wraps_in_yellow() { |
| 36 | + let out = format_severity("medium", true); |
| 37 | + assert!(out.contains("\x1b[33m"), "expected yellow 33m; got {out:?}"); |
| 38 | +} |
| 39 | + |
| 40 | +#[test] |
| 41 | +fn format_severity_low_wraps_in_cyan() { |
| 42 | + let out = format_severity("low", true); |
| 43 | + assert!(out.contains("\x1b[36m"), "expected cyan 36m; got {out:?}"); |
| 44 | +} |
| 45 | + |
| 46 | +#[test] |
| 47 | +fn format_severity_unknown_passes_through_unwrapped() { |
| 48 | + // The `_` arm returns the input verbatim — no ANSI wrapper. |
| 49 | + let out = format_severity("nonsense", true); |
| 50 | + assert!(!out.contains("\x1b["), "unknown severity must not wrap: {out:?}"); |
| 51 | + assert_eq!(out, "nonsense"); |
| 52 | +} |
| 53 | + |
| 54 | +#[test] |
| 55 | +fn format_severity_case_insensitive() { |
| 56 | + // The lowercase match must apply to mixed-case input. |
| 57 | + assert!(format_severity("CRITICAL", true).contains("\x1b[31m")); |
| 58 | + assert!(format_severity("High", true).contains("\x1b[91m")); |
| 59 | + assert!(format_severity("MEDIUM", true).contains("\x1b[33m")); |
| 60 | + assert!(format_severity("Low", true).contains("\x1b[36m")); |
| 61 | +} |
| 62 | + |
| 63 | +#[test] |
| 64 | +fn color_with_use_color_false_returns_input() { |
| 65 | + assert_eq!(color("text", "31", false), "text"); |
| 66 | +} |
| 67 | + |
| 68 | +#[test] |
| 69 | +fn color_with_use_color_true_wraps_with_code() { |
| 70 | + let out = color("text", "31", true); |
| 71 | + assert_eq!(out, "\x1b[31mtext\x1b[0m"); |
| 72 | +} |
| 73 | + |
| 74 | +#[test] |
| 75 | +fn color_with_empty_text_still_wraps() { |
| 76 | + // Edge case: empty input still gets the ANSI envelope when |
| 77 | + // colour is enabled. |
| 78 | + let out = color("", "31", true); |
| 79 | + assert_eq!(out, "\x1b[31m\x1b[0m"); |
| 80 | +} |
0 commit comments