117 lines
5.0 KiB
C#
117 lines
5.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using BLAIzor.Data;
|
|
using BLAIzor.Models;
|
|
using BLAIzor.Services;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Moq;
|
|
using Xunit;
|
|
|
|
namespace SeemGen.Tests;
|
|
public class ContentEditorServiceTests
|
|
{
|
|
[Fact]
|
|
public async Task SaveAndSyncContentItemAsync_UpdatesFieldsAndRechunks_WhenContentChanged()
|
|
{
|
|
// Arrange
|
|
var contentItemId = 1;
|
|
var oldContent = "Old content";
|
|
var newContent = "New content for chunking";
|
|
var collectionName = "test_collection";
|
|
|
|
var contentGroup = new ContentGroup { Id = 1, Name = "Group", SiteInfoId = 1, Items = new List<ContentItem>() };
|
|
var existingChunks = new List<ContentChunk>
|
|
{
|
|
new ContentChunk { Id = 1, ContentItemId = contentItemId, QdrantPointId = Guid.NewGuid().ToString(), ChunkIndex = 0, CreatedAt = DateTime.UtcNow }
|
|
};
|
|
var existingItem = new ContentItem
|
|
{
|
|
Id = contentItemId,
|
|
Title = "Old Title",
|
|
Description = "Old Desc",
|
|
Content = oldContent,
|
|
Language = "en",
|
|
Tags = "tag1",
|
|
IsPublished = true,
|
|
LastUpdated = DateTime.UtcNow,
|
|
ContentGroup = contentGroup,
|
|
Chunks = existingChunks
|
|
};
|
|
contentGroup.Items.Add(existingItem);
|
|
|
|
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
|
|
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
|
|
.Options;
|
|
var dbContext = new ApplicationDbContext(options);
|
|
dbContext.ContentItems.Add(existingItem);
|
|
dbContext.ContentChunks.AddRange(existingChunks);
|
|
dbContext.SaveChanges();
|
|
|
|
var serviceScopeFactoryMock = new Mock<IServiceScopeFactory>();
|
|
var scopeMock = new Mock<IServiceScope>();
|
|
var providerMock = new Mock<IServiceProvider>();
|
|
providerMock.Setup(x => x.GetService(typeof(ApplicationDbContext))).Returns(dbContext);
|
|
scopeMock.Setup(x => x.ServiceProvider).Returns(providerMock.Object);
|
|
serviceScopeFactoryMock.Setup(x => x.CreateScope()).Returns(scopeMock.Object);
|
|
|
|
var qdrantServiceMock = new Mock<QDrantService>(null);
|
|
qdrantServiceMock.Setup(x => x.DeletePointsAsync(It.IsAny<Guid[]>(), collectionName)).Returns(Task.CompletedTask);
|
|
qdrantServiceMock.Setup(x => x.QDrantInsertManyAsync(It.IsAny<List<WebPageContent>>(), collectionName)).Returns(Task.CompletedTask);
|
|
|
|
var openAIEmbeddingServiceMock = new Mock<OpenAIEmbeddingService>();
|
|
openAIEmbeddingServiceMock.Setup(x => x.GenerateEmbeddingAsync(It.IsAny<string>())).ReturnsAsync(new float[] { 0.1f, 0.2f });
|
|
|
|
var localEmbeddingServiceMock = new Mock<LocalEmbeddingService>();
|
|
localEmbeddingServiceMock.Setup(x => x.GenerateEmbeddingAsync(It.IsAny<string>())).ReturnsAsync(new float[] { 0.1f, 0.2f });
|
|
|
|
var openAIApiServiceMock = new Mock<OpenAIApiService>(MockBehavior.Loose, null, null);
|
|
var htmlSnippetProcessorMock = new Mock<HtmlSnippetProcessor>(qdrantServiceMock.Object, openAIEmbeddingServiceMock.Object, localEmbeddingServiceMock.Object, null);
|
|
var scopedContentServiceMock = new Mock<ScopedContentService>(dbContext, serviceScopeFactoryMock.Object);
|
|
|
|
var configMock = new Mock<IConfiguration>();
|
|
configMock.Setup(x => x.GetSection("AiSettings").GetValue<string>("EmbeddingService")).Returns("openai");
|
|
|
|
var service = new ContentEditorService(
|
|
openAIApiServiceMock.Object,
|
|
qdrantServiceMock.Object,
|
|
openAIEmbeddingServiceMock.Object,
|
|
localEmbeddingServiceMock.Object,
|
|
htmlSnippetProcessorMock.Object,
|
|
serviceScopeFactoryMock.Object,
|
|
scopedContentServiceMock.Object,
|
|
configMock.Object
|
|
);
|
|
|
|
var dto = new ContentItem
|
|
{
|
|
Id = contentItemId,
|
|
Title = "New Title",
|
|
Description = "New Desc",
|
|
Content = newContent,
|
|
Language = "fr",
|
|
Tags = "tag2",
|
|
IsPublished = false,
|
|
LastUpdated = DateTime.UtcNow
|
|
};
|
|
|
|
// Act
|
|
var result = await service.SaveAndSyncContentItemAsync(dto, collectionName, false);
|
|
|
|
// Assert
|
|
Assert.Equal(dto.Title, result.Title);
|
|
Assert.Equal(dto.Description, result.Description);
|
|
Assert.Equal(dto.Content, result.Content);
|
|
Assert.Equal(dto.Language, result.Language);
|
|
Assert.Equal(dto.Tags, result.Tags);
|
|
Assert.Equal(dto.IsPublished, result.IsPublished);
|
|
Assert.True(result.Version > 0);
|
|
Assert.NotEmpty(dbContext.ContentChunks.Where(c => c.ContentItemId == contentItemId));
|
|
qdrantServiceMock.Verify(x => x.DeletePointsAsync(It.IsAny<Guid[]>(), collectionName), Times.Once);
|
|
qdrantServiceMock.Verify(x => x.QDrantInsertManyAsync(It.IsAny<List<WebPageContent>>(), collectionName), Times.Once);
|
|
}
|
|
}
|