MarketAlly.AIPlugin.Extensions/MarketAlly.AIPlugin.Learnin.../Services/UnifiedContextServiceTests.cs

232 lines
8.3 KiB
C#
Executable File

using MarketAlly.AIPlugin.Learning.Configuration;
using MarketAlly.AIPlugin.Learning.Models;
using MarketAlly.AIPlugin.Learning.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace MarketAlly.AIPlugin.Learning.Tests.Services
{
[TestClass]
public class UnifiedContextServiceTests
{
private UnifiedContextService _unifiedContextService;
private Mock<ILLMContextService> _mockLlmContextService;
private Mock<ILogger<UnifiedContextService>> _mockLogger;
private LearningConfiguration _config;
[TestInitialize]
public void Setup()
{
_mockLlmContextService = new Mock<ILLMContextService>();
_mockLogger = new Mock<ILogger<UnifiedContextService>>();
_config = new LearningConfiguration
{
AI = new AIConfiguration
{
MaxContextTokens = 8000,
EnableSemanticSearch = true,
MaxSearchResults = 10
},
Git = new GitConfiguration(),
Security = new SecurityConfiguration(),
Performance = new PerformanceConfiguration(),
LearningModes = new LearningModeConfiguration(),
Logging = new LoggingConfiguration()
};
var options = Options.Create(_config);
_unifiedContextService = new UnifiedContextService(options, _mockLogger.Object);
}
[TestMethod]
public void UnifiedContextService_Constructor_InitializesCorrectly()
{
// Arrange
var options = Options.Create(_config);
// Act & Assert - Should not throw
var service = new UnifiedContextService(options, _mockLogger.Object);
Assert.IsNotNull(service);
}
[TestMethod]
public async Task PrepareFullContextAsync_ValidQuery_ReturnsComprehensiveContext()
{
// Arrange
var query = "analyze code for refactoring opportunities";
var llmContext = new LLMContext
{
Query = query,
MaxTokens = 8000,
CodeChunks = new List<CodeChunk>(),
Dependencies = new List<DependencyInfo>(),
Relationships = new List<CodeRelationship>(),
GeneratedAt = DateTime.UtcNow
};
_mockLlmContextService
.Setup(x => x.PrepareContextAsync(It.IsAny<string>(), It.IsAny<int>()))
.ReturnsAsync(llmContext);
// Act
var result = await _unifiedContextService.PrepareFullContextAsync(query);
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(query, result.Query);
}
[TestMethod]
public async Task InitializeLearningSessionAsync_ValidParameters_ReturnsSessionContext()
{
// Arrange
var projectPath = Path.GetTempPath();
var topic = "Test learning session";
// Act
var result = await _unifiedContextService.InitializeLearningSessionAsync(projectPath, topic);
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(projectPath, result.ProjectPath);
Assert.IsNotNull(result.SessionId);
}
[TestMethod]
public async Task StoreLearningInsightAsync_ValidInsight_DoesNotThrow()
{
// Arrange
var insight = "Test insight";
var category = "refactoring";
var filePath = "test.cs";
var metadata = new Dictionary<string, object> { { "test", "value" } };
// Act & Assert - Should not throw
await _unifiedContextService.StoreLearningInsightAsync(insight, category, filePath, metadata);
}
[TestMethod]
public async Task FindSimilarPastIssuesAsync_ValidIssue_ReturnsHistoricalInsights()
{
// Arrange
var currentIssue = "Code complexity issue";
var projectPath = Path.GetTempPath();
// Act
var result = await _unifiedContextService.FindSimilarPastIssuesAsync(currentIssue, projectPath);
// Assert
Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(List<HistoricalInsight>));
}
[TestMethod]
public async Task GetRelatedDecisionsAsync_ValidSymbol_ReturnsPreviousDecisions()
{
// Arrange
var symbolName = "UserService";
var operationType = "refactor";
// Act
var result = await _unifiedContextService.GetRelatedDecisionsAsync(symbolName, operationType);
// Assert
Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(List<PreviousDecision>));
}
[TestMethod]
public async Task StoreRefactoringDecisionAsync_ValidDecision_DoesNotThrow()
{
// Arrange
var decision = "Extract method from large function";
var reasoning = "Improves readability and maintainability";
var filePath = "UserService.cs";
var successful = true;
// Act & Assert - Should not throw
await _unifiedContextService.StoreRefactoringDecisionAsync(decision, reasoning, filePath, successful);
}
[TestMethod]
public async Task FinalizeLearningSessionAsync_ValidSummary_ReturnsSessionSummary()
{
// Arrange
var sessionSummary = "Learning session completed successfully";
var metrics = new Dictionary<string, object>
{
{ "filesProcessed", 5 },
{ "successfulRefactorings", 3 },
{ "duration", TimeSpan.FromMinutes(10) }
};
// Act
var result = await _unifiedContextService.FinalizeLearningSessionAsync(sessionSummary, metrics);
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(sessionSummary, result.Summary);
Assert.IsNotNull(result.Metrics);
}
[TestMethod]
public async Task PrepareFullContextAsync_WithFilePath_ReturnsFileSpecificContext()
{
// Arrange
var query = "analyze specific file";
var filePath = "UserService.cs";
var llmContext = new LLMContext
{
Query = query,
MaxTokens = 8000,
PrimaryFile = filePath,
CodeChunks = new List<CodeChunk>(),
Dependencies = new List<DependencyInfo>(),
Relationships = new List<CodeRelationship>(),
GeneratedAt = DateTime.UtcNow
};
_mockLlmContextService
.Setup(x => x.PrepareCodeAnalysisContextAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()))
.ReturnsAsync(llmContext);
// Act
var result = await _unifiedContextService.PrepareFullContextAsync(query, filePath);
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(query, result.Query);
Assert.AreEqual(filePath, result.FilePath);
}
[TestMethod]
public async Task PrepareFullContextAsync_CachingEnabled_UsesCacheForSameQuery()
{
// Arrange
var query = "test caching";
var llmContext = new LLMContext
{
Query = query,
MaxTokens = 8000,
CodeChunks = new List<CodeChunk>(),
Dependencies = new List<DependencyInfo>(),
Relationships = new List<CodeRelationship>(),
GeneratedAt = DateTime.UtcNow
};
_mockLlmContextService
.Setup(x => x.PrepareContextAsync(It.IsAny<string>(), It.IsAny<int>()))
.ReturnsAsync(llmContext);
// Act
var result1 = await _unifiedContextService.PrepareFullContextAsync(query);
var result2 = await _unifiedContextService.PrepareFullContextAsync(query);
// Assert
Assert.IsNotNull(result1);
Assert.IsNotNull(result2);
Assert.AreEqual(result1.Query, result2.Query);
}
}
}