#!/usr/bin/env bash set -euo pipefail mode="${1:-all}" selection="${2:-}" url="${ENELIX_SYMCON_URL:-http://127.0.0.1:3777/api/}" script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" repository_root="$(cd -- "$script_dir/../../.." && pwd)" output_dir="${ENELIX_TEST_OUTPUT_DIR:-$repository_root/build/symcon-tests}" case "$mode" in all|single|affected) ;; *) echo "Unbekannter Testmodus: $mode" >&2 exit 2 ;; esac encode_base64() { printf '%s' "$1" | base64 | tr -d '\r\n' } mode_base64="$(encode_base64 "$mode")" selection_base64="$(encode_base64 "$selection")" php_code="require_once IPS_GetKernelDir() . 'modules/Enelix-EMS/tests/Symcon/bootstrap.php'; " php_code+="\$mode = base64_decode('$mode_base64'); " php_code+="\$selection = array_values(array_filter(explode(',', base64_decode('$selection_base64')), " php_code+="static fn (string \$value): bool => \$value !== '')); " php_code+="\$result = enelixEmsRunSymconTests(\$mode, \$selection); " php_code+="echo implode(':', [" php_code+="base64_encode(json_encode(\$result, JSON_THROW_ON_ERROR)), " php_code+="base64_encode(json_encode(\$result['report'], JSON_THROW_ON_ERROR)), " php_code+="base64_encode(\$result['junit']), " php_code+="base64_encode(\$result['console']), " php_code+="\$result['report']['passed'] ? '1' : '0'" php_code+="]);" php_code_base64="$(encode_base64 "$php_code")" rpc_code="eval(base64_decode('$php_code_base64'));" payload="$(printf '{"jsonrpc":"2.0","id":1,"method":"IPS_RunScriptTextWaitEx","params":["%s",{}]}' "$rpc_code")" mkdir -p "$output_dir" response_file="$output_dir/rpc-response.json" result_file="$output_dir/result.json" curl --silent --show-error --fail \ --header 'Content-Type: application/json' \ --data "$payload" \ "$url" > "$response_file" if grep -q '"error"' "$response_file"; then sed -n '1,120p' "$response_file" >&2 exit 1 fi wire="$(sed -n 's/^.*"result":"\([^"]*\)".*$/\1/p' "$response_file" | sed 's#\\/#/#g')" if [[ -z "$wire" ]]; then echo 'IP-Symcon lieferte keine auswertbaren Testdaten:' >&2 sed -n '1,120p' "$response_file" >&2 exit 1 fi IFS=: read -r result_base64 report_base64 junit_base64 console_base64 passed extra <<< "$wire" if [[ -n "${extra:-}" || -z "$result_base64" || -z "$report_base64" || -z "$junit_base64" ]]; then echo 'IP-Symcon lieferte ein ungültiges Testergebnis.' >&2 exit 1 fi printf '%s' "$result_base64" | base64 -d > "$result_file" printf '%s' "$report_base64" | base64 -d > "$output_dir/report.json" printf '%s' "$junit_base64" | base64 -d > "$output_dir/junit.xml" printf '%s' "$console_base64" | base64 -d [[ "$passed" == 1 ]]