using System.Numerics;
using AcDream.App.Input;
using AcDream.App.Physics;
using AcDream.Core.Physics;
using AcDream.Core.Physics.Motion;
using DatReaderWriter.Types;
using DatEnvCell = DatReaderWriter.DBObjs.EnvCell;
namespace AcDream.App.Tests.Input;
public sealed class PlayerMovementPlacementTransactionTests
{
private const uint PlayerGuid = 0x5000_0001u;
private const uint TargetGuid = 0x7000_0001u;
private const uint PriorCell = 0x0101_0101u;
private const uint DestinationCell = 0x0101_0102u;
///
/// C5a (2026-08-05) re-point: PlayerMovementController.CommitPreparedPosition
/// is deleted (zero production callers — production placement now arms
/// the login-entry leash via ArmConstraintLeashAtCommittedPlacement,
/// called from RuntimeLocalPlayerPhysicsPublicationState.ArmFirstEntryConstraintLeash
/// AFTER RuntimeSetPositionState.TryApplyDormantLocalActivationFinalCommit
/// has already published the shared current cell — see that method's
/// doc comment). Architecture-review correction (A5, 2026-08-05): the two
/// deleted invariants did NOT relocate symmetrically.
/// Render-root publish DID move — it happens inside
/// RuntimeSetPositionState.TryApplyDormantLocalActivationFinalCommit
/// itself, via _physics.Engine.UpdatePlayerCurrCell(result.CellId)
/// (RuntimeSetPositionState.cs:2774).
/// Sticky-target release did NOT move anywhere — grep -rn
/// "UnStick" src/AcDream.Runtime/ returns zero call sites on this
/// path; the only local-player UnStick left is
/// PlayerMovementController.SetPositionCore's
/// if (publishSharedState) PositionManager?.UnStick();, and
/// PreparePositionForCommit passes publishSharedState: false.
/// The unstick-at-first-entry-commit behaviour CommitPreparedPosition
/// used to perform is simply gone — it was dead code (the method had zero
/// production callers before this slice deleted it), so nothing regresses
/// today, but no layer pins the invariant "first-entry commit releases any
/// sticky target" any more. That is disposition 3.6's one real coverage
/// loss (see the C5a commit message).
/// This App-layer fixture (a bare +
/// pair, no Runtime activation pipeline)
/// cannot drive the Runtime final-commit transaction without
/// disproportionate fixture growth — recorded here as the explicit C5a
/// scope decision rather than silently dropped.
/// What THIS test still proves, unchanged:
/// defers the render-root publish exactly as before (controller.CellId
/// updates immediately; physics.DataCache.CellGraph.CurrCell and
/// the sticky target stay untouched), and the surviving production
/// commit-arm, ,
/// arms the constraint leash without independently touching either —
/// neither the render-root publish (a DIFFERENT layer's job now) nor the
/// sticky release (nobody's job any more).
///
[Fact]
public void PreparedPosition_DefersRenderRootPublish_CommitArmsLeashOnly()
{
PhysicsEngine physics = PhysicsWithCells(PriorCell, DestinationCell);
physics.UpdatePlayerCurrCell(PriorCell);
var controller = new PlayerMovementController(physics);
var hosts = new Dictionary();
IPhysicsObjHost? Resolve(uint id) => hosts.GetValueOrDefault(id);
EntityPhysicsHost player = Host(PlayerGuid, Resolve);
EntityPhysicsHost target = Host(TargetGuid, Resolve);
hosts.Add(PlayerGuid, player);
hosts.Add(TargetGuid, target);
controller.PositionManager = player.PositionManager;
player.PositionManager.StickTo(TargetGuid, 0.5f, 1.8f);
controller.PreparePositionForCommit(
new Vector3(2f, 3f, 4f),
DestinationCell,
new Vector3(2f, 3f, 4f));
Assert.Equal(DestinationCell, controller.CellId);
Assert.Equal(PriorCell, physics.DataCache!.CellGraph.CurrCell!.Id);
Assert.Equal(TargetGuid, player.PositionManager.GetStickyObjectId());
Assert.False(player.PositionManager.IsFullyConstrained());
Assert.Null(player.PositionManager.Constraint);
controller.ArmConstraintLeashAtCommittedPlacement();
// The leash is armed...
Assert.NotNull(player.PositionManager.Constraint);
Assert.True(player.PositionManager.Constraint!.IsConstrained);
// ...but the surviving App-layer commit does NOT publish the render
// root or release the sticky target — those now happen inside the
// Runtime dormant-activation final commit, before this method runs.
Assert.Equal(PriorCell, physics.DataCache.CellGraph.CurrCell!.Id);
Assert.Equal(TargetGuid, player.PositionManager.GetStickyObjectId());
}
private static PhysicsEngine PhysicsWithCells(params uint[] cellIds)
{
var physics = new PhysicsEngine { DataCache = new PhysicsDataCache() };
foreach (uint cellId in cellIds)
{
var cellStruct = new CellStruct
{
VertexArray = new VertexArray
{
Vertices = new Dictionary(),
},
Polygons = new Dictionary(),
CellBSP = new CellBSPTree
{
Root = new CellBSPNode
{
Type = DatReaderWriter.Enums.BSPNodeType.Leaf,
},
},
};
var envCell = new DatEnvCell
{
CellPortals = [],
VisibleCells = [],
};
physics.DataCache.CacheCellStruct(
cellId,
envCell,
cellStruct,
Matrix4x4.Identity);
}
return physics;
}
private static EntityPhysicsHost Host(
uint guid,
Func resolve) =>
new(
guid,
getPosition: () => new AcDream.Core.Physics.Position(
PriorCell,
Vector3.Zero,
Quaternion.Identity),
getVelocity: () => Vector3.Zero,
getRadius: () => 0.5f,
inContact: () => true,
minterpMaxSpeed: () => 1f,
curTime: () => 0d,
physicsTimerTime: () => 0d,
getObjectA: resolve,
handleUpdateTarget: _ => { },
interruptCurrentMovement: () => { });
}