pinch-points 0.9.0

A fast, kid-friendly crab-routing game: route streams of crabs into your sandcastle before time runs out
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
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
//! The versus/puzzle session driver: board boot and teardown, the level
//! reload path, the fixed-tick sim driver, and the round-outcome checks.
//! Split from the app's `mod.rs` so that keeps only types and wiring: a
//! versus round's life, a puzzle run's, and the tick they share are each
//! a file here, with what both draw from beside them.

use super::*;

mod puzzle;
mod tick;
mod versus;

pub(super) use puzzle::*;
pub(crate) use tick::*;
pub(super) use versus::*;

/// Where a seat's cursor starts a round: fanned out near its own castle,
/// two tiles in from the corner so the first press is not into a wall.
///
/// Clamped to the board, not to the two-tile inset: a custom arena can be
/// as small as the level format allows, and asking `clamp` for an inset
/// the board cannot hold took a 3x3 beach down on its first frame. On a
/// board too small for the inset the cursor sits as far in as there is.
fn cursor_home(board: &Board, player: u8) -> (u8, u8) {
    let (w, h) = (board.width(), board.height());
    // The board is asked first, and the spot table is only the fallback:
    // which seat owns which castle is drawn with the beach (see
    // `seat_spots`), and a handmade beach was never in the table at all.
    let spots = castle_spots(w, h);
    let (cx, cy) = board
        .castle_of(player)
        .unwrap_or_else(|| spots[usize::from(player).min(spots.len() - 1)]);
    let inset = |len: u8| 2.min(len.saturating_sub(1) / 2);
    (
        cx.clamp(inset(w), w - 1 - inset(w)),
        cy.clamp(inset(h), h - 1 - inset(h)),
    )
}

/// The board in play, the actions queued for its next tick, and whether
/// it is held: what loading a round resets and ticking one advances.
#[derive(bevy::ecs::system::SystemParam)]
pub(super) struct Play<'w> {
    sim: ResMut<'w, Sim>,
    pending: ResMut<'w, PendingActions>,
    paused: ResMut<'w, Paused>,
}

/// What laying a fresh board out takes: the commands and art to draw its
/// statics with, and the cursors to set down on it. Both ways into a board
/// (a versus round, a puzzle level) draw it the same way and home the
/// cursors their own way.
#[derive(bevy::ecs::system::SystemParam)]
pub(super) struct BoardStage<'w, 's> {
    commands: Commands<'w, 's>,
    art: Res<'w, art::Art>,
    cursors: Query<'w, 's, (&'static mut cursor::Cursor, &'static mut Transform)>,
    generation: ResMut<'w, sim_events::BoardGeneration>,
}

impl BoardStage<'_, '_> {
    /// Draw everything about `board` that does not move: the beach, the
    /// sand and the walls. And count it a new board, which a board laid
    /// out afresh is whatever its clock says.
    fn lay_out(&mut self, board: &Board) {
        self.generation.laid_out(board);
        board_render::spawn_static_board(&mut self.commands, board, &self.art);
    }
}

/// Every category of entity rendered from board state, bundled so the two
/// teardown paths (screen exit and level reload) cannot drift apart: a
/// missed category left turnstile sprites probing a smaller board and
/// panicking in `tile_at`.
#[derive(bevy::ecs::system::SystemParam)]
pub(super) struct BoardSprites<'w, 's> {
    statics: Query<'w, 's, Entity, With<board_render::BoardStatic>>,
    posts: Query<'w, 's, Entity, With<board_render::SignpostSprite>>,
    castles: Query<'w, 's, Entity, With<board_render::CastleSprite>>,
    logs: Query<'w, 's, Entity, With<board_render::TurnstileSprite>>,
    crabs: Query<'w, 's, Entity, With<creatures::CrabSprite>>,
    gulls: Query<'w, 's, Entity, With<creatures::GullSprite>>,
    /// Gulls flying in to the old beach, on their way to tiles it had.
    arrivals: Query<'w, 's, Entity, With<creatures::GullArrival>>,
    /// Crab Monopoly's crabs in the air. Only their own system takes them
    /// down otherwise, and it runs on the board screens alone: a round
    /// quit inside a sweep left them glowing over the menu.
    sweeps: Query<'w, 's, Entity, With<creatures::SweepSprite>>,
    /// The old beach's dust, splashes, footprints and crabs hopping into
    /// its castles: drawn where things stood on it, and on a reload or
    /// an editor swap on the same screen they went on over the new one.
    particles: Query<'w, 's, Entity, With<effects::Particle>>,
    hops: Query<'w, 's, Entity, With<effects::Hop>>,
}

impl BoardSprites<'_, '_> {
    pub(super) fn despawn_all(&self, commands: &mut Commands) {
        for entity in self
            .statics
            .iter()
            .chain(self.posts.iter())
            .chain(self.castles.iter())
            .chain(self.logs.iter())
            .chain(self.crabs.iter())
            .chain(self.gulls.iter())
            .chain(self.arrivals.iter())
            .chain(self.sweeps.iter())
            .chain(self.particles.iter())
            .chain(self.hops.iter())
        {
            commands.entity(entity).despawn();
        }
    }
}

/// Despawn everything rendered from board state, in every category.
pub(super) fn despawn_board_sprites(mut commands: Commands, sprites: BoardSprites) {
    sprites.despawn_all(&mut commands);
}

