package state

import (
	"bytes"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/helmfile/helmfile/pkg/helmexec"
	"github.com/helmfile/helmfile/pkg/testutil"
)

func TestAppendWaitForJobsFlags(t *testing.T) {
	tests := []struct {
		name     string
		release  *ReleaseSpec
		syncOpts *SyncOpts
		helmSpec HelmSpec
		expected []string
	}{
		{
			name:     "release wait for jobs",
			release:  &ReleaseSpec{WaitForJobs: &[]bool{true}[0]},
			syncOpts: nil,
			helmSpec: HelmSpec{},
			expected: []string{"--wait-for-jobs"},
		},
		{
			name:     "cli flags wait for jobs",
			release:  &ReleaseSpec{},
			syncOpts: &SyncOpts{WaitForJobs: true},
			helmSpec: HelmSpec{},
			expected: []string{"--wait-for-jobs"},
		},
		{
			name:     "helm defaults wait for jobs",
			release:  &ReleaseSpec{},
			syncOpts: nil,
			helmSpec: HelmSpec{WaitForJobs: true},
			expected: []string{"--wait-for-jobs"},
		},
		{
			name:     "release wait for jobs false",
			release:  &ReleaseSpec{WaitForJobs: &[]bool{false}[0]},
			syncOpts: nil,
			helmSpec: HelmSpec{WaitForJobs: true},
			expected: []string{},
		},
		{
			name:     "cli flags wait for jobs false",
			release:  &ReleaseSpec{},
			syncOpts: &SyncOpts{},
			helmSpec: HelmSpec{WaitForJobs: true},
			expected: []string{"--wait-for-jobs"},
		},
		{
			name:     "helm defaults wait for jobs false",
			release:  &ReleaseSpec{},
			syncOpts: nil,
			helmSpec: HelmSpec{WaitForJobs: false},
			expected: []string{},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			st := &HelmState{}
			st.HelmDefaults = tt.helmSpec
			got := st.appendWaitForJobsFlags([]string{}, tt.release, tt.syncOpts)
			require.Equalf(t, tt.expected, got, "appendWaitForJobsFlags() = %v, want %v", got, tt.expected)
		})
	}
}

