| | | 1 | | using AsiBackbone.Core.Audit; |
| | | 2 | | using AsiBackbone.Core.Constraints; |
| | | 3 | | using AsiBackbone.Core.Decisions; |
| | | 4 | | using AsiBackbone.Core.Results; |
| | | 5 | | |
| | | 6 | | namespace AsiBackbone.Testing.Contracts; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides reusable safe-collapse assertions for governance decisions and audit residue. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class AsiBackboneDecisionContract |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Verifies that a governance decision is present and carries the minimum shape required for safe downstream handli |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="decision">The decision returned by an implementation under test.</param> |
| | | 17 | | /// <param name="contractName">The human-readable contract name used in failure messages.</param> |
| | | 18 | | /// <returns>The verified decision.</returns> |
| | | 19 | | public static GovernanceDecision VerifySafeDecision( |
| | | 20 | | GovernanceDecision? decision, |
| | | 21 | | string contractName = "Governance decision") |
| | | 22 | | { |
| | 54 | 23 | | if (decision is null) |
| | | 24 | | { |
| | 4 | 25 | | throw new AsiBackboneContractViolationException($"{contractName} must return a decision and must never retur |
| | | 26 | | } |
| | | 27 | | |
| | 50 | 28 | | VerifySupportedOutcome(decision, contractName); |
| | | 29 | | |
| | 48 | 30 | | if (RequiresReasonCodes(decision) && decision.Reasons.Count == 0) |
| | | 31 | | { |
| | 10 | 32 | | throw new AsiBackboneContractViolationException($"{contractName} outcome '{decision.Outcome}' must include a |
| | | 33 | | } |
| | | 34 | | |
| | 108 | 35 | | for (int index = 0; index < decision.Reasons.Count; index++) |
| | | 36 | | { |
| | 22 | 37 | | OperationReason reason = decision.Reasons[index] ?? throw new AsiBackboneContractViolationException($"{contr |
| | | 38 | | |
| | 20 | 39 | | if (string.IsNullOrWhiteSpace(reason.Code)) |
| | | 40 | | { |
| | 2 | 41 | | throw new AsiBackboneContractViolationException($"{contractName} contains a reason with an empty code at |
| | | 42 | | } |
| | | 43 | | |
| | 18 | 44 | | if (string.IsNullOrWhiteSpace(reason.Message)) |
| | | 45 | | { |
| | 2 | 46 | | throw new AsiBackboneContractViolationException($"{contractName} contains a reason with an empty message |
| | | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | 32 | 50 | | return decision; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Verifies decision shape, context correlation propagation, and policy telemetry presence when supplied. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <typeparam name="TContext">The framework-neutral evaluation context type.</typeparam> |
| | | 57 | | /// <param name="decision">The decision returned by an implementation under test.</param> |
| | | 58 | | /// <param name="context">The context supplied to the implementation under test.</param> |
| | | 59 | | /// <param name="contractName">The human-readable contract name used in failure messages.</param> |
| | | 60 | | /// <returns>The verified decision.</returns> |
| | | 61 | | public static GovernanceDecision VerifyTelemetryFromContext<TContext>( |
| | | 62 | | GovernanceDecision? decision, |
| | | 63 | | TContext context, |
| | | 64 | | string contractName = "Governance decision") |
| | | 65 | | where TContext : IAsiBackboneConstraintEvaluationContext |
| | | 66 | | { |
| | 16 | 67 | | ArgumentNullException.ThrowIfNull(context); |
| | | 68 | | |
| | 14 | 69 | | GovernanceDecision verifiedDecision = VerifySafeDecision(decision, contractName); |
| | 12 | 70 | | VerifyCorrelationTelemetryValue(context.CorrelationId, verifiedDecision.CorrelationId, contractName); |
| | 10 | 71 | | VerifyTelemetryPresence(context.PolicyVersion, verifiedDecision.PolicyVersion, "policy version", contractName); |
| | 8 | 72 | | VerifyTelemetryPresence(context.PolicyHash, verifiedDecision.PolicyHash, "policy hash", contractName); |
| | 6 | 73 | | return verifiedDecision; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Verifies that a known invalid capability-grant scenario does not produce an allow decision. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="decision">The decision returned by the capability validator for a known invalid scenario.</param> |
| | | 80 | | /// <param name="contractName">The human-readable contract name used in failure messages.</param> |
| | | 81 | | /// <returns>The verified decision.</returns> |
| | | 82 | | public static GovernanceDecision VerifyInvalidCapabilityGrantDoesNotAllow( |
| | | 83 | | GovernanceDecision? decision, |
| | | 84 | | string contractName = "Invalid capability grant") |
| | | 85 | | { |
| | 8 | 86 | | GovernanceDecision verifiedDecision = VerifySafeDecision(decision, contractName); |
| | | 87 | | |
| | 8 | 88 | | return verifiedDecision.IsAllowed |
| | 8 | 89 | | ? throw new AsiBackboneContractViolationException($"{contractName} must not return Allow for an invalid or u |
| | 8 | 90 | | : verifiedDecision; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Verifies that audit residue contains the minimum identity, operation, outcome, and policy telemetry shape. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <param name="residue">The audit residue to verify.</param> |
| | | 97 | | /// <param name="contractName">The human-readable contract name used in failure messages.</param> |
| | | 98 | | /// <returns>The verified audit residue.</returns> |
| | | 99 | | public static IAsiBackboneAuditResidue VerifyAuditResidue( |
| | | 100 | | IAsiBackboneAuditResidue? residue, |
| | | 101 | | string contractName = "Audit residue") |
| | | 102 | | { |
| | 28 | 103 | | if (residue is null) |
| | | 104 | | { |
| | 2 | 105 | | throw new AsiBackboneContractViolationException($"{contractName} must not be null."); |
| | | 106 | | } |
| | | 107 | | |
| | 26 | 108 | | VerifyRequiredString(residue.EventId, "event ID", contractName); |
| | 22 | 109 | | VerifyRequiredString(residue.ActorId, "actor ID", contractName); |
| | 20 | 110 | | VerifyRequiredString(residue.OperationName, "operation name", contractName); |
| | 18 | 111 | | VerifyRequiredString(residue.Outcome, "outcome", contractName); |
| | | 112 | | |
| | 16 | 113 | | if (residue.ReasonCodes is null) |
| | | 114 | | { |
| | 2 | 115 | | throw new AsiBackboneContractViolationException($"{contractName} reason-code collection must not be null."); |
| | | 116 | | } |
| | | 117 | | |
| | 32 | 118 | | for (int index = 0; index < residue.ReasonCodes.Count; index++) |
| | | 119 | | { |
| | 4 | 120 | | if (string.IsNullOrWhiteSpace(residue.ReasonCodes[index])) |
| | | 121 | | { |
| | 2 | 122 | | throw new AsiBackboneContractViolationException($"{contractName} contains an empty reason code at index |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | |
| | 12 | 126 | | return residue.Metadata is null |
| | 12 | 127 | | ? throw new AsiBackboneContractViolationException($"{contractName} metadata collection must not be null.") |
| | 12 | 128 | | : residue; |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | private static void VerifySupportedOutcome(GovernanceDecision decision, string contractName) |
| | | 132 | | { |
| | 50 | 133 | | if (decision.Outcome is not GovernanceDecisionOutcome.Allowed |
| | 50 | 134 | | and not GovernanceDecisionOutcome.Warning |
| | 50 | 135 | | and not GovernanceDecisionOutcome.Denied |
| | 50 | 136 | | and not GovernanceDecisionOutcome.Deferred |
| | 50 | 137 | | and not GovernanceDecisionOutcome.AcknowledgmentRequired |
| | 50 | 138 | | and not GovernanceDecisionOutcome.EscalationRecommended) |
| | | 139 | | { |
| | 2 | 140 | | throw new AsiBackboneContractViolationException($"{contractName} returned unsupported outcome '{decision.Out |
| | | 141 | | } |
| | 48 | 142 | | } |
| | | 143 | | |
| | | 144 | | private static bool RequiresReasonCodes(GovernanceDecision decision) |
| | | 145 | | { |
| | 48 | 146 | | return decision.Outcome is GovernanceDecisionOutcome.Warning |
| | 48 | 147 | | or GovernanceDecisionOutcome.Denied |
| | 48 | 148 | | or GovernanceDecisionOutcome.Deferred |
| | 48 | 149 | | or GovernanceDecisionOutcome.AcknowledgmentRequired |
| | 48 | 150 | | or GovernanceDecisionOutcome.EscalationRecommended; |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | private static void VerifyCorrelationTelemetryValue( |
| | | 154 | | string? expected, |
| | | 155 | | string? actual, |
| | | 156 | | string contractName) |
| | | 157 | | { |
| | 12 | 158 | | if (!string.IsNullOrWhiteSpace(expected) && !string.Equals(expected, actual, StringComparison.Ordinal)) |
| | | 159 | | { |
| | 2 | 160 | | throw new AsiBackboneContractViolationException($"{contractName} must preserve the supplied correlation ID w |
| | | 161 | | } |
| | 10 | 162 | | } |
| | | 163 | | |
| | | 164 | | private static void VerifyTelemetryPresence( |
| | | 165 | | string? expected, |
| | | 166 | | string? actual, |
| | | 167 | | string telemetryName, |
| | | 168 | | string contractName) |
| | | 169 | | { |
| | 18 | 170 | | if (!string.IsNullOrWhiteSpace(expected) && string.IsNullOrWhiteSpace(actual)) |
| | | 171 | | { |
| | 4 | 172 | | throw new AsiBackboneContractViolationException($"{contractName} must include a {telemetryName} when one is |
| | | 173 | | } |
| | 14 | 174 | | } |
| | | 175 | | |
| | | 176 | | private static void VerifyRequiredString(string? value, string name, string contractName) |
| | | 177 | | { |
| | 86 | 178 | | if (string.IsNullOrWhiteSpace(value)) |
| | | 179 | | { |
| | 10 | 180 | | throw new AsiBackboneContractViolationException($"{contractName} must include a non-empty {name}."); |
| | | 181 | | } |
| | 76 | 182 | | } |
| | | 183 | | } |