# Copyright (c) 2024 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_runtime/js_runtime_config.gni")
import("//arkcompiler/ets_runtime/test/test_helper.gni")

module_output_path = "ets_runtime/ets_runtime"

test_js_path = "//arkcompiler/ets_runtime/ecmascript/dfx/stackinfo/tests/js_files/"

test_js_files = [
  "index",
]

foreach(file, test_js_files) {
  es2abc_gen_abc("gen_${file}_abc") {
    test_js = "${test_js_path}${file}.js"
    test_abc = "$target_out_dir/abc_files/${file}.abc"
    # Only targets in this file can depend on this.
    extra_visibility = [ ":*" ]
    src_js = rebase_path(test_js)
    dst_file = rebase_path(test_abc)
    extra_args = [
      "--module",
      "--merge-abc",
      "--debug",
    ]

    in_puts = [ test_js ]
    out_puts = [ test_abc ]
  }
}

host_unittest_action("JsStackInfoTest") {
  module_out_path = module_output_path

  sources = [
    # test file
    "js_stackinfo_test.cpp",
    "js_stackinfo_test2.cpp",
  ]

  configs = [
    "../../../../:ecma_test_config",
  ]

  if (ark_standalone_build || is_arkui_x) {
    configs += [
      "$ark_root/assembler:arkassembler_public_config",
      "$ark_root/libpandafile:arkfile_public_config",
    ]
  }

  deps = [
    "../../../../:libark_jsruntime_test",
  ]
  foreach(file, test_js_files) {
    deps += [ ":gen_${file}_abc" ]
  }

  if (is_ohos && is_standard_system) {
    test_abc_dir = "/data/test/"
  } else {
    test_abc_dir = rebase_path(target_out_dir + "/abc_files/")
  }

  test_js_dir = rebase_path(test_js_path)
  defines = [
    "STACKINFO_TEST_JS_FILES_DIR=\"${test_js_dir}\"",
    "STACKINFO_TEST_ABC_FILES_DIR=\"${test_abc_dir}\"",
  ]

  # hiviewdfx libraries
  external_deps = hiviewdfx_ext_deps
  external_deps += [
    "icu:shared_icui18n",
    "icu:shared_icuuc",
    "zlib:libz",
    "runtime_core:libarkassembler_static",
  ]
  if (enable_static_backtrace) {
    external_deps += [
      "runtime_core:arkbacktrace",
      "runtime_core:libarktsbase",
      "runtime_core:libarkruntime",
    ]
  }
  if (!ark_standalone_build && !is_arkui_x) {
    external_deps += [ 
      "runtime_core:arkassembler_public_headers",
      "runtime_core:arkfile_public_headers",
    ]
  }
  deps += hiviewdfx_deps
}

async_stack_test_js_files = [
  "async_await",
  "multi_async_await",
  "promise_then",
  "async_await_promise_then",
]

foreach(file, async_stack_test_js_files) {
  es2abc_gen_abc("gen_${file}_abc") {
    test_js = "${test_js_path}${file}.js"
    test_abc = "$target_out_dir/abc_files/${file}.abc"
    # Only targets in this file can depend on this.
    extra_visibility = [ ":*" ]
    src_js = rebase_path(test_js)
    dst_file = rebase_path(test_abc)
    extra_args = [
      "--module",
      "--merge-abc",
      "--debug",
    ]

    in_puts = [ test_js ]
    out_puts = [ test_abc ]
  }
}

async_stack_test_js_file_dirs = [
  "dynamic_import",
]

foreach(_target_name_, async_stack_test_js_file_dirs) {
  merge_file = "$target_out_dir/${_target_name_}.txt"
  merge_file_prefix = "${test_js_path}${_target_name_}/"
  _merge_file_raw_ = "${test_js_path}${_target_name_}/${_target_name_}.txt"

  action("gen_${_target_name_}_merge_file") {
    script = "//arkcompiler/ets_runtime/test/quickfix/generate_merge_file.py"
    args = [
      "--input",
      rebase_path(_merge_file_raw_),
      "--output",
      rebase_path(merge_file),
      "--prefix",
      rebase_path(merge_file_prefix),
    ]

    inputs = [ _merge_file_raw_ ]
    outputs = [ merge_file ]
  }

  abc_path = "$target_out_dir/abc_files/${_target_name_}.abc"

  es2abc_gen_abc("gen_${_target_name_}_abc") {
    extra_visibility = [ ":*" ]  # Only targets in this file can depend on this.
    extra_dependencies = [ ":gen_${_target_name_}_merge_file" ]
    src_js = "@" + rebase_path(merge_file)
    dst_file = rebase_path(abc_path)
    extra_args = [
      "--module",
      "--merge-abc",
    ]

    in_puts = [ merge_file ]
    out_puts = [ abc_path ]
  }
}

host_unittest_action("AsyncStackTest") {
  module_out_path = module_output_path

  sources = [
  # test file
    "async_stack_test.cpp",
  ]

  configs = [
    "../../../../:ecma_test_config",
  ]

  if (ark_standalone_build || is_arkui_x) {
    configs += [
      "$ark_root/assembler:arkassembler_public_config",
      "$ark_root/libpandafile:arkfile_public_config",
    ]
  }

  deps = [
    "../../../../:libark_jsruntime_test",
  ]
  foreach(file, async_stack_test_js_files) {
    deps += [ ":gen_${file}_abc" ]
  }
  foreach(_target_name_, async_stack_test_js_file_dirs) {
    deps += [ ":gen_${_target_name_}_abc" ]
  }

  if (is_ohos && is_standard_system) {
    test_abc_dir = "/data/test/"
  } else {
    test_abc_dir = rebase_path(target_out_dir + "/abc_files/")
  }

  test_js_dir = rebase_path(test_js_path)
  defines = [
    "STACKINFO_TEST_JS_FILES_DIR=\"${test_js_dir}\"",
    "STACKINFO_TEST_ABC_FILES_DIR=\"${test_abc_dir}\"",
  ]

  # hiviewdfx libraries
  external_deps = hiviewdfx_ext_deps
  external_deps += [
    "icu:shared_icui18n",
    "icu:shared_icuuc",
    "zlib:libz",
    "runtime_core:libarkassembler_static",
  ]
  if (!ark_standalone_build && !is_arkui_x) {
    external_deps += [
      "runtime_core:arkassembler_public_headers",
      "runtime_core:arkfile_public_headers",
    ]
  }
  if (enable_async_stack) {
    external_deps += [ "faultloggerd:libasync_stack" ]
  }
  deps += hiviewdfx_deps
}

group("unittest") {
  testonly = true
  deps = [ ":JsStackInfoTest",
           ":AsyncStackTest", ]
}

group("host_unittest") {
  testonly = true
  deps = [ ":JsStackInfoTestAction",
           ":AsyncStackTestAction", ]
  if (is_mac) {
    deps -= [ ":JsStackInfoTestAction",
              ":AsyncStackTestAction", ]
  }
}