guinea 0.25.0

The application layer for Rust GUI
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
#![cfg(all(windows, feature = "winui"))]

//! The router driven through the facade, the way an application sees it.
//!
//! Integration rather than unit tests on purpose: `routes!` resolves paths
//! through `proc_macro_crate`, which answers `Itself` inside the crate and
//! `Name` outside it - only the second case is what an application gets.

mod routing {
    use guinea::winui::*;
    use guinea::feature::{FeatureHost, FeatureInitContext};
    use std::rc::Rc;
    use guinea::router::*;
    use guinea_core::actor::UiThreadToken;
    use guinea_core::feature::Bound;
    use guinea_core::scope::Reducer;
    use guinea_macros::routes;

    /// The reducer is the state; `Processes` below is the page that shows it.
    #[derive(Default, Clone, PartialEq, Debug)]
    struct Listing {
        seeded_from: String,
    }

    impl Reducer for Listing {
        type Update = String;

        fn reduce(&mut self, from: String) {
            self.seeded_from = from;
        }
    }

    fn install_processes(
        ctx: &FeatureInitContext,
        params: &ProcessesParams,
    ) -> anyhow::Result<Bound<Listing>> {
        Ok(ctx
            .state::<Listing>()
            .seed(Listing {
                seeded_from: params.context.clone(),
            })
            .plain())
    }

    #[derive(Default)]
    struct Processes;

    routes! {
        AppRoute {
            page(Processes) link("/:context/processes") { context: String }
        }
    }

    #[page]
    impl Page for Processes {
        type Params = ProcessesParams;
        /// The claim itself, which is what makes `Listing` readable here.
        type Installs = Bound<Listing>;

        fn install(
            ctx: &FeatureInitContext,
            params: &ProcessesParams,
        ) -> anyhow::Result<Bound<Listing>> {
            install_processes(ctx, params)
        }

        fn view(&self, cx: &mut PageCx<'_, '_, Self>) -> windows_reactor::View {
            let (state, _dispatch) = cx.read::<Listing>();
            assert_eq!(state.seeded_from, "ubuntu");
            windows_reactor::Grid::new().into()
        }
    }

