|
| 1 | +using ModularityKit.Mutator.Abstractions.Context; |
| 2 | +using ModularityKit.Mutator.Abstractions.Intent; |
| 3 | +using ModularityKit.Mutator.Abstractions.Policies; |
| 4 | +using ModularityKit.Mutator.Governance.Abstractions.Lifecycle.Model; |
| 5 | +using ModularityKit.Mutator.Governance.Abstractions.Requests.Factory; |
| 6 | +using ModularityKit.Mutator.Governance.Redis.Serialization; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace ModularityKit.Mutator.Governance.Redis.Tests.Serialization; |
| 10 | + |
| 11 | +public sealed class RedisMutationRequestSerializerTests |
| 12 | +{ |
| 13 | + [Fact] |
| 14 | + public void Roundtrip_preserves_request_shape_needed_by_governance_runtime() |
| 15 | + { |
| 16 | + var request = MutationRequestFactory.PendingApproval( |
| 17 | + stateId: "tenant-42:roles", |
| 18 | + stateType: "IamRoleState", |
| 19 | + mutationType: "GrantRoleMutation", |
| 20 | + intent: new MutationIntent |
| 21 | + { |
| 22 | + OperationName = "GrantRole", |
| 23 | + Category = "Security", |
| 24 | + Description = "Grant elevated access", |
| 25 | + Tags = new HashSet<string> { "security", "urgent" }, |
| 26 | + EstimatedBlastRadius = BlastRadius.Module, |
| 27 | + Metadata = new Dictionary<string, object> |
| 28 | + { |
| 29 | + ["risk-owner"] = "platform" |
| 30 | + } |
| 31 | + }, |
| 32 | + context: MutationContext.User("requester-1", "Requester One", "Need emergency access") with |
| 33 | + { |
| 34 | + StateId = "tenant-42:roles", |
| 35 | + Metadata = new Dictionary<string, object> |
| 36 | + { |
| 37 | + ["source"] = "tests" |
| 38 | + } |
| 39 | + }, |
| 40 | + requirements: |
| 41 | + [ |
| 42 | + new PolicyRequirement |
| 43 | + { |
| 44 | + Type = "Approval", |
| 45 | + Description = "Requires security approval", |
| 46 | + Data = new Dictionary<string, object> |
| 47 | + { |
| 48 | + ["Approver"] = "security-lead", |
| 49 | + ["Reason"] = "Elevated role", |
| 50 | + ["StepOrder"] = 1L, |
| 51 | + ["RequiredApprovals"] = 1L |
| 52 | + } |
| 53 | + } |
| 54 | + ], |
| 55 | + expectedStateVersion: "v10", |
| 56 | + metadata: new Dictionary<string, object> |
| 57 | + { |
| 58 | + ["team"] = "security", |
| 59 | + ["priority"] = "high" |
| 60 | + }) |
| 61 | + with |
| 62 | + { |
| 63 | + CreatedAt = new DateTimeOffset(2026, 6, 25, 9, 0, 0, TimeSpan.Zero), |
| 64 | + UpdatedAt = new DateTimeOffset(2026, 6, 25, 9, 5, 0, TimeSpan.Zero) |
| 65 | + }; |
| 66 | + |
| 67 | + var json = RedisMutationRequestSerializer.Serialize(request); |
| 68 | + var roundtrip = RedisMutationRequestSerializer.Deserialize(json); |
| 69 | + |
| 70 | + Assert.Equal(request.RequestId, roundtrip.RequestId); |
| 71 | + Assert.Equal(request.Status, roundtrip.Status); |
| 72 | + Assert.Equal(request.PendingReason, roundtrip.PendingReason); |
| 73 | + Assert.Equal(request.Intent.Category, roundtrip.Intent.Category); |
| 74 | + Assert.Contains("security", roundtrip.Intent.Tags); |
| 75 | + Assert.Equal(BlastRadiusScope.Module, roundtrip.Intent.EstimatedBlastRadius?.Scope); |
| 76 | + Assert.Equal("security", roundtrip.Metadata["team"]); |
| 77 | + Assert.Single(roundtrip.Requirements); |
| 78 | + Assert.Single(roundtrip.ApprovalRequirements); |
| 79 | + Assert.Equal("security-lead", roundtrip.ApprovalRequirements[0].ApproverId); |
| 80 | + Assert.Equal(3, roundtrip.Decisions.Count); |
| 81 | + } |
| 82 | +} |
0 commit comments