func TestAppendWaitFlags(t *testing.T) {
	tests := []struct {
		name     string
		release  *ReleaseSpec
		syncOpts *SyncOpts
		helmSpec HelmSpec
		helm     helmexec.Interface
		expected []string
	}{
		// --wait
		{
			name:     "release wait",
			release:  &ReleaseSpec{Wait: &[]bool{true}[0]},
			syncOpts: nil,
			helmSpec: HelmSpec{},
			helm:     testutil.NewVersionHelmExec("3.15.0"),
			expected: []string{"--wait"},
		},
		{
			name:     "cli flags wait",
			release:  &ReleaseSpec{},
			syncOpts: &SyncOpts{Wait: true},
			helmSpec: HelmSpec{},
			helm:     testutil.NewVersionHelmExec("3.15.0"),
			expected: []string{"--wait"},
		},
		{
			name:     "helm defaults wait",
			release:  &ReleaseSpec{},
			syncOpts: nil,
			helmSpec: HelmSpec{Wait: true},
			helm:     testutil.NewVersionHelmExec("3.15.0"),
			expected: []string{"--wait"},
		},
		{
			name:     "release wait false",
			release:  &ReleaseSpec{Wait: &[]bool{false}[0]},
			syncOpts: nil,
			helmSpec: HelmSpec{Wait: true},
			helm:     testutil.NewVersionHelmExec("3.15.0"),
			expected: []string{},
		},
		{
			name:     "cli flags wait false",
			release:  &ReleaseSpec{},
			syncOpts: &SyncOpts{},
			helmSpec: HelmSpec{Wait: true},
			helm:     testutil.NewVersionHelmExec("3.15.0"),
			expected: []string{"--wait"},
		},
		{
			name:     "helm defaults wait false",
			release:  &ReleaseSpec{},
			syncOpts: nil,
			helmSpec: HelmSpec{Wait: false},
			helm:     testutil.NewVersionHelmExec("3.15.0"),
			expected: []string{},
		},
		// --wait-retries flag has been removed from Helm
		{
			name:     "release wait and retry unsupported",
			release:  &ReleaseSpec{Wait: &[]bool{true}[0], WaitRetries: &[]int{1}[0]},
			syncOpts: nil,
			helmSpec: HelmSpec{},
			helm:     testutil.NewVersionHelmExec("3.15.0"),
			expected: []string{"--wait"},
		},
		{
			name:     "release wait and retry - retries ignored",
			release:  &ReleaseSpec{Wait: &[]bool{true}[0], WaitRetries: &[]int{1}[0]},
			syncOpts: nil,
			helmSpec: HelmSpec{},
			helm:     testutil.NewVersionHelmExec("3.15.0"),
			expected: []string{"--wait"},
		},
		{
			name:     "no wait retry",
			release:  &ReleaseSpec{WaitRetries: &[]int{1}[0]},
			syncOpts: nil,
			helmSpec: HelmSpec{},
			helm:     testutil.NewVersionHelmExec("3.15.0"),
			expected: []string{},
		},
		{
			name:     "cli flags wait and retry - retries ignored",
			release:  &ReleaseSpec{},
			syncOpts: &SyncOpts{Wait: true, WaitRetries: 2},
			helmSpec: HelmSpec{},
			helm:     testutil.NewVersionHelmExec("3.15.0"),
			expected: []string{"--wait"},
		},
		{
			name:     "helm defaults wait retry - retries ignored",
			release:  &ReleaseSpec{},
			syncOpts: nil,
			helmSpec: HelmSpec{Wait: true, WaitRetries: 3},
			helm:     testutil.NewVersionHelmExec("3.15.0"),
			expected: []string{"--wait"},
		},
		{
			name:     "release wait default retries - retries ignored",
			release:  &ReleaseSpec{Wait: &[]bool{true}[0]},
			syncOpts: nil,
			helmSpec: HelmSpec{WaitRetries: 4},
			helm:     testutil.NewVersionHelmExec("3.15.0"),
			expected: []string{"--wait"},
		},
		{
			name:     "release retries default wait - retries ignored",
			release:  &ReleaseSpec{WaitRetries: &[]int{5}[0]},
			syncOpts: nil,
			helmSpec: HelmSpec{Wait: true},
			helm:     testutil.NewVersionHelmExec("3.15.0"),
			expected: []string{"--wait"},
		},
		// helm-legacy track mode with Helm v4
		{
			name:     "helm-legacy track mode with Helm v4",
			release:  &ReleaseSpec{Wait: &[]bool{true}[0], TrackMode: "helm-legacy"},
			syncOpts: nil,
			helmSpec: HelmSpec{},
			helm:     testutil.NewVersionHelmExec("4.0.0"),
			expected: []string{"--wait=legacy"},
		},
		{
			name:     "helm-legacy track mode with Helm v4 from syncOpts",
			release:  &ReleaseSpec{Wait: &[]bool{true}[0]},
			syncOpts: &SyncOpts{TrackMode: "helm-legacy"},
			helmSpec: HelmSpec{},
			helm:     testutil.NewVersionHelmExec("4.0.0"),
			expected: []string{"--wait=legacy"},
		},
		{
			name:     "helm-legacy track mode with Helm v4 from helmDefaults",
			release:  &ReleaseSpec{Wait: &[]bool{true}[0]},
			syncOpts: nil,
			helmSpec: HelmSpec{TrackMode: "helm-legacy"},
			helm:     testutil.NewVersionHelmExec("4.0.0"),
			expected: []string{"--wait=legacy"},
		},
		{
			name:     "helm-legacy track mode with Helm v3 shows warning and uses --wait",
			release:  &ReleaseSpec{Wait: &[]bool{true}[0], Name: "test-release", TrackMode: "helm-legacy"},
			syncOpts: nil,
			helmSpec: HelmSpec{},
			helm:     testutil.NewVersionHelmExec("3.15.0"),
			expected: []string{"--wait"},
		},
		{
			name:     "helm-legacy track mode without wait flag",
			release:  &ReleaseSpec{Wait: &[]bool{false}[0], Name: "test-release", TrackMode: "helm-legacy"},
			syncOpts: nil,
			helmSpec: HelmSpec{},
			helm:     testutil.NewVersionHelmExec("4.0.0"),
			expected: []string{},
		},
		{
			name:     "helm track mode with Helm v4 uses --wait",
			release:  &ReleaseSpec{Wait: &[]bool{true}[0], TrackMode: "helm"},
			syncOpts: nil,
			helmSpec: HelmSpec{},
			helm:     testutil.NewVersionHelmExec("4.0.0"),
			expected: []string{"--wait"},
		},
		{
			// Kubedog tracking does not customize the helm --wait strategy.
			// helm runs with its default waiter — wedges are recovered by
			// the safety-valve helm-killer in helmx.go, so we don't try to
			// pick a "less buggy" wait strategy in advance. This keeps the
			// wait-flag logic identical to upstream helmfile.
			name:     "kubedog track mode with Helm v4 and wait true uses plain --wait",
			release:  &ReleaseSpec{Wait: &[]bool{true}[0], TrackMode: "kubedog"},
			syncOpts: nil,
			helmSpec: HelmSpec{},
			helm:     testutil.NewVersionHelmExec("4.0.0"),
			expected: []string{"--wait"},
		},
		{
			name:     "kubedog track mode with Helm v4 and explicit wait false skips --wait",
			release:  &ReleaseSpec{Wait: &[]bool{false}[0], TrackMode: "kubedog"},
			syncOpts: nil,
			helmSpec: HelmSpec{Wait: true},
			helm:     testutil.NewVersionHelmExec("4.0.0"),
			expected: []string{},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var buffer bytes.Buffer
			st := &HelmState{}
			st.HelmDefaults = tt.helmSpec
			st.logger = helmexec.NewLogger(&buffer, "debug")
			got := st.appendWaitFlags([]string{}, tt.helm, tt.release, tt.syncOpts)
			require.Equalf(t, tt.expected, got, "appendWaitFlags() = %v, want %v", got, tt.expected)

			// Check for warning message when helm-legacy is used with Helm v3
			if tt.name == "helm-legacy track mode with Helm v3 shows warning and uses --wait" {
				require.Contains(t, buffer.String(), "trackMode 'helm-legacy' requires Helm v4")
				require.Contains(t, buffer.String(), tt.release.Name)
			}
		})
	}
}

