RequestAnalysisContextCommandTests.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using UnityEngine;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using LLM.Editor.Data;
  5. using LLM.Editor.Helper;
  6. using LLM.Editor.Commands;
  7. using UnityEngine.TestTools;
  8. namespace LLM.Editor.Tests.Unit
  9. {
  10. [TestFixture]
  11. public class RequestAnalysisContextCommandTests
  12. {
  13. private CommandContext _context;
  14. private bool _eventFired;
  15. private RequestAnalysisContextParams _receivedParams;
  16. [SetUp]
  17. public void SetUp()
  18. {
  19. _context = new CommandContext();
  20. _eventFired = false;
  21. _receivedParams = null;
  22. // Subscribe to the event before each test
  23. RequestAnalysisContextCommand.OnAnalysisContextRequested += HandleAnalysisContextRequested;
  24. }
  25. [TearDown]
  26. public void TearDown()
  27. {
  28. // Unsubscribe to prevent test pollution
  29. RequestAnalysisContextCommand.OnAnalysisContextRequested -= HandleAnalysisContextRequested;
  30. }
  31. private void HandleAnalysisContextRequested(RequestAnalysisContextParams parameters)
  32. {
  33. _eventFired = true;
  34. _receivedParams = parameters;
  35. }
  36. [Test]
  37. public void Execute_WithValidRoles_FiresEventWithCorrectParameters()
  38. {
  39. // Arrange
  40. var roles = new[] { "Character", "Weapon" };
  41. var parameters = new RequestAnalysisContextParams { subjectRoles = roles };
  42. var command = new RequestAnalysisContextCommand(parameters.ToJson());
  43. // Act
  44. var outcome = command.Execute(_context);
  45. // Assert
  46. Assert.AreEqual(CommandOutcome.Success, outcome);
  47. Assert.IsTrue(_eventFired, "The OnAnalysisContextRequested event should have been fired.");
  48. Assert.IsNotNull(_receivedParams, "The received parameters should not be null.");
  49. Assert.IsTrue(roles.SequenceEqual(_receivedParams.subjectRoles), "The subject roles in the event should match the input.");
  50. }
  51. [Test]
  52. public void Execute_WithNullRoles_ReturnsErrorAndDoesNotFireEvent()
  53. {
  54. // Arrange
  55. var parameters = new RequestAnalysisContextParams { subjectRoles = null };
  56. var command = new RequestAnalysisContextCommand(parameters.ToJson());
  57. LogAssert.Expect(LogType.Error, "[RequestAnalysisContextCommand] Invalid parameters. Subject roles are required.");
  58. // Act
  59. var outcome = command.Execute(_context);
  60. // Assert
  61. Assert.AreEqual(CommandOutcome.Error, outcome);
  62. Assert.IsFalse(_eventFired, "The OnAnalysisContextRequested event should not be fired for invalid parameters.");
  63. }
  64. [Test]
  65. public void Execute_WithEmptyRoles_ReturnsErrorAndDoesNotFireEvent()
  66. {
  67. // Arrange
  68. var parameters = new RequestAnalysisContextParams { subjectRoles = new string[0] };
  69. var command = new RequestAnalysisContextCommand(parameters.ToJson());
  70. LogAssert.Expect(LogType.Error, "[RequestAnalysisContextCommand] Invalid parameters. Subject roles are required.");
  71. // Act
  72. var outcome = command.Execute(_context);
  73. // Assert
  74. Assert.AreEqual(CommandOutcome.Error, outcome);
  75. Assert.IsFalse(_eventFired, "The OnAnalysisContextRequested event should not be fired for empty roles.");
  76. }
  77. }
  78. }