Files
ChatBot/ChatBot.Tests/Integration/ProgramIntegrationTests.cs
Leonid Pershin 1c910d7b7f
All checks were successful
SonarQube / Build and analyze (push) Successful in 3m54s
Unit Tests / Run Tests (push) Successful in 2m23s
add tests
2025-10-20 15:11:42 +03:00

551 lines
23 KiB
C#

using ChatBot.Data;
using ChatBot.Data.Interfaces;
using ChatBot.Data.Repositories;
using ChatBot.Models.Configuration;
using ChatBot.Models.Configuration.Validators;
using ChatBot.Services;
using ChatBot.Services.Interfaces;
using ChatBot.Services.Telegram.Commands;
using ChatBot.Services.Telegram.Interfaces;
using ChatBot.Services.Telegram.Services;
using ChatBot.Tests.TestUtilities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Moq;
using Telegram.Bot;
namespace ChatBot.Tests.Integration;
public class ProgramIntegrationTests : TestBase
{
protected override void ConfigureServices(IServiceCollection services)
{
// Add configuration
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(
new Dictionary<string, string?>
{
["TelegramBot:BotToken"] = "test_token",
["Ollama:Url"] = "http://localhost:11434",
["Ollama:DefaultModel"] = "llama2",
["AI:MaxTokens"] = "1000",
["AI:Temperature"] = "0.7",
["Database:ConnectionString"] =
"Host=localhost;Port=5432;Database=test_chatbot;Username=postgres;Password=postgres",
["Database:CommandTimeout"] = "30",
["Database:EnableSensitiveDataLogging"] = "false",
}
)
.Build();
services.AddSingleton<IConfiguration>(configuration);
services.AddLogging(builder => builder.AddDebug());
services.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>));
services.AddLogging(builder => builder.AddDebug());
services.AddLogging(builder => builder.AddDebug());
services.AddLogging(builder => builder.AddDebug());
services.AddLogging(builder => builder.AddDebug());
services.AddLogging(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Debug));
}
[Fact]
public void Program_SuccessfulConfigurationPipeline_ShouldBuildAndValidate()
{
// Arrange
var services = new ServiceCollection();
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(
new Dictionary<string, string?>
{
["TelegramBot:BotToken"] = "1234567890123456789012345678901234567890",
["Ollama:Url"] = "http://localhost:11434",
["Ollama:DefaultModel"] = "llama3",
["AI:CompressionThreshold"] = "100",
["Database:ConnectionString"] =
"Host=localhost;Port=5432;Database=test;Username=test;Password=test",
["Database:CommandTimeout"] = "30",
["Database:EnableSensitiveDataLogging"] = "false",
}
)
.Build();
services.AddSingleton<IConfiguration>(configuration);
services.AddLogging(builder => builder.AddDebug());
// Configure options and validators similar to Program.cs
services.Configure<TelegramBotSettings>(configuration.GetSection("TelegramBot"));
services.AddSingleton<
IValidateOptions<TelegramBotSettings>,
TelegramBotSettingsValidator
>();
services.Configure<OllamaSettings>(configuration.GetSection("Ollama"));
services.AddSingleton<IValidateOptions<OllamaSettings>, OllamaSettingsValidator>();
services.Configure<AISettings>(configuration.GetSection("AI"));
services.AddSingleton<IValidateOptions<AISettings>, AISettingsValidator>();
services.Configure<DatabaseSettings>(configuration.GetSection("Database"));
services.AddSingleton<IValidateOptions<DatabaseSettings>, DatabaseSettingsValidator>();
// Register health checks similar to Program.cs
services
.AddHealthChecks()
.AddCheck<ChatBot.Services.HealthChecks.OllamaHealthCheck>("ollama")
.AddCheck<ChatBot.Services.HealthChecks.TelegramBotHealthCheck>("telegram");
var serviceProvider = services.BuildServiceProvider();
// Act & Assert - validate options using validators
var telegramValidator = serviceProvider.GetRequiredService<
IValidateOptions<TelegramBotSettings>
>();
var ollamaValidator = serviceProvider.GetRequiredService<
IValidateOptions<OllamaSettings>
>();
var aiValidator = serviceProvider.GetRequiredService<IValidateOptions<AISettings>>();
var dbValidator = serviceProvider.GetRequiredService<IValidateOptions<DatabaseSettings>>();
var telegram = serviceProvider.GetRequiredService<IOptions<TelegramBotSettings>>().Value;
var ollama = serviceProvider.GetRequiredService<IOptions<OllamaSettings>>().Value;
var ai = serviceProvider.GetRequiredService<IOptions<AISettings>>().Value;
var db = serviceProvider.GetRequiredService<IOptions<DatabaseSettings>>().Value;
Assert.True(telegramValidator.Validate("TelegramBot", telegram).Succeeded);
Assert.True(ollamaValidator.Validate("Ollama", ollama).Succeeded);
Assert.True(aiValidator.Validate("AI", ai).Succeeded);
Assert.True(dbValidator.Validate("Database", db).Succeeded);
// Assert health checks are registered
var hcOptions = serviceProvider
.GetRequiredService<IOptions<HealthCheckServiceOptions>>()
.Value;
var registrations = hcOptions.Registrations.Select(r => r.Name).ToList();
Assert.Contains("ollama", registrations);
Assert.Contains("telegram", registrations);
}
[Fact]
public void ServiceRegistrations_ShouldIncludeCoreServicesAndHealthCheckTags()
{
// Arrange
var services = new ServiceCollection();
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(
new Dictionary<string, string?>
{
["TelegramBot:BotToken"] = "1234567890123456789012345678901234567890",
["Ollama:Url"] = "http://localhost:11434",
["Ollama:DefaultModel"] = "llama3",
["AI:CompressionThreshold"] = "100",
["Database:ConnectionString"] =
"Host=localhost;Port=5432;Database=test;Username=test;Password=test",
["Database:CommandTimeout"] = "30",
["Database:EnableSensitiveDataLogging"] = "false",
}
)
.Build();
services.AddSingleton<IConfiguration>(configuration);
// Options + validators
services.Configure<TelegramBotSettings>(configuration.GetSection("TelegramBot"));
services.AddSingleton<
IValidateOptions<TelegramBotSettings>,
TelegramBotSettingsValidator
>();
services.Configure<OllamaSettings>(configuration.GetSection("Ollama"));
services.AddSingleton<IValidateOptions<OllamaSettings>, OllamaSettingsValidator>();
services.Configure<AISettings>(configuration.GetSection("AI"));
services.AddSingleton<IValidateOptions<AISettings>, AISettingsValidator>();
services.Configure<DatabaseSettings>(configuration.GetSection("Database"));
services.AddSingleton<IValidateOptions<DatabaseSettings>, DatabaseSettingsValidator>();
// DbContext (in-memory for test)
services.AddDbContext<ChatBotDbContext>(options => options.UseInMemoryDatabase("test-db"));
// Repository and session storage
services.AddSingleton<ILogger<ChatSessionRepository>>(_ =>
NullLogger<ChatSessionRepository>.Instance
);
services.AddScoped<IChatSessionRepository, ChatSessionRepository>();
services.AddSingleton<ILogger<DatabaseSessionStorage>>(_ =>
NullLogger<DatabaseSessionStorage>.Instance
);
services.AddScoped<ISessionStorage, DatabaseSessionStorage>();
// Core services
services.AddSingleton<ILogger<ModelService>>(_ => NullLogger<ModelService>.Instance);
services.AddSingleton<ModelService>();
services.AddSingleton<ILogger<SystemPromptService>>(_ =>
NullLogger<SystemPromptService>.Instance
);
services.AddSingleton<SystemPromptService>();
services.AddSingleton<IHistoryCompressionService, HistoryCompressionService>();
services.AddSingleton<ILogger<HistoryCompressionService>>(_ =>
NullLogger<HistoryCompressionService>.Instance
);
services.AddSingleton<ILogger<AIService>>(_ => NullLogger<AIService>.Instance);
services.AddSingleton<IAIService, AIService>();
services.AddSingleton<ILogger<ChatService>>(_ => NullLogger<ChatService>.Instance);
services.AddScoped<ChatService>();
// IOllamaClient
services.AddSingleton<IOllamaClient>(sp =>
{
var opts = sp.GetRequiredService<IOptions<OllamaSettings>>();
return new OllamaClientAdapter(opts.Value.Url);
});
// Telegram services and commands
services.AddSingleton<ITelegramBotClient>(_ => new Mock<ITelegramBotClient>().Object);
services.AddSingleton<ILogger<TelegramMessageSender>>(_ =>
NullLogger<TelegramMessageSender>.Instance
);
services.AddSingleton<ITelegramMessageSenderWrapper>(_ =>
new Mock<ITelegramMessageSenderWrapper>().Object
);
services.AddSingleton<ITelegramMessageSender, TelegramMessageSender>();
services.AddSingleton<ILogger<TelegramErrorHandler>>(_ =>
NullLogger<TelegramErrorHandler>.Instance
);
services.AddSingleton<ITelegramErrorHandler, TelegramErrorHandler>();
services.AddSingleton<ILogger<CommandRegistry>>(_ => NullLogger<CommandRegistry>.Instance);
services.AddScoped<CommandRegistry>();
services.AddSingleton<ILogger<BotInfoService>>(_ => NullLogger<BotInfoService>.Instance);
services.AddSingleton<BotInfoService>();
services.AddSingleton<ILogger<TelegramCommandProcessor>>(_ =>
NullLogger<TelegramCommandProcessor>.Instance
);
services.AddScoped<ITelegramCommandProcessor, TelegramCommandProcessor>();
services.AddSingleton<ILogger<TelegramMessageHandler>>(_ =>
NullLogger<TelegramMessageHandler>.Instance
);
services.AddScoped<ITelegramMessageHandler, TelegramMessageHandler>();
services.AddScoped<ITelegramCommand, StartCommand>();
services.AddScoped<ITelegramCommand, HelpCommand>();
services.AddScoped<ITelegramCommand, ClearCommand>();
services.AddScoped<ITelegramCommand, SettingsCommand>();
services.AddScoped<ITelegramCommand, StatusCommand>();
// Hosted service simulation for Telegram bot service
services.AddSingleton<ILogger<TelegramBotService>>(_ =>
NullLogger<TelegramBotService>.Instance
);
services.AddSingleton<ITelegramBotService, TelegramBotService>();
services.AddSingleton<IHostedService>(sp =>
(IHostedService)sp.GetRequiredService<ITelegramBotService>()
);
// Simulate HealthCheck registrations via options to avoid bringing in DefaultHealthCheckService
var hcOptions = new HealthCheckServiceOptions();
hcOptions.Registrations.Add(
new HealthCheckRegistration(
"ollama",
sp => new Mock<IHealthCheck>().Object,
null,
new[] { "api", "ollama" }
)
);
hcOptions.Registrations.Add(
new HealthCheckRegistration(
"telegram",
sp => new Mock<IHealthCheck>().Object,
null,
new[] { "api", "telegram" }
)
);
services.AddSingleton<IOptions<HealthCheckServiceOptions>>(Options.Create(hcOptions));
// ILogger for health checks is provided by AddLogging above; no direct registration for internal types
var sp = services.BuildServiceProvider();
// Assert: core services can be resolved
Assert.NotNull(sp.GetRequiredService<ChatBotDbContext>());
Assert.NotNull(sp.GetRequiredService<IChatSessionRepository>());
Assert.NotNull(sp.GetRequiredService<ISessionStorage>());
Assert.NotNull(sp.GetRequiredService<ModelService>());
Assert.NotNull(sp.GetRequiredService<SystemPromptService>());
Assert.NotNull(sp.GetRequiredService<IHistoryCompressionService>());
Assert.NotNull(sp.GetRequiredService<IAIService>());
Assert.NotNull(sp.GetRequiredService<ChatService>());
Assert.NotNull(sp.GetRequiredService<IOllamaClient>());
// Assert: Telegram stack resolves
Assert.NotNull(sp.GetRequiredService<ITelegramBotClient>());
Assert.NotNull(sp.GetRequiredService<ITelegramMessageSender>());
Assert.NotNull(sp.GetRequiredService<ITelegramErrorHandler>());
Assert.NotNull(sp.GetRequiredService<ITelegramCommandProcessor>());
Assert.NotNull(sp.GetRequiredService<ITelegramMessageHandler>());
Assert.NotEmpty(sp.GetServices<ITelegramCommand>());
Assert.NotNull(sp.GetRequiredService<IHostedService>());
// Assert: health checks names and tags
var hcResolved = sp.GetRequiredService<IOptions<HealthCheckServiceOptions>>().Value;
var regDict = hcResolved.Registrations.ToDictionary(r => r.Name, r => r.Tags);
Assert.True(regDict.ContainsKey("ollama"));
Assert.True(regDict.ContainsKey("telegram"));
Assert.Contains("api", regDict["ollama"]);
Assert.Contains("ollama", regDict["ollama"]);
Assert.Contains("api", regDict["telegram"]);
Assert.Contains("telegram", regDict["telegram"]);
}
[Fact]
public void Program_ShouldHaveRequiredUsingStatements()
{
// This test verifies that Program.cs has the necessary using statements
// by checking if the types can be resolved
var services = new ServiceCollection();
ConfigureServices(services);
var serviceProvider = services.BuildServiceProvider();
// Verify that key services can be resolved
Assert.NotNull(serviceProvider.GetService<IConfiguration>());
Assert.NotNull(serviceProvider.GetService<ILoggerFactory>());
}
[Fact]
public void Program_ShouldConfigureTelegramBotSettings()
{
// Arrange
var services = new ServiceCollection();
ConfigureServices(services);
// Act
var serviceProvider = services.BuildServiceProvider();
// Assert
var botToken = serviceProvider.GetRequiredService<IConfiguration>()["TelegramBot:BotToken"];
Assert.Equal("test_token", botToken);
}
[Fact]
public void Program_ShouldConfigureOllamaSettings()
{
// Arrange
var services = new ServiceCollection();
ConfigureServices(services);
// Act
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
// Assert
var ollamaUrl = configuration["Ollama:Url"];
var defaultModel = configuration["Ollama:DefaultModel"];
Assert.Equal("http://localhost:11434", ollamaUrl);
Assert.Equal("llama2", defaultModel);
}
[Fact]
public void Program_ShouldConfigureAISettings()
{
// Arrange
var services = new ServiceCollection();
ConfigureServices(services);
// Act
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
// Assert
var maxTokens = configuration["AI:MaxTokens"];
var temperature = configuration["AI:Temperature"];
Assert.Equal("1000", maxTokens);
Assert.Equal("0.7", temperature);
}
[Fact]
public void Program_ShouldConfigureDatabaseSettings()
{
// Arrange
var services = new ServiceCollection();
ConfigureServices(services);
// Act
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
// Assert
var connectionString = configuration["Database:ConnectionString"];
var commandTimeout = configuration["Database:CommandTimeout"];
Assert.Equal(
"Host=localhost;Port=5432;Database=test_chatbot;Username=postgres;Password=postgres",
connectionString
);
Assert.Equal("30", commandTimeout);
Assert.Equal("false", configuration["Database:EnableSensitiveDataLogging"]);
}
[Fact]
public void Program_ShouldHandleEnvironmentVariables()
{
// Arrange
Environment.SetEnvironmentVariable("TELEGRAM_BOT_TOKEN", "env_token");
Environment.SetEnvironmentVariable("OLLAMA_URL", "http://env:11434");
Environment.SetEnvironmentVariable("OLLAMA_DEFAULT_MODEL", "env_model");
Environment.SetEnvironmentVariable("DB_HOST", "env_host");
Environment.SetEnvironmentVariable("DB_PORT", "8080");
Environment.SetEnvironmentVariable("DB_NAME", "env_db");
Environment.SetEnvironmentVariable("DB_USER", "env_user");
Environment.SetEnvironmentVariable("DB_PASSWORD", "env_pass");
try
{
// Act
var services = new ServiceCollection();
ConfigureServices(services);
// Assert
Assert.Equal("env_token", Environment.GetEnvironmentVariable("TELEGRAM_BOT_TOKEN"));
Assert.Equal("http://env:11434", Environment.GetEnvironmentVariable("OLLAMA_URL"));
Assert.Equal("env_model", Environment.GetEnvironmentVariable("OLLAMA_DEFAULT_MODEL"));
Assert.Equal("env_host", Environment.GetEnvironmentVariable("DB_HOST"));
Assert.Equal("8080", Environment.GetEnvironmentVariable("DB_PORT"));
Assert.Equal("env_db", Environment.GetEnvironmentVariable("DB_NAME"));
Assert.Equal("env_user", Environment.GetEnvironmentVariable("DB_USER"));
Assert.Equal("env_pass", Environment.GetEnvironmentVariable("DB_PASSWORD"));
}
finally
{
// Cleanup
Environment.SetEnvironmentVariable("TELEGRAM_BOT_TOKEN", null);
Environment.SetEnvironmentVariable("OLLAMA_URL", null);
Environment.SetEnvironmentVariable("OLLAMA_DEFAULT_MODEL", null);
Environment.SetEnvironmentVariable("DB_HOST", null);
Environment.SetEnvironmentVariable("DB_PORT", null);
Environment.SetEnvironmentVariable("DB_NAME", null);
Environment.SetEnvironmentVariable("DB_USER", null);
Environment.SetEnvironmentVariable("DB_PASSWORD", null);
}
}
[Fact]
public void Program_ShouldHaveValidConfigurationStructure()
{
// Arrange
var services = new ServiceCollection();
ConfigureServices(services);
// Act
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
// Assert - Check that all required configuration sections exist
Assert.NotNull(configuration.GetSection("TelegramBot"));
Assert.NotNull(configuration.GetSection("Ollama"));
Assert.NotNull(configuration.GetSection("AI"));
Assert.NotNull(configuration.GetSection("Database"));
}
[Fact]
public void Program_ShouldHaveRequiredConfigurationValues()
{
// Arrange
var services = new ServiceCollection();
ConfigureServices(services);
// Act
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
// Assert - Check that all required configuration values are present
Assert.NotNull(configuration["TelegramBot:BotToken"]);
Assert.NotNull(configuration["Ollama:Url"]);
Assert.NotNull(configuration["Ollama:DefaultModel"]);
Assert.NotNull(configuration["AI:MaxTokens"]);
Assert.NotNull(configuration["AI:Temperature"]);
Assert.NotNull(configuration["Database:ConnectionString"]);
Assert.NotNull(configuration["Database:CommandTimeout"]);
Assert.NotNull(configuration["Database:EnableSensitiveDataLogging"]);
}
[Fact]
public void Program_ShouldHaveValidNumericConfigurationValues()
{
// Arrange
var services = new ServiceCollection();
ConfigureServices(services);
// Act
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
// Assert - Check that numeric values can be parsed
var maxTokensValue = configuration["AI:MaxTokens"];
Assert.NotNull(maxTokensValue);
Assert.True(int.TryParse(maxTokensValue, out var maxTokens));
Assert.True(maxTokens > 0);
var temperatureValue = configuration["AI:Temperature"];
Assert.NotNull(temperatureValue);
Assert.True(
double.TryParse(
temperatureValue,
System.Globalization.NumberStyles.Float,
System.Globalization.CultureInfo.InvariantCulture,
out var temperature
)
);
Assert.True(temperature >= 0 && temperature <= 2);
Assert.True(int.TryParse(configuration["Database:CommandTimeout"], out var commandTimeout));
Assert.True(commandTimeout > 0);
Assert.True(bool.TryParse(configuration["Database:EnableSensitiveDataLogging"], out _));
}
[Fact]
public void Program_ShouldHaveValidUrlConfigurationValues()
{
// Arrange
var services = new ServiceCollection();
ConfigureServices(services);
// Act
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
// Assert - Check that URL values are valid
var ollamaUrl = configuration["Ollama:Url"];
Assert.NotNull(ollamaUrl);
Assert.True(Uri.TryCreate(ollamaUrl, UriKind.Absolute, out var uri));
Assert.True(uri.Scheme == "http" || uri.Scheme == "https");
}
[Fact]
public void Program_ShouldHaveValidDatabaseConnectionString()
{
// Arrange
var services = new ServiceCollection();
ConfigureServices(services);
// Act
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
// Assert - Check that connection string has required components
var connectionString = configuration["Database:ConnectionString"];
Assert.NotNull(connectionString);
Assert.Contains("Host=", connectionString);
Assert.Contains("Port=", connectionString);
Assert.Contains("Database=", connectionString);
Assert.Contains("Username=", connectionString);
Assert.Contains("Password=", connectionString);
}
}