[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,180 @@
using SolutionErp.Domain.Contracts; // WorkflowApproverKind reuse
using SolutionErp.Domain.Identity;
using SolutionErp.Domain.PurchaseEvaluations;
namespace SolutionErp.Domain.Tests.PurchaseEvaluations;
// Tests cho PurchaseEvaluationPolicy — 2 quy trình A/B chính.
// Mục tiêu: chống regression khi PEPhase thêm phase mới hoặc role mapping bị edit.
public class PurchaseEvaluationPolicyTests
{
// ===== A — DuyetNcc (NccOnly): 3-step (Drafter → Purchasing → CCM → CEO) =====
[Fact]
public void NccOnly_Drafter_DangSoanThao_To_ChoPurchasing_Allowed()
{
PurchaseEvaluationPolicies.NccOnly
.IsTransitionAllowed(PurchaseEvaluationPhase.DangSoanThao, PurchaseEvaluationPhase.ChoPurchasing,
[AppRoles.Drafter])
.Should().BeTrue();
}
[Fact]
public void NccOnly_Procurement_ChoPurchasing_To_ChoCCM_Allowed()
{
PurchaseEvaluationPolicies.NccOnly
.IsTransitionAllowed(PurchaseEvaluationPhase.ChoPurchasing, PurchaseEvaluationPhase.ChoCCM,
[AppRoles.Procurement])
.Should().BeTrue();
}
[Fact]
public void NccOnly_CostControl_ChoCCM_GoesDirectlyTo_ChoCEODuyetNCC()
{
// A skip ChoDuAn + ChoCEODuyetPA — CCM đẩy thẳng CEO duyệt NCC
PurchaseEvaluationPolicies.NccOnly
.IsTransitionAllowed(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.ChoCEODuyetNCC,
[AppRoles.CostControl])
.Should().BeTrue();
}
[Fact]
public void NccOnly_DoesNotHave_ChoDuAn_Phase()
{
PurchaseEvaluationPolicies.NccOnly.HasPhase(PurchaseEvaluationPhase.ChoDuAn)
.Should().BeFalse("Quy trình A chỉ áp dụng khi không cần Dự án duyệt");
}
[Fact]
public void NccOnly_DoesNotHave_ChoCEODuyetPA_Phase()
{
PurchaseEvaluationPolicies.NccOnly.HasPhase(PurchaseEvaluationPhase.ChoCEODuyetPA)
.Should().BeFalse();
}
[Fact]
public void NccOnly_Director_FinalApproval_To_DaDuyet()
{
PurchaseEvaluationPolicies.NccOnly
.IsTransitionAllowed(PurchaseEvaluationPhase.ChoCEODuyetNCC, PurchaseEvaluationPhase.DaDuyet,
[AppRoles.Director])
.Should().BeTrue();
PurchaseEvaluationPolicies.NccOnly
.IsTransitionAllowed(PurchaseEvaluationPhase.ChoCEODuyetNCC, PurchaseEvaluationPhase.DaDuyet,
[AppRoles.AuthorizedSigner])
.Should().BeTrue();
}
// ===== B — DuyetNccPhuongAn (NccWithPlan): 5-step =====
[Fact]
public void NccWithPlan_Has_ChoDuAn_AndChoCEODuyetPA_Phases()
{
PurchaseEvaluationPolicies.NccWithPlan.HasPhase(PurchaseEvaluationPhase.ChoDuAn)
.Should().BeTrue();
PurchaseEvaluationPolicies.NccWithPlan.HasPhase(PurchaseEvaluationPhase.ChoCEODuyetPA)
.Should().BeTrue();
}
[Fact]
public void NccWithPlan_Procurement_ChoPurchasing_GoesTo_ChoDuAn_NotChoCCM()
{
// Khác với A — B đi qua Dự án trước CCM
PurchaseEvaluationPolicies.NccWithPlan
.IsTransitionAllowed(PurchaseEvaluationPhase.ChoPurchasing, PurchaseEvaluationPhase.ChoDuAn,
[AppRoles.Procurement])
.Should().BeTrue();
PurchaseEvaluationPolicies.NccWithPlan
.IsTransitionAllowed(PurchaseEvaluationPhase.ChoPurchasing, PurchaseEvaluationPhase.ChoCCM,
[AppRoles.Procurement])
.Should().BeFalse();
}
[Fact]
public void NccWithPlan_ProjectManager_ChoDuAn_To_ChoCCM_Allowed()
{
PurchaseEvaluationPolicies.NccWithPlan
.IsTransitionAllowed(PurchaseEvaluationPhase.ChoDuAn, PurchaseEvaluationPhase.ChoCCM,
[AppRoles.ProjectManager])
.Should().BeTrue();
}
[Fact]
public void NccWithPlan_CostControl_ChoCCM_GoesTo_ChoCEODuyetPA_NotDuyetNCC()
{
// B: CCM xong đi qua CEO duyệt PA trước, sau mới CEO duyệt NCC
PurchaseEvaluationPolicies.NccWithPlan
.IsTransitionAllowed(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.ChoCEODuyetPA,
[AppRoles.CostControl])
.Should().BeTrue();
PurchaseEvaluationPolicies.NccWithPlan
.IsTransitionAllowed(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.ChoCEODuyetNCC,
[AppRoles.CostControl])
.Should().BeFalse("CCM trong quy trình B không được skip CEO duyệt PA");
}
[Fact]
public void NccWithPlan_Director_ChoCEODuyetPA_To_ChoCEODuyetNCC()
{
PurchaseEvaluationPolicies.NccWithPlan
.IsTransitionAllowed(PurchaseEvaluationPhase.ChoCEODuyetPA, PurchaseEvaluationPhase.ChoCEODuyetNCC,
[AppRoles.Director])
.Should().BeTrue();
}
// ===== Reject paths chung cả 2 quy trình =====
[Theory]
[InlineData(nameof(PurchaseEvaluationPolicies.NccOnly))]
[InlineData(nameof(PurchaseEvaluationPolicies.NccWithPlan))]
public void BothPolicies_RejectFromCCM_GoesBackTo_DangSoanThao(string policyName)
{
var policy = policyName == nameof(PurchaseEvaluationPolicies.NccOnly)
? PurchaseEvaluationPolicies.NccOnly
: PurchaseEvaluationPolicies.NccWithPlan;
policy.IsTransitionAllowed(PurchaseEvaluationPhase.ChoCCM, PurchaseEvaluationPhase.DangSoanThao,
[AppRoles.CostControl])
.Should().BeTrue();
}
[Theory]
[InlineData(nameof(PurchaseEvaluationPolicies.NccOnly))]
[InlineData(nameof(PurchaseEvaluationPolicies.NccWithPlan))]
public void BothPolicies_DangSoanThao_To_TuChoi_DrafterOrDeptManager(string policyName)
{
var policy = policyName == nameof(PurchaseEvaluationPolicies.NccOnly)
? PurchaseEvaluationPolicies.NccOnly
: PurchaseEvaluationPolicies.NccWithPlan;
policy.IsTransitionAllowed(PurchaseEvaluationPhase.DangSoanThao, PurchaseEvaluationPhase.TuChoi,
[AppRoles.Drafter])
.Should().BeTrue();
policy.IsTransitionAllowed(PurchaseEvaluationPhase.DangSoanThao, PurchaseEvaluationPhase.TuChoi,
[AppRoles.Procurement])
.Should().BeFalse();
}
// ===== Registry mapping per PEType =====
[Theory]
[InlineData(PurchaseEvaluationType.DuyetNcc, "NccOnly")]
[InlineData(PurchaseEvaluationType.DuyetNccPhuongAn, "NccWithPlan")]
public void Registry_DefaultPolicyName_MapsCorrectly(
PurchaseEvaluationType type, string expectedName)
{
PurchaseEvaluationPolicyRegistry.DefaultPolicyNameFor(type)
.Should().Be(expectedName);
}
[Fact]
public void Registry_For_TypeA_ReturnsNccOnlyPolicy()
{
PurchaseEvaluationPolicyRegistry.For(PurchaseEvaluationType.DuyetNcc).Name
.Should().Be("NccOnly");
}
}