using System.Collections.Immutable; using StellaOps.Policy; using Xunit; namespace StellaOps.Policy.Tests; public class SplMigrationToolTests { [Fact] public void ToSplPolicyJson_ConvertsRulesAndMetadata() { var rule = PolicyRule.Create( name: "Block CVE", action: new PolicyAction(PolicyActionType.Block, null, null, null, false), severities: ImmutableArray.Create(PolicySeverity.Critical), environments: ImmutableArray.Empty, sources: ImmutableArray.Empty, vendors: ImmutableArray.Empty, licenses: ImmutableArray.Empty, tags: ImmutableArray.Empty, match: PolicyRuleMatchCriteria.Create( ImmutableArray.Empty, ImmutableArray.Empty, ImmutableArray.Empty, ImmutableArray.Empty, ImmutableArray.Empty, ImmutableArray.Create("/app"), ImmutableArray.Empty, ImmutableArray.Empty), expires: null, justification: "block it", identifier: "RULE-1"); var document = new PolicyDocument( PolicySchema.CurrentVersion, ImmutableArray.Create(rule), ImmutableDictionary.Empty.Add("name", "demo"), PolicyExceptionConfiguration.Empty); var spl = SplMigrationTool.ToSplPolicyJson(document); const string expected = "{\"apiVersion\":\"spl.stellaops/v1\",\"kind\":\"Policy\",\"metadata\":{\"labels\":{\"name\":\"demo\"},\"name\":\"demo\"},\"spec\":{\"defaultEffect\":\"deny\",\"statements\":[{\"effect\":\"deny\",\"id\":\"RULE-1\",\"match\":{\"actions\":[\"access\"],\"resource\":\"/app\"}}]}}"; Assert.Equal(expected, spl); } [Fact] public void ToSplPolicyJson_UsesOverlaySafeIdsAndAudits() { var rule = PolicyRule.Create( name: "Warn entrypoint", action: new PolicyAction(PolicyActionType.Warn, null, null, null, true), severities: ImmutableArray.Create(PolicySeverity.Low), environments: ImmutableArray.Empty, sources: ImmutableArray.Empty, vendors: ImmutableArray.Empty, licenses: ImmutableArray.Empty, tags: ImmutableArray.Empty, match: PolicyRuleMatchCriteria.Empty, expires: null, justification: "soft warning"); var document = new PolicyDocument( PolicySchema.CurrentVersion, ImmutableArray.Create(rule), ImmutableDictionary.Empty, PolicyExceptionConfiguration.Empty); var spl = SplMigrationTool.ToSplPolicyJson(document); const string expectedId = "warn-entrypoint"; Assert.Contains(expectedId, spl); Assert.Contains("\"audit\":{\"message\":\"soft warning\",\"severity\":\"warn\"}", spl); } }