scope-bca-core 0.8.6

Scope Blockchain Analysis - Core library for blockchain data analysis
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Display formatting for compliance reports

use crate::compliance::risk::RiskAssessment;
// Note: Using simple table formatting for now
// For production, add comfy_table to Cargo.toml

/// Output format options
#[derive(Clone, Copy, Debug, Default, clap::ValueEnum)]
pub enum OutputFormat {
    #[default]
    Table,
    Json,
    Yaml,
    Markdown,
}

/// Format a risk assessment report
pub fn format_risk_report(
    assessment: &RiskAssessment,
    format: OutputFormat,
    detailed: bool,
) -> String {
    match format {
        OutputFormat::Table => format_risk_table(assessment, detailed),
        OutputFormat::Json => serde_json::to_string_pretty(assessment).unwrap_or_default(),
        OutputFormat::Yaml => serde_saphyr::to_string(assessment).unwrap_or_default(),
        OutputFormat::Markdown => format_risk_markdown(assessment, detailed),
    }
}

/// Format as pretty table
fn format_risk_table(assessment: &RiskAssessment, detailed: bool) -> String {
    use crate::display::terminal as t;
    let mut output = String::new();

    // Header
    output.push_str(&t::section_header(&format!(
        "{} Risk Assessment Report",
        assessment.risk_level.emoji()
    )));
    output.push('\n');

    // Summary section
    output.push_str(&t::kv_row("Address", &assessment.address));
    output.push('\n');
    output.push_str(&t::kv_row("Chain", &assessment.chain));
    output.push('\n');
    output.push_str(&t::score_bar(
        "Risk Score",
        (assessment.overall_score * 10.0) as u32,
        100,
    ));
    output.push('\n');
    output.push_str(&t::kv_row(
        "Risk Level",
        &format!(
            "{} {:?}",
            assessment.risk_level.emoji(),
            assessment.risk_level
        ),
    ));
    output.push('\n');
    output.push_str(&t::kv_row(
        "Assessed At",
        &assessment
            .assessed_at
            .format("%Y-%m-%d %H:%M UTC")
            .to_string(),
    ));
    output.push('\n');

    // Risk factors
    if detailed {
        output.push_str(&t::subsection_header("Risk Factor Breakdown"));
        output.push('\n');

        let cols = [
            t::Col {
                label: "Factor",
                width: 25,
                align: '<',
            },
            t::Col {
                label: "Category",
                width: 12,
                align: '<',
            },
            t::Col {
                label: "Score",
                width: 8,
                align: '<',
            },
            t::Col {
                label: "Weight",
                width: 8,
                align: '<',
            },
            t::Col {
                label: "Weighted",
                width: 10,
                align: '<',
            },
        ];

        output.push_str(&t::table_header(&cols));
        output.push('\n');

        for factor in &assessment.factors {
            let weighted = factor.score * factor.weight;
            let factor_name = factor.name.chars().take(24).collect::<String>();
            let category_str = format!("{:?}", factor.category)
                .chars()
                .take(11)
                .collect::<String>();
            let score_str = format!("{:.1}", factor.score);
            let weight_str = format!("{:.0}%", factor.weight * 100.0);
            let weighted_str = format!("{:.2}", weighted);

            output.push_str(&t::table_row(
                &cols,
                &[
                    &factor_name,
                    &category_str,
                    &score_str,
                    &weight_str,
                    &weighted_str,
                ],
            ));
            output.push('\n');
        }
    }

    if !assessment.not_assessed.is_empty() {
        output.push_str(&t::subsection_header(
            "Not Assessed (no data; not in the score)",
        ));
        output.push('\n');
        for reason in &assessment.not_assessed {
            output.push_str(&t::bullet_row(reason));
            output.push('\n');
        }
    }

    // Recommendations
    if !assessment.recommendations.is_empty() {
        output.push_str(&t::subsection_header("Recommendations"));
        output.push('\n');

        for (i, rec) in assessment.recommendations.iter().enumerate() {
            output.push_str(&t::numbered_row(i + 1, rec));
            output.push('\n');
        }
    }

    output.push_str(&t::section_footer());
    output.push('\n');

    output
}

