Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

140 KB
Raw Blame
package installtests

import (
	"context"
	"encoding/pem"
	"io"
	"net/http"
	"net/http/httptest"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"runtime"
	"sort"
	"strings"
	"sync/atomic"
	"testing"
	"time"
)

func TestRootInstallProxmoxLeavesLegacyCleanupExplicit(t *testing.T) {
	content, err := os.ReadFile(repoFile("install.sh"))
	if err != nil {
		t.Fatal(err)
	}
	if strings.Contains(string(content), "cleanup_stale_sensor_proxy_mounts") {
		t.Fatal("new Pulse installation must not contain or invoke estate-wide legacy mount cleanup")
	}
	// Explicit maintenance remains separate; do not replace its failure/state
	// controls with the old install-time best-effort stop/edit/start path.
	legacy, err := os.ReadFile(repoFile("scripts", "uninstall-sensor-proxy.sh"))
	if err != nil || !strings.Contains(string(legacy), "cleanup_stale_sensor_proxy_mounts()") {
		t.Fatalf("separate legacy maintenance helper missing: %v", err)
	}
}

func TestRootInstallProxmoxEntryPreservesExistingGuests(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.Skip("Unix controlling-terminal installer fixture")
	}
	for _, tc := range []struct {
		name, wantActions, wantOutput string
		wantExit                      int
	}{
		{"cancel", "cleanup\n", "Installation cancelled", 0},
		{"quick", "nextid\ncleanup\n", "fixture stopped at Port [7655]:", 47},
		{"advanced", "nextid\ncleanup\n", "fixture stopped at Frontend port [7655]:", 47},
		{"noninteractive", "nextid\ncleanup\n", "Non-interactive mode detected. Using Quick installation.", 47},
	} {
		t.Run(tc.name, func(t *testing.T) {
			dir := t.TempDir()
			// The exact production entry runs, but no real guest, /etc/pve
			// config, legacy cleanup body or lifecycle tool is ever reached.
			// The old call is caught by an inert spy, not reenacted.
			body := `#!/bin/bash
set -euo pipefail
scenario="$1"
print_header() { :; }
print_info() { printf '%s\n' "$1"; }
handle_install_interrupt() { exit 75; }
cleanup_container_install_inputs() {
    [[ -z "$1" && -z "$2" && "$3" == false ]]
    printf 'cleanup\n' >>"$FIXTURE_ROOT/actions"
}
cleanup_stale_sensor_proxy_mounts() {
    printf 'UNEXPECTED legacy-cleanup\n' >>"$FIXTURE_ROOT/actions"
}
pvesh() {
    [[ "$*" == 'get /cluster/nextid' ]] || return 81
    printf 'nextid\n' >>"$FIXTURE_ROOT/actions"
    printf '222\n'
}
pct() { printf 'UNEXPECTED pct %s\n' "$*" >>"$FIXTURE_ROOT/actions"; return 82; }
qm() { printf 'UNEXPECTED qm %s\n' "$*" >>"$FIXTURE_ROOT/actions"; return 83; }
safe_read_with_default() {
    if [[ "$1" == 'Select [1-3]: ' ]]; then
        case "$scenario" in
            cancel) printf -v "$2" 3 ;;
            quick) printf -v "$2" 1 ;;
            advanced) printf -v "$2" 2 ;;
            *) exit 84 ;;
        esac
        return 0
    fi
    printf 'fixture stopped at %s\n' "$1"
    exit 47
}
` + extractRootInstallShellFunction(t, "handle_container_install_exit") + "\n" +
				extractRootInstallShellFunction(t, "create_lxc_container") + `
create_lxc_container
exit 85
`
			script := filepath.Join(dir, "entry.sh")
			if err := os.WriteFile(script, []byte(body), 0600); err != nil {
				t.Fatal(err)
			}
			// A PTY exercises the real /dev/tty branch. A new session with
			// pipe stdio exercises the detached installer, independent of the
			// terminal from which go test was launched. This needs no root.
			const runner = `import os, pty, subprocess, sys
command = ["bash", sys.argv[1], sys.argv[2]]
if sys.argv[2] == "noninteractive":
    sys.exit(subprocess.run(command, start_new_session=True).returncode)
sys.exit(os.waitstatus_to_exitcode(pty.spawn(command)))
`
			ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
			defer cancel()
			cmd := exec.CommandContext(ctx, "python3", "-c", runner, script, tc.name)
			cmd.Env = append(os.Environ(), "FIXTURE_ROOT="+dir)
			out, err := cmd.CombinedOutput()
			if ctx.Err() != nil {
				t.Fatalf("entry fixture deadline: %v\n%s", ctx.Err(), out)
			}
			code := 0
			if err != nil {
				if exit, ok := err.(*exec.ExitError); ok {
					code = exit.ExitCode()
				} else {
					t.Fatalf("start fixture: %v\n%s", err, out)
				}
			}
			if code != tc.wantExit || !strings.Contains(string(out), tc.wantOutput) {
				t.Fatalf("entry exit %d, want %d and %q:\n%s", code, tc.wantExit, tc.wantOutput, out)
			}
			actions, err := os.ReadFile(filepath.Join(dir, "actions"))
			if err != nil || string(actions) != tc.wantActions {
				t.Fatalf("unexpected existing-guest or pre-prompt action: %v, %q; want %q\n%s", err, actions, tc.wantActions, out)
			}
			if strings.Contains(string(out), "Cleaned sensor-proxy") {
				t.Fatalf("new installation claimed legacy recovery:\n%s", out)
			}
		})
	}
}

func TestRootInstallProxmoxFailurePreservesRecovery(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.Skip("Unix installer failure fixture")
	}
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
	defer cancel()
	cmd := exec.CommandContext(ctx, "python3", repoFile("scripts", "tests", "test_server_lxc_recovery.py"), "-v")
	cmd.Env = append(os.Environ(), "PULSE_INSTALLER_UNDER_TEST="+repoFile("install.sh"))
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("Proxmox installer failure/recovery controls: %v\n%s", err, out)
	}
	for _, control := range []string{
		"test_inner_installer_exit_is_reported_with_and_without_timeout",
		"test_interruptions_never_destroy_the_partially_installed_container",
		"test_late_unexpected_failures_keep_state_and_original_exit",
		"test_no_owned_container_is_claimed_before_create_succeeds",
		"test_real_owned_cleanup_failure_does_not_replace_the_primary_failure",
		"test_required_dependency_failures_stop_before_install",
		"test_start_network_and_copy_failures_keep_recovery_state",
		"test_success_and_optional_settings_failures_still_complete",
		"test_timeout_retains_copied_inputs_and_requires_state_reconciliation",
	} {
		if !strings.Contains(string(out), control+" (__main__.LXCRecovery."+control+") ... ok") {
			t.Fatalf("required LXC recovery control %s did not pass:\n%s", control, out)
		}
	}
}

// Both sourced entries must propagate removal failures even when a conditional
// caller disables errexit. The fixture confines every mutation to temporary
// ordinary-user files; native systemd acceptance stays with the existing job.
func TestRootInstallRemovalFilesystemFailures(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.Skip("Unix server removal fixture")
	}
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
	defer cancel()
	cmd := exec.CommandContext(ctx, "python3", repoFile("scripts", "tests", "test_server_removal_failures.py"), "-v")
	cmd.Env = append(os.Environ(), "PULSE_INSTALLER_UNDER_TEST="+repoFile("install.sh"))
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("server removal filesystem/account/reload controls: %v\n%s", err, out)
	}
	for _, control := range []string{
		"test_each_runtime_deletion_failure_preserves_data_and_stops",
		"test_reload_or_account_failure_preserves_data",
		"test_partial_data_deletion_is_not_completion_or_intact_state",
		"test_unsafe_or_unreadable_layout_refuses_all_file_deletion",
	} {
		if !strings.Contains(string(out), control) {
			t.Fatalf("required removal control %s did not run:\n%s", control, out)
		}
	}
}

func TestRootInstallScriptVersionFlagRequiresValue(t *testing.T) {
	scriptPath := filepath.Join("..", "..", "install.sh")

	cmd := exec.Command("bash", scriptPath, "--version")
	out, err := cmd.CombinedOutput()
	if err == nil {
		t.Fatal("expected install.sh --version without value to fail")
	}

	got := string(out)
	if !strings.Contains(got, "Missing value for --version") {
		t.Fatalf("expected friendly missing-value error, got:\n%s", got)
	}
	if strings.Contains(got, "unbound variable") {
		t.Fatalf("expected guarded parser error, got shell failure:\n%s", got)
	}
}

func TestRootInstallScriptArchiveFlagRequiresValue(t *testing.T) {
	scriptPath := filepath.Join("..", "..", "install.sh")

	cmd := exec.Command("bash", scriptPath, "--archive")
	out, err := cmd.CombinedOutput()
	if err == nil {
		t.Fatal("expected install.sh --archive without value to fail")
	}

	got := string(out)
	if !strings.Contains(got, "--archive requires a local .tar.gz path") {
		t.Fatalf("expected friendly archive missing-value error, got:\n%s", got)
	}
	if strings.Contains(got, "unbound variable") {
		t.Fatalf("expected guarded parser error, got shell failure:\n%s", got)
	}
}

func TestRootInstallScriptArchiveCannotBeUsedWithSource(t *testing.T) {
	scriptPath := filepath.Join("..", "..", "install.sh")

	cmd := exec.Command("bash", scriptPath, "--source", "--archive", "/tmp/pulse-v6.0.0-linux-amd64.tar.gz")
	out, err := cmd.CombinedOutput()
	if err == nil {
		t.Fatal("expected install.sh --source --archive to fail")
	}

	got := string(out)
	if !strings.Contains(got, "--archive cannot be used with --source") {
		t.Fatalf("expected archive/source conflict error, got:\n%s", got)
	}
}

func TestRootInstallScriptArchiveSupportContract(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}

	script := string(content)
	required := []string{
		`ARCHIVE_OVERRIDE="${PULSE_ARCHIVE_PATH:-}"`,
		`--archive PATH`,
		`resolve_archive_override()`,
		`infer_release_from_archive_name()`,
		`validate_pulse_binary_architecture()`,
		`ensure_update_disk_headroom()`,
		`UPDATE_MIN_TEMP_FREE_BYTES=$((900 * 1024 * 1024))`,
		`UPDATE_MIN_INSTALL_FREE_BYTES=$((256 * 1024 * 1024))`,
		`ensure_update_disk_headroom "$staging_parent" "$INSTALL_DIR"`,
		`prefetch_pulse_archive_for_container()`,
		`download_release_archive()`,
		`install_pulse_archive()`,
		`Archive version $inferred_release does not match requested version $FORCE_VERSION`,
	}
	for _, needle := range required {
		if !strings.Contains(script, needle) {
			t.Fatalf("install.sh missing archive support contract: %s", needle)
		}
	}
}

func TestRootInstallSystemdServiceExposesOnlyPulseOwnedSubscriptionCLIHome(t *testing.T) {
	body := extractRootInstallShellFunction(t, "install_systemd_service")
	for _, required := range []string{
		`Environment="HOME=$INSTALL_DIR"`,
		`Environment="PATH=$INSTALL_DIR/.local/bin:$INSTALL_DIR/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"`,
		`User=pulse`,
		`ProtectHome=true`,
	} {
		if !strings.Contains(body, required) {
			t.Fatalf("systemd service missing subscription CLI boundary %q", required)
		}
	}
	if strings.Contains(body, "/root/") {
		t.Fatal("systemd service must not expose a privileged home to local subscription CLIs")
	}
}

func TestRootInstallScriptStagesUpdateBeforeStoppingService(t *testing.T) {
	downloadPulse := extractRootInstallShellFunction(t, "download_pulse")
	orderedSteps := []string{
		`ensure_update_disk_headroom "$staging_parent" "$INSTALL_DIR"`,
		`download_release_archive "$LATEST_RELEASE" "$pulse_arch" "$archive_path"`,
		`run_upgrade_readiness_preflight "$CURRENT_VERSION" "$expected_release"`,
		`install_pulse_archive "$archive_path" "$expected_release"`,
	}

	previous := -1
	for _, step := range orderedSteps {
		position := strings.Index(downloadPulse, step)
		if position < 0 {
			t.Fatalf("download_pulse missing update safety step: %s", step)
		}
		if position <= previous {
			t.Fatalf("download_pulse update safety steps are out of order at: %s", step)
		}
		previous = position
	}
	if strings.Contains(downloadPulse, "stop_pulse_for_replacement") {
		t.Fatal("download_pulse must not stop Pulse before archive admission")
	}
	archiveInstall := extractRootInstallShellFunction(t, "install_pulse_archive")
	previous = -1
	for _, step := range []string{
		`verify_release_signature "$archive_path"`,
		`tar --no-same-owner --no-overwrite-dir -xzf`,
		`validate_pulse_binary_architecture "$pulse_binary_path"`,
		`cp "$pulse_binary_path" "$binary_stage/pulse"`,
		`timeout 5 "$binary_stage/pulse" --version`,
		`stop_pulse_for_replacement "$service_name"`,
		`mv -fT "$binary_stage/pulse" "$INSTALL_DIR/bin/pulse"`,
	} {
		position := strings.Index(archiveInstall, step)
		if position <= previous {
			t.Fatalf("archive admission/replacement steps are out of order or missing at %s", step)
		}
		previous = position
	}
	if strings.Contains(archiveInstall, "pulse.old") || strings.Contains(archiveInstall, "temp_extract2") {
		t.Fatal("archive replacement must not move/delete the old binary or blindly re-extract")
	}
	sourceInstall := extractRootInstallShellFunction(t, "build_from_source")
	previous = -1
	for _, step := range []string{
		`make build`,
		`mktemp -d "$INSTALL_DIR/bin/.pulse-stage-XXXXXX"`,
		`cp pulse "$binary_stage/pulse"`,
		`chmod 755 "$binary_stage/pulse"`,
		`chown pulse:pulse "$binary_stage/pulse"`,
		`timeout 5 "$binary_stage/pulse" --version`,
		`source_revision=$(git rev-parse --short HEAD)`,
		`stop_pulse_for_replacement "$service_name"`,
		`mv -fT "$binary_stage/pulse" "$INSTALL_DIR/bin/pulse"`,
	} {
		position := strings.Index(sourceInstall, step)
		if position <= previous {
			t.Fatalf("source admission/replacement steps are out of order or missing at %s", step)
		}
		previous = position
	}
	if strings.Contains(sourceInstall, "pulse.old") || strings.Contains(sourceInstall, `cp pulse "$INSTALL_DIR/bin/pulse"`) {
		t.Fatal("source replacement must not move/delete the old binary or copy onto the live path")
	}
	if !strings.Contains(sourceInstall, `recover_pulse_after_failed_replacement "$service_name"`) {
		t.Fatal("failed source rename must use prior-active-only recovery")
	}
}

func TestRootInstallScriptInstallsSignatureVerificationDependencies(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}

	script := string(content)
	required := []string{
		`apt-get install -y -qq curl wget ca-certificates openssh-client jq`,
		`apt-get install -y -qq curl wget ca-certificates openssh-client`,
		`ssh-keygen is required to verify signed Pulse release assets.`,
	}
	for _, needle := range required {
		if !strings.Contains(script, needle) {
			t.Fatalf("install.sh missing signature verification dependency contract: %s", needle)
		}
	}
}

func TestRootInstallScriptInfersPrivateProArchiveVersion(t *testing.T) {
	script := `
		set -euo pipefail
` + extractRootInstallShellFunction(t, "infer_release_from_archive_name") + `
		infer_release_from_archive_name /tmp/pulse-v6.0.0-rc.5-linux-amd64.tar.gz
		infer_release_from_archive_name /tmp/pulse-pro-v6.0.0-rc.5-linux-amd64.tar.gz
		infer_release_from_archive_name /tmp/pulse-pro-v6.0.0-linux-arm64.tar.gz
	`

	out, err := exec.Command("bash", "-c", script).CombinedOutput()
	if err != nil {
		t.Fatalf("bash: %v\n%s", err, out)
	}

	got := strings.Fields(string(out))
	want := []string{"v6.0.0-rc.5", "v6.0.0-rc.5", "v6.0.0"}
	if len(got) != len(want) {
		t.Fatalf("versions = %#v, want %#v", got, want)
	}
	for i := range want {
		if got[i] != want[i] {
			t.Fatalf("versions = %#v, want %#v", got, want)
		}
	}
}

func TestRootInstallScriptUpdateDiskHeadroomRejectsSharedLowSpaceFilesystem(t *testing.T) {
	script := `
		set -euo pipefail
		print_error() { :; }
		print_info() { :; }
		print_warn() { :; }
		INSTALL_DIR="/opt/pulse"
		UPDATE_MIN_TEMP_FREE_BYTES=$((100 * 1024))
		UPDATE_MIN_INSTALL_FREE_BYTES=$((80 * 1024))
` + extractRootInstallShellFunction(t, "bytes_to_human") + `
` + extractRootInstallShellFunction(t, "get_available_bytes_for_path") + `
` + extractRootInstallShellFunction(t, "get_filesystem_device_for_path") + `
` + extractRootInstallShellFunction(t, "ensure_update_disk_headroom") + `
		df() {
			if [[ "$1" == "-Pk" ]]; then
				case "$2" in
					/tmp|/opt/pulse)
						printf 'Filesystem 1024-blocks Used Available Capacity Mounted on\n'
						printf '/dev/shared 1000 0 150 0%% /\n'
						return 0
						;;
				esac
			fi
			command df "$@"
		}
		if ensure_update_disk_headroom /tmp /opt/pulse; then
			echo "ensure_update_disk_headroom unexpectedly passed on a shared full filesystem" >&2
			exit 1
		fi
	`

	out, err := exec.Command("bash", "-c", script).CombinedOutput()
	if err != nil {
		t.Fatalf("bash: %v\n%s", err, out)
	}
}

func TestRootInstallScriptUpdateDiskHeadroomAcceptsSeparateFilesystems(t *testing.T) {
	script := `
		set -euo pipefail
		print_error() { :; }
		print_info() { :; }
		print_warn() { :; }
		INSTALL_DIR="/opt/pulse"
		UPDATE_MIN_TEMP_FREE_BYTES=$((100 * 1024))
		UPDATE_MIN_INSTALL_FREE_BYTES=$((80 * 1024))
` + extractRootInstallShellFunction(t, "bytes_to_human") + `
` + extractRootInstallShellFunction(t, "get_available_bytes_for_path") + `
` + extractRootInstallShellFunction(t, "get_filesystem_device_for_path") + `
` + extractRootInstallShellFunction(t, "ensure_update_disk_headroom") + `
		df() {
			if [[ "$1" == "-Pk" ]]; then
				case "$2" in
					/tmp)
						printf 'Filesystem 1024-blocks Used Available Capacity Mounted on\n'
						printf '/dev/tmp 1000 0 120 0%% /tmp\n'
						return 0
						;;
					/opt/pulse)
						printf 'Filesystem 1024-blocks Used Available Capacity Mounted on\n'
						printf '/dev/root 1000 0 90 0%% /\n'
						return 0
						;;
				esac
			fi
			command df "$@"
		}
		ensure_update_disk_headroom /tmp /opt/pulse
	`

	out, err := exec.Command("bash", "-c", script).CombinedOutput()
	if err != nil {
		t.Fatalf("bash: %v\n%s", err, out)
	}
}

func TestRootInstallScriptConfigBackupHeadroomRejectsLowSpace(t *testing.T) {
	configDir := t.TempDir()
	configParent := filepath.Dir(configDir)

	script := `
		set -euo pipefail
		print_error() { :; }
		print_info() { :; }
		print_warn() { :; }
		CONFIG_DIR="$CONFIG_DIR_UNDER_TEST"
		CONFIG_BACKUP_MIN_EXTRA_BYTES=$((64 * 1024))
` + extractRootInstallShellFunction(t, "bytes_to_human") + `
` + extractRootInstallShellFunction(t, "get_available_bytes_for_path") + `
` + extractRootInstallShellFunction(t, "get_directory_size_bytes") + `
` + extractRootInstallShellFunction(t, "ensure_config_backup_headroom") + `
		df() {
			if [[ "$1" == "-Pk" && "$2" == "$CONFIG_PARENT_UNDER_TEST" ]]; then
				printf 'Filesystem 1024-blocks Used Available Capacity Mounted on\n'
				printf '/dev/root 1000 0 65 0%% /\n'
				return 0
			fi
			command df "$@"
		}
		du() {
			if [[ "$1" == "-sk" && "$2" == "$CONFIG_DIR_UNDER_TEST" ]]; then
				printf '4\t%s\n' "$CONFIG_DIR_UNDER_TEST"
				return 0
			fi
			command du "$@"
		}
		if ensure_config_backup_headroom "$CONFIG_DIR"; then
			echo "ensure_config_backup_headroom unexpectedly passed with no backup margin" >&2
			exit 1
		fi
	`

	cmd := exec.Command("bash", "-c", script)
	cmd.Env = append(os.Environ(),
		"CONFIG_DIR_UNDER_TEST="+configDir,
		"CONFIG_PARENT_UNDER_TEST="+configParent,
	)
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("bash: %v\n%s", err, out)
	}
}

