/*
 * 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

import spock.lang.Specification
import spock.lang.Unroll

/**
 *
 * @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
 */
class SysEnvTest extends Specification {

    def 'should get default config' () {
        expect:
        SysEnv.get('HOME')  == System.getenv('HOME')
    }

    def 'should push a new env' () {
        when:
        SysEnv.push([HOME: 'hola'])
        then:
        SysEnv.get('HOME') == 'hola'
        and:
        SysEnv.get().size() == 1

        when:
        SysEnv.push([HOME: 'ciao'])
        then:
        SysEnv.get('HOME') == 'ciao'

        when:
        SysEnv.pop()
        then:
        SysEnv.get('HOME') == 'hola'

        when:
        SysEnv.pop()
        then:
        SysEnv.get('HOME') == System.getenv('HOME')

    }

    @Unroll
    def 'should get bool value' () {
        given:
        SysEnv.push(STATE)

        expect:
        SysEnv.getBool('FOO', DEF) == EXPECTED

        where:
        STATE           | DEF       | EXPECTED
        [:]             | false     | false
        [FOO:'false']   | false     | false
        [FOO:'true']    | false     | true
        and:
        [:]             | true      | true
        [FOO:'false']   | false     | false
        [FOO:'true']    | true      | true
    }

    @Unroll
    def 'should get integer value' () {
        given:
        SysEnv.push(STATE)

        expect:
        SysEnv.getInteger('FOO', DEF) == EXPECTED

        where:
        STATE           | DEF       | EXPECTED
        [:]             | null      | null
        [FOO:'0']       | null      | 0
        [FOO:'1']       | null      | 1
        and:
        [:]             | 0         | 0
        [FOO:'0']       | 0         | 0
        [FOO:'100']     | 0         | 100
        and:
        [:]             | 1         | 1
        [FOO:'0']       | 1         | 0
        [FOO:'100']     | 1         | 100
    }

    @Unroll
    def 'should get long value' () {
        given:
        SysEnv.push(STATE)

        expect:
        SysEnv.getLong('FOO', DEF) == EXPECTED

        where:
        STATE           | DEF       | EXPECTED
        [:]             | null      | null
        [FOO:'0']       | null      | 0
        [FOO:'1']       | null      | 1
        and:
        [:]             | 0         | 0
        [FOO:'0']       | 0         | 0
        [FOO:'100']     | 0         | 100
        and:
        [:]             | 1         | 1
        [FOO:'0']       | 1         | 0
        [FOO:'100']     | 1         | 100
    }

    @Unroll
    def 'should detect agent mode' () {
        given:
        SysEnv.push(STATE)

        expect:
        SysEnv.isAgentMode() == EXPECTED

        cleanup:
        SysEnv.pop()

        where:
        STATE                       | EXPECTED
        [:]                         | false
        [NXF_AGENT_MODE:'true']          | true
        [NXF_AGENT_MODE:'false']         | false
        [AGENT:'true']              | true
        [CLAUDECODE:'true']         | true
        // Multiple can be set, any true triggers agent mode
        [NXF_AGENT_MODE:'true', AGENT:'false']  | true
        // Support '1' as truthy value (common Unix convention)
        [NXF_AGENT_MODE:'1']             | true
        [AGENT:'1']                 | true
        [CLAUDECODE:'1']            | true
    }
}
