"""
This test checks that the autosubmit report command works as expected.
It is a regression test, so it is not run by default.
It only run within my home desktop computer. It is not run in the CI. Eventually it will be included TODO
Just to be sure that the autosubmitconfigparser work as expected if there are changes.
"""
import os
import subprocess
import re
from pathlib import Path
BIN_PATH = '../../bin'
VERSION = 4.0

def check_cmd(command, path=BIN_PATH):
    try:
        output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
        error = False
    except subprocess.CalledProcessError as e:
        output = e.output
        error = True
    return output, error

def run_test(expid):
    #check_cmd(f"rm -r /home/dbeltran/new_autosubmit/{expid}/tmp/LOG_{expid}/*")
    output = check_cmd(f"../../bin/autosubmit create {expid} -np -v -d;")
    return output
def perform_test(expids):
    to_exclude = []

    for expid in expids:
        try:
            output,error = run_test(expid)
            # output to str
            output = output.decode("UTF-8")
            output = output.split("Job list created successfully[0m[39m")[1]
            output = expid + output
            # put it in a single file
            with open(f"{VERSION}_multi_test.txt", "a") as myfile:
                myfile.write(output)
        except:
            to_exclude.append(expid)
    # print to_exclude in format ["a001","a002"]
    print(to_exclude)


open(f"{VERSION}_multi_test.txt", "w").close()

# list all experiments under ~/new_autosubmit.
# except the excluded ones, which are not run
expids = []
excluded = []
for experiment in os.listdir("/home/dbeltran/new_autosubmit"):
    if experiment.startswith("a") or experiment.startswith("t") and len(experiment) == 4:
        if experiment not in excluded:
            expids.append(experiment)
perform_test(expids)