1
//! A value the server sends must be one the client reader accepts: the decoder
2
//! ceiling is `MAX_WIRE_LIST_DEPTH`, and one level deeper is a server error.
3

            
4
use scripting::MAX_WIRE_LIST_DEPTH;
5

            
6
use super::*;
7

            
8
const WIDEN: &str = "(> (rpc-protocol-version) 999)";
9

            
10
/// `n` nested lists built at runtime around `seed`; the `or` forces `AnyRef`.
11
4
fn labels_nest(n: usize, seed: &str) -> String {
12
4
    format!(
13
        "(labels ((build (n acc) \
14
4
             (if (<= n 0) acc (build (- n 1) (or {WIDEN} (list acc)))))) \
15
4
           (build {n} {seed}))"
16
    )
17
4
}
18

            
19
4
fn anyref_nest(n: usize) -> String {
20
4
    labels_nest(n, "nil")
21
4
}
22

            
23
/// A fixed `PairRef` result whose one car is the deeper `AnyRef` chain.
24
2
fn fixed_nest(n: usize) -> String {
25
2
    format!("(let ((r {})) (list r))", anyref_nest(n - 1))
26
2
}
27

            
28
4
async fn parsed(form: &str) -> ResponsePayload {
29
4
    let wire = eval_value_of(form).await;
30
4
    crate::parse_response(&wire)
31
        .unwrap_or_else(|e| panic!("client cannot read {form}: {e}\n{wire}"))
32
        .payload
33
4
}
34

            
35
2
async fn assert_ceiling(build: impl Fn(usize) -> String) {
36
2
    let ok = parsed(&build(MAX_WIRE_LIST_DEPTH)).await;
37
2
    assert!(matches!(ok, ResponsePayload::Value(_)), "{ok:?}");
38
2
    let over = parsed(&build(MAX_WIRE_LIST_DEPTH + 1)).await;
39
2
    assert!(
40
2
        matches!(&over, ResponsePayload::Error { message, .. } if message.contains("depth limit")),
41
        "{over:?}"
42
    );
43
2
}
44

            
45
#[tokio::test]
46
1
async fn a_fixed_pair_result_is_read_back_at_the_ceiling() {
47
1
    assert_ceiling(fixed_nest).await;
48
1
}
49

            
50
#[tokio::test]
51
1
async fn an_anyref_result_is_read_back_at_the_ceiling() {
52
1
    assert_ceiling(anyref_nest).await;
53
1
}