[CLAUDE] Tests Phase 1: Domain unit tests + CI gate (xUnit + FluentAssertions)
All checks were successful
Deploy SOLUTION_ERP / build-deploy (push) Successful in 3m25s

Phase 1 (MVP) — chống regression Workflow state machine. 54 test pure
function (no DB / IO), all pass < 7 giây.

Test project:
- tests/SolutionErp.Domain.Tests/ (xUnit 2.9.3 + FluentAssertions 7.2 — pin trước v8 commercial license)
- ProjectReference SolutionErp.Domain
- Added vào SolutionErp.slnx folder /tests/

Test files:
- Contracts/WorkflowPolicyTests.cs (~17 test):
  - Standard policy 9-phase: role transitions, CCM check, BOD signing, terminal
  - SkipCcm policy 7-phase: bypass CCM verify, no DangKiemTraCCM transition
  - Registry: DefaultPolicyName per ContractType (7 type), bypass flag override
  - FromDefinition versioned: build từ ordered steps + reject path + TuChoi auto-add + UserKindApprover populate UserTransitions
- PurchaseEvaluations/PurchaseEvaluationPolicyTests.cs (~17 test):
  - NccOnly (A) 3-step: skip ChoDuAn + ChoCEODuyetPA, CCM đẩy thẳng CEO duyệt NCC
  - NccWithPlan (B) 5-step: có ChoDuAn (PM) + ChoCEODuyetPA (Director) trước
  - Reject path cả 2 quy trình về DangSoanThao
  - Registry mapping per PEType
- Budgets/BudgetPolicyTests.cs (~13 test):
  - Default 3-step (Drafter→CCM→CEO) role guard
  - Reject paths về DangSoanThao
  - DaDuyet + TuChoi terminal (no NextPhases)
  - SLA spec 5d/3d/2d cho 3 phase đầu

CI gate (.gitea/workflows/deploy.yml):
- Step "Run unit tests (Domain)" thêm TRƯỚC build/publish/deploy
- Test fail (LASTEXITCODE != 0) → exit → KHÔNG deploy
- TRX log saved + upload artifact (continue-on-error nếu Gitea runner thiếu actions/upload-artifact)

Verify local:
- dotnet test tests/SolutionErp.Domain.Tests → Total tests: 54 / Passed: 54 / 6.4s
- dotnet build SolutionErp.slnx (full solution incl. test project) → 0 error

Phase 2-5 pending (xem plan ở chat):
- Code generator atomic concurrency tests (Infra)
- DbInitializer reconcile drift tests (Infra)
- Application handler smoke tests (CQRS) với EF InMemory
- API smoke tests qua WebApplicationFactory
- FE Vitest cho lib utility (queryMatches, fmtMoney)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
pqhuy1987
2026-04-29 13:19:15 +07:00
parent 5d94bb449a
commit d3f9346840
6 changed files with 596 additions and 0 deletions

View File

