add tests
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
using ChatBot.Models.Configuration;
|
||||
using ChatBot.Models.Configuration.Validators;
|
||||
using FluentAssertions;
|
||||
|
||||
namespace ChatBot.Tests.Configuration.Validators;
|
||||
|
||||
public class AISettingsValidatorTests
|
||||
{
|
||||
private readonly AISettingsValidator _validator = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnSuccess_WhenSettingsAreValid()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new AISettings
|
||||
{
|
||||
Temperature = 0.7,
|
||||
SystemPromptPath = "Prompts/system-prompt.txt",
|
||||
MaxRetryAttempts = 3,
|
||||
RetryDelayMs = 1000,
|
||||
MaxRetryDelayMs = 10000,
|
||||
EnableExponentialBackoff = true,
|
||||
RequestTimeoutSeconds = 30,
|
||||
EnableHistoryCompression = true,
|
||||
CompressionThreshold = 10,
|
||||
CompressionTarget = 5,
|
||||
MinMessageLengthForSummarization = 50,
|
||||
MaxSummarizedMessageLength = 200,
|
||||
CompressionTimeoutSeconds = 15,
|
||||
StatusCheckTimeoutSeconds = 5,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnFailure_WhenTemperatureIsInvalid()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new AISettings
|
||||
{
|
||||
Temperature = 3.0, // Invalid: > 2.0
|
||||
SystemPromptPath = "Prompts/system-prompt.txt",
|
||||
MaxRetryAttempts = 3,
|
||||
RetryDelayMs = 1000,
|
||||
MaxRetryDelayMs = 10000,
|
||||
EnableExponentialBackoff = true,
|
||||
RequestTimeoutSeconds = 30,
|
||||
EnableHistoryCompression = true,
|
||||
CompressionThreshold = 10,
|
||||
CompressionTarget = 5,
|
||||
MinMessageLengthForSummarization = 50,
|
||||
MaxSummarizedMessageLength = 200,
|
||||
CompressionTimeoutSeconds = 15,
|
||||
StatusCheckTimeoutSeconds = 5,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result
|
||||
.Failures.Should()
|
||||
.Contain(f => f.Contains("Temperature must be between 0.0 and 2.0"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnFailure_WhenSystemPromptPathIsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new AISettings
|
||||
{
|
||||
Temperature = 0.7,
|
||||
SystemPromptPath = "", // Invalid: empty
|
||||
MaxRetryAttempts = 3,
|
||||
RetryDelayMs = 1000,
|
||||
MaxRetryDelayMs = 10000,
|
||||
EnableExponentialBackoff = true,
|
||||
RequestTimeoutSeconds = 30,
|
||||
EnableHistoryCompression = true,
|
||||
CompressionThreshold = 10,
|
||||
CompressionTarget = 5,
|
||||
MinMessageLengthForSummarization = 50,
|
||||
MaxSummarizedMessageLength = 200,
|
||||
CompressionTimeoutSeconds = 15,
|
||||
StatusCheckTimeoutSeconds = 5,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("System prompt path cannot be empty"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnFailure_WhenMaxRetryAttemptsIsInvalid()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new AISettings
|
||||
{
|
||||
Temperature = 0.7,
|
||||
SystemPromptPath = "Prompts/system-prompt.txt",
|
||||
MaxRetryAttempts = 15, // Invalid: > 10
|
||||
RetryDelayMs = 1000,
|
||||
MaxRetryDelayMs = 10000,
|
||||
EnableExponentialBackoff = true,
|
||||
RequestTimeoutSeconds = 30,
|
||||
EnableHistoryCompression = true,
|
||||
CompressionThreshold = 10,
|
||||
CompressionTarget = 5,
|
||||
MinMessageLengthForSummarization = 50,
|
||||
MaxSummarizedMessageLength = 200,
|
||||
CompressionTimeoutSeconds = 15,
|
||||
StatusCheckTimeoutSeconds = 5,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("Max retry attempts cannot exceed 10"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnFailure_WhenCompressionSettingsAreInvalid()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new AISettings
|
||||
{
|
||||
Temperature = 0.7,
|
||||
SystemPromptPath = "Prompts/system-prompt.txt",
|
||||
MaxRetryAttempts = 3,
|
||||
RetryDelayMs = 1000,
|
||||
MaxRetryDelayMs = 10000,
|
||||
EnableExponentialBackoff = true,
|
||||
RequestTimeoutSeconds = 30,
|
||||
EnableHistoryCompression = true,
|
||||
CompressionThreshold = 5, // Invalid: same as target
|
||||
CompressionTarget = 5,
|
||||
MinMessageLengthForSummarization = 50,
|
||||
MaxSummarizedMessageLength = 200,
|
||||
CompressionTimeoutSeconds = 15,
|
||||
StatusCheckTimeoutSeconds = 5,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result
|
||||
.Failures.Should()
|
||||
.Contain(f => f.Contains("Compression target must be less than compression threshold"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
using ChatBot.Models.Configuration;
|
||||
using ChatBot.Models.Configuration.Validators;
|
||||
using FluentAssertions;
|
||||
|
||||
namespace ChatBot.Tests.Configuration.Validators;
|
||||
|
||||
public class DatabaseSettingsValidatorTests
|
||||
{
|
||||
private readonly DatabaseSettingsValidator _validator = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnSuccess_WhenSettingsAreValid()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new DatabaseSettings
|
||||
{
|
||||
ConnectionString =
|
||||
"Host=localhost;Port=5432;Database=chatbot;Username=user;Password=pass",
|
||||
CommandTimeout = 30,
|
||||
EnableSensitiveDataLogging = false,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnFailure_WhenConnectionStringIsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new DatabaseSettings
|
||||
{
|
||||
ConnectionString = "",
|
||||
CommandTimeout = 30,
|
||||
EnableSensitiveDataLogging = false,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("Database connection string is required"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnFailure_WhenCommandTimeoutIsInvalid()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new DatabaseSettings
|
||||
{
|
||||
ConnectionString =
|
||||
"Host=localhost;Port=5432;Database=chatbot;Username=user;Password=pass",
|
||||
CommandTimeout = 0, // Invalid: <= 0
|
||||
EnableSensitiveDataLogging = false,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("Command timeout must be greater than 0"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void Validate_ShouldReturnFailure_WhenConnectionStringIsNullOrWhitespace(
|
||||
string? connectionString
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var settings = new DatabaseSettings
|
||||
{
|
||||
ConnectionString = connectionString!,
|
||||
CommandTimeout = 30,
|
||||
EnableSensitiveDataLogging = false,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("Database connection string is required"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using ChatBot.Models.Configuration;
|
||||
using ChatBot.Models.Configuration.Validators;
|
||||
using FluentAssertions;
|
||||
|
||||
namespace ChatBot.Tests.Configuration.Validators;
|
||||
|
||||
public class OllamaSettingsValidatorTests
|
||||
{
|
||||
private readonly OllamaSettingsValidator _validator = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnSuccess_WhenSettingsAreValid()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new OllamaSettings
|
||||
{
|
||||
Url = "http://localhost:11434",
|
||||
DefaultModel = "llama3.2",
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnFailure_WhenUrlIsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new OllamaSettings { Url = "", DefaultModel = "llama3.2" };
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("Ollama URL is required"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnFailure_WhenUrlIsInvalid()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new OllamaSettings { Url = "invalid-url", DefaultModel = "llama3.2" };
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("Invalid Ollama URL format"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnFailure_WhenDefaultModelIsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new OllamaSettings { Url = "http://localhost:11434", DefaultModel = "" };
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("DefaultModel is required"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void Validate_ShouldReturnFailure_WhenUrlIsNullOrWhitespace(string? url)
|
||||
{
|
||||
// Arrange
|
||||
var settings = new OllamaSettings { Url = url!, DefaultModel = "llama3.2" };
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("Ollama URL is required"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using ChatBot.Models.Configuration;
|
||||
using ChatBot.Models.Configuration.Validators;
|
||||
using FluentAssertions;
|
||||
|
||||
namespace ChatBot.Tests.Configuration.Validators;
|
||||
|
||||
public class TelegramBotSettingsValidatorTests
|
||||
{
|
||||
private readonly TelegramBotSettingsValidator _validator = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnSuccess_WhenSettingsAreValid()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new TelegramBotSettings
|
||||
{
|
||||
BotToken = "1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk",
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnFailure_WhenBotTokenIsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new TelegramBotSettings { BotToken = "" };
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("Telegram bot token is required"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnFailure_WhenBotTokenIsTooShort()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new TelegramBotSettings
|
||||
{
|
||||
BotToken = "1234567890:ABC", // 15 chars, too short
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result
|
||||
.Failures.Should()
|
||||
.Contain(f => f.Contains("Telegram bot token must be at least 40 characters"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void Validate_ShouldReturnFailure_WhenBotTokenIsNullOrWhitespace(string? botToken)
|
||||
{
|
||||
// Arrange
|
||||
var settings = new TelegramBotSettings { BotToken = botToken! };
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("Telegram bot token is required"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user