Fungible commodity tests: merge by design, Extract mints nothing, pre-ruling saves read, sell-ore by ore kind

MiningCut5bTests becomes FungibleCommodityTests; StationServicesTests, HaulFeedTests, ItemTitleTests, RunSaveTests and StationKindDescriptionTests drop their lot clauses.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-27 17:11:19 +01:00
commit 71a07e53bb
6 changed files with 141 additions and 187 deletions

View file

@ -12,15 +12,16 @@ using GameCult.Caching.MessagePack;
using MessagePack;
using Xunit;
// Mining Cut 5b (docs/mining-cut-refresh.md, Cut 5, Q6b = A): a mined unit carries an Extracted lot naming the zone, the belt
// and the ore; stacks merge, split, restore and save by lot. Fixtures are Cut 3's belt scene and Cut 5a's bay and stock helpers.
public sealed class MiningCut5bTests : IDisposable
// Ruling aetheria-living-world:ruling:simple-commodities-fungible (supersedes Mining Cut 5b, Q6b A): a simple commodity carries
// no lot, so units of one design merge whatever their source, Extract mints nothing, and a save from before the ruling still
// reads. Fixtures are Cut 3's belt scene and Cut 5a's bay and stock helpers.
public sealed class FungibleCommodityTests : IDisposable
{
private readonly MiningCut3Tests _scenes = new MiningCut3Tests();
private readonly string _root = Path.Combine(Path.GetTempPath(), "aetheria-mining5b-" + Guid.NewGuid().ToString("N"));
private uint _ordinal;
public MiningCut5bTests() => Directory.CreateDirectory(_root);
public FungibleCommodityTests() => Directory.CreateDirectory(_root);
public void Dispose()
{
@ -79,86 +80,86 @@ public sealed class MiningCut5bTests : IDisposable
FireControl.Extract(zone, chunk, zone.Wear(chunk, 1e6f), ship, SimulationDice.For(zone.DropSeed, SimulationDice.OreStream, ++_ordinal));
}
private static SimpleCommodity[] Stacks(EquippedCargoBay bay) => bay.Cargo.Keys.OfType<SimpleCommodity>().OrderBy(s => s.Lot).ToArray();
private static SimpleCommodity[] Stacks(EquippedCargoBay bay) => bay.Cargo.Keys.OfType<SimpleCommodity>().OrderBy(s => s.Quantity).ToArray();
private static SimpleCommodity Units(Field f, SimpleCommodityData design, int quantity) =>
new SimpleCommodity { Data = new CultRecordRef<ItemData>(f.Scene.Cache.RefOf(design).Key), Quantity = quantity };
[Fact]
public void MinedUnitsCarryAnExtractedLotNamingZoneBeltAndOre()
{
var f = Build();
var zone = ZoneOf(f, 2);
var ship = Shooter(f, zone);
var bay = MiningCut5aTests.Bay(f.Scene, ship, 4, 4, "Hold");
Mine(f, zone, ship, 1, 0);
var stack = Assert.Single(Stacks(bay));
Assert.Equal(4, stack.Quantity);
Assert.NotEqual(0, stack.Lot);
var lot = f.Scene.Items.GetLot(stack);
var origin = Assert.IsType<Extracted>(lot.Origin);
Assert.Equal(2, origin.Zone);
Assert.Equal(f.Scene.Belts[1], origin.Body.Key);
Assert.Equal(stack.Data.Key, origin.Commodity.Key);
Assert.Equal(stack.Data.Key, lot.Design.Key);
Assert.Equal(1f, lot.Quality);
Assert.Equal(default, lot.Product);
}
[Fact]
public void AZoneWithNoGalaxyIsMinus1()
{
var f = Build();
var zone = ZoneOf(f, -1);
var ship = Shooter(f, zone);
var bay = MiningCut5aTests.Bay(f.Scene, ship, 4, 4, "Hold");
Mine(f, zone, ship, 0, 0);
Assert.Equal(-1, Assert.IsType<Extracted>(f.Scene.Items.GetLot(Assert.Single(Stacks(bay))).Origin).Zone);
}
[Fact]
public void UnitsFromOneSourceStack_UnitsFromTwoSourcesDoNot()
public void UnitsMinedFromTwoBeltsAndTwoZonesMergeIntoOneStack()
{
var f = Build();
var zone = ZoneOf(f, 1);
var ship = Shooter(f, zone);
var bay = MiningCut5aTests.Bay(f.Scene, ship, 4, 4, "Hold");
Mine(f, zone, ship, 0, 0);
Mine(f, zone, ship, 0, 1);
var one = Assert.Single(Stacks(bay));
Assert.Equal(8, one.Quantity);
Mine(f, zone, ship, 1, 0);
var stacks = Stacks(bay);
Assert.Equal(new[] { 8, 4 }, stacks.Select(s => s.Quantity));
Assert.NotEqual(stacks[0].Lot, stacks[1].Lot);
Assert.Equal(2, f.Scene.Items.Lots.Lots.Values.Count(l => l.Origin is Extracted));
// Two lots of one ore still total one commodity for the market and the hold.
Assert.Equal(12, MiningCut5aTests.Units(bay, "Rock", f.Scene));
Assert.Equal(8, Assert.Single(Stacks(bay)).Quantity);
var otherZone = ZoneOf(f, 2);
var other = Shooter(f, otherZone);
var otherBay = MiningCut5aTests.Bay(f.Scene, other, 4, 4, "Hold");
Mine(f, otherZone, other, 0, 1);
Assert.True(bay.TryStore(otherBay.Remove(Assert.Single(Stacks(otherBay)), 4)));
Assert.Equal(12, Assert.Single(Stacks(bay)).Quantity);
}
// TryFindSpace and the at-coordinate store both look for a stack to top up: a stack of another lot is not one.
[Fact]
public void ASearchAndAStoreIgnoreAStackOfAnotherLot()
public void BoughtAndMinedUnitsOfOneDesignMerge()
{
var f = Build();
var zone = ZoneOf(f, 0);
var ship = Shooter(f, zone);
var bay = MiningCut5aTests.Bay(f.Scene, ship, 1, 1, "Hold");
var key = new CultRecordRef<ItemData>(f.Scene.Cache.RefOf(f.Ore).Key);
SimpleCommodity Units(int lot, int quantity) => new SimpleCommodity { Data = key, Lot = lot, Quantity = quantity };
var bay = MiningCut5aTests.Bay(f.Scene, ship, 4, 4, "Hold");
Assert.True(bay.TryStore(Units(f, f.Ore, 5)));
Mine(f, zone, ship, 0, 0);
Assert.Equal(9, Assert.Single(Stacks(bay)).Quantity);
Assert.True(bay.TryStore(Units(7, 90)));
Assert.False(bay.TryFindSpace(Units(8, 5), out _));
Assert.True(bay.TryFindSpace(Units(7, 5), out _));
var other = Units(8, 5);
Assert.False(bay.TryStore(other, new int2(0, 0)));
Assert.Equal(90, Assert.Single(bay.Cargo.Keys.OfType<SimpleCommodity>()).Quantity);
Assert.Equal(5, other.Quantity);
Assert.True(bay.TryStore(Units(7, 5), new int2(0, 0)));
Assert.Equal(95, Assert.Single(bay.Cargo.Keys.OfType<SimpleCommodity>()).Quantity);
var floating = zone.Release(Units(f, f.Ore, 4), new float2(0, 0), new float2(0, 0));
Assert.True(zone.TryPickUp(ship, floating.Id));
Assert.Equal(13, Assert.Single(Stacks(bay)).Quantity);
}
[Fact]
public void ASplitStackKeepsItsLot()
public void TwoDesignsNeverMerge()
{
var f = Build();
var zone = ZoneOf(f, 0);
var ship = Shooter(f, zone);
var shells = MiningCut5aTests.Commodity("Shells");
f.Scene.Cache.Upsert(shells);
var bay = MiningCut5aTests.Bay(f.Scene, ship, 4, 4, "Hold");
Assert.True(bay.TryStore(Units(f, f.Ore, 5)));
Assert.True(bay.TryStore(Units(f, shells, 5)));
Assert.Equal(2, Stacks(bay).Length);
var small = MiningCut5aTests.Bay(f.Scene, ship, 1, 1, "Small");
Assert.True(small.TryStore(Units(f, f.Ore, 90)));
Assert.False(small.TryFindSpace(Units(f, shells, 5), out _));
Assert.True(small.TryFindSpace(Units(f, f.Ore, 5), out _));
var other = Units(f, shells, 5);
Assert.False(small.TryStore(other, new int2(0, 0)));
Assert.Equal(90, Assert.Single(Stacks(small)).Quantity);
Assert.True(small.TryStore(Units(f, f.Ore, 5), new int2(0, 0)));
Assert.Equal(95, Assert.Single(Stacks(small)).Quantity);
}
[Fact]
public void ExtractAddsNothingToTheLedger()
{
var f = Build();
var zone = ZoneOf(f, 0);
var ship = Shooter(f, zone);
MiningCut5aTests.Bay(f.Scene, ship, 4, 4, "Hold");
var lots = f.Scene.Items.Lots;
var (count, next) = (lots.Lots.Count, lots.NextLot);
Mine(f, zone, ship, 0, 0);
Mine(f, zone, ship, 1, 0);
Assert.Equal((count, next), (lots.Lots.Count, lots.NextLot));
}
[Fact]
public void ASplitStackIsTheSameDesign()
{
var f = Build();
var zone = ZoneOf(f, 0);
@ -166,24 +167,20 @@ public sealed class MiningCut5bTests : IDisposable
var bay = MiningCut5aTests.Bay(f.Scene, ship, 4, 4, "Hold");
Mine(f, zone, ship, 0, 0);
var stack = Assert.Single(Stacks(bay));
var part = bay.Remove(stack, 3);
Assert.Equal(3, part.Quantity);
Assert.Equal(stack.Lot, part.Lot);
Assert.NotEqual(0, part.Lot);
Assert.Equal(1, stack.Quantity);
Assert.Equal((3, 1), (part.Quantity, stack.Quantity));
Assert.Equal(stack.Data.Key, part.Data.Key);
}
// A saved stack above its design's MaxStack comes back as several stacks, every one of the saved lot.
// A saved stack above its design's MaxStack comes back as several stacks of the one design.
[Fact]
public void ARestoredOversizeStackKeepsItsLot()
public void ARestoredOversizeStackSplitsByDesign()
{
var f = Build();
var zone = ZoneOf(f, 0);
var ship = Shooter(f, zone);
var bay = MiningCut5aTests.Bay(f.Scene, ship, 4, 4, "Hold");
for (var rock = 0; rock < 2; rock++) Mine(f, zone, ship, 0, rock);
var lot = Assert.Single(Stacks(bay)).Lot;
var pack = EntitySerializer.Pack(ship);
f.Ore.MaxStack = 3;
@ -192,7 +189,7 @@ public sealed class MiningCut5bTests : IDisposable
var stacks = restored.CargoBays.SelectMany(Stacks).ToArray();
Assert.Equal(8, stacks.Sum(s => s.Quantity));
Assert.True(stacks.Length >= 3);
Assert.All(stacks, s => Assert.Equal(lot, s.Lot));
Assert.All(stacks, s => Assert.Equal(f.Scene.Cache.RefOf(f.Ore).Key, s.Data.Key));
}
private CultCache Open() => AetheriaStores.Open(Path.Combine(_root, "Aetheria.cc"), Path.Combine(_root, "run.cc"), Path.Combine(_root, "player.cc"));
@ -238,26 +235,7 @@ public sealed class MiningCut5bTests : IDisposable
using (var cache = Open()) return RunSave.Lots(cache);
}
// An ore the draw never picks mints no lot: the ledger gains nothing for it, only for the ore that was drawn.
[Fact]
public void AnOreThatDrawsZeroUnitsMintsNoLot()
{
var f = Build();
var dud = MiningCut5aTests.Commodity("Dud");
MiningCut5aTests.Stock(f.Scene, f.Scene.Kinds[0], (f.Ore, 1f), (dud, 1e-12f));
var zone = ZoneOf(f, 0);
var ship = Shooter(f, zone);
var bay = MiningCut5aTests.Bay(f.Scene, ship, 4, 4, "Hold");
var before = f.Scene.Items.Lots.Lots.Count;
Mine(f, zone, ship, 0, 0);
Assert.Equal(4, MiningCut5aTests.Units(bay, "Rock", f.Scene));
Assert.Equal(0, MiningCut5aTests.Units(bay, "Dud", f.Scene));
Assert.Equal(before + 1, f.Scene.Items.Lots.Lots.Count);
Assert.Equal(1, f.Scene.Items.Lots.Lots.Values.Count(l => l.Origin is Extracted));
}
// A crafted item with no lot is corruption and the save says so; a SimpleCommodity at Lot 0 is ordinary stock.
// A crafted item with no lot is corruption and the save says so; a simple commodity has no lot and roots nothing.
[Fact]
public void ACraftedItemAtLotZeroFailsTheSaveLoudly()
{
@ -266,59 +244,31 @@ public sealed class MiningCut5bTests : IDisposable
var thrown = Assert.Throws<InvalidOperationException>(() => SaveAndReload(new ProvenanceLedger(), crafted));
Assert.Contains("Lot 0 is not in the provenance ledger", thrown.Message);
var stock = new SimpleCommodity { Data = new CultRecordRef<ItemData>(f.Scene.Cache.RefOf(f.Ore).Key), Quantity = 3, Lot = 0 };
var stock = new SimpleCommodity { Data = new CultRecordRef<ItemData>(f.Scene.Cache.RefOf(f.Ore).Key), Quantity = 3 };
var savedStock = SaveAndReload(new ProvenanceLedger(), stock);
Assert.Equal(new[] { _hullLot }, savedStock.Lots.Keys);
}
[Fact]
public void AMinedStackSurvivesSaveGc()
private static byte[] PreRulingStack(CultRecordRef<ItemData> key, MessagePackSerializerOptions options)
{
var f = Build();
var zone = ZoneOf(f, 1);
var ship = Shooter(f, zone);
var bay = MiningCut5aTests.Bay(f.Scene, ship, 4, 4, "Hold");
Mine(f, zone, ship, 0, 0);
Mine(f, zone, ship, 1, 0);
var held = Stacks(bay);
Assert.Equal(2, held.Length);
// A third lot nobody holds: floated and gone, or never collected.
var orphan = f.Scene.Items.ExtractedLot(1, new CultRecordKey("elsewhere"), f.Ore);
var saved = SaveAndReload(f.Scene.Items.Lots, held);
Assert.Equal(held.Select(s => s.Lot).Append(_hullLot).OrderBy(i => i), saved.Lots.Keys.OrderBy(i => i));
Assert.DoesNotContain(orphan, saved.Lots.Keys);
var origin = Assert.IsType<Extracted>(saved[held[0].Lot].Origin);
Assert.Equal(1, origin.Zone);
Assert.Equal(f.Scene.Belts[0], origin.Body.Key);
var buffer = new System.Buffers.ArrayBufferWriter<byte>();
var writer = new MessagePackWriter(buffer);
writer.WriteArrayHeader(2);
writer.Write(0); // Union(0, SimpleCommodity)
writer.WriteArrayHeader(12);
MessagePackSerializer.Serialize(ref writer, key, options);
MessagePackSerializer.Serialize(ref writer, ItemRotation.None, options);
writer.Write(7); // Quantity
for (var slot = 3; slot < 11; slot++) writer.WriteNil();
writer.Write(9); // slot 11: the lot a pre-ruling mined stack named
writer.Flush();
return buffer.WrittenSpan.ToArray();
}
// A save from before the ruling: a three-slot stack from before SimpleCommodity had a lot, and a twelve-slot stack
// whose slot 11 names one. Both read as a plain stack, and merge with a fresh stack of their design.
[Fact]
public void TheLotIndexIsRebuiltOnContinue()
{
var f = Build();
var zone = ZoneOf(f, 1);
var ship = Shooter(f, zone);
var bay = MiningCut5aTests.Bay(f.Scene, ship, 4, 4, "Hold");
Mine(f, zone, ship, 0, 0);
var held = Assert.Single(Stacks(bay));
var saved = SaveAndReload(f.Scene.Items.Lots, held);
var continued = new ItemManager(1u, f.Scene.Cache, saved, RunSaveTests.TestSettings(), _ => { });
Assert.Equal(held.Lot, continued.ExtractedLot(1, f.Scene.Belts[0], f.Ore));
Assert.Equal(2, saved.Lots.Count); // the mined lot and the hull
var next = continued.ExtractedLot(1, f.Scene.Belts[1], f.Ore);
Assert.NotEqual(held.Lot, next);
Assert.Equal(3, saved.Lots.Count);
// Zone and ore tell lots apart as the belt does.
Assert.NotEqual(next, continued.ExtractedLot(2, f.Scene.Belts[1], f.Ore));
}
// A save from before the field reached SimpleCommodity: Data, Rotation, Quantity and nothing else.
[Fact]
public void OldSavesReadLotZero()
public void APreRulingStackReadsAndMerges()
{
var f = Build();
var options = CultDocumentMessagePackSerialization.OptionsFor(typeof(ItemInstance).Assembly);
@ -332,17 +282,42 @@ public sealed class MiningCut5bTests : IDisposable
MessagePackSerializer.Serialize(ref writer, ItemRotation.None, options);
writer.Write(7);
writer.Flush();
var old = Assert.IsType<SimpleCommodity>(MessagePackSerializer.Deserialize<ItemInstance>(buffer.WrittenMemory, options));
Assert.Equal(7, old.Quantity);
Assert.Equal(0, old.Lot);
Assert.Equal(key.Key, old.Data.Key);
Assert.Equal((7, key.Key), (old.Quantity, old.Data.Key));
// A lot rides the wire; a Lot-0 stack is no root, so the old stack saves no lot.
var back = MessagePackSerializer.Deserialize<ItemInstance>(MessagePackSerializer.Serialize<ItemInstance>(
new SimpleCommodity { Data = key, Quantity = 2, Lot = 9 }, options), options);
Assert.Equal(9, back.Lot);
var savedOld = SaveAndReload(new ProvenanceLedger(), old);
Assert.Equal(new[] { _hullLot }, savedOld.Lots.Keys); // the hull only
var lotted = Assert.IsType<SimpleCommodity>(MessagePackSerializer.Deserialize<ItemInstance>(PreRulingStack(key, options), options));
Assert.Equal((7, key.Key), (lotted.Quantity, lotted.Data.Key));
var zone = ZoneOf(f, 0);
var ship = Shooter(f, zone);
var bay = MiningCut5aTests.Bay(f.Scene, ship, 1, 1, "Hold");
Assert.True(bay.TryStore(Units(f, f.Ore, 5)));
Assert.True(bay.TryStore(lotted));
Assert.Equal(12, Assert.Single(Stacks(bay)).Quantity);
}
// A run ledger saved before the ruling holds Extracted lots (Provenance arm 2: zone, commodity, belt). They read as
// RetiredExtracted, and a save drops them: no item roots them.
[Fact]
public void APreRulingExtractedLotLoadsAndCommitDropsIt()
{
var f = Build();
var options = CultDocumentMessagePackSerialization.OptionsFor(typeof(ItemInstance).Assembly);
var buffer = new System.Buffers.ArrayBufferWriter<byte>();
var writer = new MessagePackWriter(buffer);
writer.WriteArrayHeader(2);
writer.Write(2);
writer.WriteArrayHeader(3);
writer.Write(1);
MessagePackSerializer.Serialize(ref writer, new CultRecordRef<SimpleCommodityData>(f.Scene.Cache.RefOf(f.Ore).Key), options);
MessagePackSerializer.Serialize(ref writer, new CultRecordRef<BodyData>(f.Scene.Belts[0]), options);
writer.Flush();
var origin = Assert.IsType<RetiredExtracted>(MessagePackSerializer.Deserialize<Provenance>(buffer.WrittenMemory, options));
var ledger = new ProvenanceLedger();
var retired = ledger.Add(new Lot { Design = default, Origin = origin, Quality = 1f, Roles = new List<RoleFill>() });
var saved = SaveAndReload(ledger);
Assert.DoesNotContain(retired, saved.Lots.Keys);
Assert.Equal(new[] { _hullLot }, saved.Lots.Keys);
}
}

