renox 1.1.0

A batteries-included web framework for Rust, inspired by Laravel: Axum, HTMX and Alpine.js, SQLite or PostgreSQL, deployed as one binary.
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! Registering live components.

use renox::live_component::LiveContext;
use renox::prelude::*;
use renox::testing::TestApp;

#[derive(serde::Serialize, serde::Deserialize, Default)]
struct Counter {
    count: i64,
    name: String,
    search: String,
}

impl Validate for Counter {
    fn rules(&self, v: &mut Validator) {
        v.field("name", &self.name).required();
    }
}

impl LiveComponent for Counter {
    const NAME: &'static str = "counter";
    const VIEW: &'static str = "live/counter.html";

    async fn data(&self, _ctx: &LiveContext) -> Result<serde_json::Value> {
        Ok(json!({ "label": "clicks" }))
    }

    async fn call(
        &mut self,
        action: &str,
        args: Vec<serde_json::Value>,
        ctx: &mut LiveContext,
    ) -> Result {
        match action {
            "increment" => {
                self.count += 1;
                Ok(())
            }
            "add" => {
                let n: i64 = serde_json::from_value(args.first().cloned().unwrap_or_default())
                    .map_err(|e| Error::BadRequest(e.to_string()))?;
                self.count += n;
                Ok(())
            }
            "save" => ctx.validate(&*self).await,
            "notify" => {
                ctx.toast(Toast::success("Done"));
                Ok(())
            }
            "go" => {
                ctx.redirect("/elsewhere");
                Ok(())
            }
            "ping" => {
                ctx.dispatch("pinged", json!({ "n": 1 }));
                Ok(())
            }
            "_private" => Ok(()),
            _ => Err(Error::NotFound),
        }
    }
}

#[renox::test]
async fn a_component_registers_through_the_app() {
    let app = App::with_config(Config::default()).live_component::<Counter>();
    assert!(app.boot().await.is_ok());
}

#[renox::test]
async fn a_duplicate_component_is_a_boot_error() {
    let err = App::with_config(Config::default())
        .live_component::<Counter>()
        .live_component::<Counter>()
        .boot()
        .await
        .err()
        .unwrap();
    assert!(
        format!("{err:?}").contains("live component `counter` is registered twice"),
        "{err:?}"
    );
}

async fn counter(ctx: LiveContext) -> Result<View> {
    let list = ctx
        .mount(Counter {
            count: 2,
            ..Default::default()
        })
        .await?;
    Ok(view("page.html", context! { list }))
}

struct Pages;

impl Module for Pages {
    fn name(&self) -> &'static str {
        "pages"
    }

    fn routes(&self) -> Routes {
        Routes::new().get("/counter", counter)
    }
}

#[renox::test]
async fn a_page_mounts_a_component() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::create_dir_all(dir.path().join("live")).unwrap();
    std::fs::write(
        dir.path().join("page.html"),
        r#"{% set component = list %}{% include "renox/live.html" %}"#,
    )
    .unwrap();
    std::fs::write(
        dir.path().join("live/counter.html"),
        "<b>{{ state.count }}</b>{{ data.label }}{{ csrf_field() }}",
    )
    .unwrap();
    let path = dir.path().to_path_buf();
    let app = TestApp::with_config(
        App::new()
            .module(Auth::new())
            .module(Pages)
            .live_component::<Counter>(),
        move |c| c.views_path = path,
    )
    .await;
    let page = app.get("/counter").await;
    page.assert_ok()
        .assert_see(r#"data-rx-live="counter""#)
        .assert_see("<b>2</b>clicks")
        .assert_see(r#"name="_token""#)
        .assert_see("data-rx-snapshot=");
}

#[derive(serde::Serialize, serde::Deserialize)]
struct Other {
    count: i64,
}

impl LiveComponent for Other {
    const NAME: &'static str = "other";
    const VIEW: &'static str = "live/other.html";

    async fn call(&mut self, _: &str, _: Vec<serde_json::Value>, _: &mut LiveContext) -> Result {
        Ok(())
    }
}

async fn other(ctx: LiveContext) -> Result<View> {
    let list = ctx.mount(Other { count: 1 }).await?;
    Ok(view("page.html", context! { list }))
}

struct ActionPages;

impl Module for ActionPages {
    fn name(&self) -> &'static str {
        "action-pages"
    }

    fn routes(&self) -> Routes {
        Routes::new().get("/counter", counter).get("/other", other)
    }
}