/// Who decides each seat of an online round on this peer, as the round
/// reads it.
#[cfg(test)]
pub(crate) fn online_controllers_for_tests(session: &net::OnlineSession) -> Controllers {
    online_controllers(session)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sim::{BotLevel, Level, Replay};
    use bevy::prelude::*;

    /// A bystander the teardown must not touch.
    #[derive(Component)]
    struct Marker;

    /// The smallest world `advance_sim` will run in: a board, the
    /// resources it reads and writes, and nothing else.
    fn sim_app() -> App {
        let mut app = App::new();
        app.insert_resource(Sim(classic_arena(false, 2)));
        app.init_resource::<PendingActions>();
        app.init_resource::<Paused>();
        app.init_resource::<net::Online>();
        app.init_resource::<Recorder>();
        app.init_resource::<Playback>();
        app.init_resource::<replays::PlaybackSpeed>();
        app.init_resource::<Controllers>();
        app.init_resource::<bot_seats::BotSeats>();
        app.init_resource::<awards::RoundTally>();
        app.add_systems(Update, advance_sim);
        app
    }

    /// A couch match set up here opens on the line-up, every round of it;
    /// the daily, a dev hook's table and a resumed round do not.
    #[test]
    fn a_couch_match_opens_on_the_lineup() {
        use super::versus::RoundOrigin;
        let config = crate::app::match_setup::MatchConfig::default();
        assert!(RoundOrigin::Configured(&config).opens_on_lineup(true));
        assert!(
            !RoundOrigin::Configured(&config).opens_on_lineup(false),
            "the daily"
        );
        assert!(!RoundOrigin::Unconfigured.opens_on_lineup(true));
    }

    /// What a finished round is filed under. Nothing else tests this, and
    /// it names every file in the replay library: a draw and a team win
    /// both have to answer, and a seat that was renamed keeps its name.
    #[test]
    fn a_kept_round_is_filed_under_whoever_took_it() {
        let mut board = Board::new(5, 5, 0);
        board.set_tile(0, 0, crate::sim::TileKind::Castle(0));
        board.set_tile(4, 4, crate::sim::TileKind::Castle(1));
        let seats = Seats(2);
        let online = net::Online::default();
        let named = |scores: [u32; MAX_PLAYERS], settings: &settings::GameSettings| {
            let mut board = board.clone();
            for (seat, score) in scores.iter().enumerate() {
                board.set_score(seat as u8, *score);
            }
            // Couch rounds resolve seat names from the settings table.
            let names = SeatNames(settings.names.clone());
            let mode = teams::in_play(settings.team_mode, &online, None, seats.0);
            winner_name(&Sim(board), &seats, mode, &names, settings.tr())
        };

        let plain = settings::GameSettings::default();
        assert_eq!(named([0, 7, 0, 0, 0, 0], &plain).as_deref(), Some("P2"));
        // Level scores are nobody's round, and the file has to say so
        // rather than crediting the lowest seat.
        assert_eq!(named([7, 7, 0, 0, 0, 0], &plain), None);
        assert_eq!(named([0, 0, 0, 0, 0, 0], &plain), None);

        // A renamed seat is filed under its name, since that is what the
        // player will look for on the shelf.
        let mut settings = settings::GameSettings::default();
        settings.names[1] = "Bo".to_string();
        assert_eq!(named([0, 7, 0, 0, 0, 0], &settings).as_deref(), Some("Bo"));

        // A round won in pairs is filed under the pair, as the card names
        // it, and a replay of one under the mode it was played in, not
        // the setting it is watched with.
        let four = Seats(4);
        let mut board = Board::new(5, 5, 0);
        for seat in 0..4 {
            board.set_tile(seat, 0, crate::sim::TileKind::Castle(seat));
            board.set_score(seat, [5, 5, 4, 5][usize::from(seat)]);
        }
        let pairs = settings::GameSettings {
            team_mode: teams::TeamMode::Pairs,
            ..settings::GameSettings::default()
        };
        let names = SeatNames(pairs.names.clone());
        let live_mode = teams::in_play(pairs.team_mode, &online, None, four.0);
        let live = winner_name(&Sim(board.clone()), &four, live_mode, &names, pairs.tr());
        assert_eq!(live.as_deref(), Some("P1+P2"));
        let mut replay = Replay::new(Level::from_board("T", 3, board.clone()));
        replay.teams = teams::TeamMode::Pairs.index() as u8;
        let watched_mode = teams::in_play(plain.team_mode, &online, Some(&replay), four.0);
        let watched = winner_name(&Sim(board), &four, watched_mode, &names, plain.tr());
        assert_eq!(watched.as_deref(), Some("P1+P2"));
    }

    fn armed(seats: u8, bots: u8) -> match_setup::MatchConfig {
        match_setup::MatchConfig {
            armed: true,
            ..match_setup::MatchConfig::with_ai(seats, bots)
        }
    }

    /// A round launched from the setup steps is played on the board its
    /// map card showed: the board the card's picture is drawn from (the
    /// card's config, the setup's seed) against the one the round builds
    /// from the config the launch leaves. With no seed kept (a series'
    /// next round, a rematch) every round draws its own.
    #[test]
    fn a_launched_round_is_played_on_the_previewed_seed() {
        use match_setup::{Card, MapChoice};
        let beaches = match_setup::CustomBeaches::default();
        let setup = armed(4, 2);
        let card = Card::Map(MapChoice::GenLarge);
        let shown = match_setup::card_config(&setup, card, &beaches).expect("a card");
        let pictured = match_setup::preview_board(&shown, &beaches, 31337);
        let mut launched = setup.clone();
        match_setup::pick_card(&mut launched, card, &beaches);
        match_setup::seat_the_match(&mut launched, &[], 31337, &beaches, 0);
        let mut board = RoundOrigin::Configured(&launched).board(None, &beaches, false, 0);
        assert_eq!(board.seed(), 31337);
        for _ in 0..match_setup::PREVIEW_TICKS {
            board.tick_idle();
        }
        assert_eq!(board.state_hash(), pictured.state_hash());
        let fresh = match_setup::MatchConfig {
            seed: None,
            ..launched
        };
        let board = RoundOrigin::Configured(&fresh).board(None, &beaches, false, 0);
        assert_ne!(board.seed(), 31337);
    }

    /// Step 3 puts the AI in any seat, not only the top ones: the round
    /// seats it where step 3 did, and a cursor on every other seat.
    #[test]
    fn the_ai_sits_where_step_three_put_it() {
        let mut config = armed(4, 0);
        config.controllers[1] = SeatController::Ai(BotLevel::Easy);
        let table = bot_seats(&config);
        assert_eq!(table.0[1], SeatController::Ai(BotLevel::Easy));
        assert_eq!(table.0[3], SeatController::Local);
        assert_eq!(config.human_seats(), vec![0, 2, 3]);
    }

    /// The open ocean has no edges on the couch either. The local launch
    /// built its own board and never opened them, so a couch round on it
    /// was played walled while the same map online wrapped.
    #[test]
    fn a_couch_round_on_the_open_ocean_wraps() {
        let beaches = match_setup::CustomBeaches::default();
        for (map, wraps) in [
            (match_setup::MapChoice::GenOcean, true),
            (match_setup::MapChoice::GenLarge, false),
            (match_setup::MapChoice::Classic, false),
        ] {
            let config = match_setup::MatchConfig { map, ..armed(2, 0) };
            let board = RoundOrigin::Configured(&config).board(None, &beaches, false, 0);
            assert_eq!(board.wrap(), wraps, "{map:?}");
        }
    }

    /// The daily's table is one human and three fierce rivals, built from
    /// its own config rather than the player's.
    #[test]
    fn the_daily_seats_one_human_against_three_hard_bots() {
        let daily = match_setup::MatchConfig::daily();
        assert!(daily.armed, "ready to launch as it is");
        let table = bot_seats(&daily);
        assert_eq!(table.0[0], SeatController::Local, "the player's chair");
        assert_eq!(
            &table.0[1..4],
            &[SeatController::Ai(BotLevel::Hard); 3],
            "three fierce rivals"
        );
        assert!(
            table.0[4..].iter().all(|c| *c == SeatController::Local),
            "and no more chairs"
        );
        let player = match_setup::MatchConfig::default();
        assert_ne!(player.seats, daily.seats, "the player's own is untouched");
    }

    /// A seat a bot drives online is a bot's, not this machine's person's:
    /// joined as a bot (route 1) or come straight to the host (route 2),
    /// it earns no trophies and is not tagged "(you)", both of which key
    /// on `Local`.
    #[test]
    fn a_seat_a_bot_drives_online_is_not_the_players() {
        let session = |seat: u8, bot: bool, bots_here: Vec<u8>| {
            let mut session = net::OnlineSession::new(
                crate::transport::UdpTransport::host(0).expect("socket"),
                crate::sim::Lockstep::new(seat, vec![0, 1, 2], crate::sim::DEFAULT_DELAY),
                3,
                crate::transport::MatchTerms::default(),
            );
            session.bot = bot.then(|| "Greedy (Ana)".to_string());
            session.bots_here = bots_here
                .into_iter()
                .map(|seat| net::HostedBot {
                    seat,
                    id: 0,
                    name: "Greedy".to_string(),
                })
                .collect();
            session
        };
        // A person at seat one: their own chair, the others down the wire.
        let table = online_controllers(&session(1, false, vec![]));
        assert_eq!(table.0[1], SeatController::Local);
        assert_eq!(table.0[0], SeatController::Remote);
        // Route 1: this peer's own seat is its bot's.
        let table = online_controllers(&session(1, true, vec![]));
        assert_eq!(
            table.0[1],
            SeatController::Bot,
            "the bot's, not the person's"
        );
        assert_eq!(table.0[0], SeatController::Remote);
        // Route 2: the host plays seat zero, its bot seat two.
        let table = online_controllers(&session(0, false, vec![2]));
        assert_eq!(table.0[0], SeatController::Local);
        assert_eq!(table.0[2], SeatController::Bot);
        assert_eq!(table.0[1], SeatController::Remote);
    }

    /// Boards from the shelf or a pasted code can be any size at all, and
    /// the cursor's opening spot has to be on every one of them: near the
    /// castle on a real arena, and simply on the board when there is no
    /// room for the two-tile inset.
    #[test]
    fn a_cursor_opens_on_the_board_whatever_its_size() {
        for (w, h) in [(1, 1), (2, 1), (3, 3), (4, 5), (5, 5), (12, 9), (20, 13)] {
            let board = Board::new(w, h, 0);
            for player in 0..MAX_PLAYERS as u8 {
                let (x, y) = cursor_home(&board, player);
                assert!(
                    x < w && y < h,
                    "seat {player} opened at ({x},{y}) on a {w}x{h} board"
                );
            }
        }
        let big = Board::new(12, 9, 0);
        assert_eq!(
            cursor_home(&big, 0),
            (2, 2),
            "two in from the host's corner"
        );
        assert_eq!(cursor_home(&big, 1), (9, 6), "and from the far one");
    }

    #[test]
    fn the_ai_takes_the_top_seats() {
        let mut config = armed(4, 2);
        config.controllers[3] = SeatController::Ai(BotLevel::Hard);
        config.controllers[2] = SeatController::Ai(BotLevel::Easy);
        assert_eq!(bot_seats(&config).levels(), {
            let mut want = [None; MAX_PLAYERS];
            want[2] = Some(BotLevel::Easy);
            want[3] = Some(BotLevel::Hard);
            want
        });
        // An unarmed config is a dev hook or a replay: nobody is botted.
        let mut idle = armed(4, 3);
        idle.armed = false;
        assert_eq!(bot_seats(&idle), Controllers::default());
    }

    /// A round copied with a bot in it keeps that seat off the keyboard:
    /// pasted back, the bot is not there, so the AI that stands in for a
    /// bot holds it, rather than a person's keys or nobody at all.
    #[test]
    fn a_bots_seat_is_saved_as_the_ai_that_stands_in_for_it() {
        let mut config = armed(3, 2);
        config.controllers[2] = SeatController::Bot;
        config.controllers[1] = SeatController::Ai(BotLevel::Hard);
        let table = bot_seats(&config);
        assert_eq!(table.0[2], SeatController::Bot);
        let saved = table.levels();
        assert_eq!(saved[0], None, "the person stays a person");
        assert_eq!(saved[1], Some(BotLevel::Hard));
        assert_eq!(
            saved[2],
            Some(bot_seats::STAND_IN),
            "the bot's seat is the stand-in AI's"
        );
        let resumed = Controllers::from_levels(saved);
        assert_eq!(resumed.0[2], SeatController::Ai(bot_seats::STAND_IN));
    }

    /// A replay's board with castles for `seats` seats.
    fn recorded(seats: u8) -> Replay {
        Replay::new(Level::from_board(
            "Turf War",
            3,
            classic_arena(false, seats),
        ))
    }

    fn online_at(seats: u8) -> net::OnlineSession {
        net::OnlineSession::new(
            crate::transport::UdpTransport::host(0).expect("socket"),
            crate::sim::Lockstep::new(0, vec![0, 1], crate::sim::DEFAULT_DELAY),
            seats,
            crate::transport::MatchTerms::default(),
        )
    }

    /// Each way into a round has its own authority on the seat count.
    #[test]
    fn every_entry_path_knows_its_own_seat_count() {
        let config = armed(3, 1);
        // A replay: the highest castle owner on the recorded board.
        let replay = recorded(4);
        let board = replay.level.board();
        assert_eq!(RoundOrigin::Replay(&replay).table(&config, &board, 0).1, 4);
        let bare = Board::new(4, 4, 1);
        assert_eq!(
            RoundOrigin::Replay(&replay).table(&config, &bare, 0).1,
            2,
            "a castle-less recording still seats two"
        );
        // Online: the lobby agreed the count, whatever the local config says.
        let session = online_at(2);
        assert_eq!(RoundOrigin::Online(&session).table(&config, &bare, 0).1, 2);
        // A configured match is told.
        assert_eq!(
            RoundOrigin::Configured(&config).table(&config, &bare, 0).1,
            3
        );
        // A dev hook: the keyboard's two seats, plus a pad each beyond that.
        assert_eq!(RoundOrigin::Unconfigured.table(&config, &bare, 0).1, 2);
        assert_eq!(RoundOrigin::Unconfigured.table(&config, &bare, 3).1, 3);
    }

    /// The daily's beach is the one for the day handed in, which is the day
    /// `load_versus` keeps for the trophies, and not whatever the clock says
    /// by the time anyone asks again.
    #[test]
    fn the_daily_is_built_from_the_day_it_is_handed() {
        let daily = match_setup::MatchConfig::daily();
        let built = |day| {
            RoundOrigin::Configured(&daily)
                .board(Some(day), &Default::default(), false, 0)
                .state_hash()
        };
        assert_eq!(built(20_000), built(20_000), "one beach for everybody");
        assert_ne!(built(20_000), built(20_001), "and a fresh one tomorrow");
    }

    /// The arrows dial on the setup card is the rule the round is played
    /// under, on every kind of beach it builds, and an online round keeps
    /// the versus three whatever this machine's card says.
    #[test]
    fn the_arrows_dial_is_the_rule_the_round_plays() {
        use crate::app::match_setup::MapChoice;
        for map in [MapChoice::Classic, MapChoice::GenLarge] {
            for posts in match_setup::POSTS_RANGE {
                let config = match_setup::MatchConfig {
                    map,
                    posts,
                    ..armed(2, 1)
                };
                let board =
                    RoundOrigin::Configured(&config).board(None, &Default::default(), false, 0);
                assert_eq!(
                    board.signpost_rule(),
                    (posts, crate::sim::CapPolicy::Evict),
                    "{map:?}"
                );
            }
        }
        let board = RoundOrigin::Online(&online_at(2)).board(None, &Default::default(), false, 0);
        assert_eq!(
            board.signpost_rule().0,
            crate::sim::MAX_SIGNPOSTS_PER_PLAYER as u8
        );
    }

    /// A table of five on a four-castle beach, however it got there, is
    /// played on a beach with a castle for every seat.
    #[test]
    fn a_table_too_big_for_its_beach_still_gets_a_castle_each() {
        use crate::app::match_setup::MapChoice;
        for map in [
            MapChoice::Classic,
            MapChoice::GenSmall,
            MapChoice::GenClassic,
        ] {
            for seats in [5, 6] {
                let config = match_setup::MatchConfig {
                    map,
                    ..armed(seats, 0)
                };
                let board =
                    RoundOrigin::Configured(&config).board(None, &Default::default(), false, 0);
                let mut owners: Vec<u8> = board.castle_owners().collect();
                owners.sort_unstable();
                owners.dedup();
                assert_eq!(owners.len(), usize::from(seats), "{seats} on {map:?}");
            }
        }
    }

    /// Two of the sources are outside the game's control, and the count
    /// they give becomes the length of every per-seat loop: a seventh seat
    /// runs off the end of the `MAX_PLAYERS`-long scores in
    /// `leading_seats`.
    #[test]
    fn a_seat_count_never_leaves_the_table() {
        let config = armed(3, 1);
        let seated = MAX_PLAYERS as u8;
        let bare = Board::new(4, 4, 1);
        // A host can put any byte in a `Start`.
        assert_eq!(
            RoundOrigin::Online(&online_at(200))
                .table(&config, &bare, 0)
                .1,
            seated
        );
        assert_eq!(
            RoundOrigin::Online(&online_at(0))
                .table(&config, &bare, 0)
                .1,
            2
        );
        // And a player can plug in more gamepads than there are chairs.
        assert_eq!(RoundOrigin::Unconfigured.table(&config, &bare, 9).1, seated);
        // Whatever comes back is a count the per-seat arrays can hold.
        for asked in [0, 1, 3, 6, 7, 200, 255] {
            assert!((2..=seated).contains(&clamp_seats(asked)), "{asked}");
        }
    }

    /// Only a round played from its first tick is recorded: a replay is a
    /// recording already, and one resumed or caught up with mid-round has
    /// no first tick to record from.
    #[test]
    fn only_a_round_played_from_its_first_tick_is_recorded() {
        let config = armed(3, 1);
        let replay = recorded(2);
        let mut caught_up = online_at(2);
        assert!(RoundOrigin::Configured(&config).recorded());
        assert!(RoundOrigin::Unconfigured.recorded());
        assert!(RoundOrigin::Online(&caught_up).recorded());
        assert!(!RoundOrigin::Replay(&replay).recorded());
        caught_up.catch_up.board = Some(Board::new(4, 4, 1));
        assert!(!RoundOrigin::Online(&caught_up).recorded());
        let resumed = suspend::Suspended {
            seats: 3,
            bots: [None; MAX_PLAYERS],
            board: Board::new(4, 4, 1),
        };
        let origin = RoundOrigin::Resumed(Box::new(resumed));
        assert!(!origin.recorded());
        assert_eq!(
            origin.table(&config, &Board::new(4, 4, 1), 0).1,
            3,
            "its own table"
        );
    }

    /// The AI fill is why an online AI seat is safe at all: two peers holding
    /// the same board must produce the same moves for it, frame after frame,
    /// while the humans' moves ride over the wire untouched.
    #[test]
    fn the_ai_fill_is_identical_on_every_peer() {
        use crate::sim::{Direction, classic_arena_seeded};

        // Two peers, each with its own copy of the same beach.
        let mut here = classic_arena_seeded(0x51DE, false, 4);
        let mut there = classic_arena_seeded(0x51DE, false, 4);
        // Two humans in the low seats, two AI behind them: a 2v2 online match.
        let mut bots = Controllers::default();
        bots.0[2] = SeatController::Ai(BotLevel::Normal);
        bots.0[3] = SeatController::Ai(BotLevel::Hard);

        let pressed = PlayerAction::Place {
            x: 4,
            y: 4,
            dir: Direction::Up,
        };
        for frame in 0..600 {
            // The wire delivers the humans' actions to both peers alike.
            let mut wire = [PlayerAction::None; MAX_PLAYERS];
            if frame == 30 {
                wire[1] = pressed;
            }
            let (mut mine, mut theirs) = (wire, wire);
            fill_bot_actions(&here, &bots, &mut mine);
            fill_bot_actions(&there, &bots, &mut theirs);
            assert_eq!(mine, theirs, "peers disagreed on an AI seat at {frame}");
            assert_eq!(mine[0], PlayerAction::None, "seat 1 has no AI to fill");
            if frame == 30 {
                assert_eq!(mine[1], pressed, "a human's press was overwritten");
            }
            here.tick(&mine);
            there.tick(&theirs);
            assert_eq!(
                here.state_hash(),
                there.state_hash(),
                "the boards diverged at frame {frame}"
            );
        }
        // And the AI seats actually did something with those 600 frames.
        assert!(
            here.scores()[2] > 0 || here.scores()[3] > 0,
            "no AI seat banked anything, so the fill proved nothing"
        );
    }

    /// The teardown has to sweep *every* category of board sprite.
    ///
    /// A category left behind meant turnstile sprites surviving onto a
    /// smaller board and panicking in `tile_at`: a crash at level load,
    /// from a sprite nobody was looking at. The two teardown paths share
    /// this bundle, and this is the guard that the bundle is complete.
    #[test]
    fn the_teardown_leaves_no_board_sprite_behind() {
        use crate::app::{board_render, creatures};
        let mut app = App::new();
        // One of each marked category, plus a bystander that must survive:
        // a teardown that simply emptied the world would pass every
        // assertion below without it.
        let survivor = app.world_mut().spawn(Marker).id();
        app.world_mut().spawn(board_render::BoardStatic);
        app.world_mut().spawn(creatures::CrabSprite {
            id: 1,
            kind: crate::sim::CrabKind::Common,
            shade: 0.0,
        });
        app.world_mut().spawn(creatures::GullSprite(1));
        app.world_mut()
            .spawn(creatures::SweepSprite::new(2, Vec2::ZERO));
        // A footprint: on a reload it was left pressed into the new beach.
        app.world_mut().spawn(effects::Particle::default());
        let before = app.world().entities().len();
        assert!(before > 5, "the fixture did not spawn");

        app.add_systems(Update, despawn_board_sprites);
        app.update();

        assert_eq!(
            app.world_mut()
                .query_filtered::<Entity, With<board_render::BoardStatic>>()
                .iter(app.world())
                .count(),
            0,
            "the static beach survived the teardown"
        );
        for left in [
            app.world_mut()
                .query_filtered::<Entity, With<creatures::CrabSprite>>()
                .iter(app.world())
                .count(),
            app.world_mut()
                .query_filtered::<Entity, With<creatures::GullSprite>>()
                .iter(app.world())
                .count(),
            app.world_mut()
                .query_filtered::<Entity, With<creatures::SweepSprite>>()
                .iter(app.world())
                .count(),
            app.world_mut()
                .query_filtered::<Entity, With<effects::Particle>>()
                .iter(app.world())
                .count(),
        ] {
            assert_eq!(left, 0, "a category of board sprite survived the teardown");
        }
        assert!(
            app.world().get_entity(survivor).is_ok(),
            "the teardown took something that was not a board sprite"
        );
    }

    /// A seat given up on mid-round turns AI on the frame the table
    /// agreed on, and not one frame sooner.
    ///
    /// Found by killing a real joiner: every peer desynced within a second
    /// of the drop. `Controllers` says which seats the AI holds and nothing about
    /// when it took them, so `fill_bot_actions` filled the chair from the
    /// instant the notice reached each shell. That instant is the host's
    /// own decision on the host and a datagram everywhere else, and a peer
    /// still working through frames it already had the departed player's
    /// inputs for wrote a bot's move over each of them. `PlayerAction::None`
    /// is what a player pressing nothing sends, so this was most of them.
    #[test]
    fn an_abandoned_seat_turns_ai_on_the_frame_the_table_agreed_on() {
        let bots = Controllers::from_levels([None, None, Some(BotLevel::Normal), None, None, None]);
        // Seat 1 walked out, and the round agreed to empty it from 300.
        let abandoned = [(1u8, 300u32)];
        let at = |seat, frame| ai_holding(&bots, &abandoned, BotLevel::Hard, seat, frame);

        assert_eq!(at(1, 299), None, "299 was still theirs to play");
        assert_eq!(at(1, 300), Some(BotLevel::Hard), "and 300 is the AI's");
        assert_eq!(at(1, 900), Some(BotLevel::Hard), "and every frame after");

        // A seat dealt to the AI at the launch was never anyone's, so the
        // frame has nothing to say about it, and a human seat stays human.
        assert_eq!(
            at(2, 0),
            Some(BotLevel::Normal),
            "the launch AI, from the start"
        );
        assert_eq!(
            at(2, 900),
            Some(BotLevel::Normal),
            "and all the way through"
        );
        assert_eq!(at(0, 900), None, "and a seat nobody left is left be");
    }

    /// A recorded round whose tide comes in on the first frame: see
    /// [`round_over`].
    fn finished_round(dir: &std::path::Path, online: net::Online) -> App {
        round_over(dir, online, true)
    }

    /// A round whose tide comes in on the first frame, on a shelf of its
    /// own, with the results card's keep key and the arena's teardown.
    /// `online` is the table it was played at, and `recorded` whether this
    /// machine has a recording of it.
    fn round_over(dir: &std::path::Path, online: net::Online, recorded: bool) -> App {
        round_over_with(dir, online, recorded, |_| {})
    }

    /// [`round_over`], with `prepare` let at the world before the round
    /// is played.
    fn round_over_with(
        dir: &std::path::Path,
        online: net::Online,
        recorded: bool,
        prepare: impl FnOnce(&mut App),
    ) -> App {
        let mut app = App::new();
        app.add_plugins(bevy::state::app::StatesPlugin);
        app.insert_state(Screen::Versus);
        app.insert_state(VersusPhase::Running);
        let mut board = classic_arena(false, 2);
        board.rules_mut().round_length = Some(0);
        board.set_score(1, 3);
        let mut replay = Replay::new(Level::from_board("Versus", 3, board.clone()));
        replay.ai[1] = Some(BotLevel::Hard);
        app.insert_resource(Sim(board));
        app.insert_resource(Recorder(recorded.then_some(replay)));
        app.insert_resource(Seats(2));
        app.init_resource::<Controllers>();
        let settings = settings::GameSettings::default();
        app.insert_resource(SeatNames(settings.names.clone()));
        app.insert_resource(settings);
        app.insert_resource(online);
        app.init_resource::<Playback>();
        app.init_resource::<Daily>();
        app.init_resource::<ReelThread>();
        app.init_resource::<Highlight>();
        app.init_resource::<replays::RoundToKeep>();
        app.insert_resource(replays::Library {
            dir: dir.to_path_buf(),
            ..replays::Library::default()
        });
        app.init_resource::<ButtonInput<KeyCode>>();
        app.init_resource::<keycaps::KeyCaps>();
        app.init_resource::<cursor::LiveSeatKeys>();
        app.init_resource::<pause::PauseMenu>();
        app.init_resource::<spectators::SpectatorChat>();
        app.init_resource::<PendingActions>();
        app.init_resource::<rematch::LocalTable>();
        app.init_resource::<match_setup::MatchConfig>();
        app.add_systems(
            Update,
            (
                check_versus_over.run_if(in_state(VersusPhase::Running)),
                replays::keep_input.run_if(in_state(VersusPhase::Over)),
                replays::keep_input.run_if(in_state(Screen::Interlude)),
            ),
        );
        app.add_systems(OnExit(Screen::Versus), end_versus);
        app.add_systems(OnExit(Screen::Interlude), replays::let_the_round_go);
        prepare(&mut app);
        app.update();
        app.update();
        assert_eq!(
            *app.world().resource::<State<VersusPhase>>().get(),
            VersusPhase::Over,
            "the tide is in"
        );
        app
    }

    fn press_keep(app: &mut App) {
        let key = app
            .world()
            .resource::<keycaps::KeyCaps>()
            .key_for(replays::KEEP_CAP)
            .expect("a key");
        let mut keys = app.world_mut().resource_mut::<ButtonInput<KeyCode>>();
        keys.clear();
        keys.press(key);
        app.update();
        let mut keys = app.world_mut().resource_mut::<ButtonInput<KeyCode>>();
        keys.release(key);
        keys.clear();
    }

    /// A shelf of its own, emptied first.
    fn scratch_shelf(name: &str) -> std::path::PathBuf {
        let dir = std::env::temp_dir().join(format!("pinch-shelf-{name}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        dir
    }

    /// A finished round goes on the shelf only when it is kept: nothing
    /// lands while the card is up, and one press files it once, under
    /// the name the round earned and with its whole recording, the AI's
    /// level line included.
    #[test]
    fn a_finished_round_is_shelved_only_when_kept() {
        let dir = scratch_shelf("kept");
        let mut app = finished_round(&dir, net::Online::default());
        for _ in 0..3 {
            app.update();
        }
        assert!(replays::shelf_in(&dir).is_empty(), "not by itself");
        assert_eq!(
            app.world().resource::<replays::RoundToKeep>().offer(),
            replays::KeepOffer::Offered
        );
        press_keep(&mut app);
        let shelf = replays::shelf_in(&dir);
        assert_eq!(shelf.len(), 1, "kept");
        assert_eq!(shelf[0].winner, "P2", "filed under who took it");
        let kept = Replay::parse(&std::fs::read_to_string(&shelf[0].path).expect("read"))
            .expect("a round");
        assert_eq!(kept.ai[1], Some(BotLevel::Hard), "with its AI's level");
        assert_eq!(kept.playback().scores()[1], 3);
        // And how it ended, as the results card showed it: the board the
        // tide came in on, and the row's points the card's first row.
        let card = *app.world().resource::<Sim>().0.scores();
        assert_eq!(kept.scores, Some(card), "the final board's scores");
        let mode = teams::TeamMode::Solo;
        assert_eq!(shelf[0].points, Some(teams::top_total(&card, 2, mode)));
        assert_eq!(shelf[0].points, Some(3));
        press_keep(&mut app);
        assert_eq!(replays::shelf_in(&dir).len(), 1, "and only once");
        assert_eq!(
            app.world().resource::<replays::RoundToKeep>().offer(),
            replays::KeepOffer::Kept
        );
    }

    /// A seat named with a trailing space, "Bo ", beside one named "Bo!",
    /// takes the round for the AI: it is kept under the name the shelf
    /// reads back from the recording, and the shelf tags the AI's row,
    /// not the person's whose name tidies to what an untrimmed one did.
    #[test]
    fn a_name_ending_in_a_space_is_kept_under_the_name_read_back() {
        let dir = scratch_shelf("spaced");
        let mut app = round_over_with(&dir, net::Online::default(), true, |app| {
            let names = ["Bo!", "Bo ", "", "", "", ""].map(String::from);
            app.world_mut()
                .resource_mut::<settings::GameSettings>()
                .names = names;
            app.world_mut().resource_mut::<Controllers>().0[1] = SeatController::Ai(BotLevel::Hard);
            app.init_resource::<SeatKinds>();
            app.add_systems(Startup, resolve_seat_names);
        });
        press_keep(&mut app);
        let shelf = replays::shelf_in(&dir);
        assert_eq!(shelf.len(), 1);
        assert_eq!(shelf[0].winner, "Bo", "filed as the recording names it");
        assert_eq!(
            shelf[0].won_by,
            Some(replays::WonBy::Ai(Some(BotLevel::Hard))),
            "and tagged as the AI that took it"
        );
    }

    /// Leaving the card to the menu, or straight back in for a rematch,
    /// lets the round go: nothing is shelved, then or after.
    #[test]
    fn leaving_the_card_lets_an_unkept_round_go() {
        for (way, to) in [("menu", Screen::Menu), ("rematch", Screen::Versus)] {
            let dir = scratch_shelf(way);
            let mut app = finished_round(&dir, net::Online::default());
            app.world_mut().resource_mut::<NextState<Screen>>().set(to);
            app.update();
            assert_eq!(
                app.world().resource::<replays::RoundToKeep>().offer(),
                replays::KeepOffer::Nothing,
                "{way}: let go"
            );
            press_keep(&mut app);
            assert!(replays::shelf_in(&dir).is_empty(), "{way}: nothing shelved");
        }
    }

    /// Leaving the card for the interlude before a series' next round
    /// keeps the offer standing there, and the key keeps it from there;
    /// the interlude left, for the next round or the menu, lets an unkept
    /// round go.
    #[test]
    fn the_interlude_holds_the_offer_until_it_is_left() {
        for (way, after) in [("next round", Screen::Versus), ("menu", Screen::Menu)] {
            let dir = scratch_shelf(&format!("interlude-{}", after == Screen::Menu));
            let mut app = finished_round(&dir, net::Online::default());
            let go = |app: &mut App, to: Screen| {
                app.world_mut().resource_mut::<NextState<Screen>>().set(to);
                app.update();
            };
            go(&mut app, Screen::Interlude);
            assert_eq!(
                app.world().resource::<replays::RoundToKeep>().offer(),
                replays::KeepOffer::Offered,
                "{way}: still on offer in the interlude"
            );
            go(&mut app, after);
            assert_eq!(
                app.world().resource::<replays::RoundToKeep>().offer(),
                replays::KeepOffer::Nothing,
                "{way}: let go as the interlude is left"
            );
            press_keep(&mut app);
            assert!(replays::shelf_in(&dir).is_empty(), "{way}: nothing shelved");
        }
    }

    /// Online, the host calls the next round of a series the moment its
    /// own card is up, which walks a joiner off its card on the spot. The
    /// joiner can still keep the round it just played, from the
    /// interlude: once, and that round rather than the one being called.
    #[test]
    fn a_joiner_walked_on_keeps_the_round_from_the_interlude() {
        use crate::app::tournament::{SeriesLength, Tournament};
        use crate::sim::{DEFAULT_DELAY, Lockstep};
        use crate::transport::{MatchTerms, UdpTransport};
        let dir = scratch_shelf("walked-on");
        let joiner = net::Online(Some(net::OnlineSession::new(
            UdpTransport::join(("127.0.0.1", 47998)).expect("socket"),
            Lockstep::new(1, vec![0, 1], DEFAULT_DELAY),
            2,
            MatchTerms::default(),
        )));
        let mut app = round_over_with(&dir, joiner, true, |app| {
            app.insert_resource(Tournament::start(SeriesLength::BestOfThree));
            app.init_resource::<lobby::Homecoming>();
            app.init_resource::<Time>();
            app.init_resource::<crate::app::next_map::NextMap>();
            app.init_resource::<match_setup::MatchMenu>();
            app.add_systems(
                Update,
                play_input::versus_over_input.run_if(conditions::versus_over),
            );
        });
        let finished = match app.world().resource::<replays::RoundToKeep>() {
            replays::RoundToKeep::Offered(round) => round.text.clone(),
            other @ (replays::RoundToKeep::Nothing | replays::RoundToKeep::Kept) => {
                panic!("the round is offered: {other:?}")
            }
        };
        // The host's call lands while the joiner's card is up.
        app.world_mut()
            .resource_mut::<net::Online>()
            .0
            .as_mut()
            .expect("at the table")
            .next_round = true;
        app.update();
        app.update();
        assert_eq!(
            *app.world().resource::<State<Screen>>().get(),
            Screen::Interlude,
            "walked on by the call"
        );
        press_keep(&mut app);
        press_keep(&mut app);
        let shelf = replays::shelf_in(&dir);
        assert_eq!(shelf.len(), 1, "kept, and only once");
        let kept = std::fs::read_to_string(&shelf[0].path).expect("read");
        assert_eq!(kept, finished, "the round just played");
        assert_eq!(
            app.world().resource::<replays::RoundToKeep>().offer(),
            replays::KeepOffer::Kept
        );
        // The next round starts: nothing of the last is left on offer.
        app.world_mut()
            .resource_mut::<NextState<Screen>>()
            .set(Screen::Versus);
        app.update();
        assert_eq!(
            app.world().resource::<replays::RoundToKeep>().offer(),
            replays::KeepOffer::Nothing
        );
        assert_eq!(replays::shelf_in(&dir).len(), 1);
    }

    /// Online each machine keeps the round for itself: the host keeping
    /// it shelves it on the host alone, and a joiner keeps its own copy
    /// or none, with nothing sent either way.
    #[test]
    fn host_and_joiner_each_keep_their_own() {
        use crate::sim::{DEFAULT_DELAY, Lockstep};
        use crate::transport::{MatchTerms, UdpTransport};
        let table = |seat: u8| {
            let transport = match seat {
                0 => UdpTransport::host(0).expect("socket"),
                _ => UdpTransport::join(("127.0.0.1", 47999)).expect("socket"),
            };
            net::Online(Some(net::OnlineSession::new(
                transport,
                Lockstep::new(seat, vec![0, 1], DEFAULT_DELAY),
                2,
                MatchTerms::default(),
            )))
        };
        let (host_dir, joiner_dir) = (scratch_shelf("host"), scratch_shelf("joiner"));
        let mut host = finished_round(&host_dir, table(0));
        let mut joiner = finished_round(&joiner_dir, table(1));
        assert!(
            host.world()
                .resource::<net::Online>()
                .0
                .as_ref()
                .expect("on")
                .is_host()
        );
        for app in [&host, &joiner] {
            assert_eq!(
                app.world().resource::<replays::RoundToKeep>().offer(),
                replays::KeepOffer::Offered,
                "both are offered it"
            );
        }
        press_keep(&mut host);
        joiner.update();
        assert_eq!(replays::shelf_in(&host_dir).len(), 1, "the host kept it");
        assert!(
            replays::shelf_in(&joiner_dir).is_empty(),
            "the joiner did not"
        );
        press_keep(&mut joiner);
        assert_eq!(replays::shelf_in(&joiner_dir).len(), 1, "until it does");
        assert_eq!(replays::shelf_in(&host_dir).len(), 1);
    }

    /// A round with no recording here, a replay watched or a round picked
    /// up from a code, has nothing to offer the shelf.
    #[test]
    fn a_round_with_no_recording_offers_nothing() {
        let dir = scratch_shelf("none");
        let mut app = round_over(&dir, net::Online::default(), false);
        assert_eq!(
            app.world().resource::<replays::RoundToKeep>().offer(),
            replays::KeepOffer::Nothing
        );
        press_keep(&mut app);
        assert!(replays::shelf_in(&dir).is_empty());
    }

    /// A round being recorded is stamped with what held each seat and,
    /// for the game's AI, at which level: the shelf names the AI that
    /// took a round by it.
    /// A couch round names each seat after the person partner picks put
    /// there, from the names saved in settings, which stay as typed; the
    /// daily's own table goes by the saved rows.
    #[test]
    fn a_couch_round_names_the_people_picked_into_each_seat() {
        let mut app = App::new();
        let mut saved = settings::GameSettings::default();
        saved.names[1] = "Bo".into();
        saved.names[2] = "Cleo".into();
        app.insert_resource(saved);
        let mut config = match_setup::MatchConfig::default();
        config.people.0.swap(1, 2);
        app.insert_resource(config);
        app.init_resource::<Daily>();
        app.init_resource::<net::Online>();
        app.init_resource::<Playback>();
        app.init_resource::<SeatNames>();
        app.init_resource::<SeatKinds>();
        app.insert_resource(Seats(4));
        app.init_resource::<Controllers>();
        app.insert_resource(Recorder(None));
        app.add_systems(Update, resolve_seat_names);
        app.update();
        assert_eq!(app.world().resource::<SeatNames>().0[1..3], ["Cleo", "Bo"]);
        app.world_mut().resource_mut::<Daily>().active = true;
        app.update();
        assert_eq!(app.world().resource::<SeatNames>().0[1..3], ["Bo", "Cleo"]);
    }

    #[test]
    fn a_recorded_round_keeps_each_ai_level() {
        let mut app = App::new();
        app.init_resource::<settings::GameSettings>();
        app.init_resource::<match_setup::MatchConfig>();
        app.init_resource::<Daily>();
        app.init_resource::<net::Online>();
        app.init_resource::<Playback>();
        app.init_resource::<SeatNames>();
        app.init_resource::<SeatKinds>();
        app.insert_resource(Seats(4));
        app.insert_resource(Controllers([
            SeatController::Local,
            SeatController::Ai(BotLevel::Hard),
            SeatController::Bot,
            SeatController::Ai(BotLevel::Easy),
            SeatController::Local,
            SeatController::Local,
        ]));
        app.insert_resource(Recorder(Some(Replay::new(Level::from_board(
            "Turf War",
            3,
            classic_arena(false, 4),
        )))));
        app.add_systems(Update, resolve_seat_names);
        app.update();
        let replay = app.world().resource::<Recorder>().0.clone().expect("kept");
        assert_eq!(
            replay.ai,
            [
                None,
                Some(BotLevel::Hard),
                None,
                Some(BotLevel::Easy),
                None,
                None
            ]
        );
        assert_eq!(replay.kinds[2], crate::sim::SeatKind::Bot);
        let back = Replay::parse(&replay.to_text()).expect("reads back");
        assert_eq!(back.ai, replay.ai, "and the file says so");
    }

    /// A round recorded as it is played holds one input a tick, and played
    /// back from its first board lands on the board the round left: the
    /// whole promise of the replay library, through the real tick system.
    #[test]
    fn a_recorded_round_plays_back_to_the_board_it_left() {
        let mut app = sim_app();
        app.world_mut().resource_mut::<Controllers>().0[0] = SeatController::Ai(BotLevel::Normal);
        app.world_mut().resource_mut::<Controllers>().0[1] = SeatController::Ai(BotLevel::Hard);
        let start = app.world().resource::<Sim>().0.clone();
        app.world_mut().resource_mut::<Recorder>().0 =
            Some(Replay::new(Level::from_board("Turf War", 3, start)));
        for _ in 0..40 {
            app.update();
        }
        let board = &app.world().resource::<Sim>().0;
        let replay = app
            .world()
            .resource::<Recorder>()
            .0
            .as_ref()
            .expect("recording");
        assert_eq!(board.ticks(), 40);
        assert_eq!(replay.inputs.len(), 40, "one input a tick");
        assert_eq!(
            replay.playback().state_hash(),
            board.state_hash(),
            "and played back, the same beach"
        );
    }

    /// The host's call for the next round can land on a joiner still a few
    /// frames short of the tide: the session is the new round's from then
    /// on, and the board is still the old one. Ticked on, the old board
    /// played the new round's first frames out, and the new board began at
    /// tick zero with the lockstep frames ahead of it: a desync for the
    /// whole of the next round. The board is left where it stood, nothing
    /// is committed against it, and the joiner is walked on to the round
    /// that was called.
    #[test]
    fn a_called_round_leaves_the_old_board_where_it_stood() {
        use crate::transport::{MatchTerms, UdpTransport};

        let mut app = sim_app();
        app.add_plugins(bevy::state::app::StatesPlugin);
        app.init_state::<Screen>();
        app.insert_resource(State::new(Screen::Versus));
        app.add_systems(Update, follow_the_called_round);
        // The last round, most of the way through on this machine.
        for _ in 0..50 {
            app.world_mut()
                .resource_mut::<Sim>()
                .0
                .tick(&[PlayerAction::None; MAX_PLAYERS]);
        }
        // And the next one taken up off the wire: a fresh lockstep, whose
        // first frames need nobody's input to run.
        let mut joiner = net::OnlineSession::new(
            UdpTransport::host(0).expect("socket"),
            crate::sim::Lockstep::new(1, vec![0, 1], crate::sim::DEFAULT_DELAY),
            2,
            MatchTerms::default(),
        );
        joiner.next_round = true;
        let commit = joiner.session.next_commit();
        app.insert_resource(net::Online(Some(joiner)));
        app.world_mut().resource_mut::<PendingActions>().0[1] = PlayerAction::Remove { x: 1, y: 1 };

        app.update();
        assert_eq!(
            app.world().resource::<Sim>().0.ticks(),
            50,
            "the old board played the new round's frames"
        );
        let online = app.world().resource::<net::Online>();
        let session = online.0.as_ref().expect("the session");
        assert_eq!(session.session.frame(), 0, "the new round is untouched");
        assert_eq!(
            session.session.next_commit(),
            commit,
            "a key meant for the old board went to the new one"
        );
        app.update();
        assert_eq!(
            *app.world().resource::<State<Screen>>().get(),
            Screen::Interlude,
            "and on to the round that was called"
        );
    }

    /// A round nobody is watching still stops when it is paused.
    #[test]
    fn a_paused_round_does_not_advance() {
        let mut app = sim_app();
        app.world_mut().resource_mut::<Paused>().0 = true;
        let before = app.world().resource::<Sim>().0.ticks();
        app.update();
        assert_eq!(
            app.world().resource::<Sim>().0.ticks(),
            before,
            "a paused beach kept walking"
        );
        app.world_mut().resource_mut::<Paused>().0 = false;
        app.update();
        assert!(
            app.world().resource::<Sim>().0.ticks() > before,
            "and never started again"
        );
    }

    /// A recording is fed one frame per tick, or as many as the transport
    /// asks for. The speed is the whole of the fast-forward: there is no
    /// separate scrubbing path.
    #[test]
    fn the_transport_speed_is_how_many_frames_a_replay_eats() {
        for speed in [1u8, 2, 4] {
            let mut app = sim_app();
            let level = Level::from_board("Turf War", 3, classic_arena(false, 2));
            let mut replay = Replay::new(level);
            for _ in 0..40 {
                replay.record([PlayerAction::None; MAX_PLAYERS]);
            }
            app.world_mut().insert_resource(Playback(Some((replay, 0))));
            app.world_mut()
                .insert_resource(replays::PlaybackSpeed(speed));
            app.update();
            let (_, idx) = app
                .world()
                .resource::<Playback>()
                .0
                .as_ref()
                .expect("still watching");
            assert_eq!(
                *idx,
                usize::from(speed),
                "at {speed}x the recording moved {idx} frames in one tick"
            );
        }
    }

    /// And it stops at the end rather than running off the tape.
    #[test]
    fn a_replay_stops_when_the_recording_runs_out() {
        let mut app = sim_app();
        let level = Level::from_board("Turf War", 3, classic_arena(false, 2));
        let mut replay = Replay::new(level);
        replay.record([PlayerAction::None; MAX_PLAYERS]);
        app.world_mut().insert_resource(Playback(Some((replay, 0))));
        app.world_mut().insert_resource(replays::PlaybackSpeed(4));
        for _ in 0..5 {
            app.update();
        }
        let (replay, idx) = app
            .world()
            .resource::<Playback>()
            .0
            .as_ref()
            .expect("still watching");
        assert_eq!(*idx, replay.inputs.len(), "it read past the last frame");
    }
}