simpleplatform-sdk 0.3.0

Simple Platform Rust SDK
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Running an action on your own machine, with no host and no wasm.
//!
//! # Why this is in the SDK and not in your action
//!
//! A handler reaches its host through one seam, so the stand-in for that host
//! belongs beside it. Shipping it here means a test drives the same seam
//! production drives, and every action gets the same one — kept in step with the
//! wire format by the crate that defines the wire format.
//!
//! # The whole of it
//!
//! ```
//! use simpleplatform_sdk::prelude::*;
//! use simpleplatform_sdk::testing;
//!
//! #[derive(Deserialize)]
//! struct Input {
//!     id: String,
//! }
//!
//! fn handler(request: Request<Input>) -> Result<Value, Error> {
//!     let answer: Value = simple::graphql::query(
//!         "query One($id: ID!) { thing: app__thing(where: {id: {_eq: $id}}) { id } }",
//!         json!({ "id": request.data.id }),
//!     )?;
//!
//!     Ok(answer)
//! }
//!
//! let session = testing::install(|name, params| {
//!     assert_eq!(name, "action:db/execute");
//!     assert_eq!(params["variables"]["id"], json!("T1"));
//!
//!     Ok(json!({ "thing": [{ "id": "T1" }] }))
//! });
//!
//! let answer = handler(Request::new(Input { id: "T1".into() })).unwrap();
//!
//! assert_eq!(answer, json!({ "thing": [{ "id": "T1" }] }));
//! assert_eq!(session.calls().len(), 1);
//! ```
//!
//! The closure answers in *results*, not envelopes: return the data the host
//! would have produced, or an [`Error`] for a host that refused. Wrapping it in
//! `{ ok, data }` is this module's job, exactly as unwrapping it is the SDK's
//! job in production.
//!
//! # Testing the envelope as well as the answer
//!
//! [`crate::run`] works here too. Give the session a request payload and it will
//! drive the whole path — parse, handle, report — and keep the `__done__`
//! document for you to assert on:
//!
//! ```
//! use simpleplatform_sdk::prelude::*;
//! use simpleplatform_sdk::testing;
//!
//! let session = testing::install(|_name, _params| Ok(json!(null)))
//!     .with_request(json!({ "id": "T1" }));
//!
//! simple::run(|request: Request<Value>| Ok(json!({ "echoed": request.data })));
//!
//! assert_eq!(
//!     session.done().unwrap(),
//!     json!({ "data": { "echoed": { "id": "T1" } }, "errors": [], "ok": true })
//! );
//! ```
//!
//! # One session per thread
//!
//! The slot is thread-local, so tests run in parallel without seeing each
//! other's hosts. Installing twice on one thread replaces the first; dropping
//! the session empties the slot.

use std::cell::RefCell;
use std::rc::Rc;

use serde_json::{json, Value};

use crate::error::Error;
use crate::host::{self, Transport, DONE};
use crate::run::Context;

/// One thing the code under test asked its host for.
#[derive(Clone, Debug)]
pub struct Call {
    /// The action name.
    pub name: String,
    /// The parameters, exactly as they were sent.
    pub params: Value,
}

/// How a test answers a call whose reply is a run of bytes.
type BytesReply = Box<dyn Fn(String, Value) -> Result<Vec<u8>, Error>>;

/// The shared middle of a session: what the transport writes and the test reads.
#[derive(Default)]
struct Recorder {
    calls: RefCell<Vec<Call>>,
    done: RefCell<Option<Value>>,
    request: RefCell<String>,
    context: RefCell<Context>,
    bytes: RefCell<Option<BytesReply>>,
}

/// A host that answers from a closure and remembers what it was asked.
struct Mock {
    reply: Box<dyn Fn(String, Value) -> Result<Value, Error>>,
    recorder: Rc<Recorder>,
}

impl Transport for Mock {
    /// The closure's answer, wrapped in the envelope a host would have sent and
    /// then unwrapped by the same code the guest uses.
    ///
    /// Going the long way round is deliberate: a test exercises the same reply
    /// ladder that runs in production, so what it proves about a handler holds
    /// for the handler as shipped.
    fn call(&self, name: String, params: Value) -> Result<Value, Error> {
        if let Ok(mut calls) = self.recorder.calls.try_borrow_mut() {
            calls.push(Call {
                name: name.clone(),
                params: params.clone(),
            });
        }

        let reply = match (self.reply)(name.clone(), params) {
            Ok(data) => json!({ "ok": true, "data": data }),
            Err(refusal) => json!({ "ok": false, "error": { "message": refusal.message() } }),
        };

        host::unwrap_reply(&name, reply)
    }

