//go:build !windows

package deploy

import (
	"os/exec"
	"testing"
)

// TestWorkerProcessRunningNotFound checks the negative path (process absent).
// On any CI/local box the test binary name is extremely unlikely to exist as a
// running process by that exact name, so pgrep should return non-zero.
func TestWorkerProcessRunningNotFound(t *testing.T) {
	// Use a name that will never be a running process.
	if workerProcessRunning("__aetherforge_nonexistent_9z__") {
		t.Error("should return false for a process that does not exist")
	}
}

// TestWorkerProcessRunningCurrent verifies detection works when a process IS
// running. We use "go" (the go toolchain itself) since it's always present
// during a test run inside go test.
func TestWorkerProcessRunningCurrent(t *testing.T) {
	// pgrep -x matches exact process names; "go" may or may not match
	// depending on how go test spawns subprocesses. Use the test binary itself.
	name := "go"
	out, err := exec.Command("pgrep", "-x", name).Output()
	if err != nil || len(out) == 0 {
		t.Skipf("pgrep -x go found nothing; skip live-process check (pgrep not in PATH?)")
	}
	// If pgrep found it, our function must also find it.
	if !workerProcessRunning(name) {
		t.Errorf("workerProcessRunning(%q) should be true when pgrep finds it", name)
	}
}

// TestWorkerProcessRunningStripsExeSuffix ensures that Windows-style ".exe"
// suffixes in process names are stripped before the pgrep call so the same
// config name can be used on both platforms.
func TestWorkerProcessRunningStripsExeSuffix(t *testing.T) {
	// Process "crypto-miner-agent.exe" → pgrep -x "crypto-miner-agent"
	// We can't guarantee it's running, but we can confirm it doesn't panic
	// and returns a bool.
	result := workerProcessRunning("__nonexistent__.exe")
	if result {
		t.Error("should not find __nonexistent__.exe as a running process")
	}
}
