MarketAlly.AIPlugin.Extensions/MarketAlly.AIPlugin.Learnin.../TestHelpers/TestDataBuilder.cs

279 lines
10 KiB
C#
Executable File

using MarketAlly.AIPlugin.Learning.Configuration;
using MarketAlly.AIPlugin.Learning.Services;
namespace MarketAlly.AIPlugin.Learning.Tests.TestHelpers
{
public static class TestDataBuilder
{
public static LearningConfiguration CreateValidConfiguration()
{
return new LearningConfiguration
{
Git = new GitConfiguration
{
BranchPrefix = "test-ai-refactoring",
CommitterName = "Test AI Learning System",
CommitterEmail = "test-ai@learning.system",
AutoMerge = false,
RequireCleanWorkingDirectory = true,
MaxBranchRetentionDays = 30
},
Security = new SecurityConfiguration
{
ForbiddenDirectories = new string[] { "bin", "obj", ".git", "node_modules" },
AllowedFileExtensions = new string[] { ".cs", ".csproj", ".sln", ".json" },
MaxFileSizeBytes = 10 * 1024 * 1024,
MaxFilesPerSession = 1000,
EnablePathValidation = true,
EnableInputSanitization = true
},
AI = new AIConfiguration
{
EnableSemanticSearch = true,
MaxSearchResults = 10,
MinSimilarityScore = 0.7,
MaxContextTokens = 8000,
EnableContextPreparation = true,
EnableDependencyTracking = true,
MaxContextDepth = 3
},
Performance = new PerformanceConfiguration
{
MaxConcurrentOperations = Environment.ProcessorCount,
BatchSize = 10,
EnableCaching = true,
CacheExpirationMinutes = 60,
RetryAttempts = 3
},
LearningModes = new LearningModeConfiguration
{
Conservative = new LearningModeSettings
{
Name = "Conservative",
MaxIterations = 10,
MaxAttemptsPerFile = 2,
TimeoutMinutes = 30,
AllowedApproaches = new string[] { "RenameVariable", "AddDocumentation" },
RiskThreshold = 0.1
},
Moderate = new LearningModeSettings
{
Name = "Moderate",
MaxIterations = 20,
MaxAttemptsPerFile = 3,
TimeoutMinutes = 60,
AllowedApproaches = new string[] { "RenameVariable", "ExtractMethod", "AddDocumentation" },
RiskThreshold = 0.3
},
Aggressive = new LearningModeSettings
{
Name = "Aggressive",
MaxIterations = 50,
MaxAttemptsPerFile = 5,
TimeoutMinutes = 120,
AllowedApproaches = new string[] { "ExtractMethod", "RenameVariable", "ReduceCoupling" },
RiskThreshold = 0.5
}
},
Logging = new LoggingConfiguration
{
EnableStructuredLogging = true,
LogLevel = "Information",
LogToFile = true,
LogDirectory = "Logs"
}
};
}
public static ComprehensiveLearningSession CreateValidLearningSession(string? solutionPath = null)
{
return new ComprehensiveLearningSession
{
SessionId = Guid.NewGuid(),
SolutionPath = solutionPath ?? @"C:\TestProject\Solution.sln",
ReportsDirectory = "TestReports",
LearningMode = "moderate",
MaxIterations = 20,
MaxAttemptsPerFile = 3,
SessionTimeoutMinutes = 60,
VerboseReporting = false,
SkipWarningsAnalysis = false,
EnableSemanticSearch = false,
OpenAIApiKey = null,
StartTime = DateTime.UtcNow
};
}
public static LLMContext CreateSampleLLMContext(int estimatedTokens = 1000)
{
return new LLMContext
{
Query = "test query",
MaxTokens = 8000,
EstimatedTokens = estimatedTokens,
GeneratedAt = DateTime.UtcNow,
CodeChunks = new List<CodeChunk>
{
new CodeChunk
{
FilePath = "TestFile.cs",
Content = "public class TestClass { public void TestMethod() { } }",
EstimatedTokens = estimatedTokens / 2,
RelevanceScore = 0.9f,
Type = CodeChunkType.PrimaryFile,
Symbols = new List<string> { "TestClass" },
LineStart = 1,
LineEnd = 3
}
},
Dependencies = new List<DependencyInfo>(),
Relationships = new List<CodeRelationship>()
};
}
public static DependencyContext CreateSampleDependencyContext(string targetSymbol = "TestClass")
{
return new DependencyContext
{
RootSymbol = targetSymbol,
MaxDepth = 3,
Dependencies = new List<DependencyInfo>()
};
}
public static ChangeImpactContext CreateSampleChangeImpactContext(string targetFile = "TestFile.cs", int targetLine = 10)
{
return new ChangeImpactContext
{
TargetFile = targetFile,
TargetLine = targetLine,
ChangeType = "CodeChange",
PotentiallyAffectedFiles = new List<string> { "TestFile.cs", "TestFileTests.cs" },
RiskLevel = "Low"
};
}
public static CodeRelationshipContext CreateSampleCodeRelationshipContext(string targetSymbol = "TestClass")
{
return new CodeRelationshipContext
{
TargetSymbol = targetSymbol,
Callers = new List<string> { "MainProgram.Main" },
Callees = new List<string> { "Console.WriteLine" },
Implementers = new List<string>(),
Inheritors = new List<string>()
};
}
public static SessionContext CreateSampleSessionContext(string projectPath = @"C:\TestProject")
{
return new SessionContext
{
SessionId = Guid.NewGuid().ToString(),
StartTime = DateTime.UtcNow,
ProjectPath = projectPath,
Metadata = new Dictionary<string, object>
{
["testData"] = true
}
};
}
public static Dictionary<string, object> CreateValidPluginParameters(string? solutionPath = null)
{
return new Dictionary<string, object>
{
["solutionPath"] = solutionPath ?? @"C:\TestProject\Solution.sln",
["reportsDirectory"] = "TestReports",
["maxIterations"] = 20,
["maxAttemptsPerFile"] = 3,
["sessionTimeoutMinutes"] = 60,
["verboseReporting"] = false,
["learningMode"] = "moderate",
["skipWarningsAnalysis"] = false,
["configPath"] = @"C:\TestConfig\refactoriq.json",
["enableAIEmbeddings"] = false,
["enableSemanticSearch"] = false,
["openAIApiKey"] = "test-api-key"
};
}
public static string CreateTemporarySolutionFile()
{
var tempPath = Path.GetTempFileName();
var solutionPath = Path.ChangeExtension(tempPath, ".sln");
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
Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""TestProject"", ""TestProject\TestProject.csproj"", ""{12345678-1234-1234-1234-123456789012}""
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{12345678-1234-1234-1234-123456789012}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12345678-1234-1234-1234-123456789012}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12345678-1234-1234-1234-123456789012}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12345678-1234-1234-1234-123456789012}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
";
File.WriteAllText(solutionPath, solutionContent);
File.Delete(tempPath); // Clean up the original temp file
return solutionPath;
}
public static string CreateTemporaryCSharpFile(string content = "")
{
if (string.IsNullOrEmpty(content))
{
content = @"
using System;
namespace TestProject
{
public class TestClass
{
public void TestMethod()
{
Console.WriteLine(""Hello, World!"");
}
public string TestProperty { get; set; } = ""Test"";
public int Calculate(int x, int y)
{
return x + y;
}
}
}
";
}
var tempPath = Path.GetTempFileName();
var csPath = Path.ChangeExtension(tempPath, ".cs");
File.WriteAllText(csPath, content);
File.Delete(tempPath); // Clean up the original temp file
return csPath;
}
public static ValidationResult CreateValidationResult(bool isValid, IEnumerable<string>? errors = null)
{
return isValid
? ValidationResult.Success()
: ValidationResult.Failure(errors ?? new[] { "Test validation error" });
}
}
}