trane 0.28.0

An automated system for learning complex skills
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
//! End-to-end tests to test basic scenarios.
//!
//! The `scheduler` module in Trane is difficult to test with unit tests that check the expected
//! return value of `get_exercise_batch` against the actual output for a couple of reasons:
//! - When the dependents of a unit are added to the stack during the depth-first search phase, they
//!   are shuffled beforehand to avoid traversing the graph in the same order every time.
//! - In the second phase, candidate exercises are grouped in buckets based on their score. From
//!   each of these buckets, a subset is selected randomly based on weights assigned to each
//!   candidate.
//!
//! Instead of testing the output of a single call to `get_exercise_batch`, Trane's end-to-end tests
//! verify less strict assertions about the results of a simulated study session in which a
//! simulated student scores simulated exercises. For example, given a course library, an empty
//! blacklist, and a student that always scores the highest score in all exercises, all the
//! exercises in the course should be scheduled after going through a sufficiently large number of
//! exercise batches. If this assertion fails, it points to a bug in the scheduler that is not
//! letting the student make progress past some points in the graph.
//!
//! In practice, this testing strategy has been used to catch many bugs in Trane. Most of these bugs
//! were in the scheduler, but some were in other parts of the codebase as this simulation opens a
//! real course library that is written to disk beforehand. While this testing strategy cannot catch
//! every bug, it has proved to offer sufficient coverage for the part of Trane's codebase that is
//! difficult to verify using simple unit tests.
//!
//! The end-to-end tests in this file all use the same hand-coded course library and perform basic
//! checks, ensuring, among other things, that Trane makes progress when good scores are entered by
//! the student, that bad scores cause progress to stall, and that course and lesson filters are
//! respected.

use std::{collections::BTreeMap, sync::LazyLock};

use anyhow::{Ok, Result};
use chrono::{Duration, Utc};
use tempfile::TempDir;
use trane::{
    Trane,
    course_library::{CourseLibrary, LocalCourseLibrary, SerializedCourseLibrary},
    data::{
        MasteryScore, SchedulerOptions, UnitType, UserPreferences,
        filter::{ExerciseFilter, SessionPart, StudySession, StudySessionData, UnitFilter},
    },
    review_list::ReviewList,
    scheduler::ExerciseScheduler,
    test_utils::*,
};
use ustr::Ustr;

