# Copyright (c) 2026 Huawei Device Co., Ltd.
# 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.

import("//arkcompiler/ets_frontend/ets_frontend_config.gni")
import("$build_root/test.gni")

if ((defined(ark_standalone_build) && ark_standalone_build) ||
    (defined(is_arkui_x) && is_arkui_x)) {
  import("//arkcompiler/runtime_core/ark_config.gni")
} else {
  import("//arkcompiler/ets_frontend/ark_config.gni")
}

template("host_unittest_action") {
  _target_name_ = "${target_name}"

  ohos_unittest(_target_name_) {
    forward_variables_from(invoker, "*")
  }

  _module_out_path_ = invoker.module_out_path

  action("${_target_name_}_action") {
    testonly = true

    defines = [ "HOST_UT" ]

    _host_test_target_ = ":${_target_name_}(${toolchain_linux})"
    _root_out_dir_ = get_label_info(_host_test_target_, "root_out_dir")

    deps = [ _host_test_target_ ]

    script = "//arkcompiler/ets_runtime/script/run_ark_executable.py"

    args = [
      "--script-file",
      rebase_path(_root_out_dir_) +
          "/tests/unittest/${_module_out_path_}/${_target_name_}",
      "--expect-output",
      "0",
      "--env-path",
      rebase_path(_root_out_dir_) + "/arkcompiler/runtime_core:" +
          rebase_path(_root_out_dir_) + "/thirdparty/zlib:",
      "--timeout-limit",
      "1200",
    ]

    inputs = [
      "$_root_out_dir_/tests/unittest/${_module_out_path_}/${_target_name_}",
    ]
    outputs = [ "$target_out_dir/${_target_name_}/" ]
  }
}

# Template for es2panda and merge_abc frontend unit tests
template("es2panda_frontend_unittest") {
  host_unittest_action(target_name) {
    use_exceptions = true
    testonly = true
    sources = invoker.sources

    configs = [
      "//arkcompiler/ets_frontend/es2panda:es2abc_config_common",
      "//arkcompiler/ets_frontend/merge_abc:panda_assembly_proto_public_config",
    ]
    if (defined(invoker.configs)) {
      configs += invoker.configs
    }

    include_dirs = [
      "//arkcompiler/ets_frontend/es2panda",
      "${target_out_dir}",
    ]
    if (defined(invoker.include_dirs)) {
      include_dirs += invoker.include_dirs
    }

    deps = [
      "//arkcompiler/ets_frontend/es2panda:es2panda_lib",
      "//arkcompiler/ets_frontend/merge_abc:panda_assembly_proto_static",
    ]
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }

    external_deps = [
      "googletest:gtest_main",
    ]

    external_deps += [ sdk_libc_secshared_dep ]

    external_deps += [
      "icu:static_icuuc",
      "runtime_core:arkabc2program_public_headers",
      "runtime_core:arkassembler_public_headers",
      "runtime_core:arkbase_public_headers",
      "runtime_core:arkfile_public_headers",
    ]

    external_deps += [
      "json:nlohmann_json_static",
      "runtime_core:abc2program_frontend_static",
      "runtime_core:libarkassembler_frontend_static",
      "runtime_core:libarkbase_frontend_static",
      "runtime_core:libarkfile_frontend_static",
      "runtime_core:libarkziparchive_frontend_static",
    ]

    module_out_path = "arkcompiler/ets_frontend"
  }
}