package cli

import (
	"strings"
	"testing"
)

func TestBuildVMExecScriptWithGuestPath(t *testing.T) {
	got := buildVMExecScript("/root/repo", []string{"make", "test"})
	want := "cd '/root/repo' && if command -v mise >/dev/null 2>&1; then mise exec -- 'make' 'test'; else 'make' 'test'; fi"
	if got != want {
		t.Fatalf("buildVMExecScript with path:\n got:  %q\n want: %q", got, want)
	}
}

func TestBuildVMExecScriptWithoutGuestPath(t *testing.T) {
	got := buildVMExecScript("", []string{"whoami"})
	want := "if command -v mise >/dev/null 2>&1; then mise exec -- 'whoami'; else 'whoami'; fi"
	if got != want {
		t.Fatalf("buildVMExecScript without path:\n got:  %q\n want: %q", got, want)
	}
	if strings.Contains(got, "cd ") {
		t.Fatalf("expected no cd when guestPath is empty, got: %q", got)
	}
}

func TestBuildVMExecScriptShellQuotesPathWithSpaces(t *testing.T) {
	got := buildVMExecScript("/tmp/with space", []string{"echo", "a b"})
	if !strings.Contains(got, "cd '/tmp/with space'") {
		t.Fatalf("expected guest path to be shell-quoted, got: %q", got)
	}
	if !strings.Contains(got, "mise exec -- 'echo' 'a b'") {
		t.Fatalf("expected command args to be shell-quoted, got: %q", got)
	}
}