func TestRootInstallScriptConfigBackupCleansPartialCopy(t *testing.T) {
	configDir := t.TempDir()
	backupDir := configDir + ".backup.20260704-141422"

	script := `
		set -euo pipefail
		print_error() { :; }
		print_info() { :; }
		print_warn() { :; }
		CONFIG_DIR="$CONFIG_DIR_UNDER_TEST"
		CONFIG_BACKUP_MIN_EXTRA_BYTES=$((64 * 1024))
` + extractRootInstallShellFunction(t, "bytes_to_human") + `
` + extractRootInstallShellFunction(t, "get_available_bytes_for_path") + `
` + extractRootInstallShellFunction(t, "get_directory_size_bytes") + `
` + extractRootInstallShellFunction(t, "ensure_config_backup_headroom") + `
` + extractRootInstallShellFunction(t, "backup_existing") + `
		date() { printf '20260704-141422\n'; }
		cp() {
			if [[ "$1" == "-a" && "$3" == "$CONFIG_DIR_UNDER_TEST/." ]]; then
				printf 'partial\n' > "$4/partial"
				return 1
			fi
			command cp "$@"
		}
		if backup_existing; then
			echo "backup_existing unexpectedly passed after a failed copy" >&2
			exit 1
		fi
		if compgen -G "$CONFIG_DIR_UNDER_TEST.backup.*" >/dev/null ||
		   compgen -G "$(dirname "$CONFIG_DIR_UNDER_TEST")/.$(basename "$CONFIG_DIR_UNDER_TEST").backup.*" >/dev/null; then
			echo "partial backup was not removed" >&2
			exit 1
		fi
	`

	cmd := exec.Command("bash", "-c", script)
	cmd.Env = append(os.Environ(),
		"CONFIG_DIR_UNDER_TEST="+configDir,
		"BACKUP_DIR_UNDER_TEST="+backupDir,
	)
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("bash: %v\n%s", err, out)
	}
}

// Issue #1646: unattended updates created one config snapshot per run under
// the hardened-unit fallback directory and never pruned old ones, so small
// root filesystems filled up. backup_existing must rotate snapshots after a
// successful copy.
func TestRootInstallScriptConfigBackupRotatesOldSnapshots(t *testing.T) {
	if os.Geteuid() == 0 {
		t.Skip("root ignores write bits, so the read-only fallback path cannot be simulated")
	}
	configDir := t.TempDir()
	installDir := t.TempDir()
	backupParent := filepath.Join(installDir, "config-backups")
	if err := os.MkdirAll(backupParent, 0755); err != nil {
		t.Fatalf("mkdir backup parent: %v", err)
	}
	prefix := filepath.Base(configDir) + ".backup."
	oldStamps := []string{"20260701-020000", "20260702-020000", "20260703-020000", "20260704-020000", "20260705-020000"}
	for _, stamp := range oldStamps {
		if err := os.MkdirAll(filepath.Join(backupParent, prefix+stamp), 0755); err != nil {
			t.Fatalf("mkdir old snapshot: %v", err)
		}
	}

	script := `
		set -euo pipefail
		print_error() { :; }
		print_info() { :; }
		print_warn() { :; }
		CONFIG_DIR="$CONFIG_DIR_UNDER_TEST"
		INSTALL_DIR="$INSTALL_DIR_UNDER_TEST"
		CONFIG_BACKUP_MIN_EXTRA_BYTES=0
` + extractRootInstallShellFunction(t, "bytes_to_human") + `
` + extractRootInstallShellFunction(t, "get_available_bytes_for_path") + `
` + extractRootInstallShellFunction(t, "get_directory_size_bytes") + `
` + extractRootInstallShellFunction(t, "ensure_config_backup_headroom") + `
` + extractRootInstallShellFunction(t, "config_backup_receipt_matches") + "\n" +
		extractRootInstallShellFunction(t, "prune_config_backups") + `
` + extractRootInstallShellFunction(t, "backup_existing") + `
		for entry in "$INSTALL_DIR/config-backups"/*; do
			(umask 077; printf 'pulse-config-snapshot-v1\0%s\0%s\0%s\0' \
				"$CONFIG_DIR" "$entry" "$(stat -c '%d:%i' -- "$entry")" > "$entry.pulse-snapshot")
		done
		date() { printf '20260706-020000\n'; }
		chmod a-w "$(dirname "$CONFIG_DIR_UNDER_TEST")" 2>/dev/null || true
		trap 'chmod u+w "$(dirname "$CONFIG_DIR_UNDER_TEST")" 2>/dev/null || true' EXIT
		backup_existing
	`

	cmd := exec.Command("bash", "-c", script)
	cmd.Env = append(os.Environ(),
		"CONFIG_DIR_UNDER_TEST="+configDir,
		"INSTALL_DIR_UNDER_TEST="+installDir,
	)
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("bash: %v\n%s", err, out)
	}

	entries, err := os.ReadDir(backupParent)
	if err != nil {
		t.Fatalf("read backup parent: %v", err)
	}
	names := make([]string, 0, len(entries))
	for _, entry := range entries {
		if entry.IsDir() {
			names = append(names, entry.Name())
		}
	}
	if len(names) != 5 {
		t.Fatalf("expected 5 snapshots after rotation, got %d: %v", len(names), names)
	}
	for _, gone := range []string{prefix + "20260701-020000"} {
		for _, name := range names {
			if name == gone {
				t.Fatalf("expected oldest snapshot %s to be pruned, still present: %v", gone, names)
			}
		}
	}
	found := false
	for _, name := range names {
		if strings.HasPrefix(name, prefix+"20260706-020000.") {
			found = true
		}
	}
	if !found {
		t.Fatalf("expected fresh snapshot to exist, got %v", names)
	}
}

// Issue #2127: when an update aborts at the staging disk-headroom check it has
// not replaced anything, so the configuration snapshot taken for it is dead
// weight. Leaving it behind makes the low-space condition worse on every
// automatic retry. backup_existing records the snapshot and
// discard_last_config_backup must remove it and clear the record.
func TestRootInstallScriptDiscardsUnneededConfigBackup(t *testing.T) {
	if os.Geteuid() == 0 {
		t.Skip("root ignores write bits, so the read-only fallback path cannot be simulated")
	}
	configDir := t.TempDir()
	installDir := t.TempDir()

	script := `
		set -euo pipefail
		print_error() { :; }
		print_info() { :; }
		print_warn() { :; }
		CONFIG_DIR="$CONFIG_DIR_UNDER_TEST"
		INSTALL_DIR="$INSTALL_DIR_UNDER_TEST"
		CONFIG_BACKUP_MIN_EXTRA_BYTES=0
` + extractRootInstallShellFunction(t, "bytes_to_human") + `
` + extractRootInstallShellFunction(t, "get_available_bytes_for_path") + `
` + extractRootInstallShellFunction(t, "get_directory_size_bytes") + `
` + extractRootInstallShellFunction(t, "ensure_config_backup_headroom") + `
` + extractRootInstallShellFunction(t, "config_backup_receipt_matches") + "\n" +
		extractRootInstallShellFunction(t, "prune_config_backups") + `
` + extractRootInstallShellFunction(t, "backup_existing") + `
` + extractRootInstallShellFunction(t, "discard_last_config_backup") + `
		date() { printf '20260920-160000\n'; }
		chmod a-w "$(dirname "$CONFIG_DIR_UNDER_TEST")" 2>/dev/null || true
		trap 'chmod u+w "$(dirname "$CONFIG_DIR_UNDER_TEST")" 2>/dev/null || true' EXIT
		backup_existing
		if [[ -z "${LAST_CONFIG_BACKUP_DIR:-}" || ! -d "$LAST_CONFIG_BACKUP_DIR" ]]; then
			echo "backup_existing did not record the created snapshot" >&2
			exit 1
		fi
		backup_path="$LAST_CONFIG_BACKUP_DIR"
		discard_last_config_backup
		if [[ -n "${LAST_CONFIG_BACKUP_DIR:-}" ]]; then
			echo "discard_last_config_backup did not clear the recorded path" >&2
			exit 1
		fi
		if [[ -e "$backup_path" ]]; then
			echo "unneeded configuration backup was not removed" >&2
			exit 1
		fi
	`

	cmd := exec.Command("bash", "-c", script)
	cmd.Env = append(os.Environ(),
		"CONFIG_DIR_UNDER_TEST="+configDir,
		"INSTALL_DIR_UNDER_TEST="+installDir,
	)
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("bash: %v\n%s", err, out)
	}
}

// download_pulse must discard the earlier configuration snapshot when the
// staging disk-headroom check fails, so the update path cannot accumulate
// backups while the filesystem is short of space (#2127).
func TestRootInstallScriptDiscardsBackupWhenStagingHeadroomFails(t *testing.T) {
	downloadPulse := extractRootInstallShellFunction(t, "download_pulse")
	headroom := strings.Index(downloadPulse, `ensure_update_disk_headroom "$staging_parent" "$INSTALL_DIR"`)
	discard := strings.Index(downloadPulse, `discard_last_config_backup`)
	if headroom < 0 || discard < 0 {
		t.Fatalf("download_pulse must check staging headroom and discard the unneeded config backup (headroom=%d discard=%d)", headroom, discard)
	}
	if discard < headroom {
		t.Fatalf("download_pulse must discard the config backup after the headroom check, not before")
	}
}

func TestRootInstallScriptV5ToV6PreflightWarnsWhenAgentScopeMissing(t *testing.T) {
	configDir := t.TempDir()
	if err := os.WriteFile(filepath.Join(configDir, "api_tokens.json"), []byte(`[{"id":"tok-1","name":"admin","hash":"hash","scopes":["settings:read"]}]`), 0600); err != nil {
		t.Fatalf("write api_tokens.json: %v", err)
	}

	script := `
		set -euo pipefail
		print_error() { echo "ERROR: $*"; }
		print_info() { echo "INFO: $*"; }
		print_warn() { echo "WARN: $*"; }
		print_success() { echo "SUCCESS: $*"; }
		UPGRADE_PREFLIGHT_RAN=false
		SKIP_UPGRADE_PREFLIGHT=false
` + extractRootInstallShellFunction(t, "version_major") + `
` + extractRootInstallShellFunction(t, "is_pre_v6_to_v6_upgrade") + `
` + extractRootInstallShellFunction(t, "inspect_api_tokens_for_upgrade") + `
` + extractRootInstallShellFunction(t, "run_upgrade_readiness_preflight") + `
		run_upgrade_readiness_preflight v5.1.23 v6.0.0
	`

	cmd := exec.Command("bash", "-c", script)
	cmd.Env = append(os.Environ(), "CONFIG_DIR="+configDir)
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("bash: %v\n%s", err, out)
	}
	if !strings.Contains(string(out), "No agent reporting token scope was found") {
		t.Fatalf("expected missing-scope warning, got:\n%s", out)
	}
}

func TestRootInstallScriptV5ToV6PreflightAcceptsLegacyHostAgentScope(t *testing.T) {
	configDir := t.TempDir()
	if err := os.WriteFile(filepath.Join(configDir, "api_tokens.json"), []byte(`[{"id":"tok-1","name":"agent","hash":"hash","scopes":["host-agent:report"]}]`), 0600); err != nil {
		t.Fatalf("write api_tokens.json: %v", err)
	}

	script := `
		set -euo pipefail
		print_error() { echo "ERROR: $*"; }
		print_info() { echo "INFO: $*"; }
		print_warn() { echo "WARN: $*"; }
		print_success() { echo "SUCCESS: $*"; }
		UPGRADE_PREFLIGHT_RAN=false
		SKIP_UPGRADE_PREFLIGHT=false
` + extractRootInstallShellFunction(t, "version_major") + `
` + extractRootInstallShellFunction(t, "is_pre_v6_to_v6_upgrade") + `
` + extractRootInstallShellFunction(t, "inspect_api_tokens_for_upgrade") + `
` + extractRootInstallShellFunction(t, "run_upgrade_readiness_preflight") + `
		run_upgrade_readiness_preflight v5.1.23 v6.0.0
	`

	cmd := exec.Command("bash", "-c", script)
	cmd.Env = append(os.Environ(), "CONFIG_DIR="+configDir)
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("bash: %v\n%s", err, out)
	}
	if !strings.Contains(string(out), "Agent reporting token scope is present") {
		t.Fatalf("expected success output, got:\n%s", out)
	}
}

func TestRootInstallScriptAutoRegisterUsesSecureContractShape(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}

	script := string(content)
	required := []string{
		`str(data.get("setupToken", ""))`,
		`str(data.get("tokenHint", ""))`,
		`str(data.get("type", ""))`,
		`str(data.get("host", ""))`,
		`str(data.get("url", ""))`,
		`str(data.get("downloadURL", ""))`,
		`str(data.get("scriptFileName", ""))`,
		`str(data.get("command", ""))`,
		`str(data.get("commandWithEnv", ""))`,
		`str(data.get("commandWithoutEnv", ""))`,
		`expires_raw = data.get("expires", "")`,
		`str(expires_raw)`,
		`expiry_state = "live"`,
		`expires_int > int(time.time())`,
		`expected_setup_url = f"{pulse_url}/api/setup-script?{backup_query}host={quote(host, safe='')}&pulse_url={quote(pulse_url, safe='')}&type=pve"`,
		`expected_download_url = expected_setup_url`,
		`setup_download_url not in (expected_download_url, legacy_download_url)`,
		`expected_script_name = "pulse-setup-pve.sh"`,
		`setup_url != expected_setup_url`,
		`modern = setup_download_url == expected_download_url`,
		`setup_script_name != expected_script_name`,
		`not setup_command`,
		`not setup_command_with_env`,
		`not setup_command_without_env`,
		`command_fields = (`,
		`if not _value or expected_setup_url not in _value:`,
		`'if [ "$(id -u)" -eq 0 ]; then' not in _value`,
		`'elif command -v sudo >/dev/null 2>&1; then' not in _value`,
		`valid = "PULSE_SETUP_TOKEN_FILE=" in _value and "PULSE_SETUP_TOKEN=" not in _value and setup_token not in _value`,
		`valid = "PULSE_SETUP_TOKEN_FILE=" not in _value and (("PULSE_SETUP_TOKEN=" in _value and setup_token in _value) if _requires_token else ("PULSE_SETUP_TOKEN=" not in _value and setup_token not in _value))`,
		`not token_hint or token_hint == setup_token`,
		`[[ "$setup_type" != "pve" ]]`,
		`[[ "$setup_host" != "$normalized_host_url" ]]`,
		`[[ "$setup_url" != "$expected_setup_url" ]]`,
		`[[ "$setup_download_url" != "$expected_download_url" ]]`,
		`[[ "$setup_script_name" != "$expected_script_name" ]]`,
		`[[ -z "$setup_command" ]]`,
		`[[ -z "$setup_command_with_env" ]]`,
		`[[ -z "$setup_command_without_env" ]]`,
		`[[ -z "$setup_token_hint" ]]`,
		`[[ "$setup_expiry_state" != "live" ]]`,
		`host, token_id, server_name = sys.argv[1:]`,
		`token_value, setup_token = __import__("os").fdopen(3).read().splitlines()`,
		`"tokenId": token_id`,
		`"tokenValue": token_value`,
		`"authToken": setup_token`,
		`"source": "script"`,
		`data.get("action", "")`,
		`data.get("type", "")`,
		`data.get("source", "")`,
		`data.get("host", "")`,
		`data.get("tokenId", "")`,
		`data.get("tokenValue", "")`,
		`data.get("nodeId", "")`,
		`data.get("nodeName", "")`,
		`[[ "$register_status" != "success" ]] || [[ "$register_action" != "use_token" ]] || [[ "$register_type" != "pve" ]] || [[ "$register_source" != "script" ]]`,
		`AUTO_NODE_REGISTERED_NAME="$register_node_name"`,
		`curl --retry 3 --retry-delay 2 -fsS -X POST "$pulse_url/api/setup-script-url" -H "Content-Type: application/json" -d "$setup_payload"`,
		`printf %s "$register_payload" | curl --retry 3 --retry-delay 2 -fsS -X POST "$pulse_url/api/auto-register" -H "Content-Type: application/json" -d @-`,
		`token_output=$(pveum user token add pulse-monitor@pve "$token_name" --privsep 1 2>&1)`,
		`pveum aclmod / -token "$token_id" -role PVEAuditor`,
		`pveum aclmod / -token "$token_id" -role PulseMonitor`,
		`pveum aclmod /storage -token "$token_id" -role PVEDatastoreAdmin`,
		`priv_string="$(IFS=,; echo "${extra_privs[*]}")"`,
		`pveum role modify PulseMonitor -privs "$priv_string"`,
		`pveum role add PulseGuestFileReadProbe -privs VM.GuestAgent.FileRead`,
		`extra_privs+=("VM.GuestAgent.Audit")`,
		`extra_privs+=("VM.GuestAgent.FileRead")`,
		`extra_privs+=("VM.Monitor")`,
		`slug = re.sub(r"[^a-z0-9]+", "-", host)`,
		`print(f"pulse-{slug}")`,
	}
	for _, needle := range required {
		if !strings.Contains(script, needle) {
			t.Fatalf("root install.sh missing secure installer auto-register contract fragment: %s", needle)
		}
	}
	if strings.Contains(script, `local token_name="pulse-${pulse_host_slug}-$(date +%s)"`) {
		t.Fatalf("root install.sh preserved stale timestamp-suffixed Proxmox token naming")
	}
	guestBranch := strings.Index(script, `if [[ "$has_guest_audit" == true ]]; then`)
	monitorBranch := strings.Index(script, `if [[ "$has_vm_monitor" == true ]]; then`)
	if guestBranch < 0 || monitorBranch < 0 || guestBranch > monitorBranch {
		t.Fatalf("root install.sh must prefer VM.GuestAgent.* privileges before legacy VM.Monitor")
	}
	forbidden := []string{
		`local bootstrap_token=""`,
		`X-Setup-Token: $bootstrap_token`,
		`Discovered bootstrap token from container`,
		`--privsep 0`,
		`pveum role delete PulseMonitor`,
	}
	for _, needle := range forbidden {
		if strings.Contains(script, needle) {
			t.Fatalf("root install.sh preserved stale setup-script-url bootstrap auth fragment: %s", needle)
		}
	}
}

func TestRootInstallShowsBootstrapTokenCommandInsteadOfEncryptedFile(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}

	script := string(content)
	required := []string{
		`pulse bootstrap-token`,
		`PULSE_DATA_DIR=`,
	}
	for _, needle := range required {
		if !strings.Contains(script, needle) {
			t.Fatalf("root install.sh missing bootstrap-token display fragment: %s", needle)
		}
	}

	forbidden := []string{
		`cat $CONFIG_DIR/.bootstrap_token`,
		`cat "$TOKEN_FILE"`,
		`Token: ${GREEN}`,
	}
	for _, needle := range forbidden {
		if strings.Contains(script, needle) {
			t.Fatalf("root install.sh still exposes encrypted bootstrap file contents: %s", needle)
		}
	}
}

func TestCanonicalServerDeploymentMethodsAreStampedForTelemetry(t *testing.T) {
	repoRoot := filepath.Join("..", "..")
	required := map[string]string{
		"Dockerfile":         "ENV PULSE_DEPLOYMENT_METHOD=container_other",
		"docker-compose.yml": "PULSE_DEPLOYMENT_METHOD=docker_compose",
		"install.sh":         `Environment="PULSE_DEPLOYMENT_METHOD=systemd"`,
		"README.md":          "PULSE_DEPLOYMENT_METHOD=docker_run",
	}
	for relativePath, marker := range required {
		content, err := os.ReadFile(filepath.Join(repoRoot, relativePath))
		if err != nil {
			t.Fatalf("read %s: %v", relativePath, err)
		}
		if !strings.Contains(string(content), marker) {
			t.Errorf("%s must stamp coarse deployment method %q", relativePath, marker)
		}
	}
}

