flo_scene 0.2.0

Entity-messaging system for composing large programs from small programs
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
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
//!
//! This tests the various ways that connections can be established
//!
//! The current design produces a lot of ways this can happen. It's difficult to simplify this without reducing
//! the functionality of the system, though a possible avenue of attack is to combine the source and target filters
//! in a way that supports the existing functionality somehow.
//!
//! There are three types of source ('Any', a subprogram ID and a filtered stream)
//! There are four types of target ('None', 'All', a subprogram ID, and a subprogram ID with a filter attached)
//!
//! The type of stream is specified with a stream ID. Stream IDs are generally untargeted, but subprograms can 
//! request connections to specific subprogram IDs, which uses a stream ID annotated with that target.
//!
//! Here are the general rules:
//!
//! * Connections can be set up before the target subprogram has started (this makes initialization easier, 
//!     especially when talking to the scene control program) 
//! * Connections can be set up after the output sink has been opened (which allows dynamic configuration and can 
//!     also arise out of race conditions)
//! * Source filters can be used to define ways that connections can be rewritten to attach to new targets
//!     (this is useful for messages like 'query' or 'subscribe' where they always need to go through a filter
//!     to be useful)
//! * Target filters can be used to enable a subprogram to accept input types other than its defaults
//!     (this is the more usual type of filtering where we want to be able to adapt the output of one
//!     program to the input of another without the two needing to know about each other)
//! * Filters usually don't combine, but source filters do need to chain with target filters sometimes
//!     (otherwise you have to re-declare the source filters when using a target filter to adapt something)
//! * Connections can use 'Any' as the source to create a default connection without needing to connect every subprogram
//!     (which is just generally useful, but a pain when rewriting existing connections)
//! * Subprograms can create connections to specific subprogram targets, these can be identified and overridden by a 
//!     stream ID with a target if needed
//!     (very uncommon to actually need to do this, except in tests where it's very useful indeed, in the future this
//!     is probably quite useful for 'live patching' software that's running too)
//!
//! (Internally, a connection is either made when sink_for_target() is called in scene_core, or when finish_connecting_programs()
//! is called. The former is for when the connections are made ahead of time, and the latter has to deal with reconnecting
//! existing programs, which is the harder case)
//!

use flo_scene::*;
use flo_scene::programs::*;

use futures::prelude::*;
use serde::*;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)]
struct TestResult(String);
impl SceneMessage for TestResult { }

