test: replace runtime root source freezes

This commit is contained in:
Erik 2026-08-18 16:02:15 +02:00
parent 80c7b44457
commit 3c492aedc2
4 changed files with 248 additions and 220 deletions

View file

@ -1,6 +1,7 @@
using System.Reflection;
using AcDream.App.Rendering;
using AcDream.App.Net;
using AcDream.App.Rendering;
using AcDream.App.Tests.Architecture;
using AcDream.Core.Net;
using AcDream.Runtime;
using AcDream.Runtime.Session;
@ -61,43 +62,34 @@ public sealed class GameWindowLiveSessionOwnershipTests
[Fact]
public void ProductionWindowConstructsOnlyTheCanonicalRuntimeRoot()
{
string root = FindRepositoryRoot();
string source = File.ReadAllText(Path.Combine(
root,
"src",
"AcDream.App",
"Rendering",
"GameWindow.cs"));
IReadOnlyList<CompiledCall> calls =
CompiledCallGraph.ReadDeclared(typeof(GameWindow));
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 =
Assert.Single(
calls,
call => call.Target.DeclaringType == typeof(GameRuntime)
&& call.Target.IsConstructor);
Assert.Single(
calls,
call => call.Target.DeclaringType == typeof(GameRuntime)
&& call.Target.Name == nameof(GameRuntime.AcquireHostLease));
HashSet<string> forbiddenRuntimeRoots =
[
"new RuntimeEntityObjectLifetime(",
"new RuntimeInventoryState(",
"new RuntimeCharacterState(",
"new RuntimeCommunicationState(",
"new RuntimeActionState(",
"new RuntimeLocalPlayerMovementState(",
"new RuntimeWorldTransitState(",
"new LiveSessionController(",
"new GameRuntimeClock(",
"RuntimeEntityObjectLifetime",
"RuntimeInventoryState",
"RuntimeCharacterState",
"RuntimeCommunicationState",
"RuntimeActionState",
"RuntimeLocalPlayerMovementState",
"RuntimeWorldTransitState",
nameof(LiveSessionController),
"GameRuntimeClock",
];
Assert.All(
forbidden,
value => Assert.DoesNotContain(
value,
source,
StringComparison.Ordinal));
Assert.DoesNotContain(
calls,
call => call.Target.IsConstructor
&& call.Target.DeclaringType is { } type
&& forbiddenRuntimeRoots.Contains(type.Name));
}
[Fact]
@ -110,14 +102,12 @@ public sealed class GameWindowLiveSessionOwnershipTests
// gated command route. A second construction site anywhere under
// src/AcDream.App would silently split that route into two, each
// with its own activation/dispose lifecycle.
string root = FindRepositoryRoot();
string appRoot = Path.Combine(root, "src", "AcDream.App");
int total = Directory
.EnumerateFiles(appRoot, "*.cs", SearchOption.AllDirectories)
.Sum(path => CountOccurrences(
File.ReadAllText(path),
"new LiveSessionCommandSurface("));
int total = typeof(GameWindow).Assembly
.GetTypes()
.SelectMany(CompiledCallGraph.ReadDeclared)
.Count(call => call.Target.DeclaringType
== typeof(LiveSessionCommandSurface)
&& call.Target.IsConstructor);
Assert.Equal(1, total);
}
@ -142,17 +132,11 @@ public sealed class GameWindowLiveSessionOwnershipTests
/// (the App-layer wiring that forwards those two Runtime events to
/// <c>SessionStatusWriter</c>, feeding the launcher's status-payload
/// cycle) leaves every test suite green. <c>LiveSessionRuntimeFactory</c>
/// has exactly one production construction site
/// (<c>SessionPlayerComposition.cs</c>), buried inside the full
/// <c>GameWindow</c> composition graph, and no test in this repository
/// constructs it directly — there is no practical seam to exercise the
/// wiring behaviorally without a <see cref="GameWindow"/>. This test
/// follows the SAME source-text-pin pattern the rest of this file
/// already uses for wiring that can't otherwise be unit-tested
/// (<see cref="ProductionWindowConstructsOnlyTheCanonicalRuntimeRoot"/>,
/// <see cref="DisplacedLifecycleBodiesAreAbsent"/>): it fails if either
/// delegate assignment is removed or its argument mapping changes. The
/// exact PAYLOAD shape these calls must produce is pinned separately,
/// has exactly one production construction site, buried inside the full
/// <c>GameWindow</c> composition graph. The compiled delegate targets are
/// therefore the narrowest non-mutating seam for proving both bindings and
/// their argument maps without opening a real graphical session. The exact
/// payload shape these calls must produce is pinned separately,
/// at <c>SessionStatusWriterTests.CharacterCreatedAndCreationFailed_WriteThePinnedShape</c>
/// (<c>tests/AcDream.Runtime.Tests/Session/SessionStatusWriterTests.cs</c>)
/// — together the two tests cover "the delegates are bound" (here) and
@ -161,69 +145,57 @@ public sealed class GameWindowLiveSessionOwnershipTests
[Fact]
public void LiveSessionRuntimeFactoryBindsCharacterCreatedAndCreationFailedToTheStatusWriter()
{
string root = FindRepositoryRoot();
string source = File.ReadAllText(Path.Combine(
root,
"src",
"AcDream.App",
"Net",
"LiveSessionRuntimeFactory.cs"));
MethodInfo create = typeof(LiveSessionRuntimeFactory).GetMethod(
nameof(LiveSessionRuntimeFactory.Create))!;
MethodBase[] targets = CompiledCallGraph.ReadMethodReferences(create)
.Select(call => call.Target)
.Where(method => method.GetMethodBody() is not null)
.Distinct()
.ToArray();
MethodBase created = Assert.Single(
targets,
target => CallsStatusWriter(target, nameof(SessionStatusWriter.CharacterCreated)));
MethodBase failed = Assert.Single(
targets,
target => CallsStatusWriter(target, nameof(SessionStatusWriter.CreationFailed)));
Assert.Contains(
"CharacterCreated: identity => _statusWriter.CharacterCreated(",
source,
StringComparison.Ordinal);
created.GetParameters(),
parameter => parameter.ParameterType
== typeof(RuntimeCharacterCreationIdentity));
AssertCallOrder(
created,
(typeof(RuntimeCharacterCreationIdentity), "get_Guid"),
(typeof(RuntimeCharacterCreationIdentity), "get_Name"),
(typeof(SessionStatusWriter), nameof(SessionStatusWriter.CharacterCreated)));
Assert.Contains(
"identity.Guid,",
source,
StringComparison.Ordinal);
Assert.Contains(
"identity.Name),",
source,
StringComparison.Ordinal);
Assert.Contains(
"CreationFailed: rejection => _statusWriter.CreationFailed(",
source,
StringComparison.Ordinal);
Assert.Contains(
"rejection.RawCode,",
source,
StringComparison.Ordinal);
Assert.Contains(
"rejection.Reason,",
source,
StringComparison.Ordinal);
Assert.Contains(
"rejection.AttemptedName)),",
source,
StringComparison.Ordinal);
failed.GetParameters(),
parameter => parameter.ParameterType
== typeof(RuntimeCharacterCreationRejection));
AssertCallOrder(
failed,
(typeof(RuntimeCharacterCreationRejection), "get_RawCode"),
(typeof(RuntimeCharacterCreationRejection), "get_Reason"),
(typeof(RuntimeCharacterCreationRejection), "get_AttemptedName"),
(typeof(SessionStatusWriter), nameof(SessionStatusWriter.CreationFailed)));
}
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 bool CallsStatusWriter(MethodBase method, string methodName) =>
CompiledCallGraph.Read(method).Any(call =>
call.Target.DeclaringType == typeof(SessionStatusWriter)
&& call.Target.Name == methodName);
private static string FindRepositoryRoot()
private static void AssertCallOrder(
MethodBase method,
params (Type Type, string Method)[] expected)
{
var current = new DirectoryInfo(AppContext.BaseDirectory);
while (current is not null)
IReadOnlyList<CompiledCall> calls = CompiledCallGraph.Read(method);
int cursor = -1;
foreach ((Type type, string name) in expected)
{
if (File.Exists(Path.Combine(current.FullName, "AcDream.slnx")))
return current.FullName;
current = current.Parent;
int found = CompiledCallGraph.IndexOf(calls, type, name, cursor + 1);
Assert.True(found > cursor, $"Missing compiled edge {type.FullName}.{name}.");
cursor = found;
}
throw new DirectoryNotFoundException("AcDream.slnx was not found.");
}
}