Skip to main content

mobiler_core/
lib.rs

1//! Mobiler runtime — the developer-facing API.
2//!
3//! Implement [`MobilerApp`] with your **typed** events, model, and view (built
4//! from the [builders](#functions)). Mobiler wraps it in [`MobilerShell`], a
5//! Crux app speaking the fixed UI ABI ([`mobiler_ui`]); you never touch the wire
6//! protocol. Device APIs are capabilities via [`Cx`].
7
8use std::marker::PhantomData;
9
10use crux_core::{
11    App, Command,
12    capability::Operation,
13    macros::effect,
14    render::{RenderOperation, render},
15};
16use facet::Facet;
17use serde::{Deserialize, Serialize, de::DeserializeOwned};
18
19pub use mobiler_ui::{
20    Action, BoxAlign, ButtonStyle, CardStyle, Icon, ImageRatio, ImageShape, InputValue,
21    ProjectColor, Spacing, Tab, TextStyle, Tone, Widget,
22};
23
24// ============================ capabilities ============================
25
26/// Built-in capabilities the generic shell fulfils.
27#[effect(facet_typegen)]
28#[derive(Debug)]
29pub enum Effect {
30    Render(RenderOperation),
31    /// Fire-and-forget plugin call (shell does not resolve).
32    PluginNotify(PluginNotify),
33    /// Request/response plugin call (shell resolves with a [`PluginResponse`]).
34    Plugin(PluginCall),
35}
36
37#[derive(Facet, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
38pub struct PluginNotify {
39    pub plugin: String,
40    pub op: String,
41    pub input: String,
42}
43impl Operation for PluginNotify {
44    type Output = ();
45}
46
47#[derive(Facet, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
48pub struct PluginCall {
49    pub plugin: String,
50    pub op: String,
51    pub input: String,
52}
53impl Operation for PluginCall {
54    type Output = PluginResponse;
55}
56
57#[derive(Facet, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
58pub struct PluginResponse {
59    pub ok: bool,
60    pub output: String,
61}
62
63type Continuation<E> = Box<dyn FnOnce(PluginResponse) -> E + Send>;
64
65/// Effects an app requests during `update`, generic over the app event type so
66/// continuations stay fully typed.
67pub struct Cx<E> {
68    notifications: Vec<PluginNotify>,
69    requests: Vec<(PluginCall, Continuation<E>)>,
70}
71
72impl<E> Default for Cx<E> {
73    fn default() -> Self {
74        Self { notifications: Vec::new(), requests: Vec::new() }
75    }
76}
77
78impl<E> Cx<E> {
79    /// Fire-and-forget call to a native plugin.
80    pub fn notify(&mut self, plugin: impl Into<String>, op: impl Into<String>, input: impl Into<String>) {
81        self.notifications.push(PluginNotify { plugin: plugin.into(), op: op.into(), input: input.into() });
82    }
83
84    /// Request/response call: when the plugin replies, `then(response)` produces
85    /// the typed event delivered back to your `update`.
86    pub fn plugin(
87        &mut self,
88        plugin: impl Into<String>,
89        op: impl Into<String>,
90        input: impl Into<String>,
91        then: impl FnOnce(PluginResponse) -> E + Send + 'static,
92    ) {
93        self.requests
94            .push((PluginCall { plugin: plugin.into(), op: op.into(), input: input.into() }, Box::new(then)));
95    }
96
97    /// Persist `data` (handed back to [`MobilerApp::restore`] on next startup).
98    pub fn save(&mut self, data: impl Into<String>) {
99        self.notify("storage", "save", data);
100    }
101
102    /// Copy `text` to the system clipboard (built-in `clipboard` capability).
103    pub fn copy(&mut self, text: impl Into<String>) {
104        self.notify("clipboard", "copy", text);
105    }
106
107    /// Open the system share sheet with `text` (built-in `share` capability).
108    pub fn share(&mut self, text: impl Into<String>) {
109        self.notify("share", "text", text);
110    }
111
112    /// Open `url` in the platform browser / default handler (built-in `browser`
113    /// capability). Fire-and-forget: the app leaves the foreground.
114    pub fn open_url(&mut self, url: impl Into<String>) {
115        self.notify("browser", "open", url);
116    }
117
118    /// Show a transient toast / snackbar with `text` (built-in `toast` capability).
119    pub fn toast(&mut self, text: impl Into<String>) {
120        self.notify("toast", "show", text);
121    }
122
123    /// Fire a haptic tap (built-in `haptics` capability). `style` is `"light"`,
124    /// `"medium"`, or `"heavy"`; unknown styles fall back to medium.
125    pub fn haptic(&mut self, style: impl Into<String>) {
126        self.notify("haptics", style, "");
127    }
128
129    /// Perform an HTTP request via the shell's built-in `http` capability. When it
130    /// completes, `then(response)` produces the typed event delivered back to
131    /// `update` — `response.output` is the body, `response.ok` is success (2xx).
132    /// Rides the request/response plugin mechanism, so it resolves asynchronously.
133    pub fn http(
134        &mut self,
135        method: impl Into<String>,
136        url: impl Into<String>,
137        body: Option<String>,
138        then: impl FnOnce(PluginResponse) -> E + Send + 'static,
139    ) {
140        #[derive(Serialize)]
141        struct HttpReq {
142            url: String,
143            body: Option<String>,
144        }
145        let input = serde_json::to_string(&HttpReq { url: url.into(), body })
146            .expect("serialize http request");
147        self.plugin("http", method, input, then);
148    }
149
150    /// `GET url`, delivering the response to `then`.
151    pub fn get(&mut self, url: impl Into<String>, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
152        self.http("GET", url, None, then);
153    }
154    /// `POST url` with a JSON `body`, delivering the response to `then`.
155    pub fn post(&mut self, url: impl Into<String>, body: impl Into<String>, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
156        self.http("POST", url, Some(body.into()), then);
157    }
158    /// `PATCH url` with a JSON `body`, delivering the response to `then`.
159    pub fn patch(&mut self, url: impl Into<String>, body: impl Into<String>, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
160        self.http("PATCH", url, Some(body.into()), then);
161    }
162    /// `DELETE url`, delivering the response to `then`.
163    pub fn delete(&mut self, url: impl Into<String>, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
164        self.http("DELETE", url, None, then);
165    }
166
167    /// Query the device model/name via the built-in `device` capability; the result
168    /// (`response.output`, e.g. "Google Pixel 7" / "Apple iPhone (iOS 18.0)") is
169    /// delivered to `then`.
170    pub fn device_model(&mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
171        self.plugin("device", "model", "", then);
172    }
173
174    /// Let the user pick an image (built-in `photo` capability — the system photo
175    /// picker, no permission required). `then` receives the result: on success
176    /// `response.ok` is `true` and `response.output` is a local image URI you can
177    /// hand straight to the `image(...)` widget; on cancel, `ok` is `false`.
178    pub fn pick_photo(&mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static) {
179        self.plugin("photo", "pick", "", then);
180    }
181
182    /// Ask the user to confirm via a native dialog (built-in `dialog` capability).
183    /// `then` receives the choice: `response.ok` is `true` if confirmed, `false` if
184    /// cancelled/dismissed. Resolves asynchronously (the user replies whenever).
185    pub fn confirm(
186        &mut self,
187        title: impl Into<String>,
188        message: impl Into<String>,
189        then: impl FnOnce(PluginResponse) -> E + Send + 'static,
190    ) {
191        #[derive(Serialize)]
192        struct Confirm {
193            title: String,
194            message: String,
195        }
196        let input = serde_json::to_string(&Confirm { title: title.into(), message: message.into() })
197            .expect("serialize confirm");
198        self.plugin("dialog", "confirm", input, then);
199    }
200}
201
202// ============================ the app trait ============================
203
204/// What a Mobiler app implements. Write typed domain events; Mobiler serializes
205/// them into opaque tokens behind the scenes.
206pub trait MobilerApp: Default {
207    type Event: Serialize + DeserializeOwned + Send + 'static;
208    type Model: Default;
209
210    fn update(&self, event: Self::Event, model: &mut Self::Model, cx: &mut Cx<Self::Event>);
211
212    fn input(&self, id: &str, value: InputValue, model: &mut Self::Model, cx: &mut Cx<Self::Event>) {
213        let _ = (id, value, model, cx);
214    }
215
216    /// Restore persisted state on startup. `data` is whatever you last passed to
217    /// `cx.save` (or empty if nothing was saved). Default: ignore.
218    fn restore(&self, data: &str, model: &mut Self::Model) {
219        let _ = (data, model);
220    }
221
222    /// Run once on startup, after [`restore`](Self::restore). The place to kick
223    /// off initial effects — e.g. fetch data with `cx.get`. Default: nothing.
224    fn init(&self, model: &mut Self::Model, cx: &mut Cx<Self::Event>) {
225        let _ = (model, cx);
226    }
227
228    fn view(&self, model: &Self::Model) -> Widget;
229}
230
231/// Crux adapter: turns a [`MobilerApp`] into an app speaking the fixed ABI.
232pub struct MobilerShell<A>(PhantomData<fn() -> A>);
233
234impl<A> Default for MobilerShell<A> {
235    fn default() -> Self {
236        Self(PhantomData)
237    }
238}
239
240impl<A: MobilerApp> App for MobilerShell<A> {
241    type Event = Action;
242    type Model = A::Model;
243    type ViewModel = Widget;
244    type Effect = Effect;
245
246    fn update(&self, action: Action, model: &mut Self::Model) -> Command<Effect, Action> {
247        let app = A::default();
248        let mut cx = Cx::<A::Event>::default();
249        match action {
250            Action::Fired { token } => {
251                if let Ok(event) = serde_json::from_str::<A::Event>(&token) {
252                    app.update(event, model, &mut cx);
253                }
254            }
255            Action::Input { id, value } => app.input(&id, value, model, &mut cx),
256            Action::Restore { data } => app.restore(&data, model),
257            Action::Start => app.init(model, &mut cx),
258        }
259        let mut commands: Vec<Command<Effect, Action>> = Vec::new();
260        for op in cx.notifications {
261            commands.push(Command::notify_shell(op).build());
262        }
263        for (op, then) in cx.requests {
264            commands.push(Command::request_from_shell(op).then_send(move |response: PluginResponse| {
265                Action::Fired { token: serde_json::to_string(&then(response)).expect("serialize event") }
266            }));
267        }
268        commands.push(render());
269        Command::all(commands)
270    }
271
272    fn view(&self, model: &Self::Model) -> Widget {
273        A::default().view(model)
274    }
275}
276
277// ============================ navigation ============================
278
279/// A navigation stack the app holds in its `Model`. The **core owns the stack**
280/// (single source of truth); the framework reads its `route`/`depth` to drive
281/// the shell's push/pop transitions and back button.
282///
283/// `R` is your screen-route type (typically a small enum). Hold it in the model,
284/// mutate it in `update` (`push`/`pop`/`reset`), match `current()` in `view`, and
285/// build the shell with [`nav_scaffold`]. Wire a `Msg::Back` (or similar) event to
286/// `pop` so the back affordance works.
287///
288/// ```ignore
289/// #[derive(Clone, Serialize)] enum Route { List, Detail(u32) }
290/// // model.nav: Nav<Route> = Nav::new(Route::List);
291/// // update: Msg::Open(id) => model.nav.push(Route::Detail(id)),
292/// //         Msg::Back      => model.nav.pop(),
293/// // view:   nav_scaffold(title, dark, tabs, body, &model.nav, Msg::Back)
294/// ```
295#[derive(Clone, Debug)]
296pub struct Nav<R> {
297    stack: Vec<R>,
298}
299
300impl<R: Clone + Serialize> Nav<R> {
301    /// A stack containing a single root route.
302    #[must_use]
303    pub fn new(root: R) -> Self {
304        Self { stack: vec![root] }
305    }
306    /// Push a new screen onto the stack.
307    pub fn push(&mut self, route: R) {
308        self.stack.push(route);
309    }
310    /// Pop the top screen (no-op at the root).
311    pub fn pop(&mut self) {
312        if self.stack.len() > 1 {
313            self.stack.pop();
314        }
315    }
316    /// Replace the whole stack with a fresh root (e.g. switching bottom-nav tabs).
317    pub fn reset(&mut self, root: R) {
318        self.stack = vec![root];
319    }
320    /// The current (top) route — what `view` should render.
321    #[must_use]
322    pub fn current(&self) -> &R {
323        self.stack.last().expect("nav stack is never empty")
324    }
325    /// Stack depth (root = 1).
326    #[must_use]
327    pub fn depth(&self) -> u32 {
328        self.stack.len() as u32
329    }
330    /// Whether there is a screen to pop back to.
331    #[must_use]
332    pub fn can_go_back(&self) -> bool {
333        self.stack.len() > 1
334    }
335    /// Stable identity of the current route (its serialization), used by the shell
336    /// to decide when to animate a transition.
337    fn route_key(&self) -> String {
338        serde_json::to_string(self.current()).expect("serialize route")
339    }
340}
341
342// ============================ widget builders ============================
343// Action-carrying builders take a TYPED event and serialize it into a token.
344
345fn tok<E: Serialize>(event: E) -> String {
346    serde_json::to_string(&event).expect("serialize event")
347}
348
349#[must_use]
350pub fn styled(content: impl Into<String>, style: TextStyle) -> Widget {
351    Widget::Text { content: content.into(), style }
352}
353#[must_use]
354pub fn text(content: impl Into<String>) -> Widget { styled(content, TextStyle::Body) }
355#[must_use]
356pub fn title(content: impl Into<String>) -> Widget { styled(content, TextStyle::Title) }
357#[must_use]
358pub fn subtitle(content: impl Into<String>) -> Widget { styled(content, TextStyle::Subtitle) }
359#[must_use]
360pub fn caption(content: impl Into<String>) -> Widget { styled(content, TextStyle::Caption) }
361#[must_use]
362pub fn emphasis(content: impl Into<String>) -> Widget { styled(content, TextStyle::Emphasis) }
363
364#[must_use]
365pub fn image(source: impl Into<String>, shape: ImageShape, ratio: ImageRatio) -> Widget {
366    Widget::Image { source: source.into(), shape, ratio }
367}
368#[must_use]
369pub fn badge(label: impl Into<String>, tone: Tone) -> Widget {
370    Widget::Badge { label: label.into(), tone }
371}
372/// A small colored identity dot.
373#[must_use]
374pub fn color_dot(color: ProjectColor) -> Widget {
375    Widget::ColorDot { color }
376}
377#[must_use]
378pub fn divider() -> Widget { Widget::Divider }
379#[must_use]
380pub fn spacer(size: Spacing) -> Widget { Widget::Spacer { size } }
381
382#[must_use]
383pub fn row(children: Vec<Widget>) -> Widget { Widget::Row { children } }
384#[must_use]
385pub fn column(children: Vec<Widget>) -> Widget { Widget::Column { children } }
386#[must_use]
387pub fn card(child: Widget, style: CardStyle) -> Widget {
388    Widget::Card { child: Box::new(child), style, on_press: None }
389}
390/// A tappable card carrying a typed press event.
391#[must_use]
392pub fn card_button<E: Serialize>(child: Widget, style: CardStyle, on_press: E) -> Widget {
393    Widget::Card { child: Box::new(child), style, on_press: Some(tok(on_press)) }
394}
395/// Z-stack/overlay (the `Box` widget). With `scrim`, the first child is a
396/// darkened background and the rest render on top.
397#[must_use]
398pub fn stack(align: BoxAlign, scrim: bool, children: Vec<Widget>) -> Widget {
399    Widget::Box { children, align, scrim }
400}
401#[must_use]
402pub fn grid(children: Vec<Widget>) -> Widget { Widget::Grid { children } }
403
404#[must_use]
405pub fn button<E: Serialize>(label: impl Into<String>, style: ButtonStyle, on_press: E) -> Widget {
406    Widget::Button { label: label.into(), style, on_press: tok(on_press) }
407}
408#[must_use]
409pub fn icon_button<E: Serialize>(icon: Icon, on_press: E) -> Widget {
410    Widget::IconButton { icon, on_press: tok(on_press) }
411}
412#[must_use]
413pub fn chip<E: Serialize>(label: impl Into<String>, selected: bool, on_press: E) -> Widget {
414    Widget::Chip { label: label.into(), selected, on_press: tok(on_press) }
415}
416#[must_use]
417pub fn text_field(id: impl Into<String>, placeholder: impl Into<String>, value: impl Into<String>) -> Widget {
418    Widget::TextField { id: id.into(), placeholder: placeholder.into(), value: value.into() }
419}
420#[must_use]
421pub fn toggle(id: impl Into<String>, label: impl Into<String>, value: bool) -> Widget {
422    Widget::Toggle { id: id.into(), label: label.into(), value }
423}
424#[must_use]
425pub fn checkbox(id: impl Into<String>, label: impl Into<String>, value: bool) -> Widget {
426    Widget::Checkbox { id: id.into(), label: label.into(), value }
427}
428#[must_use]
429pub fn slider(id: impl Into<String>, value: i32, max: i32) -> Widget {
430    Widget::Slider { id: id.into(), value, max }
431}
432#[must_use]
433pub fn stepper<E: Serialize>(value: i32, on_decrement: E, on_increment: E) -> Widget {
434    Widget::Stepper { value, on_decrement: tok(on_decrement), on_increment: tok(on_increment) }
435}
436
437/// A bottom-nav tab carrying a typed selection event.
438#[must_use]
439pub fn tab<E: Serialize>(label: impl Into<String>, selected: bool, on_select: E) -> Tab {
440    Tab { label: label.into(), selected, on_select: tok(on_select) }
441}
442
443/// App shell: top bar + bottom-nav `tabs` + scrollable `body`. `dark_mode` is
444/// theme-as-data (the shell themes the whole app from it).
445#[must_use]
446pub fn scaffold(title: impl Into<String>, dark_mode: bool, tabs: Vec<Tab>, body: Widget) -> Widget {
447    let title = title.into();
448    // route defaults to the title; root depth = 1.
449    Widget::Scaffold { route: title.clone(), title, body: Box::new(body), tabs, back: None, dark_mode, depth: 1 }
450}
451
452/// Like [`scaffold`], but the top bar (and the system back button) navigate back
453/// via `back` — e.g. a detail screen pushed over a tab (treated as depth 2).
454/// For multi-level stacks, drive navigation with [`Nav`] + [`nav_scaffold`].
455#[must_use]
456pub fn scaffold_back<E: Serialize>(title: impl Into<String>, dark_mode: bool, tabs: Vec<Tab>, body: Widget, back: E) -> Widget {
457    let title = title.into();
458    Widget::Scaffold { route: title.clone(), title, body: Box::new(body), tabs, back: Some(tok(back)), dark_mode, depth: 2 }
459}
460
461/// Scaffold driven by a [`Nav`] stack: fills `route` (from the current route's
462/// serialization) and `depth` (stack depth) so the shell animates transitions,
463/// and shows a back affordance (top-bar arrow + system back button) firing
464/// `on_back` whenever the stack can pop.
465#[must_use]
466pub fn nav_scaffold<R, E>(
467    title: impl Into<String>,
468    dark_mode: bool,
469    tabs: Vec<Tab>,
470    body: Widget,
471    nav: &Nav<R>,
472    on_back: E,
473) -> Widget
474where
475    R: Clone + Serialize,
476    E: Serialize,
477{
478    Widget::Scaffold {
479        title: title.into(),
480        body: Box::new(body),
481        tabs,
482        back: if nav.can_go_back() { Some(tok(on_back)) } else { None },
483        dark_mode,
484        route: nav.route_key(),
485        depth: nav.depth(),
486    }
487}
488
489#[cfg(test)]
490mod tests {
491    use super::*;
492    use serde::Serialize;
493
494    #[derive(Clone, Copy, Serialize, PartialEq, Debug)]
495    enum Route {
496        Home,
497        Detail(u32),
498    }
499
500    #[derive(Serialize)]
501    enum Ev {
502        Tap,
503        Open(u32),
504    }
505
506    // ---- Nav ----
507
508    #[test]
509    fn nav_push_pop_depth() {
510        let mut nav = Nav::new(Route::Home);
511        assert_eq!(nav.depth(), 1);
512        assert!(!nav.can_go_back());
513
514        nav.push(Route::Detail(7));
515        assert_eq!(nav.depth(), 2);
516        assert!(nav.can_go_back());
517        assert!(matches!(nav.current(), Route::Detail(7)));
518
519        nav.pop();
520        assert_eq!(nav.depth(), 1);
521        assert!(matches!(nav.current(), Route::Home));
522
523        nav.pop(); // no-op at the root
524        assert_eq!(nav.depth(), 1);
525    }
526
527    #[test]
528    fn nav_reset_replaces_stack() {
529        let mut nav = Nav::new(Route::Home);
530        nav.push(Route::Detail(1));
531        nav.push(Route::Detail(2));
532        nav.reset(Route::Detail(9));
533        assert_eq!(nav.depth(), 1);
534        assert!(matches!(nav.current(), Route::Detail(9)));
535    }
536
537    #[test]
538    fn nav_route_key_is_serialization() {
539        let nav = Nav::new(Route::Detail(3));
540        assert_eq!(nav.route_key(), serde_json::to_string(&Route::Detail(3)).unwrap());
541    }
542
543    // ---- builders ----
544
545    #[test]
546    fn scaffold_sets_route_depth_and_no_back() {
547        match scaffold("Home", false, vec![], text("x")) {
548            Widget::Scaffold { route, depth, back, dark_mode, .. } => {
549                assert_eq!(route, "Home");
550                assert_eq!(depth, 1);
551                assert!(back.is_none());
552                assert!(!dark_mode);
553            }
554            other => panic!("expected Scaffold, got {other:?}"),
555        }
556    }
557
558    #[test]
559    fn scaffold_back_is_depth_2_with_back() {
560        match scaffold_back("Detail", true, vec![], text("x"), Ev::Tap) {
561            Widget::Scaffold { depth, back, dark_mode, .. } => {
562                assert_eq!(depth, 2);
563                assert_eq!(back, Some(serde_json::to_string(&Ev::Tap).unwrap()));
564                assert!(dark_mode);
565            }
566            other => panic!("expected Scaffold, got {other:?}"),
567        }
568    }
569
570    #[test]
571    fn nav_scaffold_shows_back_only_when_poppable() {
572        let mut nav = Nav::new(Route::Home);
573        // at the root: no back, depth 1, route = serialized current route
574        match nav_scaffold("T", false, vec![], text("x"), &nav, Ev::Tap) {
575            Widget::Scaffold { back, depth, route, .. } => {
576                assert!(back.is_none());
577                assert_eq!(depth, 1);
578                assert_eq!(route, serde_json::to_string(&Route::Home).unwrap());
579            }
580            other => panic!("expected Scaffold, got {other:?}"),
581        }
582        // after a push: back present, depth 2
583        nav.push(Route::Detail(2));
584        match nav_scaffold("T", false, vec![], text("x"), &nav, Ev::Tap) {
585            Widget::Scaffold { back, depth, .. } => {
586                assert_eq!(back, Some(serde_json::to_string(&Ev::Tap).unwrap()));
587                assert_eq!(depth, 2);
588            }
589            other => panic!("expected Scaffold, got {other:?}"),
590        }
591    }
592
593    #[test]
594    fn buttons_carry_serialized_event_tokens() {
595        match button("Go", ButtonStyle::Filled, Ev::Open(5)) {
596            Widget::Button { label, on_press, .. } => {
597                assert_eq!(label, "Go");
598                assert_eq!(on_press, serde_json::to_string(&Ev::Open(5)).unwrap());
599            }
600            other => panic!("expected Button, got {other:?}"),
601        }
602        match card_button(text("c"), CardStyle::Elevated, Ev::Tap) {
603            Widget::Card { on_press, .. } => {
604                assert_eq!(on_press, Some(serde_json::to_string(&Ev::Tap).unwrap()));
605            }
606            other => panic!("expected Card, got {other:?}"),
607        }
608        // a plain card is not tappable
609        match card(text("c"), CardStyle::Elevated) {
610            Widget::Card { on_press, .. } => assert!(on_press.is_none()),
611            other => panic!("expected Card, got {other:?}"),
612        }
613    }
614
615    // ---- Cx capabilities ----
616
617    #[test]
618    fn cx_notify_and_save_enqueue_notifications() {
619        let mut cx = Cx::<Ev>::default();
620        cx.notify("toast", "show", "hi");
621        cx.save("blob");
622        assert_eq!(cx.notifications.len(), 2);
623        assert_eq!(cx.notifications[0], PluginNotify { plugin: "toast".into(), op: "show".into(), input: "hi".into() });
624        assert_eq!(cx.notifications[1], PluginNotify { plugin: "storage".into(), op: "save".into(), input: "blob".into() });
625        assert!(cx.requests.is_empty());
626    }
627
628    #[test]
629    fn cx_http_helpers_build_requests() {
630        let mut cx = Cx::<Ev>::default();
631        cx.get("http://h/x", |_| Ev::Tap);
632        cx.post("http://h/y", "hello", |_| Ev::Tap);
633        cx.patch("http://h/z", "patch", |_| Ev::Tap);
634        cx.delete("http://h/d", |_| Ev::Tap);
635
636        let methods: Vec<&str> = cx.requests.iter().map(|(c, _)| c.op.as_str()).collect();
637        assert_eq!(methods, ["GET", "POST", "PATCH", "DELETE"]);
638        assert!(cx.requests.iter().all(|(c, _)| c.plugin == "http"));
639
640        let get_input: serde_json::Value = serde_json::from_str(&cx.requests[0].0.input).unwrap();
641        assert_eq!(get_input["url"], "http://h/x");
642        assert!(get_input["body"].is_null());
643
644        let post_input: serde_json::Value = serde_json::from_str(&cx.requests[1].0.input).unwrap();
645        assert_eq!(post_input["url"], "http://h/y");
646        assert_eq!(post_input["body"], "hello");
647    }
648}