Files
Nexus.Reader/tests/NexusReader.Application.Tests/Commands/CreateBookTests.cs
T
Antigravity c94e8f0acb feat(creator): overhaul Creator flow, editor duplication, and staging setup (#83)
This pull request completely overhauls the Creator editor flow, resolves the editor duplication race condition, aligns layout/styling themes in light and dark mode, and adds Docker staging setups.

### Key Changes
1. **Creator Flow Polish**: Redesigned the editor canvas to prevent double scrolling by delegating overflow to the editor canvas layer, updated styles to a premium aesthetic.
2. **Race Condition Prevention**: Resolved Crepe editor duplication when loading or switching chapters by tracking state via shared window maps (`window.editorCache`, `window.editorStates`) and checking `_lastInitializedEditorId` synchronously in Blazor.
3. **Theme Synchronization**: Integrated explicit theme initialization (`ThemeService.InitializeAsync()`) and anchored CSS isolation selectors to correctly sync with Light (Soft Sepia) and Deep Dark theme preferences.
4. **Staging Automation**: Created staging docker configurations with `--nexus-only` flag to allow iterative development without resetting PG/Neo4j database containers.

---------

Co-authored-by: Marek Jasiński <jasins.marek@gmail.com>
Reviewed-on: #83
Co-authored-by: Antigravity <antigravity@google.com>
Co-committed-by: Antigravity <antigravity@google.com>
2026-06-15 17:15:42 +00:00

174 lines
5.7 KiB
C#

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Moq;
using NexusReader.Application.Features.Books.Commands;
using NexusReader.Data.Persistence;
using NexusReader.Domain.Entities;
using Xunit;
namespace NexusReader.Application.Tests.Commands;
public class CreateBookTests : IDisposable
{
private readonly SqliteConnection _connection;
private readonly DbContextOptions<AppDbContext> _contextOptions;
private readonly Mock<IDbContextFactory<AppDbContext>> _dbContextFactoryMock;
public CreateBookTests()
{
_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));
}
private NexusUser SeedUser(string userId, string tenantId)
{
var user = new NexusUser
{
Id = userId,
UserName = $"user_{userId}",
Email = $"{userId}@example.com",
TenantId = tenantId,
SubscriptionPlanId = 1
};
using var context = new AppDbContext(_contextOptions);
context.Users.Add(user);
context.SaveChanges();
return user;
}
[Fact]
public async Task Handle_WithValidCommand_SuccessfullyCreatesBookRevisionAndIntroductionChapter()
{
// Arrange
var userId = "creator-123";
var tenantId = "tenant-abc";
SeedUser(userId, tenantId);
var command = new CreateBookCommand(
Title: "The Art of Agentic Systems",
Description: "A masterclass on building self-healing AI agents.",
UserId: userId,
TenantId: tenantId
);
var handler = new CreateBookCommandHandler(_dbContextFactoryMock.Object);
// Act
var result = await handler.Handle(command, CancellationToken.None);
// Assert
result.IsSuccess.Should().BeTrue();
result.Value.Should().NotBeEmpty();
using (var context = new AppDbContext(_contextOptions))
{
var book = await context.Books
.Include(b => b.CurrentDraftRevision)
.ThenInclude(r => r!.Chapters)
.FirstOrDefaultAsync(b => b.Id == result.Value);
book.Should().NotBeNull();
book!.Title.Should().Be("The Art of Agentic Systems");
book.UserId.Should().Be(userId);
book.TenantId.Should().Be(tenantId);
book.CurrentDraftRevisionId.Should().NotBeNull();
var revision = book.CurrentDraftRevision;
revision.Should().NotBeNull();
revision!.VersionString.Should().Be("Working Draft");
revision.IsPublished.Should().BeFalse();
revision.BookId.Should().Be(book.Id);
revision.Chapters.Should().HaveCount(1);
var chapter = revision.Chapters.First();
chapter.Title.Should().Be("Introduction");
chapter.MarkdownContent.Should().Be("# Introduction\nStart writing here...");
chapter.SortOrder.Should().Be(1);
}
}
[Fact]
public async Task Handle_WithEmptyTitle_ReturnsFailureResult()
{
// Arrange
var command = new CreateBookCommand(
Title: "",
Description: "No title",
UserId: "user-1",
TenantId: "tenant-1"
);
var handler = new CreateBookCommandHandler(_dbContextFactoryMock.Object);
// Act
var result = await handler.Handle(command, CancellationToken.None);
// Assert
result.IsSuccess.Should().BeFalse();
result.Errors.Should().NotBeEmpty();
result.Errors.First().Message.Should().Contain("title is required");
}
[Fact]
public async Task Handle_OnDatabaseViolation_RollsBackTransaction()
{
// Arrange
// We trigger a database violation by not seeding the user 'missing-user'
// and letting the foreign key constraint fail (if SQLite enforces it).
// If foreign keys aren't strictly enforced on SQLite by default without PRAGMA,
// we can check if it rolls back upon other violations, or manually verify error handling.
var command = new CreateBookCommand(
Title: "Violating Book",
Description: "Triggering constraint failure",
UserId: "non-existent-user-id-constraint",
TenantId: "tenant-1"
);
// Let's force foreign key constraints on SQLite to verify rollback
using (var context = new AppDbContext(_contextOptions))
{
context.Database.ExecuteSqlRaw("PRAGMA foreign_keys = ON;");
}
var handler = new CreateBookCommandHandler(_dbContextFactoryMock.Object);
// Act
var result = await handler.Handle(command, CancellationToken.None);
// Assert
result.IsSuccess.Should().BeFalse();
result.Errors.Should().NotBeEmpty();
// Ensure nothing was committed to the DB
using (var context = new AppDbContext(_contextOptions))
{
var books = await context.Books.ToListAsync();
books.Should().BeEmpty();
}
}
public void Dispose()
{
_connection.Close();
_connection.Dispose();
}
}