/// A simple set of courses to test the basic functionality of Trane.
static LIBRARY: LazyLock<Vec<TestCourse>> = LazyLock::new(|| {
    vec![
        TestCourse {
            id: TestId(0, None, None),
            dependencies: vec![],
            encompassed: vec![],
            superseded: vec![],
            metadata: BTreeMap::default(),
            lessons: vec![
                TestLesson {
                    id: TestId(0, Some(0), None),
                    dependencies: vec![],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
                TestLesson {
                    id: TestId(0, Some(1), None),
                    dependencies: vec![TestId(0, Some(0), None)],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
            ],
        },
        TestCourse {
            id: TestId(1, None, None),
            dependencies: vec![TestId(0, None, None)],
            encompassed: vec![],
            superseded: vec![],
            metadata: BTreeMap::default(),
            lessons: vec![
                TestLesson {
                    id: TestId(1, Some(0), None),
                    dependencies: vec![],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
                TestLesson {
                    id: TestId(1, Some(1), None),
                    dependencies: vec![TestId(1, Some(0), None)],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
            ],
        },
        TestCourse {
            id: TestId(2, None, None),
            dependencies: vec![TestId(0, None, None)],
            encompassed: vec![],
            superseded: vec![],
            metadata: BTreeMap::default(),
            lessons: vec![
                TestLesson {
                    id: TestId(2, Some(0), None),
                    dependencies: vec![],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
                TestLesson {
                    id: TestId(2, Some(1), None),
                    dependencies: vec![TestId(2, Some(0), None)],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
                TestLesson {
                    id: TestId(2, Some(2), None),
                    dependencies: vec![TestId(2, Some(1), None)],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
            ],
        },
        TestCourse {
            id: TestId(4, None, None),
            dependencies: vec![],
            encompassed: vec![],
            superseded: vec![],
            metadata: BTreeMap::default(),
            lessons: vec![
                TestLesson {
                    id: TestId(4, Some(0), None),
                    dependencies: vec![],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
                TestLesson {
                    id: TestId(4, Some(1), None),
                    dependencies: vec![TestId(4, Some(0), None), TestId(2, Some(1), None)],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
                TestLesson {
                    id: TestId(4, Some(2), None),
                    dependencies: vec![TestId(4, Some(0), None)],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
                TestLesson {
                    id: TestId(4, Some(3), None),
                    dependencies: vec![TestId(4, Some(2), None)],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
            ],
        },
        TestCourse {
            id: TestId(5, None, None),
            dependencies: vec![
                // Depends on a missing course.
                TestId(3, None, None),
                TestId(4, None, None),
            ],
            encompassed: vec![],
            superseded: vec![],
            metadata: BTreeMap::default(),
            lessons: vec![
                TestLesson {
                    id: TestId(5, Some(0), None),
                    dependencies: vec![TestId(4, Some(1), None)],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
                TestLesson {
                    id: TestId(5, Some(1), None),
                    dependencies: vec![
                        TestId(5, Some(0), None),
                        // Depends on a missing lesson.
                        TestId(3, Some(3), None),
                    ],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
            ],
        },
        TestCourse {
            id: TestId(6, None, None),
            dependencies: vec![TestId(3, None, None)],
            encompassed: vec![(TestId(5, Some(0), None), 0.5)],
            superseded: vec![],
            metadata: BTreeMap::default(),
            lessons: vec![
                TestLesson {
                    id: TestId(6, Some(0), None),
                    dependencies: vec![],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
                TestLesson {
                    id: TestId(6, Some(1), None),
                    dependencies: vec![TestId(6, Some(0), None)],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
            ],
        },
        TestCourse {
            id: TestId(7, None, None),
            dependencies: vec![],
            encompassed: vec![],
            superseded: vec![],
            metadata: BTreeMap::from([
                (
                    "course_key_1".to_string(),
                    vec!["course_key_1:value_1".to_string()],
                ),
                (
                    "course_key_2".to_string(),
                    vec!["course_key_2:value_1".to_string()],
                ),
            ]),
            lessons: vec![
                TestLesson {
                    id: TestId(7, Some(0), None),
                    dependencies: vec![TestId(0, None, None)],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
                TestLesson {
                    id: TestId(7, Some(1), None),
                    dependencies: vec![
                        TestId(0, Some(0), None),
                        // Depends on a missing lesson.
                        TestId(6, Some(11), None),
                    ],
                    encompassed: vec![],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    num_exercises: 10,
                },
                TestLesson {
                    id: TestId(7, Some(2), None),
                    dependencies: vec![TestId(7, Some(1), None)],
                    encompassed: vec![(TestId(7, Some(0), None), 1.0)],
                    superseded: vec![],
                    metadata: BTreeMap::default(),
                    // Lesson with no exercises.
                    num_exercises: 0,
                },
            ],
        },
        // Course with no lessons.
        TestCourse {
            id: TestId(8, None, None),
            dependencies: vec![TestId(7, None, None)],
            encompassed: vec![],
            superseded: vec![],
            metadata: BTreeMap::default(),
            lessons: vec![],
        },
        // Course with no lessons and no dependencies.
        TestCourse {
            id: TestId(9, None, None),
            dependencies: vec![],
            encompassed: vec![],
            superseded: vec![],
            metadata: BTreeMap::default(),
            lessons: vec![],
        },
    ]
});

/// A test that verifies that we retrieve the expected unit IDs.
#[test]
fn get_unit_ids() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Verify the course IDs.
    let course_ids = trane.get_course_ids();
    let expected_course_ids = vec![
        Ustr::from("0"),
        Ustr::from("1"),
        Ustr::from("2"),
        Ustr::from("4"),
        Ustr::from("5"),
        Ustr::from("6"),
        Ustr::from("7"),
        Ustr::from("8"),
        Ustr::from("9"),
    ];
    assert_eq!(course_ids, expected_course_ids);

    // Verify the lesson and exercise IDs.
    for course_id in course_ids {
        let course_test_id = TestId::from(&course_id);
        let lesson_ids = trane.get_lesson_ids(course_id).unwrap_or_default();
        for lesson_id in lesson_ids {
            let lesson_test_id = TestId::from(&lesson_id);
            assert_eq!(course_test_id.0, lesson_test_id.0);
            assert_eq!(lesson_test_id.2, None);
            let exercise_ids = trane.get_exercise_ids(lesson_id).unwrap_or_default();
            for exercise_id in exercise_ids {
                let exercise_test_id = TestId::from(&exercise_id);
                assert!(exercise_test_id.2.is_some());
                assert_eq!(course_test_id.0, exercise_test_id.0);
                assert_eq!(lesson_test_id.1, exercise_test_id.1);
            }
        }
    }

    Ok(())
}

/// A test that verifies retrieving all exercise IDs from the given unit.
#[test]
fn get_all_exercise_ids() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Get all exercises from a course.
    let exercise_ids = trane.get_all_exercise_ids(Some(Ustr::from("0")));
    assert!(!exercise_ids.is_empty());
    for exercise_id in exercise_ids {
        assert!(exercise_id.starts_with("0::"));
    }

    // Get all exercises from a lesson.
    let exercise_ids = trane.get_all_exercise_ids(Some(Ustr::from("0::0")));
    assert!(!exercise_ids.is_empty());
    for exercise_id in exercise_ids {
        assert!(exercise_id.starts_with("0::0::"));
    }

    // Get all exercises from an exercise. This should return only the exercise itself.
    let exercise_ids = trane.get_all_exercise_ids(Some(Ustr::from("0::0::0")));
    assert_eq!(exercise_ids.len(), 1);
    assert_eq!(exercise_ids[0], Ustr::from("0::0::0"));

    // Get all exercises from a non-existent unit. This should return an empty vector.
    let exercise_ids = trane.get_all_exercise_ids(Some(Ustr::from("0::0::100")));
    assert!(exercise_ids.is_empty());

    // Get all the exercises from the library.
    let exercise_ids = trane.get_all_exercise_ids(None);
    let expected_ids = all_test_exercises(&LIBRARY);
    assert_eq!(exercise_ids.len(), expected_ids.len());

    Ok(())
}

/// Verifies that all the exercises are scheduled with no blacklist or filter when the user gives a
/// score of five to every exercise.
#[test]
fn all_exercises_scheduled() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let mut trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Run the simulation.
    let mut simulation = TraneSimulation::new(1000, Box::new(|_| Some(MasteryScore::Five)));
    simulation.run_simulation(&mut trane, &vec![], &None)?;

    // Every exercise ID should be in `simulation.answer_history`.
    let exercise_ids = all_test_exercises(&LIBRARY);
    for exercise_id in exercise_ids {
        let exercise_ustr = exercise_id.to_ustr();
        assert!(
            simulation.answer_history.contains_key(&exercise_ustr),
            "exercise {:?} should have been scheduled",
            exercise_id
        );
        assert_simulation_scores(exercise_ustr, &trane, &simulation.answer_history)?;
    }
    Ok(())
}

/// Verifies no exercises past the first course and lesson are scheduled when the user scores every
/// exercise in that lesson with a mastery score of one.
#[test]
fn bad_score_prevents_advancing() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let mut trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Run the simulation.
    let mut simulation = TraneSimulation::new(200, Box::new(|_| Some(MasteryScore::One)));
    simulation.run_simulation(&mut trane, &vec![], &None)?;

    // Only the exercises in the first lessons should be in `simulation.answer_history`.
    let first_lessons = [
        TestId(0, Some(0), None),
        TestId(4, Some(0), None),
        TestId(6, Some(0), None),
    ];
    let exercise_ids = all_test_exercises(&LIBRARY);
    for exercise_id in exercise_ids {
        let exercise_ustr = exercise_id.to_ustr();
        if first_lessons
            .iter()
            .any(|lesson| exercise_id.exercise_in_lesson(lesson))
        {
            assert!(
                simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should have been scheduled",
                exercise_id
            );
            assert_simulation_scores(exercise_ustr, &trane, &simulation.answer_history)?;
        } else {
            assert!(
                !simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should not have been scheduled",
                exercise_id
            );
        }
    }
    Ok(())
}

/// Verifies only exercises in the given courses are scheduled when a course filter is provided.
#[test]
fn scheduler_respects_course_filter() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let mut trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Run one first simulation to make sure course TestId(4, None, None) is unlocked. This course
    // has a dependency on a lesson from a course in the second filter and the test should verify
    // that the boundary is not crossed.
    let mut simulation = TraneSimulation::new(500, Box::new(|_| Some(MasteryScore::Five)));
    let course_filter = UnitFilter::CourseFilter {
        course_ids: vec![TestId(4, None, None).to_ustr()],
    };
    simulation.run_simulation(
        &mut trane,
        &vec![],
        &Some(ExerciseFilter::UnitFilter(course_filter)),
    )?;
    simulation.answer_history.clear();

    // Run the second course filter.
    let selected_courses = [
        TestId(2, None, None),
        // Missing course.
        TestId(3, None, None),
    ];
    let course_filter = UnitFilter::CourseFilter {
        course_ids: selected_courses.iter().map(|id| id.to_ustr()).collect(),
    };
    simulation.run_simulation(
        &mut trane,
        &vec![],
        &Some(ExerciseFilter::UnitFilter(course_filter)),
    )?;

    // Every exercise ID should be in `simulation.answer_history`.
    let exercise_ids = all_test_exercises(&LIBRARY);
    for exercise_id in exercise_ids {
        let exercise_ustr = exercise_id.to_ustr();
        if selected_courses
            .iter()
            .any(|course_id| exercise_id.exercise_in_course(course_id))
        {
            assert!(
                simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should have been scheduled",
                exercise_id
            );
            assert_simulation_scores(exercise_ustr, &trane, &simulation.answer_history)?;
        } else {
            assert!(
                !simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should not have been scheduled",
                exercise_id
            );
        }
    }

    // All exercises should be from the selected courses, ignoring any dependencies going to missing
    // courses or courses not in the list.
    for exercise_id in simulation.answer_history.keys() {
        let exercise_test_id = TestId::from(exercise_id);
        assert!(
            selected_courses
                .iter()
                .any(|course_id| exercise_test_id.exercise_in_course(course_id)),
            "exercise {:?} should be from one of the selected courses",
            exercise_id
        );
    }
    Ok(())
}

/// Verifies only exercises in the given lesson are scheduled when a lesson filter is provided.
#[test]
fn scheduler_respects_lesson_filter() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let mut trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Run the simulation.
    let mut simulation = TraneSimulation::new(500, Box::new(|_| Some(MasteryScore::Five)));
    let selected_lessons = [
        TestId(2, Some(0), None),
        TestId(4, Some(1), None),
        // Missing lesson.
        TestId(3, Some(0), None),
    ];
    let lesson_filter = UnitFilter::LessonFilter {
        lesson_ids: selected_lessons.iter().map(|id| id.to_ustr()).collect(),
    };
    simulation.run_simulation(
        &mut trane,
        &vec![],
        &Some(ExerciseFilter::UnitFilter(lesson_filter)),
    )?;

    // Every exercise ID should be in `simulation.answer_history`.
    let exercise_ids = all_test_exercises(&LIBRARY);
    for exercise_id in exercise_ids {
        let exercise_ustr = exercise_id.to_ustr();
        if selected_lessons
            .iter()
            .any(|lesson_id| exercise_id.exercise_in_lesson(lesson_id))
        {
            assert!(
                simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should have been scheduled",
                exercise_id
            );
            assert_simulation_scores(exercise_ustr, &trane, &simulation.answer_history)?;
        } else {
            assert!(
                !simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should not have been scheduled",
                exercise_id
            );
        }
    }
    Ok(())
}

/// Verifies that exercises in the review list are scheduled when using the review list filter.
#[test]
fn schedule_exercises_in_review_list() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let mut trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Add some exercises to the review list.
    let review_exercises = vec![TestId(1, Some(0), Some(0)), TestId(2, Some(1), Some(7))];
    for unit_id in &review_exercises {
        let unit_ustr = unit_id.to_ustr();
        trane.add_to_review_list(unit_ustr)?;
    }

    // Run the simulation with the review list filter.
    let mut simulation = TraneSimulation::new(100, Box::new(|_| Some(MasteryScore::Five)));
    simulation.run_simulation(
        &mut trane,
        &vec![],
        &Some(ExerciseFilter::UnitFilter(UnitFilter::ReviewListFilter)),
    )?;

    // Only the exercises in the review list should have been scheduled.
    let exercise_ids = all_test_exercises(&LIBRARY);
    for exercise_id in exercise_ids {
        let exercise_ustr = exercise_id.to_ustr();
        if review_exercises
            .iter()
            .any(|review_exercise_id| *review_exercise_id == exercise_id)
        {
            assert!(
                simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should have been scheduled",
                exercise_id
            );
            assert_simulation_scores(exercise_ustr, &trane, &simulation.answer_history)?;
        } else {
            assert!(
                !simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should not have been scheduled",
                exercise_id
            );
        }
    }
    Ok(())
}

/// Verifies that exercises from the lessons in the review list are scheduled when using the review
/// list filter.
#[test]
fn schedule_lessons_in_review_list() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let mut trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Add some lessons to the review list.
    let review_lessons = vec![TestId(1, Some(0), None), TestId(2, Some(1), None)];
    for unit_id in &review_lessons {
        let unit_ustr = unit_id.to_ustr();
        trane.add_to_review_list(unit_ustr)?;
    }

    // Run the simulation with the review list filter.
    let mut simulation = TraneSimulation::new(100, Box::new(|_| Some(MasteryScore::Five)));
    simulation.run_simulation(
        &mut trane,
        &vec![],
        &Some(ExerciseFilter::UnitFilter(UnitFilter::ReviewListFilter)),
    )?;

    // Only the exercises from the lessons in the review list should have been scheduled.
    let exercise_ids = all_test_exercises(&LIBRARY);
    for exercise_id in exercise_ids {
        let exercise_ustr = exercise_id.to_ustr();
        if review_lessons
            .iter()
            .any(|lesson_id| exercise_id.exercise_in_lesson(lesson_id))
        {
            assert!(
                simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should have been scheduled",
                exercise_id
            );
            assert_simulation_scores(exercise_ustr, &trane, &simulation.answer_history)?;
        } else {
            assert!(
                !simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should not have been scheduled",
                exercise_id
            );
        }
    }
    Ok(())
}

/// Verifies that exercises from the courses in the review list are scheduled when using the
/// review list filter.
#[test]
fn schedule_courses_in_review_list() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let mut trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Add some courses to the review list.
    let review_courses = vec![TestId(1, None, None), TestId(2, None, None)];
    for unit_id in &review_courses {
        let unit_ustr = unit_id.to_ustr();
        trane.add_to_review_list(unit_ustr)?;
    }

    // Run the simulation with the review list filter.
    let mut simulation = TraneSimulation::new(500, Box::new(|_| Some(MasteryScore::Five)));
    simulation.run_simulation(
        &mut trane,
        &vec![],
        &Some(ExerciseFilter::UnitFilter(UnitFilter::ReviewListFilter)),
    )?;

    // Only the exercises from the courses in the review list should have been scheduled.
    let exercise_ids = all_test_exercises(&LIBRARY);
    for exercise_id in exercise_ids {
        let exercise_ustr = exercise_id.to_ustr();
        if review_courses
            .iter()
            .any(|course_id| exercise_id.exercise_in_course(course_id))
        {
            assert!(
                simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should have been scheduled",
                exercise_id
            );
            assert_simulation_scores(exercise_ustr, &trane, &simulation.answer_history)?;
        } else {
            assert!(
                !simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should not have been scheduled",
                exercise_id
            );
        }
    }
    Ok(())
}

/// Verifies scheduling exercises from the given units and their dependents.
#[test]
fn schedule_units_and_dependents() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let mut trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Only schedule the exercises from the given units and their dependents.
    let starting_units = [TestId(5, Some(0), None)];
    let unit_and_dependents = [
        TestId(5, Some(0), None),
        TestId(5, Some(1), None),
        TestId(5, Some(2), None),
    ];

    // Run the simulation with the dependents filter.
    let mut simulation = TraneSimulation::new(500, Box::new(|_| Some(MasteryScore::Five)));
    simulation.run_simulation(
        &mut trane,
        &vec![],
        &Some(ExerciseFilter::UnitFilter(UnitFilter::Dependents {
            unit_ids: starting_units
                .iter()
                .map(|unit_id| unit_id.to_ustr())
                .collect(),
        })),
    )?;

    // Only the exercises from the starting units and their dependents should have been scheduled.
    let exercise_ids = all_test_exercises(&LIBRARY);
    for exercise_id in exercise_ids {
        let exercise_ustr = exercise_id.to_ustr();
        if unit_and_dependents
            .iter()
            .any(|course_id| exercise_id.exercise_in_lesson(course_id))
        {
            assert!(
                simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should have been scheduled",
                exercise_id
            );
            assert_simulation_scores(exercise_ustr, &trane, &simulation.answer_history)?;
        } else {
            assert!(
                !simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should not have been scheduled",
                exercise_id
            );
        }
    }
    Ok(())
}

/// Verifies scheduling exercises from the dependencies of a unit at a given depth.
#[test]
fn schedule_dependencies() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let mut trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Only schedule the exercises from the dependencies of the unit at depth 1.
    let starting_units = [TestId(5, Some(1), None)];
    let depth = 1;
    let matching_lessons = [TestId(5, Some(0), None), TestId(5, Some(1), None)];

    // Run the simulation.
    let mut simulation = TraneSimulation::new(500, Box::new(|_| Some(MasteryScore::Five)));
    simulation.run_simulation(
        &mut trane,
        &vec![],
        &Some(ExerciseFilter::UnitFilter(UnitFilter::Dependencies {
            unit_ids: starting_units
                .iter()
                .map(|unit_id| unit_id.to_ustr())
                .collect(),
            depth,
        })),
    )?;

    // Only exercises that are dependencies of the starting units at the given depth or any of their
    // dependents should have been scheduled.
    let exercise_ids = all_test_exercises(&LIBRARY);
    for exercise_id in exercise_ids {
        let exercise_ustr = exercise_id.to_ustr();
        if matching_lessons
            .iter()
            .any(|course_id| exercise_id.exercise_in_lesson(course_id))
        {
            assert!(
                simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should have been scheduled",
                exercise_id
            );
            assert_simulation_scores(exercise_ustr, &trane, &simulation.answer_history)?;
        } else {
            assert!(
                !simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should not have been scheduled",
                exercise_id
            );
        }
    }
    Ok(())
}

/// Verifies scheduling exercises from the dependencies of a unit at a depth that is larger than the
/// depth of the graph.
#[test]
fn schedule_dependencies_large_depth() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let mut trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Only schedule the exercises from the dependencies of the unit at depth 5. The search should
    // stop earlier because the graph is not as deep.
    let starting_units = [TestId(2, None, None)];
    let depth = 5;
    let matching_courses = [
        TestId(0, None, None),
        TestId(1, None, None),
        TestId(2, None, None),
        TestId(7, None, None),
        TestId(8, None, None),
    ];

    // Run the simulation.
    let mut simulation = TraneSimulation::new(500, Box::new(|_| Some(MasteryScore::Five)));
    simulation.run_simulation(
        &mut trane,
        &vec![],
        &Some(ExerciseFilter::UnitFilter(UnitFilter::Dependencies {
            unit_ids: starting_units
                .iter()
                .map(|unit_id| unit_id.to_ustr())
                .collect(),
            depth,
        })),
    )?;

    // Only exercises that are dependencies of the starting units at the given depth or any of their
    // dependents should have been scheduled.
    let exercise_ids = all_test_exercises(&LIBRARY);
    for exercise_id in exercise_ids {
        let exercise_ustr = exercise_id.to_ustr();
        if matching_courses
            .iter()
            .any(|course_id| exercise_id.exercise_in_course(course_id))
        {
            assert!(
                simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should have been scheduled",
                exercise_id
            );
            assert_simulation_scores(exercise_ustr, &trane, &simulation.answer_history)?;
        } else {
            assert!(
                !simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should not have been scheduled",
                exercise_id
            );
        }
    }
    Ok(())
}

/// Verifies scheduling exercises from the dependencies of some unknown unit does not schedule any
/// exercises.
#[test]
fn schedule_dependencies_unknown_unit() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let mut trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Only schedule the exercises from the dependencies of the unit at depth 5. Since the unit does
    // not exist, no exercises should be scheduled.
    let starting_units = [TestId(20, None, None)];
    let depth = 5;

    // Run the simulation.
    let mut simulation = TraneSimulation::new(500, Box::new(|_| Some(MasteryScore::Five)));
    simulation.run_simulation(
        &mut trane,
        &vec![],
        &Some(ExerciseFilter::UnitFilter(UnitFilter::Dependencies {
            unit_ids: starting_units
                .iter()
                .map(|unit_id| unit_id.to_ustr())
                .collect(),
            depth,
        })),
    )?;

    // Verify no exercises were scheduled.
    let exercise_ids = all_test_exercises(&LIBRARY);
    for exercise_id in exercise_ids {
        let exercise_ustr = exercise_id.to_ustr();
        assert!(
            !simulation.answer_history.contains_key(&exercise_ustr),
            "exercise {:?} should not have been scheduled",
            exercise_id
        );
    }
    Ok(())
}

/// Verifies scheduling exercises from a study session.
#[test]
fn schedule_study_session() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let mut trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Create a study session with a couple of parts.
    let session_data = StudySessionData {
        start_time: Utc::now() - Duration::minutes(30),
        definition: StudySession {
            id: "session".into(),
            description: "session".into(),
            parts: vec![
                SessionPart::UnitFilter {
                    filter: UnitFilter::CourseFilter {
                        course_ids: vec!["0".into()],
                    },
                    duration: 15,
                },
                SessionPart::UnitFilter {
                    filter: UnitFilter::CourseFilter {
                        course_ids: vec!["1".into()],
                    },
                    duration: 30,
                },
            ],
        },
    };

    // Run the simulation.
    let mut simulation = TraneSimulation::new(500, Box::new(|_| Some(MasteryScore::Five)));
    simulation.run_simulation(
        &mut trane,
        &vec![],
        &Some(ExerciseFilter::StudySession(session_data)),
    )?;

    // The second part of the session is active, so only exercises from course 1 should have been
    // scheduled.
    let matching_courses = [TestId(1, None, None)];
    let exercise_ids = all_test_exercises(&LIBRARY);
    for exercise_id in exercise_ids {
        let exercise_ustr = exercise_id.to_ustr();
        if matching_courses
            .iter()
            .any(|course| exercise_id.exercise_in_course(course))
        {
            assert!(
                simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should have been scheduled",
                exercise_id
            );
            assert_simulation_scores(exercise_ustr, &trane, &simulation.answer_history)?;
        } else {
            assert!(
                !simulation.answer_history.contains_key(&exercise_ustr),
                "exercise {:?} should not have been scheduled",
                exercise_id
            );
        }
    }

    Ok(())
}

/// Verifies matching the courses with the given prefix.
#[test]
fn get_matching_courses() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // The test will use the ID of course 0 as the prefix.
    let prefix = TestId(0, None, None).to_ustr();

    // Get all the courses that match the prefix.
    let matching_courses = trane.get_matching_prefix(&prefix, Some(UnitType::Course));
    assert_eq!(matching_courses.len(), 1);
    assert!(matching_courses.contains(&prefix));
    Ok(())
}

/// Verifies matching the lessons with the given prefix.
#[test]
fn get_matching_lessons() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // The test will use the ID of lesson 0::0 as the prefix.
    let prefix = TestId(0, Some(0), None).to_ustr();

    // Get all the lessons that match the prefix.
    let matching_lessons = trane.get_matching_prefix(&prefix, Some(UnitType::Lesson));
    assert_eq!(matching_lessons.len(), 1);
    assert!(matching_lessons.contains(&prefix));
    Ok(())
}

