433 lines
15 KiB
C#
Executable File
433 lines
15 KiB
C#
Executable File
using MarketAlly.AIPlugin.Learning;
|
|
using MarketAlly.AIPlugin.Learning.Services;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace MarketAlly.AIPlugin.Learning.Tests.Plugins
|
|
{
|
|
[TestClass]
|
|
public class ComprehensiveLearningRefactorPluginTests
|
|
{
|
|
private ComprehensiveLearningRefactorPlugin _plugin;
|
|
|
|
[TestInitialize]
|
|
public void Setup()
|
|
{
|
|
_plugin = new ComprehensiveLearningRefactorPlugin();
|
|
}
|
|
|
|
[TestCleanup]
|
|
public void Cleanup()
|
|
{
|
|
_plugin?.Dispose();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Plugin_SupportedParameters_ContainsAllExpectedParameters()
|
|
{
|
|
// Act
|
|
var supportedParams = _plugin.SupportedParameters;
|
|
|
|
// Assert
|
|
Assert.IsNotNull(supportedParams);
|
|
Assert.IsTrue(supportedParams.ContainsKey("solutionPath"));
|
|
Assert.IsTrue(supportedParams.ContainsKey("reportsDirectory"));
|
|
Assert.IsTrue(supportedParams.ContainsKey("maxIterations"));
|
|
Assert.IsTrue(supportedParams.ContainsKey("maxAttemptsPerFile"));
|
|
Assert.IsTrue(supportedParams.ContainsKey("sessionTimeoutMinutes"));
|
|
Assert.IsTrue(supportedParams.ContainsKey("verboseReporting"));
|
|
Assert.IsTrue(supportedParams.ContainsKey("learningMode"));
|
|
Assert.IsTrue(supportedParams.ContainsKey("skipWarningsAnalysis"));
|
|
Assert.IsTrue(supportedParams.ContainsKey("configPath"));
|
|
Assert.IsTrue(supportedParams.ContainsKey("enableAIEmbeddings"));
|
|
Assert.IsTrue(supportedParams.ContainsKey("enableSemanticSearch"));
|
|
Assert.IsTrue(supportedParams.ContainsKey("openAIApiKey"));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Plugin_SupportedParameters_HasCorrectTypes()
|
|
{
|
|
// Act
|
|
var supportedParams = _plugin.SupportedParameters;
|
|
|
|
// Assert
|
|
Assert.AreEqual(typeof(string), supportedParams["solutionPath"]);
|
|
Assert.AreEqual(typeof(string), supportedParams["reportsDirectory"]);
|
|
Assert.AreEqual(typeof(int), supportedParams["maxIterations"]);
|
|
Assert.AreEqual(typeof(int), supportedParams["maxAttemptsPerFile"]);
|
|
Assert.AreEqual(typeof(int), supportedParams["sessionTimeoutMinutes"]);
|
|
Assert.AreEqual(typeof(bool), supportedParams["verboseReporting"]);
|
|
Assert.AreEqual(typeof(string), supportedParams["learningMode"]);
|
|
Assert.AreEqual(typeof(bool), supportedParams["skipWarningsAnalysis"]);
|
|
Assert.AreEqual(typeof(string), supportedParams["configPath"]);
|
|
Assert.AreEqual(typeof(bool), supportedParams["enableAIEmbeddings"]);
|
|
Assert.AreEqual(typeof(bool), supportedParams["enableSemanticSearch"]);
|
|
Assert.AreEqual(typeof(string), supportedParams["openAIApiKey"]);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Plugin_DefaultProperties_AreSetCorrectly()
|
|
{
|
|
// Assert
|
|
Assert.AreEqual("Reports", _plugin.ReportsDirectory);
|
|
Assert.AreEqual(20, _plugin.MaxIterations);
|
|
Assert.AreEqual(3, _plugin.MaxAttemptsPerFile);
|
|
Assert.AreEqual(60, _plugin.SessionTimeoutMinutes);
|
|
Assert.IsFalse(_plugin.VerboseReporting);
|
|
Assert.AreEqual("conservative", _plugin.LearningMode);
|
|
Assert.IsFalse(_plugin.SkipWarningsAnalysis);
|
|
Assert.IsFalse(_plugin.EnableAIEmbeddings);
|
|
Assert.IsFalse(_plugin.EnableSemanticSearch);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ExecuteAsync_MissingRequiredParameter_ReturnsFailureResult()
|
|
{
|
|
// Arrange
|
|
var parameters = new Dictionary<string, object>
|
|
{
|
|
// Missing required "solutionPath"
|
|
["maxIterations"] = 10
|
|
};
|
|
|
|
// Act
|
|
var result = await _plugin.ExecuteAsync(parameters);
|
|
|
|
// Assert
|
|
Assert.IsNotNull(result);
|
|
Assert.IsFalse(result.Success);
|
|
Assert.IsNotNull(result.Message);
|
|
Assert.IsTrue(result.Message.Contains("failed") || result.Message.Contains("error"));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ExecuteAsync_InvalidSolutionPath_ReturnsFailureResult()
|
|
{
|
|
// Arrange
|
|
var parameters = new Dictionary<string, object>
|
|
{
|
|
["solutionPath"] = @"C:\NonExistent\Solution.sln",
|
|
["learningMode"] = "conservative",
|
|
["maxIterations"] = 5
|
|
};
|
|
|
|
// Act
|
|
var result = await _plugin.ExecuteAsync(parameters);
|
|
|
|
// Assert
|
|
Assert.IsNotNull(result);
|
|
Assert.IsFalse(result.Success);
|
|
Assert.IsNotNull(result.Message);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ExecuteAsync_ValidMinimalParameters_CreatesCorrectSession()
|
|
{
|
|
// Arrange
|
|
var testSolutionPath = CreateTestSolutionFile();
|
|
var parameters = new Dictionary<string, object>
|
|
{
|
|
["solutionPath"] = testSolutionPath
|
|
};
|
|
|
|
try
|
|
{
|
|
// Act
|
|
var result = await _plugin.ExecuteAsync(parameters);
|
|
|
|
// Assert
|
|
Assert.IsNotNull(result);
|
|
// Result should either succeed or fail gracefully with detailed error
|
|
Assert.IsNotNull(result.Message);
|
|
}
|
|
finally
|
|
{
|
|
// Cleanup
|
|
if (File.Exists(testSolutionPath))
|
|
{
|
|
File.Delete(testSolutionPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ExecuteAsync_AllParameters_CreatesComprehensiveSession()
|
|
{
|
|
// Arrange
|
|
var testSolutionPath = CreateTestSolutionFile();
|
|
var parameters = new Dictionary<string, object>
|
|
{
|
|
["solutionPath"] = testSolutionPath,
|
|
["reportsDirectory"] = "TestReports",
|
|
["maxIterations"] = 15,
|
|
["maxAttemptsPerFile"] = 2,
|
|
["sessionTimeoutMinutes"] = 45,
|
|
["verboseReporting"] = true,
|
|
["learningMode"] = "moderate",
|
|
["skipWarningsAnalysis"] = true,
|
|
["configPath"] = @"C:\TestConfig\refactoriq.json",
|
|
["enableAIEmbeddings"] = true,
|
|
["enableSemanticSearch"] = true,
|
|
["openAIApiKey"] = "test-api-key"
|
|
};
|
|
|
|
try
|
|
{
|
|
// Act
|
|
var result = await _plugin.ExecuteAsync(parameters);
|
|
|
|
// Assert
|
|
Assert.IsNotNull(result);
|
|
Assert.IsNotNull(result.Message);
|
|
}
|
|
finally
|
|
{
|
|
// Cleanup
|
|
if (File.Exists(testSolutionPath))
|
|
{
|
|
File.Delete(testSolutionPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ExecuteAsync_ConservativeLearningMode_UsesCorrectConfiguration()
|
|
{
|
|
// Arrange
|
|
var testSolutionPath = CreateTestSolutionFile();
|
|
var parameters = new Dictionary<string, object>
|
|
{
|
|
["solutionPath"] = testSolutionPath,
|
|
["learningMode"] = "conservative",
|
|
["maxIterations"] = 5
|
|
};
|
|
|
|
try
|
|
{
|
|
// Act
|
|
var result = await _plugin.ExecuteAsync(parameters);
|
|
|
|
// Assert
|
|
Assert.IsNotNull(result);
|
|
// Conservative mode should be handled appropriately
|
|
}
|
|
finally
|
|
{
|
|
// Cleanup
|
|
if (File.Exists(testSolutionPath))
|
|
{
|
|
File.Delete(testSolutionPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ExecuteAsync_AggressiveLearningMode_UsesCorrectConfiguration()
|
|
{
|
|
// Arrange
|
|
var testSolutionPath = CreateTestSolutionFile();
|
|
var parameters = new Dictionary<string, object>
|
|
{
|
|
["solutionPath"] = testSolutionPath,
|
|
["learningMode"] = "aggressive",
|
|
["maxIterations"] = 25
|
|
};
|
|
|
|
try
|
|
{
|
|
// Act
|
|
var result = await _plugin.ExecuteAsync(parameters);
|
|
|
|
// Assert
|
|
Assert.IsNotNull(result);
|
|
// Aggressive mode should be handled appropriately
|
|
}
|
|
finally
|
|
{
|
|
// Cleanup
|
|
if (File.Exists(testSolutionPath))
|
|
{
|
|
File.Delete(testSolutionPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ExecuteAsync_SemanticSearchEnabled_IncludesAIFeatures()
|
|
{
|
|
// Arrange
|
|
var testSolutionPath = CreateTestSolutionFile();
|
|
var parameters = new Dictionary<string, object>
|
|
{
|
|
["solutionPath"] = testSolutionPath,
|
|
["enableSemanticSearch"] = true,
|
|
["openAIApiKey"] = "test-api-key"
|
|
};
|
|
|
|
try
|
|
{
|
|
// Act
|
|
var result = await _plugin.ExecuteAsync(parameters);
|
|
|
|
// Assert
|
|
Assert.IsNotNull(result);
|
|
// Should handle AI features appropriately
|
|
}
|
|
finally
|
|
{
|
|
// Cleanup
|
|
if (File.Exists(testSolutionPath))
|
|
{
|
|
File.Delete(testSolutionPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Plugin_Dispose_CanBeCalledMultipleTimes()
|
|
{
|
|
// Act & Assert - Should not throw
|
|
_plugin.Dispose();
|
|
_plugin.Dispose();
|
|
|
|
Assert.IsTrue(true); // Test passes if no exception is thrown
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Plugin_ServiceProvider_BuildsCorrectly()
|
|
{
|
|
// Arrange
|
|
var parameters = new Dictionary<string, object>
|
|
{
|
|
["solutionPath"] = @"C:\TestProject\Solution.sln",
|
|
["enableSemanticSearch"] = true
|
|
};
|
|
|
|
// This test verifies that the service provider can be built without throwing
|
|
// We can't easily test the private BuildServiceProvider method directly,
|
|
// but ExecuteAsync calls it internally
|
|
try
|
|
{
|
|
// Act - This will internally call BuildServiceProvider
|
|
var task = _plugin.ExecuteAsync(parameters);
|
|
|
|
// Assert - If we get here without exception, service provider building succeeded
|
|
Assert.IsNotNull(task);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// If there's an exception, it should be related to missing files or dependencies,
|
|
// not service provider configuration issues
|
|
Assert.IsFalse(ex.Message.Contains("dependency injection") ||
|
|
ex.Message.Contains("service provider") ||
|
|
ex.Message.Contains("registration"));
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ExecuteAsync_InvalidLearningMode_HandlesGracefully()
|
|
{
|
|
// Arrange
|
|
var testSolutionPath = CreateTestSolutionFile();
|
|
var parameters = new Dictionary<string, object>
|
|
{
|
|
["solutionPath"] = testSolutionPath,
|
|
["learningMode"] = "invalid-mode"
|
|
};
|
|
|
|
try
|
|
{
|
|
// Act
|
|
var result = await _plugin.ExecuteAsync(parameters);
|
|
|
|
// Assert
|
|
Assert.IsNotNull(result);
|
|
// Should handle invalid learning mode gracefully
|
|
Assert.IsNotNull(result.Message);
|
|
}
|
|
finally
|
|
{
|
|
// Cleanup
|
|
if (File.Exists(testSolutionPath))
|
|
{
|
|
File.Delete(testSolutionPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ExecuteAsync_ExceptionDuringExecution_ReturnsFailureResult()
|
|
{
|
|
// Arrange
|
|
var parameters = new Dictionary<string, object>
|
|
{
|
|
["solutionPath"] = "", // Invalid empty path
|
|
["learningMode"] = "conservative"
|
|
};
|
|
|
|
// Act
|
|
var result = await _plugin.ExecuteAsync(parameters);
|
|
|
|
// Assert
|
|
Assert.IsNotNull(result);
|
|
Assert.IsFalse(result.Success);
|
|
Assert.IsNotNull(result.Message);
|
|
Assert.IsTrue(result.Message.Contains("failed") || result.Message.Contains("error"));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Plugin_Properties_CanBeSetAndRetrieved()
|
|
{
|
|
// Arrange & Act
|
|
_plugin.SolutionPath = @"C:\TestProject\Solution.sln";
|
|
_plugin.ReportsDirectory = "CustomReports";
|
|
_plugin.MaxIterations = 25;
|
|
_plugin.MaxAttemptsPerFile = 4;
|
|
_plugin.SessionTimeoutMinutes = 90;
|
|
_plugin.VerboseReporting = true;
|
|
_plugin.LearningMode = "aggressive";
|
|
_plugin.SkipWarningsAnalysis = true;
|
|
_plugin.ConfigPath = @"C:\Config\test.json";
|
|
_plugin.EnableAIEmbeddings = true;
|
|
_plugin.EnableSemanticSearch = true;
|
|
_plugin.OpenAIApiKey = "test-key";
|
|
|
|
// Assert
|
|
Assert.AreEqual(@"C:\TestProject\Solution.sln", _plugin.SolutionPath);
|
|
Assert.AreEqual("CustomReports", _plugin.ReportsDirectory);
|
|
Assert.AreEqual(25, _plugin.MaxIterations);
|
|
Assert.AreEqual(4, _plugin.MaxAttemptsPerFile);
|
|
Assert.AreEqual(90, _plugin.SessionTimeoutMinutes);
|
|
Assert.IsTrue(_plugin.VerboseReporting);
|
|
Assert.AreEqual("aggressive", _plugin.LearningMode);
|
|
Assert.IsTrue(_plugin.SkipWarningsAnalysis);
|
|
Assert.AreEqual(@"C:\Config\test.json", _plugin.ConfigPath);
|
|
Assert.IsTrue(_plugin.EnableAIEmbeddings);
|
|
Assert.IsTrue(_plugin.EnableSemanticSearch);
|
|
Assert.AreEqual("test-key", _plugin.OpenAIApiKey);
|
|
}
|
|
|
|
private string CreateTestSolutionFile()
|
|
{
|
|
var tempPath = Path.GetTempFileName();
|
|
var solutionPath = Path.ChangeExtension(tempPath, ".sln");
|
|
|
|
// Create a minimal solution file content
|
|
var solutionContent = @"
|
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
|
# Visual Studio Version 17
|
|
VisualStudioVersion = 17.0.31903.59
|
|
MinimumVisualStudioVersion = 10.0.40219.1
|
|
Global
|
|
GlobalSection(SolutionProperties) = preSolution
|
|
HideSolutionNode = FALSE
|
|
EndGlobalSection
|
|
EndGlobal
|
|
";
|
|
|
|
File.WriteAllText(solutionPath, solutionContent);
|
|
File.Delete(tempPath); // Clean up the original temp file
|
|
|
|
return solutionPath;
|
|
}
|
|
}
|
|
} |