func TestAppendCascadeFlags(t *testing.T) {
	type args struct {
		flags    []string
		release  *ReleaseSpec
		cascade  string
		helm     helmexec.Interface
		helmSpec HelmSpec
		expected []string
	}
	tests := []struct {
		name string
		args args
	}{
		{
			name: "no cascade when helm less than 3.11.0",
			args: args{
				flags:    []string{},
				release:  &ReleaseSpec{},
				cascade:  "background",
				helm:     testutil.NewVersionHelmExec("3.11.0"),
				expected: []string{},
			},
		},
		{
			name: "cascade from release",
			args: args{
				flags:    []string{},
				release:  &ReleaseSpec{Cascade: &[]string{"background", "background"}[0]},
				cascade:  "",
				helm:     testutil.NewVersionHelmExec("3.12.1"),
				expected: []string{"--cascade", "background"},
			},
		},
		{
			name: "cascade from cmd flag",
			args: args{
				flags:    []string{},
				release:  &ReleaseSpec{},
				cascade:  "background",
				helm:     testutil.NewVersionHelmExec("3.12.1"),
				expected: []string{"--cascade", "background"},
			},
		},
		{
			name: "cascade from helm defaults",
			args: args{
				flags:    []string{},
				release:  &ReleaseSpec{},
				helmSpec: HelmSpec{Cascade: &[]string{"background", "background"}[0]},
				cascade:  "",
				helm:     testutil.NewVersionHelmExec("3.12.1"),
				expected: []string{"--cascade", "background"},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			st := &HelmState{}
			st.HelmDefaults = tt.args.helmSpec
			got := st.appendCascadeFlags(tt.args.flags, tt.args.helm, tt.args.release, tt.args.cascade)
			require.Equalf(t, tt.args.expected, got, "appendCascadeFlags() = %v, want %v", got, tt.args.expected)
		})
	}
}
func TestAppendSuppressOutputLineRegexFlags(t *testing.T) {
	tests := []struct {
		name                    string
		flags                   []string
		release                 *ReleaseSpec
		suppressOutputLineRegex []string
		helmDefaults            HelmSpec
		expected                []string
	}{
		{
			name:                    "release suppress output line regex",
			flags:                   []string{},
			release:                 &ReleaseSpec{SuppressOutputLineRegex: []string{"regex1", "regex2"}},
			suppressOutputLineRegex: nil,
			helmDefaults:            HelmSpec{},
			expected:                []string{"--suppress-output-line-regex", "regex1", "--suppress-output-line-regex", "regex2"},
		},
		{
			name:                    "cmd suppress output line regex",
			flags:                   []string{},
			release:                 &ReleaseSpec{},
			suppressOutputLineRegex: []string{"regex1", "regex2"},
			helmDefaults:            HelmSpec{},
			expected:                []string{"--suppress-output-line-regex", "regex1", "--suppress-output-line-regex", "regex2"},
		},
		{
			name:                    "helm defaults suppress output line regex",
			flags:                   []string{},
			release:                 &ReleaseSpec{},
			suppressOutputLineRegex: nil,
			helmDefaults:            HelmSpec{SuppressOutputLineRegex: []string{"regex1", "regex2"}},
			expected:                []string{"--suppress-output-line-regex", "regex1", "--suppress-output-line-regex", "regex2"},
		},
		{
			name:                    "empty suppress output line regex",
			flags:                   []string{},
			release:                 &ReleaseSpec{},
			suppressOutputLineRegex: nil,
			helmDefaults:            HelmSpec{},
			expected:                []string{},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			st := &HelmState{}
			st.HelmDefaults = tt.helmDefaults
			got := st.appendSuppressOutputLineRegexFlags(tt.flags, tt.release, tt.suppressOutputLineRegex)
			require.Equalf(t, tt.expected, got, "appendSuppressOutputLineRegexFlags() = %v, want %v", got, tt.expected)
		})
	}
}

