1
//! Acceptance rows tagged phase 6 in `doc/ops/0010-common-lisp-booleans.org`:
2
//! the host decodes the struct/array kinds (ratio, commodity, string, entity,
3
//! nested pairs) riding a heterogeneous `anyref` cell instead of leaving the
4
//! `<anyref>` placeholder, and a nested constant list compiles as a runtime
5
//! list element. `v` stands for `(rpc-protocol-version)`, which is 1.
6

            
7
use super::*;
8

            
9
#[tokio::test]
10
1
async fn mixed_list_of_index_ratio_string_t_and_nil_renders_element_by_element() {
11
1
    assert_values(&[(
12
1
        r#"(list (rpc-protocol-version) 1/2 "a" t nil)"#,
13
1
        "(1 1/2 \"a\" t nil)",
14
1
    )])
15
1
    .await;
16
1
}
17

            
18
#[tokio::test]
19
1
async fn or_of_a_false_test_and_a_string_renders_the_string() {
20
1
    assert_values(&[("(or (> (rpc-protocol-version) 5) \"x\")", "\"x\"")]).await;
21
1
}
22

            
23
#[tokio::test]
24
1
async fn constant_ratio_and_string_cells_decode_by_kind() {
25
1
    assert_values(&[(r#"(list 1/2 nil "a")"#, "(1/2 nil \"a\")")]).await;
26
1
}
27

            
28
#[tokio::test]
29
1
async fn nested_constant_list_compiles_as_a_runtime_list_element() {
30
1
    assert_values(&[("(list (list 1) (list 1 2))", "((1) (1 2))")]).await;
31
1
}
32

            
33
#[tokio::test]
34
1
async fn a_quoted_nested_list_beside_a_runtime_index_compiles_too() {
35
    // Regression: `(list '(1) v)` used to fail with "cannot compile to WASM
36
    // stack value: '(1)" for the same reason the all-`list`-call form did —
37
    // `push_pair_car` rejected any quoted datum before it ever tried to
38
    // materialize it as a `$pair` chain.
39
1
    assert_values(&[("(list '(1) (rpc-protocol-version))", "((1) 1)")]).await;
40
1
}
41

            
42
#[tokio::test]
43
1
async fn nested_quoted_data_is_never_evaluated_as_code() {
44
    // Codex review regression: `push_anyref_car`'s nested-list branch used to
45
    // hand quoted-datum members straight to `compile_pair_chain`, which
46
    // treats its input as EXPRESSIONS to compile — so `'((+ 1 2))` (a
47
    // one-element list whose member is the literal data `(+ 1 2)`) called
48
    // `+` and built `((3) 1)` instead of preserving the datum. Fixed to
49
    // route through the SAME quoting `emit_constant_chain` already used for
50
    // a CONS cdr, a bare symbol inside that data (here, `+`) is a symbol
51
    // cell, never a call.
52
1
    let response = eval_value_of("(list '((+ 1 2)) (rpc-protocol-version))").await;
53
1
    assert!(
54
1
        response.contains("((+ 1 2))"),
55
1
        "must not execute the quoted `+` as a call: {response}"
56
1
    );
57
1
}
58

            
59
#[tokio::test]
60
1
async fn nested_quoted_numeric_list_is_data_not_a_call() {
61
    // Same bug, the other reproducer Codex gave: `'((1))` is the list
62
    // containing the list containing 1 — never a call to the number 1.
63
1
    assert_values(&[("(list '((1)) (rpc-protocol-version))", "(((1)) 1)")]).await;
64
1
}
65

            
66
#[tokio::test]
67
1
async fn nested_constant_list_materialization_preserves_the_producers_effect() {
68
    // Codex review regression: the nested-list branch built the `$pair`
69
    // chain from the ALREADY-RESOLVED constant members and returned without
70
    // ever compiling the original producer expression, so a side effect
71
    // inside it (here, `setf` before the literal tail) never ran.
72
1
    assert_values(&[(
73
1
        "(let ((x (rpc-protocol-version))) (list (progn (setf x 2) '(7)) x))",
74
1
        "((7) 2)",
75
1
    )])
76
1
    .await;
77
1
}
78

            
79
#[tokio::test]
80
1
async fn filter_over_nested_constant_list_elements() {
81
1
    assert_values(&[(
82
1
        "(filter (lambda (x) (cdr x)) (list (list 1) (list 1 2)))",
83
1
        "((1 2))",
84
1
    )])
85
1
    .await;
86
1
}
87

            
88
#[tokio::test]
89
1
async fn deeply_nested_anyref_pairs_hit_a_bounded_decode_depth_not_a_host_crash() {
90
    // Codex review regression: `render_pair_as_string` / `render_car` used
91
    // to recurse into a nested-pair `AnyRef` car with no depth limit — this
92
    // runs AFTER guest execution, on the HOST's own call stack, so neither
93
    // wasm fuel nor the epoch deadline bounds it. A tail-recursive `labels`
94
    // function that wraps its accumulator in one more `(list acc)` per call
95
    // (forced to `AnyRef` by the `or`'s Bool/PairRef arm mismatch, the same
96
    // widening trick every other test in this file uses) builds exactly
97
    // that shape at RUNTIME (a real recursive call, not compile-time
98
    // unrolling, so it does not trip the compiler's own inlining-depth
99
    // ceiling). The fix turns unbounded host recursion into a bounded one
100
    // that returns a catchable error well before 100 levels — safe to run
101
    // in-process, unlike the unpatched unbounded version this regresses.
102
1
    let response = eval_value_of(
103
1
        "(labels ((build (n acc) \
104
1
                     (if (<= n 0) acc (build (- n 1) (or (> (rpc-protocol-version) 999) (list acc)))))) \
105
1
           (build 100 nil))",
106
1
    )
107
1
    .await;
108
1
    assert!(
109
1
        response.contains(":error") && response.contains("depth limit"),
110
1
        "expected a bounded decode-depth error, got: {response}"
111
1
    );
112
1
}
113

            
114
#[tokio::test]
115
1
async fn catch_each_ok_cells_render_by_kind_not_placeholder() {
116
1
    assert_values(&[("(catch-each (list 1 2) x (> x 1))", "((OK nil) (OK t))")]).await;
117
1
}
118

            
119
#[tokio::test]
120
1
async fn catch_each_err_cell_carries_a_symbol_code() {
121
1
    assert_values(&[(
122
1
        r#"(catch-each (list 1) x (error 'boom "m"))"#,
123
1
        r#"((ERR BOOM "m"))"#,
124
1
    )])
125
1
    .await;
126
1
}
127

            
128
#[tokio::test]
129
1
async fn catch_each_cell_tags_dispatch_with_eq_and_case() {
130
1
    assert_values(&[
131
1
        ("(eq? (car (car (catch-each (list 1) x x))) 'ok)", "t"),
132
1
        (
133
1
            r#"(let ((c (car (catch-each (list 1) x (error 'boom "m"))))) (list (eq? (car c) 'err) (eq? (car (cdr c)) 'boom)))"#,
134
1
            "(t t)",
135
1
        ),
136
1
        (
137
1
            r#"(mapcar (lambda (c) (case (car c) (ok 1) (err 2))) (catch-each (list 1 2) x (if (> x 1) (error 'boom "m") x)))"#,
138
1
            "(1 2)",
139
1
        ),
140
1
    ])
141
1
    .await;
142
1
}