View file

@ -53,7 +53,6 @@ public sealed class HaulFeedTests : IDisposable
Assert.Equal((GainKind.Mined, 4, 0), (gain.Kind, gain.Stored, gain.Adrift));
Assert.Same(rig.Shooter, gain.Taker);
Assert.Equal(4, MiningCut5aTests.Units(bay, "Rock", rig.Scene));
Assert.NotEqual(0, gain.Lot);
// A full hold: everything floats.
var (full, fullBay, fullGains) = Mining(1, 1, (MiningCut5aTests.Commodity("Grit", maxStack: 1), 1f));
@ -99,11 +98,10 @@ public sealed class HaulFeedTests : IDisposable
var chunk = rig.Rock;
FireControl.Extract(rig.Zone, chunk, rig.Zone.Wear(chunk, 1e6f), rig.Shooter, Dice(rig, 1));
var keys = bay.Cargo.Keys.ToArray();
// Same rock field and index, same ore, so the same lot: the second extraction merges and writes no Cargo key.
// Same rock field and index, same ore, so the same design: the second extraction merges and writes no Cargo key.
FireControl.Extract(rig.Zone, chunk, 40f, rig.Shooter, Dice(rig, 2));
Assert.Equal(keys, bay.Cargo.Keys.ToArray());
Assert.Equal(2, gains.Count);
Assert.Equal(gains[0].Lot, gains[1].Lot);
Assert.True(gains[1].Stored > 0);
}
@ -114,7 +112,7 @@ public sealed class HaulFeedTests : IDisposable
rig.Zone.Settings.AsteroidYield = new ExponentialLerp { Minimum = 400f, Maximum = 400f, Exponent = 1f };
FireControl.Extract(rig.Zone, rig.Rock, rig.Zone.Wear(rig.Rock, 1e6f), rig.Shooter, Dice(rig, 3));
Assert.Equal(2, gains.Count);
Assert.Equal(2, gains.Select(g => g.Lot).Distinct().Count());
Assert.Equal(2, gains.Select(g => g.Design.Key).Distinct().Count());
Assert.All(gains, g => Assert.True(g.Stored + g.Adrift > 0));
}
@ -152,9 +150,9 @@ public sealed class HaulFeedTests : IDisposable
rig.Zone.Admit(taker, piloted: false);
var bay = MiningCut5aTests.Bay(rig.Scene, taker, 2, 1, "Taker");
var design = rig.Scene.Cache.RefOf<ItemData>(grit);
Assert.True(bay.TryStore(new SimpleCommodity { Data = design, Lot = 7, Quantity = 3 }));
Assert.True(bay.TryStore(new SimpleCommodity { Data = design, Quantity = 3 }));
gains.Clear();
var floating = rig.Zone.Release(new SimpleCommodity { Data = design, Lot = 7, Quantity = 4 }, new float2(0, 0), new float2(0, 0));
var floating = rig.Zone.Release(new SimpleCommodity { Data = design, Quantity = 4 }, new float2(0, 0), new float2(0, 0));
Assert.True(rig.Zone.TryPickUp(taker, floating.Id));
Assert.Equal(7, MiningCut5aTests.Units(bay, "Grit", rig.Scene));
Assert.Equal((GainKind.Collected, 4, 0), (Assert.Single(gains).Kind, gains[0].Stored, gains[0].Adrift));