func TestSystemdServerLogsPreservePulseSeverityInJournal(t *testing.T) {
	rootInstall, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}

	unitContract := string(rootInstall)
	for _, marker := range []string{
		"StandardOutput=journal",
		"StandardError=journal",
		"SyslogLevelPrefix=true",
		`Environment="PULSE_LOG_JOURNAL_LEVEL_PREFIX=true"`,
	} {
		if !strings.Contains(unitContract, marker) {
			t.Errorf("systemd server unit must include %q", marker)
		}
	}
}

func TestPrereleaseUpdateCopyUsesPreviewFraming(t *testing.T) {
	rootInstall, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}

	installScript := string(rootInstall)
	requiredInstall := []string{
		`Update to $RC_VERSION (prerelease preview)`,
		`--rc, --pre        Install latest prerelease preview version`,
		`Prerelease channel detected in configuration`,
		// Bind the menu to the complete server-release selector rather than
		// requiring a removed, non-user-facing implementation comment.
		`RC_VERSION=$(resolve_latest_release_tag_for_channel rc 2>/dev/null || true)`,
	}
	for _, needle := range requiredInstall {
		if !strings.Contains(installScript, needle) {
			t.Fatalf("root install.sh missing prerelease framing fragment: %s", needle)
		}
	}
	forbiddenInstall := []string{
		`Update to $RC_VERSION (release candidate)`,
		`--rc, --pre        Install latest RC/pre-release version`,
		`RC channel detected in configuration`,
		`RC channel: Get latest release (including pre-releases, but skip drafts)`,
	}
	for _, needle := range forbiddenInstall {
		if strings.Contains(installScript, needle) {
			t.Fatalf("root install.sh preserved stale release-candidate framing fragment: %s", needle)
		}
	}

	autoUpdate, err := os.ReadFile(filepath.Join("..", "..", "scripts", "pulse-auto-update.sh"))
	if err != nil {
		t.Fatalf("read pulse-auto-update.sh: %v", err)
	}
	autoUpdateScript := string(autoUpdate)
	if !strings.Contains(autoUpdateScript, `Non-stable channel detected; unattended auto-updates run only on stable`) {
		t.Fatalf("pulse-auto-update.sh missing non-stable channel log message")
	}
	if strings.Contains(autoUpdateScript, `RC channel detected; unattended auto-updates run only on stable`) {
		t.Fatalf("pulse-auto-update.sh preserved stale release-candidate channel log message")
	}
}

func TestRootInstallScriptSupportsInstanceScopedServerInstalls(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}

	script := string(content)
	required := []string{
		`SERVICE_NAME_EXPLICIT="false"`,
		`SERVICE_NAME="${PULSE_SERVICE_NAME:-$DEFAULT_SERVICE_NAME}"`,
		`INSTALL_DIR="${PULSE_INSTALL_DIR:-$(default_install_dir_for_service "$SERVICE_NAME")}"`,
		`CONFIG_DIR="${PULSE_CONFIG_DIR:-$(default_config_dir_for_service "$SERVICE_NAME")}"`,
		`BINARY_LINK_PATH="${PULSE_BINARY_LINK_PATH:-$(default_binary_link_path_for_service "$SERVICE_NAME")}"`,
		`UPDATE_HELPER_PATH="${PULSE_UPDATE_HELPER_PATH:-$(default_update_helper_path_for_service "$SERVICE_NAME")}"`,
		`AUTO_UPDATE_DEST="${PULSE_AUTO_UPDATE_DEST:-$(default_auto_update_dest_for_service "$SERVICE_NAME")}"`,
		`UPDATE_SERVICE_PATH="${PULSE_UPDATE_SERVICE_PATH:-$(default_update_service_path_for_service "$SERVICE_NAME")}"`,
		`UPDATE_TIMER_PATH="${PULSE_UPDATE_TIMER_PATH:-$(default_update_timer_path_for_service "$SERVICE_NAME")}"`,
		`if [[ "$SERVICE_NAME_EXPLICIT" == "true" ]]; then`,
		`install_binary_symlink "$INSTALL_DIR/bin/pulse" "$BINARY_LINK_PATH"`,
		`ln -sf "$target" "$link_path"`,
		`safe_systemctl enable "$update_timer_unit" || true`,
		`safe_systemctl start "$update_timer_unit" || true`,
		`Environment="PULSE_SERVICE_NAME=$service_name"`,
		`Environment="PULSE_INSTALL_DIR=$install_dir"`,
		`Environment="PULSE_CONFIG_DIR=$config_dir"`,
		`Environment="PULSE_UPDATE_TIMER_UNIT=$update_timer_unit"`,
		`local update_helper_path="${UPDATE_HELPER_PATH:-${PULSE_UPDATE_HELPER_PATH:-/bin/update}}"`,
		`printf '%q' "$update_helper_path"`,
	}
	for _, needle := range required {
		if !strings.Contains(script, needle) {
			t.Fatalf("root install.sh missing instance-scoped install contract fragment: %s", needle)
		}
	}
}

func TestRootInstallScriptRequiresSignedReleaseDownloads(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}

	script := string(content)
	required := []string{
		`PINNED_RELEASE_SSH_PUBLIC_KEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMZd/DaH+BldzOkq1A8KVTcFk73nAyrE8aJOyf7i00jm pulse-installer"`,
		`require_release_signature_verifier() {`,
		`verify_release_signature() {`,
		`local signature_url="${download_url}.sshsig"`,
		`Failed to download signature for Pulse release`,
		`verify_release_signature "$archive_path" "$signature_file" "downloaded Pulse release"`,
		`INSTALLER_SIG_URL="\${INSTALLER_URL}.sshsig"`,
		`verify_release_signature "\$tmp_installer" "\$tmp_signature" "downloaded Pulse installer"`,
		`Failed to download signature for pulse-auto-update.sh`,
		`verify_release_signature "$dest" "$signature_file" "downloaded pulse-auto-update.sh"`,
	}
	for _, needle := range required {
		if !strings.Contains(script, needle) {
			t.Fatalf("root install.sh missing signed-release verification contract: %s", needle)
		}
	}
}

func TestPulseAutoUpdateScriptSupportsInstanceScopedServerInstalls(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "scripts", "pulse-auto-update.sh"))
	if err != nil {
		t.Fatalf("read pulse-auto-update.sh: %v", err)
	}

	script := string(content)
	required := []string{
		`SERVICE_NAME="${PULSE_SERVICE_NAME:-pulse}"`,
		`INSTALL_DIR="${PULSE_INSTALL_DIR:-/opt/pulse}"`,
		`CONFIG_DIR="${PULSE_CONFIG_DIR:-/etc/pulse}"`,
		`UPDATE_TIMER_UNIT="${PULSE_UPDATE_TIMER_UNIT:-${SERVICE_NAME}-update.timer}"`,
		`if [[ -n "${PULSE_SERVICE_NAME:-}" ]]; then`,
		`"PULSE_SERVICE_NAME=$service_name"`,
		`"PULSE_INSTALL_DIR=$INSTALL_DIR"`,
		`"PULSE_CONFIG_DIR=$CONFIG_DIR"`,
		`systemctl is-enabled --quiet "$UPDATE_TIMER_UNIT"`,
	}
	for _, needle := range required {
		if !strings.Contains(script, needle) {
			t.Fatalf("pulse-auto-update.sh missing instance-scoped install contract fragment: %s", needle)
		}
	}
}

func TestPulseAutoUpdateScriptRequiresSignedInstallerDownloads(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "scripts", "pulse-auto-update.sh"))
	if err != nil {
		t.Fatalf("read pulse-auto-update.sh: %v", err)
	}

	script := string(content)
	required := []string{
		`PINNED_RELEASE_SSH_PUBLIC_KEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMZd/DaH+BldzOkq1A8KVTcFk73nAyrE8aJOyf7i00jm pulse-installer"`,
		`require_release_signature_verifier() {`,
		`verify_release_signature() {`,
		`local install_signature_url="${install_script_url}.sshsig"`,
		`Failed to download installer signature from $install_signature_url`,
		`verify_release_signature "$installer_tmp" "$signature_tmp" "downloaded Pulse installer"`,
		`Installer signature verified`,
	}
	for _, needle := range required {
		if !strings.Contains(script, needle) {
			t.Fatalf("pulse-auto-update.sh missing signed-installer verification contract: %s", needle)
		}
	}
}

func TestOperatorInstallDocsAvoidUnverifiedBootstrapAndFloatingImageTags(t *testing.T) {
	files := []string{
		filepath.Join("..", "..", "README.md"),
		filepath.Join("..", "..", "docs", "INSTALL.md"),
		filepath.Join("..", "..", "docs", "UPGRADE_v6.md"),
		filepath.Join("..", "..", "docs", "UPGRADE_v5.md"),
		filepath.Join("..", "..", "docs", "DOCKER.md"),
		filepath.Join("..", "..", "docs", "AUTO_UPDATE.md"),
		filepath.Join("..", "..", "docs", "operations", "AUTO_UPDATE.md"),
		filepath.Join("..", "..", "docs", "FAQ.md"),
	}
	forbidden := []string{
		`curl -fsSL https://github.com/rcourtman/Pulse/releases/latest/download/install.sh |`,
		`curl -sL https://github.com/rcourtman/Pulse/releases/latest/download/install.sh |`,
		`rcourtman/pulse:latest`,
		`docker pull rcourtman/pulse:latest`,
		`image: rcourtman/pulse:latest`,
	}

	for _, path := range files {
		content, err := os.ReadFile(path)
		if err != nil {
			t.Fatalf("read %s: %v", path, err)
		}
		text := string(content)
		for _, needle := range forbidden {
			if strings.Contains(text, needle) {
				t.Fatalf("%s preserved insecure operator guidance: %s", path, needle)
			}
		}
	}
}

// TestRootInstallDeployAgentScriptsDeploysSignatureSidecars guards the fix for
// the rc.6 "Install on Linux" agent-wizard regression (issue #1470). The
// running server serves /opt/pulse/scripts/install.sh at its /install.sh
// endpoint, but for published releases the handler only serves that local copy
// when its .sig and .sshsig sidecars are present next to it; otherwise it
// proxies the top-level GitHub install.sh asset, which is the SERVER installer
// (not the agent installer) and rejects the wizard's --url/--token-file flags.
// The Docker image deploys these sidecars; deploy_agent_scripts must too.
// TestRootInstallUninstallCleansLegacySensorProxy guards #34: `install.sh
// --uninstall` on a Proxmox host that was upgraded from v5 must remove the
// leftover pulse-sensor-proxy footprint locally — binary, units, runtime/state,
// service user, and (security-relevant) the managed SSH keys in root's
// authorized_keys — so a "complete uninstall" leaves nothing behind. Cluster-
// wide key removal and Proxmox API-user deletion stay behind the explicit
// standalone scripts/uninstall-sensor-proxy.sh, which we only point users to.
func TestRootInstallUninstallCleansLegacySensorProxy(t *testing.T) {
	tmp := t.TempDir()

	binPath := filepath.Join(tmp, "bin", "pulse-sensor-proxy")
	systemdDir := filepath.Join(tmp, "systemd")
	unitPath := filepath.Join(systemdDir, "pulse-sensor-proxy.service")
	authKeys := filepath.Join(tmp, "authorized_keys")
	marker := filepath.Join(tmp, "calls.log")

	for _, dir := range []string{filepath.Dir(binPath), systemdDir} {
		if err := os.MkdirAll(dir, 0o755); err != nil {
			t.Fatalf("mkdir %s: %v", dir, err)
		}
	}
	if err := os.WriteFile(binPath, []byte("#!/bin/sh\n"), 0o755); err != nil {
		t.Fatalf("write binary: %v", err)
	}
	if err := os.WriteFile(unitPath, []byte("[Unit]\n"), 0o644); err != nil {
		t.Fatalf("write unit: %v", err)
	}
	authContent := "ssh-ed25519 AAAA keepme@admin\n" +
		"ssh-ed25519 BBBB # pulse-managed-key\n" +
		"ssh-ed25519 CCCC # pulse-proxy-key\n" +
		"ssh-rsa DDDD keep-this-too\n"
	if err := os.WriteFile(authKeys, []byte(authContent), 0o600); err != nil {
		t.Fatalf("write authorized_keys: %v", err)
	}

	env := `
		set -uo pipefail
		SENSOR_PROXY_BINARY_PATH="` + binPath + `"
		SENSOR_PROXY_SYSTEMD_DIR="` + systemdDir + `"
		SENSOR_PROXY_INSTALL_ROOT="` + filepath.Join(tmp, "sensor-proxy") + `"
		SENSOR_PROXY_RUNTIME_DIR="` + filepath.Join(tmp, "run") + `"
		SENSOR_PROXY_WORK_DIR="` + filepath.Join(tmp, "work") + `"
		SENSOR_PROXY_CONFIG_DIR="` + filepath.Join(tmp, "config") + `"
		SENSOR_PROXY_LOG_DIR="` + filepath.Join(tmp, "log") + `"
		SENSOR_PROXY_SERVICE_USER="pulse-sensor-proxy-test"
		SENSOR_PROXY_AUTHORIZED_KEYS_PATH="` + authKeys + `"
		timeout() { shift 3; "$@"; }
        systemctl() {
            if [[ "$1" == "show" ]]; then
                case "$3" in
                    --property=LoadState) echo loaded ;;
                    --property=ActiveState) echo inactive ;;
                    --property=UnitFileState) echo disabled ;;
                    *) return 1 ;;
                esac
            fi
        }
		userdel() { echo "userdel $*" >>"` + marker + `"; return 0; }
		groupdel() { echo "groupdel $*" >>"` + marker + `"; return 0; }
		id() { return 0; }
		getent() { return 0; }
	`

	funcs := extractRootInstallShellFunction(t, "confirm_pulse_unit_removable") + "\n" +
		extractRootInstallShellFunction(t, "stop_pulse_unit_for_removal") + "\n" +
		`print_error() { echo "$*" >&2; }` + "\n" +
		extractRootInstallShellFunction(t, "local_sensor_proxy_present") + "\n" +
		extractRootInstallShellFunction(t, "remove_local_sensor_proxy_managed_keys") + "\n" +
		extractRootInstallShellFunction(t, "cleanup_local_sensor_proxy")

	out, err := exec.Command("bash", "-c", env+funcs+"\ncleanup_local_sensor_proxy\n").CombinedOutput()
	if err != nil {
		t.Fatalf("cleanup_local_sensor_proxy failed: %v\n%s", err, out)
	}

	if _, statErr := os.Stat(binPath); !os.IsNotExist(statErr) {
		t.Fatalf("expected sensor-proxy binary removed, stat err = %v", statErr)
	}
	if _, statErr := os.Stat(unitPath); !os.IsNotExist(statErr) {
		t.Fatalf("expected sensor-proxy unit removed, stat err = %v", statErr)
	}

	keysAfter, err := os.ReadFile(authKeys)
	if err != nil {
		t.Fatalf("read authorized_keys after cleanup: %v", err)
	}
	keysText := string(keysAfter)
	if strings.Contains(keysText, "pulse-managed-key") || strings.Contains(keysText, "pulse-proxy-key") {
		t.Fatalf("expected managed/proxy SSH keys stripped, got:\n%s", keysText)
	}
	for _, keep := range []string{"keepme@admin", "keep-this-too"} {
		if !strings.Contains(keysText, keep) {
			t.Fatalf("expected unrelated SSH key %q preserved, got:\n%s", keep, keysText)
		}
	}

	markerBytes, err := os.ReadFile(marker)
	if err != nil {
		t.Fatalf("expected userdel/groupdel to run: %v", err)
	}
	if !strings.Contains(string(markerBytes), "userdel") {
		t.Fatalf("expected service user removal, got marker:\n%s", markerBytes)
	}

	if !strings.Contains(string(out), "uninstall-sensor-proxy.sh") {
		t.Fatalf("expected pointer to standalone cluster cleanup script, got:\n%s", out)
	}
	if !strings.Contains(string(out), "--local-only") {
		t.Fatalf("expected standalone cleanup pointer to avoid unprovisioned cluster SSH, got:\n%s", out)
	}

	// Presence-gated: a host with no sensor-proxy footprint is a silent no-op.
	empty := t.TempDir()
	noopEnv := `
		set -uo pipefail
		SENSOR_PROXY_BINARY_PATH="` + filepath.Join(empty, "pulse-sensor-proxy") + `"
		SENSOR_PROXY_SYSTEMD_DIR="` + empty + `"
		SENSOR_PROXY_INSTALL_ROOT="` + filepath.Join(empty, "sensor-proxy") + `"
		SENSOR_PROXY_RUNTIME_DIR="` + filepath.Join(empty, "run") + `"
		SENSOR_PROXY_WORK_DIR="` + filepath.Join(empty, "work") + `"
		SENSOR_PROXY_CONFIG_DIR="` + filepath.Join(empty, "config") + `"
		SENSOR_PROXY_LOG_DIR="` + filepath.Join(empty, "log") + `"
		SENSOR_PROXY_SERVICE_USER="pulse-sensor-proxy-test"
		SENSOR_PROXY_AUTHORIZED_KEYS_PATH="` + filepath.Join(empty, "authorized_keys") + `"
		systemctl() { return 0; }
		userdel() { return 0; }
		groupdel() { return 0; }
		id() { return 0; }
		getent() { return 0; }
	`
	noopOut, err := exec.Command("bash", "-c", noopEnv+funcs+"\ncleanup_local_sensor_proxy\n").CombinedOutput()
	if err != nil {
		t.Fatalf("cleanup_local_sensor_proxy no-op path failed: %v\n%s", err, noopOut)
	}
	if strings.TrimSpace(string(noopOut)) != "" {
		t.Fatalf("expected silent no-op when no footprint present, got:\n%s", noopOut)
	}
}

// TestRootInstallUninstallWiresSensorProxyCleanup pins that uninstall_pulse
// actually invokes the local sensor-proxy cleanup (the functional test above
// only exercises the helper in isolation).
func TestRootInstallUninstallWiresSensorProxyCleanup(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}

	uninstall := extractRootInstallShellFunction(t, "uninstall_pulse")
	if !strings.Contains(uninstall, "cleanup_local_sensor_proxy") {
		t.Fatalf("uninstall_pulse does not invoke cleanup_local_sensor_proxy:\n%s", uninstall)
	}

	script := string(content)
	for _, needle := range []string{
		`cleanup_local_sensor_proxy() {`,
		`local_sensor_proxy_present() {`,
		`remove_local_sensor_proxy_managed_keys() {`,
		`# pulse-(managed|proxy)-key$`,
	} {
		if !strings.Contains(script, needle) {
			t.Fatalf("install.sh missing sensor-proxy cleanup contract: %s", needle)
		}
	}
}

// TestRootInstallExtractsPveTokenValue guards the #44/#1312 token-extraction
// hardening for the install-time auto-register path: token capture must prefer
// the deterministic `pveum --output-format json` form and parse the `value`
// field, while still recovering from the legacy box-drawing table layout that
// older pveum builds emit, so it does not silently fail or mis-parse when
// pveum's table formatting drifts.
func TestRootInstallExtractsPveTokenValue(t *testing.T) {
	fn := extractRootInstallShellFunction(t, "extract_pve_token_value")

	const secret = "12345678-1234-1234-1234-1234567890ab"

	jsonOutput := `{"full-tokenid":"pulse-monitor@pve!pulse-x","info":{"privsep":"1"},"value":"` + secret + `"}`
	tableOutput := "" +
		"┌──────────────┬──────────────────────────────────────┐\n" +
		"│ key          │ value                                │\n" +
		"╞══════════════╪══════════════════════════════════════╡\n" +
		"│ full-tokenid │ pulse-monitor@pve!pulse-x            │\n" +
		"├──────────────┼──────────────────────────────────────┤\n" +
		"│ info         │ {\"privsep\":\"1\"}                       │\n" +
		"├──────────────┼──────────────────────────────────────┤\n" +
		"│ value        │ " + secret + " │\n" +
		"└──────────────┴──────────────────────────────────────┘\n"

	cases := []struct {
		name   string
		output string
		want   string
	}{
		{"json", jsonOutput, secret},
		{"table", tableOutput, secret},
		{"garbage", "no token here\n", ""},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			script := fn + "\nextract_pve_token_value \"$TOKEN_OUTPUT\"\n"
			cmd := exec.Command("bash", "-c", script)
			cmd.Env = append(os.Environ(), "TOKEN_OUTPUT="+tc.output)
			out, err := cmd.CombinedOutput()
			if err != nil {
				t.Fatalf("extract_pve_token_value failed: %v\n%s", err, out)
			}
			if got := strings.TrimSpace(string(out)); got != tc.want {
				t.Fatalf("extract_pve_token_value(%s) = %q, want %q", tc.name, got, tc.want)
			}
		})
	}
}

