// Example project to show how to use Atrium in combination with mocha
// For more information on how to setup Atrium for a JS project -> https://github.com/robstoll/atrium#js

buildscript {
    ext {
        // for infix-api -> change to 'atrium-infix-en_GB-js'
        atrium_api = 'atrium-fluent-en_GB-js'
        atrium_version = '0.11.0'
    }
    repositories {
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath 'com.github.node-gradle:gradle-node-plugin:2.2.0'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.71'
    }
}

apply plugin: 'kotlin2js'
apply plugin: 'com.github.node-gradle.node'

group 'org.atriumlib.samples'
version '0.0.1'

repositories {
    jcenter()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-js"

    // setup for Atrium:
    testImplementation("ch.tutteli.atrium:$atrium_api:$atrium_version")

    // setup for mocha:
    testImplementation "org.jetbrains.kotlin:kotlin-test-js"
}

// we only configure compileTestKotlin2Js and not compileKotlin2Js as well
// so that you can also use UMD as moduleKind for compileKotlin2Js
compileTestKotlin2Js.configure {
    // mocha requires commonjs
    kotlinOptions.moduleKind = "commonjs"

    // the following allows that debugging tests kind of works within kotlin
    // sometimes break points within a kotlin file is not taken up by Intellij.
    // in such a case it is worth while to create a break point in build/classes/kotlin/test/...test.js
    // stepping through the js code then usually switches to the kotlin file.
    kotlinOptions.sourceMap = true
    kotlinOptions.sourceMapEmbedSources = "always"
}

//gather all required dependencies and put them into the node_modules folder of this (sub)-project
task populateNodeModules(type: Copy, dependsOn: compileKotlin2Js) {
    from compileKotlin2Js.destinationDir

    afterEvaluate {
        // in contrast to kotlin examples we use testRuntimeClasspath this way we also pick up runtimeOnly dependencies
        configurations.testRuntimeClasspath.each {
            from zipTree(it.absolutePath).matching { include '*.js', '*.js.map' }
        }
    }

    into "$buildDir/node_modules"
}

//node, npm and dependencies of mocha is installed in the same folder for all sub-projects
def nodeModulesParentDir = file("$rootProject.projectDir/gradle/")
node {
    //download this way we don't have to download each time
    download = true
    npmVersion = '6.4.1'
    workDir = file("$rootProject.projectDir/.gradle/nodejs")
    npmWorkDir = file("$rootProject.projectDir/.gradle/npm")
    nodeModulesDir = nodeModulesParentDir
}

//either you use this task or create a testSetup.kt in your src/test with the following content
//
// // adopt to the api you use
// import ch.tutteli.atrium.fluent.en_GB.dependOnAtrium
//
// @Suppress("unused")
// private val establishDependencyToAtrium = dependOnAtrium()
task establishDependencyToAtrium(dependsOn: [compileTestKotlin2Js]) {
    doLast {
        def srcFile = file("$buildDir/classes/kotlin/test/${project.name}_test.js")
        String newContent = srcFile.getText('UTF-8')
            .replace("(function (_, Kotlin,", "(function (_, Kotlin, \$module\$${atrium_api.replace('-', '_')},")
            .replace("(module.exports, require('kotlin'),", "(module.exports, require('kotlin'), require('$atrium_api'),")
        srcFile.write(newContent, 'UTF-8')
    }
}
task runMocha(type: NpxTask, group: 'Verification', dependsOn: [compileTestKotlin2Js, populateNodeModules, establishDependencyToAtrium]) {
    command = "mocha"
    args = [compileTestKotlin2Js.outputFile, '--full-trace']
}
check.dependsOn runMocha
