package com.sonymusic

import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

import static org.junit.jupiter.api.Assertions.assertNull

class SuiteAppBuildTest extends BaseGlobalVarTest {
    def suiteAppBuild

    @BeforeEach
    void setUp() {
        super.setUp()
        suiteAppBuild = loadScript('vars/suiteAppBuild.groovy')
        helper.registerAllowedMethod('withCredentials', [Map, Closure])
        helper.registerAllowedMethod('yarnRun', [Map])
        helper.registerAllowedMethod('packageManagerRun', [Map])
        binding.setVariable('env', [:])
    }

    @Test
    void testCallWithInvalidArgs() {
        assertThrowsWithMessage(
            IllegalArgumentException.class,
            "suiteAppBuild: Missing required parameter: 'env'",
            { suiteAppBuild.call() }
        )
    }

    @Test
    void testCallWithQaEnv() {
        assertNull(
            suiteAppBuild(env: 'qa')
        )

        assertMethodCalledOnceWith('withCredentials', '[POEDITOR_API_TOKEN]')
        assertMethodCalledOnceWith('yarnRun', [
            'scriptNames=[build]',
            'ENV=qa',
            'GRAPHQL_URL=https://qa-ows-grass.theorchard.io/graphql-router/graphql',
            'AUTH0_DOMAIN=qalogin.theorchard.com',
            'AUTH0_AUDIENCE=https://workstation.qaorch.com/api',
            'CDN_URL=https://qa-cdn.theorchard.io'
        ])
    }

    @Test
    void testCallWithUatEnv() {
        assertNull(
            suiteAppBuild(env: 'uat')
        )

        assertMethodCalledOnceWith('withCredentials', '[POEDITOR_API_TOKEN]')
        assertMethodCalledOnceWith('yarnRun', [
            'scriptNames=[build]',
            'ENV=uat',
            'GRAPHQL_URL=https://uat-ows-grass.theorchard.io/graphql-router/graphql',
            'AUTH0_DOMAIN=qalogin.theorchard.com',
            'AUTH0_AUDIENCE=https://workstation.qaorch.com/api',
            'CDN_URL=https://uat-cdn.theorchard.io'
        ])
    }

    @Test
    void testCallWithProdEnv() {
        assertNull(
            suiteAppBuild(env: 'prod')
        )

        assertMethodCalledOnceWith('withCredentials', '[POEDITOR_API_TOKEN]')
        assertMethodCalledOnceWith('yarnRun', [
            'scriptNames=[build]',
            'ENV=prod',
            'GRAPHQL_URL=https://ows-grass.theorchard.io/graphql-router/graphql',
            'AUTH0_DOMAIN=login.distroauth.com',
            'AUTH0_AUDIENCE=https://workstation.theorchard.com/api',
            'CDN_URL=https://cdn.theorchard.io'
        ])
    }

    @Test
    void testCallWithScriptNames() {
        assertNull(
            suiteAppBuild(
                env: 'dev',
                scriptNames: ['test', 'build']
            )
        )

        assertMethodCalledOnceWith('withCredentials', '[POEDITOR_API_TOKEN]')
        assertMethodCalledOnceWith('yarnRun', [
            'scriptNames=[test, build]',
            'ENV=dev'
        ])
    }

    @Test
    void testCallWithEnvVars() {
        assertNull(
            suiteAppBuild(
                env: 'dev',
                envVars: [TEST: 'test', ENV: 'qa']
            )
        )

        assertMethodCalledOnceWith('withCredentials', '[POEDITOR_API_TOKEN]')
        assertMethodCalledOnceWith('yarnRun', [
            'scriptNames=[build]',
            'TEST=test'
        ])
    }

    @Test
    void testCallWithNodeVersion() {
        assertNull(
            suiteAppBuild(
                env: 'dev',
                nodeVersion: '20'
            )
        )

        assertMethodCalledOnceWith('withCredentials', '[POEDITOR_API_TOKEN]')
        assertMethodCalledOnceWith('yarnRun', [
            'scriptNames=[build]',
            'nodeVersion=20'
        ])
    }

    @Test
    void testCallWithDetectPackageManagerFalse() {
        assertNull(
            suiteAppBuild(
                env: 'qa',
                detectPackageManager: false
            )
        )

        assertMethodCalledOnceWith('withCredentials', '[POEDITOR_API_TOKEN]')
        assertMethodCalledOnceWith('yarnRun', [
            'scriptNames=[build]',
            'ENV=qa'
        ])
        assertMethodNotCalled('packageManagerRun')
    }

    @Test
    void testCallWithDetectPackageManagerTrue() {
        assertNull(
            suiteAppBuild(
                env: 'qa',
                detectPackageManager: true
            )
        )

        assertMethodCalledOnceWith('withCredentials', '[POEDITOR_API_TOKEN]')
        assertMethodCalledOnceWith('packageManagerRun', [
            'scriptNames=[build]',
            'ENV=qa',
            'GRAPHQL_URL=https://qa-ows-grass.theorchard.io/graphql-router/graphql',
            'AUTH0_DOMAIN=qalogin.theorchard.com',
            'AUTH0_AUDIENCE=https://workstation.qaorch.com/api',
            'CDN_URL=https://qa-cdn.theorchard.io'
        ])
        assertMethodNotCalled('yarnRun')
    }

    @Test
    void testCallWithGitCommit() {
        binding.setVariable('env', [GIT_COMMIT: 'abc123'])

        assertNull(
            suiteAppBuild(env: 'qa')
        )

        assertMethodCalledOnceWith('withCredentials', '[POEDITOR_API_TOKEN]')
        assertMethodCalledOnceWith('yarnRun', [
            'scriptNames=[build]',
            'ENV=qa',
            'GIT_COMMIT=abc123'
        ])
    }

    @Test
    void testCallWithGitCommitOverride() {
        binding.setVariable('env', [GIT_COMMIT: 'abc123'])

        assertNull(
            suiteAppBuild(
                env: 'qa',
                envVars: [GIT_COMMIT: 'override456']
            )
        )

        assertMethodCalledOnceWith('withCredentials', '[POEDITOR_API_TOKEN]')
        assertMethodCalledOnceWith('yarnRun', [
            'scriptNames=[build]',
            'ENV=qa',
            'GIT_COMMIT=override456'
        ])
    }

    @Test
    void testCallWithDetectPackageManagerAndNodeVersion() {
        assertNull(
            suiteAppBuild(
                env: 'prod',
                detectPackageManager: true,
                nodeVersion: '22'
            )
        )

        assertMethodCalledOnceWith('withCredentials', '[POEDITOR_API_TOKEN]')
        assertMethodCalledOnceWith('packageManagerRun', [
            'scriptNames=[build]',
            'ENV=prod',
            'nodeVersion=22'
        ])
        assertMethodNotCalled('yarnRun')
    }
}