// TestRootInstallAutoRegisterPrefersJsonTokenForm pins that the install-time
// auto-register path requests the JSON form first and keeps the legacy table
// form only as an explicit fallback (so the secure-installer contract pin on
// the bare form stays satisfied).
func TestRootInstallAutoRegisterPrefersJsonTokenForm(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}
	script := string(content)

	required := []string{
		`pveum user token add pulse-monitor@pve "$token_name" --privsep 1 --output-format json 2>&1`,
		`pveum user token add pulse-monitor@pve "$token_name" --privsep 1 2>&1`,
		`token_value=$(extract_pve_token_value "$token_output"`,
		`extract_pve_token_value() {`,
	}
	for _, needle := range required {
		if !strings.Contains(script, needle) {
			t.Fatalf("install.sh missing hardened token-extraction contract: %s", needle)
		}
	}

	jsonIdx := strings.Index(script, `--privsep 1 --output-format json 2>&1`)
	bareIdx := strings.Index(script, "\n        token_output=$(pveum user token add pulse-monitor@pve \"$token_name\" --privsep 1 2>&1)")
	if jsonIdx < 0 || bareIdx < 0 || jsonIdx > bareIdx {
		t.Fatalf("expected JSON token form to precede the legacy table fallback (json=%d bare=%d)", jsonIdx, bareIdx)
	}
}

func TestRootInstallAutoRegisterRotatesExistingDeterministicToken(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}
	script := string(content)

	required := []string{
		`pve_token_already_exists_error() {`,
		`create_pve_auto_register_token() {`,
		`create_pve_auto_register_token "$token_name" token_output token_status`,
		`pve_token_already_exists_error "$token_output"`,
		`pveum user token remove pulse-monitor@pve "$token_name"`,
		`Existing Proxmox monitoring token '${token_name}' found; rotating it so Pulse receives a fresh secret`,
	}
	for _, needle := range required {
		if !strings.Contains(script, needle) {
			t.Fatalf("install.sh missing deterministic token rotation contract: %s", needle)
		}
	}

	alreadyExistsIdx := strings.Index(script, `pve_token_already_exists_error "$token_output"`)
	removeIdx := strings.Index(script, `pveum user token remove pulse-monitor@pve "$token_name"`)
	retryIdx := strings.LastIndex(script, `create_pve_auto_register_token "$token_name" token_output token_status`)
	if alreadyExistsIdx < 0 || removeIdx < 0 || retryIdx < 0 || !(alreadyExistsIdx < removeIdx && removeIdx < retryIdx) {
		t.Fatalf("expected existing-token detection to remove then retry token creation (exists=%d remove=%d retry=%d)", alreadyExistsIdx, removeIdx, retryIdx)
	}
	if strings.Contains(script, `pveum user token remove pulse-monitor@pve`) && !strings.Contains(script, `pve_token_already_exists_error "$token_output"`) {
		t.Fatalf("token removal must stay gated by an explicit existing-token create error")
	}
}

func TestRootInstallAutoRegisterSmokeTestsCreatedTokenBeforeRegistration(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}
	script := string(content)

	for _, needle := range []string{
		`smoke_test_pve_auto_register_token() {`,
		`printf 'Authorization: PVEAPIToken=%s=%s\n' "$token_id" "$token_value" | curl --retry 2 --retry-delay 1 -kfsS -H @- "${host_url%/}/api2/json/nodes"`,
		`AUTO_NODE_REGISTER_ERROR="token smoke check failed"`,
		`smoke_test_pve_auto_register_token "$normalized_host_url" "$token_id" "$token_value"`,
	} {
		if !strings.Contains(script, needle) {
			t.Fatalf("install.sh missing Proxmox token smoke-check contract: %s", needle)
		}
	}

	smokeCallIdx := strings.Index(script, `smoke_test_pve_auto_register_token "$normalized_host_url" "$token_id" "$token_value"`)
	registerIdx := strings.Index(script, `curl --retry 3 --retry-delay 2 -fsS -X POST "$pulse_url/api/auto-register" -H "Content-Type: application/json" -d @-`)
	if smokeCallIdx < 0 || registerIdx < 0 || smokeCallIdx > registerIdx {
		t.Fatalf("expected token smoke check to run before /api/auto-register (smoke=%d register=%d)", smokeCallIdx, registerIdx)
	}
}

func TestRootInstallDeployAgentScriptsDeploysSignatureSidecars(t *testing.T) {
	extractDir := t.TempDir()
	installDir := t.TempDir()

	scriptsSrc := filepath.Join(extractDir, "scripts")
	if err := os.MkdirAll(scriptsSrc, 0o755); err != nil {
		t.Fatalf("mkdir scripts: %v", err)
	}
	for _, name := range []string{
		"install.sh", "install.sh.sig", "install.sh.sshsig",
		"install.ps1", "install.ps1.sig", "install.ps1.sshsig",
	} {
		if err := os.WriteFile(filepath.Join(scriptsSrc, name), []byte("payload-"+name), 0o644); err != nil {
			t.Fatalf("write %s: %v", name, err)
		}
	}

	script := `
		set -euo pipefail
		print_warn() { :; }
		print_success() { :; }
		print_error() { printf '%s\n' "$*" >&2; }
		chown() { :; }
		INSTALL_DIR="` + installDir + `"
` + extractRootInstallShellFunction(t, "deploy_agent_scripts") + `
		deploy_agent_scripts "` + extractDir + `"
	`

	if out, err := exec.Command("bash", "-c", script).CombinedOutput(); err != nil {
		t.Fatalf("deploy_agent_scripts failed: %v\n%s", err, out)
	}

	for _, name := range []string{
		"install.sh", "install.sh.sig", "install.sh.sshsig",
		"install.ps1", "install.ps1.sig", "install.ps1.sshsig",
	} {
		if _, err := os.Stat(filepath.Join(installDir, "scripts", name)); err != nil {
			t.Fatalf("deploy_agent_scripts did not deploy %s next to the served script: %v", name, err)
		}
	}
}

// Script bytes and their detached signatures publish as one directory bundle.
// Real archive/source producers and all main consumers use confined files and
// manager/build doubles; no host service or published payload is exercised.
func TestRootInstallAgentScriptPublicationPreservesPreviousBundle(t *testing.T) {
	runPythonFixtureControls(t, repoFile("scripts", "tests", "test_server_script_publication.py"), "ServerScriptPublication", []string{
		"test_preparation_failures_preserve_the_complete_old_bundle",
		"test_failed_or_skipped_publication_restores_old_bundle",
		"test_failed_restore_retains_original_bundle_and_names_its_location",
		"test_symlinked_script_is_replaced_without_writing_its_target",
		"test_symlinked_bundle_directory_is_not_replaced",
		"test_success_publishes_scripts_and_both_sidecars_preserving_other_files",
		"test_unsigned_source_scripts_do_not_inherit_stale_release_sidecars",
		"test_archive_and_source_consumers_fail_and_only_recover_prior_active_service",
		"test_recovery_message_does_not_claim_previous_binary",
		"test_uncertain_bundle_restore_does_not_restart_the_server",
		"test_every_main_consumer_stops_before_markers_or_completion",
		"test_archive_source_and_main_consumers_complete_with_a_whole_bundle",
	}, 30*time.Second, "PULSE_INSTALLER_UNDER_TEST="+repoFile("install.sh"))
}

// Regression test for the corrupted ExecCondition: setup_auto_updates used to
// render the pulse-update.service unit through an unquoted heredoc containing
// `$${PULSE_SERVICE_NAME}`, which bash expanded to the installer's PID. The
// resulting condition always failed, so systemd silently skipped every
// scheduled auto-update run. This test renders the real unit and executes the
// rendered ExecCondition command, instead of asserting source-text fragments.
func TestSetupAutoUpdatesRendersExecutableExecCondition(t *testing.T) {
	for _, tc := range []struct {
		name        string
		serviceName string // empty = rely on the default
		want        string
	}{
		{name: "default service name", serviceName: "", want: "pulse"},
		{name: "instance-scoped service name", serviceName: "pulse-blue", want: "pulse-blue"},
	} {
		t.Run(tc.name, func(t *testing.T) {
			tmpDir := t.TempDir()
			configDir := filepath.Join(tmpDir, "config")
			installDir := filepath.Join(tmpDir, "install")
			autoUpdateSrc := filepath.Join(installDir, "scripts", "pulse-auto-update.sh")
			autoUpdateDest, servicePath, timerPath := prepareAutoUpdatePaths(t, tmpDir)

			if err := os.MkdirAll(configDir, 0755); err != nil {
				t.Fatalf("mkdir config dir: %v", err)
			}
			if err := os.MkdirAll(filepath.Dir(autoUpdateSrc), 0755); err != nil {
				t.Fatalf("mkdir auto-update src dir: %v", err)
			}
			if err := os.WriteFile(autoUpdateSrc, []byte("#!/usr/bin/env bash\n"), 0755); err != nil {
				t.Fatalf("write auto-update src: %v", err)
			}

			serviceNameLine := ""
			if tc.serviceName != "" {
				serviceNameLine = `SERVICE_NAME="` + tc.serviceName + `"`
			}
			script := `
		CONFIG_DIR="` + configDir + `"
		INSTALL_DIR="` + installDir + `"
		PULSE_AUTO_UPDATE_DEST="` + autoUpdateDest + `"
		PULSE_UPDATE_SERVICE_PATH="` + servicePath + `"
		PULSE_UPDATE_TIMER_PATH="` + timerPath + `"
		` + serviceNameLine + `
		FORCE_CHANNEL=""
		UPDATE_CHANNEL=""
		GITHUB_REPO="rcourtman/Pulse"
		print_info() { :; }
		print_warn() { :; }
		print_success() { :; }
		safe_systemctl() { :; }
		systemctl() { return 0; }
		chown() { :; }
` + extractSetupAutoUpdatesShellFunctions(t) + `
		setup_auto_updates
	`

			out, err := exec.Command("bash", "-c", script).CombinedOutput()
			if err != nil {
				t.Fatalf("bash: %v\n%s", err, out)
			}

			unitBytes, err := os.ReadFile(servicePath)
			if err != nil {
				t.Fatalf("read rendered service unit: %v", err)
			}
			unit := string(unitBytes)

			wantLine := `ExecCondition=/bin/sh -c 'systemctl is-active --quiet ` + tc.want + `'`
			if !strings.Contains(unit, wantLine+"\n") {
				t.Fatalf("rendered unit missing %q:\n%s", wantLine, unit)
			}
			// The heredoc substitutes every variable at render time; any $
			// left in the unit means an unexpanded (or PID-corrupted)
			// reference leaked through again.
			if strings.Contains(unit, "$") {
				t.Fatalf("rendered unit contains an unexpanded $:\n%s", unit)
			}

			// Execute the rendered condition the way systemd would, with a
			// recording systemctl stub, to prove the command itself is sound.
			binDir := filepath.Join(tmpDir, "stub-bin")
			if err := os.MkdirAll(binDir, 0755); err != nil {
				t.Fatalf("mkdir stub bin: %v", err)
			}
			recordPath := filepath.Join(tmpDir, "systemctl-args")
			stub := "#!/bin/sh\nprintf '%s' \"$*\" > \"" + recordPath + "\"\nexit 0\n"
			if err := os.WriteFile(filepath.Join(binDir, "systemctl"), []byte(stub), 0755); err != nil {
				t.Fatalf("write systemctl stub: %v", err)
			}
			condition := strings.TrimPrefix(wantLine, "ExecCondition=")
			condOut, err := exec.Command("bash", "-c", `PATH="`+binDir+`:$PATH" `+condition).CombinedOutput()
			if err != nil {
				t.Fatalf("rendered ExecCondition failed to execute: %v\n%s", err, condOut)
			}
			recorded, err := os.ReadFile(recordPath)
			if err != nil {
				t.Fatalf("ExecCondition never invoked systemctl: %v", err)
			}
			if got, want := string(recorded), "is-active --quiet "+tc.want; got != want {
				t.Fatalf("ExecCondition invoked systemctl %q, want %q", got, want)
			}
		})
	}
}

// Regression test for stale updater scripts surviving upgrades: a v5 box with
// auto-updates already enabled keeps pulse-update.timer, so the update flow
// never re-ran setup_auto_updates and the v5.1-pinned helper script stayed in
// place, logging "Already running latest version" forever. refresh_auto_updates
// must replace the helper and units without touching system.json or the
// timer's enabled/started state.
func TestRefreshAutoUpdatesReplacesStaleHelperWithoutChangingEnablement(t *testing.T) {
	tmpDir := t.TempDir()
	configDir := filepath.Join(tmpDir, "config")
	installDir := filepath.Join(tmpDir, "install")
	autoUpdateSrc := filepath.Join(installDir, "scripts", "pulse-auto-update.sh")
	autoUpdateDest, servicePath, timerPath := prepareAutoUpdatePaths(t, tmpDir)
	callsPath := filepath.Join(tmpDir, "systemctl-calls")

	if err := os.MkdirAll(configDir, 0755); err != nil {
		t.Fatalf("mkdir config dir: %v", err)
	}
	if err := os.MkdirAll(filepath.Dir(autoUpdateSrc), 0755); err != nil {
		t.Fatalf("mkdir auto-update src dir: %v", err)
	}
	if err := os.WriteFile(autoUpdateSrc, []byte("#!/usr/bin/env bash\necho v6-helper\n"), 0755); err != nil {
		t.Fatalf("write auto-update src: %v", err)
	}
	// The stale v5.1-pinned helper and a v5-style unit without ExecCondition.
	if err := os.WriteFile(autoUpdateDest, []byte("#!/usr/bin/env bash\necho v5-stale-helper\n"), 0755); err != nil {
		t.Fatalf("write stale auto-update dest: %v", err)
	}
	if err := os.WriteFile(servicePath, []byte("[Service]\nExecStart="+autoUpdateDest+"\n"), 0644); err != nil {
		t.Fatalf("write stale service unit: %v", err)
	}
	// The user explicitly disabled auto-updates; a refresh must not flip it.
	systemJSON := `{"autoUpdateEnabled":false,"updateChannel":"stable"}`
	if err := os.WriteFile(filepath.Join(configDir, "system.json"), []byte(systemJSON), 0644); err != nil {
		t.Fatalf("write system.json: %v", err)
	}

	script := `
		CONFIG_DIR="` + configDir + `"
		INSTALL_DIR="` + installDir + `"
		PULSE_AUTO_UPDATE_DEST="` + autoUpdateDest + `"
		PULSE_UPDATE_SERVICE_PATH="` + servicePath + `"
		PULSE_UPDATE_TIMER_PATH="` + timerPath + `"
		GITHUB_REPO="rcourtman/Pulse"
		print_info() { :; }
		print_warn() { :; }
		safe_systemctl() { printf '%s\n' "$*" >> "` + callsPath + `"; }
` + extractRootInstallShellFunction(t, "repo_web_url") + `
` + extractRootInstallShellFunction(t, "configure_auto_update_script_repo") + `
` + extractInstallAutoUpdateAssetsShellFunctions(t) + `
` + extractRootInstallShellFunction(t, "refresh_auto_updates") + `
		refresh_auto_updates
	`

	out, err := exec.Command("bash", "-c", script).CombinedOutput()
	if err != nil {
		t.Fatalf("bash: %v\n%s", err, out)
	}

	helper, err := os.ReadFile(autoUpdateDest)
	if err != nil {
		t.Fatalf("read refreshed helper: %v", err)
	}
	if strings.Contains(string(helper), "v5-stale-helper") {
		t.Fatalf("refresh left the stale helper in place:\n%s", helper)
	}
	if !strings.Contains(string(helper), "v6-helper") {
		t.Fatalf("refresh did not install the release helper:\n%s", helper)
	}

	unit, err := os.ReadFile(servicePath)
	if err != nil {
		t.Fatalf("read refreshed service unit: %v", err)
	}
	if !strings.Contains(string(unit), "ExecCondition=/bin/sh -c 'systemctl is-active --quiet pulse'") {
		t.Fatalf("refresh did not rewrite the service unit:\n%s", unit)
	}
	if _, err := os.Stat(timerPath); err != nil {
		t.Fatalf("refresh did not write the timer unit: %v", err)
	}

	gotJSON, err := os.ReadFile(filepath.Join(configDir, "system.json"))
	if err != nil {
		t.Fatalf("read system.json: %v", err)
	}
	if string(gotJSON) != systemJSON {
		t.Fatalf("refresh modified system.json:\n got: %s\nwant: %s", gotJSON, systemJSON)
	}

	calls, err := os.ReadFile(callsPath)
	if err != nil {
		t.Fatalf("read recorded systemctl calls: %v", err)
	}
	if string(calls) != "daemon-reload\n" {
		t.Fatalf("refresh changed systemd state beyond daemon-reload:\n%s", calls)
	}
}

// Pins the wiring: every existing-install flow (update, reinstall, --version,
// --source) and the fresh-install tail must refresh already-installed
// auto-update assets when the user did not opt into a full re-setup.
func TestRootInstallScriptUpdateFlowsRefreshExistingAutoUpdateAssets(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}

	if !strings.Contains(string(content), "refresh_auto_updates() {") {
		t.Fatal("install.sh missing refresh_auto_updates definition")
	}

	wired := regexp.MustCompile(`(?m)^\s*elif update_timer_exists; then\n\s*refresh_auto_updates$`)
	if got := len(wired.FindAll(content, -1)); got < 5 {
		t.Fatalf("expected at least 5 install flows to refresh existing auto-update assets, found %d", got)
	}
}

// The tested --version, menu-update and menu-reinstall paths must all use the
// same affirmative-only prompt; a new inline default-yes branch is unsafe.
func TestRootInstallExistingFlowsShareExplicitAutoUpdateChoice(t *testing.T) {
	main := extractRootInstallShellFunction(t, "main")
	if got := strings.Count(main, "offer_existing_auto_updates"); got != 3 {
		t.Fatalf("expected three existing-install consent calls, found %d", got)
	}
	if strings.Contains(main, `Enable auto-updates? [Y/n]`) || strings.Contains(main, `Re-enable auto-updates? [Y/n]`) {
		t.Fatal("existing install must not default to enabling auto-updates")
	}
	if !strings.Contains(main, `safe_read_with_default "Enable auto-updates? [y/N]: " enable_updates "n"`) {
		t.Fatal("fresh-install consent must remain opt-in")
	}
}

// Regression test for #1526 (and the earlier #1396): when the installer is piped
// to bash (curl ... | bash) there is no source file, so BASH_SOURCE is unset.
// The "am I being sourced?" guard must default the lookup or `set -u` aborts the
// whole run before the installer body with "BASH_SOURCE[0]: unbound variable".
func TestRootInstallScriptSourceGuardSurvivesPipedExecution(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}
	script := string(content)

	if strings.Contains(script, `[[ "${BASH_SOURCE[0]}" == "$0" ]] || return 0`) {
		t.Fatal("install.sh still uses the unguarded BASH_SOURCE source check that aborts under curl | bash")
	}
	guard := `if [[ -n "${BASH_SOURCE[0]:-}" && "${BASH_SOURCE[0]}" != "$0" ]]; then`
	if !strings.Contains(script, guard) {
		t.Fatalf("install.sh missing piped-safe source guard: %s", guard)
	}

	// Run the guard the way `curl ... | bash` does: fed through stdin with no
	// source file, so BASH_SOURCE is empty. It must fall through to the body.
	harness := "set -euo pipefail\n" + guard + "\n    return 0\nfi\necho INSTALLER_BODY_REACHED\n"
	cmd := exec.Command("bash")
	cmd.Stdin = strings.NewReader(harness)
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("piped source guard failed: %v\n%s", err, out)
	}
	got := string(out)
	if strings.Contains(got, "unbound variable") {
		t.Fatalf("source guard aborted piped execution with unbound variable:\n%s", got)
	}
	if !strings.Contains(got, "INSTALLER_BODY_REACHED") {
		t.Fatalf("source guard did not fall through to the installer body when piped:\n%s", got)
	}
}

