EditScriptCommandTests.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using NUnit.Framework;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.IO;
  5. using LLM.Editor.Commands;
  6. using LLM.Editor.Data;
  7. using LLM.Editor.Helper;
  8. using UnityEngine.TestTools;
  9. namespace LLM.Editor.Tests.Unit
  10. {
  11. [TestFixture]
  12. public class EditScriptCommandTests
  13. {
  14. private const string TestScriptName = "TempEditTestScript.cs";
  15. private string _testScriptPath;
  16. private string _testScriptGuid;
  17. private CommandContext _context;
  18. [SetUp]
  19. public void SetUp()
  20. {
  21. _context = new CommandContext();
  22. _testScriptPath = Path.Combine(Application.dataPath, TestScriptName);
  23. // Create a dummy script file for testing
  24. File.WriteAllText(_testScriptPath, "public class OldContent {}");
  25. AssetDatabase.Refresh();
  26. _testScriptGuid = AssetDatabase.AssetPathToGUID($"Assets/{TestScriptName}");
  27. Assert.IsFalse(string.IsNullOrEmpty(_testScriptGuid), "Setup failed: Could not get GUID for test script.");
  28. }
  29. [TearDown]
  30. public void TearDown()
  31. {
  32. // Clean up the dummy script
  33. if (File.Exists(_testScriptPath))
  34. {
  35. File.Delete(_testScriptPath);
  36. File.Delete(_testScriptPath + ".meta");
  37. AssetDatabase.Refresh();
  38. }
  39. }
  40. [Test]
  41. public void Execute_WithValidInput_OverwritesScriptContent()
  42. {
  43. // Arrange
  44. var newContent = "public class NewContent {}";
  45. var parameters = new EditScriptParams { scriptIdentifier = _testScriptGuid, newScriptContent = newContent };
  46. var command = new EditScriptCommand(parameters.ToJson());
  47. // Act
  48. var outcome = command.Execute(_context);
  49. // Assert
  50. Assert.AreEqual(CommandOutcome.Success, outcome);
  51. var fileContent = File.ReadAllText(_testScriptPath);
  52. Assert.AreEqual(newContent, fileContent);
  53. }
  54. [Test]
  55. public void Execute_WithInvalidGuid_ReturnsError()
  56. {
  57. // Arrange
  58. var parameters = new EditScriptParams { scriptIdentifier = "InvalidGuid", newScriptContent = "some content" };
  59. var command = new EditScriptCommand(parameters.ToJson());
  60. LogAssert.Expect(LogType.Error, "[EditScriptCommand] Could not find a valid script with GUID 'InvalidGuid'.");
  61. // Act
  62. var outcome = command.Execute(_context);
  63. // Assert
  64. Assert.AreEqual(CommandOutcome.Error, outcome);
  65. }
  66. [Test]
  67. public void Execute_WithEmptyContent_ReturnsError()
  68. {
  69. // Arrange
  70. var parameters = new EditScriptParams { scriptIdentifier = _testScriptGuid, newScriptContent = "" };
  71. var command = new EditScriptCommand(parameters.ToJson());
  72. LogAssert.Expect(LogType.Error, "[EditScriptCommand] Invalid parameters. Script identifier and new content are required.");
  73. // Act
  74. var outcome = command.Execute(_context);
  75. // Assert
  76. Assert.AreEqual(CommandOutcome.Error, outcome);
  77. }
  78. }
  79. }