Files
Nexus.Reader/tests/NexusReader.Application.Tests/Queries/QueryTests.cs
T
Antigravity 541e9e1fb5 feat(ai-ux): deduplicate AI queries, handle ServiceUnavailable retries, and optimize reader canvas graph prerendering (#44)
This Pull Request encapsulates all outstanding AI, Blazor InteractiveAuto lifecycle, pgvector, and Firefox authorization/session compatibility fixes.

### Key Accomplishments:
1. **Concurrent Request Deduplication (Option B):** Implemented a thread-safe active task registry in `KnowledgeService` that groups concurrent graph extraction queries for the same content, preventing duplicate AI calls completely.
2. **Resilience Strategy for Downstream Demands:** Extended the `ai-retry` resilience pipeline to automatically intercept and retry on temporary Google API `503 ServiceUnavailable` / `high demand` spikes.
3. **Interactive Graph Generation Guard (Option A):** Prevented server-side prerender-phase graph requests in the reader canvas component.
4. **Firefox Compatibility & Cookie Handler:** Implemented an authentication endpoint and hybrid hidden-form submission flow to solve login, registration, and logout redirections and cookies securely.
5. **Autoscrolling & Graph Exclusions:** Added concept-to-block smooth scrolling, active block badging, and filtered out markdown code blocks from being extracted as nodes.

All unit tests compiled and passed 100% cleanly.

---------

Co-authored-by: Marek Jasiński <jasins.marek@gmail.com>
Reviewed-on: #44
Co-authored-by: Antigravity <antigravity@google.com>
Co-committed-by: Antigravity <antigravity@google.com>
2026-05-18 17:53:36 +00:00

174 lines
6.5 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 NexusReader.Application.DTOs.AI;
using NexusReader.Application.DTOs.User;
using NexusReader.Application.Queries.Library;
using NexusReader.Data.Persistence;
using NexusReader.Domain.Entities;
using Pgvector;
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 handler = new SearchLibrarySemanticallyQueryHandler(_dbContextFactoryMock.Object, _embeddingGeneratorMock.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_WithNoResults_TriggersFallback1536Embedding()
{
// Arrange
// Mock 768-dim primary embedding generator response
var embedding768 = new Embedding<float>(new float[768]);
var mockResponse768 = new GeneratedEmbeddings<Embedding<float>>(new List<Embedding<float>> { embedding768 });
_embeddingGeneratorMock.Setup(g => g.GenerateAsync(
It.Is<IEnumerable<string>>(s => s.Contains("test")),
It.Is<EmbeddingGenerationOptions>(o => o.Dimensions == 768),
It.IsAny<CancellationToken>()))
.ReturnsAsync(mockResponse768);
// Mock 1536-dim fallback embedding generator response
var embedding1536 = new Embedding<float>(new float[1536]);
var mockResponse1536 = new GeneratedEmbeddings<Embedding<float>>(new List<Embedding<float>> { embedding1536 });
_embeddingGeneratorMock.Setup(g => g.GenerateAsync(
It.Is<IEnumerable<string>>(s => s.Contains("test")),
It.Is<EmbeddingGenerationOptions>(o => o.Dimensions == 1536),
It.IsAny<CancellationToken>()))
.ReturnsAsync(mockResponse1536);
// Seed one legacy cache entry
using (var context = new AppDbContext(_contextOptions))
{
var cacheEntry = new SemanticKnowledgeCache
{
TenantId = "tenant-123",
ContentHash = "hash-123",
OriginalText = "Fallback Cache Content Snippet",
Vector = new Vector(new float[1536]),
PromptVersion = "1",
CreatedAt = DateTime.UtcNow
};
context.SemanticKnowledgeCache.Add(cacheEntry);
await context.SaveChangesAsync();
}
var handler = new SearchLibrarySemanticallyQueryHandler(_dbContextFactoryMock.Object, _embeddingGeneratorMock.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("Fallback Cache Content Snippet");
result.Value.First().ContentHash.Should().Be("hash-123");
}
public void Dispose()
{
_connection.Close();
_connection.Dispose();
}
}