/// Verifies matching the exercises with the given prefix.
#[test]
fn get_matching_exercises() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // The test will use the ID of exercise 0::0::0 as the prefix.
    let prefix = TestId(0, Some(0), Some(0)).to_ustr();

    // Get all the exercises that match the prefix.
    let matching_exercises = trane.get_matching_prefix(&prefix, Some(UnitType::Exercise));
    assert_eq!(matching_exercises.len(), 1);
    assert!(matching_exercises.contains(&prefix));
    Ok(())
}

/// Verifies matching all units with the given prefix.
#[test]
fn get_matching_units() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // The test will use the ID of course 0 as the prefix.
    let prefix = TestId(0, None, None).to_ustr();

    // Get all the units that match the prefix. The 23 units are the course, the two lessons, and
    // the ten exercises in each lesson.
    let matching_units = trane.get_matching_prefix(&prefix, None);
    assert_eq!(matching_units.len(), 23);
    assert!(matching_units.contains(&prefix));
    assert!(matching_units.contains(&TestId(0, Some(0), None).to_ustr()));
    assert!(matching_units.contains(&TestId(0, Some(0), Some(0)).to_ustr()));
    Ok(())
}

/// Verifies setting the scheduler options.
#[test]
fn set_scheduler_options() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let mut trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Set the scheduler options to have a batch size of ten.
    let scheduler_options = SchedulerOptions {
        batch_size: 10,
        ..Default::default()
    };
    trane.set_scheduler_options(scheduler_options);

    // Verify the scheduler options were set.
    let scheduler_options = trane.get_scheduler_options();
    assert_eq!(scheduler_options.batch_size, 10);
    Ok(())
}