func TestRootInstallServiceGrantsIcmpProbeCapability(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}

	script := string(content)
	required := []string{
		`NoNewPrivileges=true`,
		`AmbientCapabilities=CAP_NET_RAW`,
		`CapabilityBoundingSet=CAP_NET_RAW`,
	}
	for _, needle := range required {
		if !strings.Contains(script, needle) {
			t.Fatalf("install.sh missing systemd ICMP capability grant: %s", needle)
		}
	}

	// The capability grant must live in the same hardening block that sets
	// NoNewPrivileges, so every unit the installer writes gets both.
	hardening := script[strings.Index(script, "# Security hardening"):]
	if end := strings.Index(hardening, "[Install]"); end >= 0 {
		hardening = hardening[:end]
	}
	if !strings.Contains(hardening, "AmbientCapabilities=CAP_NET_RAW") {
		t.Fatal("AmbientCapabilities=CAP_NET_RAW is not in the unit's security hardening block")
	}
}

// TestRootInstallScriptUpdateHelperWriteIsNonFatalOnReadOnlyPath asserts the
// #1630 guarantee: setup_update_command's write of the /bin/update helper
// (and its PATH appends) must not abort the installer under errexit when the
// destination is unwritable. The stock pulse-update.service runs the
// unattended updater with ProtectSystem=strict; its ReadWritePaths covers the
// install dir, config dir, /tmp and the auto-update helper and unit
// directories, so /bin (the default helper path) and /etc/profile stay
// read-only. The old behavior killed the installer after the new binary was
// installed and the service stopped, and the auto-update rollback then left
// Pulse down. A regular file as the "parent directory"
// makes writes beneath it fail with ENOTDIR, which also fails when the test
// runs as root (unlike chmod 555).
func TestRootInstallScriptUpdateHelperWriteIsNonFatalOnReadOnlyPath(t *testing.T) {
	script := `
set -euo pipefail
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT
touch "$TMP/blocker"
GITHUB_REPO="rcourtman/Pulse"
INSTALL_SIGNATURE_IDENTITY="pulse-installer"
INSTALL_SIGNATURE_NAMESPACE="pulse-install"
PINNED_RELEASE_SSH_PUBLIC_KEY="test-key"
print_warn() { echo "WARN: $*"; }
print_success() { echo "OK: $*"; }
release_signature_key_available() { :; }
require_release_signature_verifier() { :; }
verify_release_signature() { :; }
` + extractRootInstallShellFunction(t, "setup_update_command") + `
UPDATE_HELPER_PATH="$TMP/blocker/update" \
PULSE_PROFILE_PATH="$TMP/profile" \
PULSE_BASHRC_PATH="$TMP/bashrc" \
setup_update_command
echo "SURVIVED_UNWRITABLE"
UPDATE_HELPER_PATH="$TMP/bin/update" \
PULSE_PROFILE_PATH="$TMP/profile" \
PULSE_BASHRC_PATH="$TMP/bashrc" \
setup_update_command
[[ -x "$TMP/bin/update" ]] && echo "HELPER_WRITTEN"
grep -q "Pulse update command" "$TMP/bin/update" && echo "HELPER_BODY_OK"
`

	out, err := exec.Command("bash", "-c", script).CombinedOutput()
	if err != nil {
		t.Fatalf("setup_update_command aborted the installer on an unwritable helper path: %v\n%s", err, out)
	}
	got := string(out)
	for _, want := range []string{"SURVIVED_UNWRITABLE", "WARN:", "HELPER_WRITTEN", "HELPER_BODY_OK"} {
		if !strings.Contains(got, want) {
			t.Fatalf("missing %q in setup_update_command output:\n%s", want, got)
		}
	}
}

// TestRootInstallScriptBinarySymlinkIsIdempotentAndNonFatal asserts the
// companion #1630 guarantee for the /usr/local/bin/pulse convenience
// symlink: an unwritable link path only warns, a writable one creates the
// link, and an already-correct link is kept without needing ln at all
// (the update-run case where the link survives but the fs is read-only).
func TestRootInstallScriptBinarySymlinkIsIdempotentAndNonFatal(t *testing.T) {
	script := `
set -euo pipefail
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT
touch "$TMP/blocker"
mkdir -p "$TMP/bin"
touch "$TMP/bin/pulse-binary"
print_warn() { echo "WARN: $*"; }
print_success() { echo "OK: $*"; }
` + extractRootInstallShellFunction(t, "install_binary_symlink") + `
install_binary_symlink "$TMP/bin/pulse-binary" "$TMP/blocker/pulse"
echo "SURVIVED_UNWRITABLE"
install_binary_symlink "$TMP/bin/pulse-binary" "$TMP/bin/pulse"
[[ "$(readlink "$TMP/bin/pulse")" == "$TMP/bin/pulse-binary" ]] && echo "LINK_CREATED"
ln() { echo "LN_CALLED_AGAIN"; return 1; }
install_binary_symlink "$TMP/bin/pulse-binary" "$TMP/bin/pulse"
echo "SURVIVED_EXISTING_LINK"
`

	out, err := exec.Command("bash", "-c", script).CombinedOutput()
	if err != nil {
		t.Fatalf("install_binary_symlink aborted under errexit: %v\n%s", err, out)
	}
	got := string(out)
	for _, want := range []string{"SURVIVED_UNWRITABLE", "WARN:", "LINK_CREATED", "already in place", "SURVIVED_EXISTING_LINK"} {
		if !strings.Contains(got, want) {
			t.Fatalf("missing %q in install_binary_symlink output:\n%s", want, got)
		}
	}
	if strings.Contains(got, "LN_CALLED_AGAIN") {
		t.Fatalf("install_binary_symlink should not invoke ln when the correct link already exists:\n%s", got)
	}
}

// renderAutoUpdateUnits runs install_auto_update_assets against a seeded
// release helper and returns the rendered service and timer unit contents.
func renderAutoUpdateUnits(t *testing.T) (string, string, string, string) {
	t.Helper()

	tmpDir := t.TempDir()
	configDir := filepath.Join(tmpDir, "config")
	installDir := filepath.Join(tmpDir, "install")
	autoUpdateSrc := filepath.Join(installDir, "scripts", "pulse-auto-update.sh")
	autoUpdateDest, servicePath, timerPath := prepareAutoUpdatePaths(t, tmpDir)

	if err := os.MkdirAll(configDir, 0755); err != nil {
		t.Fatalf("mkdir config dir: %v", err)
	}
	if err := os.MkdirAll(filepath.Dir(autoUpdateSrc), 0755); err != nil {
		t.Fatalf("mkdir auto-update src dir: %v", err)
	}
	if err := os.WriteFile(autoUpdateSrc, []byte("#!/usr/bin/env bash\n"), 0755); err != nil {
		t.Fatalf("write auto-update src: %v", err)
	}

	script := `
		CONFIG_DIR="` + configDir + `"
		INSTALL_DIR="` + installDir + `"
		PULSE_AUTO_UPDATE_DEST="` + autoUpdateDest + `"
		PULSE_UPDATE_SERVICE_PATH="` + servicePath + `"
		PULSE_UPDATE_TIMER_PATH="` + timerPath + `"
		GITHUB_REPO="rcourtman/Pulse"
		print_info() { :; }
		print_warn() { :; }
		safe_systemctl() { :; }
` + extractRootInstallShellFunction(t, "repo_web_url") + `
` + extractRootInstallShellFunction(t, "configure_auto_update_script_repo") + `
` + extractInstallAutoUpdateAssetsShellFunctions(t) + `
		install_auto_update_assets
	`

	out, err := exec.Command("bash", "-c", script).CombinedOutput()
	if err != nil {
		t.Fatalf("bash: %v\n%s", err, out)
	}

	serviceBytes, err := os.ReadFile(servicePath)
	if err != nil {
		t.Fatalf("read rendered service unit: %v", err)
	}
	timerBytes, err := os.ReadFile(timerPath)
	if err != nil {
		t.Fatalf("read rendered timer unit: %v", err)
	}
	return string(serviceBytes), string(timerBytes), autoUpdateDest, servicePath
}

// Regression test for the doubled auto-update schedule (issue #1643): the
// rendered timer carried both OnCalendar=daily and OnCalendar=02:00, so with
// RandomizedDelaySec=4h every box attempted two updates per day, one in the
// 00:00-04:00 window and one in 02:00-06:00. Exactly one OnCalendar line may
// survive, and it must be the documented 02:00 schedule.
func TestAutoUpdateTimerSchedulesSingleDailyRun(t *testing.T) {
	_, timer, _, _ := renderAutoUpdateUnits(t)

	var schedules []string
	for _, line := range strings.Split(timer, "\n") {
		if strings.HasPrefix(strings.TrimSpace(line), "OnCalendar=") {
			schedules = append(schedules, strings.TrimSpace(line))
		}
	}
	if len(schedules) != 1 {
		t.Fatalf("rendered timer must hold exactly one OnCalendar line, got %d:\n%s", len(schedules), timer)
	}
	if schedules[0] != "OnCalendar=*-*-* 02:00:00" {
		t.Fatalf("rendered timer schedule = %q, want the documented 02:00 daily run:\n%s", schedules[0], timer)
	}
	if !strings.Contains(timer, "RandomizedDelaySec=4h\n") {
		t.Fatalf("rendered timer lost the 4h random spread:\n%s", timer)
	}
}

// Regression test for the sandbox that froze updater fixes (issue #1637
// triage): pulse-update.service runs install.sh with ProtectSystem=strict and
// ReadWritePaths that excluded the helper and unit directories, so
// refresh_auto_updates could never replace /usr/local/bin/pulse-auto-update.sh
// or rewrite the units during an unattended update — helper fixes only reached
// boxes via manual installs. The rendered sandbox must grant write access to
// both directories.
func TestAutoUpdateServiceSandboxAllowsHelperAndUnitRefresh(t *testing.T) {
	service, _, autoUpdateDest, servicePath := renderAutoUpdateUnits(t)

	var rwLine string
	for _, line := range strings.Split(service, "\n") {
		if strings.HasPrefix(line, "ReadWritePaths=") {
			rwLine = line
		}
	}
	if rwLine == "" {
		t.Fatalf("rendered service unit lost its ReadWritePaths line:\n%s", service)
	}
	paths := strings.Fields(strings.TrimPrefix(rwLine, "ReadWritePaths="))
	want := map[string]bool{
		filepath.Dir(autoUpdateDest): false,
		filepath.Dir(servicePath):    false,
	}
	for _, p := range paths {
		if _, ok := want[p]; ok {
			want[p] = true
		}
	}
	for dir, found := range want {
		if !found {
			t.Fatalf("ReadWritePaths %q is missing %q; unattended refreshes cannot write there:\n%s", rwLine, dir, service)
		}
	}
}

// Regression test for the destructive failure path in
// install_auto_update_assets (issue #1637 triage): a failed
// configure_auto_update_script_repo used to rm -f the installed helper,
// leaving the still-enabled timer with a dangling ExecStart. The helper is now
// staged in its destination directory and swapped in with an atomic rename
// only after configuration succeeds, so any failure must leave the previously
// working helper untouched, the units unwritten, and no staging litter behind.
func TestInstallAutoUpdateAssetsKeepsWorkingHelperWhenConfigureFails(t *testing.T) {
	tmpDir := t.TempDir()
	configDir := filepath.Join(tmpDir, "config")
	installDir := filepath.Join(tmpDir, "install")
	autoUpdateSrc := filepath.Join(installDir, "scripts", "pulse-auto-update.sh")
	autoUpdateDest, servicePath, timerPath := prepareAutoUpdatePaths(t, tmpDir)

	if err := os.MkdirAll(configDir, 0755); err != nil {
		t.Fatalf("mkdir config dir: %v", err)
	}
	if err := os.MkdirAll(filepath.Dir(autoUpdateSrc), 0755); err != nil {
		t.Fatalf("mkdir auto-update src dir: %v", err)
	}
	if err := os.WriteFile(autoUpdateSrc, []byte("#!/usr/bin/env bash\necho new-helper\n"), 0755); err != nil {
		t.Fatalf("write auto-update src: %v", err)
	}
	workingHelper := "#!/usr/bin/env bash\necho working-helper\n"
	if err := os.WriteFile(autoUpdateDest, []byte(workingHelper), 0755); err != nil {
		t.Fatalf("write installed helper: %v", err)
	}

	// The failing configure stub is defined after the extracted functions so
	// it overrides the real implementation.
	script := `
		CONFIG_DIR="` + configDir + `"
		INSTALL_DIR="` + installDir + `"
		PULSE_AUTO_UPDATE_DEST="` + autoUpdateDest + `"
		PULSE_UPDATE_SERVICE_PATH="` + servicePath + `"
		PULSE_UPDATE_TIMER_PATH="` + timerPath + `"
		GITHUB_REPO="rcourtman/Pulse"
		print_info() { :; }
		print_warn() { :; }
		safe_systemctl() { :; }
` + extractRootInstallShellFunction(t, "repo_web_url") + `
` + extractInstallAutoUpdateAssetsShellFunctions(t) + `
		configure_auto_update_script_repo() { return 1; }
		if install_auto_update_assets; then
			echo "UNEXPECTED_SUCCESS"
		else
			echo "FAILED_AS_EXPECTED"
		fi
	`

	out, err := exec.Command("bash", "-c", script).CombinedOutput()
	if err != nil {
		t.Fatalf("bash: %v\n%s", err, out)
	}
	if !strings.Contains(string(out), "FAILED_AS_EXPECTED") {
		t.Fatalf("install_auto_update_assets should report failure when configure fails:\n%s", out)
	}

	helper, err := os.ReadFile(autoUpdateDest)
	if err != nil {
		t.Fatalf("installed helper is gone after failed configure: %v", err)
	}
	if string(helper) != workingHelper {
		t.Fatalf("failed configure replaced the working helper:\n%s", helper)
	}
	if _, err := os.Stat(servicePath); !os.IsNotExist(err) {
		t.Fatalf("failed configure still rewrote the service unit (stat err %v)", err)
	}
	entries, err := os.ReadDir(filepath.Dir(autoUpdateDest))
	if err != nil {
		t.Fatalf("read helper dir: %v", err)
	}
	for _, entry := range entries {
		if strings.Contains(entry.Name(), ".staged.") {
			t.Fatalf("failed configure left staging litter behind: %s", entry.Name())
		}
	}
}

// autoUpdateAssetsHarness builds a bash harness that runs the real
// install_auto_update_assets against temp paths, with a seeded release helper
// and a previously installed working helper. extra is appended after the
// extracted functions so it can override them with stubs.
func autoUpdateAssetsHarness(t *testing.T, tmpDir string, autoUpdateDest, servicePath, timerPath, extra string) string {
	t.Helper()

	configDir := filepath.Join(tmpDir, "config")
	installDir := filepath.Join(tmpDir, "install")
	autoUpdateSrc := filepath.Join(installDir, "scripts", "pulse-auto-update.sh")

	if err := os.MkdirAll(configDir, 0755); err != nil {
		t.Fatalf("mkdir config dir: %v", err)
	}
	if err := os.MkdirAll(filepath.Dir(autoUpdateSrc), 0755); err != nil {
		t.Fatalf("mkdir auto-update src dir: %v", err)
	}
	if err := os.WriteFile(autoUpdateSrc, []byte("#!/usr/bin/env bash\necho new-helper\n"), 0755); err != nil {
		t.Fatalf("write auto-update src: %v", err)
	}

	return `
		CONFIG_DIR="` + configDir + `"
		INSTALL_DIR="` + installDir + `"
		PULSE_AUTO_UPDATE_DEST="` + autoUpdateDest + `"
		PULSE_UPDATE_SERVICE_PATH="` + servicePath + `"
		PULSE_UPDATE_TIMER_PATH="` + timerPath + `"
		GITHUB_REPO="rcourtman/Pulse"
		print_info() { :; }
		print_warn() { echo "WARN: $*"; }
		print_success() { :; }
		safe_systemctl() { :; }
` + extractRootInstallShellFunction(t, "repo_web_url") + `
` + extractRootInstallShellFunction(t, "configure_auto_update_script_repo") + `
` + extractInstallAutoUpdateAssetsShellFunctions(t) + `
` + extra + `
		if install_auto_update_assets; then
			echo "UNEXPECTED_SUCCESS"
		else
			echo "FAILED_AS_EXPECTED"
		fi
	`
}

// assertNoAutoUpdateStagingLitter fails when a staged helper or a staged unit
// file survived a run. `.service.tmp` is not a unit suffix systemd loads, but
// leaving one behind still means the function abandoned a partial write.
func assertNoAutoUpdateStagingLitter(t *testing.T, dirs ...string) {
	t.Helper()

	for _, dir := range dirs {
		entries, err := os.ReadDir(dir)
		if err != nil {
			t.Fatalf("read %s: %v", dir, err)
		}
		for _, entry := range entries {
			name := entry.Name()
			if strings.Contains(name, ".staged.") || strings.HasSuffix(name, ".tmp") {
				t.Fatalf("run left staging litter behind in %s: %s", dir, name)
			}
		}
	}
}

// Regression test for the unchecked staging copy in install_auto_update_assets:
// the bundled helper was copied into the staged mktemp file with a bare `cp`,
// and because both call sites invoke the function under `if !` errexit is
// suppressed for its whole body. A cp that failed (ENOSPC) therefore fell
// through to configure_auto_update_script_repo, whose awk happily emits a lone
// GITHUB_REPO= line for empty input, and the resulting one-line shebang-less
// stub replaced the working helper with a "script" whose only behaviour is to
// exit 0 — silently disabling unattended updates on the box. A failed or
// truncated copy must leave the installed helper and the units untouched.
func TestInstallAutoUpdateAssetsKeepsWorkingHelperWhenStagingCopyFails(t *testing.T) {
	workingHelper := "#!/usr/bin/env bash\necho working-helper\n"

	for _, tc := range []struct {
		name string
		stub string
	}{
		{
			name: "copy reports failure",
			stub: `cp() { return 1; }`,
		},
		{
			// cp that "succeeds" having written nothing: the shape the awk
			// rewrite turns into a plausible-looking one-line helper.
			name: "copy silently produces an empty file",
			stub: `cp() { : > "$2"; }`,
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			tmpDir := t.TempDir()
			autoUpdateDest, servicePath, timerPath := prepareAutoUpdatePaths(t, tmpDir)
			if err := os.WriteFile(autoUpdateDest, []byte(workingHelper), 0755); err != nil {
				t.Fatalf("write installed helper: %v", err)
			}

			script := autoUpdateAssetsHarness(t, tmpDir, autoUpdateDest, servicePath, timerPath, tc.stub)
			out, err := exec.Command("bash", "-c", script).CombinedOutput()
			if err != nil {
				t.Fatalf("bash: %v\n%s", err, out)
			}
			if !strings.Contains(string(out), "FAILED_AS_EXPECTED") {
				t.Fatalf("install_auto_update_assets should report failure when the staging copy fails:\n%s", out)
			}

			helper, err := os.ReadFile(autoUpdateDest)
			if err != nil {
				t.Fatalf("installed helper is gone after a failed staging copy: %v", err)
			}
			if string(helper) != workingHelper {
				t.Fatalf("failed staging copy replaced the working helper:\n%s", helper)
			}
			if _, err := os.Stat(servicePath); !os.IsNotExist(err) {
				t.Fatalf("failed staging copy still rewrote the service unit (stat err %v)", err)
			}
			if _, err := os.Stat(timerPath); !os.IsNotExist(err) {
				t.Fatalf("failed staging copy still rewrote the timer unit (stat err %v)", err)
			}
			assertNoAutoUpdateStagingLitter(t, filepath.Dir(autoUpdateDest), filepath.Dir(servicePath))
		})
	}
}

