GameFlowTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using DevilLink2.Core;
  6. using DevilLink2.Interaction;
  7. using NUnit.Framework;
  8. namespace DevilLink2.Gameplay.Tests
  9. {
  10. public sealed class GameFlowTests
  11. {
  12. private static GameFlowController Create(ManualClock clock, GameMode mode = GameMode.Classic, int level = 1,
  13. PlayerEconomy economy = null, ModeProgress progress = null)
  14. {
  15. progress = progress ?? new ModeProgress();
  16. if (level > 1) progress.RecordClear(mode, level - 1);
  17. return new GameFlowController(mode, progress, economy ?? new PlayerEconomy(), clock, 2026);
  18. }
  19. private static IEnumerable<Tile> Tiles(Board board)
  20. {
  21. for(int r=0;r<board.Rows;r++) for(int c=0;c<board.Columns;c++)
  22. if(!board.IsEmpty(r,c)) yield return board.GetTile(r,c);
  23. }
  24. private static void Match(GameFlowController flow, ManualClock clock)
  25. {
  26. clock.Seconds += .25; flow.Update();
  27. Assert.That(new BoardMoveFinder().TryFindMove(flow.Runtime.Session.Board,out var pair),Is.True);
  28. Assert.That(flow.Click(pair.First.Row,pair.First.Column),Is.EqualTo(TileClickResult.Selected));
  29. Assert.That(flow.Click(pair.Second.Row,pair.Second.Column),Is.EqualTo(TileClickResult.Matched));
  30. }
  31. private static void Win(GameFlowController flow, ManualClock clock)
  32. {
  33. while(flow.State == GameFlowState.Running) Match(flow,clock);
  34. Assert.That(flow.State,Is.EqualTo(GameFlowState.Victory));
  35. }
  36. // Snapshot real notification delegates to simulate duplicated or queued stale delivery.
  37. private static T Notification<T>(object source,string name) where T:class
  38. => typeof(GameSessionRuntime).GetField(name,BindingFlags.Instance|BindingFlags.NonPublic).GetValue(source) as T;
  39. public static IEnumerable<TestCaseData> AllLevels
  40. {
  41. get
  42. {
  43. foreach(GameMode mode in Enum.GetValues(typeof(GameMode)))
  44. foreach(var level in LevelCatalog.GetAll(mode))
  45. yield return new TestCaseData(mode,level.Level).SetName("FullVictoryRewards_"+mode+"_"+level.Level);
  46. }
  47. }
  48. [TestCaseSource(nameof(AllLevels))]
  49. public void FullVictoryGrantsOnlyCatalogRewardsAndIndependentProgress(GameMode mode,int level)
  50. {
  51. var clock=new ManualClock();
  52. using(var flow=Create(clock,mode,level))
  53. {
  54. int settlements=0; flow.Settled += _=>settlements++;
  55. Assert.That(flow.Start(),Is.True); Win(flow,clock);
  56. var definition=LevelCatalog.Get(mode,level);
  57. Assert.That(flow.Economy.Coins,Is.EqualTo(2000L+definition.ClearCoinReward));
  58. Assert.That(flow.Economy.SpinTickets,Is.EqualTo(1));
  59. Assert.That(flow.Progress.GetHighestCleared(mode),Is.EqualTo(level));
  60. foreach(GameMode other in Enum.GetValues(typeof(GameMode)))
  61. if(other!=mode) Assert.That(flow.Progress.GetHighestCleared(other),Is.Zero);
  62. Assert.That(flow.Result.CoinReward,Is.EqualTo(definition.ClearCoinReward));
  63. Assert.That(flow.Result.SpinTicketReward,Is.EqualTo(1));
  64. Assert.That(flow.Result.FinalScore,Is.EqualTo(flow.Runtime.Score.TotalScore));
  65. Assert.That(flow.Result.ClearBonusScore,Is.EqualTo(flow.Runtime.Score.ClearBonus));
  66. Assert.That(flow.Result.IsSettled,Is.True); Assert.That(settlements,Is.EqualTo(1));
  67. }
  68. }
  69. [Test] public void ReadyDoesNotTickOrAcceptInputAndStartTransitionsOnce()
  70. {
  71. var clock=new ManualClock(); using(var flow=Create(clock))
  72. {
  73. var states=new List<GameFlowState>(); flow.StateChanged += (a,b)=>states.Add(b);
  74. clock.Seconds=20; flow.Update();
  75. Assert.That(flow.State,Is.EqualTo(GameFlowState.Ready)); Assert.That(flow.Runtime.Timer.RemainingTime,Is.EqualTo(180));
  76. Assert.That(flow.Click(0,0),Is.EqualTo(TileClickResult.Ignored));
  77. Assert.That(flow.Pause() || flow.Resume() || flow.RestartCurrentLevel() || flow.Retry() || flow.NextLevel(),Is.False);
  78. Assert.That(flow.Start(),Is.True); Assert.That(flow.Start(),Is.False);
  79. clock.Seconds=20.25; flow.Update(); Assert.That(flow.Runtime.Timer.RemainingTime,Is.EqualTo(179.75));
  80. Assert.That(states,Is.EqualTo(new[]{GameFlowState.Running}));
  81. }
  82. }
  83. [Test] public void PauseResumePreserveBoardScoreAndSelectionButComboUsesRealTime()
  84. {
  85. var clock=new ManualClock(); using(var flow=Create(clock))
  86. {
  87. flow.Start(); Match(flow,clock); clock.Seconds+=.25; flow.Update();
  88. var tile=Tiles(flow.Runtime.Session.Board).First(); flow.Click(tile.Row,tile.Column);
  89. var board=flow.Runtime.Session.Board; var remaining=flow.Runtime.Timer.RemainingTime;
  90. var states=new List<GameFlowState>(); flow.StateChanged += (a,b)=>states.Add(b);
  91. Assert.That(flow.Pause(),Is.True); Assert.That(flow.Pause(),Is.False);
  92. clock.Seconds+=10; flow.Update(); Assert.That(flow.Click(tile.Row,tile.Column),Is.EqualTo(TileClickResult.Ignored));
  93. Assert.That(flow.Runtime.Score.ComboCount,Is.Zero); Assert.That(flow.Runtime.Score.TotalScore,Is.EqualTo(100));
  94. Assert.That(flow.Runtime.Timer.RemainingTime,Is.EqualTo(remaining)); Assert.That(flow.Runtime.Interaction.Selected,Is.SameAs(tile));
  95. Assert.That(flow.Economy.Coins,Is.EqualTo(2000)); Assert.That(flow.Economy.SpinTickets,Is.Zero);
  96. Assert.That(flow.Progress.GetHighestCleared(GameMode.Classic),Is.Zero);
  97. Assert.That(flow.Resume(),Is.True); Assert.That(flow.Resume(),Is.False);
  98. Assert.That(flow.Runtime.Session.Board,Is.SameAs(board)); Assert.That(flow.Runtime.Score.TotalScore,Is.EqualTo(100));
  99. clock.Seconds+=.25; flow.Update(); Assert.That(flow.Runtime.Timer.RemainingTime,Is.EqualTo(remaining-.25));
  100. Assert.That(states,Is.EqualTo(new[]{GameFlowState.Paused,GameFlowState.Running}));
  101. }
  102. }
  103. [TestCase(GameMode.Classic)] [TestCase(GameMode.TimeAttack)] [TestCase(GameMode.Zen)]
  104. public void PausedRestartCreatesCleanRunAndKeepsPlayerData(GameMode mode)
  105. {
  106. var clock=new ManualClock(); using(var flow=Create(clock,mode,3))
  107. {
  108. flow.Start(); Match(flow,clock); clock.Seconds+=.25; flow.Update();
  109. var tile=Tiles(flow.Runtime.Session.Board).First(); flow.Click(tile.Row,tile.Column); flow.Pause();
  110. var previous=flow.Runtime; var states=new List<GameFlowState>(); flow.StateChanged += (a,b)=>states.Add(b);
  111. Assert.That(flow.RestartCurrentLevel(),Is.True);
  112. Assert.That(flow.Runtime.Session.Board,Is.Not.SameAs(previous.Session.Board));
  113. Assert.That(flow.Runtime.Session.Id,Is.Not.EqualTo(previous.Session.Id));
  114. Assert.That(flow.Runtime.Session.Definition,Is.SameAs(previous.Session.Definition));
  115. Assert.That(flow.Runtime.Score.TotalScore,Is.Zero); Assert.That(flow.Runtime.Score.ComboCount,Is.Zero);
  116. Assert.That(flow.Runtime.Timer.ElapsedTime,Is.Zero); Assert.That(flow.Runtime.Timer.RemainingTime,Is.EqualTo(flow.Runtime.Timer.InitialTime));
  117. Assert.That(flow.Runtime.Interaction.Selected,Is.Null); Assert.That(previous.Interaction.Selected,Is.Null);
  118. Assert.That(flow.Runtime.Interaction.IsLocked,Is.False); Assert.That(previous.Timer.IsRunning,Is.False);
  119. Assert.That(previous.Click(0,0),Is.EqualTo(TileClickResult.Ignored));
  120. Assert.That(flow.Economy.Coins,Is.EqualTo(2000)); Assert.That(flow.Economy.SpinTickets,Is.Zero);
  121. Assert.That(flow.Progress.GetHighestCleared(mode),Is.EqualTo(2)); Assert.That(flow.Result,Is.Null);
  122. Assert.That(states,Is.EqualTo(new[]{GameFlowState.Ready,GameFlowState.Running}));
  123. }
  124. }
  125. [TestCase(GameMode.Classic)] [TestCase(GameMode.TimeAttack)]
  126. public void DefeatPreservesScoreAndSnapshotWithoutRewardsAndRetryKeepsLevel(GameMode mode)
  127. {
  128. var clock=new ManualClock(); using(var flow=Create(clock,mode,4))
  129. {
  130. flow.Start(); Match(flow,clock); int settled=0; flow.Settled += _=>settled++;
  131. clock.Seconds+=1000; flow.Update(); var result=flow.Result; var runtime=flow.Runtime;
  132. Assert.That(flow.State,Is.EqualTo(GameFlowState.Defeat)); Assert.That(result.Outcome,Is.EqualTo(GameOutcome.Defeat));
  133. Assert.That(result.FinalScore,Is.EqualTo(100)); Assert.That(result.ClearBonusScore,Is.Zero);
  134. Assert.That(result.SessionId,Is.EqualTo(runtime.Session.Id)); Assert.That(result.GameMode,Is.EqualTo(mode));
  135. Assert.That(result.LevelNumber,Is.EqualTo(4)); Assert.That(result.HasTimer,Is.True);
  136. Assert.That(result.InitialTime,Is.EqualTo(LevelCatalog.Get(mode,4).TimeLimitSeconds));
  137. Assert.That(result.RemainingTime,Is.Zero); Assert.That(result.ElapsedTime,Is.EqualTo(result.InitialTime));
  138. Assert.That(result.CoinReward+result.SpinTicketReward,Is.Zero); Assert.That(result.IsSettled,Is.True);
  139. Assert.That(flow.Economy.Coins,Is.EqualTo(2000)); Assert.That(flow.Economy.SpinTickets,Is.Zero);
  140. Assert.That(flow.Progress.GetHighestCleared(mode),Is.EqualTo(3));
  141. Assert.That(flow.NextLevel() || flow.Start() || flow.Resume() || flow.Pause(),Is.False);
  142. Assert.That(flow.Click(0,0),Is.EqualTo(TileClickResult.Ignored)); Assert.That(flow.AddTime(30),Is.False);
  143. Notification<Action>(runtime,"TimedOut")(); Notification<Action<int>>(runtime,"ClearBonusAwarded")(1000); flow.Update();
  144. Assert.That(flow.Result,Is.SameAs(result)); Assert.That(settled,Is.EqualTo(1));
  145. Assert.That(flow.Retry(),Is.True); Assert.That(flow.Runtime.Session.Definition.Level,Is.EqualTo(4));
  146. Assert.That(flow.Runtime.Score.TotalScore,Is.Zero); Assert.That(flow.Runtime.Session.Board,Is.Not.SameAs(runtime.Session.Board));
  147. }
  148. }
  149. [Test] public void VictoryResultIsImmutableAndDuplicateOrOppositeEventsCannotSettleAgain()
  150. {
  151. var clock=new ManualClock(); using(var flow=Create(clock))
  152. {
  153. flow.Start(); var runtime=flow.Runtime; int events=0; flow.Settled += _=>events++; Win(flow,clock);
  154. var result=flow.Result; var balance=flow.Economy.Coins;
  155. Notification<Action<int>>(runtime,"ClearBonusAwarded")(runtime.Score.ClearBonus);
  156. Notification<Action>(runtime,"TimedOut")(); runtime.Interaction.Advance(10); flow.Update();
  157. Assert.That(flow.State,Is.EqualTo(GameFlowState.Victory)); Assert.That(flow.Result,Is.SameAs(result));
  158. Assert.That(flow.Economy.Coins,Is.EqualTo(balance)); Assert.That(flow.Economy.SpinTickets,Is.EqualTo(1)); Assert.That(events,Is.EqualTo(1));
  159. Assert.That(result.InitialTime,Is.EqualTo(180)); Assert.That(result.RemainingTime,Is.EqualTo(runtime.Timer.RemainingTime));
  160. Assert.That(result.ElapsedTime,Is.EqualTo(runtime.Timer.ElapsedTime)); Assert.That(result.LevelNumber,Is.EqualTo(1));
  161. Assert.That(result.GameMode,Is.EqualTo(GameMode.Classic)); Assert.That(result.Outcome,Is.EqualTo(GameOutcome.Victory));
  162. Assert.That(result.ClearBonusScore,Is.EqualTo(1000+(int)Math.Ceiling(result.RemainingTime)*10));
  163. Assert.That(typeof(GameResult).GetProperties().All(p=>p.SetMethod==null),Is.True);
  164. }
  165. }
  166. [TestCase(GameMode.Classic)] [TestCase(GameMode.TimeAttack)] [TestCase(GameMode.Zen)]
  167. public void VictoryRetryKeepsCurrentLevelAndEconomyWithoutAdvancingAgain(GameMode mode)
  168. {
  169. var clock=new ManualClock(); using(var flow=Create(clock,mode,5))
  170. {
  171. flow.Start(); Win(flow,clock); var old=flow.Runtime; var coins=flow.Economy.Coins;
  172. Assert.That(flow.Retry(),Is.True); Assert.That(flow.Runtime.Session.Definition.Level,Is.EqualTo(5));
  173. Assert.That(flow.Progress.GetHighestCleared(mode),Is.EqualTo(5)); Assert.That(flow.Economy.Coins,Is.EqualTo(coins));
  174. Assert.That(flow.Economy.SpinTickets,Is.EqualTo(1)); Assert.That(flow.Runtime.Session.Board,Is.Not.SameAs(old.Session.Board));
  175. Assert.That(flow.Result,Is.Null); Assert.That(flow.Runtime.Score.TotalScore,Is.Zero);
  176. }
  177. }
  178. [TestCase(GameMode.Classic,1)] [TestCase(GameMode.Classic,19)]
  179. [TestCase(GameMode.TimeAttack,1)] [TestCase(GameMode.TimeAttack,19)]
  180. [TestCase(GameMode.Zen,1)] [TestCase(GameMode.Zen,19)]
  181. public void NextLevelUsesNewBoardAndPreservesEconomy(GameMode mode,int level)
  182. {
  183. var clock=new ManualClock(); using(var flow=Create(clock,mode,level))
  184. {
  185. Assert.That(flow.NextLevel(),Is.False); flow.Start(); Win(flow,clock);
  186. var old=flow.Runtime; var coins=flow.Economy.Coins;
  187. Assert.That(flow.NextLevel(),Is.True); Assert.That(flow.Runtime.Session.Definition.Level,Is.EqualTo(level+1));
  188. Assert.That(flow.Runtime.Session.Board,Is.Not.SameAs(old.Session.Board)); Assert.That(flow.Runtime.Score.TotalScore,Is.Zero);
  189. Assert.That(flow.Runtime.Timer.RemainingTime,Is.EqualTo(LevelCatalog.Get(mode,level+1).TimeLimitSeconds));
  190. Assert.That(flow.Economy.Coins,Is.EqualTo(coins)); Assert.That(flow.Economy.SpinTickets,Is.EqualTo(1));
  191. Assert.That(flow.Progress.GetHighestCleared(mode),Is.EqualTo(level));
  192. }
  193. }
  194. [TestCase(GameMode.Classic)] [TestCase(GameMode.TimeAttack)] [TestCase(GameMode.Zen)]
  195. public void LevelTwentyAlwaysReplaysTwentyAndPaysFullReward(GameMode mode)
  196. {
  197. var clock=new ManualClock(); using(var flow=Create(clock,mode,20))
  198. {
  199. flow.Start(); var definition=LevelCatalog.Get(mode,20);
  200. for(int i=1;i<=3;i++)
  201. {
  202. Win(flow,clock); Assert.That(flow.Economy.Coins,Is.EqualTo(2000L+i*definition.ClearCoinReward));
  203. Assert.That(flow.Economy.SpinTickets,Is.EqualTo(i)); Assert.That(flow.Progress.GetHighestCleared(mode),Is.EqualTo(20));
  204. Assert.That(flow.NextLevel(),Is.True); Assert.That(flow.Runtime.Session.Definition.Level,Is.EqualTo(20));
  205. }
  206. }
  207. }
  208. [TestCase(false)] [TestCase(true)]
  209. public void ReturnToLobbyDisablesRunWithoutResultRewardsOrProgress(bool paused)
  210. {
  211. var clock=new ManualClock(); using(var flow=Create(clock))
  212. {
  213. flow.Start(); Match(flow,clock); if(paused) flow.Pause();
  214. var runtime=flow.Runtime; var staleClear=Notification<Action<int>>(runtime,"ClearBonusAwarded"); var staleTimeout=Notification<Action>(runtime,"TimedOut");
  215. int lobby=0,settled=0; flow.LobbyRequested += ()=>lobby++; flow.Settled += _=>settled++;
  216. clock.Seconds=1000; Assert.That(flow.ReturnToLobby(),Is.True);
  217. Assert.That(flow.State,Is.EqualTo(GameFlowState.Abandoned)); Assert.That(runtime.Timer.IsRunning,Is.False);
  218. flow.Update(); staleClear(1000); staleTimeout(); runtime.Interaction.Advance(1000);
  219. Assert.That(flow.Click(0,0),Is.EqualTo(TileClickResult.Ignored)); Assert.That(flow.Result,Is.Null);
  220. Assert.That(flow.Start() || flow.Resume() || flow.RestartCurrentLevel() || flow.NextLevel() || flow.ReturnToLobby(),Is.False);
  221. Assert.That(flow.Economy.Coins,Is.EqualTo(2000)); Assert.That(flow.Economy.SpinTickets,Is.Zero);
  222. Assert.That(flow.Progress.GetHighestCleared(GameMode.Classic),Is.Zero); Assert.That(lobby,Is.EqualTo(1)); Assert.That(settled,Is.Zero);
  223. Assert.That(Notification<Action<int>>(runtime,"ClearBonusAwarded"),Is.Null); Assert.That(Notification<Action>(runtime,"TimedOut"),Is.Null);
  224. }
  225. }
  226. [Test] public void OldQueuedCallbacksCannotMutateNewRunAcrossRepeatedRestarts()
  227. {
  228. var clock=new ManualClock(); using(var flow=Create(clock))
  229. {
  230. flow.Start(); int settled=0; flow.Settled += _=>settled++;
  231. for(int i=0;i<5;i++)
  232. {
  233. Match(flow,clock); var old=flow.Runtime; var stale=Notification<Action<int>>(old,"ClearBonusAwarded");
  234. var staleTimeout=Notification<Action>(old,"TimedOut"); flow.Pause(); flow.RestartCurrentLevel();
  235. old.Interaction.Advance(1000); stale(1000); staleTimeout(); old.Update();
  236. Assert.That(flow.State,Is.EqualTo(GameFlowState.Running)); Assert.That(flow.Result,Is.Null);
  237. Assert.That(flow.Runtime.Score.TotalScore,Is.Zero); Assert.That(flow.Economy.Coins,Is.EqualTo(2000));
  238. }
  239. Win(flow,clock); Assert.That(settled,Is.EqualTo(1)); Assert.That(flow.Economy.Coins,Is.EqualTo(3000));
  240. }
  241. }
  242. [Test] public void SettlementCallbacksObserveCommittedDataAndCannotReenterCommands()
  243. {
  244. var clock=new ManualClock(); using(var flow=Create(clock))
  245. {
  246. flow.StateChanged += (old,next)=> {
  247. if(next!=GameFlowState.Victory) return;
  248. Assert.That(flow.Result.IsSettled,Is.True); Assert.That(flow.Economy.Coins,Is.EqualTo(3000));
  249. Assert.That(flow.NextLevel() || flow.Retry() || flow.ReturnToLobby(),Is.False);
  250. };
  251. flow.Settled += result=> { Assert.That(result,Is.SameAs(flow.Result)); Assert.That(flow.RestartCurrentLevel(),Is.False); };
  252. flow.Start(); Win(flow,clock); Assert.That(flow.NextLevel(),Is.True);
  253. }
  254. }
  255. [Test] public void PauseAtDeadlineBecomesDefeatRatherThanPaused()
  256. {
  257. var clock=new ManualClock(); using(var flow=Create(clock))
  258. { flow.Start(); clock.Seconds=180; Assert.That(flow.Pause(),Is.False); Assert.That(flow.State,Is.EqualTo(GameFlowState.Defeat)); }
  259. }
  260. [Test] public void SharedPlayerDataSurvivesLeavingAndEnteringAnotherMode()
  261. {
  262. var clock=new ManualClock(); var economy=new PlayerEconomy(); var progress=new ModeProgress();
  263. using(var first=Create(clock,GameMode.Classic,1,economy,progress)) { first.Start(); Win(first,clock); first.ReturnToLobby(); }
  264. using(var second=Create(clock,GameMode.Zen,1,economy,progress))
  265. {
  266. Assert.That(economy.Coins,Is.EqualTo(3000)); Assert.That(economy.SpinTickets,Is.EqualTo(1));
  267. Assert.That(progress.GetHighestCleared(GameMode.Classic),Is.EqualTo(1)); Assert.That(second.Runtime.Session.Definition.Level,Is.EqualTo(1));
  268. second.Start(); Win(second,clock); Assert.That(economy.Coins,Is.EqualTo(4000)); Assert.That(economy.SpinTickets,Is.EqualTo(2));
  269. }
  270. }
  271. [TestCase(false)] [TestCase(true)]
  272. public void LastPairDeadlineHasOneOutcome(bool atDeadline)
  273. {
  274. var clock=new ManualClock(); using(var flow=Create(clock))
  275. {
  276. var board=flow.Runtime.Session.Board;
  277. new BoardMoveFinder().TryFindMove(board,out var pair);
  278. foreach(var tile in Tiles(board).ToArray()) if(tile!=pair.First && tile!=pair.Second) board.RemoveTile(tile);
  279. int settled=0; flow.Settled += _=>settled++; flow.Start();
  280. flow.Click(pair.First.Row,pair.First.Column); clock.Seconds=atDeadline?180:179.75;
  281. var click=flow.Click(pair.Second.Row,pair.Second.Column);
  282. Assert.That(click,Is.EqualTo(atDeadline?TileClickResult.Ignored:TileClickResult.Matched));
  283. Assert.That(flow.State,Is.EqualTo(atDeadline?GameFlowState.Defeat:GameFlowState.Victory));
  284. Assert.That(flow.Result.FinalScore,Is.EqualTo(atDeadline?0:1110));
  285. clock.Seconds=1000; flow.Update();
  286. Assert.That(settled,Is.EqualTo(1)); Assert.That(flow.Economy.Coins,Is.EqualTo(atDeadline?2000:3000));
  287. }
  288. }
  289. [Test] public void StaleVictoryNotificationFromCompletedRunCannotWinNextRun()
  290. {
  291. var clock=new ManualClock(); using(var flow=Create(clock))
  292. {
  293. flow.Start(); Win(flow,clock); var old=flow.Runtime;
  294. var queued=Notification<Action<int>>(old,"ClearBonusAwarded"); var snapshot=flow.Result;
  295. flow.NextLevel(); queued(old.Score.ClearBonus); old.Interaction.Advance(10);
  296. Assert.That(flow.State,Is.EqualTo(GameFlowState.Running)); Assert.That(flow.Result,Is.Null);
  297. Assert.That(flow.Economy.Coins,Is.EqualTo(3000)); Assert.That(flow.Economy.SpinTickets,Is.EqualTo(1));
  298. Assert.That(snapshot.SessionId,Is.Not.EqualTo(flow.Runtime.Session.Id)); Assert.That(snapshot.CoinReward,Is.EqualTo(1000));
  299. }
  300. }
  301. [Test] public void DestroyingFlowInsideSettlementDefersCleanupUntilNotificationCompletes()
  302. {
  303. var clock=new ManualClock(); var flow=Create(clock); int abandoned=0;
  304. flow.StateChanged += (a,b)=> { if(b==GameFlowState.Abandoned) abandoned++; };
  305. flow.Settled += _=>flow.Dispose(); flow.Start();
  306. while(flow.State==GameFlowState.Running) Match(flow,clock);
  307. Assert.That(flow.State,Is.EqualTo(GameFlowState.Abandoned)); Assert.That(flow.Result.Outcome,Is.EqualTo(GameOutcome.Victory));
  308. Assert.That(flow.Economy.Coins,Is.EqualTo(3000)); Assert.That(flow.Runtime.Timer.IsRunning,Is.False);
  309. Assert.That(abandoned,Is.EqualTo(1)); Assert.That(flow.Start(),Is.False); flow.Dispose();
  310. }
  311. [Test] public void EconomyDefaultsAndInvalidBalancesAreChecked()
  312. {
  313. var player=new PlayerEconomy(); Assert.That(player.Coins,Is.EqualTo(2000)); Assert.That(player.SpinTickets,Is.Zero);
  314. Assert.Throws<ArgumentOutOfRangeException>(()=>new PlayerEconomy(-1)); Assert.Throws<ArgumentOutOfRangeException>(()=>new PlayerEconomy(0,-1));
  315. }
  316. }
  317. }