# 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.

# Black-box end-to-end tests for gluegen: unlike test/native/unit/ (which links gluegen's sources
# directly into a gtest binary and calls Gluegen::Run() in-process), these tests spawn the actual
# built `gluegen` executable as a subprocess -- the only way to observe main.cpp's CLI parsing and
# process exit code, which is exactly what the negative-cases suite needs to check.
#
# Wired up the same way ets2panda/test/tsconfig/test-decl/CMakeLists.txt drives its own
# process-level tests: a plain `add_custom_target` (rather than ctest's `add_test`) that `DEPENDS`
# on the binary under test, so a single `ninja gluegen_e2e_tests` both builds `gluegen` and runs
# the suite -- no separate `ninja gluegen` + `ctest -R ...` dance needed.
if(NOT PANDA_WITH_TESTS)
    return()
endif()

find_program(NODE_EXECUTABLE NAMES node)
if(NOT NODE_EXECUTABLE)
    message(WARNING "gluegen e2e tests skipped: 'node' executable not found (required to run test/native/e2e/runner)")
    return()
endif()

set(GLUEGEN_E2E_DIR ${CMAKE_CURRENT_SOURCE_DIR})

# Generates a dynamic-re-exports-specific arktsconfig.json at cmake configure
# time, *before* the gluegen_e2e_tests target runs.  Wrapped in a function so
# the logic is self-contained and reusable; the generated file is registered as
# a dependency of a phony custom target so `ninja gluegen_e2e_tests` (or any
# target that transitively depends on it) picks up the correct file.
#
# Parameters:
#   TARGET_NAME        - phony custom-target name that "owns" the output
#   OUTPUT_FILE        - absolute path where arktsconfig.json is written
function(gen_e2e_arktsconfig TARGET_NAME OUTPUT_FILE)
    file(WRITE "${OUTPUT_FILE}"
"{
  \"!NOTE!\": \"Generated by gen_e2e_arktsconfig() in CMakeLists.txt\",
  \"compilerOptions\": {
    \"baseUrl\": \"${GLUEGEN_E2E_DIR}\",
    \"paths\": {
    },
    \"dependencies\": {
      \"dyn\": { \"language\": \"js\", \"ohmUrl\": \"dyn\", \"path\": \"${CMAKE_CURRENT_SOURCE_DIR}/prefabs/dynamic-modules/dyn/entry.d.ets\", \"sourceFilePath\": \"${CMAKE_CURRENT_SOURCE_DIR}/prefabs/dynamic-modules/dyn/entry.d.ets\" },
      \"dyn/entry\": {\"language\": \"js\", \"path\": \"${CMAKE_CURRENT_SOURCE_DIR}/prefabs/dynamic-modules/dyn/entry.d.ets\", \"ohmUrl\": \"@normalized:N&&&dyn/entry\", \"sourceFilePath\": \"${CMAKE_CURRENT_SOURCE_DIR}/prefabs/dynamic-modules/dyn/entry.d.ets\" }
    }
  }
}
")
    # Phony target so gluegen_e2e_tests can express DEPENDS on this file.
    add_custom_target(${TARGET_NAME} DEPENDS "${OUTPUT_FILE}")
endfunction()

# Reuses the same repo-wide, stdlib-backed arktsconfig.json fixture that
# test/native/unit/gluegen_test.cpp's ArktsConfigPath() relies on (generated by ets2panda's own
# CMakeLists.txt's `gen_arktsconfig(...)` into bin-gtests/, alongside every gtest binary). Unlike
# the `gluegen` executable itself, this fixture is written at `cmake` configure time (not built by
# ninja), so it already exists by the time this target runs.
set(GLUEGEN_E2E_DEFAULT_ARKTSCONFIG ${PANDA_BINARY_ROOT}/gluegen_e2e_tests/arktsconfig.json)

gen_e2e_arktsconfig(
    gluegen_e2e_dynamic_arktsconfig
    "${GLUEGEN_E2E_DEFAULT_ARKTSCONFIG}"
)

add_custom_target(gluegen_e2e_tests
    COMMENT "Running gluegen e2e tests"
    DEPENDS gluegen gluegen_e2e_dynamic_arktsconfig
    WORKING_DIRECTORY ${GLUEGEN_E2E_DIR}
    COMMAND ${NODE_EXECUTABLE} ${GLUEGEN_E2E_DIR}/runner/run-e2e.js
        --gluegen-binary $<TARGET_FILE:gluegen>
        --default-arktsconfig ${GLUEGEN_E2E_DEFAULT_ARKTSCONFIG}
        --positive-cases-dir ${GLUEGEN_E2E_DIR}/positive-cases
        --negative-cases-dir ${GLUEGEN_E2E_DIR}/negative-cases
        --run-prefix "${PANDA_RUN_PREFIX}"
)