// Regression test for the unit writes in install_auto_update_assets: both units
// were rendered with a bare truncating `cat > "$unit"` whose status was never
// checked, and the function's last statement is safe_systemctl daemon-reload,
// which returns 0 by design even when systemctl fails. A failing write
// therefore truncated a working unit and still reported success. Each unit is
// now rendered beside its destination and committed with a rename, so a failure
// at either step must be reported and must leave the installed unit intact.
func TestInstallAutoUpdateAssetsWritesUnitsAtomically(t *testing.T) {
	workingHelper := "#!/usr/bin/env bash\necho working-helper\n"
	workingUnit := "[Service]\nExecStart=/usr/local/bin/pulse-auto-update.sh\n"

	for _, tc := range []struct {
		name string
		stub string
	}{
		{
			// The render itself fails (a full disk hitting the heredoc).
			name: "unit render fails",
			stub: `cat() { return 1; }`,
		},
		{
			// The render succeeds but the commit does not; the live unit must
			// still hold its previous contents rather than a truncated file.
			name: "unit commit fails",
			stub: `mv() { if [[ "${2:-}" == "` + "SERVICE_PATH_PLACEHOLDER" + `" ]]; then return 1; fi; command mv "$@"; }`,
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			tmpDir := t.TempDir()
			autoUpdateDest, servicePath, timerPath := prepareAutoUpdatePaths(t, tmpDir)
			if err := os.WriteFile(autoUpdateDest, []byte(workingHelper), 0755); err != nil {
				t.Fatalf("write installed helper: %v", err)
			}
			if err := os.WriteFile(servicePath, []byte(workingUnit), 0644); err != nil {
				t.Fatalf("write installed service unit: %v", err)
			}

			stub := strings.ReplaceAll(tc.stub, "SERVICE_PATH_PLACEHOLDER", servicePath)
			script := autoUpdateAssetsHarness(t, tmpDir, autoUpdateDest, servicePath, timerPath, stub)
			out, err := exec.Command("bash", "-c", script).CombinedOutput()
			if err != nil {
				t.Fatalf("bash: %v\n%s", err, out)
			}
			if !strings.Contains(string(out), "FAILED_AS_EXPECTED") {
				t.Fatalf("install_auto_update_assets should report failure when a unit write fails:\n%s", out)
			}

			unit, err := os.ReadFile(servicePath)
			if err != nil {
				t.Fatalf("installed service unit is gone after a failed write: %v", err)
			}
			if string(unit) != workingUnit {
				t.Fatalf("failed unit write clobbered the installed unit:\n%s", unit)
			}
			if _, err := os.Stat(timerPath); !os.IsNotExist(err) {
				t.Fatalf("failed service unit write still installed the timer (stat err %v)", err)
			}
			assertNoAutoUpdateStagingLitter(t, filepath.Dir(autoUpdateDest), filepath.Dir(servicePath))
		})
	}
}

// A successful run must commit through renames and leave nothing staged.
func TestInstallAutoUpdateAssetsLeavesNoStagingFilesOnSuccess(t *testing.T) {
	_, _, autoUpdateDest, servicePath := renderAutoUpdateUnits(t)
	assertNoAutoUpdateStagingLitter(t, filepath.Dir(autoUpdateDest), filepath.Dir(servicePath))
}

// Migration coverage for deployed boxes (the EROFS chicken-and-egg): a box
// installed before the sandbox was widened runs this installer from a
// pulse-update.service whose ReadWritePaths excludes /etc/systemd/system and
// /usr/local/bin, so the run that would install the corrected unit cannot write
// it. install_auto_update_assets must detect the unwritable directory up front
// and hand off to the sandbox escape rather than failing the refresh.
func TestInstallAutoUpdateAssetsMigratesWhenSandboxBlocksUnitDir(t *testing.T) {
	tmpDir := t.TempDir()
	autoUpdateDest, _, _ := prepareAutoUpdatePaths(t, tmpDir)

	// A regular file standing in for the unit directory makes it unwritable in
	// a way that also holds when the test runs as root (unlike chmod 555).
	blocker := filepath.Join(tmpDir, "blocker")
	if err := os.WriteFile(blocker, nil, 0644); err != nil {
		t.Fatalf("write blocker: %v", err)
	}
	servicePath := filepath.Join(blocker, "pulse-update.service")
	timerPath := filepath.Join(blocker, "pulse-update.timer")
	recordPath := filepath.Join(tmpDir, "migrated-dir")

	stub := `migrate_auto_update_assets_outside_sandbox() { printf '%s' "$1" > "` + recordPath + `"; return 0; }`
	script := autoUpdateAssetsHarness(t, tmpDir, autoUpdateDest, servicePath, timerPath, stub)

	out, err := exec.Command("bash", "-c", script).CombinedOutput()
	if err != nil {
		t.Fatalf("bash: %v\n%s", err, out)
	}
	if !strings.Contains(string(out), "UNEXPECTED_SUCCESS") {
		t.Fatalf("a successful sandbox escape must be reported as success:\n%s", out)
	}

	recorded, err := os.ReadFile(recordPath)
	if err != nil {
		t.Fatalf("unwritable unit directory did not trigger the sandbox escape: %v\n%s", err, out)
	}
	if string(recorded) != blocker {
		t.Fatalf("sandbox escape was told %q was blocked, want %q", recorded, blocker)
	}
	assertNoAutoUpdateStagingLitter(t, filepath.Dir(autoUpdateDest))
}

// The sandbox escape itself: systemd-run asks PID 1 to fork the repair, so the
// transient unit runs in the host mount namespace instead of inheriting the
// update unit's ProtectSystem=strict. The installer is copied into the install
// dir first because the calling unit's PrivateTmp=yes hides the helper's /tmp
// copy of it from PID 1, and the escaped run must be handed the same service,
// helper and unit paths or it would repair the defaults instead.
func TestMigrateAutoUpdateAssetsOutsideSandboxReExecsInstallerViaSystemdRun(t *testing.T) {
	tmpDir := t.TempDir()
	installDir := filepath.Join(tmpDir, "install")
	if err := os.MkdirAll(installDir, 0755); err != nil {
		t.Fatalf("mkdir install dir: %v", err)
	}
	argsPath := filepath.Join(tmpDir, "systemd-run-args")

	harness := `#!/usr/bin/env bash
set -uo pipefail
INSTALL_DIR="` + installDir + `"
CONFIG_DIR="` + tmpDir + `/config"
SERVICE_NAME="pulse"
AUTO_UPDATE_DEST="` + tmpDir + `/bin/pulse-auto-update.sh"
UPDATE_SERVICE_PATH="` + tmpDir + `/systemd/pulse-update.service"
UPDATE_TIMER_PATH="` + tmpDir + `/systemd/pulse-update.timer"
print_info() { :; }
print_warn() { echo "WARN: $*"; }
print_success() { echo "MIGRATED"; }
id() { echo 0; }
systemd-run() { printf '%s\n' "$@" > "` + argsPath + `"; return 0; }
` + extractRootInstallShellFunction(t, "migrate_auto_update_assets_outside_sandbox") + `
if migrate_auto_update_assets_outside_sandbox /etc/systemd/system; then
	echo "ESCAPED"
else
	echo "DID_NOT_ESCAPE"
fi
# The escape must refuse to recurse: the escaped run has no sandbox, so if it
# still cannot write, escaping again would loop forever.
rm -f "` + argsPath + `.recursion"
systemd-run() { printf '%s\n' "$@" > "` + argsPath + `.recursion"; return 0; }
if PULSE_AUTO_UPDATE_ASSET_REPAIR=1 migrate_auto_update_assets_outside_sandbox /etc/systemd/system; then
	echo "RECURSED"
fi
[[ -e "` + argsPath + `.recursion" ]] && echo "RECURSION_INVOKED_SYSTEMD_RUN"
exit 0
`
	harnessPath := filepath.Join(tmpDir, "installer-harness.sh")
	if err := os.WriteFile(harnessPath, []byte(harness), 0755); err != nil {
		t.Fatalf("write harness: %v", err)
	}

	out, err := exec.Command("bash", harnessPath).CombinedOutput()
	if err != nil {
		t.Fatalf("bash: %v\n%s", err, out)
	}
	got := string(out)
	if !strings.Contains(got, "ESCAPED") || !strings.Contains(got, "MIGRATED") {
		t.Fatalf("sandbox escape did not report success:\n%s", got)
	}
	for _, unwanted := range []string{"RECURSED", "RECURSION_INVOKED_SYSTEMD_RUN"} {
		if strings.Contains(got, unwanted) {
			t.Fatalf("sandbox escape recursed inside the escaped run (%s):\n%s", unwanted, got)
		}
	}

	argsBytes, err := os.ReadFile(argsPath)
	if err != nil {
		t.Fatalf("sandbox escape never invoked systemd-run: %v\n%s", err, got)
	}
	args := strings.Split(strings.TrimRight(string(argsBytes), "\n"), "\n")
	joined := strings.Join(args, " ")
	for _, want := range []string{
		"--wait",
		"--property=Type=oneshot",
		"--setenv=PULSE_AUTO_UPDATE_ASSET_REPAIR=1",
		"--repair-auto-update-units",
		"--setenv=PULSE_UPDATE_SERVICE_PATH=" + tmpDir + "/systemd/pulse-update.service",
		"--setenv=PULSE_UPDATE_TIMER_PATH=" + tmpDir + "/systemd/pulse-update.timer",
		"--setenv=PULSE_AUTO_UPDATE_DEST=" + tmpDir + "/bin/pulse-auto-update.sh",
		"--setenv=PULSE_INSTALL_DIR=" + installDir,
	} {
		if !strings.Contains(joined, want) {
			t.Fatalf("systemd-run invocation missing %q:\n%s", want, joined)
		}
	}

	// The re-exec target must be the copy inside the install dir: PrivateTmp
	// hides the helper's /tmp copy of the installer from PID 1.
	repairCopy := filepath.Join(installDir, ".pulse-update-asset-repair.sh")
	if !strings.Contains(joined, repairCopy) {
		t.Fatalf("systemd-run was not pointed at the install-dir copy %q:\n%s", repairCopy, joined)
	}
	if _, err := os.Stat(repairCopy); !os.IsNotExist(err) {
		t.Fatalf("sandbox escape left the installer copy behind (stat err %v)", err)
	}
}

// The other half of the migration: install.sh must expose the re-entry point
// the transient unit invokes, and it must rewrite the helper and both units
// without running a full install. This runs the real installer file.
func TestRootInstallScriptRepairAutoUpdateUnitsEntryPoint(t *testing.T) {
	tmpDir := t.TempDir()
	installDir := filepath.Join(tmpDir, "install")
	configDir := filepath.Join(tmpDir, "config")
	autoUpdateDest, servicePath, timerPath := prepareAutoUpdatePaths(t, tmpDir)

	if err := os.MkdirAll(filepath.Join(installDir, "scripts"), 0755); err != nil {
		t.Fatalf("mkdir install scripts dir: %v", err)
	}
	if err := os.MkdirAll(configDir, 0755); err != nil {
		t.Fatalf("mkdir config dir: %v", err)
	}
	if err := os.WriteFile(filepath.Join(installDir, "scripts", "pulse-auto-update.sh"),
		[]byte("#!/usr/bin/env bash\necho repaired-helper\n"), 0755); err != nil {
		t.Fatalf("write release helper: %v", err)
	}
	// This is an asset-rendering/re-entry fixture, not admission to the host
	// service manager. Supply a successful exact unmasked inventory and record
	// the only permitted manager operation. Whole-installer controls exercise
	// failed reads and effective masks using real fixture-root inventories.
	toolsDir := filepath.Join(tmpDir, "tools")
	if err := os.MkdirAll(toolsDir, 0755); err != nil {
		t.Fatal(err)
	}
	callsPath := filepath.Join(tmpDir, "systemctl-calls")
	stub := `#!/usr/bin/env bash
set -eu
printf '%s\n' "$*" >> "` + callsPath + `"
case "$*" in
    'list-unit-files --no-legend --no-pager --full -- pulse-update.timer pulse-update.service')
        printf 'pulse-update.timer disabled enabled\npulse-update.service disabled enabled\n' ;;
    daemon-reload) : ;;
    *) echo "Unexpected fixture manager operation: $*" >&2; exit 97 ;;
esac
`
	if err := os.WriteFile(filepath.Join(toolsDir, "systemctl"), []byte(stub), 0755); err != nil {
		t.Fatal(err)
	}

	installer, err := filepath.Abs(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("resolve install.sh: %v", err)
	}
	cmd := exec.Command("bash", installer, "--repair-auto-update-units")
	cmd.Env = append(os.Environ(),
		"PATH="+toolsDir+string(os.PathListSeparator)+os.Getenv("PATH"),
		"PULSE_SERVICE_NAME=pulse",
		"PULSE_INSTALL_DIR="+installDir,
		"PULSE_CONFIG_DIR="+configDir,
		"PULSE_AUTO_UPDATE_DEST="+autoUpdateDest,
		"PULSE_UPDATE_SERVICE_PATH="+servicePath,
		"PULSE_UPDATE_TIMER_PATH="+timerPath,
		"PULSE_AUTO_UPDATE_ASSET_REPAIR=1",
	)
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("install.sh --repair-auto-update-units failed: %v\n%s", err, out)
	}
	calls, err := os.ReadFile(callsPath)
	if err != nil || string(calls) != "list-unit-files --no-legend --no-pager --full -- pulse-update.timer pulse-update.service\ndaemon-reload\n" {
		t.Fatalf("repair did not inspect both units before its only manager operation: %v\n%s", err, calls)
	}

	helper, err := os.ReadFile(autoUpdateDest)
	if err != nil {
		t.Fatalf("repair did not install the helper: %v\n%s", err, out)
	}
	if !strings.Contains(string(helper), "repaired-helper") {
		t.Fatalf("repair installed the wrong helper:\n%s", helper)
	}
	if info, err := os.Stat(autoUpdateDest); err != nil {
		t.Fatalf("stat repaired helper: %v", err)
	} else if info.Mode().Perm() != 0755 {
		t.Fatalf("repaired helper mode = %v, want 0755 (the update unit's ExecStart)", info.Mode().Perm())
	}

	unit, err := os.ReadFile(servicePath)
	if err != nil {
		t.Fatalf("repair did not write the service unit: %v\n%s", err, out)
	}
	if !strings.Contains(string(unit), "ReadWritePaths=") ||
		!strings.Contains(string(unit), filepath.Dir(servicePath)) ||
		!strings.Contains(string(unit), filepath.Dir(autoUpdateDest)) {
		t.Fatalf("repaired unit did not carry the widened sandbox:\n%s", unit)
	}

	timer, err := os.ReadFile(timerPath)
	if err != nil {
		t.Fatalf("repair did not write the timer unit: %v\n%s", err, out)
	}
	if strings.Count(string(timer), "\nOnCalendar=") != 1 {
		t.Fatalf("repaired timer does not hold exactly one schedule:\n%s", timer)
	}
	assertNoAutoUpdateStagingLitter(t, filepath.Dir(autoUpdateDest), filepath.Dir(servicePath))
}

// Regression tests for #1663: a half-removed installation (binary still at
// /opt/pulse/bin/pulse, but /etc/pulse and the systemd unit deleted by hand)
// re-ran the installer, which took the update path ("Reinstalling version
// ..."). With auto-updates enabled it crashed writing system.json into the
// missing config dir; without them it printed a success completion while
// `systemctl enable/start` had failed with "Unit pulse.service could not be
// found", softened into the unprivileged-container note. The update flows
// must recreate the config dir and the unit, and start_pulse must fail
// loudly when the unit does not exist at all.
func TestRootInstallScriptUpdateFlowsRepairHalfRemovedInstall(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	cmd := exec.CommandContext(ctx, "python3", repoFile("scripts", "tests", "test_server_update_repair.py"), "-v")
	cmd.Env = append(os.Environ(), "PULSE_INSTALLER_UNDER_TEST="+repoFile("install.sh"))
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("half-removed installer update/repair controls: %v\n%s", err, out)
	}
}

func TestRootInstallEnsureSystemdServiceRecreatesMissingUnit(t *testing.T) {
	script := `
set -euo pipefail
SERVICE_NAME="pulse-missing-unit-1663"
print_warn() { echo "WARN: $*"; }
systemctl() { return 0; }
install_systemd_service() { echo "INSTALL_SYSTEMD_SERVICE_CALLED"; }
` + extractRootInstallShellFunction(t, "ensure_systemd_service_installed") + `
ensure_systemd_service_installed
`
	out, err := exec.Command("bash", "-c", script).CombinedOutput()
	if err != nil {
		t.Fatalf("ensure_systemd_service_installed failed: %v\n%s", err, out)
	}
	if !strings.Contains(string(out), "INSTALL_SYSTEMD_SERVICE_CALLED") {
		t.Fatalf("missing unit was not recreated:\n%s", out)
	}
}

func TestRootInstallStartPulseFailsWhenUnitMissing(t *testing.T) {
	stubs := `
set -euo pipefail
PULSE_WAS_ACTIVE="false"
print_info() { echo "INFO: $*"; }
print_warn() { echo "WARN: $*"; }
print_error() { echo "ERROR: $*"; }
print_success() { echo "SUCCESS: $*"; }
safe_systemctl() { return 0; }
timeout() { shift; "$@"; }
sleep() { :; }
journalctl() { return 0; }
ensure_pulse_running_after_update() { return 0; }
` + extractRootInstallShellFunction(t, "wait_for_service_active") + "\n" +
		extractRootInstallShellFunction(t, "start_pulse") + "\n"

	// Unit genuinely absent: no unit file and `systemctl cat` cannot find it.
	// start_pulse must fail with a clear error instead of reporting success
	// behind the unprivileged-container note.
	missing := stubs + `
SERVICE_NAME="pulse-missing-unit-1663"
systemctl() { return 1; }
start_pulse
`
	out, err := exec.Command("bash", "-c", missing).CombinedOutput()
	if err == nil {
		t.Fatalf("start_pulse reported success with no unit installed:\n%s", out)
	}
	if !strings.Contains(string(out), "does not exist") {
		t.Fatalf("start_pulse did not explain the missing unit:\n%s", out)
	}
	if strings.Contains(string(out), "SUCCESS:") {
		t.Fatalf("start_pulse printed success for a missing unit:\n%s", out)
	}

	// Unit resolvable via systemctl cat: the guard must fall through and the
	// normal start path must succeed.
	present := stubs + `
SERVICE_NAME="pulse-missing-unit-1663"
systemctl() { return 0; }
start_pulse
`
	out, err = exec.Command("bash", "-c", present).CombinedOutput()
	if err != nil {
		t.Fatalf("start_pulse failed with a resolvable unit: %v\n%s", err, out)
	}
	if !strings.Contains(string(out), "SUCCESS:") {
		t.Fatalf("start_pulse did not report a successful start:\n%s", out)
	}
}

// Reported on #1663: reinstalling after `rm -rf /etc/pulse` reached
// setup_auto_updates before setup_directories had recreated the config
// directory, so the system.json write failed with "No such file or
// directory" while the run still reported that auto-updates were enabled.
func TestRootInstallScriptAutoUpdateSetupCreatesMissingConfigDir(t *testing.T) {
	parent := t.TempDir()
	configDir := filepath.Join(parent, "pulse")

	script := `
		set -euo pipefail
		print_info() { :; }
		print_warn() { echo "WARN: $*"; }
		print_success() { :; }
		selected_update_channel() { printf 'stable\n'; }
		install_auto_update_assets() { return 0; }
		safe_systemctl() { return 0; }
		chown() { return 0; }
		CONFIG_DIR="$CONFIG_DIR_UNDER_TEST"
		SERVICE_NAME="pulse"
		UPDATE_TIMER_PATH="/tmp/pulse-update.timer"
		ENABLE_AUTO_UPDATES=true
` + extractRootInstallShellFunction(t, "setup_auto_updates") + `
		setup_auto_updates
	`

	cmd := exec.Command("bash", "-c", script)
	cmd.Env = append(os.Environ(), "CONFIG_DIR_UNDER_TEST="+configDir)
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("bash: %v\n%s", err, out)
	}
	if strings.Contains(string(out), "No such file or directory") {
		t.Fatalf("expected config dir to be created before the system.json write, got:\n%s", out)
	}

	systemJSON := filepath.Join(configDir, "system.json")
	contents, readErr := os.ReadFile(systemJSON)
	if readErr != nil {
		t.Fatalf("expected %s to be written, got: %v\n%s", systemJSON, readErr, out)
	}
	if !strings.Contains(string(contents), `"autoUpdateEnabled":true`) {
		t.Fatalf("expected auto-updates to be enabled in system.json, got: %s", contents)
	}
}

