1
// Skipped under Miri: these tests compile+run wasm via wasmtime, whose
2
// Cranelift backend refuses to run under Miri.
3
#![cfg(not(miri))]
4

            
5
use nms::interpreter::Interpreter;
6
use scripting::nomiscript::{Number, Value};
7

            
8
#[test]
9
1
fn test_eval_num_eq() {
10
1
    let mut interp = Interpreter::new(false).unwrap();
11
1
    assert_eq!(interp.eval("(= 1 1 1)").unwrap(), vec![Value::T]);
12
1
    assert_eq!(interp.eval("(= 1 2)").unwrap(), vec![Value::Nil]);
13
1
}
14

            
15
#[test]
16
1
fn test_eval_num_eq_single() {
17
1
    let mut interp = Interpreter::new(false).unwrap();
18
1
    assert_eq!(interp.eval("(= 42)").unwrap(), vec![Value::T]);
19
1
}
20

            
21
#[test]
22
1
fn test_eval_num_neq() {
23
1
    let mut interp = Interpreter::new(false).unwrap();
24
1
    assert_eq!(interp.eval("(/= 1 2 3)").unwrap(), vec![Value::T]);
25
1
    assert_eq!(interp.eval("(/= 1 2 1)").unwrap(), vec![Value::Nil]);
26
1
}
27

            
28
#[test]
29
1
fn test_eval_num_lt() {
30
1
    let mut interp = Interpreter::new(false).unwrap();
31
1
    assert_eq!(interp.eval("(< 1 2 3)").unwrap(), vec![Value::T]);
32
1
    assert_eq!(interp.eval("(< 1 2 2)").unwrap(), vec![Value::Nil]);
33
1
}
34

            
35
#[test]
36
1
fn test_eval_num_gt() {
37
1
    let mut interp = Interpreter::new(false).unwrap();
38
1
    assert_eq!(interp.eval("(> 3 2 1)").unwrap(), vec![Value::T]);
39
1
    assert_eq!(interp.eval("(> 3 3 1)").unwrap(), vec![Value::Nil]);
40
1
}
41

            
42
#[test]
43
1
fn test_eval_num_le() {
44
1
    let mut interp = Interpreter::new(false).unwrap();
45
1
    assert_eq!(interp.eval("(<= 1 2 2 3)").unwrap(), vec![Value::T]);
46
1
    assert_eq!(interp.eval("(<= 1 3 2)").unwrap(), vec![Value::Nil]);
47
1
}
48

            
49
#[test]
50
1
fn test_eval_num_ge() {
51
1
    let mut interp = Interpreter::new(false).unwrap();
52
1
    assert_eq!(interp.eval("(>= 3 2 2 1)").unwrap(), vec![Value::T]);
53
1
    assert_eq!(interp.eval("(>= 1 2 3)").unwrap(), vec![Value::Nil]);
54
1
}
55

            
56
#[test]
57
1
fn test_eval_comparison_type_error() {
58
1
    let mut interp = Interpreter::new(false).unwrap();
59
1
    assert!(interp.eval("(= 1 \"one\")").is_err());
60
1
    assert!(interp.eval("(< \"a\" \"b\")").is_err());
61
1
}
62

            
63
#[test]
64
1
fn test_eval_eql_numbers() {
65
1
    let mut interp = Interpreter::new(false).unwrap();
66
1
    assert_eq!(interp.eval("(eql 1 1)").unwrap(), vec![Value::T]);
67
1
    assert_eq!(interp.eval("(eql 1 2)").unwrap(), vec![Value::Nil]);
68
1
}
69

            
70
#[test]
71
1
fn test_eval_eql_strings() {
72
1
    let mut interp = Interpreter::new(false).unwrap();
73
1
    assert_eq!(interp.eval(r#"(eql "abc" "abc")"#).unwrap(), vec![Value::T]);
74
1
    assert_eq!(
75
1
        interp.eval(r#"(eql "abc" "def")"#).unwrap(),
76
1
        vec![Value::Nil]
77
    );
78
1
}
79

            
80
#[test]
81
1
fn test_eval_eql_mixed_types() {
82
1
    let mut interp = Interpreter::new(false).unwrap();
83
1
    assert_eq!(interp.eval(r#"(eql 1 "1")"#).unwrap(), vec![Value::Nil]);
84
1
}
85

            
86
#[test]
87
1
fn test_eval_eql_nil() {
88
1
    let mut interp = Interpreter::new(false).unwrap();
89
1
    assert_eq!(interp.eval("(eql nil nil)").unwrap(), vec![Value::T]);
90
1
}
91

            
92
#[test]
93
1
fn test_eval_eql_arity_error() {
94
1
    let mut interp = Interpreter::new(false).unwrap();
95
1
    assert!(interp.eval("(eql 1)").is_err());
96
1
    assert!(interp.eval("(eql 1 2 3)").is_err());
97
1
}
98

            
99
#[test]
100
1
fn test_eval_equal_strings() {
101
1
    let mut interp = Interpreter::new(false).unwrap();
102
1
    assert_eq!(
103
1
        interp.eval(r#"(equal "hello" "hello")"#).unwrap(),
104
1
        vec![Value::T]
105
    );
106
1
    assert_eq!(
107
1
        interp.eval(r#"(equal "hello" "world")"#).unwrap(),
108
1
        vec![Value::Nil]
109
    );
110
1
}
111

            
112
#[test]
113
1
fn test_eval_equal_numbers() {
114
1
    let mut interp = Interpreter::new(false).unwrap();
115
1
    assert_eq!(interp.eval("(equal 0.5 0.5)").unwrap(), vec![Value::T]);
116
1
}
117

            
118
#[test]
119
1
fn test_eval_equal_arity_error() {
120
1
    let mut interp = Interpreter::new(false).unwrap();
121
1
    assert!(interp.eval("(equal 1)").is_err());
122
1
}
123

            
124
// --- Runtime bool serialization (WasmType::Bool) ---
125
// `(transaction-tag-count 0)` is a RUNTIME i32 (0 on the minimal input), so
126
// these comparisons/predicates can't const-fold — they exercise the runtime
127
// producers. The result must serialize as Bool/Nil (the falsy/truthy pair),
128
// not Number, mirroring the const-fold path above.
129

            
130
#[test]
131
1
fn test_runtime_comparison_true_is_bool() {
132
1
    let mut interp = Interpreter::new(false).unwrap();
133
1
    assert_eq!(
134
1
        interp.eval("(= (transaction-tag-count 0) 0)").unwrap(),
135
1
        vec![Value::T]
136
    );
137
1
}
138

            
139
#[test]
140
1
fn test_runtime_comparison_false_is_nil() {
141
1
    let mut interp = Interpreter::new(false).unwrap();
142
1
    assert_eq!(
143
1
        interp.eval("(< 0 (transaction-tag-count 0))").unwrap(),
144
1
        vec![Value::Nil]
145
    );
146
1
}
147

            
148
#[test]
149
1
fn test_runtime_not_is_bool() {
150
1
    let mut interp = Interpreter::new(false).unwrap();
151
    // (not <runtime-false>) → true; tag-count is 0 (falsy) so not → Bool(true).
152
1
    assert_eq!(
153
1
        interp
154
1
            .eval("(not (< 0 (transaction-tag-count 0)))")
155
1
            .unwrap(),
156
1
        vec![Value::T]
157
    );
158
1
}
159

            
160
#[test]
161
1
fn test_runtime_and_or_are_bool() {
162
1
    let mut interp = Interpreter::new(false).unwrap();
163
    // and of two runtime comparisons: tag-count 0 == 0 (true) and 0 >= 0
164
    // (true) → Bool(true).
165
1
    assert_eq!(
166
1
        interp
167
1
            .eval("(and (= (transaction-tag-count 0) 0) (>= (transaction-tag-count 0) 0))")
168
1
            .unwrap(),
169
1
        vec![Value::T]
170
    );
171
    // or short-circuits on the first truthy runtime comparison. This case
172
    // (true FIRST arg) is the regression: the old effect-only short-circuit
173
    // helper produced no output entity on the early-exit branch, so a
174
    // top-level `(or <true> …)` returned "no output entities". The
175
    // value-producing stack path yields a result on every branch.
176
1
    assert_eq!(
177
1
        interp
178
1
            .eval("(or (= (transaction-tag-count 0) 0) (< (transaction-tag-count 0) 0))")
179
1
            .unwrap(),
180
1
        vec![Value::T]
181
    );
182
    // false first, true second — exercises the short-circuit fall-through too.
183
1
    assert_eq!(
184
1
        interp
185
1
            .eval("(or (< (transaction-tag-count 0) 0) (= (transaction-tag-count 0) 0))")
186
1
            .unwrap(),
187
1
        vec![Value::T]
188
    );
189
1
}
190

            
191
#[test]
192
1
fn test_unary_runtime_and_or_over_a_count_is_the_count_itself() {
193
    // ADR-0033: `and`/`or` return values, as in Common Lisp. `(and a)` / `(or
194
    // a)` (a single operand) is just `a` itself, not a collapsed boolean —
195
    // `(transaction-tag-count 0)` is 0 on the minimal input.
196
1
    let mut interp = Interpreter::new(false).unwrap();
197
1
    assert_eq!(
198
1
        interp.eval("(and (transaction-tag-count 0))").unwrap(),
199
1
        vec![Value::Number(Number::Index(0))]
200
    );
201
1
    assert_eq!(
202
1
        interp.eval("(or (transaction-tag-count 0))").unwrap(),
203
1
        vec![Value::Number(Number::Index(0))]
204
    );
205
1
}
206

            
207
#[test]
208
1
fn test_unary_and_or_over_a_bound_count_local_is_the_count_itself() {
209
    // A bound runtime local (`WasmLocal(_, I32)`) must get the same result as a
210
    // fresh placeholder.
211
1
    let mut interp = Interpreter::new(false).unwrap();
212
1
    assert_eq!(
213
1
        interp
214
1
            .eval("(let* ((n (transaction-tag-count 0)) (x (and n))) x)")
215
1
            .unwrap(),
216
1
        vec![Value::Number(Number::Index(0))]
217
    );
218
1
    assert_eq!(
219
1
        interp
220
1
            .eval("(let* ((n (transaction-tag-count 0)) (x (or n))) x)")
221
1
            .unwrap(),
222
1
        vec![Value::Number(Number::Index(0))]
223
    );
224
1
}
225

            
226
#[test]
227
1
fn test_effect_position_and_or_accepts_a_ref_runtime_operand() {
228
    // ADR-0033: any value is a valid test — `reject_non_boolean_runtime_test`
229
    // is gone. A ref-typed runtime operand (`(transaction-post-date 0)` is a
230
    // runtime Ratio) in effect position now compiles instead of erroring.
231
1
    let mut interp = Interpreter::new(false).unwrap();
232
1
    interp
233
1
        .eval(r#"(and (transaction-post-date 0) 1) 7"#)
234
1
        .expect("a ref-typed AND operand in effect position must compile");
235
1
    interp
236
1
        .eval(r#"(or (transaction-post-date 0) 1) 7"#)
237
1
        .expect("a ref-typed OR operand in effect position must compile");
238
1
}
239

            
240
#[test]
241
1
fn test_runtime_and_or_accepts_a_ref_typed_operand() {
242
    // ADR-0033: `and`/`or` rewrite into an `IF` chain (arm reconciliation),
243
    // not an i32-only short-circuit block, so a ref-typed (StringRef/…)
244
    // operand rides it like any other value.
245
1
    let mut interp = Interpreter::new(false).unwrap();
246
1
    interp
247
1
        .eval(r#"(and (= (transaction-tag-count 0) 0) "x")"#)
248
1
        .expect("a ref-typed AND operand must compile");
249
1
}
250

            
251
#[test]
252
1
fn test_runtime_nullable_ref_in_and_or_is_a_valid_test() {
253
    // #58 / ADR-0033: a let-bound runtime PairRef that is nil at runtime is a
254
    // valid `or`/`and` test now (any value is a valid test) — nil beside the
255
    // literal `7` widens the result to `AnyRef`, it is not rejected.
256
1
    let mut interp = Interpreter::new(false).unwrap();
257
1
    let nullable = "(let* ((x (cdr (cons (transaction-tag-count 0) nil)))) ";
258
1
    interp
259
1
        .eval(&format!("{nullable} (or x 7))"))
260
1
        .expect("a nullable PairRef test in OR must compile");
261
1
    interp
262
1
        .eval(&format!("{nullable} (and x 7))"))
263
1
        .expect("a nullable PairRef test in AND must compile");
264
    // The `(null? …)` escape hatch yields a Bool, so the form compiles and the
265
    // null ref reads as truthy-null: `(or (null? x) …)` → Bool(true).
266
1
    assert_eq!(
267
1
        interp
268
1
            .eval(&format!(
269
1
                "{nullable} (or (null? x) (= (transaction-tag-count 0) 5)))"
270
1
            ))
271
1
            .unwrap(),
272
1
        vec![Value::T]
273
    );
274
1
}
275

            
276
// --- Phase 7 (0010-common-lisp-booleans.org): identity against a type that
277
// can never be `t`/`nil` (`nil_repr` is `Never` — Index, Ratio, Commodity). A
278
// runtime Index (`transaction-tag-count`) and Ratio (`transaction-post-date`)
279
// producer stand in for the never-nil types this doesn't require a database
280
// for; the Commodity case lives in
281
// `tests-integration/tests/eq_identity_never_nil.rs`, since a runtime
282
// Commodity only exists behind the DB-backed `account-balance` host fn.
283
// `t`/`nil` are unique objects, so no value of such a type is ever one of
284
// them — the comparison is `nil`, not the old "= expects numeric arguments,
285
// got nil" compile error.
286

            
287
#[test]
288
1
fn test_eq_nil_against_runtime_index_is_nil() {
289
1
    let mut interp = Interpreter::new(false).unwrap();
290
1
    assert_eq!(
291
1
        interp.eval("(eq? nil (transaction-tag-count 0))").unwrap(),
292
1
        vec![Value::Nil]
293
    );
294
1
}
295

            
296
#[test]
297
1
fn test_eq_runtime_index_against_nil_is_nil_reversed_order() {
298
1
    let mut interp = Interpreter::new(false).unwrap();
299
1
    assert_eq!(
300
1
        interp.eval("(eq? (transaction-tag-count 0) nil)").unwrap(),
301
1
        vec![Value::Nil]
302
    );
303
1
}
304

            
305
#[test]
306
1
fn test_eq_t_against_runtime_index_is_nil() {
307
1
    let mut interp = Interpreter::new(false).unwrap();
308
1
    assert_eq!(
309
1
        interp.eval("(eq? t (transaction-tag-count 0))").unwrap(),
310
1
        vec![Value::Nil]
311
    );
312
1
}
313

            
314
#[test]
315
1
fn test_eq_nil_against_runtime_ratio_is_nil() {
316
1
    let mut interp = Interpreter::new(false).unwrap();
317
1
    assert_eq!(
318
1
        interp.eval("(eq? nil (transaction-post-date 0))").unwrap(),
319
1
        vec![Value::Nil]
320
    );
321
1
}
322

            
323
#[test]
324
1
fn test_eq_runtime_ratio_against_t_is_nil_reversed_order() {
325
1
    let mut interp = Interpreter::new(false).unwrap();
326
1
    assert_eq!(
327
1
        interp.eval("(eq? (transaction-post-date 0) t)").unwrap(),
328
1
        vec![Value::Nil]
329
    );
330
1
}
331

            
332
/// A never-nil operand's own effects (a `setf` inside a `progn`) must still
333
/// run — exactly once, not zero and not twice — even though `eq?` never
334
/// needs the operand's VALUE (the result is statically `nil`). `n` ends at
335
/// `1`: dropped effects would leave it `0`, a doubled compile-time-probe-plus-
336
/// real-emission bug would leave it `2`.
337
#[test]
338
1
fn test_eq_nil_against_never_nil_operand_runs_effect_exactly_once() {
339
1
    let mut interp = Interpreter::new(false).unwrap();
340
1
    assert_eq!(
341
1
        interp
342
1
            .eval(
343
1
                "(let* ((n 0)) \
344
1
                 (eq? nil (progn (setf n (+ n 1)) (transaction-tag-count 0))) \
345
1
                 n)"
346
1
            )
347
1
            .unwrap(),
348
1
        vec![Value::Number(Number::Index(1))]
349
    );
350
1
}