acdream/tests/AcDream.App.Tests/Net/GameWindowLiveSessionOwnershipTests.cs
Erik ce41efb9e5 refactor(runtime): cut graphical host over to canonical root
Make every App composition phase borrow one GameRuntime, retire the duplicate view/event adapters, and dispose the root only after its graphical borrowers release. This preserves synchronous UI commands while giving shutdown one exact ownership ledger.

Co-authored-by: OpenAI Codex <codex@openai.com>
2026-07-26 19:06:09 +02:00

143 lines
4.8 KiB
C#

using System.Reflection;
using AcDream.App.Rendering;
using AcDream.App.Net;
using AcDream.Core.Net;
using AcDream.Runtime;
using AcDream.Runtime.Session;
namespace AcDream.App.Tests.Net;
public sealed class GameWindowLiveSessionOwnershipTests
{
private const BindingFlags PrivateInstance =
BindingFlags.Instance | BindingFlags.NonPublic;
[Fact]
public void GameWindowRetainsCanonicalRuntimeAndFocusedHostButNoMirroredSession()
{
FieldInfo[] fields = typeof(GameWindow).GetFields(PrivateInstance);
Assert.Contains(
fields,
field => field.Name == "_runtime"
&& field.FieldType == typeof(GameRuntime));
Assert.Contains(
fields,
field => field.Name == "_liveSessionHost"
&& field.FieldType == typeof(LiveSessionHost));
Assert.DoesNotContain(
fields,
field => field.Name == "_liveSessionController"
|| field.FieldType == typeof(LiveSessionController));
Assert.DoesNotContain(fields, field => field.Name == "_liveSession");
Assert.DoesNotContain(fields, field => field.FieldType == typeof(WorldSession));
Assert.DoesNotContain(fields, field => field.FieldType == typeof(LiveSessionResetPlan));
Assert.DoesNotContain(fields, field => field.Name == "_liveSessionEvents");
Assert.DoesNotContain(fields, field => field.Name == "_liveSessionCommands");
}
[Fact]
public void GraphicalSessionSourceBorrowsCanonicalRuntimeState()
{
FieldInfo[] fields = typeof(LiveSessionAppSource).GetFields(PrivateInstance);
Assert.Equal(2, fields.Length);
Assert.Contains(
fields,
field => field.Name == "_session"
&& field.FieldType == typeof(LiveSessionController));
Assert.Contains(
fields,
field => field.Name == "_commands"
&& field.FieldType == typeof(LiveSessionCommandSurface));
Assert.DoesNotContain(fields, field => field.FieldType == typeof(WorldSession));
Assert.DoesNotContain(fields, field => field.FieldType == typeof(bool));
Assert.DoesNotContain(
fields,
field => field.FieldType == typeof(RuntimeGenerationToken)
|| field.FieldType == typeof(ulong));
}
[Fact]
public void ProductionWindowConstructsOnlyTheCanonicalRuntimeRoot()
{
string root = FindRepositoryRoot();
string source = File.ReadAllText(Path.Combine(
root,
"src",
"AcDream.App",
"Rendering",
"GameWindow.cs"));
Assert.Equal(
1,
CountOccurrences(source, "new GameRuntime("));
Assert.Contains(
"private readonly GameRuntime _runtime;",
source,
StringComparison.Ordinal);
Assert.Contains(
"_runtimeHostLease = _runtime.AcquireHostLease(",
source,
StringComparison.Ordinal);
string[] forbidden =
[
"new RuntimeEntityObjectLifetime(",
"new RuntimeInventoryState(",
"new RuntimeCharacterState(",
"new RuntimeCommunicationState(",
"new RuntimeActionState(",
"new RuntimeLocalPlayerMovementState(",
"new RuntimeWorldTransitState(",
"new LiveSessionController(",
"new GameRuntimeClock(",
];
Assert.All(
forbidden,
value => Assert.DoesNotContain(
value,
source,
StringComparison.Ordinal));
}
[Theory]
[InlineData("TryStartLiveSession")]
[InlineData("ClearInboundEntityState")]
[InlineData("WireLiveSessionEvents")]
[InlineData("DisposeLiveSessionRouting")]
[InlineData("CreateLiveSessionBinding")]
[InlineData("ApplyLiveSessionSelection")]
[InlineData("ApplyLiveSessionEnteredWorld")]
public void DisplacedLifecycleBodiesAreAbsent(string methodName)
{
Assert.Null(typeof(GameWindow).GetMethod(methodName, PrivateInstance));
}
private static int CountOccurrences(string source, string value)
{
int count = 0;
int cursor = 0;
while ((cursor = source.IndexOf(
value,
cursor,
StringComparison.Ordinal)) >= 0)
{
count++;
cursor += value.Length;
}
return count;
}
private static string FindRepositoryRoot()
{
var current = new DirectoryInfo(AppContext.BaseDirectory);
while (current is not null)
{
if (File.Exists(Path.Combine(current.FullName, "AcDream.slnx")))
return current.FullName;
current = current.Parent;
}
throw new DirectoryNotFoundException("AcDream.slnx was not found.");
}
}