diff --git a/tests/Aetheria.Shared.Tests/EconomyCommandTests.cs b/tests/Aetheria.Shared.Tests/EconomyCommandTests.cs new file mode 100644 index 000000000..916a4804a --- /dev/null +++ b/tests/Aetheria.Shared.Tests/EconomyCommandTests.cs @@ -0,0 +1,215 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using GameCult.Caching; +using Xunit; + +public sealed class EconomyCommandTests +{ + private sealed class Lambda : EconomyBatch + { + private readonly Action _author; + public Lambda(Action author) => _author = author; + public override void Author(EconomyAuthor author) => _author(author); + } + + private static void Flush(EconomyFixture f) => f.Cache.FlushAsync().GetAwaiter().GetResult(); + + private static CultCache Read(EconomyFixture f) => AetheriaStores.Open(f.CatalogPath, registry: TestCatalog.Registry()); + + private static int Author(EconomyFixture f, bool apply, out string output, params Action[] batches) + { + Flush(f); + var writer = new StringWriter(); + var code = EconomyCommand.Author(f.CatalogPath, batches.Select(b => (EconomyBatch) new Lambda(b)).ToList(), apply, writer, TestCatalog.Registry()); + output = writer.ToString(); + return code; + } + + // A commodity and its recipe, run by the fixture's mill unless a kind is given. + private static void Good(EconomyFixture f, EconomyAuthor a, string id, string lore = "a good", string[] fills = null, (string Id, int Count)[] inputs = null, + CultRecordRef? kind = null) + { + var key = new CultRecordKey(EconomyFixture.Key(id)); + if (inputs == null) a.Put(new SimpleCommodityData { Name = id }, key); + else a.Put(new CompoundCommodityData { Name = id }, key); + a.Put(new RecipeData + { + Output = EconomyFixture.Item(key.Value), StationKind = kind ?? f.Mill, LoreHook = lore, + Inputs = (inputs ?? Array.Empty<(string, int)>()).Select(i => new CommodityCount { Commodity = EconomyFixture.Item(EconomyFixture.Key(i.Id)), Count = i.Count }).ToList(), + Fills = (fills ?? Array.Empty()).ToList() + }, RecipeData.KeyOf(key)); + } + + [Fact] + public void ABatchWithAShapeFaultIsRefusedAndTheCatalogStaysByteIdentical() + { + using var f = EconomyFixture.Healthy(); + Flush(f); + var bytes = File.ReadAllBytes(f.CatalogPath); + var code = Author(f, true, out var output, a => Good(f, a, "tar", lore: " ")); + Assert.Equal(1, code); + Assert.Contains("L-lore", output); + Assert.Contains("refused", output); + Assert.Equal(bytes, File.ReadAllBytes(f.CatalogPath)); + } + + [Fact] + public void TheGateRefusesALoopThatCannotSustainItself() + { + using var f = EconomyFixture.Healthy(); + Flush(f); + var bytes = File.ReadAllBytes(f.CatalogPath); + var code = Author(f, true, out var output, a => + { + Good(f, a, "loopa", inputs: new[] { ("loopb", 1) }, fills: new[] { "Plate" }); + Good(f, a, "loopb", inputs: new[] { ("loopa", 1) }); + }); + Assert.Equal(1, code); + Assert.Contains("L-productive", output); + Assert.Equal(bytes, File.ReadAllBytes(f.CatalogPath)); + } + + [Fact] + public void ADryRunWritesNothingAndPrintsWhatItWouldDo() + { + using var f = EconomyFixture.Healthy(); + Flush(f); + var bytes = File.ReadAllBytes(f.CatalogPath); + var code = Author(f, false, out var output, a => Good(f, a, "coal"), a => a.Edit("ore", ore => ore.Description = "grey")); + Assert.Equal(0, code); + Assert.Contains("added commodity:coal", output); + Assert.Contains("added recipe:commodity:coal", output); + Assert.Contains("changed commodity:ore", output); + Assert.Contains("Dry run", output); + Assert.Equal(bytes, File.ReadAllBytes(f.CatalogPath)); + } + + [Fact] + public void ACleanBatchAppliesAndTheCatalogThenReportsNoFault() + { + using var f = EconomyFixture.Healthy(); + var code = Author(f, true, out var output, a => + { + Good(f, a, "coal"); + Good(f, a, "steel", inputs: new[] { ("coal", 1), ("plate", 1) }, fills: new[] { "Plate" }); + }); + Assert.Equal(0, code); + Assert.Contains("0 faults", output); + using var after = Read(f); + Assert.NotNull(after.Get(RecipeData.KeyOf(new CultRecordKey("commodity:steel")))); + Assert.Empty(EconomyChecks.Check(after).Faults); + } + + [Fact] + public void ApplyConvergesAndTouchesOnlyWhatItNames() + { + using var f = EconomyFixture.Healthy(); + Action coal = a => Good(f, a, "coal", fills: new[] { "Plate" }); + Assert.Equal(0, Author(f, true, out _, coal)); + var bytes = File.ReadAllBytes(f.CatalogPath); + Assert.Equal(0, Author(f, true, out var again, coal)); + Assert.Contains("0 added, 0 changed, 2 unchanged", again); + Assert.Equal(bytes, File.ReadAllBytes(f.CatalogPath)); + + Assert.Equal(0, Author(f, true, out var revised, a => Good(f, a, "coal", lore: "black rock", fills: new[] { "Plate" }))); + Assert.Contains("0 added, 1 changed, 1 unchanged", revised); + using var after = Read(f); + Assert.Equal("black rock", after.Get(RecipeData.KeyOf(new CultRecordKey("commodity:coal"))).LoreHook); + Assert.Equal("a good", after.Get(RecipeData.KeyOf(new CultRecordKey("commodity:ore"))).LoreHook); + } + + [Fact] + public void ACompletenessFaultIsReportedAndDoesNotBlockApply() + { + using var f = EconomyFixture.Healthy(); + var code = Author(f, true, out var output, a => Good(f, a, "dust")); + Assert.Equal(0, code); + Assert.Contains("L-source-sink", output); + using var after = Read(f); + Assert.NotNull(after.Get(RecipeData.KeyOf(new CultRecordKey("commodity:dust")))); + } + + [Fact] + public void ACommodityWrittenWithoutARecipeIsCompletenessNotARefusal() + { + using var f = EconomyFixture.Healthy(); + var code = Author(f, true, out var output, a => a.Put(new SimpleCommodityData { Name = "bare" }, new CultRecordKey("commodity:bare"))); + Assert.Equal(0, code); + Assert.Contains("L-recipe", output); + } + + [Fact] + public void AuthorsAKindWithStandIns() + { + using var f = EconomyFixture.Healthy(); + var forge = StationKindData.KeyOf("forge"); + var code = Author(f, true, out _, a => + { + a.Put(new StationKindData { Name = "Forge", StandIns = new List { new StationKindStandIn { Faction = f.Maker, Kind = f.Mill } } }, forge); + Good(f, a, "slab", kind: new CultRecordRef(forge), fills: new[] { "Plate" }); + }); + Assert.Equal(0, code); + using var after = Read(f); + Assert.Single(after.Get(forge).StandIns); + Assert.DoesNotContain(EconomyChecks.Check(after).Faults, x => x.Invariant == EconomyChecks.LOneKindList); + } + + [Fact] + public void AKindThatStandsInForItselfIsRefusedAndNothingIsWritten() + { + using var f = EconomyFixture.Healthy(); + Flush(f); + var bytes = File.ReadAllBytes(f.CatalogPath); + var forge = StationKindData.KeyOf("forge"); + var code = Author(f, true, out var output, a => a.Put(new StationKindData + { + Name = "Forge", StandIns = new List { new StationKindStandIn { Faction = f.Maker, Kind = new CultRecordRef(forge) } } + }, forge)); + Assert.Equal(1, code); + Assert.Contains("refused", output); + Assert.Equal(bytes, File.ReadAllBytes(f.CatalogPath)); + } + + [Fact] + public void AnEditNamingNoItemIsARefusalNotAThrow() + { + using var f = EconomyFixture.Healthy(); + Flush(f); + var bytes = File.ReadAllBytes(f.CatalogPath); + var code = Author(f, true, out var output, a => a.Edit("unobtainium", i => i.Price = 3)); + Assert.Equal(1, code); + Assert.Contains("unobtainium", output); + Assert.Equal(bytes, File.ReadAllBytes(f.CatalogPath)); + } + + [Fact] + public void AnEditChangesOnlyTheNamedItem() + { + using var f = EconomyFixture.Healthy(); + var code = Author(f, true, out _, a => a.Edit("ore", ore => { ore.Description = "grey"; ore.Price = 7; ore.Mass = 2; })); + Assert.Equal(0, code); + using var after = Read(f); + var ore = after.Get(new CultRecordKey("commodity:ore")); + Assert.Equal(("grey", 7, 2f), (ore.Description, ore.Price, ore.Mass)); + Assert.Null(after.Get(new CultRecordKey("commodity:plate")).Description); + } + + [Fact] + public void ReportPrintsFaultsAndDepthAndFailsOnAnyFault() + { + using var f = EconomyFixture.Healthy(); + var healthy = new StringWriter(); + Assert.Equal(0, EconomyCommand.Report(f.Cache, healthy)); + Assert.Contains("commodity:plate", healthy.ToString()); + Assert.Contains("depth 1", healthy.ToString()); + f.EditRecipe("commodity:plate", r => r.LoreHook = ""); + var broken = new StringWriter(); + Assert.Equal(1, EconomyCommand.Report(f.Cache, broken)); + Assert.Contains("L-lore", broken.ToString()); + } + + [Fact] + public void AnUnknownSubcommandFails() => Assert.Equal(1, EconomyCommand.Run(new[] { "bogus" })); +} diff --git a/tools/AetherDb/EconomyCommand.cs b/tools/AetherDb/EconomyCommand.cs new file mode 100644 index 000000000..ae62d1416 --- /dev/null +++ b/tools/AetherDb/EconomyCommand.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.IO; +using GameCult.Caching; + +// One batch of economy content: commodities, recipes, station kinds with stand-ins, outside-market rows, edits to existing items. +public abstract class EconomyBatch +{ + public abstract void Author(EconomyAuthor author); +} + +public sealed class EconomyAuthor +{ + public void Put(T document, CultRecordKey key) where T : class => throw new NotImplementedException(); + public void Edit(string name, Action edit) where T : ItemData => throw new NotImplementedException(); +} + +public static class EconomyCommand +{ + public static int Run(string[] args) => throw new NotImplementedException(); + public static int Report(CultCache catalog, TextWriter output) => throw new NotImplementedException(); + public static int Author(string catalogPath, IReadOnlyList batches, bool apply, TextWriter output, CultDocumentRegistry registry = null) => + throw new NotImplementedException(); +}