1
//! Session state across forms and the meta / server natives called from source.
2

            
3
use super::*;
4
use nomiscript::{Fraction, Number};
5

            
6
#[tokio::test]
7
1
async fn interrupt_does_not_persist_across_forms() {
8
1
    let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
9
1
    session.interrupt_handle().interrupt();
10
1
    let _ = session.handle_form("(:id 11 :form (+ 1 2))").await;
11
1
    let response = session.handle_form("(:id 12 :form (+ 1 2))").await;
12
1
    assert_eq!(response, "(:id 12 :value 3)");
13
1
}
14

            
15
#[tokio::test]
16
1
async fn session_state_persists_across_forms() {
17
1
    let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
18
1
    let defun = session
19
1
        .handle_form("(:id 1 :form (defun double (x) (* 2 x)))")
20
1
        .await;
21
1
    assert!(defun.contains(":id 1"));
22
1
    let call = session.handle_form("(:id 2 :form (double 21))").await;
23
1
    assert_eq!(call, "(:id 2 :value 42)");
24
1
}
25

            
26
#[tokio::test]
27
1
async fn fraction_results_format_canonically() {
28
    // A fractional (Scalar) literal renders canonically as `n/d`. (Integer
29
    // `(/ 1 4)` is now Index division → 0 per ADR-0028; the canonical
30
    // fraction idiom is the `1/4` Scalar literal.)
31
1
    let response = handle_form_smoke("(:id 3 :form 1/4)").await;
32
1
    assert_eq!(response, "(:id 3 :value 1/4)");
33
1
}
34

            
35
#[test]
36
1
fn nomi_runtime_value_carries_through() {
37
1
    let program = Reader::parse("(+ 0.5 0.25)").unwrap();
38
1
    let value = nomiscript::eval_program(&mut SymbolTable::with_builtins(), &program).unwrap();
39
1
    assert_eq!(value, Value::Number(Number::Scalar(Fraction::new(3, 4))));
40
1
}
41

            
42
#[tokio::test]
43
1
async fn calls_meta_native_from_nomiscript_source() {
44
1
    let response = handle_form_smoke("(:id 1 :form (rpc-protocol-version))").await;
45
1
    let expected_version = crate::natives::meta::PROTOCOL_VERSION;
46
1
    assert_eq!(response, format!("(:id 1 :value {expected_version})"));
47
1
}
48

            
49
#[tokio::test]
50
1
async fn calls_server_get_version_from_nomiscript_source() {
51
1
    let response = handle_form_smoke("(:id 1 :form (get-version))").await;
52
    // GIT_HASH is baked at server crate build time via env!. We don't
53
    // assert its exact value (changes per build) — just that the
54
    // envelope round-trips a non-empty :value string.
55
1
    assert!(
56
1
        response.starts_with("(:id 1 :value \""),
57
        "expected string response, got: {response}"
58
    );
59
1
    assert!(response.ends_with("\")"));
60
1
}
61

            
62
#[tokio::test]
63
1
async fn calls_server_get_build_date_from_nomiscript_source() {
64
1
    let response = handle_form_smoke("(:id 2 :form (get-build-date))").await;
65
1
    assert!(
66
1
        response.starts_with("(:id 2 :value \""),
67
        "expected string response, got: {response}"
68
    );
69
1
    assert!(response.ends_with("\")"));
70
1
}
71

            
72
#[tokio::test]
73
1
async fn cons_list_surfaces_as_printable_string() {
74
    // First WasmGC sub-slice: eval-mode `(cons ...)` chains now
75
    // capture through pending_string instead of erroring at compile
76
    // time. The result is a textual `(1 2 3)` value the emacs client
77
    // can (read) back into a real list. Heterogeneous car types ride
78
    // a follow-up slice once Pair/Vector/Closure/Struct share a
79
    // tagged union — today's cons cell stores i32 payloads only.
80
1
    let response = handle_form_smoke("(:id 12 :form (cons 1 (cons 2 (cons 3 nil))))").await;
81
1
    assert!(
82
1
        response.contains(":value (1 2 3)"),
83
1
        "expected :value (1 2 3), got: {response}"
84
1
    );
85
1
}
86

            
87
#[tokio::test]
88
1
async fn count_native_cannot_mix_with_ratio_arithmetic() {
89
    // account-count returns i32 (a count / Index, not a Scalar). Mixing it
90
    // with a fractional Scalar literal must fail to compile — the design
91
    // forbids accidental arithmetic across the Index/Scalar strata. (An
92
    // integer literal like `10` is itself an Index now (ADR-0028), so
93
    // `(+ 10 (account-count))` is valid Index arithmetic; the genuine
94
    // stratum clash needs a fractional `1/2` Scalar operand.) The explicit
95
    // `index->scalar` bridge is the only legal crossing.
96
1
    let response = handle_form_smoke("(:id 11 :form (+ 1/2 (account-count)))").await;
97
1
    assert!(response.contains(":code compile"), "got: {response}");
98
1
    assert!(
99
1
        response.contains("scalar") && response.contains("index"),
100
1
        "expected Index/Scalar stratum-separation error, got: {response}"
101
1
    );
102
1
}
103

            
104
#[tokio::test]
105
1
async fn get_commodity_with_non_uuid_arg_falls_back_to_symbol_lookup() {
106
    // get-commodity now accepts a uuid OR a symbol (mirroring get-account's
107
    // name fallback), so a non-uuid arg is NO LONGER an "invalid uuid"
108
    // error — it's treated as a symbol and routed to a DB lookup. On this
109
    // no-DB smoke harness that lookup surfaces a runtime DB-access error
110
    // (not a parse error); a DB-backed test
111
    // (`get_commodity_resolves_by_symbol` in tests-integration) covers the
112
    // successful resolution.
113
1
    let response = handle_form_smoke("(:id 9 :form (get-commodity \"USD\"))").await;
114
1
    assert!(response.contains(":id 9"), "got: {response}");
115
1
    assert!(
116
1
        response.contains(":code unavailable"),
117
        "expected the unavailable condition (symbol path hits the DB), got: {response}"
118
    );
119
1
    assert!(
120
1
        !response.contains("invalid uuid"),
121
1
        "a non-uuid arg must no longer short-circuit as an invalid-uuid error: {response}"
122
1
    );
123
1
}
124

            
125
#[test]
126
1
fn meta_native_unknown_in_script_mode_compile() {
127
    // host_fns are only registered in eval-mode contexts; the compiler
128
    // built without with_host_fns shouldn't see them. Sanity that the
129
    // mode flag actually gates the registration.
130
    use nomiscript::CompileMode;
131
1
    let mut compiler = Compiler::new();
132
1
    let mut symbols = SymbolTable::with_builtins();
133
1
    let program = nomiscript::Reader::parse("(rpc-protocol-version)").unwrap();
134
1
    let result = compiler.compile_with_mode(&program, &mut symbols, CompileMode::Script);
135
1
    assert!(
136
1
        result.is_err(),
137
        "host fn should not be callable when compiler has no specs"
138
    );
139
1
}
140

            
141
#[tokio::test]
142
1
async fn get_config_missing_name_yields_error_envelope() {
143
1
    let resp = handle_form_smoke("(:id 20 :form (get-config \"\"))").await;
144
1
    assert!(resp.contains(":id 20"), "{resp}");
145
1
    assert!(resp.contains(":error"), "{resp}");
146
1
    assert!(!resp.contains(":value"), "{resp}");
147
1
}
148

            
149
#[tokio::test]
150
1
async fn get_config_success_shape_is_config_value() {
151
    // Passes a non-empty name; the DB call fails (no pool) and surfaces
152
    // as an :error envelope — we verify the SUCCESS wire shape is only
153
    // emitted when the command actually succeeds. Here we just confirm
154
    // the argument-validation path (empty string) always errors.
155
1
    let resp = handle_form_smoke("(:id 21 :form (get-config \"\"))").await;
156
1
    assert!(resp.contains(":id 21"), "{resp}");
157
1
    assert!(!resp.contains(":value"), "{resp}");
158
1
}
159

            
160
#[tokio::test]
161
1
async fn a_printed_number_shows_its_kind() {
162
1
    assert_values(&[
163
1
        ("(+ 1 1)", "2"),
164
1
        ("(* 1/2 2/1)", "1/1"),
165
1
        ("(* 1/2 1/2)", "1/4"),
166
1
        ("'(2/1 3)", "(2/1 3/1)"),
167
1
        ("(index->scalar 2)", "2/1"),
168
1
        ("(scalar->index 2/1)", "2"),
169
1
    ])
170
1
    .await;
171
1
}