2ccf74c410
- KArtSell.Host.csproj: FrontendFiles glob was evaluated at project-load time, before pnpm build ran, so it copied stale/missing Vite-hashed filenames every Release build. Move the glob inside the target, after the build Exec. - ApprovalSql/AuditSql/TradeSql: fix live-DB integration failures never caught by unit tests: DateOnly and inet columns can't be bound/read directly through Dapper without conversion; kis_response (jsonb) read as JsonElement threw InvalidCastException; GdprRetention.RetentionEndsAt was typed DateTime against a DATE column. - TradeSql: UpdateTradeStatusAsync only ever persisted status/kis_response /error_message, silently dropping kis_order_id, executed_quantity, unit_price, total_amount, commission, net_proceeds and the execution/ settlement timestamps on every call. Changed it to take the Trade aggregate so the full state transition persists. - TradeSql: add a static ctor setting Dapper.DefaultTypeMap. MatchNamesWithUnderscores = true. The repo's [ModuleInitializer] in KArtSell.BuildingBlocks only fires once that assembly is actually loaded; TradeSql/Trade never reference a BuildingBlocks type, so under test isolation (or any host that queries a trade before touching BuildingBlocks) every snake_case column silently mapped to null/default. - Test fixes: seed the FK prerequisites (model_operations.models, sell_decisions) that ApprovalWorkflowTests/TradeExecutionTests were missing, correct a SellPriorityRanker test input to match the approved VS-10-SLICE_SPEC age-boost threshold, and fix a GDPR redaction assertion that called ToString() on a Dictionary instead of inspecting its values. 12 DbUpMigrationTests failures remain and are unrelated to this fix: the kartsell DB user isn't the owner of kartsell_migration_test, so DbUp's fresh-database rehearsal can't DROP/CREATE it. Needs a DBA grant. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
341 lines
10 KiB
C#
341 lines
10 KiB
C#
namespace KArtSell.Integration.Tests.SellDecision;
|
|
|
|
using KArtSell.Modules.ModelOperations.SellDecision;
|
|
using Xunit;
|
|
|
|
public class PboValidatorTests
|
|
{
|
|
private readonly IPboValidator _validator = new PboValidator();
|
|
|
|
[Fact]
|
|
public void ValidatePboScore_ValidScore_ReturnsTrue()
|
|
{
|
|
var (isValid, reason) = _validator.ValidatePboScore(0.72m, 0.65m);
|
|
Assert.True(isValid);
|
|
Assert.Contains(">=", reason);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidatePboScore_InvalidScore_ReturnsFalse()
|
|
{
|
|
var (isValid, reason) = _validator.ValidatePboScore(0.55m, 0.65m);
|
|
Assert.False(isValid);
|
|
Assert.Contains("Backtest overfit risk", reason);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidatePboScore_NullScore_ReturnsFalse()
|
|
{
|
|
var (isValid, reason) = _validator.ValidatePboScore(null, 0.65m);
|
|
Assert.False(isValid);
|
|
Assert.Contains("not yet available", reason);
|
|
}
|
|
}
|
|
|
|
public class DsrValidatorTests
|
|
{
|
|
private readonly IDsrValidator _validator = new DsrValidator();
|
|
|
|
[Fact]
|
|
public void ValidateDsrMetric_ValidMetric_ReturnsTrue()
|
|
{
|
|
var (isValid, reason) = _validator.ValidateDsrMetric(0.018m, 0.015m);
|
|
Assert.True(isValid);
|
|
Assert.Contains(">=", reason);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateDsrMetric_InvalidMetric_ReturnsFalse()
|
|
{
|
|
var (isValid, reason) = _validator.ValidateDsrMetric(0.010m, 0.015m);
|
|
Assert.False(isValid);
|
|
Assert.Contains("Daily Sharpe ratio", reason);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateDsrMetric_NullMetric_ReturnsFalse()
|
|
{
|
|
var (isValid, reason) = _validator.ValidateDsrMetric(null, 0.015m);
|
|
Assert.False(isValid);
|
|
Assert.Contains("not yet available", reason);
|
|
}
|
|
}
|
|
|
|
public class OosValidatorTests
|
|
{
|
|
private readonly IOosValidator _validator = new OosValidator();
|
|
|
|
[Fact]
|
|
public void ValidateOosPerformance_ValidReturn_ReturnsTrue()
|
|
{
|
|
var (isValid, reason) = _validator.ValidateOosPerformance("0.08", 0.05m);
|
|
Assert.True(isValid);
|
|
Assert.Contains(">=", reason);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateOosPerformance_InvalidReturn_ReturnsFalse()
|
|
{
|
|
var (isValid, reason) = _validator.ValidateOosPerformance("0.03", 0.05m);
|
|
Assert.False(isValid);
|
|
Assert.Contains("underperforms", reason);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateOosPerformance_NullData_ReturnsFalse()
|
|
{
|
|
var (isValid, reason) = _validator.ValidateOosPerformance(null, 0.05m);
|
|
Assert.False(isValid);
|
|
Assert.Contains("not yet available", reason);
|
|
}
|
|
}
|
|
|
|
public class SellPriorityRankerTests
|
|
{
|
|
private readonly ISellPriorityRanker _ranker = new SellPriorityRanker();
|
|
|
|
[Fact]
|
|
public void RankByPolicy_HardImpairment_ReturnsHardImpairment()
|
|
{
|
|
var priority = _ranker.RankByPolicy(drawdown: -0.35m, marginRatio: 0.5m, concentration: 0.1m, liquidity: 0.8m, daysHeld: 100);
|
|
Assert.Equal(SellPriority.HardImpairment, priority);
|
|
}
|
|
|
|
[Fact]
|
|
public void RankByPolicy_PortfolioSurvival_ReturnsPortfolioSurvival()
|
|
{
|
|
var priority = _ranker.RankByPolicy(drawdown: 0m, marginRatio: 0.15m, concentration: 0.1m, liquidity: 0.8m, daysHeld: 100);
|
|
Assert.Equal(SellPriority.PortfolioSurvival, priority);
|
|
}
|
|
|
|
[Fact]
|
|
public void RankByPolicy_Concentration_ReturnsConcentration()
|
|
{
|
|
var priority = _ranker.RankByPolicy(drawdown: -0.05m, marginRatio: 0.5m, concentration: 0.3m, liquidity: 0.8m, daysHeld: 100);
|
|
Assert.Equal(SellPriority.Concentration, priority);
|
|
}
|
|
|
|
[Fact]
|
|
public void CalculateScore_HardImpairment_ReturnsLowestScore()
|
|
{
|
|
var score = _ranker.CalculateScore(SellPriority.HardImpairment, fundAgeDays: 400, liquidityPercent: 0.5m);
|
|
Assert.Equal(950m, score); // 1000 - 50 (age boost, fundAgeDays > 365 per VS-10-SLICE_SPEC.md)
|
|
}
|
|
|
|
[Fact]
|
|
public void CalculateScore_ReentryOption_ReturnsHighestScore()
|
|
{
|
|
var score = _ranker.CalculateScore(SellPriority.ReentryOption, fundAgeDays: 100, liquidityPercent: 0.5m);
|
|
Assert.Equal(50m, score); // No boosts applied
|
|
}
|
|
|
|
[Fact]
|
|
public void CalculateScore_IlliquidFund_ReducesScore()
|
|
{
|
|
var scoreHighLiquidity = _ranker.CalculateScore(SellPriority.Concentration, fundAgeDays: 100, liquidityPercent: 0.5m);
|
|
var scoreLowLiquidity = _ranker.CalculateScore(SellPriority.Concentration, fundAgeDays: 100, liquidityPercent: 0.1m);
|
|
Assert.True(scoreLowLiquidity < scoreHighLiquidity); // Illiquid = higher priority (lower score)
|
|
}
|
|
}
|
|
|
|
public class SellDecisionEntityTests
|
|
{
|
|
[Fact]
|
|
public void SellDecisionEntity_CreatedWithAllFields_StoresCorrectly()
|
|
{
|
|
var now = DateTime.UtcNow;
|
|
var entity = new SellDecisionEntity
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
ModelId = Guid.NewGuid(),
|
|
Status = "PENDING",
|
|
PboScore = 0.72m,
|
|
DsrMetric = 0.018m,
|
|
OosPerformance = "0.08",
|
|
SellPriority = 1,
|
|
TargetQuantity = 500,
|
|
TargetPrice = 150.25m,
|
|
CreatedAt = now,
|
|
CreatedBy = "user@example.com",
|
|
CreatedJustification = "test",
|
|
PublishedAt = now,
|
|
CorrelationId = Guid.NewGuid(),
|
|
Revision = 1
|
|
};
|
|
|
|
Assert.Equal("PENDING", entity.Status);
|
|
Assert.Equal(0.72m, entity.PboScore);
|
|
}
|
|
}
|
|
|
|
public class SellDecisionStateTransitionTests
|
|
{
|
|
[Theory]
|
|
[InlineData("PENDING", "SIGNAL_GENERATED", true)]
|
|
[InlineData("SIGNAL_GENERATED", "PBO_VALIDATED", true)]
|
|
[InlineData("PBO_VALIDATED", "DSR_VALIDATED", true)]
|
|
[InlineData("DSR_VALIDATED", "OOS_APPROVED", true)]
|
|
[InlineData("OOS_APPROVED", "READY_FOR_APPROVAL", true)]
|
|
[InlineData("READY_FOR_APPROVAL", "APPROVED", true)]
|
|
[InlineData("APPROVED", "EXECUTED", true)]
|
|
[InlineData("EXECUTED", "CONFIRMED", true)]
|
|
[InlineData("PENDING", "APPROVED", false)] // Invalid: skipping states
|
|
public void StateTransition_ValidatesAllowedPaths(string fromState, string toState, bool shouldBeValid)
|
|
{
|
|
var validTransitions = new[]
|
|
{
|
|
("PENDING", "SIGNAL_GENERATED"),
|
|
("SIGNAL_GENERATED", "PBO_VALIDATED"),
|
|
("PBO_VALIDATED", "DSR_VALIDATED"),
|
|
("DSR_VALIDATED", "OOS_APPROVED"),
|
|
("OOS_APPROVED", "READY_FOR_APPROVAL"),
|
|
("READY_FOR_APPROVAL", "APPROVED"),
|
|
("APPROVED", "EXECUTED"),
|
|
("EXECUTED", "CONFIRMED")
|
|
};
|
|
|
|
var isValid = validTransitions.Contains((fromState, toState));
|
|
Assert.Equal(shouldBeValid, isValid);
|
|
}
|
|
}
|
|
|
|
public class SellDecisionPitTrackingTests
|
|
{
|
|
[Fact]
|
|
public void SellDecision_IncludesCorrelationIdForTracing()
|
|
{
|
|
var correlationId = Guid.NewGuid();
|
|
var entity = new SellDecisionEntity
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
ModelId = Guid.NewGuid(),
|
|
Status = "PENDING",
|
|
OosPerformance = "0.08",
|
|
CreatedBy = "user@example.com",
|
|
CreatedJustification = "test",
|
|
CorrelationId = correlationId,
|
|
PublishedAt = DateTime.UtcNow,
|
|
Revision = 1
|
|
};
|
|
|
|
Assert.Equal(correlationId, entity.CorrelationId);
|
|
}
|
|
|
|
[Fact]
|
|
public void SellDecision_TracksRevisionOnUpdate()
|
|
{
|
|
var entity = new SellDecisionEntity
|
|
{
|
|
Status = "PENDING",
|
|
OosPerformance = "0.08",
|
|
CreatedBy = "user@example.com",
|
|
CreatedJustification = "test",
|
|
Revision = 1
|
|
};
|
|
entity.Revision++; // Simulate update
|
|
|
|
Assert.Equal(2, entity.Revision);
|
|
}
|
|
|
|
[Fact]
|
|
public void SellDecision_HasPublishedAtTimestamp()
|
|
{
|
|
var now = DateTime.UtcNow;
|
|
var entity = new SellDecisionEntity
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Status = "PENDING",
|
|
OosPerformance = "0.08",
|
|
CreatedBy = "user@example.com",
|
|
CreatedJustification = "test",
|
|
PublishedAt = now
|
|
};
|
|
|
|
Assert.Equal(now, entity.PublishedAt);
|
|
}
|
|
}
|
|
|
|
public class SellDecisionIdempotencyTests
|
|
{
|
|
[Fact]
|
|
public void SellDecision_WithSameCorrelationId_ShouldBeTreatedAsIdempotent()
|
|
{
|
|
var correlationId = Guid.NewGuid();
|
|
var decision1 = new SellDecisionEntity
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Status = "PENDING",
|
|
OosPerformance = "0.08",
|
|
CreatedBy = "user@example.com",
|
|
CreatedJustification = "test",
|
|
CorrelationId = correlationId,
|
|
Revision = 1
|
|
};
|
|
var decision2 = new SellDecisionEntity
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Status = "PENDING",
|
|
OosPerformance = "0.08",
|
|
CreatedBy = "user@example.com",
|
|
CreatedJustification = "test",
|
|
CorrelationId = correlationId,
|
|
Revision = 1
|
|
};
|
|
|
|
// Both have same correlation ID, so duplicate creation should be rejected
|
|
Assert.Equal(decision1.CorrelationId, decision2.CorrelationId);
|
|
}
|
|
}
|
|
|
|
public class SellDecisionContractIntegrationTests
|
|
{
|
|
[Fact]
|
|
public void CreateSellDecisionRequest_ValidatesAllRequiredFields()
|
|
{
|
|
var request = new CreateSellDecisionRequest
|
|
{
|
|
ModelId = Guid.NewGuid(),
|
|
WindowStart = DateTime.UtcNow.AddDays(-90),
|
|
WindowEnd = DateTime.UtcNow,
|
|
ThresholdPbo = 0.65m,
|
|
ThresholdDsr = 0.015m,
|
|
Justification = "Model consensus"
|
|
};
|
|
|
|
Assert.NotEqual(Guid.Empty, request.ModelId);
|
|
Assert.NotEmpty(request.Justification);
|
|
}
|
|
|
|
[Fact]
|
|
public void CreateSellDecisionResponse_ContainsRequiredFields()
|
|
{
|
|
var response = new CreateSellDecisionResponse
|
|
{
|
|
DecisionId = Guid.NewGuid(),
|
|
ModelId = Guid.NewGuid(),
|
|
Status = "PENDING",
|
|
CorrelationId = Guid.NewGuid(),
|
|
CreatedAt = DateTime.UtcNow
|
|
};
|
|
|
|
Assert.NotEqual(Guid.Empty, response.DecisionId);
|
|
Assert.NotEmpty(response.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void ExecuteSellDecisionRequest_ValidatesApprovalLinkage()
|
|
{
|
|
var request = new ExecuteSellDecisionRequest
|
|
{
|
|
ApprovalId = Guid.NewGuid(),
|
|
ExecutionPrice = 150.25m,
|
|
Quantity = 500,
|
|
Justification = "Approved via VS-03"
|
|
};
|
|
|
|
Assert.NotEqual(Guid.Empty, request.ApprovalId);
|
|
Assert.True(request.ExecutionPrice > 0);
|
|
Assert.True(request.Quantity > 0);
|
|
}
|
|
}
|