using EntKube.Provisioning.Domain; using EntKube.Provisioning.Features.Apps.RemoveAppSecret; using EntKube.Provisioning.Infrastructure; using EntKube.SharedKernel.Domain; using FluentAssertions; namespace EntKube.Provisioning.Tests.Features; public class RemoveAppSecretHandlerTests { private readonly InMemoryAppRepository repository; private readonly RemoveAppSecretHandler handler; public RemoveAppSecretHandlerTests() { repository = new InMemoryAppRepository(); handler = new RemoveAppSecretHandler(repository); } [Fact] public async Task HandleAsync_ExistingSecret_RemovesSuccessfully() { // Arrange — An app with an environment that has a secret. App app = App.Create(Guid.NewGuid(), Guid.NewGuid(), "API", "api", AppType.Deployment); Guid envId = Guid.NewGuid(); AppEnvironment env = app.AddEnvironment(envId, Guid.NewGuid(), "api-dev"); env.AddSecret("DATABASE_URL", "db-conn"); await repository.AddAsync(app); // Act Result result = await handler.HandleAsync( new RemoveAppSecretRequest(app.Id, envId, "DATABASE_URL")); // Assert result.IsSuccess.Should().BeTrue(); App? updated = await repository.GetByIdAsync(app.Id); updated!.Environments[0].Secrets.Should().BeEmpty(); } [Fact] public async Task HandleAsync_NonexistentApp_ReturnsFailure() { // Act Result result = await handler.HandleAsync( new RemoveAppSecretRequest(Guid.NewGuid(), Guid.NewGuid(), "KEY")); // Assert result.IsFailure.Should().BeTrue(); result.Error.Should().Contain("not found"); } [Fact] public async Task HandleAsync_NonexistentSecret_ReturnsFailure() { // Arrange App app = App.Create(Guid.NewGuid(), Guid.NewGuid(), "API", "api", AppType.Deployment); Guid envId = Guid.NewGuid(); app.AddEnvironment(envId, Guid.NewGuid(), "api-dev"); await repository.AddAsync(app); // Act Result result = await handler.HandleAsync( new RemoveAppSecretRequest(app.Id, envId, "MISSING")); // Assert result.IsFailure.Should().BeTrue(); result.Error.Should().Contain("not found"); } }