async fn actions_app() -> (TestApp, tempfile::TempDir) {
    let dir = tempfile::tempdir().unwrap();
    std::fs::create_dir_all(dir.path().join("live")).unwrap();
    std::fs::write(
        dir.path().join("page.html"),
        r#"{% set component = list %}{% include "renox/live.html" %}"#,
    )
    .unwrap();
    std::fs::write(
        dir.path().join("live/counter.html"),
        "<b>{{ state.count }}</b>{{ state.search }}",
    )
    .unwrap();
    std::fs::write(dir.path().join("live/other.html"), "{{ state.count }}").unwrap();
    let path = dir.path().to_path_buf();
    let app = TestApp::with_config(
        App::new()
            .module(Auth::new())
            .module(ActionPages)
            .live_component::<Counter>()
            .live_component::<Other>(),
        move |c| c.views_path = path,
    )
    .await;
    (app, dir)
}

async fn snapshot_of(app: &TestApp, page: &str) -> String {
    let html = app.get(page).await.text();
    let rest = html.split("data-rx-snapshot=\"").nth(1).unwrap();
    rest.split('"').next().unwrap().to_string()
}

#[renox::test]
async fn an_action_updates_the_state_and_the_snapshot() {
    let (app, _dir) = actions_app().await;
    let s = snapshot_of(&app, "/counter").await;
    let res = app
        .htmx()
        .post(
            "/_renox/live/counter/add",
            &[("_snapshot", &s), ("_args", "[5]")],
        )
        .await;
    res.assert_ok().assert_see("<b>7</b>");
    assert!(!res.text().contains(&s));
    assert!(res.text().contains("data-rx-snapshot="));
}

#[renox::test]
async fn posted_fields_are_merged() {
    let (app, _dir) = actions_app().await;
    let s = snapshot_of(&app, "/counter").await;
    let res = app
        .htmx()
        .post(
            "/_renox/live/counter/_refresh",
            &[("_snapshot", &s), ("search", "tea")],
        )
        .await;
    res.assert_ok().assert_see("<b>2</b>tea");
}

#[renox::test]
async fn bad_input_is_a_400() {
    let (app, _dir) = actions_app().await;
    let s = snapshot_of(&app, "/counter").await;
    app.htmx()
        .post(
            "/_renox/live/counter/increment",
            &[("_snapshot", &s), ("count", "abc")],
        )
        .await
        .assert_status(400);
    app.htmx()
        .post(
            "/_renox/live/counter/increment",
            &[("_snapshot", &format!("{s}x"))],
        )
        .await
        .assert_status(400);
    let other = snapshot_of(&app, "/other").await;
    app.htmx()
        .post("/_renox/live/counter/increment", &[("_snapshot", &other)])
        .await
        .assert_status(400);
    app.htmx()
        .post("/_renox/live/counter/increment", &[])
        .await
        .assert_status(400);
    app.htmx()
        .post(
            "/_renox/live/counter/add",
            &[("_snapshot", &s), ("_args", "{}")],
        )
        .await
        .assert_status(400);
}

#[renox::test]
async fn unknown_and_private_actions_are_404() {
    let (app, _dir) = actions_app().await;
    let s = snapshot_of(&app, "/counter").await;
    for action in ["nope", "_private"] {
        app.htmx()
            .post(
                &format!("/_renox/live/counter/{action}"),
                &[("_snapshot", &s)],
            )
            .await
            .assert_not_found();
    }
    app.htmx()
        .post("/_renox/live/missing/increment", &[("_snapshot", &s)])
        .await
        .assert_not_found();
}

#[renox::test]
async fn a_failed_validation_is_a_422() {
    let (app, _dir) = actions_app().await;
    let s = snapshot_of(&app, "/counter").await;
    let res = app
        .htmx()
        .post("/_renox/live/counter/save", &[("_snapshot", &s)])
        .await;
    res.assert_status(422)
        .assert_json_path("errors.name", json!(["The name field is required."]));
}

