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 GentooPackageTests { const BindingFlags Private = BindingFlags.NonPublic | BindingFlags.Instance; GameObject owner; CombatManager combat; RunManager run; UnityEngine.Random.State random; bool motion; readonly List temporary = new(); static T Asset(string path) where T : UnityEngine.Object => AssetDatabase.LoadAssetAtPath(path); static PackageDatabase Database => Asset("Assets/Data/Packages/PackageDatabase.asset"); static PackageDefinition Package(string suffix) => Database.FindById("pkg_gentoo_" + suffix); static object Call(object target, string method, params object[] args) => target.GetType().GetMethod(method, Private).Invoke(target, args); static void Set(object target, string field, object value) => target.GetType().GetField(field, Private).SetValue(target, value); CombatantState Player => combat.PlayerState; [SetUp] public void Setup() { random = UnityEngine.Random.state; motion = UIPreferences.ReducedMotion; UIPreferences.ReducedMotion = true; owner = new GameObject("Gentoo equipment integration"); run = owner.AddComponent(); combat = owner.AddComponent(); Call(combat, "OnDisable"); Call(combat, "Awake"); Call(combat, "OnEnable"); } [TearDown] public void Cleanup() { Call(combat, "OnDisable"); UnityEngine.Object.DestroyImmediate(owner); foreach (var item in temporary) UnityEngine.Object.DestroyImmediate(item); temporary.Clear(); UnityEngine.Random.state = random; UIPreferences.ReducedMotion = motion; } void Configure(string distro, int version, int level, params string[] packages) { var definition = Asset($"Assets/Data/Distros/{distro}.asset"); var config = new RunConfig(definition, Language.C, Language.Haskell, Array.Empty(), packages.Select(p => new PackageInstance(Package(p), level)).ToArray(), 17, version); run.StartRun(config); Set(combat, "_runConfig", config); Set(combat, "_runManager", run); BeginWave(); } 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); } } void BeginWave() { Finish((IEnumerator)Call(combat, "StartWaveSequenced", false)); Set(combat, "currentPhase", TurnPhase.Execute); Player.ForceMaxRolls = true; } void BeginRound() { Finish((IEnumerator)Call(combat, "AllocateTurnSequenced")); Set(combat, "currentPhase", TurnPhase.Execute); } CardInstance Card(int cost = 2, ResolutionTrack track = ResolutionTrack.Native, string id = "equipment_test") { var definition = ScriptableObject.CreateInstance(); temporary.Add(definition); Set(definition, "id", id); Set(definition, "cycleCost", cost); Set(definition, "resolutionTrack", track); Set(definition, "language", Language.C); var card = new CardInstance(definition); Assert.That(combat.HandController.Add(card), Is.True); return card; } [TestCase("Gentoo", 1, 0)] [TestCase("Gentoo", 5, 5)] [TestCase("Ubuntu", 1, 0)] [TestCase("Ubuntu", 5, 5)] public void PortageStatsAndShieldAreAppliedOncePerWave(string distro, int version, int level) { Configure(distro, version, level, "portage"); int bonus = 4 + 2 * level; Assert.That(Player.MaxUptime, Is.EqualTo(combat.RunConfig.Distro.BaseUptime + bonus)); int shield = distro == "Gentoo" ? bonus : 0; Assert.That(Player.Shield, Is.EqualTo(shield)); Call(combat, "ApplyPackageWaveStartEffects"); Assert.That(Player.Shield, Is.EqualTo(shield)); BeginRound(); BeginRound(); Assert.That(Player.Shield, Is.EqualTo(shield)); BeginWave(); Assert.That(Player.Shield, Is.EqualTo(shield)); } [TestCase("Gentoo", 0)] [TestCase("Gentoo", 5)] [TestCase("Ubuntu", 0)] [TestCase("Ubuntu", 5)] public void CcacheDiscountAndShieldApplyOnlyToFirstAcceptedPlay(string distro, int level) { Configure(distro, 1, level, "ccache"); BeginRound(); var first = Card(); Assert.That(combat.GetEffectiveCardCost(first), Is.EqualTo(1)); Player.Cycles = 0; Assert.That(combat.PlayCard(first), Is.False); Assert.That(Player.Shield, Is.Zero); Assert.That(combat.GetEffectiveCardCost(first), Is.EqualTo(1)); Player.Cycles = 10; Assert.That(combat.PlayCard(first), Is.True); Assert.That(Player.Cycles, Is.EqualTo(9)); Assert.That(Player.Shield, Is.EqualTo(level)); var second = Card(); Assert.That(combat.GetEffectiveCardCost(second), Is.EqualTo(2)); Assert.That(combat.PlayCard(second), Is.True); Assert.That(Player.Shield, Is.EqualTo(level)); Call(combat, "GarbageCollect"); BeginRound(); Assert.That(Player.Cycles, Is.EqualTo(Player.MaxCycles + (distro == "Gentoo" ? 1 : 0))); Assert.That(combat.GetEffectiveCardCost(Card()), Is.EqualTo(1)); } [TestCase(ResolutionTrack.Native)] [TestCase(ResolutionTrack.InterpreterQueue)] [TestCase(ResolutionTrack.LazyStack)] public void CcacheMultiEffectAndDeferredCardsGrantShieldOnce(ResolutionTrack track) { Configure("Gentoo", 5, 5, "ccache"); BeginRound(); // Draw and deferred Cycles: several effects must still award Shield only once. var card = Card(0, track, "shop_py_async_def"); combat.SelectEnemy(0); Assert.That(combat.PlayCard(card), Is.True); Assert.That(Player.Shield, Is.EqualTo(5)); if (track == ResolutionTrack.LazyStack) Finish((IEnumerator)Call(combat, "EvaluateLazyStackSequenced")); else if (track == ResolutionTrack.InterpreterQueue) Finish((IEnumerator)Call(combat, "InterpretQueueSequenced")); Assert.That(Player.Shield, Is.EqualTo(5)); Assert.That(combat.GetEffectiveCardCost(Card()), Is.EqualTo(2), "A free first play consumes the discount"); } [TestCase("Gentoo", 1, 0)] [TestCase("Gentoo", 5, 5)] [TestCase("Ubuntu", 1, 0)] [TestCase("Ubuntu", 5, 5)] public void OpenRCUsesRoundBoundariesAndVersionCap(string distro, int version, int level) { Configure(distro, version, level, "openrc"); BeginRound(); int initial = combat.Compilation.Level; Assert.That(Player.Shield, Is.Zero); BeginRound(); Assert.That(Player.Shield, Is.EqualTo(2 + level)); Assert.That(combat.Compilation.Level, Is.EqualTo(distro == "Gentoo" ? initial + 1 : 0)); Call(combat, "ApplyPackageTurnStartEffects"); Assert.That(Player.Shield, Is.EqualTo(2 + level)); for (int i = 0; i < 10; i++) BeginRound(); Assert.That(combat.Compilation.Level, Is.EqualTo(combat.Compilation.Maximum)); BeginWave(); BeginRound(); Assert.That(Player.Shield, Is.Zero); Assert.That(combat.Compilation.Level, Is.EqualTo(initial)); } [TestCase(1, 0)] [TestCase(1, 5)] [TestCase(5, 0)] [TestCase(5, 5)] public void AllThreeTrackActualAdvancementIncludingOpenRCAtCap(int version, int level) { Configure("Gentoo", version, level, "portage", "ccache", "openrc"); BeginRound(); int expectedLevel = version == 5 ? 1 : 0; int cap = version == 5 ? 6 : 4; Assert.That(Player.Shield, Is.EqualTo(4 + 2 * level)); bool previousAdvanced = false; for (int round = 1; round <= 8; round++) { if (round > 1) { BeginRound(); Assert.That(Player.Cycles, Is.EqualTo(Player.MaxCycles + (previousAdvanced ? 1 : 0))); Assert.That(Player.Shield, Is.EqualTo(2 + level)); } bool openRcAdvanced = round > 1 && expectedLevel < cap; if (openRcAdvanced) expectedLevel++; Assert.That(combat.Compilation.Level, Is.EqualTo(expectedLevel)); Player.Cycles = 10; combat.HandController.Clear(); // Round allocation may redraw previous test cards. int before = Player.Shield; Assert.That(combat.PlayCard(Card()), Is.True); Assert.That(Player.Shield, Is.EqualTo(before + level)); Assert.That(combat.PlayCard(Card()), Is.True); Assert.That(Player.Shield, Is.EqualTo(before + level)); bool naturalAdvanced = expectedLevel < cap; Call(combat, "GarbageCollect"); if (naturalAdvanced) expectedLevel++; Assert.That(combat.Compilation.Level, Is.EqualTo(expectedLevel)); previousAdvanced = openRcAdvanced || naturalAdvanced; Player.Shield = 0; } BeginWave(); BeginRound(); Assert.That(Player.Cycles, Is.EqualTo(Player.MaxCycles)); Assert.That(Player.Shield, Is.EqualTo(4 + 2 * level)); } [TestCase(0)] [TestCase(1)] [TestCase(2)] [TestCase(3)] [TestCase(4)] [TestCase(5)] public void EveryLevelHasIndependentScalingAndAccurateCollectionText(int level) { var format = typeof(CollectionScreenController).GetMethod("FormatPackageEffect", BindingFlags.Static | BindingFlags.NonPublic); foreach (string distro in new[] { "gentoo", "ubuntu" }) { string portage = (string)format.Invoke(null, new object[] { Package("portage"), level, distro }); Assert.That(portage, Does.Contain($"+{4 + 2 * level} max Uptime")); Assert.That(portage.Contains("Shield"), Is.EqualTo(distro == "gentoo")); string ccache = (string)format.Invoke(null, new object[] { Package("ccache"), level, distro }); Assert.That(ccache, Does.Contain("costs 1 less")); if (level > 0) Assert.That(ccache, Does.Contain($"+{level} Shield")); Assert.That(ccache.Contains("Compilation advanced"), Is.EqualTo(distro == "gentoo")); string openrc = (string)format.Invoke(null, new object[] { Package("openrc"), level, distro }); Assert.That(openrc, Does.Contain($"+{2 + level} Shield")); Assert.That(openrc.Contains("+1 Compilation"), Is.EqualTo(distro == "gentoo")); } } [Test] public void ExistingShieldPackageAndGentooPassiveDoNotDoubleScaleEquipment() { Configure("Gentoo", 5, 5, "portage", "ccache"); var packages = combat.RunConfig.EquippedPackages.ToList(); packages.Add(new PackageInstance(Database.FindById("pkg_fedora_selinux"), 0)); var config = new RunConfig(combat.RunConfig.Distro, Language.C, Language.Haskell, Array.Empty(), packages, 17, 5); Set(combat, "_runConfig", config); BeginRound(); Player.Shield = 0; Assert.That(combat.PlayCard(Card(0)), Is.True); Assert.That(Player.Shield, Is.EqualTo(8), "5 equipment Shield + 3 SELinux, not Compilation-scaled card Shield"); combat.GrantPlayerShield(1, "second grant"); Assert.That(Player.Shield, Is.EqualTo(9)); } [TestCase(0)] [TestCase(5)] public void AllThreeOffDistroDoNotActivateCompilation(int level) { Configure("Ubuntu", 5, level, "portage", "ccache", "openrc"); BeginRound(); Assert.That(Player.Shield, Is.Zero); Assert.That(Player.MaxUptime, Is.EqualTo(combat.RunConfig.Distro.BaseUptime + 4 + 2 * level)); Assert.That(combat.PlayCard(Card(1)), Is.True); Assert.That(Player.Shield, Is.EqualTo(level)); Call(combat, "GarbageCollect"); BeginRound(); Assert.That(Player.Shield, Is.EqualTo(level + 2 + level)); Assert.That(Player.Cycles, Is.EqualTo(Player.MaxCycles)); Assert.That(combat.Compilation.Active, Is.False); } [Test] public void CcacheStacksWithFedoraDiscountWithoutChangingIt() { Configure("Fedora", 1, 5, "ccache"); BeginRound(); foreach (var enemy in combat.Enemies) { enemy.State.MaxUptime = 1000; enemy.State.CurrentUptime = 1000; } var card = Card(2, ResolutionTrack.Native, "lang_c_printf"); combat.SelectEnemy(0); int cycles = Player.Cycles; Assert.That(combat.GetEffectiveCardCost(card), Is.EqualTo(1)); Assert.That(combat.PlayCard(card), Is.True); Assert.That(Player.Cycles, Is.EqualTo(cycles), "ccache and Fedora each reduce cost by 1"); Assert.That(Player.Shield, Is.EqualTo(5)); } [TestCase("portage", 4, PackageSlot.Runtime)] [TestCase("ccache", 4, PackageSlot.Runtime)] [TestCase("openrc", 5, PackageSlot.Daemon)] public void RegisteredPoolsOwnershipUpgradesSaveLoadDuplicatesAndPurge(string suffix, int rarity, PackageSlot slot) { var package = Package(suffix); Assert.That(package, Is.Not.Null); Assert.That(package.Rarity, Is.EqualTo(rarity)); Assert.That(package.Slot, Is.EqualTo(slot)); Assert.That(Enumerable.Range(0, Database.CountByRarity(rarity)).Select(i => Database.FindByRarity(rarity, i)), Does.Contain(package)); Assert.That(Database.FindById("pkg_ccache").Rarity, Is.EqualTo(3)); var owned = new PlayerCollection(); owned.AddPackage(package); var wallet = new SaveData { cacheBalance = 1000, bandwidthBalance = 1000 }; var loadout = new PackageLoadout(owned, Database); Assert.That(loadout.TryEquip("gentoo", slot, package.Id, out _), Is.True); for (int i = 1; i <= 5; i++) { var result = PackageUpgrader.TryUpgrade(owned.GetOwnedPackage(package.Id), wallet); Assert.That(result.Success, Is.True); Assert.That(result.NewLevel, Is.EqualTo(i)); } Assert.That(wallet.cacheBalance, Is.EqualTo(675)); Assert.That(wallet.bandwidthBalance, Is.EqualTo(350)); Assert.That(PackageUpgrader.TryUpgrade(owned.GetOwnedPackage(package.Id), wallet).Success, Is.False); wallet.ownedPackages.Add(new OwnedPackageSaveEntry { id = package.Id, upgradeLevel = 5 }); wallet.packageLoadouts.Add(new PackageLoadoutSaveEntry { distroId = "gentoo", slots = new List { new() { slot = slot, packageId = package.Id } } }); var restored = JsonUtility.FromJson(JsonUtility.ToJson(wallet)); restored.EnsureLists(); var restoredCollection = new PlayerCollection(); foreach (var entry in restored.ownedPackages) restoredCollection.AddPackageSilently(Database.FindById(entry.id), entry.upgradeLevel); var restoredLoadout = new PackageLoadout(restoredCollection, Database); restoredLoadout.Load("gentoo", restored.packageLoadouts[0].slots); Assert.That(restoredLoadout.GetEquippedPackageInstances("gentoo").Single().UpgradeLevel, Is.EqualTo(5)); Assert.That(PackageScrapper.Scrap(restoredCollection, restoredLoadout, restored, package.Id).FailureReason, Is.EqualTo(PackageScrapFailureReason.Equipped)); restoredLoadout.TryUnequip("gentoo", slot, out _); Assert.That(PackageScrapper.Scrap(restoredCollection, restoredLoadout, restored, package.Id).Success, Is.True); Assert.That(restored.cacheBalance, Is.EqualTo(1000 + PackageTuning.GetCacheForRarity(rarity))); Assert.That(restored.bandwidthBalance, Is.EqualTo(1000)); // Exercise the production gacha duplicate path with a deterministic one-package pool. var pool = ScriptableObject.CreateInstance(); temporary.Add(pool); Set(pool, "allPackages", new List { package }); object[] args = { pool, owned, wallet, rarity, false, 0 }; typeof(GamblingSceneController).GetMethod("GrantRandomPackageByRarity", BindingFlags.Static | BindingFlags.NonPublic).Invoke(null, args); Assert.That(args[4], Is.EqualTo(true)); Assert.That(args[5], Is.EqualTo(PackageTuning.GetCacheForRarity(rarity))); var legacy = JsonUtility.FromJson("{\"ownedPackageIds\":[\"pkg_ccache\"]}"); legacy.EnsureLists(); Assert.That(legacy.ownedPackages.Single().id, Is.EqualTo("pkg_ccache")); Assert.That(legacy.ownedPackages.Single().upgradeLevel, Is.Zero); } }