acdream/tests/AcDream.Core.Tests/Physics/PhysicsDiagnosticsTests.cs
Erik cc3afbcbeb feat(phys): A6.P3 #98 — GfxObj dump infrastructure (ACDREAM_DUMP_GFXOBJS)
Mirror the existing ACDREAM_DUMP_CELLS pattern for GfxObj-owned geometry:
when ACDREAM_DUMP_GFXOBJS lists a hex GfxObj id, the first
PhysicsDataCache.CacheGfxObj for that id writes the full resolved
polygon table to a JSON fixture under
tests/AcDream.Core.Tests/Fixtures/issue98/0x{id:X8}.gfxobj.json (override
dir via ACDREAM_DUMP_GFXOBJS_DIR).

Motivation: the existing [resolve-bldg] probe captures GfxObj-level
metadata (id, BSP root radius, entity origin) but emits
"hitPoly: n/a (BSP path — side-channel not written)" because the
BSPQuery wire site that would populate LastBspHitPoly never landed.
A polygon-level dump at cache time bypasses that gap — one capture run
yields the FULL polygon table, fixture-loadable by the harness's
RegisterCottageGfxObj helper (next commit).

See docs/research/2026-05-23-a6-p3-issue98-comparison-harness-findings.md
for the cottage GfxObj 0x01000A2B context: landblock-baked static at
entity origin (130.5, 11.5, 94.0), responsible for the head-sphere cap
from below at world Z=94.0 that issue #98 is documenting.

Test baseline: 1183 + 8 pre-existing failures (serial run; +5 new tests
all pass; was 1178 + 8 pre-session).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 20:24:26 +02:00

168 lines
6 KiB
C#

