290 lines
6.8 KiB
C#
290 lines
6.8 KiB
C#
using ChatBot.Services;
|
|
using ChatBot.Services.Interfaces;
|
|
using OllamaSharp.Models;
|
|
using OllamaSharp.Models.Chat;
|
|
|
|
namespace ChatBot.Tests.Services;
|
|
|
|
public class OllamaClientAdapterTests
|
|
{
|
|
[Fact]
|
|
public void Constructor_WithValidUrl_ShouldCreateInstance()
|
|
{
|
|
// Arrange
|
|
var url = "http://localhost:11434";
|
|
|
|
// Act
|
|
var adapter = new OllamaClientAdapter(url);
|
|
|
|
// Assert
|
|
Assert.NotNull(adapter);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithNullUrl_ShouldThrowArgumentException()
|
|
{
|
|
// Arrange
|
|
string? url = null;
|
|
|
|
// Act & Assert
|
|
var exception = Assert.Throws<ArgumentException>(() => new OllamaClientAdapter(url!));
|
|
Assert.Equal("URL cannot be empty (Parameter 'url')", exception.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithEmptyUrl_ShouldThrowArgumentException()
|
|
{
|
|
// Arrange
|
|
var url = "";
|
|
|
|
// Act & Assert
|
|
var exception = Assert.Throws<ArgumentException>(() => new OllamaClientAdapter(url));
|
|
Assert.Equal("URL cannot be empty (Parameter 'url')", exception.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithWhitespaceUrl_ShouldThrowArgumentException()
|
|
{
|
|
// Arrange
|
|
var url = " ";
|
|
|
|
// Act & Assert
|
|
var exception = Assert.Throws<ArgumentException>(() => new OllamaClientAdapter(url));
|
|
Assert.Equal("URL cannot be empty (Parameter 'url')", exception.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithHttpsUrl_ShouldCreateInstance()
|
|
{
|
|
// Arrange
|
|
var url = "https://ollama.example.com";
|
|
|
|
// Act
|
|
var adapter = new OllamaClientAdapter(url);
|
|
|
|
// Assert
|
|
Assert.NotNull(adapter);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithCustomPort_ShouldCreateInstance()
|
|
{
|
|
// Arrange
|
|
var url = "http://localhost:8080";
|
|
|
|
// Act
|
|
var adapter = new OllamaClientAdapter(url);
|
|
|
|
// Assert
|
|
Assert.NotNull(adapter);
|
|
}
|
|
|
|
[Fact]
|
|
public void SelectedModel_GetAndSet_ShouldWork()
|
|
{
|
|
// Arrange
|
|
var url = "http://localhost:11434";
|
|
var adapter = new OllamaClientAdapter(url);
|
|
var modelName = "llama2";
|
|
|
|
// Act
|
|
adapter.SelectedModel = modelName;
|
|
var result = adapter.SelectedModel;
|
|
|
|
// Assert
|
|
Assert.Equal(modelName, result);
|
|
}
|
|
|
|
[Fact]
|
|
public void SelectedModel_SetMultipleTimes_ShouldUpdateValue()
|
|
{
|
|
// Arrange
|
|
var url = "http://localhost:11434";
|
|
var adapter = new OllamaClientAdapter(url);
|
|
|
|
// Act
|
|
adapter.SelectedModel = "llama2";
|
|
adapter.SelectedModel = "codellama";
|
|
var result = adapter.SelectedModel;
|
|
|
|
// Assert
|
|
Assert.Equal("codellama", result);
|
|
}
|
|
|
|
[Fact]
|
|
public void ChatAsync_ShouldReturnAsyncEnumerable()
|
|
{
|
|
// Arrange
|
|
var url = "http://localhost:11434";
|
|
var adapter = new OllamaClientAdapter(url);
|
|
var request = new ChatRequest
|
|
{
|
|
Model = "llama2",
|
|
Messages = new List<Message>
|
|
{
|
|
new() { Role = "user", Content = "Hello" },
|
|
},
|
|
};
|
|
|
|
// Act
|
|
var result = adapter.ChatAsync(request);
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
Assert.IsAssignableFrom<IAsyncEnumerable<ChatResponseStream?>>(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void ListLocalModelsAsync_ShouldReturnTask()
|
|
{
|
|
// Arrange
|
|
var url = "http://localhost:11434";
|
|
var adapter = new OllamaClientAdapter(url);
|
|
|
|
// Act
|
|
var result = adapter.ListLocalModelsAsync();
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
Assert.IsAssignableFrom<Task<IEnumerable<Model>>>(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithSpecialCharactersInUrl_ShouldCreateInstance()
|
|
{
|
|
// Arrange
|
|
var url = "http://user:pass@localhost:11434/path?query=value#fragment";
|
|
|
|
// Act
|
|
var adapter = new OllamaClientAdapter(url);
|
|
|
|
// Assert
|
|
Assert.NotNull(adapter);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithIpAddress_ShouldCreateInstance()
|
|
{
|
|
// Arrange
|
|
var url = "http://192.168.1.100:11434";
|
|
|
|
// Act
|
|
var adapter = new OllamaClientAdapter(url);
|
|
|
|
// Assert
|
|
Assert.NotNull(adapter);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithLocalhostVariations_ShouldCreateInstance()
|
|
{
|
|
// Arrange
|
|
var urls = new[]
|
|
{
|
|
"http://localhost:11434",
|
|
"http://127.0.0.1:11434",
|
|
"http://0.0.0.0:11434",
|
|
};
|
|
|
|
foreach (var url in urls)
|
|
{
|
|
// Act
|
|
var adapter = new OllamaClientAdapter(url);
|
|
|
|
// Assert
|
|
Assert.NotNull(adapter);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithDifferentProtocols_ShouldCreateInstance()
|
|
{
|
|
// Arrange
|
|
var urls = new[] { "http://localhost:11434", "https://localhost:11434" };
|
|
|
|
foreach (var url in urls)
|
|
{
|
|
// Act
|
|
var adapter = new OllamaClientAdapter(url);
|
|
|
|
// Assert
|
|
Assert.NotNull(adapter);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithTrailingSlash_ShouldCreateInstance()
|
|
{
|
|
// Arrange
|
|
var url = "http://localhost:11434/";
|
|
|
|
// Act
|
|
var adapter = new OllamaClientAdapter(url);
|
|
|
|
// Assert
|
|
Assert.NotNull(adapter);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithPath_ShouldCreateInstance()
|
|
{
|
|
// Arrange
|
|
var url = "http://localhost:11434/api/v1";
|
|
|
|
// Act
|
|
var adapter = new OllamaClientAdapter(url);
|
|
|
|
// Assert
|
|
Assert.NotNull(adapter);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithUnicodeUrl_ShouldCreateInstance()
|
|
{
|
|
// Arrange
|
|
var url = "http://тест.рф:11434";
|
|
|
|
// Act
|
|
var adapter = new OllamaClientAdapter(url);
|
|
|
|
// Assert
|
|
Assert.NotNull(adapter);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithVeryLongUrl_ShouldThrowException()
|
|
{
|
|
// Arrange
|
|
var url = "http://" + new string('a', 1000) + ".example.com:11434";
|
|
|
|
// Act & Assert
|
|
Assert.Throws<UriFormatException>(() => new OllamaClientAdapter(url));
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithUrlContainingSpaces_ShouldCreateInstance()
|
|
{
|
|
// Arrange
|
|
var url = "http://localhost:11434/path with spaces";
|
|
|
|
// Act
|
|
var adapter = new OllamaClientAdapter(url);
|
|
|
|
// Assert
|
|
Assert.NotNull(adapter);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithUrlContainingSpecialCharacters_ShouldCreateInstance()
|
|
{
|
|
// Arrange
|
|
var url = "http://localhost:11434/path!@#$%^&*()";
|
|
|
|
// Act
|
|
var adapter = new OllamaClientAdapter(url);
|
|
|
|
// Assert
|
|
Assert.NotNull(adapter);
|
|
}
|
|
}
|