Files
yugioh-legacy-bots/tests/YGOLegacy.Bots.Tests/DecisionTests.cs
T
donnyandClaude Opus 5.5 c034354d12
CI / build (push) Successful in 57s
feat(decisions): host default, first brain, every choice through the seam; v0.2.0
BotHost.Decisions is the default decision maker for seats without a Decisions= token (builtin overrides it).
'first' answers from the request's JSON alone, sums and counters included. MSG_SORT_CHAIN, the field-first card
prompt and the effect prompt of an unknown card now ask the maker too (built-in answers unchanged); only
prompts with no choice are answered without asking, and seats with a maker log their counts per duel.

Tests: the built-in --seed 7 report equals the v0.1.0 release's (tests/fixture/expected, clocks aside); first vs
first, 100 duels: 15952 prompts asked, 0 handed back, 0 errors, unasked only 'chain: nothing to chain'.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 18:38:00 -07:00

114 lines
5.3 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using NUnit.Framework;
using YGOLegacy.Bots.Decisions;
namespace YGOLegacy.Bots.Tests
{
[TestFixture, NonParallelizable]
public class DecisionTests
{
private sealed class First : IDecisionMaker
{
public Decision Decide(DecisionRequest request) => Decision.Of(0);
}
[Test]
public void NoTokenAndBuiltInAreTheSeatsOwnBrain()
{
Assert.That(DecisionMakers.For(new DecisionSeat { name = "A" }), Is.Null);
Assert.That(DecisionMakers.For(new DecisionSeat { name = "A", decisions = "BuiltIn" }), Is.Null);
}
[Test]
public void AnUnknownNameIsLoggedAndGetsTheBuiltInBrain()
{
var host = new TestHost();
var log = BotHost.Log;
BotHost.Log = host;
try
{
Assert.That(DecisionMakers.For(new DecisionSeat { name = "A", decisions = "nosuch" }), Is.Null);
Assert.That(host.Lines, Has.Some.Contains("no decision maker named 'nosuch'"));
}
finally { BotHost.Log = log; }
}
[Test]
public void ARegisteredMakerIsBuiltPerSeat()
{
var seats = new List<string>();
DecisionMakers.Register("test-first", seat => { seats.Add(seat.name); return new First(); });
try
{
Assert.That(DecisionMakers.For(new DecisionSeat { name = "A", decisions = "test-first" }), Is.InstanceOf<First>());
Assert.That(seats, Is.EqualTo(new[] { "A" }));
Assert.Throws<System.ArgumentException>(() => DecisionMakers.Register("builtin", seat => new First()));
}
finally { DecisionMakers.Unregister("test-first"); }
}
[Test]
public void TheHostDefaultAppliesUnlessTheSeatSaysOtherwise()
{
try
{
BotHost.Decisions = DecisionMakers.First;
Assert.That(DecisionMakers.For(new DecisionSeat { name = "A" }), Is.InstanceOf<FirstDecisions>(), "no token: the host default");
Assert.That(DecisionMakers.For(new DecisionSeat { name = "A", decisions = "random" }), Is.InstanceOf<RandomDecisions>(), "the token wins");
Assert.That(DecisionMakers.For(new DecisionSeat { name = "A", decisions = "builtin" }), Is.Null, "builtin: the executor despite the default");
}
finally { BotHost.Decisions = null; }
}
[Test]
public void FirstSolvesSumsAndCountersFromTheOptions()
{
var first = new FirstDecisions();
var sum = new DecisionRequest { kind = DecisionKind.Sum, value = 8, exact = true, min = 1, max = 3 };
foreach (var (v1, v2) in new[] { (4, 0), (3, 0), (2, 6), (4, 0) })
sum.options.Add(new DecisionOption { index = sum.options.Count, value = v1, value2 = v2 });
Assert.That(first.Decide(sum).choose, Is.EqualTo(new[] { 0, 3 }), "the first options, in order, that make exactly 8");
sum.exact = false;
sum.value = 9;
Assert.That(first.Decide(sum).choose, Is.EqualTo(new[] { 0, 1, 2 }), "at least 9, and dropping the smallest falls short");
var counter = new DecisionRequest { kind = DecisionKind.Counter, value = 3 };
counter.options.Add(new DecisionOption { index = 0, value = 2 });
counter.options.Add(new DecisionOption { index = 1, value = 5 });
Assert.That(first.Decide(counter).counts, Is.EqualTo(new[] { 2, 1 }));
}
[Test]
public void RequestsAndAnswersAreJson()
{
var request = new DecisionRequest
{
kind = DecisionKind.Idle, seat = "A", prompt = 3,
options = { new DecisionOption { index = 0, action = "summon", card = new CardView { code = 11091375, location = "hand" } },
new DecisionOption { index = 1, action = "toEnd" } },
duel = new DuelView { turn = 1, phase = "main1", myTurn = true, me = new PlayerView { lp = 8000 }, opponent = new PlayerView { lp = 8000 } },
};
var back = JsonConvert.DeserializeObject<DecisionRequest>(JsonConvert.SerializeObject(request));
Assert.That(JsonConvert.SerializeObject(back), Is.EqualTo(JsonConvert.SerializeObject(request)));
Assert.That(back.options[0].card.code, Is.EqualTo(11091375));
var answer = JsonConvert.DeserializeObject<Decision>("{\"choose\":[1]}");
Assert.That(answer.choose, Is.EqualTo(new[] { 1 }));
}
[Test]
public void RandomAnswersAreInRange()
{
var random = new RandomDecisions(1);
var request = new DecisionRequest { kind = DecisionKind.Tribute, min = 2, max = 2, cancelable = true };
for (int i = 0; i < 4; ++i) request.options.Add(new DecisionOption { index = i, action = "card" });
for (int i = 0; i < 200; ++i)
{
var n = random.Decide(request).choose.Count;
Assert.That(n == 0 || n == 2, "cancel or exactly two, got " + n);
}
Assert.That(random.Decide(new DecisionRequest { kind = DecisionKind.Sum }), Is.Null, "sums are left to the brain");
}
}
}