using AcDream.Core.Physics;
using DatReaderWriter.Enums;
using System.Collections.Generic;
using System.Numerics;
using Xunit;
namespace AcDream.Core.Tests.Physics;
/// <summary>
/// L.2d slice 1 (2026-05-13) — unit coverage for the new
/// <see cref="PhysicsDiagnostics.ProbeBuildingEnabled"/> flag and
/// <see cref="PhysicsDiagnostics.LastBspHitPoly"/> diagnostic
/// side-channel.
///
/// <para>
/// The full multi-line <c>[resolve-bldg]</c> format itself is verified
/// by the slice's acceptance criterion #2 (live Holtburg-doorway
/// capture) — covering it here would require a heavy
/// <c>PhysicsEngine</c> + <c>ShadowObjectRegistry</c> + <c>Transition</c>
/// fixture for what's a diagnostic-only emission. These tests pin the
/// static API contract that the emission code depends on; if either of
/// these tests breaks the emission will start producing stale data or
/// failing to emit at all.
/// </para>
/// </summary>
public class PhysicsDiagnosticsTests
{
// -----------------------------------------------------------------------
// ProbeBuildingEnabled — flag gates the emission path.
// -----------------------------------------------------------------------
[Fact]
public void ProbeBuilding_StaticApi_Roundtrip()
{
bool initial = PhysicsDiagnostics.ProbeBuildingEnabled;
try
{
PhysicsDiagnostics.ProbeBuildingEnabled = true;
Assert.True(PhysicsDiagnostics.ProbeBuildingEnabled);
PhysicsDiagnostics.ProbeBuildingEnabled = false;
Assert.False(PhysicsDiagnostics.ProbeBuildingEnabled);
}
finally
{
// Restore so a process-wide static doesn't leak between tests
// (env-var init was the only thing that set this before).
PhysicsDiagnostics.ProbeBuildingEnabled = initial;
}
}
// -----------------------------------------------------------------------
// LastBspHitPoly — side-channel set by BSPQuery, read by FindObjCollisions.
//
// TransitionTypes.FindObjCollisions clears this to null before each
// shadow-entry dispatch; BSPQuery writes to it on hit when the probe is
// on; the emission site reads it. A failure here means the side-channel
// can't carry data through the call chain.
// -----------------------------------------------------------------------
[Fact]
public void LastBspHitPoly_StaticApi_Roundtrip()
{
ResolvedPolygon? initial = PhysicsDiagnostics.LastBspHitPoly;
try
{
PhysicsDiagnostics.LastBspHitPoly = null;
Assert.Null(PhysicsDiagnostics.LastBspHitPoly);
var synthetic = new ResolvedPolygon
{
Vertices = new[]
{
new Vector3(-1f, 0f, 0f),
new Vector3( 1f, 0f, 0f),
new Vector3( 1f, 0f, 2f),
new Vector3(-1f, 0f, 2f),
},
Plane = new System.Numerics.Plane(0f, 1f, 0f, -94.123f),
NumPoints = 4,
SidesType = CullMode.None,
};
PhysicsDiagnostics.LastBspHitPoly = synthetic;
var read = PhysicsDiagnostics.LastBspHitPoly;
Assert.NotNull(read);
Assert.Equal(4, read!.NumPoints);
Assert.Equal(synthetic.Plane.D, read.Plane.D);
Assert.Same(synthetic, read);
PhysicsDiagnostics.LastBspHitPoly = null;
Assert.Null(PhysicsDiagnostics.LastBspHitPoly);
}
finally
{
PhysicsDiagnostics.LastBspHitPoly = initial;
}
}
// -----------------------------------------------------------------------
// ProbePushBackEnabled — flag gates the [push-back] emission path.
// A6.P1 (2026-05-21).
// -----------------------------------------------------------------------
[Fact]
public void ProbePushBack_StaticApi_Roundtrip()
{
bool initial = PhysicsDiagnostics.ProbePushBackEnabled;
try
{
PhysicsDiagnostics.ProbePushBackEnabled = true;
Assert.True(PhysicsDiagnostics.ProbePushBackEnabled);
PhysicsDiagnostics.ProbePushBackEnabled = false;
Assert.False(PhysicsDiagnostics.ProbePushBackEnabled);
}
finally
{
PhysicsDiagnostics.ProbePushBackEnabled = initial;
}
}
// -----------------------------------------------------------------------
// ProbeStepWalkEnabled - flag gates the [step-walk] emission path.
// A6.P3 issue #98 (2026-05-23).
// -----------------------------------------------------------------------
[Fact]
public void ProbeStepWalk_StaticApi_Roundtrip()
{
bool initial = PhysicsDiagnostics.ProbeStepWalkEnabled;
try
{
PhysicsDiagnostics.ProbeStepWalkEnabled = true;
Assert.True(PhysicsDiagnostics.ProbeStepWalkEnabled);
PhysicsDiagnostics.ProbeStepWalkEnabled = false;
Assert.False(PhysicsDiagnostics.ProbeStepWalkEnabled);
}
finally
{
PhysicsDiagnostics.ProbeStepWalkEnabled = initial;
}
}
// -----------------------------------------------------------------------
// ProbeDumpGfxObjs — parallel of ProbeDumpCells (A6.P3 #98, evening v2).
// -----------------------------------------------------------------------
[Fact]
public void ProbeDumpGfxObjs_EnabledTracksIdSetNonEmpty()
{
var initial = PhysicsDiagnostics.ProbeDumpGfxObjIds;
try
{
PhysicsDiagnostics.ProbeDumpGfxObjIds = new HashSet<uint>();
Assert.False(PhysicsDiagnostics.ProbeDumpGfxObjsEnabled);
PhysicsDiagnostics.ProbeDumpGfxObjIds = new HashSet<uint> { 0x01000A2Bu };
Assert.True(PhysicsDiagnostics.ProbeDumpGfxObjsEnabled);
Assert.Contains(0x01000A2Bu, PhysicsDiagnostics.ProbeDumpGfxObjIds);
}
finally
{
PhysicsDiagnostics.ProbeDumpGfxObjIds = initial;
}
}
}