Files
KArtSell.Aegis/tests/KArtSell.Integration.Tests/ApprovalWorkflow/ApprovalWorkflowTests.cs
T
kjh2064 b1e38ac374 feat: Phase 3 J/K/L (Sell Decision, Trade Execution, Portfolio Reconciliation) + fix pre-existing build/boot breakage
Completes VS-10/VS-12/VS-14 and makes the solution and Host actually
build and boot for the first time on this branch (main did not build
before this commit).

Root-cause fixes required to reach a green build/boot (not scoped to
J/K/L but blocking any verification of it):
- Restore Polly PackageVersion accidentally deleted from
  Directory.Packages.props (broke KArtSell.Host).
- Remove MediatR dependency from Compliance/VS-04 (package was never
  installed; ICommand/ICommandHandler/IMediator never existed) and
  wire Endpoint -> Handler directly per this repo's convention.
- Migrate FastEndpoints v5 API calls (SendOkAsync/SendAsync/
  SendCreatedAtAsync/SendNotFoundAsync, Description().WithName()) to
  the v7 Send.* fluent API across ~10 endpoint files.
- Fix migrations 0036/0038/0039/0040: rewritten from invalid T-SQL
  (`IF NOT EXISTS ... BEGIN ... END`) to idiomatic Postgres
  (`CREATE TABLE/INDEX IF NOT EXISTS`) — these could not apply to any
  fresh database before this fix.
- Collapse 3 duplicate cross-cutting abstractions that shadowed the
  BuildingBlocks versions and caused type-mismatch compile errors:
  IKrxDataService, IOutboxWriter (ReconcileTradeHandler), IClock
  (ApprovalWorkflow/ApprovalPolicy).
- Inject IClock (BuildingBlocks.Time) in place of direct
  DateTime.Now/UtcNow across 19 files to satisfy the architecture
  test AGENTS.md#DateTime-abstraction rule (13/13 architecture tests
  now pass, was 12/13).
- Register all new and previously-unregistered slices in
  Program.cs DI (SellDecision, TradeExecution, PortfolioReconciliation,
  Compliance, Features/ApprovalWorkflow) — the Host had never
  successfully completed a boot with this code present.
- Disable ("[DontRegister]") the older, route-colliding
  ApprovalWorkflow/ (Workstream H) endpoint set in favor of
  Features/ApprovalWorkflow/ (Workstream G, matches the documented
  Features/<Slice>/ convention); kept for its existing test coverage.
  See TECH_DEBT-017 for the follow-up decision needed.

Verified: dotnet build 0 errors/0 warnings; architecture tests 13/13;
unit tests 54/54 + 18/18; integration tests 34/36 (2 failures are a
local test-DB migration-journal/schema mismatch, not a code defect);
Host boots cleanly and registers all 34 endpoints.

New tech debt recorded: DEBT-017 (duplicate VS-03 implementation),
DEBT-018 (outbox write not co-transactional with entity write in
TradeExecution/PortfolioReconciliation), DEBT-019 (duplicate
BuildingBlocks-shadowing abstractions, partially resolved).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-07 19:53:38 +09:00

200 lines
5.7 KiB
C#