/// Format as markdown report
fn format_risk_markdown(assessment: &RiskAssessment, detailed: bool) -> String {
    let mut md = String::new();

    md.push_str("# Risk Assessment Report\n\n");
    md.push_str(&format!("**Address:** `{}`\n\n", assessment.address));
    md.push_str(&format!("**Chain:** {}\n\n", assessment.chain));
    md.push_str(&format!(
        "**Risk Score:** {:.1}/10\n\n",
        assessment.overall_score
    ));
    md.push_str(&format!(
        "**Risk Level:** {} {:?}\n\n",
        assessment.risk_level.emoji(),
        assessment.risk_level
    ));
    md.push_str(&format!(
        "**Assessed At:** {}\n\n",
        assessment.assessed_at.format("%Y-%m-%d %H:%M UTC")
    ));

    if detailed {
        md.push_str("## Risk Factor Breakdown\n\n");
        md.push_str("| Factor | Category | Score | Weight | Weighted |\n");
        md.push_str("|--------|----------|-------|--------|----------|\n");

        for factor in &assessment.factors {
            let weighted = factor.score * factor.weight;
            md.push_str(&format!(
                "| {} | {:?} | {:.1} | {:.0}% | {:.2} |\n",
                factor.name,
                factor.category,
                factor.score,
                factor.weight * 100.0,
                weighted
            ));
        }

        md.push('\n');

        // Detailed factor descriptions
        md.push_str("## Factor Details\n\n");
        for factor in &assessment.factors {
            md.push_str(&format!("### {} ({:?})\n\n", factor.name, factor.category));
            md.push_str(&format!("{}\n\n", factor.description));

            if !factor.evidence.is_empty() {
                md.push_str("**Evidence:**\n");
                for ev in &factor.evidence {
                    md.push_str(&format!("- {}\n", ev));
                }
                md.push('\n');
            }
        }
    }

    if !assessment.not_assessed.is_empty() {
        md.push_str("## Not Assessed\n\nThese factors had no data. They are not in the score.\n\n");
        for reason in &assessment.not_assessed {
            md.push_str(&format!("- {}\n", reason));
        }
        md.push('\n');
    }

    if !assessment.recommendations.is_empty() {
        md.push_str("## Recommendations\n\n");
        for rec in &assessment.recommendations {
            md.push_str(&format!("- {}\n", rec));
        }
        md.push('\n');
    }

    md.push_str("---\n\n");
    md.push_str("*This report was generated automatically. Always verify data from primary sources before making compliance decisions.*\n");

    md
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::compliance::risk::{RiskAssessment, RiskCategory, RiskFactor, RiskLevel};
    use chrono::Utc;

    fn create_test_assessment() -> RiskAssessment {
        RiskAssessment {
            address: "0x742d35Cc6634C0532925a3b844Bc9e7595f1b3c2".to_string(),
            chain: "ethereum".to_string(),
            overall_score: 4.5,
            risk_level: RiskLevel::Medium,
            factors: vec![
                RiskFactor {
                    name: "Behavioral".to_string(),
                    category: RiskCategory::Behavioral,
                    score: 3.0,
                    weight: 0.25,
                    description: "Test behavioral".to_string(),
                    evidence: vec!["Evidence 1".to_string()],
                },
                RiskFactor {
                    name: "Association".to_string(),
                    category: RiskCategory::Association,
                    score: 6.0,
                    weight: 0.30,
                    description: "Test association".to_string(),
                    evidence: vec!["Evidence 2".to_string()],
                },
            ],
            not_assessed: Vec::new(),
            assessed_at: Utc::now(),
            recommendations: vec!["Monitor closely".to_string()],
        }
    }

    #[test]
    fn test_format_risk_report_table() {
        let assessment = create_test_assessment();
        let output = format_risk_report(&assessment, OutputFormat::Table, false);
        assert!(output.contains("Risk Assessment Report"));
        assert!(output.contains("0x742d35Cc6634C0532925a3b844Bc9e7595f1b3c2"));
        assert!(output.contains("ethereum"));
    }

    #[test]
    fn test_incomplete_report_lists_what_was_not_assessed() {
        // The table wraps at the terminal width, which width tests change.
        let _lock = crate::display::terminal::width_lock();
        // A report without data must say so in every format, not read as Low.
        let mut assessment = create_test_assessment();
        assessment.risk_level = RiskLevel::Incomplete;
        assessment.not_assessed = vec!["Source of Funds: no Etherscan API key".to_string()];
        for format in [OutputFormat::Table, OutputFormat::Markdown] {
            let output = format_risk_report(&assessment, format, false);
            assert!(output.contains("Not Assessed"), "{output}");
            assert!(
                output.contains("Source of Funds: no Etherscan API key"),
                "{output}"
            );
            assert!(output.contains("Incomplete"), "{output}");
        }
        let json = format_risk_report(&assessment, OutputFormat::Json, false);
        assert!(json.contains("\"not_assessed\""), "{json}");
    }

    #[test]
    fn test_format_risk_report_detailed() {
        let assessment = create_test_assessment();
        let output = format_risk_report(&assessment, OutputFormat::Table, true);
        assert!(output.contains("Risk Factor Breakdown"));
        assert!(output.contains("Behavioral"));
        assert!(output.contains("Association"));
    }

    #[test]
    fn test_format_risk_report_json() {
        let assessment = create_test_assessment();
        let output = format_risk_report(&assessment, OutputFormat::Json, false);
        assert!(output.contains("address"));
        assert!(output.contains("ethereum"));
        assert!(output.contains("overall_score"));
    }

    #[test]
    fn test_format_risk_report_yaml() {
        let assessment = create_test_assessment();
        let output = format_risk_report(&assessment, OutputFormat::Yaml, false);
        assert!(output.contains("address:"));
        assert!(output.contains("chain:"));
    }

    #[test]
    fn test_format_risk_report_markdown() {
        let assessment = create_test_assessment();
        let output = format_risk_report(&assessment, OutputFormat::Markdown, true);
        assert!(output.contains("# Risk Assessment Report"));
        assert!(output.contains("## Risk Factor Breakdown"));
        assert!(output.contains("## Recommendations"));
    }

    #[test]
    fn test_format_low_risk() {
        let mut assessment = create_test_assessment();
        assessment.risk_level = RiskLevel::Low;
        assessment.overall_score = 2.0;

        let output = format_risk_report(&assessment, OutputFormat::Table, false);
        assert!(output.contains("🟢"));
    }

    #[test]
    fn test_format_high_risk() {
        let mut assessment = create_test_assessment();
        assessment.risk_level = RiskLevel::High;
        assessment.overall_score = 7.5;

        let output = format_risk_report(&assessment, OutputFormat::Table, false);
        assert!(output.contains("🔴"));
    }

    #[test]
    fn test_format_critical_risk() {
        let mut assessment = create_test_assessment();
        assessment.risk_level = RiskLevel::Critical;
        assessment.overall_score = 9.0;

        let output = format_risk_report(&assessment, OutputFormat::Table, false);
        assert!(output.contains("âš«"));
    }

    #[test]
    fn test_empty_recommendations() {
        let mut assessment = create_test_assessment();
        assessment.recommendations = vec![];

        let output = format_risk_report(&assessment, OutputFormat::Table, false);
        // Should not panic and should not contain recommendations section
        assert!(output.contains("Risk Assessment Report"));
    }

    #[test]
    fn test_markdown_no_detailed() {
        let assessment = create_test_assessment();
        let output = format_risk_report(&assessment, OutputFormat::Markdown, false);
        // Should not contain detailed factor breakdown when detailed=false
        assert!(!output.contains("## Risk Factor Breakdown"));
    }

    // ========================================================================
    // Additional edge case tests
    // ========================================================================

    #[test]
    fn test_format_risk_report_no_factors() {
        let mut assessment = create_test_assessment();
        assessment.factors = vec![];
        // Table, detailed → should still render without factors
        let output = format_risk_report(&assessment, OutputFormat::Table, true);
        assert!(output.contains("Risk Assessment Report"));
        assert!(output.contains("Risk Factor Breakdown"));
    }

    #[test]
    fn test_format_risk_markdown_no_factors() {
        let mut assessment = create_test_assessment();
        assessment.factors = vec![];
        let output = format_risk_report(&assessment, OutputFormat::Markdown, true);
        assert!(output.contains("# Risk Assessment Report"));
        assert!(output.contains("## Risk Factor Breakdown"));
    }

    #[test]
    fn test_format_risk_json_roundtrip() {
        let assessment = create_test_assessment();
        let json = format_risk_report(&assessment, OutputFormat::Json, false);
        // Should be valid JSON
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(
            parsed["address"],
            "0x742d35Cc6634C0532925a3b844Bc9e7595f1b3c2"
        );
        assert_eq!(parsed["chain"], "ethereum");
    }

    #[test]
    fn test_format_risk_yaml_roundtrip() {
        let assessment = create_test_assessment();
        let yaml = format_risk_report(&assessment, OutputFormat::Yaml, false);
        // Should be valid YAML that can be deserialized back
        let parsed: serde_json::Value = serde_saphyr::from_str(&yaml).unwrap();
        assert!(parsed["address"].as_str().unwrap().starts_with("0x742d"));
    }

    #[test]
    fn test_format_risk_table_no_recommendations() {
        let mut assessment = create_test_assessment();
        assessment.recommendations = vec![];
        let output = format_risk_report(&assessment, OutputFormat::Table, false);
        assert!(!output.contains("Recommendations"));
    }

    #[test]
    fn test_format_risk_table_many_recommendations() {
        let mut assessment = create_test_assessment();
        assessment.recommendations = (0..10).map(|i| format!("Recommendation {}", i)).collect();
        let output = format_risk_report(&assessment, OutputFormat::Table, false);
        assert!(output.contains("1."));
        assert!(output.contains("10."));
    }

    #[test]
    fn test_format_risk_markdown_with_evidence() {
        let mut assessment = create_test_assessment();
        assessment.factors[0].evidence = vec![
            "Evidence A".to_string(),
            "Evidence B".to_string(),
            "Evidence C".to_string(),
        ];
        let output = format_risk_report(&assessment, OutputFormat::Markdown, true);
        assert!(output.contains("Evidence A"));
        assert!(output.contains("Evidence B"));
        assert!(output.contains("Evidence C"));
    }

    #[test]
    fn test_format_risk_markdown_empty_evidence() {
        let mut assessment = create_test_assessment();
        for factor in &mut assessment.factors {
            factor.evidence = vec![];
        }
        let output = format_risk_report(&assessment, OutputFormat::Markdown, true);
        // Should not contain "**Evidence:**" section since evidence is empty
        assert!(!output.contains("**Evidence:**"));
    }

    #[test]
    fn test_format_risk_table_long_factor_name() {
        let mut assessment = create_test_assessment();
        assessment.factors[0].name =
            "A Very Long Factor Name That Exceeds 24 Characters".to_string();
        let output = format_risk_report(&assessment, OutputFormat::Table, true);
        // Should be truncated to 24 chars
        assert!(output.contains("A Very Long Factor Name "));
    }

    #[test]
    fn test_all_output_formats_no_panic() {
        let assessment = create_test_assessment();
        for format in [
            OutputFormat::Table,
            OutputFormat::Json,
            OutputFormat::Yaml,
            OutputFormat::Markdown,
        ] {
            for detailed in [true, false] {
                let output = format_risk_report(&assessment, format, detailed);
                assert!(!output.is_empty());
            }
        }
    }

    #[test]
    fn test_output_format_default() {
        let format = OutputFormat::default();
        assert!(matches!(format, OutputFormat::Table));
    }

    #[test]
    fn test_markdown_contains_disclaimer() {
        let assessment = create_test_assessment();
        let output = format_risk_report(&assessment, OutputFormat::Markdown, false);
        assert!(output.contains("generated automatically"));
    }

    #[test]
    fn test_format_all_risk_levels() {
        let mut assessment = create_test_assessment();
        for (level, emoji) in [
            (RiskLevel::Low, "🟢"),
            (RiskLevel::Medium, "🟡"),
            (RiskLevel::High, "🔴"),
            (RiskLevel::Critical, "âš«"),
        ] {
            assessment.risk_level = level;
            let output = format_risk_report(&assessment, OutputFormat::Table, false);
            assert!(output.contains(emoji));
            let md = format_risk_report(&assessment, OutputFormat::Markdown, false);
            assert!(md.contains(emoji));
        }
    }

    #[test]
    fn test_format_risk_markdown_all_categories() {
        let mut assessment = create_test_assessment();
        assessment.factors = vec![
            RiskFactor {
                name: "Behavioral".to_string(),
                category: RiskCategory::Behavioral,
                score: 3.0,
                weight: 0.2,
                description: "Behavioral analysis".to_string(),
                evidence: vec!["evidence".to_string()],
            },
            RiskFactor {
                name: "Association".to_string(),
                category: RiskCategory::Association,
                score: 4.0,
                weight: 0.2,
                description: "Association analysis".to_string(),
                evidence: vec![],
            },
            RiskFactor {
                name: "Source".to_string(),
                category: RiskCategory::Source,
                score: 2.0,
                weight: 0.2,
                description: "Source analysis".to_string(),
                evidence: vec![],
            },
            RiskFactor {
                name: "Destination".to_string(),
                category: RiskCategory::Destination,
                score: 1.0,
                weight: 0.2,
                description: "Destination analysis".to_string(),
                evidence: vec![],
            },
            RiskFactor {
                name: "Entity".to_string(),
                category: RiskCategory::Entity,
                score: 5.0,
                weight: 0.2,
                description: "Entity analysis".to_string(),
                evidence: vec![],
            },
        ];
        let output = format_risk_report(&assessment, OutputFormat::Markdown, true);
        assert!(output.contains("Behavioral"));
        assert!(output.contains("Association"));
        assert!(output.contains("Source"));
        assert!(output.contains("Destination"));
        assert!(output.contains("Entity"));
    }
}