guinea 0.26.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
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
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
//! The harness on the race it exists for: two searches in flight, and the
//! answer that lands last decides what the page shows.
//!
//! The user types `gu`, then `guinea`. A search takes as long as it takes, so
//! the one for `gu` can land after the one for `guinea` - and a searcher that
//! shows whatever arrived last shows results for a query nobody is looking at
//! any more. Under tokio that happens in production and almost never in a
//! test; here the seeds walk the orders until one does.

use guinea::app::Harness;
use guinea::prelude::*;

const CATALOGUE: [&str; 6] = ["guinea", "guinea-app", "gui", "gum", "gulp", "gust"];

#[derive(Default, Clone, PartialEq, Debug, serde::Serialize)]
pub struct Results {
    pub query: String,
    pub found: Vec<&'static str>,
}

#[derive(Clone, Debug)]
pub struct Found {
    pub query: String,
    pub found: Vec<&'static str>,
}

impl Reducer for Results {
    type Update = Found;

    fn reduce(&mut self, update: Found) {
        self.query = update.query;
        self.found = update.found;
    }
}

#[derive(Clone, Debug)]
pub struct Query(pub String);

/// Takes as long as the seed says.
async fn search(text: String) -> Found {
    let found: Vec<&'static str> = CATALOGUE
        .iter()
        .copied()
        .filter(|name| name.contains(text.as_str()))
        .collect();

    guinea::core::executor::random_delay().await;

    Found { query: text, found }
}

/// Shows whatever answer arrives, in the order they arrive.
mod naive {
    use super::*;

    #[derive(Debug)]
    pub struct Searcher {
        pub push: Push<Results>,
    }

    actor! {
        Searcher {
            handlers { Query => { bg Found }, Found }
        }
    }

    #[handler]
    fn query(_this: &mut Searcher, Query(text): Query, cx: Cx) {
        cx.spawn_bg::<Found, _>(search(text));
    }

    #[handler]
    fn found(this: &mut Searcher, found: Found) {
        this.push.send(found);
    }

    feature! {
        pub Search {
            exports { Results }
        }
    }

    #[installs]
    fn searching(cx: &FeatureInitContext) -> anyhow::Result<Search> {
        let (results, _) = cx.state::<Results>().driven_by(|push| Searcher { push });
        Ok(Search(results))
    }
}

/// Numbers every query and shows an answer only if it is for the latest.
mod latest {
    use super::*;

    #[derive(Debug)]
    pub struct Searcher {
        pub push: Push<Results>,
        pub asked: u64,
    }

    #[derive(Clone, Debug)]
    pub struct Answered {
        pub asked: u64,
        pub found: Found,
    }

    actor! {
        Searcher {
            handlers { Query => { bg Answered }, Answered }
        }
    }

    #[handler]
    fn query(this: &mut Searcher, Query(text): Query, cx: Cx) {
        this.asked += 1;

        let asked = this.asked;
        cx.spawn_bg::<Answered, _>(async move {
            Answered {
                asked,
                found: search(text).await,
            }
        });
    }

    #[handler]
    fn answered(this: &mut Searcher, answered: Answered) {
        if answered.asked == this.asked {
            this.push.send(answered.found);
        }
    }

    feature! {
        pub Search {
            exports { Results }
        }
    }

    #[installs]
    fn searching(cx: &FeatureInitContext) -> anyhow::Result<Search> {
        let (results, _) = cx.state::<Results>().driven_by(|push| Searcher { push, asked: 0 });
        Ok(Search(results))
    }
}

fn type_gu_then_guinea(h: &Harness) {
    h.dispatch::<Results>().emit(Query("gu".into()));
    h.dispatch::<Results>().emit(Query("guinea".into()));
    h.settled();
}

#[guinea::test(iterations = 200)]
#[should_panic(expected = "SEED=")]
fn some_order_shows_an_answer_to_a_query_nobody_is_looking_at(h: &mut Harness) {
    h.install::<naive::Search>(&()).unwrap();
    type_gu_then_guinea(h);

    assert_eq!(h.state::<Results>().query, "guinea");
}

#[guinea::test(iterations = 200)]
fn in_every_order_the_latest_query_wins(h: &mut Harness) {
    h.install::<latest::Search>(&()).unwrap();
    type_gu_then_guinea(h);

    let results = h.state::<Results>();
    assert_eq!(results.query, "guinea");
    assert_eq!(results.found, ["guinea", "guinea-app"]);
    assert_eq!(h.stuck(), 0);
}