func TestAppendShowOnlyFlags(t *testing.T) {
	tests := []struct {
		name         string
		templateOpts []string
		expected     []string
	}{
		{
			name:         "cli template show only with 1 file",
			templateOpts: []string{"templates/config.yaml"},
			expected:     []string{"--show-only", "templates/config.yaml"},
		},
		{
			name:         "cli template show only with 2 files",
			templateOpts: []string{"templates/config.yaml", "templates/resources.yaml"},
			expected:     []string{"--show-only", "templates/config.yaml", "--show-only", "templates/resources.yaml"},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			st := &HelmState{}
			got := st.appendShowOnlyFlags([]string{}, tt.templateOpts)
			require.Equalf(t, tt.expected, got, "appendShowOnlyFlags() = %v, want %v", got, tt.expected)
		})
	}
}

func TestAppendHideNotesFlags(t *testing.T) {
	type args struct {
		flags    []string
		helm     helmexec.Interface
		helmSpec HelmSpec
		opt      *SyncOpts
		expected []string
	}
	tests := []struct {
		name string
		args args
	}{
		{
			name: "no hide-notes when helm less than 3.16.0",
			args: args{
				flags: []string{},
				helm:  testutil.NewVersionHelmExec("3.15.0"),
				opt: &SyncOpts{
					HideNotes: true,
				},
				expected: []string{},
			},
		},
		{
			name: "hide-notes from cmd flag",
			args: args{
				flags: []string{},
				helm:  testutil.NewVersionHelmExec("3.16.0"),
				opt: &SyncOpts{
					HideNotes: true,
				},
				expected: []string{"--hide-notes"},
			},
		},
		{
			name: "no hide-notes when ops is nil",
			args: args{
				flags:    []string{},
				helm:     testutil.NewVersionHelmExec("3.16.0"),
				opt:      nil,
				expected: []string{},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			st := &HelmState{}
			st.HelmDefaults = tt.args.helmSpec
			got := st.appendHideNotesFlags(tt.args.flags, tt.args.helm, tt.args.opt)
			require.Equalf(t, tt.args.expected, got, "appendHideNotesFlags() = %v, want %v", got, tt.args.expected)
		})
	}
}

func TestAppendTakeOwnershipFlagsForUpgrade(t *testing.T) {
	type args struct {
		flags    []string
		helm     helmexec.Interface
		helmSpec HelmSpec
		opt      *SyncOpts
		release  *ReleaseSpec
		expected []string
	}
	tests := []struct {
		name string
		args args
	}{
		{
			name: "no take-ownership when helm less than 3.17.0",
			args: args{
				flags:   []string{},
				release: &ReleaseSpec{},
				helm:    testutil.NewVersionHelmExec("3.16.0"),
				opt: &SyncOpts{
					TakeOwnership: true,
				},
				expected: []string{},
			},
		},
		{
			name: "take-ownership from cmd flag",
			args: args{
				flags:   []string{},
				helm:    testutil.NewVersionHelmExec("3.17.0"),
				release: &ReleaseSpec{},
				opt: &SyncOpts{
					TakeOwnership: true,
				},
				expected: []string{"--take-ownership"},
			},
		},
		{
			name: "take-ownership from release",
			args: args{
				flags: []string{},
				helm:  testutil.NewVersionHelmExec("3.17.0"),
				release: &ReleaseSpec{
					TakeOwnership: &[]bool{true}[0],
				},
				opt:      &SyncOpts{},
				expected: []string{"--take-ownership"},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			st := &HelmState{}
			st.HelmDefaults = tt.args.helmSpec
			got := st.appendTakeOwnershipFlagsForUpgrade(tt.args.flags, tt.args.helm, tt.args.release, tt.args.opt.TakeOwnership)
			require.Equalf(t, tt.args.expected, got, "appendTakeOwnershipFlagsForUpgrade() = %v, want %v", got, tt.args.expected)
		})
	}
}

