plugins {
    id "com.github.node-gradle.node"
}

if (project.hasProperty("nodeInstall")) {
    node {
        version = "24.11.1"
        npmVersion = "11.6.4"
        download = true
    }

    // Copy local node and npm to a fixed location for npmw
    def deleteOldNpm = tasks.register("deleteOldNpm", Delete) {
        delete('build/node/lib/node_modules/npm')
    }
    def fixedNode = tasks.register("fixedNode", Copy) {
        from(nodeSetup)
        into('build/node')
        finalizedBy(deleteOldNpm)
    }
    tasks.named("nodeSetup").configure { finalizedBy(fixedNode) }

    def fixedNpm = tasks.register("fixedNpm", Copy) {
        from(npmSetup)
        into('build/node')
    }
    tasks.named("npmSetup").configure { finalizedBy(fixedNpm) }
}

task webapp_test(type: NpmTask) {
    inputs.property('appVersion', project.version)
    inputs.files("build.gradle")
        .withPropertyName('build.gradle')
        .withPathSensitivity(PathSensitivity.RELATIVE)
    inputs.files('.postcssrc.js', 'package-lock.json', 'package.json', 'tsconfig.app.json', 'tsconfig.json', 'vite.config.mts')
        .withPropertyName('vue-build')
        .withPathSensitivity(PathSensitivity.RELATIVE)
    inputs.dir("src/main/webapp/")
        .withPropertyName("src/main/webapp/")
        .withPathSensitivity(PathSensitivity.RELATIVE)

    outputs.dir("build/test-results/jest/")
        .withPropertyName("jest-result-dir")
    outputs.file("build/test-results/TESTS-results-jest.xml")
        .withPropertyName("jest-result")
        outputs.file("build/test-results/clover.xml")
        .withPropertyName("clover-result")

    dependsOn(npmInstall, compileTestJava)
    args = ["run", "webapp:test"]
}

test.dependsOn(webapp_test)

