feat(search/rag): implement NexusSearchBox, dynamic Qdrant collection auto-provisioning, batch vector ingestion, mobile Serilog logging, and resolve 401 auth handler error (#51)

Resolves #52

This Pull Request introduces the **NexusSearchBox** search feature with premium unified styling, implements a robust **dynamic Qdrant collection auto-provisioning and batch-vector ingestion pipeline**, integrates a unified **Serilog logging infrastructure** for the Blazor Hybrid environment (MAUI), and resolves the **401 Unauthorized API header propagation error** inside mobile builds.

### 🚀 Key Implementations

#### 1. Premium `NexusSearchBox` & Semantic Search UI
* **NexusSearchBox Component:** Created an elegant search-as-you-type search box with smooth key navigation, quick-clearing, and seamless dynamic styling.
* **Unified Aesthetics:** Refactored the search box isolated styling to align perfectly with the dashboard's design system using glassmorphism, `--nexus-neon` token gradients, and smooth pulse/fade animations.
* **Semantic Search Integration:** Integrated semantic search query dispatching (`SearchLibrarySemanticallyQuery`) and wired up navigation seamlessly through the updated `ReaderNavigationService`.
* **Tests Hardening:** Added/adapted query assertions in `QueryTests.cs` to guarantee safe parameterization and error boundary mapping.

#### 2. Qdrant Collection Provisioning & Vector Ingestion
* **Dynamic Auto-Provisioning:** Implemented dynamic checking and lazy-creation of the `knowledge_units` collection using 768 dimensions and Cosine distance.
* **High-Performance Ingestion:** Optimized `ProcessKnowledgeUnitsAsync` with high-performance batch embedding generation using `_embeddingGenerator` and deterministic MD5 GUIDs for stable, duplicate-free upsertion.
* **Database Cache Clear Sync:** Integrated Qdrant collection deletion in `ClearCacheAsync` to ensure absolute consistency between the PostgreSQL database cache and vector database indices.

#### 3. Cross-Platform MAUI Logging (Serilog Infrastructure)
* **Serilog Integration:** Configured cross-platform Serilog routing in `SerilogConfiguration.cs`, streaming diagnostic logs safely across native platforms and the Blazor Webview container.
* **Interop Bridge:** Built `BlazorLoggingBridge.cs` to capture web console messages and pipe them directly to the native host logger.
* **Demo Interface:** Added an interactive `SerilogDemo.razor` sandbox under Pages.

#### 4. Resolving 401 Load Errors (Authentication Handler Flow)
* **Authentication Header Handler:** Implemented the `MobileAuthenticationHeaderHandler` to correctly extract, validate, and inject bearer JWT tokens into outbound API requests.
* **Configuration-based API Host:** Structured standard API URI routing to use clean configuration bindings in `appsettings.json`.

---

### 🧪 Verification & Build Status
* Run `dotnet build` from the solution root: Successfully compiled the full multi-targeted solution (`Liczba błędów: 0`).
* All unit and integration tests successfully executed and verified (`dotnet test`).

---------

Co-authored-by: Marek Jasiński <jasins.marek@gmail.com>
Co-authored-by: Marek Jaisński <jasins.marek@gmail.com>
Reviewed-on: #51
Co-authored-by: Antigravity <antigravity@google.com>
Co-committed-by: Antigravity <antigravity@google.com>
This commit was merged in pull request #51.
This commit is contained in:
2026-05-26 12:15:28 +00:00
committed by Marek Jaisński
parent 758b152a0c
commit a0bf6c15f4
61 changed files with 5111 additions and 570 deletions
@@ -0,0 +1,99 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Moq;
using NexusReader.Application.Abstractions.Persistence;
using NexusReader.Application.Commands.Quiz;
using NexusReader.Data.Persistence;
using NexusReader.Domain.Entities;
using NexusReader.Infrastructure.Persistence;
using Xunit;
namespace NexusReader.Application.Tests.Commands;
public class SubmitQuizResultCommandHandlerTests
{
private readonly Mock<IQuizResultRepository> _repositoryMock;
public SubmitQuizResultCommandHandlerTests()
{
_repositoryMock = new Mock<IQuizResultRepository>();
}
[Fact]
public async Task Handle_WithValidRequest_PersistsQuizResultToDatabase()
{
// Arrange
var user = new NexusUser
{
Id = "user-abc",
UserName = "testuser",
Email = "test@example.com",
TenantId = "tenant-xyz",
SubscriptionPlanId = 1
};
_repositoryMock.Setup(r => r.FindUserByIdAsync("user-abc", It.IsAny<CancellationToken>()))
.ReturnsAsync(user);
QuizResult? capturedQuizResult = null;
_repositoryMock.Setup(r => r.AddQuizResult(It.IsAny<QuizResult>()))
.Callback<QuizResult>(q => capturedQuizResult = q);
_repositoryMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(1);
var command = new SubmitQuizResultCommand(
UserId: "user-abc",
Topic: "Sprawdzian: .NET 10",
Score: 4,
TotalQuestions: 5
);
var handler = new SubmitQuizResultCommandHandler(_repositoryMock.Object);
// Act
var result = await handler.Handle(command, CancellationToken.None);
// Assert
result.IsSuccess.Should().BeTrue();
capturedQuizResult.Should().NotBeNull();
capturedQuizResult!.Topic.Should().Be("Sprawdzian: .NET 10");
capturedQuizResult.Score.Should().Be(4);
capturedQuizResult.TotalQuestions.Should().Be(5);
capturedQuizResult.TenantId.Should().Be("tenant-xyz");
_repositoryMock.Verify(r => r.AddQuizResult(It.IsAny<QuizResult>()), Times.Once);
_repositoryMock.Verify(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
}
[Fact]
public async Task Handle_WithNonExistentUser_ReturnsFailureResult()
{
// Arrange
_repositoryMock.Setup(r => r.FindUserByIdAsync("non-existent", It.IsAny<CancellationToken>()))
.ReturnsAsync((NexusUser?)null);
var command = new SubmitQuizResultCommand(
UserId: "non-existent",
Topic: "Sprawdzian: .NET 10",
Score: 4,
TotalQuestions: 5
);
var handler = new SubmitQuizResultCommandHandler(_repositoryMock.Object);
// Act
var result = await handler.Handle(command, CancellationToken.None);
// Assert
result.IsFailed.Should().BeTrue();
result.Errors.Should().ContainSingle(e => e.Message == "User not found.");
_repositoryMock.Verify(r => r.AddQuizResult(It.IsAny<QuizResult>()), Times.Never);
_repositoryMock.Verify(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Never);
}
}
@@ -0,0 +1,58 @@
using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using NexusReader.Data.Persistence;
using Xunit;
namespace NexusReader.Application.Tests.Queries;
public class CheckDatabaseTest
{
[Fact]
public async Task PrintDatabaseStats()
{
var configJson = await File.ReadAllTextAsync("../../../../../src/NexusReader.Web/appsettings.json");
var doc = JsonDocument.Parse(configJson);
var pgConn = doc.RootElement.GetProperty("ConnectionStrings").GetProperty("PostgresConnection").GetString();
Console.WriteLine($"Postgres Connection: {pgConn}");
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
optionsBuilder.UseNpgsql(pgConn);
using var context = new AppDbContext(optionsBuilder.Options);
var usersCount = await context.Users.CountAsync();
var ebooksCount = await context.Ebooks.CountAsync();
var unitsCount = await context.KnowledgeUnits.CountAsync();
var cacheCount = await context.SemanticKnowledgeCache.CountAsync();
Console.WriteLine($"=== DATABASE STATS ===");
Console.WriteLine($"Users: {usersCount}");
Console.WriteLine($"Ebooks: {ebooksCount}");
Console.WriteLine($"KnowledgeUnits: {unitsCount}");
Console.WriteLine($"SemanticKnowledgeCache: {cacheCount}");
var users = await context.Users.ToListAsync();
foreach (var u in users)
{
Console.WriteLine($"User: {u.Email}, TenantId: '{u.TenantId}'");
}
var ebooks = await context.Ebooks.ToListAsync();
foreach (var eb in ebooks)
{
Console.WriteLine($"Ebook Id: {eb.Id}, Title: '{eb.Title}', FilePath: '{eb.FilePath}', Ready: {eb.IsReadyForReading}");
}
var cache = await context.SemanticKnowledgeCache.ToListAsync();
foreach (var c in cache)
{
Console.WriteLine($"Cache Hash: {c.ContentHash}, TenantId: '{c.TenantId}', PromptVersion: {c.PromptVersion}, JsonData Preview: {c.JsonData.Substring(0, Math.Min(c.JsonData.Length, 150))}");
}
Assert.True(true);
}
}
@@ -116,11 +116,8 @@ public class QueryTests : IDisposable
public async Task SearchLibrarySemanticallyQuery_WithEmptyQueryText_ReturnsFailure()
{
// Arrange
var handler = new SearchLibrarySemanticallyQueryHandler(
_embeddingGeneratorMock.Object,
_dbContextFactoryMock.Object,
_pipelineProviderMock.Object,
_mapperMock.Object);
var knowledgeServiceMock = new Mock<IKnowledgeService>();
var handler = new SearchLibrarySemanticallyQueryHandler(knowledgeServiceMock.Object);
var query = new SearchLibrarySemanticallyQuery("", "tenant-123");
// Act
@@ -132,39 +129,38 @@ public class QueryTests : IDisposable
}
[Fact]
public async Task SearchLibrarySemanticallyQuery_WithValidQuery_GeneratesEmbeddingAndQueriesDatabase()
public async Task SearchLibrarySemanticallyQuery_WithValidQuery_CallsKnowledgeService()
{
// Arrange
var queryText = "test query";
var tenantId = "tenant-123";
var expectedResponse = new List<SemanticSearchResultDto>
{
new SemanticSearchResultDto
{
Snippet = "Matched content",
RelevanceScore = 0.95f,
SourceBookTitle = "Test Book"
}
};
var mockEmbedding = new Embedding<float>(new float[768]);
var mockResponse = new GeneratedEmbeddings<Embedding<float>>(new[] { mockEmbedding });
_embeddingGeneratorMock.Setup(g => g.GenerateAsync(
It.Is<IEnumerable<string>>(s => s.Contains(queryText)),
It.IsAny<EmbeddingGenerationOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(mockResponse);
var handler = new SearchLibrarySemanticallyQueryHandler(
_embeddingGeneratorMock.Object,
_dbContextFactoryMock.Object,
_pipelineProviderMock.Object,
_mapperMock.Object);
var knowledgeServiceMock = new Mock<IKnowledgeService>();
knowledgeServiceMock.Setup(s => s.SearchLibrarySemanticallyAsync(queryText, tenantId, 5, It.IsAny<CancellationToken>()))
.ReturnsAsync(Result.Ok(expectedResponse));
var handler = new SearchLibrarySemanticallyQueryHandler(knowledgeServiceMock.Object);
var query = new SearchLibrarySemanticallyQuery(queryText, tenantId);
// Act
Func<Task> act = async () => await handler.Handle(query, CancellationToken.None);
var result = await handler.Handle(query, CancellationToken.None);
// Assert (SQLite provider will throw an execution/translation exception since CosineDistance is not supported,
// which confirms that the query built successfully and attempted execution!)
await act.Should().ThrowAsync<Exception>();
// Assert
result.IsSuccess.Should().BeTrue();
result.Value.Should().HaveCount(1);
result.Value.First().Snippet.Should().Be("Matched content");
result.Value.First().SourceBookTitle.Should().Be("Test Book");
_embeddingGeneratorMock.Verify(g => g.GenerateAsync(
It.Is<IEnumerable<string>>(s => s.Contains(queryText)),
It.IsAny<EmbeddingGenerationOptions>(),
It.IsAny<CancellationToken>()), Times.Once);
knowledgeServiceMock.Verify(s => s.SearchLibrarySemanticallyAsync(queryText, tenantId, 5, It.IsAny<CancellationToken>()), Times.Once);
}
[Fact]