    struct Greeting(&'static str);

    struct GreetingPlugin;

    impl guinea_app::app::Plugin for GreetingPlugin {
        const ID: &'static str = "test.greeting";
        type Exports = ();

        fn build(self, app: &mut guinea_app::app::PluginBuilder) -> anyhow::Result<()> {
            app.provide(Greeting("hello"));
            Ok(())
        }
    }

    #[derive(Default)]
    struct NeedsAService;

    #[page]
    impl Page for NeedsAService {
        type Params = ();

        fn install(ctx: &FeatureInitContext, _params: &()) -> anyhow::Result<()> {
            let greeting = ctx.require::<Greeting>()?;
            assert_eq!(greeting.0, "hello");
            Ok(())
        }

        fn view(&self, _cx: &mut PageCx<'_, '_, Self>) -> windows_reactor::View {
            windows_reactor::Grid::new().into()
        }
    }

    #[test]
    fn a_page_can_read_what_an_application_plugin_provided() {
        let token = UiThreadToken::dangerously_create_token_unchecked();

        let runtime = guinea::app::GuineaApp::new()
            .plugin(GreetingPlugin)
            .install(token)
            .expect("install");
        let context = runtime.context();
        guinea_app::app::install_runtime(runtime);

        let router = Rc::new(Router::<WinUi>::new(FeatureHost::under(&context)));

        router
            .activate(page_chain::<NeedsAService>(), vec![Box::new(())])
            .expect("the page's install must find the service the plugin provided");
    }

    #[test]
    fn a_page_without_an_application_gets_a_plain_error_not_a_panic() {
        let token = UiThreadToken::dangerously_create_token_unchecked();
        let router = Rc::new(Router::<WinUi>::new(FeatureHost::detached(token)));

        let err = router
            .activate(page_chain::<NeedsAService>(), vec![Box::new(())])
            .map(|_| ())
            .expect_err("nothing provided the service");

        assert!(
            format!("{err:#}").contains("Greeting"),
            "the error should name the missing service, got: {err:#}"
        );
    }

    // Gone with the render-and-hook API, and gone as a question with it.
    //
    // `scoped_router` existed because a router had to be conjured from inside
    // a render, out of a hook slot, and the test asked whether that slot was
    // per-window or process-wide. A window is a component now and `RouterRoot`
    // owns its router as a field, so "one per window" is what the type says
    // rather than what a hook happened to do.

    #[test]
    fn route_roundtrips_through_path() {
        let route = AppRoute::Processes {
            context: "ubuntu".to_string(),
        };
        assert_eq!(route.link().as_deref(), Some("/ubuntu/processes"));
        assert_eq!(
            AppRoute::parse("/ubuntu/processes"),
            Some(AppRoute::Processes {
                context: "ubuntu".to_string()
            })
        );
    }

    #[test]
    fn a_capture_that_looks_like_a_path_stays_one_segment() {
        // `Display` used to write this straight into the link, so the address
        // read as three segments and parsed back as a different route - or as
        // nothing. The escaping lives in `LinkValue`; this checks it reaches
        // the generated `link`/`parse` rather than only the trait's own tests.
        let route = AppRoute::Processes {
            context: "a/b c".to_string(),
        };

        let link = route.link().expect("an addressable route");
        assert_eq!(link, "/a%2Fb%20c/processes");
        assert_eq!(link.matches('/').count(), 2, "still two separators");
        assert_eq!(AppRoute::parse(&link), Some(route));
    }

    #[test]
    fn activating_a_page_runs_install_and_view_can_use_it() {
        let token = UiThreadToken::dangerously_create_token_unchecked();
        let router = Rc::new(Router::<WinUi>::new(FeatureHost::detached(token)));

        let scope = router
            .activate(
                page_chain::<Processes>(),
                vec![Box::new(ProcessesParams {
                    context: "ubuntu".to_string(),
                })],
            )
            .expect("activate");
        assert!(router.active_scope().is_some());

        // install already ran (synchronously, inside activate) and seeded state.
        let state = scope.state::<Listing>();
        assert_eq!(state.borrow().seeded_from, "ubuntu");

        router.deactivate();
        assert!(router.active_scope().is_none());
    }

    // `push_after_first_render_requests_a_rerender` stood here, and there is
    // nowhere to put it.
    //
    // It drove a page's view with `RenderCx::new(..)` - a render context with
    // no window behind it - pushed to a reducer afterwards, and asserted that
    // the component had asked to be drawn again. The component model has no
    // public way to build a `ViewContext`, and the reconciler harness that
    // replaces it is documented as unstable and not part of the application
    // API, so a downstream test suite has nothing to stand on.
    //
    // What it guarded is now split in two, and both halves are still covered:
    // that a subscription is taken and dropped with the component is the
    // adapter's `read`, and that a push reaches subscribers at all is
    // `guinea-core`'s own tests.

    #[test]
    fn navigating_to_the_same_route_twice_in_a_row_keeps_the_same_scope_identity() {
        // Mirrors what `RouterRx::render` actually does: it calls
        // `Router::navigate` on *every* render, not just when the route
        // value changes (the caller doesn't know whether it changed).
        let token = UiThreadToken::dangerously_create_token_unchecked();
        let router = Rc::new(Router::<WinUi>::new(FeatureHost::detached(token)));
        let route = AppRoute::Processes { context: "ubuntu".to_string() };

        let scope_a = router
            .navigate(route.clone())
            .expect("first navigate")
            .scope()
            .expect("nothing guards this route");
        let scope_b = router
            .navigate(route)
            .expect("second navigate, same route value")
            .scope()
            .expect("nothing guards this route");

        assert_eq!(
            scope_a, scope_b,
            "re-navigating to an unchanged route must reuse the exact same Scope, \
             not silently reinstall it - anything accumulating state in that Scope \
             (e.g. a live-updating chart) would otherwise reset on every single render"
        );
    }

    // --- nested layout persistence (RouteChain / navigate) ---

    #[derive(Default, Clone, PartialEq, Debug)]
    struct Tabs {
        install_count: i32,
    }

    impl Reducer for Tabs {
        type Update = i32;

        fn reduce(&mut self, count: i32) {
            self.install_count = count;
        }
    }

    #[derive(Default)]
    struct ProcsTab;
    #[layout]
    impl Layout for ProcsTab {
        // `TabRoute` below is written by hand and declares that this layout
        // derives nothing from its pages - so a leaf's own parameter changing
        // is none of its business, which is what the tests here check.
        type Params = ();

        fn install(ctx: &FeatureInitContext, _params: &Self::Params) -> anyhow::Result<()> {
            let count = ctx.scope.peek::<Tabs>().map_or(0, |s| s.borrow().install_count);
            ctx.scope.push::<Tabs>(count + 1);
            Ok(())
        }
        fn view(&self, cx: &mut LayoutCx<'_, '_, Self>) -> windows_reactor::View {
            cx.outlet()
        }
    }

    #[derive(Default)]
    struct ServicesLeaf;
    #[page]
    impl Page for ServicesLeaf {
        type Params = ();

        fn view(&self, cx: &mut PageCx<'_, '_, Self>) -> windows_reactor::View {
            let _ = cx;
            windows_reactor::Grid::new().into()
        }
    }

    #[derive(Clone, PartialEq)]
    enum TabRoute {
        Processes { context: String },
        Services { context: String },
    }

    const PROCESSES_CHAIN: [SegmentEntry<WinUi>; 2] = [
        layout_entry::<ProcsTab>(),
        segment_entry::<Processes>(),
    ];
    const SERVICES_CHAIN: [SegmentEntry<WinUi>; 2] = [
        layout_entry::<ProcsTab>(),
        segment_entry::<ServicesLeaf>(),
    ];

    impl RouteChain<WinUi> for TabRoute {
        fn chain(&self) -> &'static [SegmentEntry<WinUi>] {
            match self {
                TabRoute::Processes { .. } => &PROCESSES_CHAIN,
                TabRoute::Services { .. } => &SERVICES_CHAIN,
            }
        }

        fn params(&self) -> Vec<Box<dyn std::any::Any>> {
            match self {
                TabRoute::Processes { context } => vec![
                    Box::new(()),
                    Box::new(ProcessesParams {
                        context: context.clone(),
                    }),
                ],
                TabRoute::Services { .. } => vec![Box::new(()), Box::new(())],
            }
        }

        fn name(&self) -> &'static str {
            match self {
                TabRoute::Processes { .. } => "Processes",
                TabRoute::Services { .. } => "Services",
            }
        }
    }

    #[test]
    fn a_layout_is_not_the_same_props_when_the_page_under_it_changes() {
        let token = UiThreadToken::dangerously_create_token_unchecked();
        let router = Rc::new(Router::<WinUi>::new(FeatureHost::detached(token)));

        let props_at_layout = |route: TabRoute| {
            router.navigate(route).expect("navigate");
            SegmentProps::<WinUi> {
                chain: router.active_chain().expect("a chain is active"),
                scopes: router.active_scopes().expect("scopes are installed"),
                cursor: 0,
            }
        };

        let showing_processes = props_at_layout(TabRoute::Processes {
            context: "ubuntu".to_string(),
        });
        let showing_services = props_at_layout(TabRoute::Services {
            context: "ubuntu".to_string(),
        });

        // The layout itself is unchanged - same type, same scope, same
        // cursor - and comparing only that is what used to make a reconciler
        // skip it. Skipping the layout skips its `outlet()`, so the old page
        // stayed on screen until something unrelated forced a re-render.
        assert_eq!(showing_processes.identity(), showing_services.identity());
        assert!(
            showing_processes != showing_services,
            "a layout showing a different page is not the same props"
        );
    }

    #[test]
    fn navigating_between_siblings_keeps_the_shared_ancestor_scope() {
        let token = UiThreadToken::dangerously_create_token_unchecked();
        let router = Rc::new(Router::<WinUi>::new(FeatureHost::detached(token)));

        router
            .navigate(TabRoute::Processes {
                context: "ubuntu".to_string(),
            })
            .expect("navigate to processes");
        let tabs_installs_after_first = router
            .scope_at(0)
            .unwrap()
            .state::<Tabs>()
            .borrow()
            .install_count;
        assert_eq!(tabs_installs_after_first, 1);

        router
            .navigate(TabRoute::Services {
                context: "ubuntu".to_string(),
            })
            .expect("navigate to services");
        let tabs_installs_after_second = router
            .scope_at(0)
            .unwrap()
            .state::<Tabs>()
            .borrow()
            .install_count;
        assert_eq!(
            tabs_installs_after_second, 1,
            "the shared ProcsTab ancestor must not reinstall when only the leaf changes"
        );
    }

    #[test]
    fn navigating_to_the_same_leaf_type_with_different_params_reinstalls_only_the_leaf() {
        let token = UiThreadToken::dangerously_create_token_unchecked();
        let router = Rc::new(Router::<WinUi>::new(FeatureHost::detached(token)));

        router
            .navigate(TabRoute::Processes {
                context: "ubuntu".to_string(),
            })
            .expect("navigate to processes/ubuntu");
        let leaf_scope_1 = router.active_scope().unwrap();

        router
            .navigate(TabRoute::Processes {
                context: "fedora".to_string(),
            })
            .expect("navigate to processes/fedora");
        let leaf_scope_2 = router.active_scope().unwrap();

        assert_ne!(
            leaf_scope_1, leaf_scope_2,
            "same leaf type but different captured params must reinstall the leaf"
        );
        assert_eq!(
            router
                .scope_at(0)
                .unwrap()
                .state::<Tabs>()
                .borrow()
                .install_count,
            1,
            "the ancestor tab, unaffected by the leaf's own param change, stays put"
        );
    }

    #[test]
    fn routes_macro_nesting_produces_the_right_chain() {
        use std::any::TypeId;

        #[derive(Default)]
        struct Services;
        #[page]
        impl Page for Services {
            type Params = ServicesParams;

            fn view(&self, cx: &mut PageCx<'_, '_, Self>) -> windows_reactor::View {
                let _ = cx;
                windows_reactor::Grid::new().into()
            }
        }

        /// A page of its own rather than the module's `Processes`, for the same
        /// reason as `DerivedTab` below: a page belongs to one tree, since its
        /// ancestry is part of what the compiler reads off it.
        #[derive(Default)]
        struct DerivedProcesses;
        #[page]
        impl Page for DerivedProcesses {
            type Params = DerivedProcessesParams;

            fn view(&self, cx: &mut PageCx<'_, '_, Self>) -> windows_reactor::View {
                let _ = cx;
                windows_reactor::Grid::new().into()
            }
        }

        /// A layout of its own rather than the module's `ProcsTab`: this tree
        /// derives parameters for it, and a layout has one `Params` for every
        /// tree it appears in.
        #[derive(Default)]
        struct DerivedTab;
        #[layout]
        impl Layout for DerivedTab {
            type Params = DerivedTabParams;

            fn view(&self, cx: &mut LayoutCx<'_, '_, Self>) -> windows_reactor::View {
                cx.outlet()
            }
        }

        routes! {
            DerivedRoute {
                layout(DerivedTab) {
                    page(DerivedProcesses) link("/tab/processes/:context") { context: String }
                    page(Services) link("/tab/services/:context") { context: String }
                }
            }
        }

        let processes_chain = DerivedRoute::DerivedProcesses {
            context: "ubuntu".to_string(),
        }
        .chain();
        assert_eq!(processes_chain.len(), 2);
        assert_eq!((processes_chain[0].type_id)(), TypeId::of::<DerivedTab>());
        assert_eq!(
            (processes_chain[1].type_id)(),
            TypeId::of::<DerivedProcesses>()
        );

        let services_chain = DerivedRoute::Services {
            context: "ubuntu".to_string(),
        }
        .chain();
        assert_eq!(services_chain.len(), 2);
        assert_eq!((services_chain[0].type_id)(), TypeId::of::<DerivedTab>());
        assert_eq!((services_chain[1].type_id)(), TypeId::of::<Services>());

        // Same ancestor position across siblings - what lets `Router::navigate`
        // detect a shared prefix and keep that Scope alive.
        assert_eq!(
            (processes_chain[0].type_id)(),
            (services_chain[0].type_id)()
        );

        // #[layout]/#[end_layout] don't touch path/parse - purely a chain()
        // concern.
        assert_eq!(
            DerivedRoute::DerivedProcesses {
                context: "ubuntu".to_string()
            }
            .link()
            .as_deref(),
            Some("/tab/processes/ubuntu")
        );
    }

    #[test]
    fn routes_macro_supports_a_zero_field_page() {
        // Regression test: `page(Home) link("/")` with no trailing `{ field: Type }`
        // block used to generate an enum whose variant was *declared*
        // struct-style (`Home {}`) but *matched/constructed* unit-style
        // (`Home`) in `path`/`parse`/`chain` - a declaration/usage mismatch
        // that fails to compile with E0533 ("expected unit struct, unit
        // variant or constant, found struct variant"). Every call site must
        // consistently use the same (struct-style) shape.
        #[derive(Default)]
        struct Home;
        #[page]
        impl Page for Home {
            type Params = HomeParams;

            fn view(&self, cx: &mut PageCx<'_, '_, Self>) -> windows_reactor::View {
                let _ = cx;
                windows_reactor::Grid::new().into()
            }
        }

        routes! {
            ZeroFieldRoute {
                page(Home) link("/")
            }
        }

        let route = ZeroFieldRoute::Home {};
        assert_eq!(route.link().as_deref(), Some("/"));
        assert_eq!(ZeroFieldRoute::parse("/"), Some(ZeroFieldRoute::Home {}));
    }

    #[test]
    fn a_page_without_a_link_has_no_address_at_all() {
        #[derive(Default)]
        struct Inner;
        #[page]
        impl Page for Inner {
            type Params = InnerParams;

            fn view(&self, _cx: &mut PageCx<'_, '_, Self>) -> windows_reactor::View {
                windows_reactor::Grid::new().into()
            }
        }

        #[derive(Default)]
        struct Shared;
        #[page]
        impl Page for Shared {
            type Params = SharedParams;

            fn view(&self, _cx: &mut PageCx<'_, '_, Self>) -> windows_reactor::View {
                windows_reactor::Grid::new().into()
            }
        }

        routes! {
            MixedRoute {
                page(Inner) { token: String }
                page(Shared) link("/shared/:id") { id: String }
            }
        }

        let inner = MixedRoute::Inner {
            token: "secret".to_string(),
        };
        assert_eq!(inner.link(), None, "nothing outside can name it");
        assert_eq!(inner.name(), "Inner", "but a log still can");

        let surface = MixedRoute::deep_links();
        assert_eq!(
            surface.iter().map(|link| link.path).collect::<Vec<_>>(),
            ["/shared/:id"],
            "the external surface is exactly what agreed to be on it"
        );
        assert_eq!(surface[0].tree, "MixedRoute");
        assert_eq!(surface[0].route, "Shared");
        assert_eq!(
            surface[0].captures,
            [guinea::link::Capture {
                name: "id",
                ty: "String"
            }],
            "the capture carries what would silently change the address"
        );

        assert_eq!(
            MixedRoute::parse("/secret"),
            None,
            "an unaddressed page is unreachable from a path, whatever the path"
        );
        assert_eq!(
            MixedRoute::parse("/shared/42"),
            Some(MixedRoute::Shared {
                id: "42".to_string()
            })
        );
    }

    #[test]
    fn a_literal_wins_over_a_capture_whatever_the_declaration_order() {
        #[derive(Default)]
        struct Settings;
        #[page]
        impl Page for Settings {
            type Params = SettingsParams;

            fn view(&self, _cx: &mut PageCx<'_, '_, Self>) -> windows_reactor::View {
                windows_reactor::Grid::new().into()
            }
        }

        #[derive(Default)]
        struct Host;
        #[page]
        impl Page for Host {
            type Params = HostParams;

            fn view(&self, _cx: &mut PageCx<'_, '_, Self>) -> windows_reactor::View {
                windows_reactor::Grid::new().into()
            }
        }

        // The capture is declared *first*, and matches anything of this
        // length. Trying arms in declaration order gave it "/settings/pods"
        // and the literal page was unreachable.
        routes! {
            ShadowRoute {
                page(Host) link("/:name/pods") { name: String }
                page(Settings) link("/settings/pods")
            }
        }

        assert_eq!(
            ShadowRoute::parse("/settings/pods"),
            Some(ShadowRoute::Settings {}),
            "the literal is more specific, so it wins wherever it was declared"
        );
        assert_eq!(
            ShadowRoute::parse("/kube-01/pods"),
            Some(ShadowRoute::Host {
                name: "kube-01".to_string()
            })
        );
    }

    #[test]
    fn a_capture_that_does_not_parse_falls_through_to_the_next_branch() {
        #[derive(Default)]
        struct ById;
        #[page]
        impl Page for ById {
            type Params = ByIdParams;

            fn view(&self, _cx: &mut PageCx<'_, '_, Self>) -> windows_reactor::View {
                windows_reactor::Grid::new().into()
            }
        }

        #[derive(Default)]
        struct ByName;
        #[page]
        impl Page for ByName {
            type Params = ByNameParams;

            fn view(&self, _cx: &mut PageCx<'_, '_, Self>) -> windows_reactor::View {
                windows_reactor::Grid::new().into()
            }
        }

        // Two branches of the same shape, told apart by whether the segment
        // parses as a number. The old emission used `?` on the parse, which
        // returned from `parse` itself - so a non-numeric path matched
        // nothing at all instead of falling through.
        routes! {
            EitherRoute {
                page(ById) link("/job/:id") { id: u32 }
                page(ByName) link("/name/:who") { who: String }
            }
        }

        assert_eq!(
            EitherRoute::parse("/job/42"),
            Some(EitherRoute::ById { id: 42 })
        );
        assert_eq!(
            EitherRoute::parse("/name/ada"),
            Some(EitherRoute::ByName {
                who: "ada".to_string()
            }),
            "a branch whose capture failed to parse must not end the search"
        );
        assert_eq!(EitherRoute::parse("/job/ada"), None);
    }

    #[test]
    fn navigating_away_from_page_disposes_actor_subscribed_to_global_bus() {
        use guinea_core::actor::event_bus::GlobalEventBus;
        use guinea_macros::{Event, actor, handler};
        use std::cell::RefCell;
        use std::rc::Rc;

        #[derive(Clone, Debug, Event)]
        struct ProbeEvent(u32);

        struct ProbeActor {
            seen: Rc<RefCell<Vec<u32>>>,
            /// Simulates a real actor holding a UI port closure produced by
            /// `ctx.port::<Reducer>()`. The port must not keep the page scope
            /// alive via a strong Rc, otherwise Scope -> Addr -> Actor -> Port
            /// -> Scope forms an Rc cycle and the actor never drops on navigation.
            _push: Box<dyn Fn(()) + 'static>,
        }

        impl std::fmt::Debug for ProbeActor {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.debug_struct("ProbeActor").finish()
            }
        }

        impl Drop for ProbeActor {
            fn drop(&mut self) {
                PROBE_DROPPED.with(|d| *d.borrow_mut() = true);
            }
        }

        actor! {
            ProbeActor {
                handlers { ProbeEvent }
            }
        }

        #[handler]
        fn on_probe(this: &mut ProbeActor, ProbeEvent(n): ProbeEvent) {
            this.seen.borrow_mut().push(n);
        }

        thread_local! {
            static PROBE_DROPPED: RefCell<bool> = RefCell::new(false);
        }

        #[derive(Clone, Default, Debug)]
        struct Probe {
            value: u32,
        }

        impl Reducer for Probe {
            type Update = ();
    
            fn reduce(&mut self, _: ()) {
                self.value += 1;
            }
        }

        #[derive(Default)]
        struct ProbePage;
        #[page]
        impl Page for ProbePage {
            type Params = ProbePageParams;

            fn install(ctx: &FeatureInitContext, _params: &ProbePageParams) -> anyhow::Result<()> {
                PROBE_DROPPED.with(|d| *d.borrow_mut() = false);
                let push = ctx.state::<Probe>().plain().port();
                let addr = ctx.spawn(ProbeActor {
                    seen: Rc::new(RefCell::new(Vec::new())),
                    _push: Box::new(move |()| push.send(())),
                });
                addr.subscribe_on::<ProbeEvent>(guinea::core::trace::Bus::Global);
                Ok(())
            }

            fn view(&self, _cx: &mut PageCx<'_, '_, Self>) -> windows_reactor::View {
                windows_reactor::Grid::new().into()
            }
        }

        #[derive(Default)]
        struct OtherPage;
        #[page]
        impl Page for OtherPage {
            type Params = OtherPageParams;

            fn view(&self, _cx: &mut PageCx<'_, '_, Self>) -> windows_reactor::View {
                windows_reactor::Grid::new().into()
            }
        }

        #[derive(Default)]
        struct ProbeLayout;
        #[layout]
        impl Layout for ProbeLayout {
            type Params = ProbeLayoutParams;

            fn view(&self, cx: &mut LayoutCx<'_, '_, Self>) -> windows_reactor::View {
                cx.outlet()
            }
        }

        routes! {
            ProbeRoute {
                layout(ProbeLayout) {
                    page(ProbePage)
                    page(OtherPage)
                }
            }
        }

        let token = UiThreadToken::dangerously_create_token_unchecked();
        let router = Rc::new(Router::<WinUi>::new(FeatureHost::detached(token)));

        router
            .navigate(ProbeRoute::ProbePage {})
            .expect("navigate to probe");
        assert!(
            GlobalEventBus::has_subscribers::<ProbeEvent>(),
            "actor must be subscribed to the global bus while the page is active"
        );

        router
            .navigate(ProbeRoute::OtherPage {})
            .expect("navigate to other");

        assert!(
            PROBE_DROPPED.with(|d| *d.borrow()),
            "ProbeActor state must be dropped when the page scope is torn down"
        );

        assert!(
            !GlobalEventBus::has_subscribers::<ProbeEvent>(),
            "global bus subscription must be removed when the page scope drops"
        );
    }
}

mod a_reducer_is_plain_rust {
    use guinea_core::scope::{Reducer, ScopeTree};

    /// What `#[reducer]`, `#[derive(ReducerState)]`, `#[dispatch]`, `#[port]`
    /// and half of `messages!` used to produce between them. The whole
    /// declaration is here, and the central type of the feature - the one
    /// every other file names - is written rather than derived from a
    /// function's name by changing its case.
    #[derive(Clone, Default, Debug)]
    struct Widget {
        value: i32,
    }

    impl Reducer for Widget {
        type Update = i32;

        fn reduce(&mut self, to: i32) {
            self.value = to;
        }
    }

    #[test]
    fn the_state_is_the_reducer() {
        let scope = ScopeTree::new();
        scope.push::<Widget>(42);

        assert_eq!(scope.state::<Widget>().borrow().value, 42);
    }
}