package detect

import (
	"os"
	"path/filepath"
	"testing"
)

func TestInContainerEnvVar(t *testing.T) {
	getenv := func(k string) string {
		if k == "container" {
			return "podman"
		}
		return ""
	}
	if !InContainer(getenv) {
		t.Error("InContainer = false, want true with container env var set")
	}
}

func TestExecutable(t *testing.T) {
	dir := t.TempDir()
	execFile := filepath.Join(dir, "tool")
	if err := os.WriteFile(execFile, []byte("#!/bin/sh\n"), 0o755); err != nil {
		t.Fatal(err)
	}
	plainFile := filepath.Join(dir, "data")
	if err := os.WriteFile(plainFile, []byte("x"), 0o644); err != nil {
		t.Fatal(err)
	}

	cases := []struct {
		path string
		want bool
	}{
		{execFile, true},
		{plainFile, false},
		{dir, false}, // directories are not executables
		{filepath.Join(dir, "missing"), false},
	}
	for _, tc := range cases {
		if got := Executable(tc.path); got != tc.want {
			t.Errorf("Executable(%q) = %v, want %v", tc.path, got, tc.want)
		}
	}
}

func TestAnyExecutable(t *testing.T) {
	dir := t.TempDir()
	execFile := filepath.Join(dir, "tool")
	if err := os.WriteFile(execFile, []byte("#!/bin/sh\n"), 0o755); err != nil {
		t.Fatal(err)
	}
	missing := filepath.Join(dir, "missing")

	if !AnyExecutable([]string{missing, execFile}) {
		t.Error("AnyExecutable = false, want true when one path is executable")
	}
	if AnyExecutable([]string{missing}) {
		t.Error("AnyExecutable = true, want false when none exist")
	}
}
