1
//! Plan 0014 PERSIST (owner decision 2A), fixtures first: what a later unit
2
//! sees of a runtime global today. A row marked `CL` reads differently in
3
//! Common Lisp and turns green with the host carry.
4

            
5
use super::*;
6
use crate::ctx::ScriptLimits;
7

            
8
23
async fn run_units(ctx: ScriptCtx, sources: &[&str]) -> Vec<String> {
9
23
    let mut session = Session::new(ctx).expect("Session::new");
10
23
    let mut outcomes = Vec::new();
11
23
    for source in sources {
12
75
        let outcome = session.handle_request(source).await;
13
75
        outcomes.push(match outcome.payload {
14
2
            ResponsePayload::Error { message, .. } => message,
15
73
            ResponsePayload::Value(value) => format!("{value:?}"),
16
        });
17
    }
18
23
    outcomes
19
23
}
20

            
21
22
async fn units(sources: &[&str]) -> Vec<String> {
22
22
    run_units(ScriptCtx::new(uuid::Uuid::nil()), sources).await
23
22
}
24

            
25
6
fn num(n: i64, d: i64) -> String {
26
6
    format!("Number(Scalar(Ratio {{ numer: {n}, denom: {d} }}))")
27
6
}
28

            
29
25
fn index(n: i64) -> String {
30
25
    format!("Number(Index({n}))")
31
25
}
32

            
33
#[tokio::test]
34
1
async fn a_callee_store_of_a_defvar_is_carried_across_units() {
35
1
    let outcomes = units(&[
36
1
        "(defvar x 7)",
37
1
        "(defun g () x)",
38
1
        "(defun s (v) (setf x v))",
39
1
        "(s 5)",
40
1
        "(g)",
41
1
    ])
42
1
    .await;
43
1
    assert_eq!(outcomes[4], index(5));
44
1
}
45

            
46
#[tokio::test]
47
1
async fn the_total_accumulator_is_carried_across_units() {
48
1
    let outcomes = units(&[
49
1
        "(setf total 0)",
50
1
        "(defun add (n) (setf total (+ total n)))",
51
1
        "(add 5)",
52
1
        "(add 5)",
53
1
        "total",
54
1
    ])
55
1
    .await;
56
1
    assert_eq!(outcomes[3], index(10));
57
1
    assert_eq!(outcomes[4], index(10));
58
1
}
59

            
60
#[tokio::test]
61
1
async fn a_store_through_a_chain_of_earlier_functions_is_carried() {
62
1
    let outcomes = units(&[
63
1
        "(setf total 0)",
64
1
        "(defun add (n) (setf total (+ total n)))",
65
1
        "(defun twice (n) (add n) (add n))",
66
1
        "(twice 5)",
67
1
        "total",
68
1
    ])
69
1
    .await;
70
1
    assert_eq!(outcomes[4], index(10));
71
1
}
72

            
73
#[tokio::test]
74
1
async fn a_unit_that_binds_and_stores_an_earlier_special_carries_the_store() {
75
1
    let outcomes = units(&[
76
1
        "(defvar x 7)",
77
1
        "(defun g () x)",
78
1
        "(progn (let ((x 1)) (g)) (setf x 9))",
79
1
        "x",
80
1
    ])
81
1
    .await;
82
1
    assert_eq!(outcomes[3], index(9));
83
1
}
84

            
85
#[tokio::test]
86
1
async fn a_created_global_is_carried_and_a_later_defvar_keeps_it() {
87
1
    let outcomes = units(&[
88
1
        "(setf g 1)",
89
1
        "(setf g 2)",
90
1
        "g",
91
1
        "(defvar g 9)",
92
1
        "g",
93
1
        "(let ((g 5)) g)",
94
1
    ])
95
1
    .await;
96
1
    assert_eq!(outcomes[2], index(2));
97
1
    assert_eq!(outcomes[4], index(2));
98
1
    assert_eq!(outcomes[5], index(5));
99
1
}
100

            
101
#[tokio::test]
102
1
async fn a_repeated_defvar_does_not_stop_the_store_being_carried() {
103
1
    let outcomes = units(&[
104
1
        "(defvar total 0)",
105
1
        "(defun add (n) (setf total (+ total n)))",
106
1
        "(add 3)",
107
1
        "(defvar total 0)",
108
1
        "(add 3)",
109
1
        "total",
110
1
        "(defvar y 5)",
111
1
        "(progn (defvar y 6) (setf y 7))",
112
1
        "y",
113
1
    ])
114
1
    .await;
115
1
    assert_eq!(outcomes[5], index(6));
116
1
    assert_eq!(outcomes[8], index(7));
117
1
}
118

            
119
#[tokio::test]
120
1
async fn a_runtime_store_before_an_uncaught_error_is_carried() {
121
1
    let outcomes = units(&[
122
1
        "(progn (defvar x 1) (setf x (+ 5 (rpc-protocol-version))) (error 'boom \"m\"))",
123
1
        "x",
124
1
    ])
125
1
    .await;
126
    // Before the failure policy the second unit read an expired global.
127
1
    assert_eq!(outcomes, ["script raised BOOM: m".to_string(), index(6)]);
128
1
}
129

            
130
#[tokio::test]
131
1
async fn a_runtime_store_before_a_fuel_trap_is_carried_with_the_value_outside_a_binding() {
132
1
    let ctx = ScriptCtx::new(uuid::Uuid::nil()).with_limits(ScriptLimits {
133
1
        fuel: 100_000,
134
1
        ..ScriptLimits::default()
135
1
    });
136
1
    let outcomes = run_units(
137
1
        ctx,
138
1
        &[
139
1
            "(progn (defvar z 1) (defvar y 1) (setf z (+ 5 (rpc-protocol-version))) (setf y 2) (let ((y 3)) (setf y 4) (do ((i 0 (+ i 1))) ((> i 1073741823) i))))",
140
1
            "z",
141
1
            "y",
142
1
        ],
143
1
    )
144
1
    .await;
145
1
    assert_eq!(outcomes[0], "fuel exhausted before completion");
146
1
    assert_eq!(outcomes[1], index(6));
147
1
    assert_eq!(outcomes[2], index(2));
148
1
}
149

            
150
#[tokio::test]
151
1
async fn a_stored_closure_is_carried_under_its_site_in_the_next_unit() {
152
1
    let first = "(progn (defvar v (lambda (x) (+ x 2))) (setf v (lambda (x) (+ x 1))))";
153
1
    assert_eq!(
154
1
        units(&[first, "v"]).await,
155
1
        [
156
1
            "Closure(Closure { code_id: Some(1), env: [] })".to_string(),
157
1
            "Closure(Closure { code_id: Some(0), env: [] })".to_string(),
158
1
        ]
159
1
    );
160
1
}
161

            
162
#[tokio::test]
163
1
async fn every_stored_value_kind_is_carried() {
164
3
    let int = |n| index(n);
165
1
    let kinds = [
166
1
        ("(progn (defvar v 1) (setf v 2))", int(2)),
167
1
        ("(progn (defvar v 1/2) (setf v 3/4))", num(3, 4)),
168
1
        ("(progn (defvar v 1) (setf v nil))", "Nil".to_string()),
169
1
        ("(progn (defvar v nil) (setf v t))", "T".to_string()),
170
1
        (
171
1
            "(progn (defvar v \"a\") (setf v \"b\"))",
172
1
            "String(\"b\")".to_string(),
173
1
        ),
174
1
        (
175
1
            "(progn (defvar v 'a) (setf v 'b))",
176
1
            "Symbol(\"B\")".to_string(),
177
1
        ),
178
1
        (
179
1
            "(progn (defvar v :a) (setf v :b))",
180
1
            "Symbol(\":B\")".to_string(),
181
1
        ),
182
1
        (
183
1
            "(progn (defvar v '(1)) (setf v '(1 2)))",
184
1
            format!(
185
1
                "Pair(Pair {{ car: {}, cdr: Pair(Pair {{ car: {}, cdr: Nil }}) }})",
186
1
                int(1),
187
1
                int(2)
188
1
            ),
189
1
        ),
190
1
    ];
191
1
    for (first, stored) in kinds {
192
8
        assert_eq!(
193
8
            units(&[first, "v"]).await,
194
8
            [stored.clone(), stored],
195
1
            "{first}"
196
1
        );
197
1
    }
198
1
}
199

            
200
#[tokio::test]
201
1
async fn a_type_change_in_a_later_unit_defines_a_fresh_home() {
202
1
    let outcomes = units(&[
203
1
        "(progn (defvar v 1) (setf v 2))",
204
1
        "(progn (defparameter v \"s\") (setf v \"t\") v)",
205
1
    ])
206
1
    .await;
207
1
    assert_eq!(outcomes, [index(2), "String(\"t\")".to_string()]);
208
1
}
209

            
210
#[tokio::test]
211
1
async fn an_integral_scalar_divides_as_a_scalar_only_while_it_is_runtime() {
212
1
    let outcomes = units(&[
213
1
        "(progn (defvar x 1/2) (setf x (* x 4/1)) (/ x 4/1))",
214
1
        "(progn (defvar y 2) (setf y (+ y 0)) (/ y 4))",
215
1
        "(progn (defvar z 2) (/ z 4))",
216
1
        "(/ 2 4)",
217
1
    ])
218
1
    .await;
219
    // CL: 1/2 everywhere. Rows 2-4 are index division on an integer literal.
220
1
    assert_eq!(outcomes, [num(1, 2), index(0), index(0), index(0)]);
221
1
}
222

            
223
#[tokio::test]
224
1
async fn a_scalar_that_ends_integral_stays_a_scalar_in_the_next_unit() {
225
1
    let outcomes = units(&[
226
1
        "(progn (defvar x 1/2) (setf x (* x 4/1)))",
227
1
        "(/ x 4/1)",
228
1
        "(+ x 1/2)",
229
1
        "x",
230
1
    ])
231
1
    .await;
232
1
    assert_eq!(outcomes, [num(2, 1), num(1, 2), num(5, 2), num(2, 1)]);
233
1
}
234

            
235
#[tokio::test]
236
1
async fn an_index_stays_an_index_in_the_next_unit() {
237
1
    let outcomes = units(&["(progn (defvar n 1) (setf n 5))", "(/ n 2)"]).await;
238
1
    assert_eq!(outcomes, [index(5), index(2)]);
239
1
}
240

            
241
#[tokio::test]
242
1
async fn a_store_by_a_defun_called_in_the_same_unit_is_carried() {
243
1
    let outcomes = units(&["(progn (defvar x 1) (defun s (v) (setf x v)) (s 5))", "x"]).await;
244
1
    assert_eq!(outcomes, [index(5), index(5)]);
245
1
}
246

            
247
#[tokio::test]
248
1
async fn a_carried_list_and_symbol_are_data_in_the_next_unit() {
249
1
    let outcomes = units(&[
250
1
        "(progn (defvar l '(1)) (setf l '(7 8)))",
251
1
        "(car l)",
252
1
        "(progn (defvar s 'a) (setf s 'b))",
253
1
        "(eq s 'b)",
254
1
    ])
255
1
    .await;
256
1
    assert_eq!(outcomes[1], index(7));
257
1
    assert_eq!(outcomes[3], "T");
258
1
}