cb4b7d0052
This Pull Request implements the complete **Retrieval module (Read Path)** for the Knowledge-Map RAG (KM-RAG) architecture within the NexusReader platform. It resolves all requirements for vector-based semantic search, Neo4j graph context expansion, structured grounding with Google Gemini, API/Wasm integration, and an interactive front-end global Q&A panel. Resolves #48 ### 🚀 Key Implementations 1. **Grounded DTOs & Schema Definition** - Added `GroundedResponseDto` and `CitationDto` for strict JSON Schema matching with Gemini. 2. **Core Service & Read Path Logic** - Implemented the robust **5-step pipeline** in `KnowledgeService.AskQuestionAsync`: 1. *Embedding*: Query vectorization using `IEmbeddingGenerator`. 2. *Semantic Search*: Multi-tenant vector search with Qdrant, supporting scoping to a specific book or global search. 3. *Graph Expansion*: Fetching connected concepts and parent relationships using Neo4j Cypher. 4. *Citation Hydration*: Cross-referencing results with PostgreSQL to fetch book titles and accurate chapter citations. 5. *Grounded Generation*: Strict structured generation via `IChatClient` (Gemini) preventing hallucinations and using citations. 3. **CQRS & Endpoints** - Added `AskLibraryQuestionQuery` and its handler. - Mapped `/api/knowledge/ask` and `/api/knowledge/search` endpoints inside `Program.cs`. - Updated `WasmKnowledgeService` to support proxying retrieval requests. 4. **Premium Blazor UI Panel** - Implemented `/intelligence` (Global AI Q&A) with a curated HSL palette, dark theme, smooth micro-animations, loading shimmers, and side-by-side citation cards. - Registered the panel within the `MainHubLayout` sidebar. 5. **Test Coverage** - Wrote comprehensive xUnit tests in `QueryTests.cs` using Moq and FluentAssertions to assert that handlers correctly validate input and interact with services. ### 🧪 Verification - Verified compilation and build gate successfully (`dotnet build`: 0 errors). - All 7 tests passed perfectly (`dotnet test`). --------- Co-authored-by: Marek Jasiński <jasins.marek@gmail.com> Reviewed-on: #49 Reviewed-by: Marek Jaisński <jasins.marek@gmail.com> Co-authored-by: Antigravity <antigravity@google.com> Co-committed-by: Antigravity <antigravity@google.com>
208 lines
7.4 KiB
C#
208 lines
7.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using FluentAssertions;
|
|
using Microsoft.Data.Sqlite;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.AI;
|
|
using Moq;
|
|
using FluentResults;
|
|
using NexusReader.Application.Abstractions.Services;
|
|
using NexusReader.Application.DTOs.AI;
|
|
using NexusReader.Application.DTOs.User;
|
|
using NexusReader.Application.Queries.Library;
|
|
using NexusReader.Data.Persistence;
|
|
using NexusReader.Domain.Entities;
|
|
using Xunit;
|
|
|
|
namespace NexusReader.Application.Tests.Queries;
|
|
|
|
public class QueryTests : IDisposable
|
|
{
|
|
private readonly SqliteConnection _connection;
|
|
private readonly DbContextOptions<AppDbContext> _contextOptions;
|
|
private readonly Mock<IDbContextFactory<AppDbContext>> _dbContextFactoryMock;
|
|
private readonly Mock<IEmbeddingGenerator<string, Embedding<float>>> _embeddingGeneratorMock;
|
|
|
|
public QueryTests()
|
|
{
|
|
_connection = new SqliteConnection("DataSource=:memory:");
|
|
_connection.Open();
|
|
|
|
_contextOptions = new DbContextOptionsBuilder<AppDbContext>()
|
|
.UseSqlite(_connection)
|
|
.Options;
|
|
|
|
// Seed initial database schema
|
|
using var context = new AppDbContext(_contextOptions);
|
|
context.Database.EnsureCreated();
|
|
|
|
_dbContextFactoryMock = new Mock<IDbContextFactory<AppDbContext>>();
|
|
_dbContextFactoryMock.Setup(f => f.CreateDbContextAsync(It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(() => new AppDbContext(_contextOptions));
|
|
_dbContextFactoryMock.Setup(f => f.CreateDbContext())
|
|
.Returns(() => new AppDbContext(_contextOptions));
|
|
|
|
_embeddingGeneratorMock = new Mock<IEmbeddingGenerator<string, Embedding<float>>>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetMyEbooksQuery_WithPopulatedDescription_ReturnsCorrectDescription()
|
|
{
|
|
// Arrange
|
|
using (var context = new AppDbContext(_contextOptions))
|
|
{
|
|
var user = new NexusUser
|
|
{
|
|
Id = "user-123",
|
|
UserName = "testuser",
|
|
Email = "test@example.com",
|
|
TenantId = "tenant-123",
|
|
SubscriptionPlanId = 1
|
|
};
|
|
context.Users.Add(user);
|
|
|
|
var author = new Author { Id = 1, Name = "Adam Mickiewicz" };
|
|
context.Authors.Add(author);
|
|
|
|
var ebook = new Ebook
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
UserId = "user-123",
|
|
Title = "Pan Tadeusz",
|
|
AuthorId = author.Id,
|
|
Description = "A Polish epic poem written by Adam Mickiewicz.",
|
|
CoverUrl = "cover.png",
|
|
Progress = 42.5,
|
|
LastChapter = "Księga I",
|
|
LastChapterIndex = 1,
|
|
AddedDate = DateTime.UtcNow,
|
|
LastReadDate = DateTime.UtcNow,
|
|
FilePath = "dummy.epub"
|
|
};
|
|
context.Ebooks.Add(ebook);
|
|
await context.SaveChangesAsync();
|
|
}
|
|
|
|
var handler = new GetMyEbooksQueryHandler(_dbContextFactoryMock.Object);
|
|
var query = new GetMyEbooksQuery("user-123");
|
|
|
|
// Act
|
|
var result = await handler.Handle(query, CancellationToken.None);
|
|
|
|
// Assert
|
|
result.IsSuccess.Should().BeTrue();
|
|
result.Value.Should().HaveCount(1);
|
|
result.Value.First().Title.Should().Be("Pan Tadeusz");
|
|
result.Value.First().Description.Should().Be("A Polish epic poem written by Adam Mickiewicz.");
|
|
result.Value.First().Progress.Should().Be(42.5);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SearchLibrarySemanticallyQuery_WithEmptyQueryText_ReturnsFailure()
|
|
{
|
|
// Arrange
|
|
var knowledgeServiceMock = new Mock<IKnowledgeService>();
|
|
var handler = new SearchLibrarySemanticallyQueryHandler(knowledgeServiceMock.Object);
|
|
var query = new SearchLibrarySemanticallyQuery("", "tenant-123");
|
|
|
|
// Act
|
|
var result = await handler.Handle(query, CancellationToken.None);
|
|
|
|
// Assert
|
|
result.IsSuccess.Should().BeFalse();
|
|
result.Errors.First().Message.Should().Be("Query text cannot be empty.");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SearchLibrarySemanticallyQuery_WithValidQuery_CallsKnowledgeService()
|
|
{
|
|
// Arrange
|
|
var knowledgeServiceMock = new Mock<IKnowledgeService>();
|
|
var expectedResults = new List<SemanticSearchResultDto>
|
|
{
|
|
new SemanticSearchResultDto
|
|
{
|
|
ContentHash = "hash-123",
|
|
Snippet = "Semantic search result content snippet",
|
|
UnitType = "Concept",
|
|
RelevanceScore = 0.95f
|
|
}
|
|
};
|
|
|
|
knowledgeServiceMock.Setup(s => s.SearchLibrarySemanticallyAsync("test", "tenant-123", 5, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(Result.Ok(expectedResults));
|
|
|
|
var handler = new SearchLibrarySemanticallyQueryHandler(knowledgeServiceMock.Object);
|
|
var query = new SearchLibrarySemanticallyQuery("test", "tenant-123");
|
|
|
|
// Act
|
|
var result = await handler.Handle(query, CancellationToken.None);
|
|
|
|
// Assert
|
|
result.IsSuccess.Should().BeTrue();
|
|
result.Value.Should().HaveCount(1);
|
|
result.Value.First().Snippet.Should().Be("Semantic search result content snippet");
|
|
result.Value.First().ContentHash.Should().Be("hash-123");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AskLibraryQuestionQuery_WithEmptyQuestion_ReturnsFailure()
|
|
{
|
|
// Arrange
|
|
var knowledgeServiceMock = new Mock<IKnowledgeService>();
|
|
var handler = new AskLibraryQuestionQueryHandler(knowledgeServiceMock.Object);
|
|
var query = new AskLibraryQuestionQuery("", "tenant-123");
|
|
|
|
// Act
|
|
var result = await handler.Handle(query, CancellationToken.None);
|
|
|
|
// Assert
|
|
result.IsSuccess.Should().BeFalse();
|
|
result.Errors.First().Message.Should().Be("Question cannot be empty.");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AskLibraryQuestionQuery_WithValidQuestion_CallsKnowledgeService()
|
|
{
|
|
// Arrange
|
|
var knowledgeServiceMock = new Mock<IKnowledgeService>();
|
|
var expectedResponse = new GroundedResponseDto
|
|
{
|
|
Answer = "Based on the book, water boils at 100 degrees Celsius.",
|
|
Citations = new List<CitationDto>
|
|
{
|
|
new CitationDto
|
|
{
|
|
CitationId = "chunk-1",
|
|
Snippet = "Water boils at 100 degrees Celsius.",
|
|
SourceBook = "Physics 101"
|
|
}
|
|
}
|
|
};
|
|
|
|
knowledgeServiceMock.Setup(s => s.AskQuestionAsync("what temp does water boil?", "tenant-123", null, 5, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(Result.Ok(expectedResponse));
|
|
|
|
var handler = new AskLibraryQuestionQueryHandler(knowledgeServiceMock.Object);
|
|
var query = new AskLibraryQuestionQuery("what temp does water boil?", "tenant-123");
|
|
|
|
// Act
|
|
var result = await handler.Handle(query, CancellationToken.None);
|
|
|
|
// Assert
|
|
result.IsSuccess.Should().BeTrue();
|
|
result.Value.Answer.Should().Be("Based on the book, water boils at 100 degrees Celsius.");
|
|
result.Value.Citations.Should().HaveCount(1);
|
|
result.Value.Citations.First().CitationId.Should().Be("chunk-1");
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_connection.Close();
|
|
_connection.Dispose();
|
|
}
|
|
}
|