/// Waits the way the example's metrics do: `tokio::time::sleep` inside the
/// background task.
mod sleepy {
    use super::*;

    #[derive(Debug)]
    pub struct Searcher {
        pub push: Push<Results>,
    }

    actor! {
        Searcher {
            handlers { Query => { bg Found }, Found }
        }
    }

    #[handler]
    fn query(_this: &mut Searcher, Query(text): Query, cx: Cx) {
        cx.spawn_bg::<Found, _>(async move {
            guinea::core::__private::tokio::time::sleep(std::time::Duration::from_millis(800)).await;
            Found { query: text, found: Vec::new() }
        });
    }

    #[handler]
    fn found(this: &mut Searcher, found: Found) {
        this.push.send(found);
    }

    feature! {
        pub Search {
            exports { Results }
        }
    }

    #[installs]
    fn searching(cx: &FeatureInitContext) -> anyhow::Result<Search> {
        let (results, _) = cx.state::<Results>().driven_by(|push| Searcher { push });
        Ok(Search(results))
    }
}

#[guinea::test(iterations = 8)]
fn a_tokio_sleep_in_the_code_under_test_waits_on_the_test_clock(h: &mut Harness) {
    let started = std::time::Instant::now();

    h.install::<sleepy::Search>(&()).unwrap();
    h.dispatch::<Results>().emit(Query("gu".into()));

    h.settled();
    assert_eq!(h.state::<Results>().query, "", "answered before its sleep was over");
    assert_eq!(h.stuck(), 1);

    h.advance(std::time::Duration::from_millis(799));
    assert_eq!(h.state::<Results>().query, "", "a millisecond early");

    h.advance(std::time::Duration::from_millis(1));
    assert_eq!(h.state::<Results>().query, "gu");
    assert_eq!(h.stuck(), 0);

    assert!(started.elapsed() < std::time::Duration::from_millis(200), "waited in real time");
}

/// Samples on a timer, the way the example's metrics feature polls.
mod polling {
    use super::*;

    #[derive(Default, Clone, PartialEq, Debug)]
    pub struct Samples {
        pub taken: u32,
    }

    #[derive(Clone, Debug)]
    pub struct Taken;

    impl Reducer for Samples {
        type Update = Taken;

        fn reduce(&mut self, _taken: Taken) {
            self.taken += 1;
        }
    }

    #[derive(Clone, Debug)]
    pub struct Sample;

    #[derive(Debug)]
    pub struct Sampler {
        pub push: Push<Samples>,
    }

    actor! {
        Sampler {
            handlers { Sample }
        }
    }

    #[handler]
    fn sample(this: &mut Sampler, _: Sample) {
        this.push.send(Taken);
    }

    feature! {
        pub Polling {
            exports { Samples }
        }
    }

    #[installs]
    fn polling(cx: &FeatureInitContext) -> anyhow::Result<Polling> {
        let (samples, sampler) = cx.state::<Samples>().driven_by(|push| Sampler { push });
        cx.every(std::time::Duration::from_millis(100), &sampler, || Sample);
        Ok(Polling(samples))
    }
}

#[guinea::test(iterations = 4)]
fn a_feature_timer_ticks_on_the_test_clock(h: &mut Harness) {
    h.install::<polling::Polling>(&()).unwrap();

    h.advance(std::time::Duration::from_millis(99));
    assert_eq!(h.state::<polling::Samples>().taken, 0);

    h.advance(std::time::Duration::from_millis(901));
    assert_eq!(h.state::<polling::Samples>().taken, 10);
}

/// A poll that never ends runs beside the search. Waiting for everything to
/// go quiet would never return; waiting for what the query set off does, and
/// the chain it hands back says what that was.
#[guinea::test(iterations = 50)]
fn settling_one_action_waits_for_its_own_work_and_no_more(h: &mut Harness) {
    h.install::<polling::Polling>(&()).unwrap();
    h.install::<sleepy::Search>(&()).unwrap();

    let asked = h.act::<Results>(Query("gu".into()));
    assert!(!asked.is_settled());

    asked.settle();
    assert_eq!(h.state::<Results>().query, "gu");

    let chain = asked.chain();
    assert!(chain.handled::<Query>());
    assert!(chain.handled::<Found>());
    assert!(chain.pushed::<Results>());
    assert!(!chain.cancelled());

    let taken = h.state::<polling::Samples>().taken;
    assert!(
        (7..=8).contains(&taken),
        "the clock went 800 ms for the search, and the poll ran on it: {taken}"
    );
}

