1
use super::*;
2
use server::command::CmdResult;
3
use sqlx::types::Uuid;
4
use std::fmt::Debug;
5
use std::future::Future;
6
use std::pin::Pin;
7

            
8
#[derive(Debug, Default)]
9
struct StubRunnable {
10
    result_lines: Option<Vec<String>>,
11
}
12

            
13
impl CliRunnable for StubRunnable {
14
2
    fn run<'a>(
15
2
        &'a self,
16
2
        _args: &'a HashMap<&str, &Argument>,
17
2
    ) -> Pin<Box<dyn Future<Output = Result<Option<CmdResult>, CommandError>> + Send + 'a>> {
18
2
        let out = self.result_lines.clone().map(CmdResult::Lines);
19
2
        Box::pin(async move { Ok(out) })
20
2
    }
21
}
22

            
23
#[tokio::test]
24
1
async fn run_and_print_accepts_empty_result() -> Result<(), CommandError> {
25
1
    let stub = StubRunnable { result_lines: None };
26
1
    run_and_print(&stub, HashMap::new()).await
27
1
}
28

            
29
#[tokio::test]
30
1
async fn run_and_print_handles_lines_result() -> Result<(), CommandError> {
31
1
    let stub = StubRunnable {
32
1
        result_lines: Some(vec!["a".to_string(), "b".to_string()]),
33
1
    };
34
1
    run_and_print(&stub, HashMap::new()).await
35
1
}
36

            
37
#[tokio::test]
38
1
async fn run_and_print_forwards_runnable_error() {
39
    #[derive(Debug)]
40
    struct Failing;
41
    impl CliRunnable for Failing {
42
1
        fn run<'a>(
43
1
            &'a self,
44
1
            _args: &'a HashMap<&str, &Argument>,
45
1
        ) -> Pin<Box<dyn Future<Output = Result<Option<CmdResult>, CommandError>> + Send + 'a>>
46
        {
47
1
            Box::pin(async move { Err(CommandError::Argument("boom".to_string())) })
48
1
        }
49
    }
50
1
    let err = run_and_print(&Failing, HashMap::new())
51
1
        .await
52
1
        .expect_err("should bubble error");
53
1
    assert!(matches!(err, CommandError::Argument(_)));
54
1
}
55

            
56
3
fn finding(kind: &str, detail: &str) -> DataFinding {
57
3
    DataFinding {
58
3
        id: Uuid::nil(),
59
3
        kind: kind.to_string(),
60
3
        subjects: vec![Uuid::nil(), Uuid::nil()],
61
3
        detail: detail.to_string(),
62
3
        created_at: sqlx::types::chrono::DateTime::UNIX_EPOCH,
63
3
    }
64
3
}
65

            
66
#[test]
67
1
fn finding_lines_say_when_there_are_none() {
68
1
    assert_eq!(finding_lines(&[]), ["no findings"]);
69
1
}
70

            
71
#[test]
72
1
fn finding_lines_group_rows_under_their_kind() {
73
1
    let lines = finding_lines(&[
74
1
        finding("price-link", "one"),
75
1
        finding("price-link", "two"),
76
1
        finding("tag-conflict", "three"),
77
1
    ]);
78
1
    assert_eq!(lines.len(), 5);
79
1
    assert_eq!(lines[0], "price-link (2)");
80
1
    assert_eq!(lines[3], "tag-conflict (1)");
81
1
    assert!(lines[4].contains("three"));
82
1
    assert!(
83
1
        lines[1]
84
1
            .contains("00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000")
85
    );
86
1
}
87

            
88
#[test]
89
1
fn a_merge_line_is_the_kept_id_and_the_moved_count() {
90
1
    let into = Uuid::from_u128(7);
91
1
    assert_eq!(merged_line(into, 3), format!("{into}\t3"));
92
1
}