package com.sonymusic

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

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

class E2ETestsTest extends BaseGlobalVarTest {
    def e2eTests

    @BeforeEach
    void setUp() {
        super.setUp()
        e2eTests = loadScript("vars/e2eTests.groovy")
        helper.registerAllowedMethod('sh')
        helper.registerAllowedMethod('aws', [Map])
        helper.registerAllowedMethod('nodejs', [Map, Closure])
        helper.registerAllowedMethod('ansiColor', [String, Closure])
        helper.registerAllowedMethod('cucumber', [Map])
        helper.registerAllowedMethod('slackNotify', [Map])
        binding.setVariable('env', [ 
            JOB_NAME: 'test',
            BUILD_ID: '123',
            WORKSPACE: 'workspacepath',
        ])
    }

    void assertDefaultCalls(){
        assertMethodCalledOnceWith('dir', 'cucumber-cypress-tests/default')
        assertMethodCalledOnceWith('checkout', 'url=git@github.com:theorchard/cucumber-cypress-tests.git')
        assertMethodCalledOnceWith('withCredentials', ['null', 'PACKAGECLOUD_TOKEN', 'GITHUB_TOKEN'])
        assertMethodCalledOnceWith('ansiColor', 'xterm')
        assertMethodCalledOnceWith('cucumber', [
            'fileIncludePattern=**/*.json',
            'jsonReportDirectory=cypress/results/'
        ])
    }

    void assertDockerContainerClean(shellIndex) {
        assertMethodCalledNthWith('sh', shellIndex, 'make clean_docker_containers')
   }

    @Test
    void testCallWithMissingParameters() {
        assertThrowsWithMessage(
            IllegalArgumentException.class,
            "e2eTests: Missing required parameter: 'tags'",
            { e2eTests.call() }
        )
    }

    @Test
    void testCallWithDefaultMakeTarget() {
        e2eTests(tags: '@test')

        assertMethodCalledNthWith('sh', 0,   "make cypress_run_parallel TAGS='@test' JOB_NAME=test BUILD_ID=123 CONFIG_FILE=qa REPORT_TITLE='QA' RERUN_FARGATE='false'")
        assertDockerContainerClean(1)
        assertDefaultCalls();
    }

    @Test
    void testCallWithProvidedMakefile() {
        e2eTests(makeTarget: 'test', tags: '@test')

        assertMethodCalledNthWith('sh', 0, "make test TAGS='@test' JOB_NAME=test BUILD_ID=123 CONFIG_FILE=qa REPORT_TITLE='QA' RERUN_FARGATE='false'")
        assertDockerContainerClean(1)
        assertDefaultCalls();
    }

    @Test
    void testCucumberReportAlwaysGenerated() {
        helper.addShMock(~/(?s).*framework.*/, 'test failed', 1)
        assertThrows(Exception.class, {
            e2eTests.call(tags: '@test')
        })
        assertMethodCalledNthWith('sh', 0,   "make cypress_run_parallel TAGS='@test' JOB_NAME=test BUILD_ID=123 CONFIG_FILE=qa REPORT_TITLE='QA' RERUN_FARGATE='false'")
        assertDockerContainerClean(1)
        assertDefaultCalls();
    }

    @Test
    void testCallWithCheckoutSubDir() {
        e2eTests(tags: '@test', checkoutSubDir: 'e2e-qa')
        assertMethodCalledNthWith('sh', 0, "make cypress_run_parallel TAGS='@test' JOB_NAME=test BUILD_ID=123 CONFIG_FILE=qa REPORT_TITLE='QA' RERUN_FARGATE='false'")
        assertMethodCalledOnceWith('dir', 'cucumber-cypress-tests/e2e-qa')
        assertMethodCalledTimes('cucumber', 1)
        assertDockerContainerClean(1)
    }

    @Test
    void testCallWithDefaultFrameworkEnvironment() {
        e2eTests(tags: '@test')
        assertMethodCalledNthWith('sh', 0, "make cypress_run_parallel TAGS='@test' JOB_NAME=test BUILD_ID=123 CONFIG_FILE=qa REPORT_TITLE='QA' RERUN_FARGATE='false'")
        assertMethodCalledNthWith('sh', 0, 'export ENV=prod')
        assertDockerContainerClean(1)
        assertMethodCalledTimes('cucumber', 1)
    }

    @Test
    void testCallWithQAFrameworkEnvironment() {
        e2eTests(tags: '@test', frameworkEnvironment: 'qa')

        assertMethodCalledNthWith('sh', 0, "make cypress_run_parallel TAGS='@test' JOB_NAME=test BUILD_ID=123 CONFIG_FILE=qa REPORT_TITLE='QA' RERUN_FARGATE='false'")
        assertMethodCalledNthWith('sh', 0, 'export ENV=qa')
        assertDockerContainerClean(1)
        assertMethodCalledTimes('cucumber', 1)
    }

    @Test
    void testCallWithEnvVars() {
        e2eTests(tags: '@test',  envVars: [CYPRESS_TEST_WISE:'true', DD_ENV:'qa'])

        assertMethodCalledNthWith('sh', 0, "make cypress_run_parallel TAGS='@test' JOB_NAME=test BUILD_ID=123 CONFIG_FILE=qa REPORT_TITLE='QA' RERUN_FARGATE='false' CYPRESS_TEST_WISE=true DD_ENV=qa")
        assertMethodCalledNthWith('sh', 0,'export ENV=prod')
        assertDockerContainerClean(1)
        assertMethodCalledTimes('cucumber', 1)
    }
    
    @Test
    void testCallWithMultipleTags() {
        e2eTests(tags: '@test and @test2',  envVars: [CYPRESS_TEST_WISE:'true', DD_ENV:'qa'])

        assertMethodCalledNthWith('sh', 0, "make cypress_run_parallel TAGS='@test and @test2' JOB_NAME=test BUILD_ID=123 CONFIG_FILE=qa REPORT_TITLE='QA' RERUN_FARGATE='false' CYPRESS_TEST_WISE=true DD_ENV=qa")
        assertMethodCalledNthWith('sh', 0,'export ENV=prod')
        assertDockerContainerClean(1)
        assertMethodCalledTimes('cucumber', 1)
    }
}
