SaveBootstrapTests.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.IO;
  3. using DevilLink2.Gameplay;
  4. using DevilLink2.Persistence;
  5. using DevilLink2.Presentation;
  6. using NUnit.Framework;
  7. using UnityEditor;
  8. using UnityEngine;
  9. using Object=UnityEngine.Object;
  10. namespace DevilLink2.Tests
  11. {
  12. public sealed class SaveBootstrapTests
  13. {
  14. private string directory;
  15. private GameObject root;
  16. [SetUp] public void Setup()
  17. {
  18. directory=Path.Combine(Path.GetTempPath(),"DevilLink2.SaveBootstrapTests",Guid.NewGuid().ToString("N"));
  19. root=new GameObject("SaveBootstrapTest",typeof(RectTransform));
  20. }
  21. [TearDown] public void Cleanup()
  22. {
  23. Object.DestroyImmediate(root);
  24. string expected=Path.GetFullPath(Path.Combine(Path.GetTempPath(),"DevilLink2.SaveBootstrapTests"))+Path.DirectorySeparatorChar;
  25. string resolved=Path.GetFullPath(directory);
  26. if(!resolved.StartsWith(expected,StringComparison.OrdinalIgnoreCase) || !Guid.TryParseExact(Path.GetFileName(resolved),"N",out _))
  27. throw new InvalidOperationException("Invalid test cleanup path.");
  28. if(Directory.Exists(resolved))Directory.Delete(resolved,true);
  29. }
  30. private BoardDemoBootstrap Bootstrap()
  31. {
  32. var viewport=(RectTransform)root.transform; viewport.sizeDelta=new Vector2(418,385);
  33. var content=(RectTransform)new GameObject("Content",typeof(RectTransform)).transform; content.SetParent(viewport,false);
  34. var board=(RectTransform)new GameObject("Board",typeof(RectTransform)).transform; board.SetParent(content,false);
  35. var prefab=AssetDatabase.LoadAssetAtPath<GameObject>("Assets/DevilLink2/Prefabs/Gameplay/TileView.prefab");
  36. var view=root.AddComponent<BoardView>(); view.Configure(prefab.GetComponent<TileView>(),viewport,content,board);
  37. var bootstrap=root.AddComponent<BoardDemoBootstrap>(); bootstrap.Configure(view); return bootstrap;
  38. }
  39. [Test] public void PersistentBootstrapLoadsBeforeCreatingSessionAndResetOnlyAbandons()
  40. {
  41. var saves=new LocalSaveService(new FileSaveStorage(directory));
  42. saves.Save(new SaveData{Coins=8000,SpinTickets=6,ClassicProgress=5});
  43. var bootstrap=Bootstrap(); Assert.That(bootstrap.Session,Is.Null); Assert.That(bootstrap.Economy,Is.Null);
  44. bootstrap.InitializePersistent(GameMode.Classic,directory);
  45. Assert.That(bootstrap.Economy.Coins,Is.EqualTo(8000)); Assert.That(bootstrap.Economy.SpinTickets,Is.EqualTo(6));
  46. Assert.That(bootstrap.Session.Definition.Level,Is.EqualTo(6)); Assert.That(bootstrap.Flow.State,Is.EqualTo(GameFlowState.Running));
  47. int settled=0; bootstrap.Flow.Settled += _=>settled++; bootstrap.ResetSaveData();
  48. Assert.That(bootstrap.Flow.State,Is.EqualTo(GameFlowState.Abandoned)); Assert.That(settled,Is.Zero);
  49. Assert.That(bootstrap.Economy.Coins,Is.EqualTo(2000)); Assert.That(saves.Load().Data.ClassicProgress,Is.Zero);
  50. Assert.That(root.GetComponent<BoardView>().VisibleTileCount,Is.Zero);
  51. }
  52. [Test] public void InjectedSavePlayerSurvivesModeChangesWithoutResettingLoadedEconomy()
  53. {
  54. var saves=new LocalSaveService(new FileSaveStorage(directory)); saves.Save(new SaveData{Coins=9000,TimeAttackProgress=19,ZenProgress=20});
  55. using(var player=new SavedPlayer(saves))
  56. {
  57. var bootstrap=Bootstrap(); bootstrap.InitializePersistent(GameMode.TimeAttack,player);
  58. Assert.That(bootstrap.Session.Definition.Level,Is.EqualTo(20));
  59. bootstrap.Initialize(GameMode.Zen,player.Progress);
  60. Assert.That(bootstrap.Session.Definition.Level,Is.EqualTo(20)); Assert.That(bootstrap.Economy.Coins,Is.EqualTo(9000));
  61. Assert.That(bootstrap.Economy,Is.SameAs(player.Economy)); Assert.That(player.ActiveFlow,Is.SameAs(bootstrap.Flow));
  62. }
  63. }
  64. }
  65. }