A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
#nullable enable
using System;
using System.Collections;
using System.IO;
using System.Linq;
using NUnit.Framework;
using Statecraft.Game;
using Statecraft.Game.Boot;
using Statecraft.Game.Map;
using Statecraft.Game.UI;
using Statecraft.Sim;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.TestTools;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;
/// <summary>#530 / phase 7 PR10: New Game can skip the opening to the map; replay returns to the start screen.
/// The presentation must not replace the session, advance the simulation, queue an order or alter the autosave.</summary>
public class OpeningTests
{
static GameInputs? inputs;
static GameInputs Inputs => inputs ??= GameInputs.FromDisk(Paths.Content, Paths.StreamingMap);
string? saveDirectory;
static VisualElement Ui => Object.FindFirstObjectByType<UIDocument>().rootVisualElement;
static bool StartOpen => Ui.Q("root").ClassListContains(StartScreenPresenter.OpenClass);
[TearDown]
public void Clean()
{
EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
if (saveDirectory != null && Directory.Exists(saveDirectory)) Directory.Delete(saveDirectory, true);
saveDirectory = null;
}
/// <summary>The map's real opening keeps both neighbouring cities at least 5% inside every supported phone and desktop
/// shape. The points come from the content MapScene just built, so this follows a sourced coordinate change.</summary>
[TestCase(390, 844)]
[TestCase(375, 667)]
[TestCase(900, 1600)]
[TestCase(1600, 900)]
[TestCase(2100, 900)]
public void TheOpeningKeepsTelAvivAndJaffaInsideTheScreen(int width, int height)
{
EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
// A target of the screen's size gives the camera that pixel rect and aspect (pixelRect's setter divides by the editor's
// own screen size, which a batch run does not fix).
var camera = Camera.main; var target = new RenderTexture(width, height, 24);
try
{
camera.targetTexture = target; camera.aspect = (float)width / height;
var map = Object.FindFirstObjectByType<MapScene>(); map.Build(Inputs.Map);
const float margin = 0.05f;
foreach (var id in new[] { "tel_aviv", "jaffa" })
{
var place = map.Settlements.Single(s => s.Id == id);
var screen = camera.WorldToScreenPoint(map.Frame.ToWorld(place.Lon, place.Lat, 0f));
Assert.Greater(screen.z, 0f, $"{width}x{height}: {id} is in front of the camera");
Assert.That(screen.x, Is.InRange(width * margin, width * (1f - margin)), $"{width}x{height}: {id} x");
Assert.That(screen.y, Is.InRange(height * margin, height * (1f - margin)), $"{width}x{height}: {id} y");
}
}
finally { camera.targetTexture = null; target.Release(); Object.DestroyImmediate(target); }
}
/// <summary>A 0-size canvas (a WebGL page booted in a background tab) gives no usable aspect: the opening falls back to
/// today's 16:9 look-at, the reference point itself, instead of throwing and failing the map boot.</summary>
[TestCase(0f)]
[TestCase(-1f)]
[TestCase(float.NaN)]
[TestCase(float.PositiveInfinity)]
[TestCase(float.NegativeInfinity)]
public void AnUnusableAspectFallsBackToTheSixteenByNineOpening(float aspect)
{
EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
var map = Object.FindFirstObjectByType<MapScene>(); map.Build(Inputs.Map);
var telAviv = map.Settlements.Single(s => s.Id == "tel_aviv");
var jaffa = map.Settlements.Single(s => s.Id == "jaffa");
Vector3 lookAt = default;
Assert.DoesNotThrow(() => lookAt = CameraRig.OpeningLookAt(map.Frame, aspect, telAviv, jaffa), $"aspect {aspect}");
Assert.AreEqual(map.Frame.ToWorld(CameraRig.StartLon, CameraRig.StartLat, 0f), lookAt, $"aspect {aspect}");
}
[UnityTest]
public IEnumerator CanBeSkippedAndReplayed()
{
saveDirectory = Path.Combine(Path.GetTempPath(), "statecraft-opening-" + Guid.NewGuid().ToString("N"));
var store = new SaveStore(saveDirectory);
EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
Object.FindFirstObjectByType<MapScene>().Build(Inputs.Map);
var root = Object.FindFirstObjectByType<GameRoot>();
root.Store = store;
root.Language = "en";
root.OpeningInEditMode = true;
var camera = Object.FindFirstObjectByType<CameraController>();
root.Begin(Inputs, 0, startScreen: true);
Assert.IsTrue(StartOpen, "precondition: the real start screen is open");
var bridge = root.Bridge!;
var session = bridge.Session;
var hash = bridge.State.Hash();
var save = session.Save.Write();
var orders = bridge.Queued.ToArray();
// This is the existing no-save New Game path, not a direct call into a future opening presenter.
UiTestKit.Press(Ui.Q<Button>("start-new-game"));
yield return null;
var opening = Ui.Q("opening-layer");
Assert.IsNotNull(opening, "New Game must show the opening before the first turn (#530)");
Assert.AreEqual(DisplayStyle.Flex, opening.resolvedStyle.display, "the opening is displayed");
Assert.IsFalse(camera.enabled, "the map cannot receive camera input behind the opening");
UiTestKit.Press(Ui.Q<Button>("opening-skip"));
yield return null;
Assert.AreEqual(DisplayStyle.None, opening.resolvedStyle.display, "Skip closes the opening");
Assert.IsFalse(StartOpen, "skipping a new game's opening returns to the map");
Assert.IsTrue(camera.enabled, "the map's camera is returned after Skip");
Assert.AreSame(session, bridge.Session, "the opening does not start another session");
Assert.AreEqual(hash, bridge.State.Hash(), "the opening does not advance the state or RNG");
Assert.AreEqual(save, bridge.Session.Save.Write(), "the opening does not alter the replay");
CollectionAssert.AreEqual(orders, bridge.Queued, "the opening queues no orders");
Assert.IsFalse(File.Exists(store.AutosavePath), "presentation alone does not create an autosave");
store.WriteAutosave(save);
root.enabled = false;
root.enabled = true;
root.Begin(Inputs, 0, startScreen: true);
Assert.IsTrue(StartOpen, "precondition: a saved session is behind the start screen");
UiTestKit.Press(Ui.Q<Button>("start-continue"));
yield return null;
Assert.IsFalse(StartOpen);
Assert.AreEqual(DisplayStyle.None, Ui.Q("opening-layer").resolvedStyle.display, "Continue does not replay the opening");
Assert.AreEqual(hash, root.Bridge!.State.Hash());
root.enabled = false;
root.enabled = true;
root.Begin(Inputs, 0, startScreen: true);
bridge = root.Bridge!;
session = bridge.Session;
orders = bridge.Queued.ToArray();
UiTestKit.Press(Ui.Q<Button>("start-opening-replay"));
yield return null;
opening = Ui.Q("opening-layer");
Assert.IsNotNull(opening);
Assert.AreEqual(DisplayStyle.Flex, opening.resolvedStyle.display, "the start screen can replay the opening");
Assert.IsFalse(camera.enabled);
UiTestKit.Press(Ui.Q<Button>("opening-skip"));
yield return null;
Assert.AreEqual(DisplayStyle.None, opening.resolvedStyle.display);
Assert.IsTrue(StartOpen, "skipping a replay returns to the start screen");
Assert.IsFalse(camera.enabled, "the start screen still shields the map");
Assert.AreSame(session, bridge.Session, "replay keeps the loaded session");
Assert.AreEqual(hash, bridge.State.Hash(), "replay leaves state and RNG unchanged");
CollectionAssert.AreEqual(orders, bridge.Queued, "replay queues no orders");
Assert.AreEqual(save, bridge.Session.Save.Write());
Assert.AreEqual(save, File.ReadAllText(store.AutosavePath), "replay does not rewrite the autosave");
}
}