// The config-directory guard must run before any state is mutated. An
// earlier version sat after `safe_systemctl enable`, so a failure to create
// the directory left the update timer enabled while the installer reported
// automatic updates as disabled and never wrote system.json.
func TestRootInstallScriptAutoUpdateConfigDirGuardRunsBeforeStateChanges(t *testing.T) {
	body := extractRootInstallShellFunction(t, "setup_auto_updates")

	mkdirIdx := strings.Index(body, `mkdir -p "$config_dir"`)
	if mkdirIdx < 0 {
		t.Fatal("expected setup_auto_updates to create the config directory")
	}
	for _, mutation := range []string{"install_auto_update_assets", "safe_systemctl enable"} {
		idx := strings.Index(body, mutation)
		if idx < 0 {
			t.Fatalf("expected setup_auto_updates to reference %s", mutation)
		}
		if idx < mkdirIdx {
			t.Fatalf("%s runs before the config-directory guard, so a guard failure would leave partial state", mutation)
		}
	}
}

// pct exec runs with PATH=/sbin:/bin:/usr/sbin:/usr/bin. Anything installed to
// /usr/local/bin, which is where BINARY_LINK_PATH and the non-default update
// helper live, is therefore unreachable by bare name and fails with exit 127.
// Every pct exec invocation the installer runs or prints must name a command
// that is either absolute or genuinely resolvable on that PATH.
func TestRootInstallPctExecCommandsResolveOnPctExecPath(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatalf("read root install.sh: %v", err)
	}

	// Commands that really do live on the pct exec PATH.
	resolvable := map[string]bool{
		"bash":      true,
		"sh":        true,
		"rm":        true,
		"hostname":  true,
		"systemctl": true,
		"env":       true,
	}

	pctExec := regexp.MustCompile(`pct exec [^\n]*? -- (.+)`)
	for i, line := range strings.Split(string(content), "\n") {
		match := pctExec.FindStringSubmatch(line)
		if match == nil {
			continue
		}
		fields := strings.Fields(match[1])
		// Skip any leading `env KEY=VALUE` prefix to reach the real command.
		for len(fields) > 1 && (fields[0] == "env" || strings.Contains(fields[0], "=")) {
			fields = fields[1:]
		}
		if len(fields) == 0 {
			continue
		}
		command := strings.Trim(fields[0], `"'`)
		if strings.HasPrefix(command, "$") || strings.HasPrefix(command, "${") {
			// A variable reference is fine as long as it is not wrapped in
			// basename, which is what strips the directory off an absolute path.
			if strings.Contains(match[1], "basename") {
				t.Fatalf("install.sh:%d passes a basename to pct exec, which drops the directory and breaks PATH resolution: %s", i+1, strings.TrimSpace(line))
			}
			continue
		}
		if strings.HasPrefix(command, "/") {
			continue
		}
		if resolvable[command] {
			continue
		}
		t.Fatalf("install.sh:%d runs %q through pct exec by bare name, which is not on PATH=/sbin:/bin:/usr/sbin:/usr/bin: %s", i+1, command, strings.TrimSpace(line))
	}
}

// Failures must propagate even when the cleanup is called from a conditional
// (where Bash disables errexit); a failed key replacement cannot claim success.
func TestRootInstallSensorProxyCleanupRejectsFailedKeyReplacement(t *testing.T) {
	dir := t.TempDir()
	installer, err := filepath.Abs(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatal(err)
	}
	keys := filepath.Join(dir, "authorized_keys")
	original := "ssh-ed25519 TEST keep-admin\nssh-ed25519 OLD # pulse-managed-key\n"
	if err := os.WriteFile(keys, []byte(original), 0600); err != nil {
		t.Fatal(err)
	}
	script := `
source "$INSTALLER_UNDER_TEST"
SENSOR_PROXY_AUTHORIZED_KEYS_PATH="$KEYS_UNDER_TEST"
mv() { return 1; }
if remove_local_sensor_proxy_managed_keys; then
    echo UNEXPECTED_COMPLETION
    exit 1
fi
`
	cmd := exec.Command("bash", "-c", script)
	cmd.Env = append(os.Environ(), "INSTALLER_UNDER_TEST="+installer, "KEYS_UNDER_TEST="+keys)
	out, err := cmd.CombinedOutput()
	if err != nil || strings.Contains(string(out), "Removed legacy") {
		t.Fatalf("failed replacement was accepted: %v\n%s", err, out)
	}
	got, err := os.ReadFile(keys)
	if err != nil || string(got) != original {
		t.Fatalf("key replacement failure changed original: %v", err)
	}
}

func TestRootInstallSensorProxyCleanupCannotHideKeyReadFailure(t *testing.T) {
	for _, pathKind := range []string{"direct", "directory_alias"} {
		t.Run(pathKind, func(t *testing.T) {
			dir := t.TempDir()
			fixtureDir := dir
			if pathKind == "directory_alias" {
				fixtureDir = filepath.Join(t.TempDir(), "fixture alias")
				if err := os.Symlink(dir, fixtureDir); err != nil {
					t.Fatal(err)
				}
			}
			keys := filepath.Join(dir, "authorized_keys")
			const original = "ssh-ed25519 AAAAold # pulse-managed-key\n" + sensorProxyRecoveryKeys
			if err := os.WriteFile(keys, []byte(original), 0600); err != nil {
				t.Fatal(err)
			}
			body := `# The writer resolves its target physically. Match that identity in the
# confinement guard too (including macOS /var -> /private/var temp paths).
# Keep the guard limited to this fixture's owned staging files.
FIXTURE_DIR=$(cd "$FIXTURE_DIR" && pwd -P) || exit 1
source "$1"
SENSOR_PROXY_AUTHORIZED_KEYS_PATH="$FIXTURE_DIR/authorized_keys"
local_sensor_proxy_present() { :; }
stop_pulse_unit_for_removal() { :; }
# Filesystem/service/user removal outside the key fixture is never executed.
rm() {
    case "${!#}" in "$FIXTURE_DIR/authorized_keys.pulse-cleanup."*) command rm "$@";; *) :;; esac
}
grep() { printf 'partial key read\n'; return 2; }
userdel() { echo unexpected-user-removal >>"$FIXTURE_DIR/actions"; }
groupdel() { echo unexpected-group-removal >>"$FIXTURE_DIR/actions"; }
if cleanup_local_sensor_proxy; then exit 0; else exit $?; fi
`
			out, code := runSensorProxySafetyShell(t, repoFile("install.sh"), fixtureDir, "grep", body)
			if code == 0 || strings.Contains(out, "Removed legacy") {
				t.Fatalf("installer cleanup hid failed key read: exit %d: %s", code, out)
			}
			if got, err := os.ReadFile(keys); err != nil || string(got) != original {
				t.Fatalf("installer failure lost recovery keys: %q, %v", got, err)
			}
			if _, err := os.Stat(filepath.Join(dir, "actions")); !os.IsNotExist(err) {
				t.Fatalf("service-user removal followed unconfirmed key cleanup: %v", err)
			}
			if leftovers, err := filepath.Glob(keys + ".pulse-cleanup.*"); err != nil || len(leftovers) != 0 {
				t.Fatalf("failed installer cleanup left staging: %v, %v", leftovers, err)
			}
		})
	}
}

func TestRootInstallResetBoundsEverySystemdObservationAndMutation(t *testing.T) {
	for _, name := range []string{"reset_pulse", "read_pulse_reset_unit_state", "confirm_pulse_reset_unit_stopped"} {
		body := extractRootInstallShellFunction(t, name)
		for _, line := range strings.Split(body, "\n") {
			if strings.Contains(line, "systemctl ") && !strings.Contains(line, "timeout -k 1 5 systemctl ") {
				t.Fatalf("%s has unbounded systemctl call: %s", name, line)
			}
		}
	}
}

// Server update staging lives under the install directory (#2785), which keeps
// an interrupted run's ~1 GB extract across reboots. The sweep removes only
// owner-only staging directories older than any live run, never symlinks,
// files, look-alike names or anything else in the install directory.
func TestRootInstallScriptSweepsOnlyStaleServerStaging(t *testing.T) {
	installDir := filepath.Join(t.TempDir(), "install")
	outside := filepath.Join(t.TempDir(), "outside")
	for _, dir := range []string{
		filepath.Join(installDir, "bin"),
		filepath.Join(installDir, ".pulse-extract-AbC123", "bin"),
		filepath.Join(installDir, ".pulse-v6.6.0-linux-amd64-XyZ789"),
		filepath.Join(installDir, ".pulse-extract-toolong1"),
		outside,
	} {
		if err := os.MkdirAll(dir, 0o700); err != nil {
			t.Fatal(err)
		}
	}
	files := []string{
		filepath.Join(installDir, ".pulse-extract-AbC123", "bin", "pulse"),
		filepath.Join(installDir, ".pulse-v6.6.0-linux-amd64-XyZ789", "pulse-v6.6.0-linux-amd64.tar.gz"),
		filepath.Join(installDir, ".pulse-extract-file01"),
		filepath.Join(installDir, "bin", "pulse"),
		filepath.Join(outside, "keep"),
	}
	for _, file := range files {
		if err := os.WriteFile(file, []byte("fixture"), 0o600); err != nil {
			t.Fatal(err)
		}
	}
	if err := os.Symlink(outside, filepath.Join(installDir, ".pulse-extract-link01")); err != nil {
		t.Fatal(err)
	}

	sweep := func(minutes string) {
		t.Helper()
		script := `
			set -euo pipefail
			print_warn() { echo "$*" >&2; }
			INSTALL_DIR="$1"
			SERVER_STAGING_STALE_MINUTES="$2"
` + extractRootInstallShellFunction(t, "is_server_staging_name") + `
` + extractRootInstallShellFunction(t, "sweep_stale_server_staging") + `
			sweep_stale_server_staging
		`
		if out, err := exec.Command("bash", "-c", script, "bash", installDir, minutes).CombinedOutput(); err != nil {
			t.Fatalf("sweep: %v\n%s", err, out)
		}
	}

	// A live run's staging is fresh: the production age keeps it.
	sweep("360")
	for _, file := range files {
		if _, err := os.Stat(file); err != nil {
			t.Fatalf("fresh staging or unrelated file removed: %s", file)
		}
	}

	time.Sleep(1100 * time.Millisecond)
	sweep("0")
	for _, removed := range []string{".pulse-extract-AbC123", ".pulse-v6.6.0-linux-amd64-XyZ789"} {
		if _, err := os.Lstat(filepath.Join(installDir, removed)); !os.IsNotExist(err) {
			t.Fatalf("stale staging %s was not removed (err=%v)", removed, err)
		}
	}
	for _, kept := range []string{".pulse-extract-toolong1", ".pulse-extract-file01", ".pulse-extract-link01", "bin/pulse"} {
		if _, err := os.Lstat(filepath.Join(installDir, kept)); err != nil {
			t.Fatalf("sweep removed non-staging entry %s: %v", kept, err)
		}
	}
	if _, err := os.Stat(filepath.Join(outside, "keep")); err != nil {
		t.Fatalf("sweep followed a staging-named symlink: %v", err)
	}
}

// The recursive pulse ownership pass must not hand root-consumed update
// staging (verified archive, extracted payload) to the service account, and
// every recursive install-dir chown goes through that one helper.
func TestRootInstallScriptPulseOwnershipExcludesServerStaging(t *testing.T) {
	content, err := os.ReadFile(filepath.Join("..", "..", "install.sh"))
	if err != nil {
		t.Fatal(err)
	}
	if regexp.MustCompile(`chown -R [^\n]*"\$INSTALL_DIR"`).Match(content) {
		t.Fatal(`install.sh must use chown_install_dir_for_pulse, not chown -R on "$INSTALL_DIR"`)
	}

	installDir := filepath.Join(t.TempDir(), "install")
	for _, dir := range []string{"bin", "scripts", ".pulse-extract-AbC123", ".pulse-v6.6.0-linux-amd64-XyZ789", ".hidden"} {
		if err := os.MkdirAll(filepath.Join(installDir, dir), 0o700); err != nil {
			t.Fatal(err)
		}
	}
	if err := os.WriteFile(filepath.Join(installDir, "VERSION"), []byte("v6.6.0\n"), 0o600); err != nil {
		t.Fatal(err)
	}
	calls := filepath.Join(t.TempDir(), "calls")
	script := `
		set -euo pipefail
		INSTALL_DIR="$1"
		CALLS="$2"
		chown() { printf '%s\n' "$*" >> "$CALLS"; }
` + extractRootInstallShellFunction(t, "is_server_staging_name") + `
` + extractRootInstallShellFunction(t, "chown_install_dir_for_pulse") + `
		chown_install_dir_for_pulse
	`
	if out, err := exec.Command("bash", "-c", script, "bash", installDir, calls).CombinedOutput(); err != nil {
		t.Fatalf("chown_install_dir_for_pulse: %v\n%s", err, out)
	}
	recorded, err := os.ReadFile(calls)
	if err != nil {
		t.Fatal(err)
	}
	got := strings.Split(strings.TrimSpace(string(recorded)), "\n")
	want := []string{
		"pulse:pulse " + installDir,
		"-R pulse:pulse " + filepath.Join(installDir, "VERSION"),
		"-R pulse:pulse " + filepath.Join(installDir, "bin"),
		"-R pulse:pulse " + filepath.Join(installDir, "scripts"),
		"-R pulse:pulse " + filepath.Join(installDir, ".hidden"),
	}
	sort.Strings(got)
	sort.Strings(want)
	if strings.Join(got, "\n") != strings.Join(want, "\n") {
		t.Fatalf("ownership calls:\n%s\nwant:\n%s", strings.Join(got, "\n"), strings.Join(want, "\n"))
	}
}

func TestRootInstallScriptConfigBackupZeroRetention(t *testing.T) {
	configDir := filepath.Join(t.TempDir(), "config")
	if err := os.Mkdir(configDir, 0700); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(configDir, "settings.json"), []byte("retained source\n"), 0600); err != nil {
		t.Fatal(err)
	}
	script := `set -euo pipefail
print_info() { :; }; print_warn() { :; }; print_error() { :; }
ensure_config_backup_headroom() { :; }
CONFIG_BACKUP_KEEP_COUNT=0
` + extractRootInstallShellFunction(t, "config_backup_receipt_matches") + "\n" +
		extractRootInstallShellFunction(t, "prune_config_backups") + "\n" +
		extractRootInstallShellFunction(t, "backup_existing") + `
backup_existing
test -f "$CONFIG_DIR/settings.json"
if compgen -G "$CONFIG_DIR.backup.*" >/dev/null; then
    echo "zero retention kept a completed snapshot" >&2
    exit 1
fi
`
	cmd := exec.Command("bash", "-c", script)
	cmd.Env = append(os.Environ(), "CONFIG_DIR="+configDir)
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("zero snapshot retention: %v\n%s", err, out)
	}
}

func TestRootInstallScriptConfigBackupPublicationPreservesPriorSnapshots(t *testing.T) {
	// A cumulative suite budget is not an individual backup-control deadline.
	// Keep all discovered controls serial and retain the existing per-control bound.
	runPythonFixtureControls(t, repoFile("scripts", "tests", "test_server_config_backups.py"), "ConfigBackupPublication", []string{
		"test_two_successful_same_second_copies_are_separate_complete_snapshots",
		"test_failed_partial_copy_does_not_mutate_or_delete_a_previous_snapshot",
		"test_copy_and_published_snapshot_remain_private_with_broad_source_permissions",
		"test_snapshot_privacy_failure_preserves_prior_snapshots_without_rotation",
		"test_allocation_failure_stops_before_copy_or_rotation",
		"test_headroom_failure_creates_no_stage_and_preserves_prior_snapshot",
		"test_failed_publication_does_not_rotate_or_publish_a_snapshot",
		"test_exit_zero_skipped_publication_preserves_file_directory_and_link",
		"test_current_snapshot_survives_rotation_even_after_clock_moves_backwards",
		"test_rotation_uses_the_literal_config_namespace",
		"test_rotation_keeps_embedded_newlines_inside_each_path",
		"test_snapshot_namespace_preserves_trailing_newlines_in_name_and_parent",
		"test_relative_option_shaped_name_and_trailing_slashes_keep_their_namespace",
		"test_path_resolution_failure_stops_before_copy_or_rotation",
		"test_rotation_skips_files_links_and_an_empty_inventory",
		"test_incomplete_copy_is_not_another_invocations_rotation_candidate",
		"test_discard_removes_only_this_invocations_new_snapshot",
		"test_missing_config_clears_stale_ownership_without_deleting_prior_snapshot",
		"test_read_only_config_parent_uses_the_existing_hardened_unit_fallback",
		"test_unmarked_prefix_directories_and_legacy_copies_are_never_deleted",
		"test_only_exact_bounded_regular_ownership_receipts_admit_rotation",
		"test_receipt_is_private_and_does_not_change_the_copied_configuration",
		"test_receipt_preparation_failure_stops_without_publication_or_rotation",
		"test_receipt_publication_failures_retain_the_copy_without_rotation",
		"test_repeated_real_publications_rotate_receipts_and_preserve_legacy_copies",
		"test_replaced_snapshot_cannot_be_deleted_by_rotation_or_discard",
		"test_every_update_entry_stops_on_failed_backup_even_in_an_or_list",
	}, 30*time.Second, "PULSE_INSTALLER_UNDER_TEST="+repoFile("install.sh"))
}

// The real directory admission and all five main entry paths run only against
// private text files and inert chown/lifecycle doubles. In particular, an OR-list
// must not turn an incomplete permission repair into successful installation.
func TestRootInstallDirectorySetupStopsOnFailures(t *testing.T) {
	runPythonFixtureControls(t, repoFile("scripts", "tests", "test_server_directory_setup.py"), "DirectorySetup", []string{
		"test_every_permission_and_directory_failure_stops_in_conditional_call",
		"test_new_environment_write_failure_stops_before_ownership_or_completion",
		"test_success_preserves_existing_bytes_and_tightens_only_required_modes",
		"test_fresh_environment_has_private_mode_and_explicit_mock_default",
		"test_all_five_entry_paths_stop_on_directory_failure_even_in_or_list",
	}, 30*time.Second, "PULSE_INSTALLER_UNDER_TEST="+repoFile("install.sh"))
}

// Run the real startup/recovery producer through each main consumer without
// touching a host service, binary or saved configuration. Conditional callers
// must not mask failure or issue completion/marker writes over unknown liveness.
func TestRootInstallStartupRequiresActiveReadback(t *testing.T) {
	runPythonFixtureControls(t, repoFile("scripts", "tests", "test_server_start_admission.py"), "ServerStartAdmission", []string{
		"test_unconfirmed_start_fails_for_fresh_and_prior_active_installations",
		"test_confirmed_active_and_delayed_start_consume_liveness_flag",
		"test_retry_is_bounded_and_only_restores_prior_active_service",
		"test_missing_unit_stops_before_enable_or_start",
		"test_all_five_main_paths_stop_before_marker_or_completion_in_or_list",
		"test_all_five_main_paths_complete_only_after_active_readback",
	}, 30*time.Second, "PULSE_INSTALLER_UNDER_TEST="+repoFile("install.sh"))
}

