<commit_before>package net.nemerosa.ontrack.extension.jenkins.indicator

import net.nemerosa.ontrack.extension.git.AbstractGitTestSupport
import net.nemerosa.ontrack.extension.indicators.model.IndicatorComputedCategory
import net.nemerosa.ontrack.extension.indicators.model.IndicatorComputedType
import net.nemerosa.ontrack.extension.indicators.model.IndicatorComputedValue
import org.junit.Test
import org.springframework.beans.factory.annotation.Autowired
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class JenkinsPipelineLibraryIndicatorComputerIT : AbstractGitTestSupport() {

    @Autowired
    private lateinit var computer: JenkinsPipelineLibraryIndicatorComputer

    @Autowired
    private lateinit var valueType: JenkinsPipelineLibraryIndicatorValueType

    @Test
    fun `No indicator value when no Git configuration`() {
        project {
            val indicators = computer.computeIndicators(project)
            assertTrue(indicators.isEmpty(), "No indicator")
        }
    }

    @Test
    fun `Several libraries detected`() {
        createRepo {
            file("Jenkinsfile", """
                @Library("pipeline-general@1.0.1")
                @Library("pipeline-common") _
                
                pipeline()
            """.trimIndent())
            git("commit", "-m", "Commit of the Jenkinsfile")
        } and { repo, _ ->
            project {
                gitProject(repo)
                val indicators = computer.computeIndicators(project)
                assertEquals(
                    listOf(
                        IndicatorComputedValue(
                            type = IndicatorComputedType(
                                category = IndicatorComputedCategory(
                                    "jenkins-pipeline-library",
                                    "Jenkins pipeline libraries"<commit_msg>#884 More tests<commit_after>package net.nemerosa.ontrack.extension.jenkins.indicator

import net.nemerosa.ontrack.extension.git.AbstractGitTestSupport
import net.nemerosa.ontrack.extension.indicators.model.IndicatorComputedCategory
import net.nemerosa.ontrack.extension.indicators.model.IndicatorComputedType
import net.nemerosa.ontrack.extension.indicators.model.IndicatorComputedValue
import org.junit.Test
import org.springframework.beans.factory.annotation.Autowired
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class JenkinsPipelineLibraryIndicatorComputerIT : AbstractGitTestSupport() {

    @Autowired
    private lateinit var computer: JenkinsPipelineLibraryIndicatorComputer

    @Autowired
    private lateinit var valueType: JenkinsPipelineLibraryIndicatorValueType

    @Test
    fun `No indicator value when no Git configuration`() {
        project {
            val indicators = computer.computeIndicators(project)
            assertTrue(indicators.isEmpty(), "No indicator")
        }
    }

    @Test
    fun `Single library`() {
        createRepo {
            file("Jenkinsfile", """
                @Library('pipeline-general@1.0.1') _
                
                pipeline()
            """.trimIndent())
            git("commit", "-m", "Commit of the Jenkinsfile")
        } and { repo, _ ->
            project {
                gitProject(repo)
                val indicators = computer.computeIndicators(project)
                assertEquals(
                    listOf(
                        IndicatorComputedValue(
                            type = IndicatorComputedType(
                                category = IndicatorComputedCategory(
                                    "jenkins-pipeline-library",
                                    "Jenkins pipeline libraries"
                                ),
                                id = "pipeline-general",
                                name = "pipeline-general",
                                link = null,
                                valueType = valueType,
                                valueConfig = JenkinsPipelineLibraryIndicatorValueTypeConfig(false, null),
                            ),
                            value = JenkinsPipelineLibraryVersion("1.0.1"),
                            comment = null
                        ),
                    ),
                    indicators
                )
            }
        }
    }

    @Test
    fun `Several libraries detected`() {
        createRepo {
            file("Jenkinsfile", """
                @Library("pipeline-general@1.0.1")
                @Library("pipeline-common") _
                
                pipeline()
            """.trimIndent())
            git("commit", "-m", "Commit of the Jenkinsfile")
        } and { repo, _ ->
            project {
                gitProject(repo)
                val indicators = computer.computeIndicators(project)
                assertEquals(
                    listOf(
                        IndicatorComputedValue(
                            type = IndicatorComputedType(
                                category = IndicatorComputedCategory(
                                    "jenkins-pipeline-library",
                                    "Jenkins pipeline libraries"
{'fraction_non_alphanumeric': 0.07739699653446284, 'fraction_numerical': 0.002887947631882942, 'mean_word_length': 1.6053159478435306, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2150648', 'n_tokens_mistral': 1235, 'n_tokens_neox': 1196, 'n_words': 218}