package com.sonymusic

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

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

class SuiteAppPublishTest extends BaseGlobalVarTest {
    def suiteAppPublish

    @BeforeEach
    void setUp() {
        super.setUp()
        suiteAppPublish = loadScript('vars/suiteAppPublish.groovy')
        helper.registerAllowedMethod('withAWS', [Map, Closure])
        helper.registerAllowedMethod('s3Upload', [Map])
    }

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

    @Test
    void testCallWithPath() {
        assertNull(
            suiteAppPublish(env: 'qa', appName: 'frontend-test')
        )

        assertMethodCalledOnceWith('withAWS', 'us-east-1')
        assertMethodCalledTimes('s3Upload', 2)
        assertMethodCalledNthWith('s3Upload', 0, [
            'bucket=qa-orcd-cdn',
            'path=frontend-test',
            'excludePathPattern=index*.html',
            'cacheControl=public,max-age=31536000'
        ])
        assertMethodCalledNthWith('s3Upload', 1, [
            'bucket=qa-orcd-cdn',
            'path=frontend-test',
            'includePathPattern=index*.html',
            'cacheControl=no-store'
        ])
    }

    @Test
    void testCallWithEnv() {
        assertNull(
            suiteAppPublish(env: 'test', appName: 'frontend-test')
        )

        assertMethodCalledOnceWith('withAWS', 'us-east-1')
        assertMethodCalledTimes('s3Upload', 2)
        assertMethodCalledNthWith('s3Upload', 0, [
            'bucket=test-orcd-cdn',
            'path=frontend-test',
            'excludePathPattern=index*.html,dml.json',
            'cacheControl=public,max-age=31536000'
        ])
        assertMethodCalledNthWith('s3Upload', 1, [
            'bucket=test-orcd-cdn',
            'path=frontend-test',
            'includePathPattern=index*.html,dml.json',
            'cacheControl=no-store'
        ])
    }

    @Test
    void testCallWithAwsRegion() {
        assertNull(
            suiteAppPublish(
                env: 'test',
                appName: 'frontend-test',
                awsRegion: 'eu-north-1'
            )
        )

        assertMethodCalledOnceWith('withAWS', 'region=eu-north-1')
        assertMethodCalledTimes('s3Upload', 2)
    }

    @Test
    void testCallWithSubpath() {
        assertNull(
            suiteAppPublish(
                env: 'dev',
                appName: 'frontend-test',
                subpath: 'prs/1'
            )
        )

        assertMethodCalledOnceWith('withAWS', 'region=us-east-1')
        assertMethodCalledTimes('s3Upload', 2)
        assertMethodCalledNthWith('s3Upload', 0, [
            'bucket=dev-orcd-cdn',
            'path=frontend-test/prs/1'
        ])
        assertMethodCalledNthWith('s3Upload', 1, [
            'bucket=dev-orcd-cdn',
            'path=frontend-test/prs/1'
        ])
    }

    @Test
    void testCallWithNoCacheExtraPatterns() {
        assertNull(
            suiteAppPublish(
                env: 'qa',
                appName: 'frontend-test',
                noCacheExtraPatterns: 'main.dml.js,main.dml.js.map'
            )
        )

        assertMethodCalledOnceWith('withAWS', 'us-east-1')
        assertMethodCalledTimes('s3Upload', 2)
        assertMethodCalledNthWith('s3Upload', 0, [
            'bucket=qa-orcd-cdn',
            'path=frontend-test',
            'excludePathPattern=index*.html,dml.json,manifest.json,main.dml.js,main.dml.js.map',
            'cacheControl=public,max-age=31536000'
        ])
        assertMethodCalledNthWith('s3Upload', 1, [
            'bucket=qa-orcd-cdn',
            'path=frontend-test',
            'includePathPattern=index*.html,dml.json,manifest.json,main.dml.js,main.dml.js.map',
            'cacheControl=no-store'
        ])
    }

    @Test
    void testCallWithNoCacheExtraPatternsWithSpaces() {
        assertNull(
            suiteAppPublish(
                env: 'qa',
                appName: 'frontend-test',
                noCacheExtraPatterns: ' main.dml.js , main.dml.js.map , '
            )
        )

        assertMethodCalledOnceWith('withAWS', 'us-east-1')
        assertMethodCalledTimes('s3Upload', 2)
        assertMethodCalledNthWith('s3Upload', 0, [
            'bucket=qa-orcd-cdn',
            'path=frontend-test',
            'excludePathPattern=index*.html,dml.json,manifest.json,main.dml.js,main.dml.js.map',
            'cacheControl=public,max-age=31536000'
        ])
        assertMethodCalledNthWith('s3Upload', 1, [
            'bucket=qa-orcd-cdn',
            'path=frontend-test',
            'includePathPattern=index*.html,dml.json,manifest.json,main.dml.js,main.dml.js.map',
            'cacheControl=no-store'
        ])
    }

    @Test
    void testCallWithDefaultBucket() {
        assertNull(
            suiteAppPublish(env: 'qa', appName: 'frontend-test')
        )

        assertMethodCalledTimes('s3Upload', 2)
        assertMethodCalledNthWith('s3Upload', 0, [
            'bucket=qa-orcd-cdn',
            'path=frontend-test'
        ])
        assertMethodCalledNthWith('s3Upload', 1, [
            'bucket=qa-orcd-cdn',
            'path=frontend-test'
        ])
    }

    @Test
    void testCallWithCustomBucket() {
        assertNull(
            suiteAppPublish(
                env: 'qa',
                appName: 'frontend-test',
                bucket: 'my-custom-bucket'
            )
        )

        assertMethodCalledTimes('s3Upload', 2)
        assertMethodCalledNthWith('s3Upload', 0, [
            'bucket=my-custom-bucket',
            'path=frontend-test'
        ])
        assertMethodCalledNthWith('s3Upload', 1, [
            'bucket=my-custom-bucket',
            'path=frontend-test'
        ])
    }

}
