package cmd

import (
	"strings"
	"testing"
)

func TestIsWrappedCommand(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name string
		cmd  string
		want bool
	}{
		{name: "backup is wrapped", cmd: "backup", want: true},
		{name: "restore is wrapped", cmd: "restore", want: true},
		{name: "snapshots is wrapped", cmd: "snapshots", want: true},
		{name: "cat is not wrapped", cmd: "cat", want: false},
		{name: "ls is not wrapped", cmd: "ls", want: false},
		{name: "empty string", cmd: "", want: false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			got := isWrappedCommand(tt.cmd)
			if got != tt.want {
				t.Errorf("isWrappedCommand(%q) = %v, want %v", tt.cmd, got, tt.want)
			}
		})
	}
}

func TestSubcommandWithArgsStaysNonInteractive(t *testing.T) {
	// Not parallel: mutates package-level flag vars via
	// setRootFlagValuesForTest and environment via t.Setenv.
	configFile := setupCommandConfig(t)
	setRootFlagValuesForTest(t, "home@cloud", true, configFile)
	t.Setenv("KELD_CONFIG_FILE", "")
	t.Setenv("KELD_DRYRUN", "")

	backup := lookupSubcommand(t, "backup")
	out, err := captureStdout(t, func() error {
		return backup.RunE(backup, []string{"--tag", "daily", "/src"})
	})
	if err != nil {
		t.Fatalf("subcommand with args returned error: %v", err)
	}

	// Should produce dry-run output containing the command.
	if !strings.Contains(out, `"backup"`) {
		t.Fatalf("expected dry-run output to contain backup command, got:\n%s", out)
	}
	if !strings.Contains(out, `"--tag" "daily"`) {
		t.Fatalf("expected dry-run output to contain --tag daily, got:\n%s", out)
	}
}