func TestFormatLabels(t *testing.T) {
	tests := []struct {
		name   string
		labels map[string]string
		want   string
	}{
		{
			name:   "empty labels",
			labels: map[string]string{},
			want:   "",
		},
		{
			name:   "single label",
			labels: map[string]string{"foo": "bar"},
			want:   "foo=bar",
		},
		{
			name:   "multiple labels",
			labels: map[string]string{"foo": "bar", "baz": "qux"},
			want:   "baz=qux,foo=bar",
		},
		{
			name:   "multiple labels with empty value",
			labels: map[string]string{"foo": "bar", "baz": "qux", "quux": ""},
			want:   "baz=qux,foo=bar,quux=",
		},
		{
			name:   "multiple labels with empty key",
			labels: map[string]string{"foo": "bar", "baz": "qux", "": "quux"},
			want:   "baz=qux,foo=bar",
		},
		{
			name:   "empty label value",
			labels: map[string]string{"foo": ""},
			want:   "foo=",
		},
		{
			name:   "empty label key",
			labels: map[string]string{"": "bar"},
			want:   "",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := formatLabels(tt.labels)
			require.Equal(t, tt.want, got, "formatLabels() = %v, want %v", got, tt.want)
		})
	}
}

func TestGetReleaseHardTimeout(t *testing.T) {
	intPtr := func(i int) *int { return &i }
	tests := []struct {
		name     string
		state    HelmState
		release  ReleaseSpec
		opts     *SyncOpts
		wantSecs int
	}{
		{
			name:     "ops.Timeout wins over release.Timeout and HelmDefaults.Timeout",
			state:    HelmState{ReleaseSetSpec: ReleaseSetSpec{HelmDefaults: HelmSpec{Timeout: 100}}},
			release:  ReleaseSpec{Timeout: intPtr(200)},
			opts:     &SyncOpts{Timeout: 300},
			wantSecs: 300,
		},
		{
			name:     "release.Timeout wins over HelmDefaults.Timeout when ops is unset",
			state:    HelmState{ReleaseSetSpec: ReleaseSetSpec{HelmDefaults: HelmSpec{Timeout: 100}}},
			release:  ReleaseSpec{Timeout: intPtr(200)},
			opts:     nil,
			wantSecs: 200,
		},
		{
			name:     "HelmDefaults.Timeout used when neither ops nor release set",
			state:    HelmState{ReleaseSetSpec: ReleaseSetSpec{HelmDefaults: HelmSpec{Timeout: 100}}},
			release:  ReleaseSpec{},
			opts:     nil,
			wantSecs: 100,
		},
		{
			name:     "falls back to release.TrackTimeout when no helm timeout configured",
			state:    HelmState{},
			release:  ReleaseSpec{TrackTimeout: intPtr(450)},
			opts:     nil,
			wantSecs: 450,
		},
		{
			name:     "falls back to ops.TrackTimeout when no other timeout configured",
			state:    HelmState{},
			release:  ReleaseSpec{},
			opts:     &SyncOpts{TrackTimeout: 450},
			wantSecs: 450,
		},
		{
			name:     "10-minute default when nothing configured",
			state:    HelmState{},
			release:  ReleaseSpec{},
			opts:     nil,
			wantSecs: 600,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := tt.state.getReleaseHardTimeout(&tt.release, tt.opts)
			if int(got.Seconds()) != tt.wantSecs {
				t.Errorf("getReleaseHardTimeout() = %v, want %ds", got, tt.wantSecs)
			}
		})
	}
}

func strPtr(s string) *string {
	return &s
}

