Expand description
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.
Structs§
- Call
- One thing the code under test asked its host for.
- Session
- An installed host, for as long as this value is alive.
Functions§
- install
- Install a host for this thread that answers from
reply.