import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent

plugins {
  id 'net.consensys.zkevm.kotlin-library-conventions'
}

group = 'build.linea.staterecover'

dependencies {
  implementation("io.vertx:vertx-core:${libs.versions.vertx.get()}")
  implementation(project(':besu-plugins:state-recovery:appcore:logic'))
  implementation(project(':besu-plugins:state-recovery:besu-plugin'))
  implementation(project(':besu-plugins:state-recovery:clients:eth-api'))
  implementation(project(':besu-plugins:state-recovery:clients:blobscan-client'))
  implementation('build.linea:l1-rollup-contract-client:6.0.0')
  implementation project(':jvm-libs:generic:vertx-helper')
  implementation project(':jvm-libs:generic:extensions:kotlin')
  implementation project(':jvm-libs:linea:besu-libs')
  implementation project(':jvm-libs:linea:testing:file-system')
  implementation(project(':coordinator:clients:smart-contract-client'))
  implementation(project(':jvm-libs:linea:linea-contracts:l1-rollup'))
  implementation(project(':coordinator:core'))
  implementation(project(":coordinator:ethereum:test-utils"))
  implementation(project(":jvm-libs:linea:testing:l1-blob-and-proof-submission"))
  implementation(testFixtures(project(':jvm-libs:linea:web3j-extensions')))
  testImplementation("io.vertx:vertx-junit5")
  testImplementation(project(":jvm-libs:linea:metrics:micrometer"))
}

sourceSets {
  integrationTest {
    kotlin {
      compileClasspath += sourceSets.main.output + sourceSets.main.compileClasspath + sourceSets.test.output + sourceSets.test.compileClasspath
      runtimeClasspath += sourceSets.main.output + sourceSets.main.runtimeClasspath + sourceSets.test.output + sourceSets.test.runtimeClasspath
    }
    compileClasspath += sourceSets.main.output + sourceSets.main.compileClasspath + sourceSets.test.output + sourceSets.test.compileClasspath
    runtimeClasspath += sourceSets.main.output + sourceSets.main.runtimeClasspath + sourceSets.test.output + sourceSets.test.runtimeClasspath
  }
  e2eTest {
    kotlin {
      compileClasspath += sourceSets.main.output + sourceSets.main.compileClasspath + sourceSets.test.output + sourceSets.test.compileClasspath
      runtimeClasspath += sourceSets.main.output + sourceSets.main.runtimeClasspath + sourceSets.test.output + sourceSets.test.runtimeClasspath
    }
    compileClasspath += sourceSets.main.output + sourceSets.main.compileClasspath + sourceSets.test.output + sourceSets.test.compileClasspath
    runtimeClasspath += sourceSets.main.output + sourceSets.main.runtimeClasspath + sourceSets.test.output + sourceSets.test.runtimeClasspath
  }
}

test {
  systemProperty "vertx.parameter.filename", project.projectDir.toPath()
      .resolve("src/test/resources/vertx-options.json")
      .toAbsolutePath().toString()
}

task integrationTest(type: Test) { test ->
  outputs.cacheIf { false }
  outputs.upToDateWhen { false }
  systemProperty "vertx.parameter.filename", project.projectDir.toPath()
      .resolve("src/test/resources/vertx-options.json")
      .toAbsolutePath().toString()

  description = "Runs integration tests."
  group = "verification"
  useJUnitPlatform()

  classpath = sourceSets.integrationTest.runtimeClasspath
  testClassesDirs = sourceSets.integrationTest.output.classesDirs

  dependsOn(":localStackForStateRecoveryComposeUp")
  // increase default timeout because this tests submit loads of blobs
  systemProperties["junit.jupiter.execution.timeout.default"] = "10 m" // 10 minutes

  testLogging {
    events TestLogEvent.FAILED,
        TestLogEvent.SKIPPED,
        TestLogEvent.STANDARD_ERROR,
        TestLogEvent.STARTED,
        TestLogEvent.PASSED
    exceptionFormat TestExceptionFormat.FULL
    showCauses true
    showExceptions true
    showStackTraces true
    // set showStandardStreams if you need to see test logs
    showStandardStreams true
  }
}

task e2eTest(type: Test) { test ->
  outputs.cacheIf { false }
  outputs.upToDateWhen { false }

  systemProperty "vertx.parameter.filename", project.projectDir.toPath()
      .resolve("src/test/resources/vertx-options.json")
      .toAbsolutePath().toString()

  description = "Runs e2e tests."
  group = "verification"
  useJUnitPlatform()

  classpath = sourceSets.e2eTest.runtimeClasspath
  testClassesDirs = sourceSets.e2eTest.output.classesDirs

  systemProperties["junit.jupiter.execution.timeout.default"] = "10 m" // 10 minutes
  systemProperties["junit.jupiter.execution.parallel.enabled"] = false
  maxParallelForks = Math.max(Runtime.runtime.availableProcessors(), 9)

  testLogging {
    events TestLogEvent.FAILED,
        TestLogEvent.SKIPPED,
        TestLogEvent.STANDARD_ERROR,
        TestLogEvent.STARTED,
        TestLogEvent.PASSED
    exceptionFormat TestExceptionFormat.FULL
    showCauses true
    showExceptions true
    showStackTraces true
    // set showStandardStreams if you need to see test logs
    showStandardStreams true
  }
}