#[test]
pub fn connect_two_subprograms() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::new();
    let program_1       = SubProgramId::new();
    let program_2       = SubProgramId::new();

    // Program_1 sends a string message
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to send strings to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send("Test".to_string()).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Connect the two programs
    scene.connect_programs(program_1, program_2, StreamId::with_message_type::<String>()).unwrap();

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_two_subprograms_using_filter() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::new();
    let program_1       = SubProgramId::new();
    let program_2       = SubProgramId::new();

    // TestMessage can be filtered into a string, but we don't set the filter up
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage;
    impl SceneMessage for TestMessage { }

    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to TestMessages to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send(TestMessage).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Connect testmessages and strings to program 2, filtering the test messages as we go
    scene.connect_programs((), program_2, StreamId::with_message_type::<String>()).unwrap();
    scene.connect_programs((), StreamTarget::Filtered(test_string_filter, program_2), StreamId::with_message_type::<TestMessage>()).unwrap();

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_two_subprograms_using_filter_then_all_no_delay() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::called("test_program");
    let program_1       = SubProgramId::called("program_1");
    let program_2       = SubProgramId::called("program_2");

    // TestMessage can be filtered into a string, but we don't set the filter up
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage;
    impl SceneMessage for TestMessage { }

    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to TestMessages to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send(TestMessage).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Connect testmessages and strings to program 2, filtering the test messages as we go
    scene.connect_programs((), program_2, StreamId::with_message_type::<String>()).unwrap();
    scene.connect_programs((), program_2, StreamId::with_message_type::<TestMessage>()).unwrap();
    scene.connect_programs((), StreamTarget::Filtered(test_string_filter, program_2), StreamId::with_message_type::<TestMessage>()).unwrap();

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_default_using_source_filter_after_all() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::called("test_program");
    let program_1       = SubProgramId::called("program_1");
    let program_2       = SubProgramId::called("program_2");

    // TestMessage can be filtered into a string, but we don't set the filter up
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage;
    impl SceneMessage for TestMessage { }

    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to TestMessages to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Tell the control program to add the source filter that we need to make the programs talk
            context.send_message(SceneControl::connect(StreamSource::Filtered(test_string_filter), (), StreamId::with_message_type::<TestMessage>())).await.unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send(TestMessage).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Connect testmessages and strings to program 2, filtering the test messages as we go
    scene.connect_programs((), program_2, StreamId::with_message_type::<TestMessage>()).unwrap();
    scene.connect_programs((), program_2, StreamId::with_message_type::<String>()).unwrap();

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_default_using_source_filter_after_all_no_delay() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::called("test_program");
    let program_1       = SubProgramId::called("program_1");
    let program_2       = SubProgramId::called("program_2");

    // TestMessage can be filtered into a string, but we don't set the filter up
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage;
    impl SceneMessage for TestMessage { }

    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to TestMessages to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send(TestMessage).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Connect testmessages and strings to program 2, filtering the test messages as we go
    scene.connect_programs((), program_2, StreamId::with_message_type::<TestMessage>()).unwrap();
    scene.connect_programs((), program_2, StreamId::with_message_type::<String>()).unwrap();
    scene.connect_programs(StreamSource::Filtered(test_string_filter), (), StreamId::with_message_type::<TestMessage>()).unwrap();

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_default_using_chained_filter() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::called("test_program");
    let program_1       = SubProgramId::called("program_1");
    let program_2       = SubProgramId::called("program_2");

    // TestMessage can be filtered into a string, but we don't set the filter up
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage1;
    impl SceneMessage for TestMessage1 { }

    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage2;
    impl SceneMessage for TestMessage2 { }

    let test_message_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage1| TestMessage2));
    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage2| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to TestMessages to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send(TestMessage1).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Connect a filtered input for TestMessage2 to program_2, then add a source filter for TestMessage1 so there are two filters in effect
    scene.connect_programs((), StreamTarget::Filtered(test_string_filter, program_2), StreamId::with_message_type::<TestMessage2>()).unwrap();
    scene.connect_programs((), program_2, StreamId::with_message_type::<String>()).unwrap();
    scene.connect_programs(StreamSource::Filtered(test_message_filter), (), StreamId::with_message_type::<TestMessage1>()).unwrap();

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_default_using_chained_filter_later_1() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::called("test_program");
    let program_1       = SubProgramId::called("program_1");
    let program_2       = SubProgramId::called("program_2");

    // TestMessage can be filtered into a string, but we don't set the filter up
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage1;
    impl SceneMessage for TestMessage1 { }

    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage2;
    impl SceneMessage for TestMessage2 { }

    let test_message_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage1| TestMessage2));
    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage2| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to TestMessages to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Add the target filter later on (this should be able to connect the source filter version of this stream)
            context.send_message(SceneControl::connect((), StreamTarget::Filtered(test_string_filter, program_2), StreamId::with_message_type::<TestMessage2>())).await.unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send(TestMessage1).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Connect a filtered input for TestMessage2 to program_2, then add a source filter for TestMessage1
    scene.connect_programs((), program_2, StreamId::with_message_type::<String>()).unwrap();
    scene.connect_programs(StreamSource::Filtered(test_message_filter), (), StreamId::with_message_type::<TestMessage1>()).unwrap();

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_default_using_chained_filter_later_2() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::called("test_program");
    let program_1       = SubProgramId::called("program_1");
    let program_2       = SubProgramId::called("program_2");

    // TestMessage can be filtered into a string, but we don't set the filter up
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage1;
    impl SceneMessage for TestMessage1 { }

    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage2;
    impl SceneMessage for TestMessage2 { }

    let test_message_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage1| TestMessage2));
    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage2| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to TestMessages to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Add the source filter later on (which should connect via the target filter we added earlier)
            context.send_message(SceneControl::connect(StreamSource::Filtered(test_message_filter), (), StreamId::with_message_type::<TestMessage1>())).await.unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send(TestMessage1).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Connect a filtered input for TestMessage2 to program_2, then add a source filter for TestMessage1
    scene.connect_programs((), StreamTarget::Filtered(test_string_filter, program_2), StreamId::with_message_type::<TestMessage2>()).unwrap();
    scene.connect_programs((), program_2, StreamId::with_message_type::<String>()).unwrap();

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_default_using_chained_filter_later_3() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::called("test_program");
    let program_1       = SubProgramId::called("program_1");
    let program_2       = SubProgramId::called("program_2");

    // TestMessage can be filtered into a string, but we don't set the filter up
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage1;
    impl SceneMessage for TestMessage1 { }

    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage2;
    impl SceneMessage for TestMessage2 { }

    let test_message_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage1| TestMessage2));
    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage2| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to TestMessages to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Add the source and target filters as separate steps
            context.send_message(SceneControl::connect(StreamSource::Filtered(test_message_filter), (), StreamId::with_message_type::<TestMessage1>())).await.unwrap();
            context.send_message(SceneControl::connect((), StreamTarget::Filtered(test_string_filter, program_2), StreamId::with_message_type::<TestMessage2>())).await.unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send(TestMessage1).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Connect a filtered input for TestMessage2 to program_2, then add a source filter for TestMessage1
    scene.connect_programs((), program_2, StreamId::with_message_type::<String>()).unwrap();

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_default_using_filter_added_later() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::called("test_program");
    let program_1       = SubProgramId::called("program_1");
    let program_2       = SubProgramId::called("program_2");

    // TestMessage2 can be filtered into a string, but we don't set the filter up until after the program has started
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage2;
    impl SceneMessage for TestMessage2 { }

    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage2| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to TestMessages to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Add a target filter for program 2 that converts TestMessage2 to a string
            context.send_message(SceneControl::connect((), StreamTarget::Filtered(test_string_filter, program_2), StreamId::with_message_type::<TestMessage2>())).await.unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send(TestMessage2).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Anything that can send a string can send it to program 2
    scene.connect_programs((), program_2, StreamId::with_message_type::<String>()).unwrap();

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_default_using_source_filter_added_later_1() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::called("test_program");
    let program_1       = SubProgramId::called("program_1");
    let program_2       = SubProgramId::called("program_2");

    // TestMessage2 can be filtered into a string, but we don't set the filter up until after we've created the stream
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage2;
    impl SceneMessage for TestMessage2 { }

    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage2| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to TestMessages to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Add a source filter for converting TestMessage2 to strings (so when we send TestMessage2 it shuld be sent to whatever can handle strings)
            context.send_message(SceneControl::connect(StreamSource::Filtered(test_string_filter), (), StreamId::with_message_type::<TestMessage2>())).await.unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send(TestMessage2).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Program 2 can receive strings (program_1 will try to send a TestMessage2 but will also install a filter)
    scene.connect_programs((), program_2, StreamId::with_message_type::<String>()).unwrap();

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_default_using_source_filter_added_later_2() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::called("test_program");
    let program_1       = SubProgramId::called("program_1");
    let program_2       = SubProgramId::called("program_2");

    // TestMessage2 can be filtered into a string. We set up the filter first, but don't create the connection that will use it until later
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage2;
    impl SceneMessage for TestMessage2 { }

    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage2| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to TestMessages to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Set up program 2 to receive strings after we've created the stream (we set up a filter earlier on)
            context.send_message(SceneControl::connect((), program_2, StreamId::with_message_type::<String>())).await.unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send(TestMessage2).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Install a source filter for converting TestMessage2 to strings
    scene.connect_programs(StreamSource::Filtered(test_string_filter), (), StreamId::with_message_type::<TestMessage2>()).unwrap();

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_two_subprograms_using_source_filter() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::new();
    let program_1       = SubProgramId::new();
    let program_2       = SubProgramId::new();

    // TestMessage can be filtered into a string, but we don't set the filter up
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage;
    impl SceneMessage for TestMessage { }

    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to TestMessages to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send(TestMessage).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Connect program 1 and 2, but first set up a source filter so that TestMessages are translated
    scene.connect_programs(StreamSource::Filtered(test_string_filter), (), StreamId::with_message_type::<TestMessage>()).unwrap();
    scene.connect_programs(program_1, program_2, StreamId::with_message_type::<TestMessage>()).unwrap();

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_two_subprograms_using_source_filter_later() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::new();
    let program_1       = SubProgramId::new();
    let program_2       = SubProgramId::new();

    // TestMessage can be filtered into a string, but we don't set the filter up
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage;
    impl SceneMessage for TestMessage { }

    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to TestMessages to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send(TestMessage).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Connect program 1 to program 2, then say that we can filter TestMessages
    scene.connect_programs(program_1, program_2, StreamId::with_message_type::<TestMessage>()).unwrap();
    scene.connect_programs(StreamSource::Filtered(test_string_filter), (), StreamId::with_message_type::<TestMessage>()).unwrap();

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_two_subprograms_using_string_type_then_source_filter() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::new();
    let program_1       = SubProgramId::new();
    let program_2       = SubProgramId::new();

    // TestMessage can be filtered into a string, but we don't set the filter up
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage;
    impl SceneMessage for TestMessage { }

    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to TestMessages to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send("Test".to_string()).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Connect program 1 and 2 as strings, then set up a source filter so that string messages can be converted from TestMessages (so our message can be sent)
    // This is the same as in `connect_two_subprograms_using_source_filter_later` except we use the input type of the target to make the connection instead of the output type
    scene.connect_programs(program_1, program_2, StreamId::with_message_type::<String>()).unwrap();
    scene.connect_programs(StreamSource::Filtered(test_string_filter), (), StreamId::with_message_type::<TestMessage>()).unwrap();

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_two_subprograms_after_creating_stream() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::new();
    let program_1       = SubProgramId::new();
    let program_2       = SubProgramId::new();

    // Program_1 sends a string message
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to send strings to the default target (will be created disconnected)
            let mut send_strings = context.send(()).unwrap();

            // Tell the control program to connect program1 to program2
            context.send_message(SceneControl::connect(program_1, program_2, StreamId::with_message_type::<String>())).await.unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send("Test".to_string()).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_default_subprogram_after_creating_stream() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::new();
    let program_1       = SubProgramId::new();
    let program_2       = SubProgramId::new();

    // Program_1 sends a string message
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to send strings to the default target
            let mut send_strings = context.send(()).unwrap();

            // Tell the control program to connect all strings to the target
            context.send_message(SceneControl::connect((), program_2, StreamId::with_message_type::<String>())).await.unwrap();

            // Send the string to the control programs
            send_strings.send("Test".to_string()).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_two_subprograms_before_creating() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::new();
    let program_1       = SubProgramId::new();
    let program_2       = SubProgramId::new();

    // Create the connection between the two programs, before the programs are started
    scene.connect_programs(program_1, program_2, StreamId::with_message_type::<String>()).unwrap();

    // Program_1 sends a string message
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            context.send_message("Test".to_string()).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_default_subprograms_before_creating() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::new();
    let program_1       = SubProgramId::new();
    let program_2       = SubProgramId::new();

    // Specify that any strings get sent to program 2, before the scene is started
    scene.connect_programs((), program_2, StreamId::with_message_type::<String>()).unwrap();

    // Program_1 sends a string message
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            context.send_message("Test".to_string()).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_default_subprograms_before_launching() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::new();
    let program_1       = SubProgramId::new();
    let program_2       = SubProgramId::new();

    // Specify that any strings get sent to program 2, before the scene is started
    scene.connect_programs((), program_2, StreamId::with_message_type::<String>()).unwrap();

    // Program_1 sends a string message
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Connect to the default target for sending strings. It's configured as program_2 but that's not running yet
            let mut send_strings = context.send(()).unwrap();

            context.wait_for_idle(100).await;

            // Start the receiver program after we've started sending the strings
            context.send_message(SceneControl::start_program(program_2, move |input: InputStream<String>, context| {
                async move {
                    let mut test_program = context.send(test_program).unwrap();

                    let mut input = input;
                    while let Some(input) = input.next().await {
                        test_program.send(TestResult(input)).await.unwrap();
                    }
                }
            }, 0)).await.unwrap();

            send_strings.send("Test".to_string()).await.unwrap();
        }
    }, 0);

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_two_subprograms_after_creating_stream_using_filter_target() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::new();
    let program_1       = SubProgramId::new();
    let program_2       = SubProgramId::new();

    // TestMessage can be filtered into a string, but we don't set the filter up
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage;
    impl SceneMessage for TestMessage { }

    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to send TestMessages to program_2 (but it accepts strings)
            let mut send_strings = context.send(program_2).unwrap();

            // Tell the control program to filter the connection between program 1 and 2
            context.send_message(SceneControl::connect(program_1, StreamTarget::Filtered(test_string_filter, program_2), StreamId::with_message_type::<TestMessage>())).await.unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send(TestMessage).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[test]
