using NUnit.Framework; using UnityEngine; using LLM.Editor.Commands; using LLM.Editor.Data; using LLM.Editor.Helper; namespace LLM.Editor.Tests.Unit { [TestFixture] public class SetHierarchyCommandTests { private GameObject _childObject; private GameObject _parentObject; private CommandContext _context; [SetUp] public void SetUp() { _childObject = new GameObject("ChildObject"); _parentObject = new GameObject("ParentObject"); _context = new CommandContext(); // Register objects with the context using their instance IDs as identifiers _context.IdentifierMap[_childObject.GetInstanceID().ToString()] = _childObject.GetInstanceID().ToString(); _context.IdentifierMap[_parentObject.GetInstanceID().ToString()] = _parentObject.GetInstanceID().ToString(); } [TearDown] public void TearDown() { if (_childObject != null) Object.DestroyImmediate(_childObject); if (_parentObject != null) Object.DestroyImmediate(_parentObject); } private SetHierarchyCommand CreateCommand(string childId, string parentId, int siblingIndex = -1) { var parameters = new SetHierarchyParams { childIdentifier = childId, parentIdentifier = parentId, siblingIndex = siblingIndex }; return new SetHierarchyCommand(parameters.ToJson()); } [Test] public void Execute_WithValidParent_SetsParentCorrectly() { // Arrange var command = CreateCommand(_childObject.GetInstanceID().ToString(), _parentObject.GetInstanceID().ToString()); // Act var outcome = command.Execute(_context); // Assert Assert.AreEqual(CommandOutcome.Success, outcome); Assert.AreEqual(_parentObject.transform, _childObject.transform.parent); } [Test] public void Execute_WithNullParent_MovesToRoot() { // Arrange _childObject.transform.SetParent(_parentObject.transform); // Pre-condition var command = CreateCommand(_childObject.GetInstanceID().ToString(), null); // Act var outcome = command.Execute(_context); // Assert Assert.AreEqual(CommandOutcome.Success, outcome); Assert.IsNull(_childObject.transform.parent); } [Test] public void Execute_WithSiblingIndex_SetsOrderCorrectly() { // Arrange // Create another child to verify sibling order var otherChild = new GameObject("OtherChild"); otherChild.transform.SetParent(_parentObject.transform); var command = CreateCommand(_childObject.GetInstanceID().ToString(), _parentObject.GetInstanceID().ToString(), 0); // Act var outcome = command.Execute(_context); // Assert Assert.AreEqual(CommandOutcome.Success, outcome); Assert.AreEqual(0, _childObject.transform.GetSiblingIndex()); Object.DestroyImmediate(otherChild); } [Test] public void Execute_WithInvalidChildId_ReturnsError() { // Arrange var command = CreateCommand("invalid_id", _parentObject.GetInstanceID().ToString()); // Act var outcome = command.Execute(_context); // Assert Assert.AreEqual(CommandOutcome.Error, outcome); Assert.IsNotNull(_context.ErrorMessage); Assert.IsTrue(_context.ErrorMessage.Contains("Could not find a valid child GameObject")); } } }