using System; using System.IO; using DevilLink2.Gameplay; using DevilLink2.Persistence; using DevilLink2.Presentation; using NUnit.Framework; using UnityEditor; using UnityEngine; using Object=UnityEngine.Object; namespace DevilLink2.Tests { public sealed class SaveBootstrapTests { private string directory; private GameObject root; [SetUp] public void Setup() { directory=Path.Combine(Path.GetTempPath(),"DevilLink2.SaveBootstrapTests",Guid.NewGuid().ToString("N")); root=new GameObject("SaveBootstrapTest",typeof(RectTransform)); } [TearDown] public void Cleanup() { Object.DestroyImmediate(root); string expected=Path.GetFullPath(Path.Combine(Path.GetTempPath(),"DevilLink2.SaveBootstrapTests"))+Path.DirectorySeparatorChar; string resolved=Path.GetFullPath(directory); if(!resolved.StartsWith(expected,StringComparison.OrdinalIgnoreCase) || !Guid.TryParseExact(Path.GetFileName(resolved),"N",out _)) throw new InvalidOperationException("Invalid test cleanup path."); if(Directory.Exists(resolved))Directory.Delete(resolved,true); } private BoardDemoBootstrap Bootstrap() { var viewport=(RectTransform)root.transform; viewport.sizeDelta=new Vector2(418,385); var content=(RectTransform)new GameObject("Content",typeof(RectTransform)).transform; content.SetParent(viewport,false); var board=(RectTransform)new GameObject("Board",typeof(RectTransform)).transform; board.SetParent(content,false); var prefab=AssetDatabase.LoadAssetAtPath("Assets/DevilLink2/Prefabs/Gameplay/TileView.prefab"); var view=root.AddComponent(); view.Configure(prefab.GetComponent(),viewport,content,board); var bootstrap=root.AddComponent(); bootstrap.Configure(view); return bootstrap; } [Test] public void PersistentBootstrapLoadsBeforeCreatingSessionAndResetOnlyAbandons() { var saves=new LocalSaveService(new FileSaveStorage(directory)); saves.Save(new SaveData{Coins=8000,SpinTickets=6,ClassicProgress=5}); var bootstrap=Bootstrap(); Assert.That(bootstrap.Session,Is.Null); Assert.That(bootstrap.Economy,Is.Null); bootstrap.InitializePersistent(GameMode.Classic,directory); Assert.That(bootstrap.Economy.Coins,Is.EqualTo(8000)); Assert.That(bootstrap.Economy.SpinTickets,Is.EqualTo(6)); Assert.That(bootstrap.Session.Definition.Level,Is.EqualTo(6)); Assert.That(bootstrap.Flow.State,Is.EqualTo(GameFlowState.Running)); int settled=0; bootstrap.Flow.Settled += _=>settled++; bootstrap.ResetSaveData(); Assert.That(bootstrap.Flow.State,Is.EqualTo(GameFlowState.Abandoned)); Assert.That(settled,Is.Zero); Assert.That(bootstrap.Economy.Coins,Is.EqualTo(2000)); Assert.That(saves.Load().Data.ClassicProgress,Is.Zero); Assert.That(root.GetComponent().VisibleTileCount,Is.Zero); } [Test] public void InjectedSavePlayerSurvivesModeChangesWithoutResettingLoadedEconomy() { var saves=new LocalSaveService(new FileSaveStorage(directory)); saves.Save(new SaveData{Coins=9000,TimeAttackProgress=19,ZenProgress=20}); using(var player=new SavedPlayer(saves)) { var bootstrap=Bootstrap(); bootstrap.InitializePersistent(GameMode.TimeAttack,player); Assert.That(bootstrap.Session.Definition.Level,Is.EqualTo(20)); bootstrap.Initialize(GameMode.Zen,player.Progress); Assert.That(bootstrap.Session.Definition.Level,Is.EqualTo(20)); Assert.That(bootstrap.Economy.Coins,Is.EqualTo(9000)); Assert.That(bootstrap.Economy,Is.SameAs(player.Economy)); Assert.That(player.ActiveFlow,Is.SameAs(bootstrap.Flow)); } } } }