Files
linuxdistro-roguelite/Assets/Tests/EditMode/PackageReadinessTests.cs
T

261 lines
13 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using KernelPanic.Combat;
using KernelPanic.Core;
using KernelPanic.Data;
using KernelPanic.Meta;
using KernelPanic.Run;
using KernelPanic.UI;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
public class PackageReadinessTests
{
private const BindingFlags Private = BindingFlags.Instance | BindingFlags.NonPublic;
private readonly List<UnityEngine.Object> _temporary = new();
private readonly List<PackageActivation> _activations = new();
private GameObject _owner;
private CombatManager _combat;
private RunManager _run;
private UnityEngine.Random.State _random;
private bool _motion;
private CombatantState Player => _combat.PlayerState;
private static PackageDatabase Packages => AssetDatabase.LoadAssetAtPath<PackageDatabase>("Assets/Data/Packages/PackageDatabase.asset");
private static object Call(object target, string method, params object[] args) => target.GetType().GetMethod(method, Private).Invoke(target, args);
private static void Set(object target, string field, object value) => target.GetType().GetField(field, Private).SetValue(target, value);
[SetUp]
public void SetUp()
{
_random = UnityEngine.Random.state;
_motion = UIPreferences.ReducedMotion;
UIPreferences.ReducedMotion = true;
_owner = new GameObject("Package readiness integration");
_run = _owner.AddComponent<RunManager>();
_combat = _owner.AddComponent<CombatManager>();
Call(_combat, "OnDisable"); Call(_combat, "Awake"); Call(_combat, "OnEnable");
_combat.PackageActivated += _activations.Add;
}
[TearDown]
public void TearDown()
{
_combat.PackageActivated -= _activations.Add;
Call(_combat, "OnDisable");
UnityEngine.Object.DestroyImmediate(_owner);
foreach (var item in _temporary) UnityEngine.Object.DestroyImmediate(item);
_temporary.Clear();
_activations.Clear();
UnityEngine.Random.state = _random;
UIPreferences.ReducedMotion = _motion;
}
private void Configure(string distro, int version, int level, params string[] ids)
{
var definition = AssetDatabase.LoadAssetAtPath<DistroDefinition>($"Assets/Data/Distros/{distro}.asset");
var config = new RunConfig(definition, Language.C, Language.Haskell, Array.Empty<CardDefinition>(),
ids.Select(id => new PackageInstance(Packages.FindById(id), level)).ToArray(), 17, version);
_run.StartRun(config);
Set(_combat, "_runConfig", config);
Set(_combat, "_runManager", _run);
BeginWave();
BeginRound();
}
private static void Finish(IEnumerator routine)
{
int steps = 0;
while (routine.MoveNext())
{
Assert.That(++steps, Is.LessThan(2000));
if (routine.Current is IEnumerator child) Finish(child);
}
}
private void BeginWave()
{
Finish((IEnumerator)Call(_combat, "StartWaveSequenced", false));
Set(_combat, "currentPhase", TurnPhase.Execute);
Player.ForceMaxRolls = true;
}
private void BeginRound()
{
Finish((IEnumerator)Call(_combat, "AllocateTurnSequenced"));
Set(_combat, "currentPhase", TurnPhase.Execute);
}
private CardInstance Card(int cost = 2, ResolutionTrack track = ResolutionTrack.Native,
string id = "package_readiness_test", Language language = Language.C)
{
var definition = ScriptableObject.CreateInstance<CardDefinition>();
_temporary.Add(definition);
Set(definition, "id", id); Set(definition, "cycleCost", cost);
Set(definition, "resolutionTrack", track); Set(definition, "language", language);
var card = new CardInstance(definition);
Assert.That(_combat.HandController.Add(card), Is.True);
return card;
}
private PackageCombatReadout Readout(string suffix) => _combat.GetPackageReadouts().Single(row => row.Definition.Id == "pkg_gentoo_" + suffix);
private int Activations(string suffix, string fragment = null) => _activations.Count(item =>
item.PackageId == "pkg_gentoo_" + suffix && (fragment == null || item.Message.Contains(fragment)));
[TestCase("Gentoo", 1, 0)] [TestCase("Gentoo", 1, 5)]
[TestCase("Gentoo", 5, 0)] [TestCase("Gentoo", 5, 5)]
[TestCase("Ubuntu", 1, 0)] [TestCase("Ubuntu", 5, 5)]
public void EquipmentReadoutsAndCostQueriesAreAccurateAndDoNotMutateCombat(string distro, int version, int level)
{
Configure(distro, version, level, "pkg_gentoo_portage", "pkg_gentoo_ccache", "pkg_gentoo_openrc");
var card = Card();
var random = UnityEngine.Random.state;
int shield = Player.Shield, cycles = Player.Cycles, uptime = Player.CurrentUptime;
int compilation = _combat.Compilation.Level, lastAdvanced = _combat.Compilation.LastAdvancedRound;
int events = _activations.Count;
var hand = _combat.HandController.Cards.ToArray();
for (int i = 0; i < 20; i++)
{
Assert.That(_combat.GetPackageReadouts(), Has.Count.EqualTo(3));
Assert.That(Readout("ccache").State, Is.EqualTo("ready"));
Assert.That(Readout("portage").Details, Does.Contain($"+{4 + level * 2} maximum Uptime"));
Assert.That(Readout("portage").Details.Contains("Shield"), Is.EqualTo(distro == "Gentoo"));
Assert.That(Readout("openrc").Details, Does.Contain($"+{2 + level} Shield"));
Assert.That(Readout("openrc").Details.Contains("Compilation"), Is.EqualTo(distro == "Gentoo"));
Assert.That(_combat.GetDisplayedPlayCost(card), Is.EqualTo(1));
Assert.That(_combat.GetCardCostBreakdown(card), Does.Contain("Cycles: 1 (base 2)").And.Contain("ccache: -1"));
Assert.That(_combat.PreviewCardPlayShield(card), Is.EqualTo(level));
}
Assert.That(UnityEngine.Random.state, Is.EqualTo(random));
Assert.That(Player.Shield, Is.EqualTo(shield));
Assert.That(Player.Cycles, Is.EqualTo(cycles));
Assert.That(Player.CurrentUptime, Is.EqualTo(uptime));
Assert.That(_combat.Compilation.Level, Is.EqualTo(compilation));
Assert.That(_combat.Compilation.LastAdvancedRound, Is.EqualTo(lastAdvanced));
Assert.That(_combat.CardsPlayedThisRound, Is.Zero);
Assert.That(_combat.HandController.Cards, Is.EqualTo(hand));
Assert.That(_activations.Count, Is.EqualTo(events));
Assert.That(Activations("portage"), Is.EqualTo(distro == "Gentoo" ? 1 : 0));
Assert.That(card.QueuePlayCount, Is.Zero);
Assert.That(card.MagnitudeBonus, Is.Zero);
}
[TestCase("Gentoo", 0)] [TestCase("Gentoo", 5)]
[TestCase("Ubuntu", 0)] [TestCase("Ubuntu", 5)]
public void FailedPlayPreservesReadinessAndOnlyTheFirstAcceptedPlayActivates(string distro, int level)
{
Configure(distro, 1, level, "pkg_gentoo_ccache");
var card = Card();
var before = Readout("ccache");
Player.Cycles = 0;
Assert.That(_combat.PlayCard(card), Is.False);
Assert.That(Readout("ccache").State, Is.EqualTo("ready"));
Assert.That(_combat.PreviewCardPlayShield(card), Is.EqualTo(level));
Assert.That(Activations("ccache"), Is.Zero);
Player.Cycles = 10;
Assert.That(_combat.PlayCard(card), Is.True);
Assert.That(Readout("ccache").State, Is.EqualTo("used"));
Assert.That(before.State, Is.EqualTo("ready"), "Published readouts are immutable snapshots.");
Assert.That(Player.Shield, Is.EqualTo(level));
Assert.That(Activations("ccache"), Is.EqualTo(1));
var second = Card();
Assert.That(_combat.PreviewCardPlayShield(second), Is.Zero);
Assert.That(_combat.GetCardCostBreakdown(second), Does.Contain("Cycles: 2 (base 2)").And.Not.Contain("ccache: -1"));
Assert.That(_combat.PlayCard(second), Is.True);
Assert.That(Player.Shield, Is.EqualTo(level));
Assert.That(Activations("ccache"), Is.EqualTo(1));
BeginRound();
Assert.That(Readout("ccache").State, Is.EqualTo("ready"));
Assert.That(_combat.PreviewCardPlayShield(Card()), Is.EqualTo(level));
BeginWave(); BeginRound();
Assert.That(Readout("ccache").State, Is.EqualTo("ready"));
Assert.That(_combat.GetDisplayedPlayCost(Card()), Is.EqualTo(1));
}
[TestCase(ResolutionTrack.Native)]
[TestCase(ResolutionTrack.InterpreterQueue)]
[TestCase(ResolutionTrack.LazyStack)]
public void MultiEffectCardProducesOneActivationIncludingFreeAndDeferredPlay(ResolutionTrack track)
{
Configure("Gentoo", 5, 5, "pkg_gentoo_ccache");
var card = Card(0, track, "shop_py_async_def", Language.Python);
Assert.That(_combat.PreviewCardPlayShield(card), Is.EqualTo(5));
Assert.That(_combat.PlayCard(card), Is.True);
Assert.That(Activations("ccache"), Is.EqualTo(1));
Assert.That(Player.Shield, Is.EqualTo(5));
if (track == ResolutionTrack.InterpreterQueue) Finish((IEnumerator)Call(_combat, "InterpretQueueSequenced"));
if (track == ResolutionTrack.LazyStack) Finish((IEnumerator)Call(_combat, "EvaluateLazyStackSequenced"));
Assert.That(Activations("ccache"), Is.EqualTo(1));
Assert.That(Player.Shield, Is.EqualTo(5));
Assert.That(Readout("ccache").State, Is.EqualTo("used"));
}
[TestCase(1, 0)] [TestCase(1, 5)] [TestCase(5, 0)] [TestCase(5, 5)]
public void PendingCycleAndCompilationCapFollowRoundStateAndWaveReset(int version, int level)
{
Configure("Gentoo", version, level, "pkg_gentoo_portage", "pkg_gentoo_ccache", "pkg_gentoo_openrc");
int initial = _combat.Compilation.Level;
Assert.That(Readout("ccache").Details, Does.Not.Contain("Next round: +1 Cycle"));
Call(_combat, "GarbageCollect");
Assert.That(Readout("ccache").Details, Does.Contain("Next round: +1 Cycle"));
int before = _activations.Count;
BeginRound();
Assert.That(Player.Cycles, Is.EqualTo(Player.MaxCycles + 1));
Assert.That(Activations("ccache", "Cycle from Compilation"), Is.EqualTo(1));
Assert.That(Activations("openrc", "Shield"), Is.EqualTo(1));
Assert.That(Activations("openrc", "Compilation ("), Is.EqualTo(1));
Assert.That(_activations.Count - before, Is.EqualTo(3));
Assert.That(_combat.Compilation.Level, Is.EqualTo(initial + 2));
Assert.That(Player.Shield, Is.EqualTo(4 + 2 * level + 2 + level));
int shield = Player.Shield, cycle = Player.Cycles;
Call(_combat, "ApplyPackageTurnStartEffects");
Call(_combat, "ApplyPackageWaveStartEffects");
Assert.That(Player.Shield, Is.EqualTo(shield));
Assert.That(Player.Cycles, Is.EqualTo(cycle));
Assert.That(_activations.Count - before, Is.EqualTo(3));
// OpenRC alone eventually reaches the real version cap. No fabricated rounds or animations.
for (int i = 0; i < 10; i++) BeginRound();
Assert.That(_combat.Compilation.Level, Is.EqualTo(_combat.Compilation.Maximum));
Assert.That(Readout("openrc").Details, Does.Contain("Compilation capped"));
Assert.That(Readout("ccache").Details, Does.Not.Contain("Next round: +1 Cycle"));
Assert.That(Player.Cycles, Is.EqualTo(Player.MaxCycles));
int compilationEvents = Activations("openrc", "Compilation (");
int cycleEvents = Activations("ccache", "Cycle from Compilation");
Call(_combat, "GarbageCollect"); BeginRound();
Assert.That(Activations("openrc", "Compilation ("), Is.EqualTo(compilationEvents));
Assert.That(Activations("ccache", "Cycle from Compilation"), Is.EqualTo(cycleEvents));
BeginWave(); BeginRound();
Assert.That(_combat.Compilation.Level, Is.EqualTo(initial));
Assert.That(Player.Shield, Is.EqualTo(4 + level * 2));
Assert.That(Player.Cycles, Is.EqualTo(Player.MaxCycles));
Assert.That(Readout("ccache").State, Is.EqualTo("ready"));
Assert.That(Readout("openrc").Details, Does.Not.Contain("Compilation capped"));
Assert.That(Activations("portage"), Is.EqualTo(2));
}
[Test]
public void CostBreakdownIncludesExistingJavaAndFedoraModifiersWithoutConsumingThem()
{
Configure("Fedora", 3, 5, "pkg_gentoo_ccache");
var card = Card(5, ResolutionTrack.Native, "lang_java_public_static_main", Language.Java);
card.ApplyCostUpgrade(-1);
card.TemporaryCostDelta = -1;
Set(_combat, "_javaCardsPlayedThisCombat", 2);
string explanation = _combat.GetCardCostBreakdown(card);
Assert.That(explanation, Does.Contain("upgrades: -1").And.Contain("temporary: -1")
.And.Contain("Java warmup: -1").And.Contain("ccache: -1").And.Contain("Bleeding Edge: -1"));
Assert.That(_combat.GetDisplayedPlayCost(card), Is.Zero);
Assert.That(_combat.CardsPlayedThisRound, Is.Zero);
Assert.That(Activations("ccache"), Is.Zero);
}
}