View file

@ -110,12 +110,6 @@ public sealed class ItemTitleTests : IDisposable
var given = _items.CreateInstance(_items.CreateLot(_array, default, .5f));
Assert.Equal("Array", _items.Title(given));
var extractedLot = _items.Lots.Add(new Lot
{
Design = _cache.RefOf<ItemData>(_array), Origin = new Extracted(), Quality = .5f, Roles = new List<RoleFill>()
});
Assert.Equal("Array", _items.Title(_items.CreateInstance(extractedLot)));
var ore = new SimpleCommodity { Data = _cache.RefOf<ItemData>(_cache.GetByName<SimpleCommodityData>("Ore")), Quantity = 3 };
Assert.Equal("Ore", _items.Title(ore));
}

View file

@ -436,7 +436,7 @@ public sealed class RunSaveTests : IDisposable
lots.Lots[2] = new Lot { Origin = new Attributed() };
lots.Lots[3] = new Lot { Origin = new Produced { Facility = 4, Inputs = new[] { 5 } } };
lots.Lots[4] = new Lot { Origin = new Attributed() };
lots.Lots[5] = new Lot { Origin = new Extracted() };
lots.Lots[5] = new Lot { Origin = new Attributed() };
lots.Lots[6] = new Lot { Origin = new Attributed() };
var child = BarePack(hull: null, dockingBayContents: new (int2, ItemInstance)[][]

View file

@ -27,11 +27,11 @@ public sealed class StationKindDescriptionTests
{
using var db = AetherDb.Open();
var cache = db.Cache;
string Extracted(string kindId) => string.Join(",", cache.GetAll<RecipeData>()
string RawAt(string kindId) => string.Join(",", cache.GetAll<RecipeData>()
.Where(recipe => Recipes.IsRaw(cache, recipe) && recipe.StationKind.IsSet() && recipe.StationKind.Key.Equals(StationKindData.KeyOf(kindId)))
.Select(recipe => cache.Get(recipe.Output).Name));
Assert.Contains("Diamond", Extracted("mining"));
Assert.Contains("Ammonia", Extracted("volatiles"));
Assert.Contains("Diamond", RawAt("mining"));
Assert.Contains("Ammonia", RawAt("volatiles"));
Assert.Contains("Diamond", cache.Get<StationKindData>(StationKindData.KeyOf("mining")).Description);
Assert.Contains("Ammonia", cache.Get<StationKindData>(StationKindData.KeyOf("volatiles")).Description);
}

View file

@ -33,7 +33,15 @@ public sealed class StationServicesTests : IDisposable
foreach (var cell in bay.AllCoordinates) bay[cell] = true;
_cache.Upsert(new CargoBayData { Name = "Crate", Shape = new Shape(), InteriorShape = bay, Price = 20, Durability = 4 });
_cache.Upsert(new DockingBayData { Name = "Berth", Shape = new Shape(), InteriorShape = bay, MaxSize = new int2(2, 2), Price = 60, Durability = 6 });
_cache.Upsert(new SimpleCommodityData { Name = "Ore", Shape = new Shape(), Price = 7, MaxStack = 100 });
var ore = new SimpleCommodityData { Name = "Ore", Shape = new Shape(), Price = 7, MaxStack = 100 };
_cache.Upsert(ore);
// A belt yields Ore, so Ore is ore; no field yields Shells, so Shells is not.
_cache.Upsert(new FieldKindData
{
Name = "Asteroid", CrossSection = 4f, GenerationWeight = 1f,
Yields = { new FieldYield { Commodity = _cache.RefOf(ore), Abundance = 1f } }
});
_cache.Upsert(new SimpleCommodityData { Name = "Shells", Shape = new Shape(), Price = 3, MaxStack = 100 });
_cache.FlushAsync().Wait();
var settings = AuthoredSettings.Load(RestoredHullsTests.FindRepoRoot()).Read<GameplaySettings>("GameplaySettings");
@ -58,6 +66,8 @@ public sealed class StationServicesTests : IDisposable
};
}
private SimpleCommodity Shells(int quantity) => new SimpleCommodity { Data = _cache.RefOf<ItemData>(_cache.GetByName<SimpleCommodityData>("Shells")), Quantity = quantity };
private SimpleCommodity Ore(int quantity) => new SimpleCommodity { Data = _cache.RefOf<ItemData>(_cache.GetByName<SimpleCommodityData>("Ore")), Quantity = quantity };
// A ship with a cargo bay.
@ -506,61 +516,38 @@ public sealed class StationServicesTests : IDisposable
Assert.Equal(0, credits);
}
private SimpleCommodity Mined(int quantity)
{
var data = _cache.GetByName<SimpleCommodityData>("Ore");
return new SimpleCommodity { Data = _cache.RefOf<ItemData>(data), Quantity = quantity, Lot = _items.ExtractedLot(0, default, data) };
}
[Fact]
public void SellOreSellsOnlyMinedStacks()
public void SellOreSellsEveryOreStackMinedOrBought()
{
var ship = Ship();
var bay = ship.CargoBays.Single();
var mined = Mined(10);
var staged = Ore(5);
var bought = Ore(10);
var shells = Shells(5);
var gun = Mint("Gun");
Assert.True(bay.TryStore(mined));
Assert.True(bay.TryStore(staged));
Assert.True(bay.TryStore(bought));
Assert.True(bay.TryStore(shells));
Assert.True(bay.TryStore(gun));
var station = Station();
var value = StationServices.OreValue(_items, ship.CargoBays);
Assert.Equal(StationServices.SellPrice(_items, mined), value);
Assert.Equal(StationServices.SellPrice(_items, bought), value);
Assert.True(value > 0);
var credits = 100;
Assert.Equal(10, StationServices.TrySellOre(ship.CargoBays, station, ref credits));
Assert.Equal(100 + value, credits);
Assert.DoesNotContain(mined, bay.Cargo.Keys);
Assert.Contains(staged, bay.Cargo.Keys);
Assert.DoesNotContain(bought, bay.Cargo.Keys);
Assert.Contains(shells, bay.Cargo.Keys);
Assert.Contains(gun, bay.Cargo.Keys);
}
[Fact]
public void SellOreLeavesALottedCommodityThatWasNotExtracted()
{
var ship = Ship();
var bay = ship.CargoBays.Single();
var data = _cache.GetByName<SimpleCommodityData>("Ore");
var lot = _items.Lots.Add(new Lot { Design = _cache.RefOf<ItemData>(data), Origin = new Attributed(), Quality = 1 });
Assert.NotEqual(0, lot);
var traded = new SimpleCommodity { Data = _cache.RefOf<ItemData>(data), Quantity = 6, Lot = lot };
Assert.True(bay.TryStore(traded));
var credits = 100;
Assert.Equal(0, StationServices.OreValue(_items, ship.CargoBays));
Assert.Equal(0, StationServices.TrySellOre(ship.CargoBays, Station(), ref credits));
Assert.Equal(100, credits);
Assert.Contains(traded, bay.Cargo.Keys);
}
[Fact]
public void SellOreAcrossBaysAndShips()
{
var one = Ship();
var two = Ship();
var bays = one.CargoBays.Concat(two.CargoBays).ToArray();
Assert.True(one.CargoBays.Single().TryStore(Mined(30)));
Assert.True(two.CargoBays.Single().TryStore(Mined(40)));
Assert.True(one.CargoBays.Single().TryStore(Ore(30)));
Assert.True(two.CargoBays.Single().TryStore(Ore(40)));
var station = Station();
// Fill the station's bay so nothing more fits: the market absorbs the sale at full price all the same.
for (var i = 0; i < 20; i++) station.CargoBays.Single().TryStore(Ore(100));
@ -578,7 +565,7 @@ public sealed class StationServicesTests : IDisposable
{
var ship = Ship();
var bay = ship.CargoBays.Single();
Assert.True(bay.TryStore(Ore(5)));
Assert.True(bay.TryStore(Shells(5)));
Assert.True(bay.TryStore(Mint("Gun")));
var held = bay.Cargo.Count;
var credits = 100;