// Run the emitted helper and real SSH verifier. Curl exits are modelled, so
// these controls prove deadline arguments and safe failure, not a live outage.
func TestRootManualUpdateDownloadDeadlines(t *testing.T) {
	sourcePath := os.Getenv("PULSE_INSTALLER_UNDER_TEST")
	if sourcePath == "" {
		sourcePath = filepath.Join("..", "..", "install.sh")
	}
	source, err := filepath.Abs(sourcePath)
	if err != nil {
		t.Fatal(err)
	}
	assets := t.TempDir()
	key := filepath.Join(assets, "key")
	installer := filepath.Join(assets, "install.sh")
	if err := os.WriteFile(installer, []byte("#!/bin/bash\nprintf '%s\\n' EXECUTED \"$@\" > \"$FIXTURE/executed\"\n"), 0600); err != nil {
		t.Fatal(err)
	}
	for _, args := range [][]string{
		{"-q", "-t", "ed25519", "-N", "", "-f", key},
		{"-q", "-Y", "sign", "-f", key, "-n", "pulse-install", installer},
	} {
		if out, err := exec.Command("ssh-keygen", args...).CombinedOutput(); err != nil {
			t.Fatalf("fixture signature: %v\n%s", err, out)
		}
	}
	publicKey, err := os.ReadFile(key + ".pub")
	if err != nil {
		t.Fatal(err)
	}
	for _, fault := range []string{"success", "installer_timeout", "signature_timeout", "bad_signature", "first_allocation", "second_allocation"} {
		t.Run(fault, func(t *testing.T) {
			dir := t.TempDir()
			for _, leaf := range []string{"config", "temporary", "tools"} {
				if err := os.Mkdir(filepath.Join(dir, leaf), 0700); err != nil {
					t.Fatal(err)
				}
			}
			config := filepath.Join(dir, "config", "system.json")
			const configBytes = `{"updateChannel":"stable","preserve":"fixture"}`
			if err := os.WriteFile(config, []byte(configBytes), 0600); err != nil {
				t.Fatal(err)
			}
			helper := filepath.Join(dir, "update")
			env := append(os.Environ(), "FIXTURE="+dir, "FIXTURE_ASSETS="+assets, "FAULT="+fault,
				"INSTALLER_SOURCE="+source, "FIXTURE_PUBLIC_KEY="+strings.TrimSpace(string(publicKey)),
				"PULSE_INSTALL_DIR="+dir, "PULSE_CONFIG_DIR="+filepath.Join(dir, "config"),
				"PULSE_UPDATE_HELPER_PATH="+helper, "PULSE_PROFILE_PATH="+filepath.Join(dir, "profile"),
				"PULSE_BASHRC_PATH="+filepath.Join(dir, "bashrc"))
			generate := exec.Command("bash", "-c", `source "$INSTALLER_SOURCE"
PINNED_RELEASE_SSH_PUBLIC_KEY="$FIXTURE_PUBLIC_KEY"
setup_update_command`)
			generate.Env = env
			if out, err := generate.CombinedOutput(); err != nil {
				t.Fatalf("generate helper: %v\n%s", err, out)
			}
			tools := map[string]string{
				"mktemp": `#!/bin/bash
set -eu
count=0
[[ ! -f "$FIXTURE/allocations" ]] || read -r count < "$FIXTURE/allocations"
count=$((count + 1))
printf '%s\n' "$count" > "$FIXTURE/allocations"
[[ "$FAULT:$count" != first_allocation:1 && "$FAULT:$count" != second_allocation:2 ]] || exit 1
exec /usr/bin/mktemp "$FIXTURE/temporary/input.XXXXXX"
`,
				"curl": `#!/bin/bash
set -eu
[[ "$1" == -q ]] || { echo 'ambient curl configuration is not disabled' >&2; exit 99; }
url=""; dest=""; connect=""; maximum=""; protocols=""; redirects=""
while [[ $# -gt 0 ]]; do
    case "$1" in
        --connect-timeout) connect="$2"; shift 2 ;;
        --max-time) maximum="$2"; shift 2 ;;
        --proto) protocols="$2"; shift 2 ;;
        --proto-redir) redirects="$2"; shift 2 ;;
        -o) dest="$2"; shift 2 ;;
        -q|-fsSL) shift ;;
        https://github.com/rcourtman/Pulse/releases/latest/download/install.sh*) url="$1"; shift ;;
        *) exit 98 ;;
    esac
done
printf '%s %s %s\n' "$url" "$connect" "$maximum" >> "$FIXTURE/transport"
[[ "$connect" == 10 && "$maximum" == 60 ]] || { echo 'missing bootstrap download deadline' >&2; exit 99; }
[[ "$protocols" == =https && "$redirects" == =https ]] || { echo 'missing bootstrap HTTPS restriction' >&2; exit 99; }
case "$url" in
    *.sshsig) phase=signature; source="$FIXTURE_ASSETS/install.sh.sig" ;;
    */install.sh) phase=installer; source="$FIXTURE_ASSETS/install.sh" ;;
    *) exit 97 ;;
esac
# Retain partial writes too: neither failure may execute the installer or
# leave a partial bootstrap input behind.
if [[ "$FAULT" == "${phase}_timeout" ]]; then
    printf partial > "$dest"
    exit 28
fi
if [[ "$phase:$FAULT" == signature:bad_signature ]]; then
    printf invalid > "$dest"
else
    cp "$source" "$dest"
fi
`,
			}
			for name, body := range tools {
				if err := os.WriteFile(filepath.Join(dir, "tools", name), []byte(body), 0700); err != nil {
					t.Fatal(err)
				}
			}
			ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
			defer cancel()
			cmd := exec.CommandContext(ctx, "bash", helper)
			cmd.Env = append(env, "PATH="+filepath.Join(dir, "tools")+string(os.PathListSeparator)+os.Getenv("PATH"))
			out, runErr := cmd.CombinedOutput()
			calls, callErr := os.ReadFile(filepath.Join(dir, "executed"))
			if fault == "success" {
				if runErr != nil || callErr != nil || string(calls) != "EXECUTED\n--stable\n" || !strings.Contains(string(out), "Update complete!") {
					t.Fatalf("valid signed installer: %v, %v, %q\n%s", runErr, callErr, calls, out)
				}
			} else {
				if runErr == nil || !os.IsNotExist(callErr) || strings.Contains(string(out), "Update complete!") {
					t.Fatalf("failed bootstrap reported/executed success: %v, %v\n%s", runErr, callErr, out)
				}
				if strings.Contains(fault, "timeout") {
					want := "Failed to download Pulse installer"
					if fault == "signature_timeout" {
						want += " signature"
					}
					if !strings.Contains(string(out), want) {
						t.Fatalf("missing download failure diagnostic: %s", out)
					}
				}
			}
			if ctx.Err() != nil {
				t.Fatalf("helper exceeded fixture bound: %v\n%s", ctx.Err(), out)
			}
			entries, err := os.ReadDir(filepath.Join(dir, "temporary"))
			if err != nil || len(entries) != 0 {
				t.Errorf("bootstrap left temporary inputs: %v, %v", entries, err)
			}
			got, err := os.ReadFile(config)
			if err != nil || string(got) != configBytes {
				t.Fatalf("bootstrap changed saved configuration: %v, %q", err, got)
			}
			transport, err := os.ReadFile(filepath.Join(dir, "transport"))
			wantRequests := 2
			if fault == "installer_timeout" {
				wantRequests = 1
			} else if strings.Contains(fault, "allocation") {
				wantRequests = 0
			}
			if err != nil && !os.IsNotExist(err) {
				t.Fatal(err)
			}
			if got := strings.Count(string(transport), "\n"); got != wantRequests {
				t.Errorf("bootstrap requests = %d, want %d (no retries): %q", got, wantRequests, transport)
			}
		})
	}
}

// Exercise the emitted helper with real curl, TLS and SSH signatures. The
// adapter changes only the two fixed GitHub URLs and the fixture CA, retaining
// production options. No service, external host or installed binary is used.
func TestRootManualUpdateHTTPSOnly(t *testing.T) {
	curl, err := exec.LookPath("curl")
	if err != nil {
		t.Fatal("curl is required for the installer transport controls: ", err)
	}
	assets := t.TempDir()
	key := filepath.Join(assets, "key")
	installer := filepath.Join(assets, "installer")
	const payload = "#!/bin/bash\nprintf '%s\\n' \"$@\" > \"$PUBLICATION_ROOT/executed\"\n"
	if err := os.WriteFile(installer, []byte(payload), 0o600); err != nil {
		t.Fatal(err)
	}
	for _, args := range [][]string{
		{"-q", "-t", "ed25519", "-N", "", "-f", key},
		{"-q", "-Y", "sign", "-f", key, "-n", "pulse-install", installer},
	} {
		if out, err := exec.Command("ssh-keygen", args...).CombinedOutput(); err != nil {
			t.Fatalf("fixture signing: %v\n%s", err, out)
		}
	}
	publicKey, err := os.ReadFile(key + ".pub")
	if err != nil {
		t.Fatal(err)
	}
	signature, err := os.ReadFile(installer + ".sig")
	if err != nil {
		t.Fatal(err)
	}
	for _, fault := range []string{"https", "https_redirect", "http_initial", "http_installer_redirect", "http_signature_redirect"} {
		t.Run(fault, func(t *testing.T) {
			dir, helper, env := manualUpdatePublicationFixture(t)
			var plainRequests, tlsRequests atomic.Int32
			serve := func(w http.ResponseWriter, r *http.Request) {
				if strings.HasSuffix(r.URL.Path, "signature") {
					_, _ = w.Write(signature)
				} else {
					_, _ = io.WriteString(w, payload)
				}
			}
			plain := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				plainRequests.Add(1)
				serve(w, r)
			}))
			defer plain.Close()
			tls := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				tlsRequests.Add(1)
				if (fault == "http_installer_redirect" && r.URL.Path == "/installer") ||
					(fault == "http_signature_redirect" && r.URL.Path == "/signature") {
					http.Redirect(w, r, plain.URL+r.URL.Path, http.StatusFound)
				} else if fault == "https_redirect" && !strings.HasPrefix(r.URL.Path, "/cdn/") {
					http.Redirect(w, r, "/cdn"+r.URL.Path, http.StatusFound)
				} else {
					serve(w, r)
				}
			}))
			defer tls.Close()
			ca := filepath.Join(dir, "fixture-ca.pem")
			if err := os.WriteFile(ca, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: tls.Certificate().Raw}), 0o600); err != nil {
				t.Fatal(err)
			}
			tools := filepath.Join(dir, "tools")
			if err := os.Mkdir(tools, 0o700); err != nil {
				t.Fatal(err)
			}
			adapter := `#!/bin/bash
set -euo pipefail
[[ "$1" == -q ]] || exit 97
shift
args=()
for arg in "$@"; do
    case "$arg" in
        https://github.com/rcourtman/Pulse/releases/latest/download/install.sh) arg="$FIXTURE_ORIGIN/installer" ;;
        https://github.com/rcourtman/Pulse/releases/latest/download/install.sh.sshsig) arg="$FIXTURE_ORIGIN/signature" ;;
    esac
    args+=("$arg")
done
exec "$REAL_CURL" -q --cacert "$FIXTURE_CA" "${args[@]}"
`
			if err := os.WriteFile(filepath.Join(tools, "curl"), []byte(adapter), 0o700); err != nil {
				t.Fatal(err)
			}
			// Keep every allocated bootstrap input private to this fixture and
			// observable after success/refusal, including the signer's scratch.
			if err := os.WriteFile(filepath.Join(tools, "mktemp"), []byte("#!/bin/bash\nexec /usr/bin/mktemp \"$PUBLICATION_ROOT/input.XXXXXX\"\n"), 0o700); err != nil {
				t.Fatal(err)
			}
			origin := tls.URL
			if fault == "http_initial" {
				origin = plain.URL
			}
			env = append(env, "FIXTURE_PUBLIC_KEY="+strings.TrimSpace(string(publicKey)),
				"FIXTURE_ORIGIN="+origin, "FIXTURE_CA="+ca, "REAL_CURL="+curl,
				"PATH="+tools+string(os.PathListSeparator)+os.Getenv("PATH"),
				"NO_PROXY=*", "no_proxy=*")
			runManualUpdatePublication(t, env, `source "$PUBLICATION_SOURCE"
PINNED_RELEASE_SSH_PUBLIC_KEY="$FIXTURE_PUBLIC_KEY"
setup_update_command`)
			ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
			defer cancel()
			cmd := exec.CommandContext(ctx, "bash", helper, "--version", "v0.0.1")
			cmd.Env = env
			out, runErr := cmd.CombinedOutput()
			if ctx.Err() != nil {
				t.Fatalf("fixture exceeded deadline: %v\n%s", ctx.Err(), out)
			}
			executed, executedErr := os.ReadFile(filepath.Join(dir, "executed"))
			if strings.HasPrefix(fault, "https") {
				wantTLS := int32(2)
				if fault == "https_redirect" {
					wantTLS = 4
				}
				if runErr != nil || executedErr != nil || string(executed) != "--version\nv0.0.1\n" || tlsRequests.Load() != wantTLS {
					t.Errorf("authenticated HTTPS did not execute the signed installer once: %v, %v, %q, TLS=%d\n%s", runErr, executedErr, executed, tlsRequests.Load(), out)
				}
			} else {
				if runErr == nil || !os.IsNotExist(executedErr) || strings.Contains(string(out), "Update complete!") {
					t.Errorf("plaintext transport reached installer/success: %v, %v\n%s", runErr, executedErr, out)
				}
				if !strings.Contains(string(out), "Failed to download Pulse installer") {
					t.Errorf("missing bootstrap refusal diagnostic: %s", out)
				}
			}
			if plainRequests.Load() != 0 {
				t.Errorf("sent %d unauthenticated HTTP requests", plainRequests.Load())
			}
			if litter, err := filepath.Glob(filepath.Join(dir, "input.*")); err != nil || len(litter) != 0 {
				t.Errorf("bootstrap inputs survived: %v, %v", litter, err)
			}
		})
	}
}

// Only setup_update_command runs: no downloads, system services or installed
// application are involved. The override admits an exact parent-source control.
func manualUpdatePublicationFixture(t *testing.T) (string, string, []string) {
	t.Helper()
	source := os.Getenv("PULSE_INSTALLER_UNDER_TEST")
	if source == "" {
		source = repoFile("install.sh")
	}
	source, err := filepath.Abs(source)
	if err != nil {
		t.Fatal(err)
	}
	dir := t.TempDir()
	helper := filepath.Join(dir, "update with spaces")
	env := append(os.Environ(), "PUBLICATION_SOURCE="+source, "PUBLICATION_ROOT="+dir,
		"PULSE_INSTALL_DIR="+filepath.Join(dir, "install"), "PULSE_CONFIG_DIR="+filepath.Join(dir, "config"),
		"PULSE_UPDATE_HELPER_PATH="+helper, "PULSE_PROFILE_PATH="+filepath.Join(dir, "profile"),
		"PULSE_BASHRC_PATH="+filepath.Join(dir, "bashrc"))
	return dir, helper, env
}

func runManualUpdatePublication(t *testing.T, env []string, script string) []byte {
	t.Helper()
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	cmd := exec.CommandContext(ctx, "bash", "-c", script)
	cmd.Env = env
	out, err := cmd.CombinedOutput()
	if err != nil || ctx.Err() != nil {
		t.Fatalf("helper publication: %v (deadline %v)\n%s", err, ctx.Err(), out)
	}
	return out
}

func assertManualUpdateStageRemoved(t *testing.T, helper string) {
	t.Helper()
	staged, err := filepath.Glob(helper + ".staged.*")
	if err != nil || len(staged) != 0 {
		t.Fatalf("publication left staging files: %v, %v", staged, err)
	}
}

func assertPublishedManualUpdateHelper(t *testing.T, helper string) {
	t.Helper()
	info, err := os.Stat(helper)
	if err != nil {
		t.Fatal(err)
	}
	if !info.Mode().IsRegular() || info.Mode().Perm() != 0o755 {
		t.Fatalf("helper mode = %v, want regular 0755", info.Mode())
	}
	body, err := os.ReadFile(helper)
	if err != nil || !strings.Contains(string(body), "# Pulse update command\n") {
		t.Fatalf("missing complete generated helper: %v\n%s", err, body)
	}
	if out, err := exec.Command("bash", "-n", helper).CombinedOutput(); err != nil {
		t.Fatalf("published helper syntax: %v\n%s", err, out)
	}
	assertManualUpdateStageRemoved(t, helper)
}

func TestManualUpdateHelperPublication(t *testing.T) {
	t.Run("fresh_under_private_umask", func(t *testing.T) {
		_, helper, env := manualUpdatePublicationFixture(t)
		runManualUpdatePublication(t, env, `set -euo pipefail
source "$PUBLICATION_SOURCE"
umask 077
setup_update_command`)
		assertPublishedManualUpdateHelper(t, helper)
	})

	t.Run("running_reader_keeps_old_inode", func(t *testing.T) {
		dir, helper, env := manualUpdatePublicationFixture(t)
		renderer := filepath.Join(dir, "refresh.sh")
		if err := os.WriteFile(renderer, []byte(`set -euo pipefail
source "$PUBLICATION_SOURCE"
setup_update_command
printf 'renderer completed\n'
`), 0o600); err != nil {
			t.Fatal(err)
		}
		// A child installer replaces the script while its parent Bash still has
		// unread commands. Padding exceeds Bash's read-ahead buffer, independent
		// of the emitted helper's size or the interpreter's current line offset.
		old := "#!/bin/bash\nset -euo pipefail\n" +
			"trap 'printf old-cleanup > \"$PUBLICATION_ROOT/cleanup\"' EXIT\n" +
			"bash \"$PUBLICATION_ROOT/refresh.sh\"\n" +
			strings.Repeat("# unread original helper bytes\n", 4096) +
			"printf 'old reader completed\\n'\n"
		if err := os.WriteFile(helper, []byte(old), 0o755); err != nil {
			t.Fatal(err)
		}
		reader, err := os.Open(helper)
		if err != nil {
			t.Fatal(err)
		}
		defer reader.Close()
		before, err := reader.Stat()
		if err != nil {
			t.Fatal(err)
		}
		out := runManualUpdatePublication(t, env, `exec bash "$PULSE_UPDATE_HELPER_PATH"`)
		if !strings.Contains(string(out), "renderer completed\n") || !strings.Contains(string(out), "old reader completed\n") {
			t.Errorf("replacement interrupted the running helper:\n%s", out)
		}
		if cleanup, err := os.ReadFile(filepath.Join(dir, "cleanup")); err != nil || string(cleanup) != "old-cleanup" {
			t.Errorf("original helper cleanup did not complete: %v, %q", err, cleanup)
		}
		after, err := os.Stat(helper)
		if err != nil {
			t.Fatal(err)
		}
		if os.SameFile(before, after) {
			t.Error("publication rewrote the running helper's inode")
		}
		retained, err := io.ReadAll(reader)
		if err != nil || string(retained) != old {
			t.Errorf("publication changed the old reader's bytes: %v", err)
		}
		assertPublishedManualUpdateHelper(t, helper)
	})

	for _, fault := range []string{"allocate", "write", "mode", "syntax", "rename", "directory"} {
		t.Run("preserve_on_"+fault+"_failure", func(t *testing.T) {
			dir, helper, env := manualUpdatePublicationFixture(t)
			const original = "#!/bin/bash\nprintf 'original helper\\n'\n"
			target := helper
			if fault == "directory" {
				if err := os.Mkdir(helper, 0o755); err != nil {
					t.Fatal(err)
				}
				target = filepath.Join(helper, "unrelated")
			}
			if err := os.WriteFile(target, []byte(original), 0o751); err != nil {
				t.Fatal(err)
			}
			before, err := os.Stat(target)
			if err != nil {
				t.Fatal(err)
			}
			faults := map[string]string{
				"allocate": "mktemp() { return 1; }",
				"write":    "cat() { printf partial; return 1; }",
				"mode":     "chmod() { return 1; }",
				"syntax":   "bash() { return 1; }",
				"rename":   "mv() { return 1; }",
			}
			out := runManualUpdatePublication(t, env, `set -euo pipefail
source "$PUBLICATION_SOURCE"
`+faults[fault]+`
setup_update_command
printf 'installer continued\n'`)
			if !strings.Contains(string(out), "[WARN]") || !strings.Contains(string(out), "installer continued\n") {
				t.Errorf("optional failure did not warn and continue:\n%s", out)
			}
			body, err := os.ReadFile(target)
			if err != nil || string(body) != original {
				t.Errorf("failed publication damaged the previous helper: %v, %q", err, body)
			}
			after, err := os.Stat(target)
			if err != nil || !os.SameFile(before, after) || after.Mode() != before.Mode() {
				t.Errorf("failed publication changed original identity or mode: %v", err)
			}
			if fault == "directory" {
				children, err := os.ReadDir(helper)
				if err != nil || len(children) != 1 || children[0].Name() != "unrelated" {
					t.Errorf("publication wrote into a directory destination: %v, %v", children, err)
				}
			}
			assertManualUpdateStageRemoved(t, helper)
			for _, name := range []string{"profile", "bashrc"} {
				if _, err := os.Stat(filepath.Join(dir, name)); !os.IsNotExist(err) {
					t.Errorf("failed publication continued with PATH mutation: %s, %v", name, err)
				}
			}
		})
	}
}