pub fn connect_default_after_creating_stream_using_filter_target() {
    // Scene with two programs that we'll connect together
    let scene           = Scene::default();
    let test_program    = SubProgramId::new();
    let program_1       = SubProgramId::new();
    let program_2       = SubProgramId::new();

    // TestMessage can be filtered into a string, but we don't set the filter up
    #[derive(Debug, Serialize, Deserialize)]
    struct TestMessage;
    impl SceneMessage for TestMessage { }

    let test_string_filter = FilterHandle::for_filter(|messages| messages.map(|_: TestMessage| "Test".to_string()));

    // Program_1 sends a TestMessage
    scene.add_subprogram(program_1, |_: InputStream<()>, context| {
        async move {
            // Create a stream to send TestMessages to program_2 (but it accepts strings)
            let mut send_strings = context.send(()).unwrap();

            // Tell the control program to filter the connection to program_2
            context.send_message(SceneControl::connect((), StreamTarget::Filtered(test_string_filter, program_2), StreamId::with_message_type::<TestMessage>())).await.unwrap();

            // Send the string to the control programs (sometimes the control program will make the connection first, sometimes it'll happen after we start to send)
            send_strings.send(TestMessage).await.unwrap();
        }
    }, 0);

    // Program 2 receives the message and sends it to the test program
    scene.add_subprogram(program_2, move |input: InputStream<String>, context| {
        async move {
            let mut test_program = context.send(test_program).unwrap();

            let mut input = input;
            while let Some(input) = input.next().await {
                test_program.send(TestResult(input)).await.unwrap();
            }
        }
    }, 0);

    // Check that we receive the test message
    TestBuilder::new()
        .expect_message(|_msg: TestResult| { Ok(()) })
        .run_in_scene(&scene, test_program);
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct SimpleTestMessage {
    value: String,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct SimpleResponseMessage {
    value: String,
}

impl SceneMessage for SimpleTestMessage {
    fn message_type_name() -> String {
        "flo_scene_tests::guest_subprogram_tests::SimpleTestMessage".into()
    }
}

impl SceneMessage for SimpleResponseMessage {
    fn message_type_name() -> String {
        "flo_scene_tests::guest_subprogram_tests::SimpleResponseMessage".into()
    }
}

// These seem to be failing because the test uses a filter target mechanism and you can't connect a specific stream to a generic filter target
// (Ie, source=Any, target=Filter does not add a filter for the source=specific, target=specific case). It's annoyingly complicated to fix this
// though the source filter is an option here.
//
// (We start the stream as disconnected in anticipation of a filter being added later on, but in a sense there already is a filter)

#[test]
fn connect_single_source_to_single_target() {
    let scene = Scene::default();

    let guest_subprogram_id     = SubProgramId::called("Guest subprogram");
    let sender_subprogram_id    = SubProgramId::called("Sender subprogram");
    let test_subprogram_id      = SubProgramId::called("Test subprogram");

    // Run a program that relays any messages it receives to the default output
    scene.add_subprogram(guest_subprogram_id, move |input_stream: InputStream<SimpleTestMessage>, context| async move {
        // Send responses to the defualt target for the scene
        let mut response = context.send::<SimpleResponseMessage>(()).unwrap();

        let mut input_stream = input_stream;
        while let Some(msg) = input_stream.next().await {
            println!("Received message: {:?}", msg);

            response.send(SimpleResponseMessage { value: msg.value }).await.unwrap();

            println!("Sent message");
        }
    }, 10);

    // Run another program to send messages to the first one
    scene.add_subprogram(sender_subprogram_id, move |_input: InputStream<()>, context| async move {
        let mut test_messages = context.send(guest_subprogram_id).unwrap();

        test_messages.send(SimpleTestMessage { value: "Hello".into() }).await.unwrap();
        test_messages.send(SimpleTestMessage { value: "Goodbyte".into() }).await.unwrap();
    }, 0);

    // Set the default output of just the program we created to the test program (but not the default for every message of this type)
    scene.connect_programs(guest_subprogram_id, test_subprogram_id, StreamId::with_message_type::<SimpleResponseMessage>()).unwrap();

    TestBuilder::new()
        .expect_message(|_: SimpleResponseMessage| { Ok(()) })
        .expect_message(|_: SimpleResponseMessage| { Ok(()) })
        .run_in_scene(&scene, test_subprogram_id);
}


#[test]
fn connect_single_source_to_single_target_before_creation() {
    let scene = Scene::default();

    let guest_subprogram_id     = SubProgramId::called("Guest subprogram");
    let sender_subprogram_id    = SubProgramId::called("Sender subprogram");
    let test_subprogram_id      = SubProgramId::called("Test subprogram");

    // Set the default output of just the program we created to the test program (but not the default for every message of this type)
    scene.connect_programs(guest_subprogram_id, test_subprogram_id, StreamId::with_message_type::<SimpleResponseMessage>()).unwrap();

    // Run a program that relays any messages it receives to the default output
    scene.add_subprogram(guest_subprogram_id, move |input_stream: InputStream<SimpleTestMessage>, context| async move {
        // Send responses to the defualt target for the scene
        let mut response = context.send::<SimpleResponseMessage>(()).unwrap();

        let mut input_stream = input_stream;
        while let Some(msg) = input_stream.next().await {
            println!("Received message: {:?}", msg);

            response.send(SimpleResponseMessage { value: msg.value }).await.unwrap();

            println!("Sent message");
        }
    }, 10);

    // Run another program to send messages to the first one
    scene.add_subprogram(sender_subprogram_id, move |_input: InputStream<()>, context| async move {
        let mut test_messages = context.send(guest_subprogram_id).unwrap();

        test_messages.send(SimpleTestMessage { value: "Hello".into() }).await.unwrap();
        test_messages.send(SimpleTestMessage { value: "Goodbyte".into() }).await.unwrap();
    }, 0);

    TestBuilder::new()
        .expect_message(|_: SimpleResponseMessage| { Ok(()) })
        .expect_message(|_: SimpleResponseMessage| { Ok(()) })
        .run_in_scene(&scene, test_subprogram_id);
}

// TODO: both these tests set up the connection before the connection is made, we also need to test making the connection later on
// TODO: `connect_single_source_to_single_target` but with source and target filters