package cmd

import (
	"strings"
	"testing"
)

func TestRootRunNonInteractiveRequiresSubcommand(t *testing.T) {
	// Not parallel: mutates package-level isStdinTerminal.

	// Override stdin terminal check to simulate non-interactive environment.
	original := isStdinTerminal
	t.Cleanup(func() {
		isStdinTerminal = original
	})
	isStdinTerminal = func() bool { return false }

	// Reset root command flags to defaults.
	setRootFlagValuesForTest(t, "", false, "")
	t.Setenv("KELD_CONFIG_FILE", "")
	t.Setenv("KELD_DRYRUN", "")

	err := rootCmd.RunE(rootCmd, nil)
	if err == nil {
		t.Fatal("expected error for bare keld in non-interactive mode, got nil")
	}

	errMsg := err.Error()
	// Should NOT be a bubbletea TTY error.
	if strings.Contains(errMsg, "bubbletea") || strings.Contains(errMsg, "TTY") {
		t.Fatalf("non-interactive mode incorrectly tried to open TTY: %v", err)
	}

	// Should mention missing subcommand.
	if !strings.Contains(errMsg, "subcommand") {
		t.Fatalf("expected error to mention subcommand, got: %v", err)
	}
}
