using System.Text.Json;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Migrations;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Maintenance;
///
/// SQL Server integration tests for the AuditLog Parquet archive (ScadaBridge#42) against the
/// real migrated, partitioned schema: archive-then-purge archives exactly the partition's rows
/// and then drops them; the purge refuses to drop a partition that changed after it was
/// archived; a damaged archive file is caught by verify.
///
///
/// Every test owns a distinct month (partition) of the shared fixture database.
///
public sealed class AuditLogPartitionArchiveTests : IClassFixture, IDisposable
{
private readonly MsSqlMigrationFixture _fixture;
private readonly DirectoryInfo _root = Directory.CreateTempSubdirectory("auditlog-archive-");
/// Creates the test class with the shared migrated SQL Server fixture.
/// The shared migration fixture.
public AuditLogPartitionArchiveTests(MsSqlMigrationFixture fixture) => _fixture = fixture;
///
public void Dispose() => _root.Delete(recursive: true);
///
/// Archive, then purge: the archive holds exactly the partition's stored rows; the switch
/// drops them, the manifest is marked Purged, and the archive verifies.
///
/// A task that represents the asynchronous operation.
[SkippableFact]
public async Task ArchiveThenPurge_ArchivesExactlyThePartition_AndVerifies()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 2);
await InsertAsync(Enumerable.Range(1, 10).Select(i => NewEvent(month.AddDays(i).AddTicks(i * 7_777))));
// Two rows of a short-retention channel, purged per channel before the archive.
await InsertAsync([NewEvent(month.AddHours(1), AuditChannel.DbOutbound), NewEvent(month.AddHours(2), AuditChannel.DbOutbound)]);
await using (var ctx = CreateContext())
{
Assert.Equal(2, await new AuditLogRepository(ctx).PurgeChannelOlderThanAsync("DbOutbound", month.AddDays(1), 100));
}
await InsertAsync([NewEvent(month.AddDays(20))]);
var before = await PartitionRowsAsync(month);
Assert.Equal(11, before.Count);
var manifest = await ArchiveAsync(month);
Assert.Equal(AuditArchiveStatus.Archived, manifest.Status);
Assert.Equal(AuditArchiveParquet.FormatVersion, manifest.FormatVersion);
Assert.Equal(11, manifest.RowCount);
Assert.Equal("2026-02", manifest.Partition);
// Row equality: the archive's data file holds exactly the partition's stored rows.
var archived = await ReadArchiveRowsAsync(manifest);
Assert.Equal(before.Select(Key).Order(), archived.Select(Key).Order());
Assert.False(File.Exists(Path.Combine(_root.FullName, "2026", manifest.Name + AuditArchiveParquet.LegacyTombstoneSuffix)));
await using (var ctx = CreateContext())
{
Assert.Equal(11, await Archive(ctx).PurgeArchivedPartitionAsync(manifest));
}
Assert.Empty(await PartitionRowsAsync(month));
var verification = await VerifyArchiveAsync(manifest.Name);
Assert.True(verification.Verified, verification.FileProblem);
Assert.Equal(AuditArchiveStatus.Purged, verification.ManifestStatus);
Assert.Equal(11, verification.RowsRead);
await using (var ctx = CreateContext())
{
var listed = Assert.Single(await Archive(ctx).ListAsync(), m => m.Partition == "2026-02");
Assert.Equal(AuditArchiveStatus.Purged, listed.Status);
Assert.NotNull(listed.PurgedAtUtc);
}
}
///
/// A row that lands in the partition after it was archived makes the purge refuse
/// (nothing dropped); a fresh archive then purges. The stale archive still verifies as a
/// complete, un-purged copy.
///
/// A task that represents the asynchronous operation.
[SkippableFact]
public async Task RowArrivingAfterTheArchive_StopsThePurge()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 3);
await InsertAsync(Enumerable.Range(1, 5).Select(i => NewEvent(month.AddDays(i))));
var stale = await ArchiveAsync(month);
await InsertAsync([NewEvent(month.AddDays(9))]);
await using (var ctx = CreateContext())
{
var ex = await Assert.ThrowsAsync(() => Archive(ctx).PurgeArchivedPartitionAsync(stale));
Assert.Equal(AuditPartitionSwitchGuard.RowCountChangedError, ((SqlException)ex.InnerException!).Number);
}
Assert.Equal(6, (await PartitionRowsAsync(month)).Count);
var staleVerification = await VerifyArchiveAsync(stale.Name);
Assert.True(staleVerification.Verified);
Assert.Equal(AuditArchiveStatus.Archived, staleVerification.ManifestStatus);
// Positive control: a fresh archive holds the new row and is allowed to purge.
var fresh = await ArchiveAsync(month);
Assert.Equal(6, fresh.RowCount);
await using (var ctx = CreateContext())
{
Assert.Equal(6, await Archive(ctx).PurgeArchivedPartitionAsync(fresh));
}
Assert.Empty(await PartitionRowsAsync(month));
}
///
/// A damaged archive fails verification: a flipped byte (file hash), and a re-written file
/// with one row dropped and the manifest's hash/size updated (row count).
///
/// A task that represents the asynchronous operation.
[SkippableFact]
public async Task DamagedArchive_FailsVerification()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 5);
await InsertAsync(Enumerable.Range(1, 8).Select(i => NewEvent(month.AddDays(i))));
var manifest = await ArchiveAndPurgeAsync(month);
Assert.True((await VerifyArchiveAsync(manifest.Name)).Verified);
var dataPath = Path.Combine(_root.FullName, "2026", manifest.DataFile);
var manifestPath = Path.Combine(_root.FullName, "2026", manifest.Name + ".manifest.json");
var original = await File.ReadAllBytesAsync(dataPath);
var originalManifest = await File.ReadAllTextAsync(manifestPath);
// 1. A flipped byte.
var flipped = (byte[])original.Clone();
flipped[original.Length / 2] ^= 0xFF;
await File.WriteAllBytesAsync(dataPath, flipped);
var result = await VerifyArchiveAsync(manifest.Name);
Assert.False(result.Verified);
Assert.False(result.FilesIntact);
Assert.Contains("SHA-256", result.FileProblem);
// 2. A row dropped from a re-written file, manifest hash/size updated to match.
var rows = await ReadArchiveRowsAsync(manifest, original);
rows.RemoveAt(3);
await RewriteAsync(rows, manifest, dataPath, manifestPath);
result = await VerifyArchiveAsync(manifest.Name);
Assert.False(result.Verified);
Assert.False(result.FilesIntact);
Assert.Contains("7 rows", result.FileProblem);
// Positive control: the untouched archive still verifies.
await File.WriteAllBytesAsync(dataPath, original);
await File.WriteAllTextAsync(manifestPath, originalManifest);
Assert.True((await VerifyArchiveAsync(manifest.Name)).Verified);
}
///
/// Rows that arrive for a month after it was purged are archived and purged again in a
/// second archive; both verify.
///
/// A task that represents the asynchronous operation.
[SkippableFact]
public async Task LateRows_AreArchivedAgain()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 6);
await InsertAsync(Enumerable.Range(1, 3).Select(i => NewEvent(month.AddDays(i))));
var first = await ArchiveAndPurgeAsync(month);
await InsertAsync([NewEvent(month.AddDays(15)), NewEvent(month.AddDays(16))]);
await Task.Delay(5); // distinct archive name (millisecond stamp)
var second = await ArchiveAndPurgeAsync(month);
Assert.Equal(2, second.RowCount);
Assert.Empty(await PartitionRowsAsync(month));
Assert.True((await VerifyArchiveAsync(second.Name)).Verified);
Assert.True((await VerifyArchiveAsync(first.Name)).Verified);
}
///
/// A format-v1 manifest (written while the hash chain existed, with chain and tombstone
/// fields) is still listed and verified.
///
/// A task that represents the asynchronous operation.
[SkippableFact]
public async Task FormatV1Manifest_IsListedAndVerified()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 7);
await InsertAsync(Enumerable.Range(1, 4).Select(i => NewEvent(month.AddDays(i))));
var manifest = await ArchiveAndPurgeAsync(month);
var manifestPath = Path.Combine(_root.FullName, "2026", manifest.Name + ".manifest.json");
var node = System.Text.Json.Nodes.JsonNode.Parse(await File.ReadAllTextAsync(manifestPath))!.AsObject();
node["formatVersion"] = 1;
node["chainedRowCount"] = 4;
node["unchainedRowCount"] = 0;
node["tombstoneFile"] = manifest.Name + AuditArchiveParquet.LegacyTombstoneSuffix;
node["tombstoneSha256"] = new string('0', 64);
node["tombstoneCount"] = 0;
node["chains"] = new System.Text.Json.Nodes.JsonArray(new System.Text.Json.Nodes.JsonObject
{
["partition"] = "2026-07", ["lastSeq"] = 4, ["lastHash"] = new string('a', 64), ["tombstoneCount"] = 0,
});
await File.WriteAllTextAsync(manifestPath, node.ToJsonString());
await using (var ctx = CreateContext())
{
var listed = Assert.Single(await Archive(ctx).ListAsync(), m => m.Partition == "2026-07");
Assert.Equal(1, listed.FormatVersion);
Assert.Equal(4, listed.RowCount);
}
Assert.True((await VerifyArchiveAsync(manifest.Name)).Verified);
}
/// A name that is not an archive name (e.g. a path) is rejected before any file access.
/// A task that represents the asynchronous operation.
[SkippableFact]
public async Task VerifyRejectsPathLikeNames()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
await using var ctx = CreateContext();
await Assert.ThrowsAsync(() => Archive(ctx).VerifyAsync("../../etc/passwd"));
await Assert.ThrowsAsync(() => Archive(ctx).VerifyAsync("auditlog-2026-01-20260101T000000000Z"));
}
// ── helpers ──────────────────────────────────────────────────────────────
private static DateTime Month(int year, int month) => new(year, month, 1, 0, 0, 0, DateTimeKind.Utc);
private AuditLogPartitionArchive Archive(ScadaBridgeDbContext ctx) => new(ctx, new AuditLogArchiveOptions
{
Enabled = true,
Directory = _root.FullName,
RowGroupSize = 1_000,
});
private async Task ArchiveAsync(DateTime month)
{
await using var ctx = CreateContext();
return await Archive(ctx).ArchivePartitionAsync(month);
}
private async Task ArchiveAndPurgeAsync(DateTime month)
{
var manifest = await ArchiveAsync(month);
await using var ctx = CreateContext();
await Archive(ctx).PurgeArchivedPartitionAsync(manifest);
return manifest;
}
private async Task VerifyArchiveAsync(string name)
{
await using var ctx = CreateContext();
return await Archive(ctx).VerifyAsync(name);
}
private async Task> ReadArchiveRowsAsync(AuditArchiveManifest manifest, byte[]? bytes = null)
{
bytes ??= await File.ReadAllBytesAsync(Path.Combine(_root.FullName, manifest.Partition[..4], manifest.DataFile));
var rows = new List();
await foreach (var row in AuditArchiveParquet.ReadRowsAsync(new MemoryStream(bytes)))
{
rows.Add(row);
}
return rows;
}
private static async Task RewriteAsync(
List rows, AuditArchiveManifest manifest, string dataPath, string manifestPath)
{
var stream = new MemoryStream();
await using (var writer = await AuditArchiveParquet.CreateDataWriterAsync(stream, manifest.Partition, 1_000))
{
foreach (var row in rows)
{
await writer.AddAsync(row);
}
}
var bytes = stream.ToArray();
await File.WriteAllBytesAsync(dataPath, bytes);
var forged = manifest with
{
Status = AuditArchiveStatus.Purged,
DataSha256 = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(bytes)),
DataBytes = bytes.Length,
};
await File.WriteAllTextAsync(manifestPath, JsonSerializer.Serialize(forged, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
}));
}
private async Task> PartitionRowsAsync(DateTime month)
{
await using var conn = new SqlConnection(_fixture.ConnectionString);
await conn.OpenAsync();
await using var cmd = conn.CreateCommand();
cmd.CommandText = $"SELECT {AuditArchiveParquet.RowColumns} FROM dbo.AuditLog "
+ "WHERE OccurredAtUtc >= @s AND OccurredAtUtc < @e;";
cmd.Parameters.Add(new SqlParameter("@s", System.Data.SqlDbType.DateTime2) { Value = month });
cmd.Parameters.Add(new SqlParameter("@e", System.Data.SqlDbType.DateTime2) { Value = month.AddMonths(1) });
var rows = new List();
await using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
rows.Add(AuditArchiveParquet.ReadRow(reader));
}
return rows;
}
private static string Key(AuditArchiveRow r) => string.Join('|',
r.EventId, r.OccurredAtUtc.Ticks, r.Actor ?? "", r.Action, r.Outcome, r.Category, r.Target ?? "",
r.SourceNode ?? "", r.CorrelationId?.ToString() ?? "", r.DetailsJson ?? "");
private async Task InsertAsync(IEnumerable events)
{
await using var ctx = CreateContext();
var repo = new AuditLogRepository(ctx);
foreach (var e in events)
{
await repo.InsertIfNotExistsAsync(e);
}
}
private ScadaBridgeDbContext CreateContext()
=> new(new DbContextOptionsBuilder().UseSqlServer(_fixture.ConnectionString).Options);
private static AuditEvent NewEvent(DateTime occurredAtUtc, AuditChannel channel = AuditChannel.ApiOutbound) =>
ScadaBridgeAuditEventFactory.Create(
channel: channel,
kind: channel == AuditChannel.DbOutbound ? AuditKind.DbWrite : AuditKind.ApiCall,
status: AuditStatus.Delivered,
occurredAtUtc: occurredAtUtc,
actor: "archive-test",
target: "erp",
sourceNode: "central-a",
sourceSiteId: "archive-test",
correlationId: Guid.NewGuid());
}