1
//! Prelude helpers, constant-list rendering on the eval path and the value-position introspection forms.
2

            
3
use super::*;
4
use nomiscript::Reader;
5

            
6
#[test]
7
1
fn host_prelude_helper_is_loaded_and_compiles() {
8
    // ADR-0029 host-dependent prelude: split:list-for-transaction is loaded
9
    // on the Session path (after register_host_fns) and is callable. We
10
    // compile a form referencing it — proving load + name resolution + the
11
    // qualified native dispatch wire up — WITHOUT running it (it bottoms out
12
    // in the DB-backed list-splits-by-transaction native; execution is
13
    // covered by the db-gated integration test).
14
1
    let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
15
1
    assert!(
16
1
        session.symbols.contains("SPLIT:LIST-FOR-TRANSACTION"),
17
        "host prelude helper not registered"
18
    );
19
    // Compile (not run) a real call: lowering emits the qualified-name
20
    // dispatch + the native import, proving resolution wires up. Host fns
21
    // never execute at compile time, so no DB is touched.
22
1
    let program = Reader::parse("(split:list-for-transaction (car (list-transactions \"\")))")
23
1
        .expect("parse");
24
1
    if let Err(e) = session
25
1
        .compiler
26
1
        .compile_eval_with_type(&program, &mut session.symbols)
27
    {
28
        panic!("host prelude helper failed to compile: {e:?}");
29
1
    }
30
1
}
31

            
32
#[tokio::test]
33
1
async fn car_of_quoted_constant_list_compiles_on_eval_path() {
34
    // Regression: CAR/CDR of a quoted CONSTANT list folds on the codegen
35
    // path but used to trap on the eval-with-type (Session) path because
36
    // the stack handler called compile_for_stack on the quoted arg, which
37
    // has no Quote arm. The stack handlers now const-fold first.
38
1
    let response = handle_form_smoke("(:id 1 :form (car '(1 2 3)))").await;
39
1
    assert_eq!(response, "(:id 1 :value 1)");
40
1
}
41

            
42
#[tokio::test]
43
1
async fn car_of_quoted_heterogeneous_list_compiles_on_eval_path() {
44
    // Mixed number + string quoted list — the element is extracted by fold.
45
1
    let response = handle_form_smoke("(:id 1 :form (car '(7 \"x\")))").await;
46
1
    assert_eq!(response, "(:id 1 :value 7)");
47
1
}
48

            
49
#[tokio::test]
50
1
async fn car_of_cdr_of_quoted_constant_compiles_on_eval_path() {
51
    // CDR folds to the quoted tail, then CAR extracts its head — exercises
52
    // the cdr fold-first path feeding car.
53
1
    let response = handle_form_smoke("(:id 1 :form (car (cdr '(1 2 3))))").await;
54
1
    assert_eq!(response, "(:id 1 :value 2)");
55
1
}
56

            
57
#[tokio::test]
58
1
async fn cdr_of_quoted_constant_renders_tail_on_eval_path() {
59
    // The bare-CDR case (no enclosing CAR): folds to a quoted tail and
60
    // renders to its printed form rather than trapping.
61
1
    assert_eq!(
62
1
        handle_form_smoke("(:id 1 :form (cdr '(1 2 3)))").await,
63
1
        "(:id 1 :value (2 3))"
64
    );
65
1
    assert_eq!(
66
1
        handle_form_smoke("(:id 1 :form (cdr '(1)))").await,
67
1
        "(:id 1 :value nil)"
68
1
    );
69
1
}
70

            
71
#[tokio::test]
72
1
async fn car_of_quoted_compound_and_symbol_heads_render_as_data() {
73
    // A compound or symbol head is quoted DATA, not code — a compound renders
74
    // to its printed form, a symbol is a symbol (not resolved as a call /
75
    // variable).
76
1
    assert_eq!(
77
1
        handle_form_smoke("(:id 1 :form (car '((1 2) 3)))").await,
78
1
        "(:id 1 :value (1 2))"
79
    );
80
1
    assert_eq!(
81
1
        handle_form_smoke("(:id 1 :form (car '(x y)))").await,
82
1
        "(:id 1 :value X)"
83
1
    );
84
1
}
85

            
86
#[tokio::test]
87
1
async fn reverse_of_constant_list_renders_on_eval_path() {
88
    // Regression (same class as CAR/CDR): REVERSE of a constant or
89
    // runtime-builder list folded but the stack handler rejected the
90
    // non-runtime-pair result on the eval-with-type path. Now it renders
91
    // the reversed datum on both surfaces.
92
1
    assert_eq!(
93
1
        handle_form_smoke("(:id 1 :form (reverse '(1 2 3)))").await,
94
1
        "(:id 1 :value (3 2 1))"
95
    );
96
1
    assert_eq!(
97
1
        handle_form_smoke("(:id 1 :form (reverse (list 1 2 3)))").await,
98
1
        "(:id 1 :value (3 2 1))"
99
    );
100
    // Composition still folds through to the element.
101
1
    assert_eq!(
102
1
        handle_form_smoke("(:id 1 :form (car (reverse '(1 2 3))))").await,
103
1
        "(:id 1 :value 3)"
104
1
    );
105
1
}
106

            
107
#[tokio::test]
108
1
async fn cons_onto_constant_list_renders_on_eval_path() {
109
    // Regression: CONS with a constant / runtime-builder list cdr trapped
110
    // in push_pair_cdr on the eval-with-type path. A fully-constant cons
111
    // folds to a list datum, which is the same `$pair` chain as a runtime
112
    // one; a dotted pair has no cell representation (plan 0014 Stage 1).
113
1
    assert_eq!(
114
1
        handle_form_smoke("(:id 1 :form (cons 0 '(1 2 3)))").await,
115
1
        "(:id 1 :value (0 1 2 3))"
116
    );
117
1
    assert_eq!(
118
1
        handle_form_smoke("(:id 1 :form (cons 0 (list 1 2 3)))").await,
119
1
        "(:id 1 :value (0 1 2 3))"
120
    );
121
1
    let response = handle_form_smoke("(:id 1 :form (cons 1 2))").await;
122
1
    assert!(
123
1
        response.contains("'(1 . 2) has no list cell representation"),
124
1
        "{response}"
125
1
    );
126
1
}
127

            
128
#[tokio::test]
129
1
async fn append_of_constant_lists_renders_on_eval_path() {
130
    // Regression: all-constant APPEND folds to a quoted list, the same
131
    // `$pair` chain as a runtime one, symbol members included.
132
1
    assert_eq!(
133
1
        handle_form_smoke("(:id 1 :form (append '(1 2) '(3)))").await,
134
1
        "(:id 1 :value (1 2 3))"
135
    );
136
1
    assert_eq!(
137
1
        handle_form_smoke("(:id 1 :form (append '(a b) '(c)))").await,
138
1
        "(:id 1 :value (A B C))"
139
1
    );
140
1
}
141

            
142
#[tokio::test]
143
1
async fn universal_prelude_helper_runs_end_to_end() {
144
    // The universal prelude is loaded on the Session path too; a math:*
145
    // helper executes through wasm and returns its value. No DB.
146
1
    let response = handle_form_smoke("(:id 9 :form (math:square 9))").await;
147
1
    assert_eq!(response, "(:id 9 :value 81)");
148
1
}
149

            
150
#[tokio::test]
151
1
async fn pp_form_at_value_position_returns_string() {
152
    // Exercises compile_pp_for_stack — the path nms / emacs see
153
    // when `(pp 42)` is the request form.
154
1
    let resp = handle_form_smoke("(:id 7 :form (pp 42))").await;
155
1
    assert!(resp.contains(":id 7"), "{resp}");
156
1
    assert!(resp.contains("\"42\""), "{resp}");
157
1
}
158

            
159
#[tokio::test]
160
1
async fn describe_form_at_value_position_returns_doc() {
161
    // compile_describe_for_stack — same shape.
162
1
    let resp = handle_form_smoke("(:id 8 :form (describe '+))").await;
163
1
    assert!(resp.contains(":id 8"), "{resp}");
164
1
    assert!(!resp.contains(":error"), "{resp}");
165
1
}
166

            
167
#[tokio::test]
168
1
async fn apropos_form_at_value_position_returns_list() {
169
1
    let resp = handle_form_smoke("(:id 9 :form (apropos \"entity\"))").await;
170
1
    assert!(resp.contains(":id 9"), "{resp}");
171
1
    assert!(!resp.contains(":error"), "{resp}");
172
1
}
173

            
174
#[tokio::test]
175
1
async fn deftest_form_at_value_position_returns_quoted_name() {
176
1
    let resp = handle_form_smoke("(:id 10 :form (deftest sanity (assert-equal 1 1)))").await;
177
1
    assert!(resp.contains(":id 10"), "{resp}");
178
1
    assert!(!resp.contains(":error"), "{resp}");
179
1
}
180

            
181
#[tokio::test]
182
1
async fn assert_equal_pass_form_at_value_position() {
183
1
    let resp = handle_form_smoke("(:id 11 :form (assert-equal 2 2))").await;
184
1
    assert!(resp.contains(":id 11"), "{resp}");
185
1
    assert!(!resp.contains(":error"), "{resp}");
186
1
}
187

            
188
#[tokio::test]
189
1
async fn assert_equal_fail_surfaces_as_error() {
190
    // assert_equal returns Err on mismatch; Session maps to
191
    // :code compile error envelope (it's a NomiError::Compile).
192
1
    let resp = handle_form_smoke("(:id 12 :form (assert-equal 1 2))").await;
193
1
    assert!(resp.contains(":id 12"), "{resp}");
194
1
    assert!(resp.contains(":error"), "{resp}");
195
1
}
196

            
197
#[tokio::test]
198
1
async fn coverage_dump_lists_called_natives() {
199
1
    let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
200
1
    let _ = session
201
1
        .handle_form("(:id 1 :form (rpc-protocol-version))")
202
1
        .await;
203
1
    let dump = session.handle_form("(:id 2 :form (coverage-dump))").await;
204
1
    assert!(dump.contains("RPC-PROTOCOL-VERSION"), "{dump}");
205
1
    assert!(dump.contains(":id 2"), "{dump}");
206
1
}
207

            
208
#[tokio::test]
209
1
async fn coverage_dump_is_empty_of_natives_before_any_call() {
210
1
    let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
211
1
    let dump = session.handle_form("(:id 1 :form (coverage-dump))").await;
212
1
    assert!(dump.contains(":id 1"), "{dump}");
213
1
    assert!(!dump.contains("RPC-PROTOCOL-VERSION"), "{dump}");
214
1
}