func TestAppendServerSideFlagsForUpgrade(t *testing.T) {
	type args struct {
		flags    []string
		helm     helmexec.Interface
		helmSpec HelmSpec
		opt      *SyncOpts
		release  *ReleaseSpec
		expected []string
		wantErr  bool
	}
	tests := []struct {
		name string
		args args
	}{
		{
			name: "no server-side when not configured",
			args: args{
				flags:    []string{},
				helm:     testutil.NewHelmExec(true),
				release:  &ReleaseSpec{},
				opt:      &SyncOpts{},
				expected: []string{},
			},
		},
		{
			name: "server-side from cmd flag",
			args: args{
				flags:    []string{},
				helm:     testutil.NewHelmExec(true),
				release:  &ReleaseSpec{},
				opt:      &SyncOpts{ServerSide: "true"},
				expected: []string{"--server-side", "true"},
			},
		},
		{
			name: "server-side from release",
			args: args{
				flags:    []string{},
				helm:     testutil.NewHelmExec(true),
				release:  &ReleaseSpec{ServerSide: strPtr("false")},
				opt:      &SyncOpts{},
				expected: []string{"--server-side", "false"},
			},
		},
		{
			name: "server-side from helmDefaults",
			args: args{
				flags:    []string{},
				helm:     testutil.NewHelmExec(true),
				helmSpec: HelmSpec{ServerSide: strPtr("auto")},
				release:  &ReleaseSpec{},
				opt:      &SyncOpts{},
				expected: []string{"--server-side", "auto"},
			},
		},
		{
			name: "release-level overrides cmd flag",
			args: args{
				flags:    []string{},
				helm:     testutil.NewHelmExec(true),
				release:  &ReleaseSpec{ServerSide: strPtr("true")},
				opt:      &SyncOpts{ServerSide: "false"},
				expected: []string{"--server-side", "true"},
			},
		},
		{
			name: "cmd flag overrides helmDefaults",
			args: args{
				flags:    []string{},
				helm:     testutil.NewHelmExec(true),
				helmSpec: HelmSpec{ServerSide: strPtr("false")},
				release:  &ReleaseSpec{},
				opt:      &SyncOpts{ServerSide: "true"},
				expected: []string{"--server-side", "true"},
			},
		},
		{
			name: "helm 3 with no config is ignored",
			args: args{
				flags:    []string{},
				helm:     testutil.NewVersionHelmExec("3.17.0"),
				release:  &ReleaseSpec{},
				opt:      &SyncOpts{},
				expected: []string{},
			},
		},
		{
			name: "helm 3 with release-level config errors",
			args: args{
				flags:    []string{},
				helm:     testutil.NewVersionHelmExec("3.17.0"),
				release:  &ReleaseSpec{ServerSide: strPtr("true")},
				opt:      &SyncOpts{},
				expected: []string{},
				wantErr:  true,
			},
		},
		{
			name: "helm 3 with helmDefaults config errors",
			args: args{
				flags:    []string{},
				helm:     testutil.NewVersionHelmExec("3.17.0"),
				helmSpec: HelmSpec{ServerSide: strPtr("true")},
				release:  &ReleaseSpec{},
				opt:      &SyncOpts{},
				expected: []string{},
				wantErr:  true,
			},
		},
		{
			name: "invalid value errors",
			args: args{
				flags:    []string{},
				helm:     testutil.NewHelmExec(true),
				release:  &ReleaseSpec{},
				opt:      &SyncOpts{ServerSide: "yes"},
				expected: []string{},
				wantErr:  true,
			},
		},
		{
			name: "empty release-level value falls through to cmd flag",
			args: args{
				flags:    []string{},
				helm:     testutil.NewHelmExec(true),
				release:  &ReleaseSpec{ServerSide: strPtr("")},
				opt:      &SyncOpts{ServerSide: "auto"},
				expected: []string{"--server-side", "auto"},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			st := &HelmState{}
			st.HelmDefaults = tt.args.helmSpec
			optServerSide := ""
			if tt.args.opt != nil {
				optServerSide = tt.args.opt.ServerSide
			}
			got, err := st.appendServerSideFlagsForUpgrade(tt.args.flags, tt.args.helm, tt.args.release, optServerSide)
			if tt.args.wantErr {
				require.Error(t, err, "appendServerSideFlagsForUpgrade() should return error")
			} else {
				require.NoError(t, err, "appendServerSideFlagsForUpgrade() should not return error")
				require.Equalf(t, tt.args.expected, got, "appendServerSideFlagsForUpgrade() = %v, want %v", got, tt.args.expected)
			}
		})
	}
}
