zeyn 0.0.8

Rust web framework for data heavy applications, written by AI agents and reviewed by people
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
//! Card grids, rails and search boxes: free text search in `DataQuery`, server rendered cards,
//! paging and sorting through the grid endpoint, suggestions through the data endpoint, access,
//! masked fields and rate limits.

use axum::body::Body;
use axum::http::{HeaderMap, Request, StatusCode, header};
use http_body_util::BodyExt;
use tower::ServiceExt;
use zeyn::prelude::*;
use zeyn::{CenterOnPhone, I18n, Identity, MAX_SEARCH_QUERY, PageRequest};

#[entity(table = "goods")]
pub struct Good {
    #[key]
    pub id: GoodId,
    pub name: String,
    pub city: String,
    pub price: i64,
}

fn goods() -> Vec<Good> {
    let names = [
        "İSTANBUL tea",
        "Ispanak",
        "Izmir figs",
        "Black <tea>",
        "Coffee",
    ];
    (1..=30)
        .map(|i| Good {
            id: GoodId(i),
            name: format!("{} {i}", names[(i as usize - 1) % names.len()]),
            city: if i % 2 == 0 {
                "Ankara".into()
            } else {
                "Bursa".into()
            },
            price: i * 10,
        })
        .collect()
}

#[action(permission = "shop.read")]
pub async fn search_goods(q: DataQuery) -> Result<Page<Good>> {
    Ok(q.apply(goods()))
}

#[test]
fn free_text_matches_text_fields_ignoring_case_and_turkish_i() {
    let q = |text: &str| DataQuery {
        q: text.into(),
        page: PageRequest::Offset {
            offset: 0,
            limit: 500,
        },
        ..DataQuery::default()
    };
    let names = |text: &str| {
        q(text)
            .apply(goods())
            .items
            .into_iter()
            .map(|g| g.name)
            .collect::<Vec<_>>()
    };
    assert_eq!(names("istanbul").len(), 6, "İSTANBUL matches istanbul");
    assert_eq!(names("ıspanak"), names("ISPANAK"), "dotless ı and I match");
    assert_eq!(names("izmir").len(), 6, "Izmir matches izmir");
    assert_eq!(names("ankara").len(), 15, "every text field is searched");
    assert!(names("300").is_empty(), "numbers are not text fields");
    assert_eq!(names("  ").len(), 30, "blank text matches every row");
    assert_eq!(
        names(&"x".repeat(MAX_SEARCH_QUERY + 1)).len(),
        30,
        "text over the cap is ignored"
    );
}

#[screen(route = "/shop", public)]
pub fn shop() -> Screen {
    Screen::new("Shop")
        .body(
            CardGrid::of(search_goods)
                .title("All goods")
                .card(|g: &Good| {
                    Card::image(format!("/img/{}.png", g.id.0), g.name.clone())
                        .text(format!("{} TL", g.price))
                        .badge(Badge::new("New"))
                        .link(results, SearchParams { q: g.name.clone() })
                })
                .sort_option("Cheapest", Good::price, false)
                .sort_option("Dearest", Good::price, true)
                .paging(Paging::infinite(4)),
        )
        .body(
            Rail::of(search_goods)
                .title("Picks")
                .card(|g: &Good| Card::image("javascript:alert(1)", g.name.clone())),
        )
        .body(
            SearchBox::of(search_goods)
                .instant(10)
                .limit(3)
                .results(results),
        )
}

#[screen(route = "/results", public)]
pub fn results(p: SearchParams) -> Screen {
    Screen::new("Results")
        .body(SearchBox::of(search_goods).value(p.q.clone()))
        .body(
            CardGrid::of(search_goods)
                .card(|g: &Good| Card::image("/x.png", g.name.clone()))
                .search(p.q)
                .paging(Paging::pages(5)),
        )
}

#[params]
pub struct GoodParams {
    pub id: i64,
}

/// A screen with a path route, so card links to it are canonical paths.
#[screen(route = "/goods/{id}", public)]
pub fn good_page(p: GoodParams) -> Screen {
    Screen::new(format!("Good {}", p.id))
}

