1
use super::*;
2

            
3
mod mux;
4

            
5
#[tokio::test]
6
1
async fn the_recording_link_receives_the_enveloped_form_and_sends_no_reply() {
7
1
    let (mut eval, sent) = ConsoleEval::recording();
8
1
    eval.submit(MAIN_STREAM, "(foo)".to_string());
9
1
    assert_eq!(sent.frames(), ["(:id 1 :form (foo))"]);
10
1
    assert!(eval.drain().is_empty());
11
1
}
12

            
13
#[tokio::test]
14
1
async fn a_request_for_another_stream_names_it_in_the_envelope() {
15
1
    let (mut eval, sent) = ConsoleEval::recording();
16
1
    eval.submit(3, "(foo)".to_string());
17
1
    assert_eq!(sent.frames(), ["(:id 1 :stream 3 :form (foo))"]);
18
1
}
19

            
20
#[tokio::test]
21
1
async fn interrupt_and_close_without_a_running_eval_do_not_panic() {
22
1
    let (mut eval, _sent) = ConsoleEval::recording();
23
1
    eval.interrupt(MAIN_STREAM);
24
1
    eval.close(3);
25
1
    assert!(eval.drain().is_empty());
26
1
}
27

            
28
#[tokio::test]
29
1
async fn submit_after_the_ids_ran_out_reports_failure() {
30
1
    let mut eval = ConsoleEval::broken();
31
1
    assert!(eval.submit(MAIN_STREAM, "(x)".to_string()).is_none());
32
1
}
33

            
34
#[tokio::test]
35
1
async fn submit_returns_monotonic_ids() {
36
1
    let (mut eval, _sent) = ConsoleEval::recording();
37
1
    let ids: Vec<Option<i64>> = ["(a)", "(b)", "(c)"]
38
1
        .into_iter()
39
3
        .map(|form| eval.submit(MAIN_STREAM, form.to_string()))
40
        .collect();
41
1
    assert_eq!(ids, [Some(1), Some(2), Some(3)]);
42
1
}
43

            
44
#[tokio::test]
45
1
async fn a_reply_that_is_not_a_response_is_unroutable() {
46
1
    let (mut eval, replies) = ConsoleEval::scripted();
47
1
    eval.submit(MAIN_STREAM, "(x)".to_string());
48
1
    assert!(replies.try_send("(:id 1 :form (x))".to_string()).is_ok());
49
1
    let items = eval.drain();
50
1
    assert!(
51
1
        matches!(items.as_slice(), [DrainItem::Unroutable(_)]),
52
1
        "{items:?}"
53
1
    );
54
1
}
55

            
56
#[test]
57
1
fn classify_response_positive_id_is_routed() {
58
1
    let item = classify_response("(:id 5 :value 42)".to_string());
59
1
    assert!(matches!(item, DrainItem::Routed { id: 5, .. }), "{item:?}");
60
1
}
61

            
62
#[test]
63
1
fn classify_response_id_zero_is_unroutable() {
64
1
    let item = classify_response("(:id 0 :value 42)".to_string());
65
1
    assert!(matches!(item, DrainItem::Unroutable(_)), "{item:?}");
66
1
}
67

            
68
#[test]
69
1
fn classify_response_parse_error_is_unroutable() {
70
1
    let item = classify_response("((((".to_string());
71
1
    assert!(matches!(item, DrainItem::Unroutable(_)), "{item:?}");
72
1
}
73

            
74
#[test]
75
1
fn classify_response_string_id_is_unroutable() {
76
1
    let item = classify_response(r#"(:id "abc" :value 1)"#.to_string());
77
1
    assert!(matches!(item, DrainItem::Unroutable(_)), "{item:?}");
78
1
}
79

            
80
#[tokio::test]
81
1
async fn each_recording_console_keeps_its_own_frames() {
82
1
    let (mut first, first_sent) = ConsoleEval::recording();
83
1
    let (mut second, second_sent) = ConsoleEval::recording();
84
1
    first.submit(MAIN_STREAM, "(a)".to_string());
85
1
    second.submit(MAIN_STREAM, "(b)".to_string());
86
1
    assert_eq!(first_sent.frames(), ["(:id 1 :form (a))"]);
87
1
    assert_eq!(second_sent.frames(), ["(:id 1 :form (b))"]);
88
1
}