@ -0,0 +1,134 @@
using SolutionErp.Domain.Budgets;
using SolutionErp.Domain.Identity;
namespace SolutionErp.Domain.Tests.Budgets;
// Tests cho BudgetPolicy (hardcoded simple 3-step Default).
// Chống regression khi BudgetPhase enum thêm phase hoặc role mapping bị edit.
public class BudgetPolicyTests
{
[Fact]
public void Default_Drafter_DangSoanThao_To_ChoCCM_Allowed()
{
BudgetPolicies.Default
.IsTransitionAllowed(BudgetPhase.DangSoanThao, BudgetPhase.ChoCCM,
[AppRoles.Drafter])
.Should().BeTrue();
}
[Fact]
public void Default_DeptManager_DangSoanThao_To_ChoCCM_Allowed()
{
BudgetPolicies.Default
.IsTransitionAllowed(BudgetPhase.DangSoanThao, BudgetPhase.ChoCCM,
[AppRoles.DeptManager])
.Should().BeTrue();
}
[Fact]
public void Default_RandomRole_DangSoanThao_To_ChoCCM_Denied()
{
BudgetPolicies.Default
.IsTransitionAllowed(BudgetPhase.DangSoanThao, BudgetPhase.ChoCCM,
[AppRoles.Procurement])
.Should().BeFalse();
}
[Fact]
public void Default_CostControl_ChoCCM_To_ChoCEO_Allowed()
{
BudgetPolicies.Default
.IsTransitionAllowed(BudgetPhase.ChoCCM, BudgetPhase.ChoCEO,
[AppRoles.CostControl])
.Should().BeTrue();
}
[Fact]
public void Default_CostControl_ChoCCM_To_DangSoanThao_Allowed()
{
// Trả về Drafter
BudgetPolicies.Default
.IsTransitionAllowed(BudgetPhase.ChoCCM, BudgetPhase.DangSoanThao,
[AppRoles.CostControl])
.Should().BeTrue();
}
[Fact]
public void Default_Director_ChoCEO_To_DaDuyet_Allowed()
{
BudgetPolicies.Default
.IsTransitionAllowed(BudgetPhase.ChoCEO, BudgetPhase.DaDuyet,
[AppRoles.Director])
.Should().BeTrue();
BudgetPolicies.Default
.IsTransitionAllowed(BudgetPhase.ChoCEO, BudgetPhase.DaDuyet,
[AppRoles.AuthorizedSigner])
.Should().BeTrue();
}
[Fact]
public void Default_CCM_Cannot_Approve_To_DaDuyet()
{
// CCM chỉ chuyển đến ChoCEO, không tự duyệt thành DaDuyet
BudgetPolicies.Default
.IsTransitionAllowed(BudgetPhase.ChoCEO, BudgetPhase.DaDuyet,
[AppRoles.CostControl])
.Should().BeFalse();
}
[Fact]
public void Default_DaDuyet_NoFurtherTransitions()
{
BudgetPolicies.Default.NextPhasesFrom(BudgetPhase.DaDuyet)
.Should().BeEmpty("DaDuyet là terminal");
}
[Fact]
public void Default_TuChoi_NoFurtherTransitions()
{
BudgetPolicies.Default.NextPhasesFrom(BudgetPhase.TuChoi)
.Should().BeEmpty("TuChoi là terminal");
}
[Fact]
public void Default_ActivePhases_Includes_All5States()
{
BudgetPolicies.Default.ActivePhases.Should().BeEquivalentTo(new[]
{
BudgetPhase.DangSoanThao, BudgetPhase.ChoCCM,
BudgetPhase.ChoCEO, BudgetPhase.DaDuyet, BudgetPhase.TuChoi,
});
}
[Fact]
public void Default_NextPhasesFrom_DangSoanThao_Includes_ChoCCM_And_TuChoi()
{
var next = BudgetPolicies.Default.NextPhasesFrom(BudgetPhase.DangSoanThao);
next.Should().Contain(BudgetPhase.ChoCCM);
next.Should().Contain(BudgetPhase.TuChoi);
}
[Fact]
public void Default_NextPhasesFrom_ChoCEO_Includes_DaDuyet_And_DangSoanThao()
{
var next = BudgetPolicies.Default.NextPhasesFrom(BudgetPhase.ChoCEO);
next.Should().Contain(BudgetPhase.DaDuyet);
next.Should().Contain(BudgetPhase.DangSoanThao);
}
// SLA — chống regression khi đổi phase deadline accidentally
[Fact]
public void Default_SlaDeadlines_Match_Spec()
{
BudgetPolicies.Default.PhaseSla[BudgetPhase.DangSoanThao]
.Should().Be(TimeSpan.FromDays(5));
BudgetPolicies.Default.PhaseSla[BudgetPhase.ChoCCM]
.Should().Be(TimeSpan.FromDays(3));
BudgetPolicies.Default.PhaseSla[BudgetPhase.ChoCEO]
.Should().Be(TimeSpan.FromDays(2));
BudgetPolicies.Default.PhaseSla[BudgetPhase.DaDuyet]
.Should().BeNull("Terminal phase không có SLA");
}
}