#!/usr/bin/env bash
. "$(dirname "${BASH_SOURCE[0]}")/lib/harness.sh"
set -eo pipefail

# A component action prints its JSON result to stdout and logs to syslog, but the
# worker used to redirect stderr into the same file with 2>&1. Anything a child
# wrote to stderr after the result - apk warnings, init-script chatter from the
# restart that a Tachyon self-update performs on itself - landed after the JSON.
# The whole-file parse then failed, the line scan found no object, and a
# successful action was reported to the UI as a failure whose message was the
# tail of a log ("Failed to execute").

UPDATES_UC="$ROOT_DIR/tachyon/files/usr/lib/components/updates.uc"
ACTION_UC="$ROOT_DIR/tachyon/files/usr/lib/components/action.uc"
VERSIONS_UC="$ROOT_DIR/tachyon/files/usr/lib/components/versions.uc"
HELPERS_UC="$ROOT_DIR/tachyon/files/usr/lib/components/helpers.uc"
TACHYON_CORE_RUNTIME_UC="$ROOT_DIR/tachyon/files/usr/lib/tachyon_core/runtime.uc"

# Comments mention 2>&1 to explain the bug; only real redirections count.
code_only() {
  sed 's://.*::'
}

worker="$(sed -n '/^function component_action_worker/,/^}/p' "$UPDATES_UC" | code_only)"
[ -n "$worker" ] || fail "components/updates.uc must define component_action_worker"

if grep -Fq '2>&1' <<<"$worker"; then
  fail "component_action_worker must not merge stderr into the result file; stray stderr corrupts the JSON the UI parses"
fi
grep -Fq '".stderr"' <<<"$worker" ||
  fail "component_action_worker must redirect stderr to a separate .stderr file"

# The automatic check runs the same action module and parses the same way.
auto="$(sed -n '/^function run_automatic_component_update_check/,/^}/p' "$UPDATES_UC" | code_only)"
[ -n "$auto" ] || fail "components/updates.uc must define run_automatic_component_update_check"
if grep -Fq '2>&1' <<<"$auto"; then
  fail "run_automatic_component_update_check must not merge stderr into the file it json-parses"
fi

# The stderr file must be cleaned up with the rest of the job, and it is the
# better source for a failure message than the (empty) result file.
finish="$(sed -n '/^function finish_component_job/,/^}/p' "$UPDATES_UC" | code_only)"
[ -n "$finish" ] || fail "components/updates.uc must define finish_component_job"
grep -Fq 'remove_file(output_file + ".stderr")' <<<"$finish" ||
  fail "finish_component_job must remove the worker .stderr file"
grep -Fq 'output_file + ".stderr"' <<<"$finish" ||
  fail "finish_component_job should report the worker stderr as the failure message"

# A worker killed mid-run leaves its .stderr behind; the orphan sweep must take
# it too, or the files pile up in tmpfs.
grep -Fq 'remove_file(output_path + ".stderr")' "$UPDATES_UC" ||
  fail "the component job sweep must remove orphaned .stderr files"
grep -Fq '"*.out.stderr"' "$UPDATES_UC" ||
  fail "the age-based orphan cleanup must cover *.out.stderr"

# --- validation must judge tachyon-core by its output, not its exit status ----
#
# `tachyon-core version` prints the version and can still exit non-zero or be
# signalled by the service stop that precedes the install. command_output()
# discards stdout whenever the raw wait status is non-zero, so validation saw an
# empty string, declared a working binary broken and rolled the install back with
# exit_code 129 (128+SIGHUP).

probe="$(sed -n '/^function read_core_binary_version/,/^}/p' "$VERSIONS_UC" | code_only)"
[ -n "$probe" ] || fail "components/versions.uc must define read_core_binary_version"
grep -Fq 'command_output_lenient(' <<<"$probe" ||
  fail "read_core_binary_version must keep stdout regardless of exit status"
if grep -Eq '[^_]command_output\(' <<<"$probe"; then
  fail "read_core_binary_version must not use the status-gated command_output"
fi

version_output="$(sed -n '/^function core_version_output/,/^}/p' "$TACHYON_CORE_RUNTIME_UC" | code_only)"
[ -n "$version_output" ] || fail "tachyon_core/runtime.uc must define core_version_output"
grep -Fq 'command_output_lenient(' <<<"$version_output" ||
  fail "core_version_output must keep stdout regardless of exit status"

# ucode does not hoist: the helper must be declared before every call site.
# Within the split module layout the declaration lives in components/helpers.uc
# and consumers bind it via require, so only files that own a copy are checked.
for file in "$TACHYON_CORE_RUNTIME_UC" "$HELPERS_UC"; do
  decl="$(grep -n '^function command_output_lenient' "$file" | head -1 | cut -d: -f1)"
  [ -n "$decl" ] || fail "$file must declare command_output_lenient"
  # Every mention that is not the declaration itself is a call site. The
  # no-matches case (declare-only file) must not trip pipefail.
  first_use="$(sed 's://.*::' "$file" | { grep -n 'command_output_lenient(' || true; } |
    { grep -v ':function ' || true; } | head -1 | cut -d: -f1 || true)"
  [ -n "$first_use" ] || continue
  [ "$decl" -lt "$first_use" ] ||
    fail "$file declares command_output_lenient at line $decl but calls it at $first_use; ucode does not hoist"
done

# --- behavioural: the helper really survives a non-zero exit ------------------

if command -v ucode >/dev/null 2>&1; then
  WORK_DIR="$(mktemp -d)"
  trap 'rm -rf "$WORK_DIR"' EXIT

  cat >"$WORK_DIR/probe.uc" <<'UC'
import * as fs from "fs";

function command_output(command) {
    let pipe = fs.popen(command, "r");
    if (!pipe)
        return "";
    let data = pipe.read("all");
    let status = pipe.close();
    if (status != 0 || data == null)
        return "";
    return "" + data;
}

function command_output_lenient(command) {
    let pipe = fs.popen(command, "r");
    if (!pipe)
        return "";
    let data = pipe.read("all");
    pipe.close();
    return data != null ? "" + data : "";
}

let cmd = "printf 'tachyon-core version 0.4.5\\n'; exit 1";
print("strict=[" + trim(command_output(cmd)) + "]\n");
print("lenient=[" + trim(command_output_lenient(cmd)) + "]\n");
UC

  out="$(ucode -R "$WORK_DIR/probe.uc")"
  grep -Fq 'strict=[]' <<<"$out" ||
    fail "sanity: status-gated command_output was expected to drop the output, got: $out"
  grep -Fq 'lenient=[tachyon-core version 0.4.5]' <<<"$out" ||
    fail "command_output_lenient must return stdout of a command that exits non-zero, got: $out"
fi

printf 'component worker output channel checks passed\n'
