diff --git a/tests/Aetheria.Shared.Tests/EconomyRegionTests.cs b/tests/Aetheria.Shared.Tests/EconomyRegionTests.cs index f37d7df39..188dadcba 100644 --- a/tests/Aetheria.Shared.Tests/EconomyRegionTests.cs +++ b/tests/Aetheria.Shared.Tests/EconomyRegionTests.cs @@ -97,13 +97,95 @@ public sealed class EconomyRegionTests Assert.True(sample.NoExtraction, "an extraction facility ran: the premise of this test is gone"); var idle = EconomyRegion.Report(new[] { sample }).Where(fault => fault.Invariant == EconomyRegion.HIdle).Select(fault => fault.Message).ToList(); Assert.NotEmpty(idle); - Assert.All(idle, message => Assert.Contains("no extraction runs", message)); + // A recipe that draws raw ore says so; one that draws an intermediate names the idle recipe above it instead. + Assert.Contains(idle, message => message.Contains("no extraction runs")); + Assert.Contains(idle, message => message.Contains("whose recipe is itself idle")); + Assert.All(idle, message => Assert.Contains("missing ", message)); Assert.All(idle, message => Assert.DoesNotContain("moves no cargo", message)); var text = new StringWriter(); EconomyRegion.Print(sample, text); Assert.Contains("no extraction runs in the region: every downstream recipe starves", text.ToString()); Assert.Contains("at least", text.ToString()); } + + // Cause branches on hand-built samples, each the true cause of its own: the booted region has only one of them today. + private static RegionRecipe Recipe(string output, Stall final, double cycle = 60, params string[] missing) + { + var recipe = new RegionRecipe { Output = output, Stations = 1, Cycle = cycle }; + recipe.Final[final] = 1; + foreach (var good in missing) recipe.Draws[good] = false; + return recipe; + } + + private static RegionSample Sample(int extractorsRunning, params RegionRecipe[] recipes) + { + var sample = new RegionSample { Seed = 7, Hours = 1, Spans = 60, Extractors = 3, ExtractorsRunning = extractorsRunning }; + sample.Recipes.AddRange(recipes); + return sample; + } + + private static string Message(RegionSample sample, string output) => + EconomyRegion.Report(new[] { sample }).Single(fault => fault.Invariant == EconomyRegion.HIdle && fault.Record == $"seed 7 {output}").Message; + + [Fact] + public void RawOreMissingWithNoExtractionNamesTheExtraction() + { + var message = Message(Sample(0, Recipe("Iron", Stall.Inputs, 60, "Iron Ore")), "Iron"); + Assert.Contains("Iron Ore, which no recipe in the region makes and no extraction runs", message); + Assert.DoesNotContain("haul leg", message); + } + + [Fact] + public void RawOreMissingWithExtractionRunningNamesTheHaulLeg() + { + var sample = Sample(1, Recipe("Iron", Stall.Inputs, 60, "Iron Ore")); + Assert.False(sample.NoExtraction); + var message = Message(sample, "Iron"); + Assert.Contains("no haul leg brings from outside", message); + Assert.DoesNotContain("no extraction runs", message); + } + + [Fact] + public void ARecipeWhoseHoldIsFullIsNotBlamedOnItsInputs() + { + var message = Message(Sample(0, Recipe("Steel", Stall.Room, 60, "Iron")), "Steel"); + Assert.Contains("no room for its output", message); + Assert.DoesNotContain("missing", message); + } + + [Fact] + public void AMissingInputIsTracedOneHopUpstream() + { + var steel = Recipe("Steel", Stall.Inputs, 60, "Iron"); + var ammo = Recipe("Ammo", Stall.Inputs, 60, "Steel"); + var idle = Message(Sample(0, ammo, steel), "Ammo"); + Assert.Contains("Steel, whose recipe is itself idle", idle); + Assert.DoesNotContain("extraction", idle); + steel.Made = 5; + var ran = Message(Sample(0, ammo, steel), "Ammo"); + Assert.Contains("Steel, whose recipe ran but whose units are not held where this one is made: no haul leg", ran); + } + + [Fact] + public void ARecipeThatTakesNoRawOreIsNotBlamedOnExtraction() + { + var message = Message(Sample(0, Recipe("Ammo", Stall.Inputs, 60, "Steel"), Recipe("Steel", Stall.Inputs, 60, "Iron Ore")), "Ammo"); + Assert.DoesNotContain("no extraction runs", message); + } + + [Fact] + public void TheRecipeTableIsALowerBoundAndNotesACycleOffTheSpan() + { + var text = new StringWriter(); + EconomyRegion.Print(Sample(0, Recipe("Algae", Stall.None, 90), Recipe("Hexamine", Stall.None, 120)), text); + var lines = text.ToString().Split('\n'); + var algae = lines.Single(line => line.TrimStart().StartsWith("Algae |", StringComparison.Ordinal)); + var hexamine = lines.Single(line => line.TrimStart().StartsWith("Hexamine |", StringComparison.Ordinal)); + Assert.Contains("at least 0 made", algae); + Assert.Contains("cycle 90 s, not a multiple of the 60 s span", algae); + Assert.Contains("at least 0 made", hexamine); + Assert.DoesNotContain("not a multiple", hexamine); + } [Fact] public void PrintNamesTheRegionAndWhatItDoesNotMove() { diff --git a/tools/AetherDb/EconomyRegion.cs b/tools/AetherDb/EconomyRegion.cs index 46c29be43..0ca424582 100644 --- a/tools/AetherDb/EconomyRegion.cs +++ b/tools/AetherDb/EconomyRegion.cs @@ -53,6 +53,9 @@ public sealed class RegionRecipe public bool Started; // The cycle of the recipe, and the Stall each facility holding it ended the horizon in. public double Cycle; + // The goods a cycle draws, and whether any station holding a facility of the recipe holds some of each when the horizon ends. + public SortedDictionary Draws = new SortedDictionary(StringComparer.Ordinal); + internal readonly Dictionary DrawKeys = new Dictionary(StringComparer.Ordinal); public SortedDictionary Final = new SortedDictionary(); } @@ -115,6 +118,7 @@ public static class EconomyRegion if (entry == null) sample.Recipes.Add(entry = new RegionRecipe { Output = Good(recipe.Output.Key) }); entry.Stations++; entry.Cycle = recipe.Cycle; + foreach (var draw in Production.Draws(catalog, recipe)) { entry.Draws[Good(draw)] = false; entry.DrawKeys[Good(draw)] = draw; } } } var hold = new EntityHold(entity); @@ -152,6 +156,14 @@ public static class EconomyRegion } } sample.Spans = ticks; + for (var s = 0; s < tracked.Count; s++) + { + var hold = new EntityHold(tracked[s].entity); + for (var f = 0; f < tracked[s].facilities.Length; f++) + foreach (var recipe in sample.Recipes.Where(r => tracked[s].station.Facilities[f].Outputs.Contains(r.Output))) + foreach (var draw in recipe.Draws.Keys.ToArray()) + if (!recipe.Draws[draw] && hold.Held(recipe.DrawKeys[draw]) > 0) recipe.Draws[draw] = true; + } for (var s = 0; s < tracked.Count; s++) for (var f = 0; f < tracked[s].facilities.Length; f++) { @@ -218,14 +230,28 @@ public static class EconomyRegion return faults; } - // Why a recipe made nothing, from the Stall its facilities ended in and from what lies upstream of it. + // Why a recipe made nothing: the Stall its facilities ended in, then each input it draws that no station holding it has, traced one + // hop upstream to what makes that input in the region and whether that ran. private static string Cause(RegionSample sample, RegionRecipe recipe) { var ends = recipe.Final.Count == 0 ? "no facility Stall was read" : "its facilities ended " + string.Join(", ", recipe.Final.Select(kv => $"{kv.Key} x{kv.Value}")); if (recipe.Final.ContainsKey(Stall.Room) && !recipe.Final.ContainsKey(Stall.Inputs)) return ends + ": the hold has no room for its output."; - if (sample.NoExtraction) - return ends + ": its inputs are not held, and no raw ore enters the region because no extraction runs (extraction recipes do not run yet, follow-up facility-extraction)."; - return ends + ": its inputs are not held where it is made, and no haul leg brings them (region-haul-legs)."; + var missing = recipe.Draws.Where(draw => !draw.Value).Select(draw => draw.Key).ToList(); + if (missing.Count == 0) return ends + ": it holds some of every input, but not a cycle's quantity of one."; + return ends + ": missing " + string.Join("; ", missing.Select(good => Upstream(sample, good))) + "."; + } + + // One missing input: what in the region makes it, and why that did not deliver. + private static string Upstream(RegionSample sample, string good) + { + var maker = sample.Recipes.FirstOrDefault(recipe => recipe.Output == good); + if (maker == null) + return sample.NoExtraction + ? $"{good}, which no recipe in the region makes and no extraction runs to supply (extraction recipes do not run yet, follow-up facility-extraction)" + : $"{good}, which no recipe in the region makes and no haul leg brings from outside (region-haul-legs)"; + return maker.Ran + ? $"{good}, whose recipe ran but whose units are not held where this one is made: no haul leg moves them (region-haul-legs)" + : $"{good}, whose recipe is itself idle"; } public static void Print(RegionSample sample, TextWriter output)