/// The shape of what a query sets off, and where it leaves the state: a
/// refactor that drops the push, or sends the answer round an extra actor,
/// changes the first; one that reorders the results changes the second.
#[guinea::test(iterations = 50)]
fn what_a_query_sets_off_keeps_its_shape(h: &mut Harness) {
    h.install::<latest::Search>(&()).unwrap();

    let asked = h.act::<Results>(Query("guinea".into()));
    asked.settle();

    insta::assert_ron_snapshot!("a_query", asked.chain().shape());
    insta::assert_ron_snapshot!("its_results", *h.state::<Results>());
}

/// What an agent reports, arriving on the global bus the way a report from
/// another process does.
mod reports {
    use super::*;

    #[derive(Clone, Debug, Event)]
    pub struct Report(pub Vec<&'static str>);

    #[derive(Default, Clone, PartialEq, Debug)]
    pub struct Running {
        pub names: Vec<&'static str>,
    }

    impl Reducer for Running {
        type Update = Report;

        fn reduce(&mut self, report: Report) {
            self.names = report.0;
        }
    }

    #[derive(Debug)]
    pub struct Reader {
        pub push: Push<Running>,
    }

    /// Asks for a report to be put on the global bus, the way a page's
    /// End task asks the agent.
    #[derive(Clone, Debug)]
    pub struct Announce(pub Vec<&'static str>);

    actor! {
        Reader {
            handlers { Report, Announce }
        }
    }

    #[handler]
    fn report(this: &mut Reader, report: Report) {
        this.push.send(report);
    }

    #[handler]
    fn announce(_this: &mut Reader, Announce(text): Announce) {
        GlobalEventBus::publish(Report(text));
    }

    feature! {
        pub Reports {
            exports { Running }
        }
    }

    #[installs]
    fn reports(cx: &FeatureInitContext) -> anyhow::Result<Reports> {
        let (running, reader) = cx.state::<Running>().driven_by(|push| Reader { push });
        reader.subscribe_on::<Report>(Bus::Global);
        Ok(Reports(running))
    }

    /// What a plugin provides, for a feature to read.
    pub struct Prefix(pub &'static str);

    pub struct Prefixing;

    impl Plugin for Prefixing {
        const ID: &'static str = "test.prefixing";
        type Exports = ();

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

    thread_local! {
        static IN_PLACE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
    }

    /// Puts something process-wide in place, the way the store plugin puts
    /// its global store, and refuses to do it twice.
    pub struct Global;

    impl Plugin for Global {
        const ID: &'static str = "test.global";
        type Exports = ();

        fn build(self, app: &mut PluginBuilder) -> anyhow::Result<()> {
            if IN_PLACE.get() {
                anyhow::bail!("a global is in place already");
            }

            IN_PLACE.set(true);
            app.on_cleanup(|_| {
                IN_PLACE.set(false);
                Ok(())
            });
            Ok(())
        }
    }

    feature! {
        pub Prefixed {}
    }

    #[installs]
    fn prefixed(cx: &FeatureInitContext) -> anyhow::Result<Prefixed> {
        let prefix = cx.require::<Prefix>()?;
        assert_eq!(prefix.0, "proc-");
        Ok(Prefixed)
    }

    /// A setting a feature has a default for and an application may override.
    #[derive(Clone, Copy, Default, Debug, PartialEq)]
    pub struct Limit(pub u32);

    thread_local! {
        pub static LIMITED_TO: std::cell::Cell<Option<Limit>> = const { std::cell::Cell::new(None) };
    }

    feature! {
        pub Limited {}
    }

    #[installs]
    fn limited(cx: &FeatureInitContext) -> anyhow::Result<Limited> {
        LIMITED_TO.set(Some(cx.require_or_default::<Limit>()));
        Ok(Limited)
    }
}

#[guinea::test(iterations = 20)]
fn a_report_published_in_a_test_is_an_act_to_settle(h: &mut Harness) {
    h.install::<reports::Reports>(&()).unwrap();

    let first = h.publish(reports::Report(vec!["explorer", "code"]));
    first.settle();
    assert_eq!(h.state::<reports::Running>().names, ["explorer", "code"]);
    assert!(first.chain().handled::<reports::Report>());
    assert!(first.chain().pushed::<reports::Running>());

    h.publish(reports::Report(vec!["code"])).settle();
    assert_eq!(h.state::<reports::Running>().names, ["code"]);
}

/// A name a tool may set from outside, as an action or as the event the
/// action publishes.
mod named {
    use super::*;

    #[derive(Clone, Debug, serde::Deserialize, guinea::Remote)]
    #[remote(action)]
    pub struct Rename(pub String);

    #[derive(Clone, Debug, serde::Deserialize, Event, guinea::Remote)]
    #[remote(event)]
    pub struct Renamed(pub String);

    #[derive(Default, Clone, PartialEq, Debug)]
    pub struct Name(pub String);

    impl Reducer for Name {
        type Update = Renamed;

        fn reduce(&mut self, renamed: Renamed) {
            self.0 = renamed.0;
        }
    }

    #[derive(Debug)]
    pub struct Namer {
        pub push: Push<Name>,
    }

    actor! {
        Namer {
            handlers { Rename, Renamed }
        }
    }

    #[handler]
    fn rename(_this: &mut Namer, Rename(name): Rename) {
        GlobalEventBus::publish(Renamed(name));
    }

    #[handler]
    fn renamed(this: &mut Namer, renamed: Renamed) {
        this.push.send(renamed);
    }

    feature! {
        pub Naming {
            exports { Name }
        }
    }

    #[installs]
    fn naming(cx: &FeatureInitContext) -> anyhow::Result<Naming> {
        let (name, namer) = cx.state::<Name>().driven_by(|push| Namer { push });
        namer.subscribe_on::<Renamed>(Bus::Global);
        Ok(Naming(name))
    }
}

/// Another page's action with the same name as `named::Rename`, answered by a
/// feature of its own.
mod titled {
    use super::*;

    #[derive(Clone, Debug, serde::Deserialize, guinea::Remote)]
    #[remote(action)]
    pub struct Rename(pub String);

    #[derive(Default, Clone, PartialEq, Debug)]
    pub struct Title(pub String);

    impl Reducer for Title {
        type Update = Rename;

        fn reduce(&mut self, rename: Rename) {
            self.0 = rename.0;
        }
    }

    #[derive(Debug)]
    pub struct Titler {
        pub push: Push<Title>,
    }

    actor! {
        Titler {
            handlers { Rename }
        }
    }

    #[handler]
    fn rename(this: &mut Titler, rename: Rename) {
        this.push.send(rename);
    }

    feature! {
        pub Titling {}
    }

    #[installs]
    fn titling(cx: &FeatureInitContext) -> anyhow::Result<Titling> {
        cx.state::<Title>().driven_by(|push| Titler { push });
        Ok(Titling)
    }
}

#[guinea::test(iterations = 4)]
fn an_action_sent_as_json_reaches_the_scope_that_answers_it(h: &mut Harness) {
    use guinea::core::remote;

    h.install::<named::Naming>(&()).unwrap();
    assert!(remote::actions().contains(&"Rename"), "{:?}", remote::actions());

    let scopes = [h.segment().context().scope];

    remote::act_in(&scopes, "Rename", r#""guinea""#).unwrap();
    h.settled();
    assert_eq!(h.state::<named::Name>().0, "guinea");

    assert!(remote::act_in(&scopes, "Rename", "42").is_err(), "a number is not a name");
    assert!(remote::act_in(&scopes, "Rename", "42").unwrap_err().contains("Rename"));

    let tree = guinea::core::scope::ScopeTree::new();
    let elsewhere = [tree.scope()];
    let refused = remote::act_in(&elsewhere, "Rename", r#""x""#).unwrap_err();
    assert!(refused.contains("nothing on the open page answers"), "{refused}");
}

/// Two pages each have a `Rename`. The short name means the one the open
/// page answers, the page before the layout above it.
#[guinea::test(iterations = 4)]
fn two_actions_of_one_name_each_reach_the_page_that_answers_it(h: &mut Harness) {
    use guinea::core::remote;

    h.install::<named::Naming>(&()).unwrap();
    let page = h.child();
    page.install::<titled::Titling>(&()).unwrap();

    let layout = [h.segment().context().scope];
    let both = [h.segment().context().scope, page.context().scope];

    remote::act_in(&both, "Rename", r#""page""#).unwrap();
    remote::act_in(&layout, "Rename", r#""layout""#).unwrap();
    h.settled();

    assert_eq!(page.state::<titled::Title>().0, "page");
    assert_eq!(h.state::<named::Name>().0, "layout");
}

/// Where both answer in one place, the short name is not enough - and the
/// path is.
#[guinea::test(iterations = 4)]
fn two_actions_of_one_name_in_one_scope_are_told_apart_by_path(h: &mut Harness) {
    use guinea::core::remote;

    h.install::<named::Naming>(&()).unwrap();
    h.install::<titled::Titling>(&()).unwrap();
    let scopes = [h.segment().context().scope];

    let refused = remote::act_in(&scopes, "Rename", r#""x""#).unwrap_err();
    assert!(refused.contains("send one by its path"), "{refused}");
    assert!(refused.contains("titled::Rename"), "{refused}");

    remote::act_in(&scopes, "harness::titled::Rename", r#""by path""#).unwrap();
    h.settled();
    assert_eq!(h.state::<titled::Title>().0, "by path");
}

#[guinea::test(iterations = 4)]
fn an_event_sent_as_json_goes_out_on_the_global_bus(h: &mut Harness) {
    use guinea::core::remote;

    h.install::<named::Naming>(&()).unwrap();
    assert!(remote::events().contains(&"Renamed"), "{:?}", remote::events());

    remote::publish("Renamed", r#""code""#).unwrap();
    h.settled();
    assert_eq!(h.state::<named::Name>().0, "code");
}

/// An actor that hears reports because its manifest says so.
mod listening {
    use super::*;

    #[derive(Default, Clone, PartialEq, Debug)]
    pub struct Heard(pub u32);

    #[derive(Clone, Debug)]
    pub struct Once;

    impl Reducer for Heard {
        type Update = Once;

        fn reduce(&mut self, _once: Once) {
            self.0 += 1;
        }
    }

    #[derive(Debug)]
    pub struct Listener {
        pub push: Push<Heard>,
    }

    actor! {
        Listener {
            handlers { reports::Report }
            subscribes { reports::Report }
        }
    }

    #[handler]
    fn report(this: &mut Listener, _: reports::Report) {
        this.push.send(Once);
    }

    feature! {
        pub Listening {
            exports { Heard }
        }
    }

    #[installs]
    fn listening(cx: &FeatureInitContext) -> anyhow::Result<Listening> {
        let (heard, _) = cx.state::<Heard>().driven_by(|push| Listener { push });
        Ok(Listening(heard))
    }
}

#[guinea::test(iterations = 4)]
fn an_actor_hears_the_bus_until_its_page_is_left(h: &mut Harness) {
    let page = h.child();
    page.install::<reports::Reports>(&()).unwrap();
    assert_eq!(GlobalEventBus::bus().subscriptions(), [("reports::Report", 1)]);

    page.leave();
    assert!(GlobalEventBus::bus().subscriptions().is_empty(), "the actor went, and so did its subscription");
}

#[guinea::test(iterations = 4)]
fn what_an_actor_s_manifest_subscribes_to_it_hears_and_lets_go_with_its_page(h: &mut Harness) {
    let page = h.child();
    page.install::<listening::Listening>(&()).unwrap();

    h.publish(reports::Report(vec!["code"])).settle();
    assert_eq!(page.state::<listening::Heard>().0, 1);

    page.leave();
    assert!(GlobalEventBus::bus().subscriptions().is_empty(), "the manifest's subscription outlived the page");
}

/// The actor publishes through `GlobalEventBus::publish`, which hops to the
/// UI thread; settling the action waits for the hop and what it set off.
#[guinea::test(iterations = 20)]
fn settling_an_action_waits_for_what_it_published_on_the_global_bus(h: &mut Harness) {
    h.install::<reports::Reports>(&()).unwrap();

    let announced = h.act::<reports::Running>(reports::Announce(vec!["code"]));
    announced.settle();

    assert!(announced.chain().published::<reports::Report>(), "{:#?}", announced.chain());
    assert_eq!(h.state::<reports::Running>().names, ["code"]);
}

/// Each seed leaves a subscription behind on purpose; the next one must not
/// hear it.
#[guinea::test(iterations = 3)]
fn every_harness_starts_on_an_empty_global_bus(h: &mut Harness) {
    assert!(
        GlobalEventBus::bus().subscriptions().is_empty(),
        "an earlier seed's subscribers are still on the bus"
    );

    h.install::<reports::Reports>(&()).unwrap();
    std::mem::forget(GlobalEventBus::subscribe_fn(|_: reports::Report| {}));

    assert_eq!(GlobalEventBus::bus().subscriptions(), [("reports::Report", 2)]);
}

#[guinea::test(iterations = 2)]
fn a_plugin_installed_into_the_harness_serves_its_features(h: &mut Harness) {
    assert!(
        h.child().install::<reports::Prefixed>(&()).is_err(),
        "nothing provides the prefix yet"
    );

    h.plugin(reports::Prefixing).unwrap();
    h.child().install::<reports::Prefixed>(&()).unwrap();
}

#[guinea::test(iterations = 2)]
fn a_service_provided_to_the_harness_serves_its_features(h: &mut Harness) {
    h.provide(reports::Prefix("proc-"));
    h.child().install::<reports::Prefixed>(&()).unwrap();
}

#[guinea::test(iterations = 2)]
fn a_segment_requires_what_the_harness_was_provided(h: &mut Harness) {
    h.provide(reports::Prefix("proc-"));

    assert_eq!(h.segment().require::<reports::Prefix>().unwrap().0, "proc-");
    assert_eq!(h.child().try_require::<reports::Prefix>().map(|prefix| prefix.0), Some("proc-"));
    assert!(h.segment().try_require::<reports::Limit>().is_none());
    assert!(h.segment().require::<reports::Limit>().is_err());
}

mod everywhere {
    use super::*;
    use guinea::Services;
    use guinea::enter::EnterCx;

    pub fn prefix<C: Services + ?Sized>(cx: &C) -> Option<&'static str> {
        cx.try_require::<reports::Prefix>().map(|prefix| prefix.0)
    }

    thread_local! {
        pub static FROM_A_PLUGIN: std::cell::Cell<Option<&'static str>> =
            const { std::cell::Cell::new(None) };
    }

    pub struct Probe;

    impl Plugin for Probe {
        const ID: &'static str = "test.probe";
        type Exports = ();

        fn build(self, app: &mut PluginBuilder) -> anyhow::Result<()> {
            FROM_A_PLUGIN.set(prefix(app));
            Ok(())
        }
    }

    pub fn from_a_guard(segment: &guinea::app::Segment<'_>) -> Option<&'static str> {
        prefix(&EnterCx::new(&segment.context().services, "Route"))
    }
}

#[guinea::test(iterations = 2)]
fn one_extension_reaches_a_service_from_every_context(h: &mut Harness) {
    h.provide(reports::Prefix("proc-"));
    h.plugin(everywhere::Probe).unwrap();
    let segment = h.child();

    let found = [
        everywhere::FROM_A_PLUGIN.get(),
        everywhere::prefix(segment.context()),
        everywhere::prefix(&segment),
        everywhere::from_a_guard(&segment),
    ];

    assert_eq!(found, [Some("proc-"); 4]);
    assert!(guinea::Services::require::<reports::Limit>(&segment).is_err());
}

#[guinea::test(iterations = 2)]
fn a_setting_nobody_provided_is_its_default(h: &mut Harness) {
    h.child().install::<reports::Limited>(&()).unwrap();
    assert_eq!(reports::LIMITED_TO.get(), Some(reports::Limit(0)));
}

#[guinea::test(iterations = 2)]
fn a_setting_the_application_provided_is_what_it_provided(h: &mut Harness) {
    h.provide(reports::Limit(7));
    h.child().install::<reports::Limited>(&()).unwrap();
    assert_eq!(reports::LIMITED_TO.get(), Some(reports::Limit(7)));
}

/// How many tests are inside the one thing a process has one of.
static INSIDE: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);

/// What a test holding a process-wide resource does: takes it, holds it a
/// while, lets it go - and would see another test in there with it.
fn hold_the_one_thing() {
    use std::sync::atomic::Ordering;

    assert_eq!(INSIDE.fetch_add(1, Ordering::SeqCst), 0, "another test is in here too");
    std::thread::sleep(std::time::Duration::from_millis(10));
    INSIDE.fetch_sub(1, Ordering::SeqCst);
}

#[guinea::test(iterations = 5, exclusive = "the one thing")]
fn tests_that_name_one_key_take_turns(_h: &mut Harness) {
    hold_the_one_thing();
}

#[guinea::test(iterations = 5, exclusive = "the one thing")]
fn tests_that_name_one_key_take_turns_with_this_one_too(_h: &mut Harness) {
    hold_the_one_thing();
}

/// Every seed installs the plugin again: the harness before it has to have
/// run the plugin's cleanup.
#[guinea::test(iterations = 3)]
fn a_harness_runs_its_plugins_cleanups_when_it_goes(h: &mut Harness) {
    h.plugin(reports::Global).unwrap();
}

/// A page's own count of what the layout above it sampled, kept by observing
/// the layout's reducer.
mod echo {
    use super::*;

    #[derive(Default, Clone, PartialEq, Debug)]
    pub struct Seen {
        pub samples: u32,
    }

    #[derive(Clone, Debug)]
    pub struct Saw;

    impl Reducer for Seen {
        type Update = Saw;

        fn reduce(&mut self, _saw: Saw) {
            self.samples += 1;
        }
    }

    feature! {
        pub Echo {
            exports { Seen }
        }
    }

    #[installs]
    fn echo(cx: &FeatureInitContext) -> anyhow::Result<Echo> {
        let seen = cx.state::<Seen>().plain();

        let pushing = seen.clone();
        cx.observe::<polling::Samples>(move |_taken| pushing.push(Saw));

        Ok(Echo(seen))
    }
}

fn ms(millis: u64) -> std::time::Duration {
    std::time::Duration::from_millis(millis)
}

#[guinea::test(iterations = 8)]
fn a_page_reads_and_hears_what_the_layout_above_it_exports(h: &mut Harness) {
    h.install::<polling::Polling>(&()).unwrap();

    let page = h.child();
    page.install::<echo::Echo>(&()).unwrap();

    h.advance(ms(300));
    assert_eq!(page.state::<polling::Samples>().taken, 3, "read from the layout");
    assert_eq!(page.state::<echo::Seen>().samples, 3, "heard every sample");
}

#[guinea::test(iterations = 50)]
fn leaving_a_page_mid_request_ends_its_work_and_only_its_work(h: &mut Harness) {
    h.install::<polling::Polling>(&()).unwrap();

    let page = h.child();
    page.install::<sleepy::Search>(&()).unwrap();

    let asked = page.act::<Results>(Query("gu".into()));
    h.advance(ms(400));
    page.leave();

    asked.settle();
    let chain = asked.chain();
    assert!(chain.cancelled(), "the search outlived its page:\n{chain:#?}");
    assert!(!chain.pushed::<Results>());

    h.advance(ms(600));
    assert_eq!(h.state::<polling::Samples>().taken, 10, "the layout's poll stopped with the page");
}

/// A watch the way a service pushes: nothing comes until the other end sends,
/// and the watch is opened by a click.
mod watching {
    use super::*;
    use guinea::core::__private::tokio::sync::mpsc;
    use guinea::core::actor::Stream;
    use std::pin::Pin;
    use std::sync::{Arc, Mutex};
    use std::task::{Context, Poll};

    #[derive(Debug)]
    pub struct Changes(pub mpsc::UnboundedReceiver<u32>);

    impl Stream for Changes {
        type Item = u32;

        fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<u32>> {
            self.0.poll_recv(cx)
        }
    }

    #[derive(Clone, Debug)]
    pub struct Watch(pub Arc<Mutex<Option<Changes>>>);

    impl Watch {
        pub fn of(changes: mpsc::UnboundedReceiver<u32>) -> Self {
            Self(Arc::new(Mutex::new(Some(Changes(changes)))))
        }
    }

    #[derive(Clone, Debug)]
    pub struct Changed(pub u32);

    #[derive(Default, Clone, PartialEq, Debug)]
    pub struct Seen(pub Vec<u32>);

    impl Reducer for Seen {
        type Update = Changed;

        fn reduce(&mut self, Changed(value): Changed) {
            self.0.push(value);
        }
    }

    #[derive(Debug)]
    pub struct Watcher {
        pub push: Push<Seen>,
    }

    actor! {
        Watcher {
            handlers { Watch => { bg Changed }, Changed }
        }
    }

    #[handler]
    fn watch(_this: &mut Watcher, Watch(changes): Watch, cx: Cx) {
        let changes = changes.lock().unwrap().take().expect("a watch is opened once");
        cx.spawn_source(changes, Changed);
    }

    #[handler]
    fn changed(this: &mut Watcher, changed: Changed) {
        this.push.send(changed);
    }

    feature! {
        pub Watching {
            exports { Seen }
        }
    }

    #[installs]
    fn watching(cx: &FeatureInitContext) -> anyhow::Result<Watching> {
        let (seen, _) = cx.state::<Seen>().driven_by(|push| Watcher { push });
        Ok(Watching(seen))
    }
}

#[guinea::test(iterations = 20)]
fn a_click_that_opens_a_watch_is_done_once_the_watch_is_open(h: &mut Harness) {
    use guinea::core::__private::tokio::sync::mpsc;
    use guinea::core::trace::Point;

    h.install::<watching::Watching>(&()).unwrap();
    let (other_end, changes) = mpsc::unbounded_channel();

    let opened = h.act::<watching::Seen>(watching::Watch::of(changes));
    opened.settle();
    assert!(opened.chain().has(|point| matches!(point, Point::Source { .. })));

    other_end.send(1).unwrap();
    other_end.send(2).unwrap();
    h.settled();
    assert_eq!(h.state::<watching::Seen>().0, [1, 2]);
    assert!(
        !opened.chain().handled::<watching::Changed>(),
        "what the watch pushed is not the click's work:\n{:#?}",
        opened.chain()
    );

    drop(other_end);
    h.settled();
    assert!(
        opened.chain().has(|point| matches!(point, Point::Closed { gone: false, .. })),
        "the watch ran dry:\n{:#?}",
        opened.chain()
    );
    assert_eq!(h.stuck(), 0);
}

#[guinea::test(iterations = 20)]
fn leaving_the_page_closes_its_watch(h: &mut Harness) {
    use guinea::core::__private::tokio::sync::mpsc;
    use guinea::core::trace::Point;

    let page = h.child();
    page.install::<watching::Watching>(&()).unwrap();
    let (other_end, changes) = mpsc::unbounded_channel();

    let opened = page.act::<watching::Seen>(watching::Watch::of(changes));
    opened.settle();

    page.leave();
    h.settled();
    assert!(
        opened.chain().has(|point| matches!(point, Point::Closed { gone: true, .. })),
        "the watch outlived its page:\n{:#?}",
        opened.chain()
    );
    assert!(other_end.send(3).is_err(), "the watch let go of its end");
    assert_eq!(h.stuck(), 0);
}

/// A background answer whose handler starts an actor and ends one - what a
/// supervisor does when an agent comes and goes.
mod supervising {
    use super::*;
    use guinea::core::actor::UiThreadToken;

    #[derive(Clone, Debug)]
    pub struct Ping;

    #[derive(Debug)]
    pub struct Agent;

    actor! {
        Agent {
            handlers { Ping }
        }
    }

    #[handler]
    fn ping(_this: &mut Agent, _: Ping) {}

    #[derive(Clone, Debug)]
    pub struct Look;

    #[derive(Clone, Debug)]
    pub struct Found;

    #[derive(Default, Clone, PartialEq, Debug)]
    pub struct Agents(pub u32);

    impl Reducer for Agents {
        type Update = Found;

        fn reduce(&mut self, _found: Found) {
            self.0 += 1;
        }
    }

    #[derive(Debug)]
    pub struct Supervisor {
        pub push: Push<Agents>,
    }

    actor! {
        Supervisor {
            handlers { Look => { bg Found }, Found }
        }
    }

    #[handler]
    fn look(_this: &mut Supervisor, _: Look, cx: Cx) {
        cx.spawn_bg::<Found, _>(async { Found });
    }

    #[handler]
    fn found(this: &mut Supervisor, found: Found) {
        let agent = Addr::new(Agent, UiThreadToken::dangerously_create_token_unchecked());
        agent.send(Ping);
        agent.dispose();

        this.push.send(found);
    }

    feature! {
        pub Supervising {
            exports { Agents }
        }
    }

    #[installs]
    fn supervising(cx: &FeatureInitContext) -> anyhow::Result<Supervising> {
        let (agents, _) = cx.state::<Agents>().driven_by(|push| Supervisor { push });
        Ok(Supervising(agents))
    }
}

#[guinea::test(iterations = 8)]
fn a_background_answer_may_start_and_end_actors(h: &mut Harness) {
    h.install::<supervising::Supervising>(&()).unwrap();

    h.act::<supervising::Agents>(supervising::Look).settle();
    assert_eq!(h.state::<supervising::Agents>().0, 1);
}

#[test]
fn a_seed_is_one_order_every_time() {
    let shown = |seed| {
        let h = Harness::new(seed);
        h.install::<naive::Search>(&()).unwrap();
        type_gu_then_guinea(&h);
        h.state::<Results>().query.clone()
    };

    let first: Vec<String> = (0..64).map(shown).collect();
    let again: Vec<String> = (0..64).map(shown).collect();
    assert_eq!(first, again);

    assert!(
        first.iter().any(|query| query == "gu") && first.iter().any(|query| query == "guinea"),
        "the seeds reach both orders: {first:?}"
    );
}