package com.codebyte.api;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;

import com.codebyte.api.support.AbstractPostgresIT;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationInfo;
import org.flywaydb.core.api.MigrationState;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * Verifies Flyway migrations apply cleanly on an empty database and that no already-applied
 * migration was edited. {@link Flyway#validate()} throws if an applied migration's checksum
 * changed, so this test fails the build the moment someone mutates a shipped migration.
 */
@SpringBootTest
class FlywayMigrationIT extends AbstractPostgresIT {

    @Autowired private Flyway flyway;

    @Test
    void should_apply_all_migrations_successfully_on_startup() {
        MigrationInfo[] applied = flyway.info().applied();

        assertThat(applied).isNotEmpty();
        assertThat(applied)
                .allSatisfy(info -> assertThat(info.getState()).isEqualTo(MigrationState.SUCCESS));
    }

    @Test
    void should_pass_validation_when_migration_checksums_unchanged() {
        assertThatCode(flyway::validate).doesNotThrowAnyException();
    }
}