// Verifies resetting the scheduler options.
#[test]
fn reset_scheduler_options() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let mut trane = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Set the scheduler options to have a batch size of ten.
    let scheduler_options = SchedulerOptions {
        batch_size: 10,
        ..Default::default()
    };
    trane.set_scheduler_options(scheduler_options);

    // Reset the scheduler options and verify the scheduler options were reset.
    trane.reset_scheduler_options();
    let scheduler_options = trane.get_scheduler_options();
    assert_eq!(
        scheduler_options.batch_size,
        SchedulerOptions::default().batch_size
    );
    Ok(())
}

/// Verifies ignoring courses specified in the user preferences.
#[test]
fn ignored_paths() -> Result<()> {
    // Set the user preferences to ignore some courses.
    let user_preferences = UserPreferences {
        ignored_paths: vec!["course_0/".to_owned(), "course_5/".to_owned()],
        ..Default::default()
    };

    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let course_builders = LIBRARY
        .iter()
        .map(|c| c.course_builder())
        .collect::<Result<Vec<_>>>()?;
    let trane = init_simulation(temp_dir.path(), &course_builders, Some(&user_preferences))?;

    // Verify the courses in the list are ignored.
    let exercise_ids = trane.get_all_exercise_ids(None);
    assert!(!exercise_ids.is_empty());
    assert!(exercise_ids.iter().all(|id| !id.starts_with("0::")));
    assert!(exercise_ids.iter().all(|id| !id.starts_with("5::")));
    Ok(())
}

