using FluentAssertions; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Npgsql; using StellaOps.Policy.Storage.Postgres; using Xunit; using StellaOps.TestKit; namespace StellaOps.Policy.Storage.Postgres.Tests; [Collection(PolicyPostgresCollection.Name)] public sealed class RecheckEvidenceMigrationTests : IAsyncLifetime { private readonly PolicyPostgresFixture _fixture; private readonly PolicyDataSource _dataSource; public RecheckEvidenceMigrationTests(PolicyPostgresFixture fixture) { _fixture = fixture; var options = fixture.Fixture.CreateOptions(); options.SchemaName = fixture.SchemaName; _dataSource = new PolicyDataSource(Options.Create(options), NullLogger.Instance); } public Task InitializeAsync() => _fixture.TruncateAllTablesAsync(); public Task DisposeAsync() => Task.CompletedTask; [Trait("Category", TestCategories.Unit)] [Fact] public async Task Migration_CreatesRecheckAndEvidenceTables() { await using var connection = await _dataSource.OpenConnectionAsync("default", "reader", CancellationToken.None); await AssertTableExistsAsync(connection, "policy.recheck_policies"); await AssertTableExistsAsync(connection, "policy.evidence_hooks"); await AssertTableExistsAsync(connection, "policy.submitted_evidence"); } private static async Task AssertTableExistsAsync(NpgsqlConnection connection, string tableName) { await using var command = new NpgsqlCommand("SELECT to_regclass(@name)", connection); command.Parameters.AddWithValue("name", tableName); var result = await command.ExecuteScalarAsync(); result.Should().NotBeNull($"{tableName} should exist after migrations"); } }