#[renox::test]
async fn effects_ride_in_headers() {
    let (app, _dir) = actions_app().await;
    let s = snapshot_of(&app, "/counter").await;
    let toast = app
        .htmx()
        .post("/_renox/live/counter/notify", &[("_snapshot", &s)])
        .await;
    toast.assert_ok();
    assert!(toast.header("hx-trigger").unwrap().contains("renox:toast"));
    app.htmx()
        .post("/_renox/live/counter/go", &[("_snapshot", &s)])
        .await
        .assert_hx_redirect("/elsewhere");
    let ping = app
        .htmx()
        .post("/_renox/live/counter/ping", &[("_snapshot", &s)])
        .await;
    assert!(
        ping.header("hx-trigger")
            .unwrap()
            .contains("rx:counter:pinged")
    );
}

#[renox::test]
async fn a_post_without_csrf_is_page_expired() {
    let (app, _dir) = actions_app().await;
    let s = snapshot_of(&app, "/counter").await;
    app.htmx()
        .without_csrf()
        .post("/_renox/live/counter/increment", &[("_snapshot", &s)])
        .await
        .assert_status(419);
}

#[renox::test]
async fn route_list_has_the_action_route() {
    let kernel = App::with_config(Config::default()).boot().await.unwrap();
    assert!(
        kernel
            .routes()
            .iter()
            .any(|r| r.method == "POST" && r.path == "/_renox/live/{component}/{action}")
    );
}

#[renox::test]
async fn a_page_with_two_components_loads_idiomorph_once() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::create_dir_all(dir.path().join("live")).unwrap();
    std::fs::write(
        dir.path().join("page.html"),
        r#"{% set component = list %}{% include "renox/live.html" %}{% set component = list %}{% include "renox/live.html" %}"#,
    )
    .unwrap();
    std::fs::write(
        dir.path().join("live/counter.html"),
        "<b>{{ state.count }}</b>",
    )
    .unwrap();
    let path = dir.path().to_path_buf();
    let app = TestApp::with_config(
        App::new()
            .module(Auth::new())
            .module(Pages)
            .live_component::<Counter>(),
        move |c| c.views_path = path,
    )
    .await;
    let body = app.get("/counter").await.text();
    assert_eq!(body.matches(r#"data-rx-live="counter""#).count(), 2);
    assert_eq!(body.matches("idiomorph-0.7.3.min.js").count(), 1);
    assert_eq!(body.matches("/_renox/live-").count(), 1);
}

#[renox::test]
async fn the_test_helper_keeps_state_between_calls() {
    let (app, _dir) = actions_app().await;
    let mut c = app.live(Counter::default());
    c.set("search", "tea");
    c.call("increment").await.assert_see("<b>1</b>");
    c.call("increment").await.assert_see("<b>2</b>tea");
    c.call_with("add", json!([5])).await.assert_see("<b>7</b>");
    let state = c.component();
    assert_eq!(state.count, 7);
    assert_eq!(state.search, "tea");
}

#[derive(serde::Serialize, serde::Deserialize, Default)]
struct Todo {
    done: Vec<i64>,
    title: String,
}

#[renox::live_component(view = "live/todo.html")]
impl Todo {
    async fn data(&self, _ctx: &LiveContext) -> Result<Vec<String>> {
        Ok(vec!["milk".to_string(), "tea".to_string()])
    }

    #[live(action)]
    async fn toggle(&mut self, _ctx: &mut LiveContext, id: i64) -> Result {
        self.done.push(id);
        Ok(())
    }

    #[live(action)]
    async fn rename(&mut self, _ctx: &mut LiveContext, id: i64, title: String) -> Result {
        self.title = format!("{id}:{title}");
        Ok(())
    }

    #[allow(dead_code)]
    async fn hidden(&mut self, _ctx: &mut LiveContext) -> Result {
        Ok(())
    }
}

async fn todo(ctx: LiveContext) -> Result<View> {
    let list = ctx.mount(Todo::default()).await?;
    Ok(view("page.html", context! { list }))
}

struct TodoPages;

impl Module for TodoPages {
    fn name(&self) -> &'static str {
        "todo-pages"
    }

    fn routes(&self) -> Routes {
        Routes::new().get("/todo", todo)
    }
}

