using System; using System.Collections.Generic; using System.Linq; using BlaxLaws; using RimWorld; using Verse; namespace BlaxLawsTests { /// /// Closed cases are pruned on save after "Keep closed cases" (GameComponent_BlaxLaws.PruneClosedCases and /// ExposeData). Each test works on its own GameComponent_BlaxLaws that the game doesn't hold, so the live /// game's cases stay as they were, and saves it with Scribe.saver.DebugOutputFor (Verse/ScribeSaver.cs:141-176), /// which runs the component's ExposeData in Saving mode into a string and writes no file. Pruning needs the /// game clock past the setting (7 to 360 days), so the tests move TicksGame forward with /// TickManager.DebugSetTicksGame (Verse/TickManager.cs:512) and put it back before they end; nothing ticks /// in between. /// public static class PruneTests { private const int Day = GenDate.TicksPerDay; // The clock while a test runs: past the longest setting (360 days) with room to spare. private const int ClockAhead = 400 * Day; [BlaxTest("cases: saving prunes a closed case older than Keep closed cases; one closed exactly that long, a younger one and every open case stay")] public static void SavePrunesOnlyOldClosedCases() { T.NotNull(Fixtures.Settings, "the mod settings"); Fixtures.Settings.keepClosedDays = 30; int keep = 30 * Day; T.Equal(keep, BlaxLawsMod.S.KeepClosedTicks, "Keep closed cases at 30 days in ticks"); WithClockAhead(now => { var comp = new GameComponent_BlaxLaws(); LawCase old = Add(comp, "BlaxLawsTest_PruneOld", CaseStatus.Closed, now - keep - 1); LawCase edge = Add(comp, "BlaxLawsTest_PruneEdge", CaseStatus.Closed, now - keep); LawCase young = Add(comp, "BlaxLawsTest_PruneYoung", CaseStatus.Closed, now - Day); LawCase open = Add(comp, "BlaxLawsTest_PruneOpen", CaseStatus.Open, -1); LawCase enforcing = Add(comp, "BlaxLawsTest_PruneEnforcing", CaseStatus.Enforcing, -1); LawCase jailed = Add(comp, "BlaxLawsTest_PruneJailed", CaseStatus.Jailed, -1); // Not closed, with a close tick from long ago: only the status decides that a case is closed. LawCase staleTick = Add(comp, "BlaxLawsTest_PruneStaleTick", CaseStatus.Open, 0); T.Equal(7, comp.Cases.Count, "cases before the save"); string xml = Save(comp); T.Check(!comp.Cases.Contains(old), "the case closed 30 days and 1 tick ago is still in the store after the save: " + old); T.Check(!xml.Contains(old.actKey), "the case closed 30 days and 1 tick ago went into the save"); foreach (LawCase stays in new[] { edge, young, open, enforcing, jailed, staleTick }) { T.Check(comp.Cases.Contains(stays), "the save pruned " + stays + " (close tick " + stays.closeTick + ", now " + now + ")"); T.Check(xml.Contains(stays.actKey), "the save lacks " + stays); } T.Equal(6, comp.Cases.Count, "cases after the save"); }); } [BlaxTest("settings: Keep closed cases moves the cutoff; a case closed 10 days ago is pruned at 7 days and kept at 30; days outside 7 to 360 count as 7 or 360")] public static void SettingMovesTheCutoff() { T.NotNull(Fixtures.Settings, "the mod settings"); WithClockAhead(now => { // At 7 days: 10 days closed goes, 5 days closed stays. Fixtures.Settings.keepClosedDays = 7; var comp7 = new GameComponent_BlaxLaws(); LawCase ten7 = Add(comp7, "BlaxLawsTest_PruneTen", CaseStatus.Closed, now - 10 * Day); LawCase five7 = Add(comp7, "BlaxLawsTest_PruneFive", CaseStatus.Closed, now - 5 * Day); Save(comp7); T.Check(!comp7.Cases.Contains(ten7), "at 7 days the case closed 10 days ago stayed"); T.Check(comp7.Cases.Contains(five7), "at 7 days the case closed 5 days ago was pruned"); // At 30 days: 10 days closed stays. Fixtures.Settings.keepClosedDays = 30; var comp30 = new GameComponent_BlaxLaws(); LawCase ten30 = Add(comp30, "BlaxLawsTest_PruneTen", CaseStatus.Closed, now - 10 * Day); LawCase forty30 = Add(comp30, "BlaxLawsTest_PruneForty", CaseStatus.Closed, now - 40 * Day); Save(comp30); T.Check(comp30.Cases.Contains(ten30), "at 30 days the case closed 10 days ago was pruned"); T.Check(!comp30.Cases.Contains(forty30), "at 30 days the case closed 40 days ago stayed"); // Below the minimum: 1 day counts as 7, so 5 days closed stays. Fixtures.Settings.keepClosedDays = 1; T.Equal(7 * Day, BlaxLawsMod.S.KeepClosedTicks, "Keep closed cases set to 1 day, in ticks"); var comp1 = new GameComponent_BlaxLaws(); LawCase five1 = Add(comp1, "BlaxLawsTest_PruneFive", CaseStatus.Closed, now - 5 * Day); Save(comp1); T.Check(comp1.Cases.Contains(five1), "at 1 day (counts as 7) the case closed 5 days ago was pruned"); // Above the maximum: 999 days counts as 360, so 361 days closed goes. Fixtures.Settings.keepClosedDays = 999; T.Equal(360 * Day, BlaxLawsMod.S.KeepClosedTicks, "Keep closed cases set to 999 days, in ticks"); var comp999 = new GameComponent_BlaxLaws(); LawCase old999 = Add(comp999, "BlaxLawsTest_PruneYearOld", CaseStatus.Closed, now - 361 * Day); LawCase young999 = Add(comp999, "BlaxLawsTest_PruneFive", CaseStatus.Closed, now - 5 * Day); Save(comp999); T.Check(!comp999.Cases.Contains(old999), "at 999 days (counts as 360) the case closed 361 days ago stayed"); T.Check(comp999.Cases.Contains(young999), "at 999 days the case closed 5 days ago was pruned"); }); } [BlaxTest("cases: pruning returns the count and drops the case from its offender's index; the offender's other cases stay")] public static void PruneKeepsOffenderIndex() { Pawn p = Fixtures.Colonist(); WithClockAhead(now => { var comp = new GameComponent_BlaxLaws(); LawCase old = Add(comp, "BlaxLawsTest_PruneOld", CaseStatus.Closed, now - 2 * Day, p); LawCase young = Add(comp, "BlaxLawsTest_PruneYoung", CaseStatus.Closed, now - Day / 2, p); LawCase open = Add(comp, "BlaxLawsTest_PruneOpen", CaseStatus.Open, -1, p); T.Equal(3, comp.CasesOf(p).Count, "the offender's cases before pruning"); T.Equal(1, comp.PruneClosedCases(Day), "cases pruned at one day"); IReadOnlyList left = comp.CasesOf(p); T.Check(!left.Contains(old), "the offender index still holds the pruned case"); T.Check(left.Contains(young) && left.Contains(open), "the offender index lost a case that stays: " + string.Join(", ", left.Select(c => c.ToString()))); T.Equal(2, left.Count, "the offender's cases after pruning"); T.Equal(0, comp.PruneClosedCases(Day), "cases pruned by a second call"); }); } // ---- helpers --------------------------------------------------------------------- /// /// Runs the body with TicksGame at least ClockAhead ahead and puts the clock back afterwards. The body /// gets the clock it runs at. No tick runs while the clock is ahead. /// private static void WithClockAhead(Action body) { TickManager tm = Find.TickManager; int before = tm.TicksGame; int now = before + ClockAhead; tm.DebugSetTicksGame(now); try { body(now); } finally { tm.DebugSetTicksGame(before); } } /// A case in the given component, added through its AddCase. private static LawCase Add(GameComponent_BlaxLaws comp, string actKey, CaseStatus status, int closeTick, Pawn offender = null) { var c = new LawCase { id = comp.Cases.Count + 1, actKey = actKey, stage = LawStage.DidIt, count = 1, openTick = 0, lastActTick = 0, status = status, closeTick = closeTick, outcome = status == CaseStatus.Closed ? CaseOutcomes.Repealed : null, }; if (offender != null) c.SetOffender(offender); comp.AddCase(c); T.Check(comp.Cases.Contains(c), "fixture: AddCase didn't add " + c); return c; } /// Runs the component's ExposeData in Saving mode, as a save does, and returns the XML it wrote. private static string Save(GameComponent_BlaxLaws comp) { T.Equal(LoadSaveMode.Inactive, Scribe.mode, "fixture: Scribe mode before the save"); string xml = Scribe.saver.DebugOutputFor(comp); T.Equal(LoadSaveMode.Inactive, Scribe.mode, "Scribe mode after the save"); T.Check(!string.IsNullOrEmpty(xml) && xml.Contains("