package com.sonymusic

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

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

class PnpmRunTest extends BaseGlobalVarTest {
    def pnpmRun

    @BeforeEach
    void setUp() {
        super.setUp()
        pnpmRun = loadScript('vars/pnpmRun.groovy')

        helper.registerAllowedMethod('nodeSh', [Map])
        binding.setVariable('env', [:])
    }

    @Test
    void testCallWithNoArgs() {
        assertThrowsWithMessage(
            IllegalArgumentException.class,
            "pnpmRun: Missing required parameter: 'scriptNames'",
            { pnpmRun.call() }
        )
    }

    @Test
    void testCallWithScriptNames() {
        assertNull(
            pnpmRun(scriptNames: ['lint', 'test:unit'])
        )
        assertMethodCalledOnceWith('nodeSh', [
            'set -e',
            'corepack enable',
            'pnpm config set "//npm.pkg.github.com/:_authToken" "$GITHUB_NPM_TOKEN"',
            'pnpm install --frozen-lockfile',
            'pnpm lint',
            'pnpm test:unit'
        ])
    }

    @Test
    void testCallWithNodeVersion() {
        assertNull(
            pnpmRun(scriptNames: ['test'], nodeVersion: '22.1.1')
        )

        assertMethodCalledOnceWith('nodeSh', [
            'nodeVersion=22.1.1',
            'corepack enable',
            'pnpm test',
        ])
    }

    @Test
    void testCallWithNodeVersion24() {
        assertNull(
            pnpmRun(scriptNames: ['test'], nodeVersion: '24.5.0')
        )

        assertMethodCalledOnceWith('nodeSh', [
            'nodeVersion=24.5.0',
            'corepack enable',
            'pnpm test',
        ])
    }

    @Test
    void testCallWithEnvVars() {
        assertNull(
            pnpmRun(
                scriptNames: ['test'],
                envVars: [
                    TEST: 'value',
                ]
            )
        )

        assertMethodCalledOnceWith('nodeSh', [
            'envVars={TEST=value}',
            'corepack enable',
            'pnpm test'
        ])
    }
}
