using BuildFlow.Shared.Domain.Errors; using FluentAssertions; using Microsoft.EntityFrameworkCore; using Workflow.Application.Form.FormDefinition.Handlers; using Workflow.Application.Form.FormDefinition.Queries; using Workflow.Domain.Entities; using Workflow.Domain.Enums; using Xunit; namespace BuildFlow.Workflow.Tests.Form; public class FormVersionHandlerTests { private static WorkflowDbContext CreateDbContext() { var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new WorkflowDbContext(options); db.SavingChanges += AuditStub.OnSave; return db; } #region CompareFormVersions [Fact] public async Task CompareFormVersions_WithSnapshot_PicksPreviousVersionAsOld() { await using var db = CreateDbContext(); var formId = Guid.NewGuid(); db.FormDefinitions.Add(new FormDefinition { Id = formId, Name = "Form", Code = "form", Version = 2, Status = FormStatus.Draft, SchemaJson = "{\"v\":2}" }); db.FormDefinitionVersions.Add(new FormDefinitionVersion { Id = Guid.NewGuid(), FormDefinitionId = formId, Version = 1, SchemaJson = "{\"v\":1}", Source = "Update" }); await db.SaveChangesAsync(); var handler = new CompareFormVersionsQueryHandler(db); var query = new CompareFormVersionsQuery(formId, OldVersionId: null, NewVersionId: null); var result = await handler.Handle(query, CancellationToken.None); result.Should().NotBeNull(); result.NewVersion.Should().NotBeNull(); result.NewVersion!.Version.Should().Be(2); result.OldVersion.Should().NotBeNull(); result.OldVersion!.Version.Should().Be(1); result.Diff.Should().NotBeNull(); } [Fact] public async Task CompareFormVersions_MissingForm_ThrowsNotFound() { await using var db = CreateDbContext(); var handler = new CompareFormVersionsQueryHandler(db); var query = new CompareFormVersionsQuery(Guid.NewGuid(), null, null); var act = async () => await handler.Handle(query, CancellationToken.None); var ex = await act.Should().ThrowAsync(); ex.Which.Code.Should().Be(ErrorCode.NotFound); } #endregion #region GetFormVersions [Fact] public async Task GetFormVersions_ReturnsOrderedList() { await using var db = CreateDbContext(); var formId = Guid.NewGuid(); db.FormDefinitions.Add(new FormDefinition { Id = formId, Name = "Form", Code = "form", Version = 3, Status = FormStatus.Draft }); db.FormDefinitionVersions.Add(new FormDefinitionVersion { Id = Guid.NewGuid(), FormDefinitionId = formId, Version = 1, SchemaJson = "{\"v\":1}" }); db.FormDefinitionVersions.Add(new FormDefinitionVersion { Id = Guid.NewGuid(), FormDefinitionId = formId, Version = 2, SchemaJson = "{\"v\":2}" }); await db.SaveChangesAsync(); var handler = new GetFormVersionsQueryHandler(db); var query = new GetFormVersionsQuery(formId); var result = await handler.Handle(query, CancellationToken.None); result.Should().NotBeNull(); result.Should().HaveCount(2); result[0].Version.Should().Be(2); result[1].Version.Should().Be(1); } [Fact] public async Task GetFormVersions_MissingForm_ThrowsNotFound() { await using var db = CreateDbContext(); var handler = new GetFormVersionsQueryHandler(db); var query = new GetFormVersionsQuery(Guid.NewGuid()); var act = async () => await handler.Handle(query, CancellationToken.None); var ex = await act.Should().ThrowAsync(); ex.Which.Code.Should().Be(ErrorCode.NotFound); } #endregion }