/// Verifies serializing a course library and opening a new one from the serialized data.
#[test]
fn serialized_course_library() -> Result<()> {
    // Initialize test course library.
    let temp_dir = TempDir::new()?;
    let _ = init_test_simulation(temp_dir.path(), &LIBRARY)?;

    // Serialize the course library. This requires opening the course library again. The `Trane`
    // struct cannot be used because it uses dynamic traits.
    let course_library = LocalCourseLibrary::new(temp_dir.path(), UserPreferences::default())?;
    let serialized_data = SerializedCourseLibrary::from(&course_library);

    // Decode and encode the data.
    let encoded_data = postcard::to_stdvec(&serialized_data)?;
    let decoded_data: SerializedCourseLibrary = postcard::from_bytes(&encoded_data)?;
    assert_eq!(serialized_data, decoded_data);

    // Open a new library using the serialized data and verify it matches the original.
    let from_serialized_library = LocalCourseLibrary::new_from_serialized(
        serialized_data.clone(),
        UserPreferences::default(),
    )?;
    assert_eq!(
        course_library.course_map,
        from_serialized_library.course_map,
    );
    assert_eq!(
        course_library.lesson_map,
        from_serialized_library.lesson_map,
    );
    assert_eq!(
        course_library.exercise_map,
        from_serialized_library.exercise_map,
    );
    assert_eq!(
        *course_library.unit_graph.read(),
        *from_serialized_library.unit_graph.read(),
    );

    // Open a new trane instance using the serialized library.
    let trane = Trane::new_local_from_serialized(temp_dir.path(), serialized_data)?;
    assert!(trane.get_all_exercise_ids(None).len() > 0);

    Ok(())
}