namespace KArtSell.Integration.Tests.ApprovalWorkflow;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
using KArtSell.BuildingBlocks.Time;
using KArtSell.Modules.ModelOperations.ApprovalWorkflow;
public class ApprovalWorkflowTests : IAsyncLifetime
{
private readonly string _connectionString;
private readonly ApprovalSql _sql;
private readonly ApprovalPolicy _policy;
private readonly IOutbox _outbox;
public ApprovalWorkflowTests()
{
_connectionString = "Host=localhost;Port=5432;Database=kartselldb;Username=kartsell;Password=kartsell4321@!";
_sql = new ApprovalSql(_connectionString, new SystemClock());
_policy = new ApprovalPolicy(new SystemClock());
_outbox = new InMemoryOutbox();
}
public async Task InitializeAsync()
{
// Ensure database is ready
await Task.CompletedTask;
}
public async Task DisposeAsync()
{
await Task.CompletedTask;
}
[Fact]
public void CanCreateProposal_WithMakerRole_ReturnsTrue()
{
// Arrange
var makerEmail = "maker@company.com";
var makerRole = "Maker";
// Act
var result = _policy.CanCreateProposal(makerEmail, makerRole);
// Assert
Assert.True(result);
}
[Fact]
public void CanCreateProposal_WithoutMakerRole_ReturnsFalse()
{
// Arrange
var email = "user@company.com";
var role = "Viewer";
// Act
var result = _policy.CanCreateProposal(email, role);
// Assert
Assert.False(result);
}
[Fact]
public void CanApproveApproval_WithDifferentChecker_ReturnsTrue()
{
// Arrange
var maker = "maker@company.com";
var checker = "checker@company.com";
var proposal = new ApprovalProposal { CreatedBy = maker, Status = ApprovalStatus.Proposed };
// Act
var result = _policy.CanApproveApproval(proposal, checker, maker);
// Assert
Assert.True(result);
}
[Fact]
public void CanApproveApproval_WithSameMaker_ReturnsFalse()
{
// Arrange
var maker = "maker@company.com";
var proposal = new ApprovalProposal { CreatedBy = maker, Status = ApprovalStatus.Proposed };
// Act
var result = _policy.CanApproveApproval(proposal, maker, maker);
// Assert
Assert.False(result);
}
[Fact]
public void CreateProposal_SetsCorrectDefaults()
{
// Arrange
var modelId = Guid.NewGuid();
var maker = "maker@company.com";
var justification = "Model passed OOS testing";
var effectiveAt = DateOnly.FromDateTime(DateTime.UtcNow.AddDays(7));
// Act
var proposal = _policy.CreateProposal(modelId, maker, justification, effectiveAt);
// Assert
Assert.Equal(modelId, proposal.ModelId);
Assert.Equal(maker, proposal.CreatedBy);
Assert.Equal(ApprovalStatus.Draft, proposal.Status);
Assert.Equal(justification, proposal.Justification);
Assert.Equal(effectiveAt, proposal.EffectiveAt);
}
[Fact]
public void ProposeApproval_TransitionsToProposed()
{
// Arrange
var proposal = new ApprovalProposal
{
Id = Guid.NewGuid(),
CreatedBy = "maker@company.com",
Status = ApprovalStatus.Draft,
Justification = "Test",
EffectiveAt = DateOnly.FromDateTime(DateTime.UtcNow)
};
// Act
var updated = _policy.ProposeApproval(proposal, "maker@company.com");
// Assert
Assert.Equal(ApprovalStatus.Proposed, updated.Status);
Assert.NotNull(updated.ProposedAt);
}
[Fact]
public void ApproveApproval_AddsEvidence()
{
// Arrange
var proposal = new ApprovalProposal
{
Id = Guid.NewGuid(),
CreatedBy = "maker@company.com",
Status = ApprovalStatus.Proposed,
Evidence = [],
CorrelationId = Guid.NewGuid()
};
var evidence = new List<EvidenceItem>
{
new() { Type = "PBO_SCORE", Url = "s3://pbo-0.95.json", Comment = "Verified" }
};
// Act
var updated = _policy.ApproveApproval(proposal, "checker@company.com", "Looks good", evidence);
// Assert
Assert.Equal(ApprovalStatus.Approved, updated.Status);
Assert.Equal("checker@company.com", updated.ApprovedBy);
Assert.Single(updated.Evidence);
Assert.Equal("PBO_SCORE", updated.Evidence[0].EvidenceType);
}
[Fact]
public async Task InsertAndRetrieveProposal_RoundTrips()
{
// Arrange
var id = Guid.NewGuid();
var modelId = Guid.NewGuid();
var correlationId = Guid.NewGuid();
// Act
await _sql.InsertProposalAsync(
id,
modelId,
"Draft",
"maker@company.com",
"Test justification",
DateOnly.FromDateTime(DateTime.UtcNow),
DateTimeOffset.UtcNow,
correlationId);
var retrieved = await _sql.GetProposalByIdAsync(id, DateTimeOffset.UtcNow.AddDays(1));
// Assert
Assert.NotNull(retrieved);
Assert.Equal(id, retrieved.Id);
Assert.Equal(modelId, retrieved.ModelId);
Assert.Equal(correlationId, retrieved.CorrelationId);
}
}
public class InMemoryOutbox : IOutbox
{
public List<(string EventType, Guid CorrelationId, object Data)> Events { get; } = [];
public Task PublishAsync(string eventType, Guid correlationId, object data)
{
Events.Add((eventType, correlationId, data));
return Task.CompletedTask;
}
}