using NUnit.Framework; using SVSim.BattleNode.Sessions.Dispatch; namespace SVSim.UnitTests.BattleNode.Sessions; [TestFixture] public class KnownListBuilderTests { // orderList as it arrives in a RawBody: a list of single-key op dicts. private static List OrderListMove(int idx, int from, int to) => new() { new Dictionary { ["move"] = new Dictionary { ["idx"] = new List { (long)idx }, ["isSelf"] = 1L, ["from"] = (long)from, ["to"] = (long)to, } } }; [Test] public void ExtractMoveTo_returns_to_for_matching_idx() { var to = KnownListBuilder.ExtractMoveTo(OrderListMove(3, 10, 20), playIdx: 3); Assert.That(to, Is.EqualTo(20)); } [Test] public void ExtractMoveTo_returns_null_when_no_move_op_matches() { Assert.That(KnownListBuilder.ExtractMoveTo(OrderListMove(3, 10, 20), playIdx: 99), Is.Null); Assert.That(KnownListBuilder.ExtractMoveTo(null, playIdx: 3), Is.Null); } [Test] public void ExtractMoveTo_returns_first_matching_move_op() { // A real PlayActions can carry several move ops; the played card's move comes first, // later ops (token add/alter) target other idxs. Confirm first-match-wins, not last. var orderList = new List { new Dictionary { ["move"] = new Dictionary { ["idx"] = new List { 3L }, ["isSelf"] = 1L, ["from"] = 10L, ["to"] = 30L, } }, new Dictionary { ["move"] = new Dictionary { ["idx"] = new List { 31L, 32L }, ["isSelf"] = 1L, ["from"] = 0L, ["to"] = 40L, } }, }; Assert.That(KnownListBuilder.ExtractMoveTo(orderList, playIdx: 3), Is.EqualTo(30)); Assert.That(KnownListBuilder.ExtractMoveTo(orderList, playIdx: 31), Is.EqualTo(40)); } [Test] public void BuildPlayedCard_returns_null_for_deck_card_with_no_matching_move_op() { // idx is in the deck, but the orderList has no move op for it → can't synthesize. var deckMap = new Dictionary { [3] = 128821011L }; var entry = KnownListBuilder.BuildPlayedCard(deckMap, playIdx: 3, orderList: OrderListMove(7, 10, 20)); Assert.That(entry, Is.Null); } [Test] public void BuildPlayedCard_synthesizes_entry_for_deck_card() { var deckMap = new Dictionary { [3] = 128821011L }; var entry = KnownListBuilder.BuildPlayedCard(deckMap, playIdx: 3, orderList: OrderListMove(3, 10, 20)); Assert.That(entry, Is.Not.Null); Assert.That(entry!.Idx, Is.EqualTo(3)); Assert.That(entry.CardId, Is.EqualTo(128821011L)); Assert.That(entry.To, Is.EqualTo(20)); Assert.That(entry.Spellboost, Is.EqualTo(0)); Assert.That(entry.AttachTarget, Is.EqualTo("")); } [Test] public void BuildPlayedCard_returns_null_for_token_idx_not_in_deck() { var deckMap = new Dictionary { [3] = 128821011L }; var entry = KnownListBuilder.BuildPlayedCard(deckMap, playIdx: 31, orderList: OrderListMove(31, 10, 20)); Assert.That(entry, Is.Null); } [Test] public void RenameTargets_passes_isSelf_through_verbatim() { var targetList = new List { new Dictionary { ["targetIdx"] = 8L, ["isSelf"] = 0L }, }; var renamed = KnownListBuilder.RenameTargets(targetList); Assert.That(renamed, Is.Not.Null); Assert.That(renamed!.Count, Is.EqualTo(1)); Assert.That(renamed[0].TargetIdx, Is.EqualTo(8)); Assert.That(renamed[0].IsSelf, Is.EqualTo(0)); } [Test] public void RenameTargets_returns_null_for_missing_or_empty() { Assert.That(KnownListBuilder.RenameTargets(null), Is.Null); Assert.That(KnownListBuilder.RenameTargets(new List()), Is.Null); } // An add op as it arrives in a RawBody: { "add": { "idx": [..], "isSelf": n, "card": { "cardId": n } } } private static Dictionary AddOp(long[] idxs, long cardId, long isSelf = 1) => new() { ["add"] = new Dictionary { ["idx"] = idxs.Select(i => (object?)i).ToList(), ["isSelf"] = isSelf, ["card"] = new Dictionary { ["cardId"] = cardId }, } }; [Test] public void MineAddOps_yields_idx_to_cardId_for_every_idx_in_an_add_op() { var orderList = new List { AddOp(new[] { 31L, 32L }, 900111010L) }; var mined = KnownListBuilder.MineAddOps(orderList).ToList(); Assert.That(mined, Is.EquivalentTo(new[] { (31, 900111010L), (32, 900111010L) })); } [Test] public void MineAddOps_skips_add_ops_for_the_opponent_isSelf_0() { // A card given to the opponent (isSelf:0) belongs in the other side's map — deferred. var orderList = new List { AddOp(new[] { 31L }, 900111010L, isSelf: 0) }; Assert.That(KnownListBuilder.MineAddOps(orderList), Is.Empty); } [Test] public void MineAddOps_skips_choice_adds_with_no_concrete_cardId() { // { "add": { "idx":[46], "card": { "candidates":[...] }, "isChoice":"1" } } — identity undetermined. var orderList = new List { new Dictionary { ["add"] = new Dictionary { ["idx"] = new List { 46L }, ["isSelf"] = 1L, ["card"] = new Dictionary { ["candidates"] = new List { 810041260L, 101041020L }, }, ["isChoice"] = "1", } } }; Assert.That(KnownListBuilder.MineAddOps(orderList), Is.Empty); } [Test] public void MineAddOps_skips_copy_token_adds_with_baseIdx_and_no_cardId() { // RegisterCopyToken.MakeCardData → { "baseIdx": N, "isPremium": 0 } — no cardId, deferred. var orderList = new List { new Dictionary { ["add"] = new Dictionary { ["idx"] = new List { 33L }, ["isSelf"] = 1L, ["card"] = new Dictionary { ["baseIdx"] = 12L, ["isPremium"] = 0L }, } } }; Assert.That(KnownListBuilder.MineAddOps(orderList), Is.Empty); } [Test] public void MineAddOps_ignores_non_add_ops_and_null() { Assert.That(KnownListBuilder.MineAddOps(OrderListMove(3, 10, 20)), Is.Empty); Assert.That(KnownListBuilder.MineAddOps(null), Is.Empty); } [Test] public void MineAddOps_yields_from_multiple_add_ops_in_one_orderList() { var orderList = new List { new Dictionary { ["move"] = new Dictionary { ["idx"] = new List { 3L }, ["isSelf"] = 1L, ["from"] = 10L, ["to"] = 30L } }, AddOp(new[] { 31L }, 900111010L), AddOp(new[] { 32L }, 900811090L), }; var mined = KnownListBuilder.MineAddOps(orderList).ToList(); Assert.That(mined, Is.EquivalentTo(new[] { (31, 900111010L), (32, 900811090L) })); } }