Refactor code structure for improved readability and maintainability; optimize performance in key functions.
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
namespace StellaOps.AuditPack.Tests;
|
||||
|
||||
using StellaOps.AuditPack.Models;
|
||||
using StellaOps.AuditPack.Services;
|
||||
|
||||
[Trait("Category", "Unit")]
|
||||
public class AuditPackBuilderTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Build_FromScanResult_CreatesCompletePack()
|
||||
{
|
||||
// Arrange
|
||||
var scanResult = new ScanResult("scan-123");
|
||||
var builder = new AuditPackBuilder();
|
||||
var options = new AuditPackOptions { Name = "test-pack" };
|
||||
|
||||
// Act
|
||||
var pack = await builder.BuildAsync(scanResult, options);
|
||||
|
||||
// Assert
|
||||
pack.Should().NotBeNull();
|
||||
pack.PackId.Should().NotBeNullOrEmpty();
|
||||
pack.Name.Should().Be("test-pack");
|
||||
pack.RunManifest.Should().NotBeNull();
|
||||
pack.Verdict.Should().NotBeNull();
|
||||
pack.EvidenceIndex.Should().NotBeNull();
|
||||
pack.PackDigest.Should().NotBeNullOrEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Export_CreatesValidArchive()
|
||||
{
|
||||
// Arrange
|
||||
var scanResult = new ScanResult("scan-123");
|
||||
var builder = new AuditPackBuilder();
|
||||
var pack = await builder.BuildAsync(scanResult, new AuditPackOptions());
|
||||
|
||||
var outputPath = Path.Combine(Path.GetTempPath(), $"test-pack-{Guid.NewGuid():N}.tar.gz");
|
||||
var exportOptions = new ExportOptions { Sign = false };
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
await builder.ExportAsync(pack, outputPath, exportOptions);
|
||||
|
||||
// Assert
|
||||
File.Exists(outputPath).Should().BeTrue();
|
||||
var fileInfo = new FileInfo(outputPath);
|
||||
fileInfo.Length.Should().BeGreaterThan(0);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(outputPath))
|
||||
File.Delete(outputPath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PackDigest_IsComputedCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
var pack = new AuditPack
|
||||
{
|
||||
PackId = "test-pack",
|
||||
Name = "Test Pack",
|
||||
CreatedAt = new DateTimeOffset(2025, 1, 1, 0, 0, 0, TimeSpan.Zero),
|
||||
RunManifest = new RunManifest("scan-1", DateTimeOffset.UtcNow),
|
||||
EvidenceIndex = new EvidenceIndex([]),
|
||||
Verdict = new Verdict("verdict-1", "pass"),
|
||||
OfflineBundle = new BundleManifest("bundle-1", "1.0"),
|
||||
Contents = new PackContents()
|
||||
};
|
||||
|
||||
// Act - digest should be set during build
|
||||
pack.PackDigest.Should().NotBeNull();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
namespace StellaOps.AuditPack.Tests;
|
||||
|
||||
using StellaOps.AuditPack.Services;
|
||||
|
||||
[Trait("Category", "Unit")]
|
||||
public class AuditPackImporterTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Import_ValidPack_Succeeds()
|
||||
{
|
||||
// Arrange
|
||||
var archivePath = await CreateTestArchiveAsync();
|
||||
var importer = new AuditPackImporter();
|
||||
var options = new ImportOptions { VerifySignatures = false };
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
var result = await importer.ImportAsync(archivePath, options);
|
||||
|
||||
// Assert
|
||||
result.Success.Should().BeTrue();
|
||||
result.Pack.Should().NotBeNull();
|
||||
result.IntegrityResult?.IsValid.Should().BeTrue();
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(archivePath))
|
||||
File.Delete(archivePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Import_MissingManifest_Fails()
|
||||
{
|
||||
// Arrange
|
||||
var archivePath = Path.Combine(Path.GetTempPath(), "invalid.tar.gz");
|
||||
await CreateEmptyArchiveAsync(archivePath);
|
||||
|
||||
var importer = new AuditPackImporter();
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
var result = await importer.ImportAsync(archivePath, new ImportOptions());
|
||||
|
||||
// Assert
|
||||
result.Success.Should().BeFalse();
|
||||
result.Errors.Should().Contain(e => e.Contains("Manifest"));
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(archivePath))
|
||||
File.Delete(archivePath);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<string> CreateTestArchiveAsync()
|
||||
{
|
||||
// Create a test pack and export it
|
||||
var builder = new AuditPackBuilder();
|
||||
var pack = await builder.BuildAsync(
|
||||
new ScanResult("test-scan"),
|
||||
new AuditPackOptions());
|
||||
|
||||
var archivePath = Path.Combine(Path.GetTempPath(), $"test-{Guid.NewGuid():N}.tar.gz");
|
||||
await builder.ExportAsync(pack, archivePath, new ExportOptions { Sign = false });
|
||||
|
||||
return archivePath;
|
||||
}
|
||||
|
||||
private static async Task CreateEmptyArchiveAsync(string path)
|
||||
{
|
||||
// Create an empty tar.gz
|
||||
using var fs = File.Create(path);
|
||||
using var gz = new System.IO.Compression.GZipStream(fs, System.IO.Compression.CompressionLevel.Fastest);
|
||||
await gz.WriteAsync(new byte[] { 0 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
namespace StellaOps.AuditPack.Tests;
|
||||
|
||||
using StellaOps.AuditPack.Models;
|
||||
using StellaOps.AuditPack.Services;
|
||||
|
||||
[Trait("Category", "Unit")]
|
||||
public class AuditPackReplayerTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Replay_ValidPack_ProducesResult()
|
||||
{
|
||||
// Arrange
|
||||
var pack = CreateTestPack();
|
||||
var importResult = new ImportResult
|
||||
{
|
||||
Success = true,
|
||||
Pack = pack,
|
||||
ExtractDirectory = Path.GetTempPath()
|
||||
};
|
||||
|
||||
var replayer = new AuditPackReplayer();
|
||||
|
||||
// Act
|
||||
var result = await replayer.ReplayAsync(importResult);
|
||||
|
||||
// Assert
|
||||
result.Success.Should().BeTrue();
|
||||
result.OriginalVerdictDigest.Should().NotBeNullOrEmpty();
|
||||
result.ReplayedVerdictDigest.Should().NotBeNullOrEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Replay_InvalidImport_Fails()
|
||||
{
|
||||
// Arrange
|
||||
var importResult = new ImportResult { Success = false };
|
||||
var replayer = new AuditPackReplayer();
|
||||
|
||||
// Act
|
||||
var result = await replayer.ReplayAsync(importResult);
|
||||
|
||||
// Assert
|
||||
result.Success.Should().BeFalse();
|
||||
result.Error.Should().Contain("Invalid import");
|
||||
}
|
||||
|
||||
private static AuditPack CreateTestPack()
|
||||
{
|
||||
return new AuditPack
|
||||
{
|
||||
PackId = "test-pack",
|
||||
Name = "Test Pack",
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
RunManifest = new RunManifest("scan-1", DateTimeOffset.UtcNow),
|
||||
EvidenceIndex = new EvidenceIndex([]),
|
||||
Verdict = new Verdict("verdict-1", "pass"),
|
||||
OfflineBundle = new BundleManifest("bundle-1", "1.0"),
|
||||
Contents = new PackContents()
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
<LangVersion>preview</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
|
||||
<PackageReference Include="xunit" Version="2.6.6" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\src\__Libraries\StellaOps.AuditPack\StellaOps.AuditPack.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
<Using Include="FluentAssertions" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user