    /// The bytes closure's answer, appended as the real transport appends them.
    /// A refusal takes the same envelope ladder as [`Mock::call`], so a test
    /// sees the error production would have raised.
    fn call_bytes(&self, name: String, params: Value, into: &mut Vec<u8>) -> Result<usize, Error> {
        if let Ok(mut calls) = self.recorder.calls.try_borrow_mut() {
            calls.push(Call {
                name: name.clone(),
                params: params.clone(),
            });
        }

        let answer = match self.recorder.bytes.try_borrow() {
            Ok(reply) => match reply.as_ref() {
                Some(reply) => reply(name.clone(), params),
                None => {
                    return Err(Error::failed(format!(
                        "{name} answers with bytes, and this session was given no way to."
                    ))
                    .hint("Answer it with Session::with_bytes."))
                }
            },
            Err(_busy) => {
                return Err(Error::failed(format!(
                    "{name} was called from inside the session's own reply."
                )))
            }
        };

        match answer {
            Ok(bytes) => {
                into.extend_from_slice(&bytes);
                Ok(bytes.len())
            }
            Err(refusal) => host::unwrap_reply(
                &name,
                json!({ "ok": false, "error": { "message": refusal.message() } }),
            )
            .map(|_never| 0),
        }
    }

    fn cast(&self, name: String, params: Value) {
        if name == DONE {
            if let Ok(mut done) = self.recorder.done.try_borrow_mut() {
                *done = Some(params);
            }

            return;
        }

        if let Ok(mut calls) = self.recorder.calls.try_borrow_mut() {
            calls.push(Call { name, params });
        }
    }

    fn context(&self) -> Option<String> {
        let request = self.recorder.request.try_borrow().ok()?.clone();
        let context = self.recorder.context.try_borrow().ok()?.clone();

        Some(
            json!({
                "context": context,
                "data": request,
                "headers": {},
            })
            .to_string(),
        )
    }
}

/// An installed host, for as long as this value is alive.
///
/// Dropping it empties the slot, so a session cannot outlive the test that made
/// it. Hold it in a binding — `let session = ...` or `let _session = ...` — and
/// not in `let _ = ...`, which drops it immediately.
pub struct Session {
    recorder: Rc<Recorder>,
}

impl Session {
    /// Everything the code under test asked the host for, in order.
    ///
    /// The `__done__` report is not among them; it is [`Session::done`].
    pub fn calls(&self) -> Vec<Call> {
        self.recorder
            .calls
            .try_borrow()
            .map(|calls| calls.clone())
            .unwrap_or_default()
    }

    /// The `__done__` document, once [`crate::run`] has reported one.
    pub fn done(&self) -> Option<Value> {
        self.recorder
            .done
            .try_borrow()
            .ok()
            .and_then(|done| done.clone())
    }

    /// The payload [`crate::run`] will hand the handler.
    pub fn with_request(self, data: Value) -> Session {
        if let Ok(mut request) = self.recorder.request.try_borrow_mut() {
            *request = data.to_string();
        }

        self
    }

    /// Answer the calls whose reply is a run of bytes, such as a read of a
    /// stored file's range, from `reply`.
    ///
    /// `reply` is handed the action name and its parameters, and answers with
    /// the bytes the host would have written or an [`Error`] for a host that
    /// refused. Every other call is still answered by the closure the session
    /// was installed with.
    pub fn with_bytes<F>(self, reply: F) -> Session
    where
        F: Fn(String, Value) -> Result<Vec<u8>, Error> + 'static,
    {
        if let Ok(mut slot) = self.recorder.bytes.try_borrow_mut() {
            *slot = Some(Box::new(reply));
        }

        self
    }

    /// The execution context [`crate::run`] will hand the handler.
    pub fn with_context(self, context: Context) -> Session {
        if let Ok(mut slot) = self.recorder.context.try_borrow_mut() {
            *slot = context;
        }

        self
    }
}

impl Drop for Session {
    fn drop(&mut self) {
        host::set(None);
    }
}

