From 7ff76cfbde7e1f8cb82437e367d31fa460ec8232 Mon Sep 17 00:00:00 2001 From: tsosunchia <59512455+tsosunchia@users.noreply.github.com> Date: Wed, 8 Apr 2026 19:00:50 +0800 Subject: [PATCH] Rewrite one-key installer for flavors and add smoke tests --- .github/workflows/test.yml | 12 + README.md | 21 +- README_zh_CN.md | 16 +- nt_install.sh | 484 +++++++++++++++++++++++++++++-------- scripts/test_nt_install.sh | 232 ++++++++++++++++++ 5 files changed, 662 insertions(+), 103 deletions(-) create mode 100644 scripts/test_nt_install.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 04a9863..dfe3cad 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,6 +15,8 @@ on: - "**/*.go" - "go.mod" - "go.sum" + - "nt_install.sh" + - "scripts/test_nt_install.sh" - ".github/workflows/*.yml" pull_request: types: [opened, synchronize, reopened] @@ -22,6 +24,8 @@ on: - "**/*.go" - "go.mod" - "go.sum" + - "nt_install.sh" + - "scripts/test_nt_install.sh" - ".github/workflows/*.yml" workflow_dispatch: @@ -53,6 +57,14 @@ jobs: sudo go env -w GOTOOLCHAIN=go1.26.0+auto sudo go test -v -covermode=count -coverprofile='coverage.out' ./... + - name: Smoke test nt_install.sh + if: ${{ matrix.os != 'windows-latest' }} + shell: bash + run: | + bash -n nt_install.sh + sh -n nt_install.sh + bash scripts/test_nt_install.sh + - name: Test with windows if: ${{ matrix.os == 'windows-latest' }} run: | diff --git a/README.md b/README.md index da24616..d676e63 100644 --- a/README.md +++ b/README.md @@ -55,13 +55,30 @@ Please note, there are exceptions to this synchronization. If a version of NTrac ### Automated Install -- Linux - - One-click installation script +- Linux / macOS / BSD + - One-click installation script (Full, default) ```shell curl -sL https://nxtrace.org/nt | bash ``` + - One-click installation script (Tiny) + + ```shell + curl -sL https://nxtrace.org/nt | bash -s -- --flavor tiny + ``` + + - One-click installation script (NTR) + + ```shell + curl -sL https://nxtrace.org/nt | bash -s -- --flavor ntr + ``` + + - Installed command names: + - Full: `nexttrace` + - Tiny: `nexttrace-tiny` + - NTR: `ntr` + - Install nxtrace from the APT repository - Supports AMD64/ARM64 architectures ```shell diff --git a/README_zh_CN.md b/README_zh_CN.md index 73c7df8..c1b3827 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -59,11 +59,23 @@ Document Language: [English](README.md) | 简体中文 ### Automated Install -- Linux - - 一键安装脚本 +- Linux / macOS / BSD + - 一键安装脚本(完整版,默认) ```shell curl -sL https://nxtrace.org/nt | bash ``` + - 一键安装脚本(Tiny) + ```shell + curl -sL https://nxtrace.org/nt | bash -s -- --flavor tiny + ``` + - 一键安装脚本(NTR) + ```shell + curl -sL https://nxtrace.org/nt | bash -s -- --flavor ntr + ``` + - 安装后的命令名: + - Full:`nexttrace` + - Tiny:`nexttrace-tiny` + - NTR:`ntr` - 从 nxtrace的APT源安装 - 支持 AMD64/ARM64 架构 ```shell diff --git a/nt_install.sh b/nt_install.sh index a33894c..d66cef1 100644 --- a/nt_install.sh +++ b/nt_install.sh @@ -1,126 +1,412 @@ -#!/bin/bash +#!/bin/sh -if [ "$1" = "http" ]; then - protocol="http" -else - protocol="https" -fi +# This file is also served at https://nxtrace.org/nt . +# Keep README one-key install snippets in sync with this script. +set -eu -Green_font="\033[32m" -Yellow_font="\033[33m" -Red_font="\033[31m" -Font_suffix="\033[0m" -Info="${Green_font}[Info]${Font_suffix}" -Error="${Red_font}[Error]${Font_suffix}" -Tips="${Green_font}[Tips]${Font_suffix}" -Temp_path="/var/tmp/nexttrace" +API_HOST="www.nxtrace.org" +API_PATH="/api/dist/core" +GITHUB_RELEASE_BASE="https://github.com/nxtrace/NTrace-dev/releases/latest/download" +DEFAULT_PROTOCOL="https" +DEFAULT_FLAVOR="full" -checkRootPermit() { - [[ $EUID -ne 0 ]] && echo -e "${Error} 请使用sudo/root权限运行本脚本" && exit 1 +SYSTEM_BIN_DIR_DEFAULT="/usr/local/bin" +USER_BIN_DIR_DEFAULT="${HOME:-}/.local/bin" + +PROTOCOL="${DEFAULT_PROTOCOL}" +FLAVOR="${DEFAULT_FLAVOR}" +INSTALL_MODE="auto" +BIN_DIR="" +ARCH_OVERRIDE="" +USE_SOFTFLOAT=0 +DOWNLOADER="" +BIN_NAME="" +TARGET_OS="" +TARGET_ARCH="" +ASSET_NAME="" +INSTALL_PATH="" +TEMP_FILE="" + +SYSTEM_BIN_DIR="${NT_INSTALL_SYSTEM_BIN_DIR:-${SYSTEM_BIN_DIR_DEFAULT}}" +USER_BIN_DIR="${NT_INSTALL_USER_BIN_DIR:-${USER_BIN_DIR_DEFAULT}}" + +info() { + printf '%s\n' "==> $*" } -checkSystemArch() { - arch=$(uname -m) - if [[ $arch == "x86_64" ]]; then - archParam="amd64" - elif [[ $arch == "i386" ]]; then - archParam="386" - elif [[ $arch == "i686" ]]; then - archParam="386" - elif [[ $arch == "aarch64" ]]; then - archParam="arm64" - elif [[ $arch == "armv7l" ]] || [[ $arch == "armv7ml" ]]; then - archParam="armv7" - elif [[ $arch == "mips" ]]; then - archParam="mips" - elif [[ $arch == "loongarch64" ]]; then - archParam="loong64" - fi +warn() { + printf '%s\n' "warning: $*" >&2 } -checkSystemDistribution() { - case "$OSTYPE" in - linux*) - osDistribution="linux" - - if [ ! -d "/usr/local" ]; - then - downPath="/usr/bin/nexttrace" - else - downPath="/usr/local/bin/nexttrace" - fi - - ;; - *) - echo "unknown: $OSTYPE" +die() { + printf '%s\n' "error: $*" >&2 exit 1 - ;; +} + +cleanup() { + if [ -n "${TEMP_FILE}" ] && [ -f "${TEMP_FILE}" ]; then + rm -f "${TEMP_FILE}" >/dev/null 2>&1 || true + fi +} + +trap cleanup EXIT INT TERM + +print_help() { + cat <<'EOF' +NextTrace one-key installer + +Usage: + curl -sL https://nxtrace.org/nt | bash + curl -sL https://nxtrace.org/nt | bash -s -- --flavor tiny + curl -sL https://nxtrace.org/nt | bash -s -- --flavor ntr + +Options: + --flavor full|tiny|ntr Install the selected release flavor + --user Install to ~/.local/bin + --system Install to /usr/local/bin + --bin-dir Install to a specific directory + --arch Override detected release arch + --softfloat Use _softfloat asset for linux mips/mipsle + --help Show this help message + +Compatibility: + http Legacy compatibility flag. Use http for the API endpoint. +EOF +} + +command_exists() { + command -v "$1" >/dev/null 2>&1 +} + +normalize_arch() { + case "$1" in + amd64|x86_64) printf '%s\n' "amd64" ;; + 386|i386|i486|i586|i686) printf '%s\n' "386" ;; + arm64|aarch64) printf '%s\n' "arm64" ;; + armv5|armv5l|armv5tel) printf '%s\n' "armv5" ;; + armv6|armv6l) printf '%s\n' "armv6" ;; + armv7|armv7l|armv7ml|armv8l) printf '%s\n' "armv7" ;; + mips) printf '%s\n' "mips" ;; + mipsel|mipsle) printf '%s\n' "mipsle" ;; + mips64) printf '%s\n' "mips64" ;; + mips64el|mips64le) printf '%s\n' "mips64le" ;; + loongarch64|loong64) printf '%s\n' "loong64" ;; + ppc64) printf '%s\n' "ppc64" ;; + ppc64le) printf '%s\n' "ppc64le" ;; + riscv64) printf '%s\n' "riscv64" ;; + s390x) printf '%s\n' "s390x" ;; + *) + return 1 + ;; esac } -downloadBinrayFile() { - echo -e "${Info} 获取最新版的 NextTrace 发行版文件信息" - for i in {1..3}; do - downloadUrls=$(curl -sLf ${protocol}://www.nxtrace.org/api/dist/core/nexttrace_${osDistribution}_${archParam} --connect-timeout 2) - if [ $? -eq 0 ]; then - break - fi - done - if [ $? -eq 0 ]; then - primaryUrl=$(echo ${downloadUrls} | awk -F '|' '{print $1}') - backupUrl=$(echo ${downloadUrls} | awk -F '|' '{print $2}') - echo -e "${Info} 正在尝试从 Primary 节点下载 NextTrace" - for i in {1..3}; do - curl -sLf ${primaryUrl} -o ${Temp_path} --connect-timeout 2 - if [ $? -eq 0 ]; then - changeMode - mv ${Temp_path} ${downPath} - echo -e "${Info} NextTrace 现在已经在您的系统中可用" - return +detect_os() { + case "$(uname -s 2>/dev/null || printf 'unknown')" in + Linux) printf '%s\n' "linux" ;; + Darwin) printf '%s\n' "darwin" ;; + FreeBSD) printf '%s\n' "freebsd" ;; + OpenBSD) printf '%s\n' "openbsd" ;; + DragonFly) printf '%s\n' "dragonfly" ;; + *) + return 1 + ;; + esac +} + +detect_arch() { + if [ -n "${ARCH_OVERRIDE}" ]; then + normalize_arch "${ARCH_OVERRIDE}" || die "unsupported arch override: ${ARCH_OVERRIDE}. Use one of the release arch names." + return 0 + fi + normalize_arch "$(uname -m 2>/dev/null || printf 'unknown')" || die "unsupported architecture: $(uname -m 2>/dev/null || printf 'unknown'). Use --arch or download the release manually." +} + +resolve_bin_name() { + case "${FLAVOR}" in + full) printf '%s\n' "nexttrace" ;; + tiny) printf '%s\n' "nexttrace-tiny" ;; + ntr) printf '%s\n' "ntr" ;; + *) + die "unsupported flavor: ${FLAVOR}" + ;; + esac +} + +resolve_asset_name() { + if [ "${TARGET_OS}" = "darwin" ]; then + printf '%s\n' "${BIN_NAME}_darwin_universal" + return 0 + fi + + asset="${BIN_NAME}_${TARGET_OS}_${TARGET_ARCH}" + if [ "${USE_SOFTFLOAT}" -eq 1 ]; then + case "${TARGET_OS}:${TARGET_ARCH}" in + linux:mips|linux:mipsle) + asset="${asset}_softfloat" + ;; + *) + die "--softfloat is only supported for linux mips/mipsle." + ;; + esac + fi + printf '%s\n' "${asset}" +} + +pick_downloader() { + if command_exists curl; then + DOWNLOADER="curl" + return 0 + fi + if command_exists wget; then + DOWNLOADER="wget" + return 0 + fi + die "curl or wget is required to download NextTrace." +} + +fetch_text() { + url="$1" + if [ "${DOWNLOADER}" = "curl" ]; then + curl -fsSL --connect-timeout 10 --max-time 30 "${url}" + return $? + fi + wget -q -O - --timeout=10 "${url}" +} + +download_file() { + url="$1" + output="$2" + if [ "${DOWNLOADER}" = "curl" ]; then + curl -fsSL --connect-timeout 10 --max-time 180 --retry 3 --retry-delay 1 -o "${output}" "${url}" + return $? + fi + wget -q -O "${output}" --timeout=10 --tries=3 --waitretry=1 "${url}" +} + +mkdir_or_die() { + dir="$1" + label="$2" + if [ -d "${dir}" ]; then + return 0 + fi + if ! mkdir -p "${dir}" >/dev/null 2>&1; then + die "unable to create ${label}: ${dir}" + fi +} + +ensure_writable_dir() { + dir="$1" + label="$2" + mkdir_or_die "${dir}" "${label}" + [ -w "${dir}" ] || die "${label} is not writable: ${dir}" +} + +can_use_dir() { + dir="$1" + if [ ! -d "${dir}" ]; then + mkdir -p "${dir}" >/dev/null 2>&1 || return 1 + fi + [ -w "${dir}" ] +} + +find_existing_binary() { + existing="$(command -v "${BIN_NAME}" 2>/dev/null || true)" + if [ -n "${existing}" ] && [ -f "${existing}" ]; then + printf '%s\n' "${existing}" + fi +} + +resolve_install_path() { + case "${INSTALL_MODE}" in + system) + ensure_writable_dir "${SYSTEM_BIN_DIR}" "system install directory" + printf '%s/%s\n' "${SYSTEM_BIN_DIR}" "${BIN_NAME}" + return 0 + ;; + user) + [ -n "${HOME:-}" ] || die "HOME is not set. Use --bin-dir or --system." + ensure_writable_dir "${USER_BIN_DIR}" "user install directory" + printf '%s/%s\n' "${USER_BIN_DIR}" "${BIN_NAME}" + return 0 + ;; + auto) + existing="$(find_existing_binary)" + if [ -n "${existing}" ]; then + existing_dir="$(dirname "${existing}")" + if [ -w "${existing_dir}" ]; then + printf '%s\n' "${existing}" + return 0 + fi + die "existing ${BIN_NAME} at ${existing} is not writable. Re-run with sudo, or use --user / --bin-dir." fi - done - if [ -z ${backupUrl} ]; then - echo -e "${Error} 从 Primary 节点下载失败,且 Backup 节点为空,无法下载 NextTrace" - exit 1 - fi - echo -e "${Error} 从 Primary 节点下载失败,正在尝试从 Backup 节点下载 NextTrace" - for i in {1..3}; do - curl -sLf ${backupUrl} -o ${Temp_path} --connect-timeout 2 - if [ $? -eq 0 ]; then - changeMode - mv ${Temp_path} ${downPath} - echo -e "${Info} NextTrace 现在已经在您的系统中可用" - return + if can_use_dir "${SYSTEM_BIN_DIR}"; then + printf '%s/%s\n' "${SYSTEM_BIN_DIR}" "${BIN_NAME}" + return 0 fi + [ -n "${HOME:-}" ] || die "HOME is not set. Use --bin-dir or run with sudo for a system install." + ensure_writable_dir "${USER_BIN_DIR}" "user install directory" + printf '%s/%s\n' "${USER_BIN_DIR}" "${BIN_NAME}" + return 0 + ;; + *) + die "unknown install mode: ${INSTALL_MODE}" + ;; + esac +} + +append_candidate() { + url="$1" + if [ -n "${url}" ]; then + printf '%s\n' "${url}" + fi +} + +build_candidate_list() { + api_url="${PROTOCOL}://${API_HOST}${API_PATH}/${ASSET_NAME}" + response="$(fetch_text "${api_url}" 2>/dev/null || true)" + if [ -n "${response}" ]; then + old_ifs="${IFS}" + IFS='|' + set -- ${response} + IFS="${old_ifs}" + for candidate in "$@"; do + append_candidate "${candidate}" done - echo -e "${Error} NextTrace 下载失败,请检查您的网络是否正常" - exit 1 else - echo -e "${Error} 获取下载地址失败,请检查您的网络是否正常" - exit 1 + warn "unable to fetch mirror list from ${api_url}, falling back to GitHub release." fi + append_candidate "${GITHUB_RELEASE_BASE}/${ASSET_NAME}" } -changeMode() { - chmod +x ${Temp_path} &> /dev/null +path_in_path_env() { + needle="$1" + case ":${PATH:-}:" in + *:"${needle}":*) + return 0 + ;; + *) + return 1 + ;; + esac } -runBinrayFileHelp() { - if [ -e ${downPath} ]; then - ${downPath} --version - echo -e "${Tips} 一切准备就绪!使用命令 nexttrace 1.1.1.1 开始您的第一次路由测试吧~ 更多进阶命令玩法可以用 nexttrace -h 查看哦\n 关于软件卸载,因为nexttrace是绿色版单文件,卸载只需输入命令 rm ${downPath} 即可" +verify_temp_binary() { + [ -s "${TEMP_FILE}" ] || return 1 + chmod 0755 "${TEMP_FILE}" >/dev/null 2>&1 || return 1 + "${TEMP_FILE}" --version >/dev/null 2>&1 +} + +download_and_install() { + install_dir="$(dirname "${INSTALL_PATH}")" + mkdir_or_die "${install_dir}" "install directory" + TEMP_FILE="$(mktemp "${install_dir}/.${BIN_NAME}.tmp.XXXXXX")" || die "unable to create temporary file in ${install_dir}" + candidate_file="$(mktemp "${install_dir}/.${BIN_NAME}.urls.XXXXXX")" || die "unable to create candidate list in ${install_dir}" + build_candidate_list >"${candidate_file}" + + downloaded_from="" + while IFS= read -r candidate; do + [ -n "${candidate}" ] || continue + if download_file "${candidate}" "${TEMP_FILE}" >/dev/null 2>&1; then + if verify_temp_binary; then + downloaded_from="${candidate}" + break + fi + fi + rm -f "${TEMP_FILE}" >/dev/null 2>&1 || true + TEMP_FILE="$(mktemp "${install_dir}/.${BIN_NAME}.tmp.XXXXXX")" || die "unable to recreate temporary file in ${install_dir}" + done <"${candidate_file}" + rm -f "${candidate_file}" >/dev/null 2>&1 || true + + [ -n "${downloaded_from}" ] || die "failed to download a working ${ASSET_NAME}. Try again later or install from the release page manually." + + mv -f "${TEMP_FILE}" "${INSTALL_PATH}" || die "failed to move ${BIN_NAME} into place: ${INSTALL_PATH}" + TEMP_FILE="" + info "Downloaded from: ${downloaded_from}" +} + +print_post_install() { + install_dir="$(dirname "${INSTALL_PATH}")" + info "Flavor: ${FLAVOR}" + info "Installed: ${INSTALL_PATH}" + info "Command: ${BIN_NAME}" + if ! path_in_path_env "${install_dir}"; then + warn "${install_dir} is not in PATH." + printf '%s\n' "Add it with: export PATH=\"${install_dir}:\$PATH\"" fi + printf '%s\n' "Run: ${BIN_NAME} --help" } -# Check Procedure -checkRootPermit -checkSystemDistribution -checkSystemArch +parse_args() { + while [ $# -gt 0 ]; do + case "$1" in + http) + PROTOCOL="http" + shift + ;; + --flavor) + [ $# -ge 2 ] || die "--flavor requires a value" + FLAVOR="$2" + shift 2 + ;; + --user) + [ "${INSTALL_MODE}" = "auto" ] || die "choose only one of --user / --system / --bin-dir" + INSTALL_MODE="user" + shift + ;; + --system) + [ "${INSTALL_MODE}" = "auto" ] || die "choose only one of --user / --system / --bin-dir" + INSTALL_MODE="system" + shift + ;; + --bin-dir) + [ $# -ge 2 ] || die "--bin-dir requires a path" + [ "${INSTALL_MODE}" = "auto" ] || die "choose only one of --user / --system / --bin-dir" + BIN_DIR="$2" + INSTALL_MODE="bin-dir" + shift 2 + ;; + --arch) + [ $# -ge 2 ] || die "--arch requires a value" + ARCH_OVERRIDE="$2" + shift 2 + ;; + --softfloat) + USE_SOFTFLOAT=1 + shift + ;; + --help|-h) + print_help + exit 0 + ;; + *) + die "unknown argument: $1" + ;; + esac + done +} -# Download Procedure -downloadBinrayFile +main() { + parse_args "$@" + TARGET_OS="$(detect_os)" || die "unsupported operating system: $(uname -s 2>/dev/null || printf 'unknown'). Please download the release manually." + TARGET_ARCH="$(detect_arch)" + BIN_NAME="$(resolve_bin_name)" + ASSET_NAME="$(resolve_asset_name)" + pick_downloader -# Run Procedure -runBinrayFileHelp + if [ "${INSTALL_MODE}" = "bin-dir" ]; then + ensure_writable_dir "${BIN_DIR}" "install directory" + INSTALL_PATH="${BIN_DIR}/${BIN_NAME}" + else + INSTALL_PATH="$(resolve_install_path)" + fi + info "Preparing ${ASSET_NAME}" + info "Installing to ${INSTALL_PATH}" + + download_and_install + print_post_install +} + +main "$@" diff --git a/scripts/test_nt_install.sh b/scripts/test_nt_install.sh new file mode 100644 index 0000000..2ec53a8 --- /dev/null +++ b/scripts/test_nt_install.sh @@ -0,0 +1,232 @@ +#!/usr/bin/env bash + +set -euo pipefail + +ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)" +INSTALLER="${ROOT_DIR}/nt_install.sh" +REAL_UNAME="$(command -v uname)" + +fail() { + printf '%s\n' "FAIL: $*" >&2 + exit 1 +} + +assert_file_exists() { + [ -f "$1" ] || fail "missing file: $1" +} + +assert_contains() { + file="$1" + needle="$2" + grep -Fq "$needle" "$file" || fail "expected '$needle' in $file" +} + +setup_case_dir() { + case_dir="$(mktemp -d)" + mkdir -p "${case_dir}/mockbin" "${case_dir}/home" "${case_dir}/system-bin" + + cat > "${case_dir}/mockbin/uname" < "${case_dir}/mockbin/curl" <<'EOF' +#!/bin/sh +set -eu + +log_file="${MOCK_CURL_LOG:-}" +output="" +url="" + +while [ $# -gt 0 ]; do + case "$1" in + -o|--output) + output="$2" + shift 2 + ;; + --connect-timeout|--max-time|--retry|--retry-delay) + shift 2 + ;; + -f|-s|-S|-L|-fsSL|-fsSLI) + shift + ;; + -*) + shift + ;; + *) + url="$1" + shift + ;; + esac +done + +[ -n "$log_file" ] && printf '%s\n' "$url" >> "$log_file" + +case "$url" in + *"/api/dist/core/"*) + if [ "${MOCK_API_FAIL:-0}" = "1" ]; then + exit 22 + fi + printf '%s' "${MOCK_API_RESPONSE:-}" + ;; + *) + if [ "${MOCK_DOWNLOAD_FAIL:-0}" = "1" ]; then + exit 22 + fi + [ -n "$output" ] || exit 1 + cat > "$output" <<'EOS' +#!/bin/sh +if [ "${1-}" = "--version" ]; then + echo "nexttrace mock 0.0.0" + exit 0 +fi +exit 0 +EOS + chmod +x "$output" + ;; +esac +EOF + chmod +x "${case_dir}/mockbin/curl" + + printf '%s\n' "${case_dir}" +} + +run_installer() { + case_dir="$1" + path_prefix="${2:-}" + shift 2 || true + + run_path="${case_dir}/mockbin:/usr/bin:/bin" + if [ -n "${path_prefix}" ]; then + run_path="${path_prefix}:${run_path}" + fi + + set +e + ( + export HOME="${case_dir}/home" + export PATH="${run_path}" + export NT_INSTALL_SYSTEM_BIN_DIR="${case_dir}/system-bin" + export NT_INSTALL_USER_BIN_DIR="${case_dir}/home/.local/bin" + export MOCK_CURL_LOG="${case_dir}/curl.log" + cat "${INSTALLER}" | bash -s -- "$@" + ) >"${case_dir}/stdout.log" 2>"${case_dir}/stderr.log" + status=$? + set -e + return "${status}" +} + +test_default_full() { + case_dir="$(setup_case_dir)" + export MOCK_API_RESPONSE="https://mirror.invalid/nexttrace_linux_amd64|https://backup.invalid/nexttrace_linux_amd64" + run_installer "${case_dir}" "" + assert_file_exists "${case_dir}/system-bin/nexttrace" + assert_contains "${case_dir}/stdout.log" "Command: nexttrace" + rm -rf "${case_dir}" +} + +test_tiny_flavor() { + case_dir="$(setup_case_dir)" + export MOCK_API_RESPONSE="https://mirror.invalid/nexttrace-tiny_linux_amd64|https://backup.invalid/nexttrace-tiny_linux_amd64" + run_installer "${case_dir}" "" --flavor tiny + assert_file_exists "${case_dir}/system-bin/nexttrace-tiny" + assert_contains "${case_dir}/stdout.log" "Command: nexttrace-tiny" + rm -rf "${case_dir}" +} + +test_ntr_flavor() { + case_dir="$(setup_case_dir)" + export MOCK_API_RESPONSE="https://mirror.invalid/ntr_linux_amd64|https://backup.invalid/ntr_linux_amd64" + run_installer "${case_dir}" "" --flavor ntr + assert_file_exists "${case_dir}/system-bin/ntr" + assert_contains "${case_dir}/stdout.log" "Command: ntr" + rm -rf "${case_dir}" +} + +test_darwin_universal_asset() { + case_dir="$(setup_case_dir)" + export MOCK_UNAME_S="Darwin" + export MOCK_UNAME_M="arm64" + export MOCK_API_RESPONSE="https://mirror.invalid/nexttrace_darwin_universal|https://backup.invalid/nexttrace_darwin_universal" + run_installer "${case_dir}" "" + assert_contains "${case_dir}/curl.log" "/api/dist/core/nexttrace_darwin_universal" + assert_file_exists "${case_dir}/system-bin/nexttrace" + rm -rf "${case_dir}" + unset MOCK_UNAME_S MOCK_UNAME_M +} + +test_user_mode() { + case_dir="$(setup_case_dir)" + export MOCK_API_RESPONSE="https://mirror.invalid/nexttrace_linux_amd64" + run_installer "${case_dir}" "" --user + assert_file_exists "${case_dir}/home/.local/bin/nexttrace" + rm -rf "${case_dir}" +} + +test_system_mode() { + case_dir="$(setup_case_dir)" + export MOCK_API_RESPONSE="https://mirror.invalid/nexttrace_linux_amd64" + run_installer "${case_dir}" "" --system + assert_file_exists "${case_dir}/system-bin/nexttrace" + rm -rf "${case_dir}" +} + +test_bin_dir_mode() { + case_dir="$(setup_case_dir)" + custom_dir="${case_dir}/custom-bin" + export MOCK_API_RESPONSE="https://mirror.invalid/nexttrace_linux_amd64" + run_installer "${case_dir}" "" --bin-dir "${custom_dir}" + assert_file_exists "${custom_dir}/nexttrace" + rm -rf "${case_dir}" +} + +test_github_fallback() { + case_dir="$(setup_case_dir)" + export MOCK_API_FAIL=1 + run_installer "${case_dir}" "" + assert_file_exists "${case_dir}/system-bin/nexttrace" + assert_contains "${case_dir}/curl.log" "https://github.com/nxtrace/NTrace-dev/releases/latest/download/nexttrace_linux_amd64" + rm -rf "${case_dir}" + unset MOCK_API_FAIL +} + +test_existing_unwritable_binary_rejected() { + case_dir="$(setup_case_dir)" + existing_dir="${case_dir}/existing-bin" + mkdir -p "${existing_dir}" + cat > "${existing_dir}/nexttrace" <<'EOF' +#!/bin/sh +exit 0 +EOF + chmod 0555 "${existing_dir}/nexttrace" + chmod 0555 "${existing_dir}" + + export MOCK_API_RESPONSE="https://mirror.invalid/nexttrace_linux_amd64" + if run_installer "${case_dir}" "${existing_dir}"; then + fail "installer should reject unwritable existing binary" + fi + assert_contains "${case_dir}/stderr.log" "existing nexttrace" + [ ! -f "${case_dir}/system-bin/nexttrace" ] || fail "unexpected second install in system bin" + [ ! -f "${case_dir}/home/.local/bin/nexttrace" ] || fail "unexpected second install in user bin" + chmod 0755 "${existing_dir}" "${existing_dir}/nexttrace" + rm -rf "${case_dir}" +} + +main() { + test_default_full + test_tiny_flavor + test_ntr_flavor + test_darwin_universal_asset + test_user_mode + test_system_mode + test_bin_dir_mode + test_github_fallback + test_existing_unwritable_binary_rejected + printf '%s\n' "nt_install smoke tests passed" +} + +main "$@"