|
| 1 | +using Quantumwake.Data; |
| 2 | + |
| 3 | +namespace Quantumwake.Tests; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// What an item is worth, as opposed to what it costs at the one terminal |
| 7 | +/// selling it cheapest. The two answer different questions and this install |
| 8 | +/// has an item where they differ tenfold. |
| 9 | +/// </summary> |
| 10 | +public class UexTypicalPriceTests : IDisposable |
| 11 | +{ |
| 12 | + private readonly string _directory = |
| 13 | + Path.Combine(Path.GetTempPath(), $"qw-typical-{Guid.NewGuid():N}"); |
| 14 | + |
| 15 | + public UexTypicalPriceTests() => Directory.CreateDirectory(_directory); |
| 16 | + |
| 17 | + public void Dispose() |
| 18 | + { |
| 19 | + if (Directory.Exists(_directory)) |
| 20 | + Directory.Delete(_directory, recursive: true); |
| 21 | + |
| 22 | + GC.SuppressFinalize(this); |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// The cache only loads as a set: prices.json is the gate, and the ids and |
| 27 | + /// terminals beside it are read unguarded, so a directory holding item |
| 28 | + /// prices alone loads nothing at all. |
| 29 | + /// </summary> |
| 30 | + private void SeedTheCacheItLoadsAsASet() |
| 31 | + { |
| 32 | + File.WriteAllText(Path.Combine(_directory, "prices.json"), "{}"); |
| 33 | + File.WriteAllText(Path.Combine(_directory, "commodity-ids.json"), "{}"); |
| 34 | + File.WriteAllText(Path.Combine(_directory, "terminals.json"), "[]"); |
| 35 | + } |
| 36 | + |
| 37 | + private UexData Seeded(string uuid, decimal cheapest, params decimal[] terminals) |
| 38 | + { |
| 39 | + SeedTheCacheItLoadsAsASet(); |
| 40 | + |
| 41 | + File.WriteAllText( |
| 42 | + Path.Combine(_directory, "item-prices.json"), |
| 43 | + $$"""{"{{uuid}}":{{cheapest}}}"""); |
| 44 | + |
| 45 | + var rows = string.Join(",", terminals.Select((b, i) => |
| 46 | + $$"""{"Terminal":"T{{i}}","Buy":{{b}}}""")); |
| 47 | + |
| 48 | + File.WriteAllText( |
| 49 | + Path.Combine(_directory, "item-market.json"), |
| 50 | + $$"""{"{{uuid}}":[{{rows}}]}"""); |
| 51 | + |
| 52 | + return new UexData(_directory); |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// The MaxLift Tractor Beam as this install sees it: stocked near 19,175 |
| 57 | + /// almost everywhere and at 1,975 in one place. The cheapest understates it |
| 58 | + /// tenfold, and a mean would still be pulled down by the odd row. |
| 59 | + /// </summary> |
| 60 | + [Fact] |
| 61 | + public void One_odd_terminal_does_not_move_the_typical_price() |
| 62 | + { |
| 63 | + var uex = Seeded("beam", 1975, 1975, 19175, 19175, 19175, 19175); |
| 64 | + |
| 65 | + Assert.Equal(1975, uex.ItemPrice("beam")); |
| 66 | + Assert.Equal(19175, uex.TypicalItemPrice("beam")); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public void An_even_number_of_terminals_takes_the_middle_pair() |
| 71 | + { |
| 72 | + var uex = Seeded("part", 100, 100, 200, 300, 400); |
| 73 | + |
| 74 | + Assert.Equal(250, uex.TypicalItemPrice("part")); |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// A price with no per-terminal rows behind it is still a price. Falling |
| 79 | + /// through to null would drop items over a gap in the market table alone. |
| 80 | + /// </summary> |
| 81 | + [Fact] |
| 82 | + public void A_price_with_no_terminal_rows_falls_back_to_the_cheapest() |
| 83 | + { |
| 84 | + SeedTheCacheItLoadsAsASet(); |
| 85 | + File.WriteAllText(Path.Combine(_directory, "item-prices.json"), """{"lonely":4200}"""); |
| 86 | + |
| 87 | + Assert.Equal(4200, new UexData(_directory).TypicalItemPrice("lonely")); |
| 88 | + } |
| 89 | + |
| 90 | + [Fact] |
| 91 | + public void An_item_nothing_stocks_has_no_typical_price() |
| 92 | + { |
| 93 | + Assert.Null(Seeded("beam", 1975, 1975).TypicalItemPrice("unstocked")); |
| 94 | + Assert.Null(Seeded("beam", 1975, 1975).TypicalItemPrice(null)); |
| 95 | + } |
| 96 | +} |
0 commit comments