/// Install a host for this thread that answers from `reply`.
///
/// `reply` is handed the action name and its parameters, and answers with the
/// data the host would have produced or an [`Error`] for a host that refused.
pub fn install<F>(reply: F) -> Session
where
    F: Fn(String, Value) -> Result<Value, Error> + 'static,
{
    let recorder = Rc::new(Recorder {
        request: RefCell::new("null".to_string()),
        ..Recorder::default()
    });

    host::set(Some(Rc::new(Mock {
        reply: Box::new(reply),
        recorder: Rc::clone(&recorder),
    })));

    Session { recorder }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::run::Request;

    #[test]
    fn a_session_records_what_it_was_asked() {
        let session = install(|_name, _params| Ok(json!({ "rows": [] })));

        let _: Value = crate::graphql::query("query Q { rows { a } }", json!({ "x": 1 })).unwrap();

        let calls = session.calls();

        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name, "action:db/execute");
        assert_eq!(calls[0].params["variables"], json!({ "x": 1 }));
    }

    #[test]
    fn a_refusing_host_reaches_the_caller_as_an_error() {
        let _session = install(|_name, _params| Err(Error::denied("nope")));

        let error = crate::graphql::query::<Value>("query Q { a }", json!({})).unwrap_err();

        assert!(error.message().contains("nope"));
    }

    #[test]
    fn the_slot_is_empty_once_the_session_is_dropped() {
        {
            let _session = install(|_name, _params| Ok(json!(null)));
            assert!(host::transport().is_ok());
        }

        assert!(host::transport().is_err());
    }

    #[test]
    fn run_reports_a_success_envelope() {
        let session = install(|_name, _params| Ok(json!(null))).with_request(json!({ "a": 1 }));

        crate::run(|request: Request<Value>| Ok(json!({ "seen": request.data })));

        assert_eq!(
            session.done().unwrap(),
            json!({ "data": { "seen": { "a": 1 } }, "errors": [], "ok": true })
        );
    }

    #[test]
    fn run_reports_a_failure_envelope_and_still_says_ok() {
        let session = install(|_name, _params| Ok(json!(null))).with_request(json!({}));

        crate::run(|_request: Request<Value>| {
            Err::<Value, Error>(Error::invalid("no id").hint("Send an id."))
        });

        let done = session.done().unwrap();

        assert_eq!(done["ok"], json!(true));
        assert_eq!(
            done["data"]["error"]["extensions"]["code"],
            json!("INVALID_TOOL_INPUT")
        );
        assert_eq!(
            done["data"]["error"]["extensions"]["retryable"],
            json!(false)
        );
        assert_eq!(
            done["data"]["error"]["extensions"]["hint"],
            json!("Send an id.")
        );
    }

    #[test]
    fn run_reports_exactly_once_even_when_the_handler_panics() {
        let session = install(|_name, _params| Ok(json!(null))).with_request(json!({}));

        crate::run(|_request: Request<Value>| -> Result<Value, Error> {
            panic!("the action fell over")
        });

        let done = session.done().unwrap();

        assert!(done["data"]["error"]["message"]
            .as_str()
            .unwrap()
            .contains("the action fell over"));
    }

    #[test]
    fn a_payload_that_does_not_match_never_reaches_the_handler() {
        let session = install(|_name, _params| Ok(json!(null))).with_request(json!({ "a": 1 }));

        #[derive(Debug, serde::Deserialize)]
        struct Needs {
            #[allow(dead_code)]
            required: String,
        }

        crate::run(|_request: Request<Needs>| -> Result<Value, Error> {
            panic!("the handler must not run")
        });

        assert_eq!(
            session.done().unwrap()["data"]["error"]["extensions"]["code"],
            json!("INVALID_TOOL_INPUT")
        );
    }

    #[test]
    fn the_context_the_session_was_given_reaches_the_handler() {
        let mut context = Context::default();
        context.logic.execution_id = "EXEC9".to_string();
        context.tenant.name = "acme".to_string();

        let session = install(|_name, _params| Ok(json!(null)))
            .with_request(json!({}))
            .with_context(context);

        crate::run(|request: Request<Value>| {
            Ok(json!({
                "execution": request.context.logic.execution_id,
                "tenant": request.context.tenant.name,
            }))
        });

        assert_eq!(
            session.done().unwrap()["data"],
            json!({ "execution": "EXEC9", "tenant": "acme" })
        );
    }
}