#[screen(route = "/picks", public)]
pub fn picks() -> Screen {
    Screen::new("Picks").body(
        Rail::of(search_goods)
            .card(|g: &Good| {
                Card::image("/x.png", g.name.clone())
                    .subtitle(g.city.clone())
                    .corner(Badge::new("Hot <1>"))
                    .badges([Badge::new("Tag a"), Badge::new("Tag b")])
                    .progress(g.price as f32 / 100.0, "Sold")
                    .horizontal()
                    .link(good_page, GoodParams { id: g.id.0 })
            })
            .scrollbar(true)
            .paging(Paging::infinite(2)),
    )
}

#[screen(route = "/rows", public)]
pub fn goods_list() -> Screen {
    Screen::new("Rows")
        .body(
            List::of(search_goods)
                .title("Goods")
                .item(|g: &Good| {
                    zeyn::ui::ListItem::new(g.name.clone())
                        .image(if g.id.0 == 1 {
                            "javascript:alert(1)".to_string()
                        } else {
                            format!("/img/{}.png", g.id.0)
                        })
                        .subtitle(g.city.clone())
                        .meta(format!("{} TL", g.price))
                        .link(results, SearchParams { q: g.name.clone() })
                })
                .sort_option("Cheapest", Good::price, false)
                .paging(Paging::infinite(3)),
        )
        .body(
            List::new()
                .item(zeyn::ui::ListItem::new("Static <one>").meta("now"))
                .item(zeyn::ui::ListItem::new("Static two")),
        )
}

/// Fourteen rows paged by the list without a total, as an action that does not count.
#[action(permission = "shop.read")]
pub async fn uncounted_goods(q: DataQuery) -> Result<Page<Good>> {
    let all: Vec<Good> = goods().into_iter().take(14).collect();
    let mut page = Page::from_slice(&all, &q);
    page.total = None;
    Ok(page)
}

#[screen(route = "/uncounted", public)]
pub fn uncounted_list() -> Screen {
    Screen::new("Uncounted").body(
        List::of(uncounted_goods)
            .item(|g: &Good| {
                zeyn::ui::ListItem::new(g.name.clone()).meta(zeyn::ui::RelativeTime::new(
                    DateTime::from_unix_ms(1_778_580_000_000),
                ))
            })
            .paging(Paging::pages(10)),
    )
}

#[screen(route = "/ankara", public)]
pub fn ankara_goods() -> Screen {
    Screen::new("Ankara")
        .body(
            Rail::of(search_goods)
                .card(|g: &Good| Card::image("/x.png", format!("{} in {}", g.name, g.city)))
                .scope(Good::city, "ankara".to_string())
                .paging(Paging::infinite(4)),
        )
        .body(
            List::of(search_goods)
                .item(|g: &Good| zeyn::ui::ListItem::new(format!("{} in {}", g.name, g.city)))
                .scope(Good::city, "Ankara".to_string())
                .paging(Paging::pages(4)),
        )
}

const TOKEN: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";

fn app(limit: Option<RateLimit>) -> axum::Router {
    let mut app = App::new("Test")
        .i18n(I18n::new("en"))
        .auth(|h: &HeaderMap| {
            let perms: Vec<String> = h
                .get("x-perms")?
                .to_str()
                .ok()?
                .split(',')
                .map(String::from)
                .collect();
            Some(Identity::user("u").with_permissions(perms))
        })
        .feature(
            Feature::new("shop")
                .screen(shop)
                .screen(results)
                .screen(picks)
                .screen(good_page)
                .screen(goods_list)
                .screen(uncounted_list)
                .screen(ankara_goods),
        );
    if let Some(l) = limit {
        app = app.write_limit(l);
    }
    app.router().unwrap()
}

async fn send(router: &axum::Router, req: Request<Body>) -> (StatusCode, String) {
    let res = router
        .clone()
        .oneshot(req)
        .await
        .unwrap();
    let status = res.status();
    (
        status,
        String::from_utf8(
            res.into_body()
                .collect()
                .await
                .unwrap()
                .to_bytes()
                .to_vec(),
        )
        .unwrap(),
    )
}

fn get(path: &str) -> Request<Body> {
    Request::get(path)
        .header("x-perms", "shop.read")
        .body(Body::empty())
        .unwrap()
}

fn post(path: &str, perms: &str, body: serde_json::Value) -> Request<Body> {
    Request::post(path)
        .header("x-perms", perms)
        .header(header::CONTENT_TYPE, "application/json")
        .header(header::COOKIE, format!("zeyn_csrf={TOKEN}"))
        .header("x-zeyn-csrf", TOKEN)
        .body(Body::from(body.to_string()))
        .unwrap()
}