#[renox::test]
async fn a_macro_made_component_runs_its_marked_actions() {
    assert_eq!(<Todo as LiveComponent>::NAME, "todo");
    let dir = tempfile::tempdir().unwrap();
    std::fs::create_dir_all(dir.path().join("live")).unwrap();
    std::fs::write(
        dir.path().join("page.html"),
        r#"{% set component = list %}{% include "renox/live.html" %}"#,
    )
    .unwrap();
    std::fs::write(
        dir.path().join("live/todo.html"),
        "<i>{{ state.done|join(',') }}</i><u>{{ state.title }}</u>{{ data|join('+') }}",
    )
    .unwrap();
    let path = dir.path().to_path_buf();
    let app = TestApp::with_config(
        App::new()
            .module(Auth::new())
            .module(TodoPages)
            .live_component::<Todo>(),
        move |c| c.views_path = path,
    )
    .await;
    let s = snapshot_of(&app, "/todo").await;
    let post = |action: &'static str, args: &'static str| {
        let s = s.clone();
        let app = &app;
        async move {
            app.htmx()
                .post(
                    &format!("/_renox/live/todo/{action}"),
                    &[("_snapshot", &s), ("_args", args)],
                )
                .await
        }
    };
    post("toggle", "[3]")
        .await
        .assert_ok()
        .assert_see("<i>3</i>")
        .assert_see("milk+tea");
    post("rename", r#"[4, "x"]"#)
        .await
        .assert_ok()
        .assert_see("<u>4:x</u>");
    post("toggle", "[]").await.assert_status(400);
    post("toggle", "[1, 2]").await.assert_status(400);
    post("toggle", r#"["a"]"#).await.assert_status(400);
    post("hidden", "[]").await.assert_status(404);
    post("data", "[]").await.assert_status(404);
}

/// The page with ids and snapshots blanked, which differ per mount.
fn blanked(html: &str) -> String {
    let mut out = String::new();
    let mut rest = html;
    while let Some(i) = rest
        .find(" id=\"")
        .or_else(|| rest.find("data-rx-snapshot=\""))
    {
        let quote = rest[i..].find('"').unwrap() + i + 1;
        let end = rest[quote..].find('"').unwrap() + quote;
        out.push_str(&rest[..quote]);
        out.push_str("...");
        rest = &rest[end..];
    }
    out.push_str(rest);
    out
}

#[renox::test]
async fn the_live_tag_renders_like_the_include() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::create_dir_all(dir.path().join("live")).unwrap();
    std::fs::write(
        dir.path().join("tag.html"),
        r#"<live-counter :component="list" @saved="open = false" class="box" data-x="1" />"#,
    )
    .unwrap();
    std::fs::write(
        dir.path().join("include.html"),
        r#"{% with component = list, live_attrs = {"x-on:rx:counter:saved.self": "open = false", "class": "box", "data-x": "1"} %}{% include "renox/live.html" %}{% endwith %}"#,
    )
    .unwrap();
    std::fs::write(
        dir.path().join("live/counter.html"),
        r#"<button rx-click="increment">{{ state.count }}</button>"#,
    )
    .unwrap();
    async fn page(ctx: LiveContext, name: &'static str) -> Result<View> {
        let list = ctx.mount(Counter::default()).await?;
        Ok(view(name, context! { list }))
    }
    struct Both;
    impl Module for Both {
        fn name(&self) -> &'static str {
            "both"
        }
        fn routes(&self) -> Routes {
            Routes::new()
                .get("/tag", |ctx: LiveContext| page(ctx, "tag.html"))
                .get("/include", |ctx: LiveContext| page(ctx, "include.html"))
        }
    }
    let path = dir.path().to_path_buf();
    let app = TestApp::with_config(
        App::new()
            .module(Auth::new())
            .module(Both)
            .live_component::<Counter>(),
        move |c| c.views_path = path,
    )
    .await;
    let tag = app.get("/tag").await;
    let include = app.get("/include").await;
    tag.assert_ok()
        .assert_see(r#"class="box""#)
        .assert_see(r#"x-on:rx:counter:saved.self="open = false""#)
        .assert_see(r#"data-x="1""#)
        .assert_see(r#"rx-click="increment""#);
    assert_eq!(blanked(&tag.text()), blanked(&include.text()));
}