.assign_test_template:
  image: $CI_DOCKER_REGISTRY/ubuntu-test-env$BOT_DOCKER_IMAGE_TAG
  stage: assign_test
  tags:
    - assign_test
  variables:
    SUBMODULES_TO_FETCH: components/esptool_py/esptool
  artifacts:
    paths:
      - ${TEST_DIR}/test_configs
      - ${BUILD_DIR}/artifact_index.json
    when: always
    expire_in: 1 week
  script:
    - python tools/ci/python_packages/ttfw_idf/IDFAssignTest.py $TEST_TYPE $TEST_DIR -c $CI_TARGET_TEST_CONFIG_FILE -o $TEST_DIR/test_configs

assign_example_test:
  extends:
    - .assign_test_template
    - .rules:build:example_test
  needs:
    - job: build_examples_cmake_esp32
      artifacts: false
      optional: true
    - job: build_examples_cmake_esp32s2
      artifacts: false
      optional: true
    - job: build_examples_cmake_esp32c3
      artifacts: false
      optional: true
    - job: build_examples_cmake_esp32s3
      artifacts: false
      optional: true
  variables:
    TEST_TYPE: example_test
    TEST_DIR: ${CI_PROJECT_DIR}/examples
    BUILD_DIR: ${CI_PROJECT_DIR}/build_examples

assign_custom_test:
  extends:
    - .assign_test_template
    - .rules:build:custom_test
  needs:
    - job: build_test_apps_esp32
      artifacts: false
      optional: true
    - job: build_test_apps_esp32s2
      artifacts: false
      optional: true
    - job: build_test_apps_esp32c3
      artifacts: false
      optional: true
    - job: build_test_apps_esp32s3
      artifacts: false
      optional: true
  variables:
    TEST_TYPE: custom_test
    TEST_DIR: ${CI_PROJECT_DIR}/tools/test_apps
    BUILD_DIR: ${CI_PROJECT_DIR}/build_test_apps

assign_component_ut:
  extends:
    - .assign_test_template
    - .rules:build:component_ut
  needs:
    - job: build_component_ut_esp32
      artifacts: false
      optional: true
    - job: build_component_ut_esp32s2
      artifacts: false
      optional: true
    - job: build_component_ut_esp32c3
      artifacts: false
      optional: true
    - job: build_component_ut_esp32s3
      artifacts: false
      optional: true
  variables:
    TEST_TYPE: component_ut
    TEST_DIR: ${CI_PROJECT_DIR}/component_ut
    BUILD_DIR: ${CI_PROJECT_DIR}/build_component_ut
  script:
    # COMPONENT_UT_DIRS is set by `set_component_ut_vars` in `utils.sh`
    - set_component_ut_vars
    - python tools/ci/python_packages/ttfw_idf/IDFAssignTest.py $TEST_TYPE $COMPONENT_UT_DIRS -c $CI_TARGET_TEST_CONFIG_FILE -o $TEST_DIR/test_configs

assign_unit_test:
  extends:
    - .assign_test_template
    - .rules:build:unit_test
  needs:
    - job: build_esp_idf_tests_cmake_esp32
      optional: true
    - job: build_esp_idf_tests_cmake_esp32s2
      optional: true
    - job: build_esp_idf_tests_cmake_esp32c3
      optional: true
    - job: build_esp_idf_tests_cmake_esp32s3
      optional: true
  variables:
    TEST_TYPE: unit_test
    TEST_DIR: ${CI_PROJECT_DIR}/components/idf_test/unit_test
    BUILD_DIR: ${CI_PROJECT_DIR}/tools/unit-test-app/builds
  script:
    - python tools/ci/python_packages/ttfw_idf/IDFAssignTest.py $TEST_TYPE $TEST_DIR -c $CI_TARGET_TEST_CONFIG_FILE -o $TEST_DIR/test_configs

