Step 3 of multi-instancing migration. The dominant per-battle singleton now resolves through BattleAmbient.Current.Mgr when a scope is active. The legacy 'main' field is renamed _mainFallback and retained for unwrapped callers (tests, anything not yet scope-wrapped). GetIns() still returns null when neither is set, preserving the '?.Foo ?? default' patterns in engine code. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
152 lines
4.8 KiB
C#
152 lines
4.8 KiB
C#
#nullable enable
|
|
using SVSim.BattleEngine.Ambient;
|
|
using NUnit.Framework;
|
|
using System.Runtime.Serialization;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SVSim.BattleEngine.Tests;
|
|
|
|
[TestFixture, Parallelizable(ParallelScope.Self)]
|
|
public class BattleAmbientTests
|
|
{
|
|
[Test]
|
|
public void Current_IsNull_WhenNoScope()
|
|
{
|
|
Assert.That(BattleAmbient.Current, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Require_Throws_WhenNoScope()
|
|
{
|
|
Assert.Throws<System.InvalidOperationException>(() => BattleAmbient.Require());
|
|
}
|
|
|
|
[Test]
|
|
public void Enter_SetsCurrent_RestoresOnDispose()
|
|
{
|
|
var ctx = new BattleAmbientContext { ViewerId = 42 };
|
|
Assert.That(BattleAmbient.Current, Is.Null);
|
|
using (var _ = BattleAmbient.Enter(ctx))
|
|
{
|
|
Assert.That(BattleAmbient.Current, Is.SameAs(ctx));
|
|
Assert.That(BattleAmbient.Require().ViewerId, Is.EqualTo(42));
|
|
}
|
|
Assert.That(BattleAmbient.Current, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Enter_Nested_RestoresPriorOnDispose()
|
|
{
|
|
var outer = new BattleAmbientContext { ViewerId = 1 };
|
|
var inner = new BattleAmbientContext { ViewerId = 2 };
|
|
using (var _o = BattleAmbient.Enter(outer))
|
|
{
|
|
Assert.That(BattleAmbient.Current!.ViewerId, Is.EqualTo(1));
|
|
using (var _i = BattleAmbient.Enter(inner))
|
|
{
|
|
Assert.That(BattleAmbient.Current!.ViewerId, Is.EqualTo(2));
|
|
}
|
|
Assert.That(BattleAmbient.Current!.ViewerId, Is.EqualTo(1));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task Enter_FlowsAcrossAwait()
|
|
{
|
|
var ctx = new BattleAmbientContext { ViewerId = 99 };
|
|
using (var _ = BattleAmbient.Enter(ctx))
|
|
{
|
|
await Task.Yield();
|
|
Assert.That(BattleAmbient.Current, Is.SameAs(ctx));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task Enter_IsolatedBetweenConcurrentTasks()
|
|
{
|
|
var ctxA = new BattleAmbientContext { ViewerId = 100 };
|
|
var ctxB = new BattleAmbientContext { ViewerId = 200 };
|
|
|
|
var taskA = Task.Run(async () => {
|
|
using var _ = BattleAmbient.Enter(ctxA);
|
|
await Task.Delay(20);
|
|
return BattleAmbient.Current!.ViewerId;
|
|
});
|
|
var taskB = Task.Run(async () => {
|
|
using var _ = BattleAmbient.Enter(ctxB);
|
|
await Task.Delay(20);
|
|
return BattleAmbient.Current!.ViewerId;
|
|
});
|
|
|
|
var results = await Task.WhenAll(taskA, taskB);
|
|
Assert.That(results[0], Is.EqualTo(100));
|
|
Assert.That(results[1], Is.EqualTo(200));
|
|
}
|
|
|
|
[Test]
|
|
public void IsForecast_ReadsAmbient_WhenScopeActive()
|
|
{
|
|
var ctx = new BattleAmbientContext { IsForecast = false };
|
|
using var _ = BattleAmbient.Enter(ctx);
|
|
Assert.That(BattleManagerBase.IsForecast, Is.False);
|
|
ctx.IsForecast = true;
|
|
Assert.That(BattleManagerBase.IsForecast, Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void IsForecast_WriteInsideScope_WritesAmbient_NotFallback()
|
|
{
|
|
var ctx = new BattleAmbientContext { IsForecast = false };
|
|
using (var _ = BattleAmbient.Enter(ctx))
|
|
{
|
|
BattleManagerBase.IsForecast = true;
|
|
Assert.That(ctx.IsForecast, Is.True);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void IsForecast_OutsideScope_FallsBackToStatic()
|
|
{
|
|
Assert.That(BattleAmbient.Current, Is.Null);
|
|
BattleManagerBase.IsForecast = true;
|
|
Assert.That(BattleManagerBase.IsForecast, Is.True);
|
|
BattleManagerBase.IsForecast = false;
|
|
Assert.That(BattleManagerBase.IsForecast, Is.False);
|
|
}
|
|
|
|
[Test]
|
|
public void IsRandomDraw_RoundtripsAmbient_And_Fallback()
|
|
{
|
|
Assert.That(BattleAmbient.Current, Is.Null);
|
|
BattleManagerBase.IsRandomDraw = true;
|
|
Assert.That(BattleManagerBase.IsRandomDraw, Is.True);
|
|
|
|
var ctx = new BattleAmbientContext { IsRandomDraw = false };
|
|
using (var _ = BattleAmbient.Enter(ctx))
|
|
{
|
|
Assert.That(BattleManagerBase.IsRandomDraw, Is.False);
|
|
}
|
|
Assert.That(BattleManagerBase.IsRandomDraw, Is.True);
|
|
|
|
BattleManagerBase.IsRandomDraw = false;
|
|
}
|
|
|
|
[Test]
|
|
public void GetIns_ReadsAmbient_WhenScopeActive()
|
|
{
|
|
var fakeMgr = (BattleManagerBase)System.Runtime.Serialization
|
|
.FormatterServices.GetUninitializedObject(typeof(BattleManagerBase));
|
|
var ctx = new BattleAmbientContext { Mgr = fakeMgr };
|
|
using var _ = BattleAmbient.Enter(ctx);
|
|
Assert.That(BattleManagerBase.GetIns(), Is.SameAs(fakeMgr));
|
|
}
|
|
|
|
[Test]
|
|
public void GetIns_OutsideScope_FallsBackToStatic()
|
|
{
|
|
Assert.That(BattleAmbient.Current, Is.Null);
|
|
var v = BattleManagerBase.GetIns();
|
|
Assert.Pass($"GetIns()={(v is null ? "null" : v.GetType().Name)}");
|
|
}
|
|
}
|