/*
 * Copyright 2013-2026, Seqera Labs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nextflow.script.parser.v1

import java.nio.file.Paths

import nextflow.Session
import nextflow.exception.ScriptCompilationException
import nextflow.script.BaseScript
import nextflow.script.ScriptBinding
import spock.lang.Specification
import test.TestHelper
/**
 *
 * @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
 */
class ScriptLoaderV1Test extends Specification {

    def 'should run a file script' () {

        given:
        def session = new Session()
        def parser = new ScriptLoaderV1(session)
        def binding = new ScriptBinding(params:[foo:'Hello'])

        def file = TestHelper.createInMemTempFile('foo.nf')
        file.text = '''
        bar = "$params.foo world!"
        '''

        when:
        parser.setBinding(binding)
        parser.runScript(file)
        then:
        parser.script instanceof BaseScript
        parser.result == 'Hello world!'
        parser.result == binding.getVariable('bar')
        parser.binding.getScriptPath() == file
        parser.binding.getSession() == session
        !session.binding.hasVariable('bar')
    }

    def 'should run a text script' () {

        given:
        def session = new Session()
        def parser = new ScriptLoaderV1(session)
        def binding = new ScriptBinding(params:[foo:'Hello'])

        def TEXT = '''
        bar = "$params.foo world!"
        '''

        when:
        parser.setBinding(binding)
        parser.runScript(TEXT)
        then:
        parser.script instanceof BaseScript
        parser.result == 'Hello world!'
        parser.result == binding.getVariable('bar')
        parser.binding.getScriptPath() == null
        parser.binding.getSession() == session
        !session.binding.hasVariable('bar')
    }

    def 'should run a script with session binding' () {

        given:
        def session = new Session()
        def parser = new ScriptLoaderV1(session)
        session.binding.setVariable('foo', 'Hello')

        def TEXT = '''
        bar = "$foo world!"
        '''

        when:
        parser.runScript(TEXT)
        then:
        parser.script instanceof BaseScript
        parser.result == 'Hello world!'
        parser.binding.getScriptPath() == null
        parser.binding.getSession() == session
        session.binding.getVariable('foo') == 'Hello'
        session.binding.getVariable('bar') == 'Hello world!'
    }

    def 'should normalise script name'() {

        given:
        def parser = new ScriptLoaderV1(Mock(Session))

        expect:
        parser.computeClassName(Paths.get(SCRIPT)) == EXPECTED

        where:
        SCRIPT              | EXPECTED
        'foo.nf'            | '_nf_script_foo'
        'foo-bar-baz.nf'    | '_nf_script_foo_bar_baz'
        '123-fo0'           | '_nf_script_23_fo0'
        '--a  b  c'         | '_nf_script_a_b_c'
    }

    def 'should normalise script text' () {

        given:
        def parser = new ScriptLoaderV1(Mock(Session))

        when:
        def result = parser.computeClassName('process foo { etc } ')
        then:
        result == '_nf_script_dd540db41b3a8b2a'
    }

    def 'should set classpath' () {

        given:
        def CL = Mock(ClassLoader)
        def SESS = Mock(Session) { getClassLoader() >> CL }

        when:
        def parser = new ScriptLoaderV1(SESS)
        then:
        parser.getSession() == SESS
        parser.getClassLoader() == CL

    }

    def 'should catch compilation errors' () {
        given:
        def session = new Session()
        def parser = new ScriptLoaderV1(session)

        def file = TestHelper.createInMemTempFile('foo.nf')
        file.text = '''
        def foo)( { )
        '''

        when:
        parser.runScript(file)
        then:
        def e = thrown(ScriptCompilationException)
        e.message.startsWith('Script compilation error')
        e.message.contains("- cause: Unexpected input: ')'")
        e.message.contains('foo.nf\n')
    }

}
