def pipelines_testdata_base_path = '/home/nelnk861/Documents/code/test-datasets/'
def projectDir = new File('.').absolutePath

// Extract test data if needed (runs once before all tests)
def extractTestData(pipelines_testdata_base_path) {
    def tarFile = new File("${pipelines_testdata_base_path}testdata/NovaSeq6000/200624_A00834_0183_BHMTFYDRXX.tar.gz")
    def extractDir = new File("${pipelines_testdata_base_path}testdata/NovaSeq6000/")
    def rundir = new File("${pipelines_testdata_base_path}testdata/NovaSeq6000/200624_A00834_0183_BHMTFYDRXX")
    
    if (tarFile.exists()) {
        println "Extracting test data from ${tarFile}..."
        def proc = "tar -xzf ${tarFile.absolutePath} -C ${extractDir.absolutePath}".execute()
        proc.waitFor()
        if (proc.exitValue() != 0) {
            throw new RuntimeException("Failed to extract test data: ${proc.err.text}")
        }
        println "Test data extracted successfully"
    } else if (rundir.exists()) {
        println "Test data already extracted"
    } else {
        throw new RuntimeException("Test data tar file not found: ${tarFile}")
    }
}

// Run extraction
extractTestData(pipelines_testdata_base_path)

nextflow_pipeline {

    name "Test pipeline"
    script "../main.nf"

    def test_scenario = [
        [
            name: "NovaSeq6000 data test",
            params: [
                input: pipelines_testdata_base_path + 'testdata/NovaSeq6000/samplesheet.csv',
            ],
            snapshot: 'stderr,stdout'
        ],
        [
            name: "NovaSeq6000 data test sample size",
            params: [
                sample_size: 90,
                input: pipelines_testdata_base_path + 'testdata/NovaSeq6000/samplesheet.csv',
            ]
        ],
        [
            name: "NovaSeq6000 data test relative sample size",
            params: [
                sample_size: 0.9,
                input: pipelines_testdata_base_path + 'testdata/NovaSeq6000/samplesheet.csv',
            ]
        ],
        [
            name: "NovaSeq6000 data test sample size exceeds available reads",
            params: [
                sample_size: 120,
                input: pipelines_testdata_base_path + 'testdata/NovaSeq6000/samplesheet.csv',
            ]
        ],
        [
            name: "NovaSeq6000 data test - No rundirs",
            params: [
                input: pipelines_testdata_base_path + 'testdata/NovaSeq6000/samplesheet_no_rundirs.csv'
            ]
        ],
        [
            name: "NovaSeq6000 data test - tag collision",
            params: [
                input: pipelines_testdata_base_path + 'testdata/NovaSeq6000/samplesheet_tag_collision.csv',
                tools_bundle: null,
                tools: 'fastqc',
            ]
        ],
    ]

    // Generate tests for each scenario
    test_scenario.each { scenario ->
        test(scenario.name, UTILS.getTest(scenario))
    }
}