#[tokio::test]
async fn card_grids_render_their_first_page_as_linked_lazy_images() {
    let (status, page) = send(&app(None), get("/shop")).await;
    assert_eq!(status, StatusCode::OK);
    assert!(
        page.contains(r#"<section class="z-cards" data-zeyn-cards data-zeyn-paging="infinite""#),
        "{page}"
    );
    assert!(page.contains(r#"data-zeyn-size="4""#) && page.contains(r#"data-zeyn-next="4""#));
    assert!(page.contains(r#"<h2 class="z-cards-title" id="shop-cards-0-title">All goods</h2>"#));
    assert!(page.contains(
        r#"<img class="z-poster-img" src="/img/1.png" alt="" width="300" height="400" loading="lazy" decoding="async" draggable="false">"#
    ));
    assert!(page.contains(r#"<a class="z-poster-link" href="/results?params="#));
    assert!(page.contains(r#"<span class="z-badge z-tone-neutral">New</span>"#));
    assert!(page.contains("Black &lt;tea&gt;"), "row text is escaped");
    assert_eq!(
        page.matches(r#"<li class="z-poster">"#)
            .count(),
        4 + 12,
        "four cards and the rail's twelve"
    );
    assert!(page.contains(r#"data-zeyn-cards-sort><option value="0" data-sort=""#));
    assert!(page.contains(r#"href="/_zeyn/catalog.css?v="#));
    assert!(
        !page.contains("javascript:"),
        "unsafe image addresses are left out"
    );
    assert!(page.contains(r#"<section class="z-rail""#) && page.contains("data-zeyn-rail-next"));
    assert!(page.contains(r#"role="combobox" aria-autocomplete="list" aria-expanded="false""#));
    assert!(page.contains(r#"data-zeyn-results="results""#) && page.contains(r#"maxlength="100""#));
}

#[tokio::test]
async fn more_cards_load_through_the_grid_endpoint_with_the_servers_page_size_and_sorts() {
    let router = app(None);
    let ask = |query: serde_json::Value| {
        post(
            "/_zeyn/grid",
            "shop.read",
            serde_json::json!({ "screen": "shop", "node": "0", "query": query }),
        )
    };
    let (status, body) = send(
        &router,
        ask(serde_json::json!({ "page": { "type": "after", "cursor": "4", "limit": 500 }, "sort": [{ "field": "price", "desc": true }] })),
    )
    .await;
    assert_eq!(status, StatusCode::OK, "{body}");
    let json: serde_json::Value = serde_json::from_str(&body).unwrap();
    let rows = json["rows"].as_str().unwrap();
    assert_eq!(
        rows.matches(r#"<li class="z-poster">"#)
            .count(),
        4,
        "the client's limit is ignored"
    );
    assert!(
        rows.contains(">260 TL<") && !rows.contains(">300 TL<"),
        "the offered sort order applies after the cursor: {rows}"
    );
    let (_, body) = send(
        &router,
        ask(serde_json::json!({ "page": { "type": "after", "limit": 4 }, "sort": [{ "field": "city", "desc": true }], "filters": [{ "field": "city", "contains": "x" }], "q": "nothing" })),
    )
    .await;
    let json: serde_json::Value = serde_json::from_str(&body).unwrap();
    let rows = json["rows"].as_str().unwrap();
    assert!(
        rows.contains(">10 TL<") && rows.matches("<li").count() == 4,
        "sorts not offered, filters and text from the client are dropped: {rows}"
    );
    let (status, _) = send(
        &router,
        post(
            "/_zeyn/grid",
            "",
            serde_json::json!({ "screen": "shop", "node": "0" }),
        ),
    )
    .await;
    assert_eq!(
        status,
        StatusCode::FORBIDDEN,
        "reading needs the action's permission"
    );
}

#[tokio::test]
async fn results_screens_search_on_the_server_and_page_by_number() {
    let params = serde_json::json!({ "q": "istanbul" }).to_string();
    let path = format!(
        "/results?params={}",
        params
            .replace('{', "%7B")
            .replace('}', "%7D")
            .replace('"', "%22")
            .replace(':', "%3A")
    );
    let (status, page) = send(&app(None), get(&path)).await;
    assert_eq!(status, StatusCode::OK, "{page}");
    assert!(
        page.contains(r#"data-zeyn-paging="pages""#) && page.contains(r#"data-zeyn-total="6""#),
        "{page}"
    );
    assert_eq!(
        page.matches(r#"<li class="z-poster">"#)
            .count(),
        5
    );
    assert!(page.contains(r#"value="istanbul""#));
    assert!(page.contains("data-zeyn-cards-next"));
}

#[tokio::test]
async fn suggestions_come_from_the_action_with_a_capped_text() {
    let router = app(None);
    let ask = |input: serde_json::Value, perms: &str| {
        post(
            "/_zeyn/data",
            perms,
            serde_json::json!({ "screen": "shop", "node": "2", "key": "suggest", "input": input }),
        )
    };
    let (status, body) = send(&router, ask(serde_json::json!("ıstanbul"), "shop.read")).await;
    assert_eq!(status, StatusCode::OK, "{body}");
    let hits: Vec<String> = serde_json::from_str(&body).unwrap();
    assert_eq!(
        hits,
        ["İSTANBUL tea 1", "İSTANBUL tea 6", "İSTANBUL tea 11"],
        "three, as the box asks"
    );
    let (_, body) = send(
        &router,
        ask(
            serde_json::json!(format!("{}tea", " ".repeat(5))),
            "shop.read",
        ),
    )
    .await;
    assert_eq!(
        serde_json::from_str::<Vec<String>>(&body)
            .unwrap()
            .len(),
        3
    );
    let (_, body) = send(
        &router,
        ask(serde_json::json!("z".repeat(10_000)), "shop.read"),
    )
    .await;
    assert_eq!(body, "[]", "long text is cut to the cap and finds nothing");
    let (status, _) = send(&router, ask(serde_json::json!("tea"), "")).await;
    assert_eq!(status, StatusCode::FORBIDDEN);
}

#[tokio::test]
async fn suggestions_count_against_the_write_limit() {
    let router = app(Some(RateLimit::per_user(
        2,
        std::time::Duration::from_secs(60),
    )));
    let ask = || {
        post(
            "/_zeyn/data",
            "shop.read",
            serde_json::json!({ "screen": "shop", "node": "2", "key": "suggest", "input": "tea" }),
        )
    };
    assert_eq!(send(&router, ask()).await.0, StatusCode::OK);
    assert_eq!(send(&router, ask()).await.0, StatusCode::OK);
    assert_eq!(send(&router, ask()).await.0, StatusCode::TOO_MANY_REQUESTS);
}

#[entity(table = "members")]
pub struct Member {
    #[key]
    pub id: MemberId,
    pub name: String,
    #[mask(last4)]
    pub phone: String,
}

#[action(public)]
pub async fn members(q: DataQuery) -> Result<Page<Member>> {
    Ok(q.apply(vec![Member {
        id: MemberId(1),
        name: "Ada".into(),
        phone: "5551234567".into(),
    }]))
}

#[screen(route = "/members", public)]
pub fn member_cards() -> Screen {
    Screen::new("Members")
        .body(CardGrid::of(members).card(|m: &Member| Card::image("/m.png", m.phone.clone())))
}

#[tokio::test]
async fn cards_of_models_with_masked_fields_are_refused_to_users_who_may_not_reveal_them() {
    let router = App::new("Test")
        .auth(|_: &HeaderMap| Some(Identity::user("u")))
        .feature(Feature::new("m").screen(member_cards))
        .router()
        .unwrap();
    let (status, page) = send(
        &router,
        Request::get("/members")
            .body(Body::empty())
            .unwrap(),
    )
    .await;
    assert_eq!(status, StatusCode::OK);
    assert!(
        !page.contains("5551234567"),
        "the masked value never reaches the page"
    );
    let (status, body) = send(
        &router,
        post(
            "/_zeyn/grid",
            "",
            serde_json::json!({ "screen": "member_cards", "node": "0" }),
        ),
    )
    .await;
    assert_eq!(status, StatusCode::FORBIDDEN, "{body}");
    assert!(!body.contains("5551234567"));
}

#[tokio::test]
async fn the_catalog_script_is_served_without_sign_in() {
    let (status, js) = send(
        &app(None),
        Request::get("/_zeyn/catalog.js")
            .body(Body::empty())
            .unwrap(),
    )
    .await;
    assert_eq!(status, StatusCode::OK);
    assert!(js.contains("aria-activedescendant"));
    assert!(
        js.contains("window.zeynCsrf()"),
        "public pages come without a CSRF token, so paging and suggestions must ask the runtime for one"
    );
}

#[tokio::test]
async fn paged_lists_without_a_total_offer_the_next_page_while_there_is_a_cursor() {
    let router = app(None);
    let (status, page) = send(&router, get("/uncounted")).await;
    assert_eq!(status, StatusCode::OK, "{page}");
    assert!(page.contains(r#"data-zeyn-paging="pages""#), "{page}");
    assert!(page.contains(r#" data-zeyn-next="10""#), "{page}");
    assert!(
        !page.contains("data-zeyn-total"),
        "the action did not count"
    );
    assert!(
        page.contains(r#"<span class="z-cards-page" data-zeyn-cards-page>Page 1</span>"#),
        "without a total the label leaves out the page count: {page}"
    );
    assert!(
        page.contains(r#"data-zeyn-cards-next aria-disabled="false""#),
        "Next works while there is a cursor: {page}"
    );
    assert!(page.contains(r#"data-text-page-only="Page {page}""#));
    assert!(
        page.contains(r#"<span class="z-list-meta"><time"#),
        "the meta of a row takes an inline component: {page}"
    );
    let (status, body) = send(
        &router,
        post(
            "/_zeyn/grid",
            "shop.read",
            serde_json::json!({ "screen": "uncounted_list", "node": "0", "query": { "page": { "type": "offset", "offset": 10, "limit": 10 }, "with_total": true } }),
        ),
    )
    .await;
    assert_eq!(status, StatusCode::OK, "{body}");
    let json: serde_json::Value = serde_json::from_str(&body).unwrap();
    assert_eq!(
        json["rows"]
            .as_str()
            .unwrap()
            .matches(r#"<li class="z-list-item">"#)
            .count(),
        4,
        "page 2 holds the last four rows: {body}"
    );
    assert!(json["next"].is_null(), "{body}");
    let (_, js) = send(
        &router,
        Request::get("/_zeyn/catalog.js")
            .body(Body::empty())
            .unwrap(),
    )
    .await;
    assert!(
        js.contains("known ? offset + size >= total : !d.zeynNext"),
        "the browser enables Next from the cursor when there is no total"
    );
}

#[tokio::test]
async fn scoped_rails_and_lists_keep_their_filter_whatever_the_client_sends() {
    let router = app(None);
    let (status, page) = send(&router, get("/ankara")).await;
    assert_eq!(status, StatusCode::OK, "{page}");
    assert_eq!(
        page.matches(" in Ankara").count(),
        8,
        "four cards and four rows of the scope: {page}"
    );
    assert!(!page.contains(" in Bursa"), "{page}");
    for node in ["0", "1"] {
        for filters in [
            serde_json::json!([]),
            serde_json::json!([{ "field": "city", "equals": "Bursa" }]),
            serde_json::json!([{ "field": "id", "range": { "from": "1", "to": "30" } }]),
        ] {
            let (status, body) = send(
                &router,
                post(
                    "/_zeyn/grid",
                    "shop.read",
                    serde_json::json!({ "screen": "ankara_goods", "node": node, "query": { "page": { "type": "offset", "offset": 4, "limit": 99 }, "filters": filters } }),
                ),
            )
            .await;
            assert_eq!(status, StatusCode::OK, "{body}");
            let json: serde_json::Value = serde_json::from_str(&body).unwrap();
            let rows = json["rows"].as_str().unwrap();
            assert!(
                rows.contains(" in Ankara") && !rows.contains(" in Bursa"),
                "node {node} with {filters}: {rows}"
            );
        }
    }
}

#[tokio::test]
async fn rails_hide_their_scrollbar_unless_kept_and_drag_only_with_a_mouse() {
    let router = app(None);
    let (_, shop_page) = send(&router, get("/shop")).await;
    assert!(
        shop_page.contains(r#"<section class="z-rail" "#),
        "{shop_page}"
    );
    let (_, picks_page) = send(&router, get("/picks")).await;
    assert!(
        picks_page.contains(r#"<section class="z-rail z-rail-bar" "#),
        "{picks_page}"
    );
    assert!(
        picks_page.contains("data-zeyn-rail-prev") && picks_page.contains("data-zeyn-rail-next"),
        "the buttons cannot be turned off"
    );
    let asset = |path: &'static str| {
        let router = router.clone();
        async move {
            send(
                &router,
                Request::get(path)
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .1
        }
    };
    let css = asset("/_zeyn/catalog.css").await;
    assert!(
        css.contains("scrollbar-width: none")
            && css.contains("::-webkit-scrollbar { display: none; }")
    );
    assert!(css.contains("scroll-snap-type: x proximity"));
    assert!(
        css.contains(".z-rail:not([data-zeyn-ready]) .z-rail-nav { visibility: hidden; }"),
        "without the script the buttons are hidden but keep their space"
    );
    let js = asset("/_zeyn/catalog.js").await;
    assert!(
        js.contains("e.pointerType !== 'mouse'"),
        "touch and pen keep native scrolling"
    );
    assert!(js.contains("const DRAG_PX = 5;"));
    assert!(
        js.contains("if (scrolled && entry.isIntersecting)"),
        "more cards load by themselves only after the user scrolled, so the first page does not shift"
    );
    let shell = asset("/_zeyn/zeyn.css").await;
    assert!(
        shell.contains("@media (max-width: 800px) and (scripting: enabled)"),
        "on phones the app menu starts folded, so the page does not jump when the script folds it"
    );
}

#[tokio::test]
async fn results_screens_open_without_params_with_empty_text() {
    let (status, page) = send(&app(None), get("/results")).await;
    assert_eq!(status, StatusCode::OK, "{page}");
    assert!(page.contains(r#"data-zeyn-total="30""#));
}

#[tokio::test]
async fn image_cards_lay_out_in_a_row_with_subtitle_corner_badges_and_progress() {
    let (status, page) = send(&app(None), get("/picks")).await;
    assert_eq!(status, StatusCode::OK, "{page}");
    assert_eq!(
        page.matches(r#"<li class="z-poster z-poster-h">"#)
            .count(),
        2
    );
    assert!(
        page.contains(r#"<span class="z-poster-subtitle">Bursa</span>"#),
        "{page}"
    );
    assert!(page.contains(r#"<span class="z-poster-corner"><span class="z-badge z-tone-neutral">Hot &lt;1&gt;</span></span>"#));
    assert!(page.contains(
        r#"<span class="z-poster-badges"><span class="z-badge z-tone-neutral">Tag a</span>"#
    ));
    assert!(page.contains(r#"role="progressbar" aria-label="Sold" aria-valuemin="0" aria-valuemax="100" aria-valuenow="10" aria-valuetext="10%""#));
    assert!(
        page.contains(r#"aria-valuenow="20""#),
        "each row has its own fraction"
    );
    assert!(page.contains("--z-progress:10%"));
}

#[tokio::test]
async fn rails_render_their_first_page_on_the_server_with_canonical_links() {
    let router = app(None);
    let (status, page) = send(&router, get("/picks")).await;
    assert_eq!(status, StatusCode::OK);
    assert!(
        page.contains(r#"<a class="z-poster-link" href="/goods/1" data-zeyn-base-href>"#),
        "a path route is linked directly, not through /_zeyn/route: {page}"
    );
    assert!(!page.contains("/_zeyn/route/good_page"));
    assert!(page.contains(r#"data-zeyn-count="2" data-zeyn-size="2""#));
    let again = send(&router, get("/picks")).await.1;
    let main =
        |html: &str| html[html.find("<main").unwrap()..html.find("</main>").unwrap()].to_string();
    assert!(
        main(&page) == main(&again),
        "the cards are the same HTML for every visitor, so a public page can be cached"
    );
    let (status, more) = send(
        &router,
        post(
            "/_zeyn/grid",
            "shop.read",
            serde_json::json!({ "screen": "picks", "params": null, "node": "0", "query": { "page": { "type": "after", "cursor": "2", "limit": 99 } } }),
        ),
    )
    .await;
    assert_eq!(status, StatusCode::OK, "{more}");
    assert!(
        more.contains(r#"href=\"/goods/3\""#),
        "later pages from the client use the same links: {more}"
    );
    let (status, opened) = send(&router, get("/goods/3")).await;
    assert_eq!(status, StatusCode::OK);
    assert!(opened.contains("Good 3"));
}

#[tokio::test]
async fn lists_render_rows_as_single_links_and_page_through_the_grid_endpoint() {
    let router = app(None);
    let (status, page) = send(&router, get("/rows")).await;
    assert_eq!(status, StatusCode::OK, "{page}");
    assert!(
        page.contains(
            r#"<section class="z-cards z-cards-rows" data-zeyn-cards data-zeyn-paging="infinite""#
        ),
        "{page}"
    );
    assert!(page.contains(r#"<ul class="z-list" id="goods_list-cards-0-list" data-zeyn-rows>"#));
    assert_eq!(
        page.matches(r#"<li class="z-list-item"><a class="z-list-row" href="#)
            .count(),
        3
    );
    assert!(
        page.contains(r#"<span class="z-list-img"></span>"#),
        "unsafe image addresses are left out"
    );
    assert!(!page.contains("javascript:"));
    assert!(
        page.contains(r#"<img class="z-list-img" src="/img/2.png" alt="" width="48" height="48""#)
    );
    assert!(page.contains(r#"<span class="z-list-meta">10 TL</span>"#));
    assert!(page.contains(r#"<li class="z-list-item"><div class="z-list-row"><span class="z-list-body"><span class="z-list-title">Static &lt;one&gt;</span></span><span class="z-list-meta">now</span></div></li>"#));
    let (status, body) = send(
        &router,
        post(
            "/_zeyn/grid",
            "shop.read",
            serde_json::json!({ "screen": "goods_list", "node": "0", "query": { "page": { "type": "after", "cursor": "3", "limit": 99 } } }),
        ),
    )
    .await;
    assert_eq!(status, StatusCode::OK, "{body}");
    let json: serde_json::Value = serde_json::from_str(&body).unwrap();
    let rows = json["rows"].as_str().unwrap();
    assert_eq!(
        rows.matches(r#"<li class="z-list-item">"#)
            .count(),
        3,
        "the server keeps the page size: {rows}"
    );
    let (status, _) = send(
        &router,
        post(
            "/_zeyn/grid",
            "",
            serde_json::json!({ "screen": "goods_list", "node": "0" }),
        ),
    )
    .await;
    assert_eq!(
        status,
        StatusCode::FORBIDDEN,
        "reading needs the action's permission"
    );
}

#[screen(route = "/store", public)]
pub fn store() -> Screen {
    Screen::new("Store")
}

fn header_app(icon: bool) -> axum::Router {
    let place = if icon {
        CenterOnPhone::Icon
    } else {
        CenterOnPhone::Row
    };
    App::new("Test")
        .auth(|h: &HeaderMap| {
            let perms = h.get("x-perms")?.to_str().ok()?;
            Some(Identity::user("u").with_permissions(perms.split(',').map(String::from)))
        })
        .feature(
            Feature::new("store")
                .screen(store)
                .screen(results)
                .screen(good_page)
                .shell(Shell::site().header(|h| {
                    h.start(Paragraph::new("Hi <there>"))
                        .center(SearchBox::of(search_goods).results(results))
                        .center(Paragraph::new("Second"))
                        .center_on_phone(place)
                        .end(LinkButton::new("Good", good_page, GoodParams { id: 3 }))
                        .bottom(Chips::links("Topics").link(
                            "Results",
                            results,
                            SearchParams::default(),
                        ))
                        .theme()
                })),
        )
        .router()
        .unwrap()
}

#[tokio::test]
async fn header_slots_take_any_component_and_a_search_box_suggests_from_there() {
    let router = header_app(false);
    let (status, page) = send(&router, get("/store")).await;
    assert_eq!(status, StatusCode::OK, "{page}");
    assert!(
        page.contains(
            r#"data-zeyn-screen="_site.store" data-zeyn-params="null" data-center-phone="row""#
        ),
        "{page}"
    );
    let at = |s: &str| {
        page.find(s)
            .unwrap_or_else(|| panic!("{s} missing: {page}"))
    };
    assert!(at(r#"class="z-site-slot z-site-slot-start""#) < at(r#"class="z-site-brand""#));
    assert!(at(r#"class="z-site-brand""#) < at(r#"<nav class="z-site-nav""#));
    assert!(at(r#"<nav class="z-site-nav""#) < at(r#"id="z-site-slot-center""#));
    assert!(at(r#"id="z-site-slot-center""#) < at(r#"id="z-site-slot-end""#));
    assert!(at(r#"id="z-site-slot-end""#) < at(r#"id="z-site-actions""#));
    assert!(at(r#"id="z-site-actions""#) < at(r#"id="z-site-slot-bottom""#));
    assert!(page.contains("Hi &lt;there&gt;"));
    assert!(page.contains(r#"data-zeyn-node="1.0"><form class="z-search""#));
    assert!(page.contains(r#"data-zeyn-node="1.1""#));
    assert!(
        page.contains(r#"href="/goods/3""#),
        "links in slots use path routes"
    );
    assert!(page.contains(r#"aria-controls="z-site-nav z-site-actions""#));
    assert!(
        !page.contains("data-zeyn-site-center-toggle"),
        "a row needs no button"
    );

    let ask = |screen: &str, node: &str, perms: &str| {
        post(
            "/_zeyn/data",
            perms,
            serde_json::json!({ "screen": screen, "node": node, "key": "suggest", "input": "coffee" }),
        )
    };
    let (status, body) = send(&router, ask("_site.store", "1.0", "shop.read")).await;
    assert_eq!(status, StatusCode::OK, "{body}");
    assert_eq!(
        serde_json::from_str::<Vec<String>>(&body)
            .unwrap()
            .len(),
        6
    );
    let (status, _) = send(&router, ask("_site.store", "1.0", "")).await;
    assert_eq!(
        status,
        StatusCode::FORBIDDEN,
        "the action's permission counts"
    );
    for (screen, node) in [
        ("_site.store", "1.1"),
        ("_site.store", "4.0"),
        ("_site.other", "1.0"),
    ] {
        let (status, _) = send(&router, ask(screen, node, "shop.read")).await;
        assert_eq!(status, StatusCode::NOT_FOUND, "{screen} {node}");
    }

    let (_, page) = send(&header_app(true), get("/store")).await;
    assert!(page.contains(r#"data-center-phone="icon""#));
    assert!(page.contains(r#"<button type="button" class="z-site-toggle z-site-center-toggle" aria-expanded="false" aria-controls="z-site-slot-center" data-zeyn-site-center-toggle hidden>"#), "{page}");
    assert!(page.contains(r#"<span class="z-sr-only">Search</span>"#));
}

#[screen(route = "/finder", public)]
pub fn finder() -> Screen {
    Screen::new("Finder").body(
        SearchBox::of(search_goods)
            .results(results)
            .item(|g: &Good| {
                zeyn::ui::ListItem::new(g.name.clone())
                    .subtitle(g.city.clone())
                    .image(if g.id.0 == 5 {
                        "javascript:alert(1)".to_string()
                    } else if g.id.0 == 10 {
                        "https://elsewhere.example/x.png".to_string()
                    } else {
                        format!("/img/{}.png", g.id.0)
                    })
                    .link(good_page, GoodParams { id: g.id.0 })
            }),
    )
}

#[tokio::test]
async fn rich_suggestions_carry_text_subtitle_an_allowed_image_and_the_canonical_link() {
    let router = App::new("Test")
        .auth(|h: &HeaderMap| {
            let perms = h.get("x-perms")?.to_str().ok()?;
            Some(Identity::user("u").with_permissions(perms.split(',').map(String::from)))
        })
        .feature(
            Feature::new("shop")
                .screen(finder)
                .screen(results)
                .screen(good_page),
        )
        .router()
        .unwrap();
    let (status, body) = send(
        &router,
        post(
            "/_zeyn/data",
            "shop.read",
            serde_json::json!({ "screen": "finder", "node": "0", "key": "suggest", "input": "coffee" }),
        ),
    )
    .await;
    assert_eq!(status, StatusCode::OK, "{body}");
    let hits: Vec<serde_json::Value> = serde_json::from_str(&body).unwrap();
    assert_eq!(
        hits[0],
        serde_json::json!({ "text": "Coffee 5", "subtitle": "Bursa", "image": null, "href": "/goods/5" }),
        "an unsafe image is left out"
    );
    assert_eq!(
        hits[1]["image"],
        serde_json::Value::Null,
        "a host outside img-src is left out"
    );
    assert_eq!(hits[2]["image"], "/img/15.png");
    assert_eq!(hits[2]["href"], "/goods/15");
}