assign_integration_test:
  extends:
    - .assign_test_template
    - .rules:test:integration_test
  needs:
    - build_ssc_esp32
    - build_ssc_esp32c3
  artifacts:
    paths:
      - $TEST_DIR/test_configs
  variables:
    TEST_DIR: ${CI_PROJECT_DIR}/tools/ci/integration_test
    BUILD_DIR: ${CI_PROJECT_DIR}/SSC/ssc_bin
    INTEGRATION_TEST_CASE_PATH: "${CI_PROJECT_DIR}/auto_test_script/TestCaseFiles"
    ASSIGN_TEST_CASE_SCRIPT: "${CI_PROJECT_DIR}/auto_test_script/bin/CIAssignTestCases.py"
    PYTHONPATH: ${CI_PROJECT_DIR}/auto_test_script/packages
    PYTHON_VER: 3.7.7
  script:
    # clone test script to assign tests
    # can not retry if downing git lfs files failed, so using empty_branch first.
    - retry_failed git clone ${CI_AUTO_TEST_SCRIPT_REPO_URL} -b empty_branch
    - retry_failed git -C auto_test_script checkout -f ${CI_AUTO_TEST_SCRIPT_REPO_BRANCH}
    - python $CHECKOUT_REF_SCRIPT auto_test_script auto_test_script --customized_only
    # assign integration test cases
    - python ${ASSIGN_TEST_CASE_SCRIPT} -t ${INTEGRATION_TEST_CASE_PATH} -c $CI_TARGET_TEST_CONFIG_FILE -b ${BUILD_DIR} -o $TEST_DIR/test_configs

update_test_cases:
  extends: .rules:ref:master-schedule
  stage: assign_test
  image: $CI_DOCKER_REGISTRY/ubuntu-test-env
  tags:
    - deploy_test
  needs:
    - build_esp_idf_tests_cmake_esp32
    - build_esp_idf_tests_cmake_esp32s2
    - build_esp_idf_tests_cmake_esp32s3
    - build_esp_idf_tests_cmake_esp32c3
  artifacts:
    when: always
    paths:
      - ${CI_PROJECT_DIR}/test-management/*.log
    expire_in: 1 week
  variables:
    SUBMODULES_TO_FETCH: "components/esptool_py/esptool"
    UNIT_TEST_DIR: "${CI_PROJECT_DIR}/components/idf_test/unit_test"
    BOT_ACCOUNT_CONFIG_FILE: "${CI_PROJECT_DIR}/test-management/Config/Account.local.yml"
    PYTHONPATH: ${CI_PROJECT_DIR}/auto_test_script/packages
    PYTHON_VER: 3.7.7
  script:
    - export GIT_SHA=$(echo ${PIPELINE_COMMIT_SHA} | cut -c 1-8)
    - retry_failed git clone $TEST_MANAGEMENT_REPO
    - python $CHECKOUT_REF_SCRIPT test-management test-management
    - cd test-management
    - echo $BOT_JIRA_ACCOUNT > ${BOT_ACCOUNT_CONFIG_FILE}
    # update unit test cases
    - export UNIT_TEST_CASE_FILES=$(find $UNIT_TEST_DIR -maxdepth 1 -name "*.yml" | xargs)
    - python ImportTestCase.py $JIRA_TEST_MANAGEMENT_PROJECT unity -d $UNIT_TEST_CASE_FILES -r $GIT_SHA
    # update example test cases
    - python ImportTestCase.py $JIRA_TEST_MANAGEMENT_PROJECT tiny_test_fw -d ${CI_PROJECT_DIR}/examples -r $GIT_SHA
    - python ImportTestCase.py $JIRA_TEST_MANAGEMENT_PROJECT tiny_test_fw -d ${CI_PROJECT_DIR}/tools/test_apps -r $GIT_SHA
    # organize test cases
    - python OrganizeTestCases.py $JIRA_TEST_MANAGEMENT_PROJECT
