188 lines
8.6 KiB
C#
188 lines
8.6 KiB
C#
using System;
|
|
using System.IO;
|
|
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;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class RewardSettlementTests
|
|
{
|
|
private const BindingFlags Private = BindingFlags.Instance | BindingFlags.NonPublic;
|
|
private string _directory;
|
|
private GameObject _owner;
|
|
private RunManager _run;
|
|
private CombatSceneController _controller;
|
|
private SaveService _save;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_directory = Path.Combine(Path.GetTempPath(), "KernelPanic-RewardTests-" + Guid.NewGuid().ToString("N"));
|
|
Directory.CreateDirectory(_directory);
|
|
_save = new SaveService(Path.Combine(_directory, "progression.json"));
|
|
_owner = new GameObject("reward persistence regression");
|
|
_owner.SetActive(false);
|
|
_controller = _owner.AddComponent<CombatSceneController>();
|
|
_run = _owner.GetComponent<RunManager>();
|
|
Set(_controller, "_runManager", _run);
|
|
Set(_controller, "_combatManager", _owner.GetComponent<CombatManager>());
|
|
Set(_controller, "_saveService", _save);
|
|
DistroDefinition distro = AssetDatabase.LoadAssetAtPath<DistroDefinition>("Assets/Data/Distros/Gentoo.asset");
|
|
_run.StartRun(new RunConfig(distro, Language.Haskell, Language.C, Array.Empty<CardDefinition>(), 314));
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
// Destruction must never cause an additional settlement or touch the default save.
|
|
if (_controller != null) Set(_controller, "_saveService", null);
|
|
if (_owner != null) UnityEngine.Object.DestroyImmediate(_owner);
|
|
if (Directory.Exists(_directory)) Directory.Delete(_directory, true);
|
|
}
|
|
|
|
[Test]
|
|
public void FailedWriteKeepsRewardsPendingAndRetryPersistsThemExactlyOnce()
|
|
{
|
|
SaveData initial = SaveData.CreateDefault();
|
|
initial.entropyBalance = 9;
|
|
initial.bandwidthBalance = 20;
|
|
_save.Save(initial);
|
|
Directory.CreateDirectory(_save.BackupPath);
|
|
Set(_run, "_accruedBandwidth", 7);
|
|
Set(_run, "_accruedEntropy", 3);
|
|
Set(_run, "_accruedCommits", 2);
|
|
Set(_run, "currentWaveNumber", 4);
|
|
|
|
Settle();
|
|
Assert.That(_run.RewardsSettled, Is.False);
|
|
Assert.That(Get<string>(_controller, "_saveError"), Is.Not.Null.And.Not.Empty);
|
|
Assert.That(_save.Load().bandwidthBalance, Is.EqualTo(20));
|
|
Assert.That(_save.Load().entropyBalance, Is.EqualTo(9));
|
|
|
|
Directory.Delete(_save.BackupPath);
|
|
Settle();
|
|
Assert.That(_run.RewardsSettled, Is.True);
|
|
Assert.That(Get<string>(_controller, "_saveError"), Is.Null);
|
|
SaveData settled = _save.Load();
|
|
Assert.That(settled.bandwidthBalance, Is.EqualTo(27));
|
|
Assert.That(settled.entropyBalance, Is.EqualTo(12));
|
|
Assert.That(settled.commitsBalance, Is.EqualTo(2));
|
|
Assert.That(settled.GetBestWave("gentoo"), Is.EqualTo(4));
|
|
string once = File.ReadAllText(_save.SavePath);
|
|
|
|
Settle();
|
|
Assert.That(File.ReadAllText(_save.SavePath), Is.EqualTo(once));
|
|
Assert.That(_save.Load().bandwidthBalance, Is.EqualTo(27));
|
|
Assert.That(_save.Load().entropyBalance, Is.EqualTo(12));
|
|
Assert.That(_save.Load().commitsBalance, Is.EqualTo(2));
|
|
}
|
|
|
|
[Test]
|
|
public void RunWithNoCurrencyRewardStillPersistsDistroAndBestWave()
|
|
{
|
|
Assert.That(_run.AccruedBandwidth, Is.Zero);
|
|
Assert.That(_run.AccruedEntropy, Is.Zero);
|
|
|
|
Settle();
|
|
|
|
Assert.That(_run.RewardsSettled, Is.True);
|
|
SaveData settled = _save.Load();
|
|
Assert.That(settled.lastRunDistroId, Is.EqualTo("gentoo"));
|
|
Assert.That(settled.lastRunLoadout.distroId, Is.EqualTo("gentoo"));
|
|
Assert.That(settled.GetBestWave("gentoo"), Is.EqualTo(1));
|
|
Assert.That(settled.bandwidthBalance, Is.Zero);
|
|
Assert.That(settled.entropyBalance, Is.Zero);
|
|
}
|
|
|
|
[Test]
|
|
public void ReadFailureAfterSettlementDoesNotTrapTheRunReportAndRecoveryClearsTheWarning()
|
|
{
|
|
Set(_run, "_accruedBandwidth", 7);
|
|
Settle();
|
|
Assert.That(_run.RewardsSettled, Is.True);
|
|
string committed = File.ReadAllText(_save.SavePath);
|
|
foreach (string field in new[] { "_waveLabel", "_phaseLabel", "_bitsLabel", "_entropyLabel", "_bandwidthLabel", "_rootCreditsLabel", "_seedLabel" })
|
|
Set(_controller, field, new Label());
|
|
|
|
using (var locked = new FileStream(_save.SavePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
|
|
{
|
|
RefreshStatus();
|
|
Assert.That(Get<string>(_controller, "_saveError"), Is.Not.Null.And.Not.Empty);
|
|
var report = (VisualElement)typeof(CombatSceneController).GetMethod("BuildRunEndOverlay", Private).Invoke(_controller, null);
|
|
Assert.That(report.Q<Button>(className: "death-return-action").enabledSelf, Is.True,
|
|
"A later read failure cannot prevent leaving a run whose rewards are already saved.");
|
|
}
|
|
|
|
RefreshStatus();
|
|
Assert.That(Get<string>(_controller, "_saveError"), Is.Null);
|
|
Assert.That(_run.RewardsSettled, Is.True);
|
|
Assert.That(File.ReadAllText(_save.SavePath), Is.EqualTo(committed), "Status refresh must not settle rewards again.");
|
|
Assert.That(_save.Load().bandwidthBalance, Is.EqualTo(7));
|
|
}
|
|
|
|
private void Settle() => typeof(CombatSceneController).GetMethod("SettleRunRewardsIfNeeded", Private).Invoke(_controller, null);
|
|
|
|
[Test]
|
|
public void CampaignUnlockCommitsBeforeEndlessAndFailedSaveNeverAnnouncesSuccess()
|
|
{
|
|
_save.Save(SaveData.CreateDefault());
|
|
Set(_run, "_wavesCleared", 10);
|
|
Set(_run, "currentWaveNumber", 11);
|
|
Directory.CreateDirectory(_save.BackupPath);
|
|
var persist = typeof(CombatSceneController).GetMethod("PersistCampaignUnlocks", Private);
|
|
persist.Invoke(_controller, null);
|
|
Assert.That(_save.Load().campaignClearLanguages, Is.Empty);
|
|
Assert.That(Get<bool>(_controller, "_campaignUnlocksSaved"), Is.False);
|
|
Assert.That(Get<string>(_controller, "_campaignUnlockNotice"), Is.Null);
|
|
Assert.That(Get<string>(_controller, "_campaignUnlockError"), Is.Not.Empty);
|
|
Directory.Delete(_save.BackupPath);
|
|
persist.Invoke(_controller, null);
|
|
Assert.That(_save.Load().campaignClearLanguages, Is.EquivalentTo(new[] { Language.C }));
|
|
Assert.That(Get<string>(_controller, "_campaignUnlockNotice"), Does.Contain("C++"));
|
|
Assert.That(_run.RewardsSettled, Is.False, "Unlocking must not bank or finish the run.");
|
|
Assert.That(_run.IsRunActive, Is.True);
|
|
string committed = File.ReadAllText(_save.SavePath);
|
|
_run.Journey.endless = true;
|
|
persist.Invoke(_controller, null);
|
|
Assert.That(File.ReadAllText(_save.SavePath), Is.EqualTo(committed));
|
|
}
|
|
|
|
[Test]
|
|
public void CampaignVictorySaveFailureCanRetryWithoutDuplicateRewardsOrHistory()
|
|
{
|
|
_save.Save(SaveData.CreateDefault());
|
|
Set(_run, "_accruedBandwidth", 100);
|
|
Set(_run, "_accruedEntropy", 3);
|
|
Set(_run, "currentWaveNumber", RunJourney.BossWave + 1);
|
|
Set(_run, "_wavesCleared", RunJourney.BossWave);
|
|
var combat = _owner.GetComponent<CombatManager>();
|
|
Set(combat, "_runManager", _run);
|
|
Set(combat, "_awaitingWaveContinue", true);
|
|
Assert.That(combat.CompleteCampaign(), Is.True);
|
|
Directory.CreateDirectory(_save.BackupPath);
|
|
Settle();
|
|
Assert.That(_run.RewardsSettled, Is.False);
|
|
Assert.That(_save.Load().recentRuns, Is.Empty);
|
|
Directory.Delete(_save.BackupPath);
|
|
Settle(); Settle();
|
|
var saved = _save.Load();
|
|
Assert.That(saved.bandwidthBalance, Is.EqualTo(100));
|
|
Assert.That(saved.entropyBalance, Is.EqualTo(3));
|
|
Assert.That(saved.GetBestWave("gentoo"), Is.EqualTo(RunJourney.BossWave));
|
|
Assert.That(saved.recentRuns.Count, Is.EqualTo(1));
|
|
Assert.That(saved.recentRuns[0].victory, Is.True);
|
|
Assert.That(saved.recentRuns[0].waveReached, Is.EqualTo(RunJourney.BossWave));
|
|
Assert.That(saved.recentRuns[0].cause, Does.Contain("campaign complete"));
|
|
}
|
|
private void RefreshStatus() => typeof(CombatSceneController).GetMethod("RefreshStatus", Private).Invoke(_controller, null);
|
|
private static void Set(object instance, string field, object value) => instance.GetType().GetField(field, Private).SetValue(instance, value);
|
|
private static T Get<T>(object instance, string field) => (T)instance.GetType().GetField(field, Private).GetValue(instance);
|
|
}
|