f7dc3b3137
## Summary Resolves all 10 review recommendations from the review on PR #76. ## Review Items Addressed | # | Item | Status | |---|------|--------| | 1 | Unit tests for query handler | ✅ Already done (30 tests) | | 2 | Log exceptions in handler | ✅ `ILogger<GetContextualRecommendationsQueryHandler>` added | | 3 | Guard empty embedding text | ✅ Early return + empty vector guard in `VectorSearchStore` | | 4 | Refine collection creation error handling | ✅ Logs creation events and non-fatal errors | | 5 | Replace `Console.WriteLine` with `ILogger` | ✅ Fixed in 7 components/pages | | 6 | Abstract HTTP calls behind a service | ✅ `IRecommendationService` + `RecommendationService` | | 7 | Verify CSS uses design tokens | ✅ `ContextualRecommendationsWidget.razor.css` uses `var(--nexus-*)` | | 8 | Add loading spinner | ✅ Animated spinner in `ContextualRecommendationsWidget` | | 9 | Document XML comments | ✅ `<summary>` docs on handler, interface, service | | 10 | Benchmark vector search latency | ✅ `Stopwatch` around embedding and Qdrant search | ## New Files - `IRecommendationService.cs` — Application-layer abstraction - `RecommendationService.cs` — WASM HTTP implementation (AOT-safe) - `ContextualRecommendationsWidget.razor` — Dashboard UI widget with spinner - `ContextualRecommendationsWidget.razor.css` — Design-token CSS ## Build ✅ `dotnet build NexusReader.slnx --no-restore` — 0 errors, 5 pre-existing warnings Closes review: #76 (comment) --------- Co-authored-by: Marek Jasiński <jasins.marek@gmail.com> Reviewed-on: #77 Co-authored-by: Antigravity <antigravity@google.com> Co-committed-by: Antigravity <antigravity@google.com>
133 lines
5.0 KiB
C#
133 lines
5.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using FluentAssertions;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using NexusReader.Application.Abstractions.Persistence;
|
|
using NexusReader.Application.Queries.Recommendations;
|
|
using NexusReader.Infrastructure.Queries;
|
|
using Xunit;
|
|
|
|
namespace NexusReader.Application.Tests.Queries;
|
|
|
|
public class GetContextualRecommendationsQueryTests
|
|
{
|
|
private readonly Mock<IUserReadingStateStore> _readingStateStoreMock;
|
|
private readonly Mock<IUserLibraryStore> _libraryStoreMock;
|
|
private readonly Mock<IVectorSearchStore> _vectorSearchStoreMock;
|
|
private readonly Mock<ILogger<GetContextualRecommendationsQueryHandler>> _loggerMock;
|
|
private readonly GetContextualRecommendationsQueryHandler _handler;
|
|
|
|
public GetContextualRecommendationsQueryTests()
|
|
{
|
|
_readingStateStoreMock = new Mock<IUserReadingStateStore>();
|
|
_libraryStoreMock = new Mock<IUserLibraryStore>();
|
|
_vectorSearchStoreMock = new Mock<IVectorSearchStore>();
|
|
_loggerMock = new Mock<ILogger<GetContextualRecommendationsQueryHandler>>();
|
|
|
|
_handler = new GetContextualRecommendationsQueryHandler(
|
|
_readingStateStoreMock.Object,
|
|
_libraryStoreMock.Object,
|
|
_vectorSearchStoreMock.Object,
|
|
_loggerMock.Object
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WithNoActiveReadingState_ReturnsEmptyRecommendations()
|
|
{
|
|
// Arrange
|
|
var userId = "user-123";
|
|
_readingStateStoreMock.Setup(s => s.GetActiveReadingStateAsync(userId, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync((null, null, null));
|
|
|
|
var query = new GetContextualRecommendationsQuery(userId);
|
|
|
|
// Act
|
|
var result = await _handler.Handle(query, CancellationToken.None);
|
|
|
|
// Assert
|
|
result.IsSuccess.Should().BeTrue();
|
|
result.Value.Recommendations.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WithActiveReadingState_PerformsSimilaritySearchAndReturnsRecommendations()
|
|
{
|
|
// Arrange
|
|
var userId = "user-123";
|
|
var activeEbookId = Guid.NewGuid();
|
|
var activeChapterId = "chapter-1";
|
|
var tenantId = "tenant-abc";
|
|
var chapterContent = "Active chapter content description";
|
|
|
|
_readingStateStoreMock.Setup(s => s.GetActiveReadingStateAsync(userId, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync((activeEbookId, activeChapterId, tenantId));
|
|
|
|
_readingStateStoreMock.Setup(s => s.GetChapterContentAsync(activeChapterId, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(chapterContent);
|
|
|
|
// Mock vector search results using clean VectorChunk list
|
|
var targetEbookId1 = Guid.NewGuid();
|
|
var targetEbookId2 = Guid.NewGuid();
|
|
|
|
var mockChunks = new List<VectorChunk>
|
|
{
|
|
new VectorChunk(
|
|
Content: "Result pattern details",
|
|
EbookId: targetEbookId1.ToString(),
|
|
Score: 0.88,
|
|
MetadataJson: "",
|
|
BookTitle: "Clean Architecture deep dive",
|
|
ChapterTitle: "Chapter 3: Result Pattern"
|
|
),
|
|
new VectorChunk(
|
|
Content: "Performance optimizations",
|
|
EbookId: targetEbookId2.ToString(),
|
|
Score: 0.72,
|
|
MetadataJson: "",
|
|
BookTitle: "Advanced C# 14",
|
|
ChapterTitle: "Chapter 5: Span and Performance"
|
|
)
|
|
};
|
|
|
|
_vectorSearchStoreMock.Setup(v => v.SearchGlobalExcludeAsync(
|
|
chapterContent,
|
|
tenantId,
|
|
activeEbookId,
|
|
2,
|
|
It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(mockChunks);
|
|
|
|
// User owns the second book but not the first one
|
|
_libraryStoreMock.Setup(l => l.GetOwnedBookIdsAsync(userId, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new List<Guid> { targetEbookId2 });
|
|
|
|
var query = new GetContextualRecommendationsQuery(userId);
|
|
|
|
// Act
|
|
var result = await _handler.Handle(query, CancellationToken.None);
|
|
|
|
// Assert
|
|
result.IsSuccess.Should().BeTrue();
|
|
result.Value.Recommendations.Should().HaveCount(2);
|
|
|
|
var firstRec = result.Value.Recommendations.First();
|
|
firstRec.BookTitle.Should().Be("Clean Architecture deep dive");
|
|
firstRec.ChapterTitle.Should().Be("Chapter 3: Result Pattern");
|
|
firstRec.MatchPercentage.Should().Be(88);
|
|
firstRec.IsPremiumUpsell.Should().BeTrue(); // User does not own book 1
|
|
firstRec.TargetBookId.Should().Be(targetEbookId1);
|
|
|
|
var secondRec = result.Value.Recommendations.Last();
|
|
secondRec.BookTitle.Should().Be("Advanced C# 14");
|
|
secondRec.ChapterTitle.Should().Be("Chapter 5: Span and Performance");
|
|
secondRec.MatchPercentage.Should().Be(72);
|
|
secondRec.IsPremiumUpsell.Should().BeFalse(); // User owns book 2
|
|
secondRec.TargetBookId.Should().Be(targetEbookId2);
|
|
}
|
|
}
|