// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package x

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

//nolint:unused
func run(steps ...func(*testing.T)) func(*testing.T) {
	return func(t *testing.T) {
		t.Helper()
		for _, step := range steps {
			step(t)
		}
	}
}

//nolint:unused
func setenv(k, v string) func(t *testing.T) {
	return func(t *testing.T) { t.Setenv(k, v) }
}

//nolint:unused
func assertEnabled[T any](f Feature[T], want T) func(*testing.T) {
	return func(t *testing.T) {
		t.Helper()
		assert.True(t, f.Enabled(), "not enabled")

		v, ok := f.Lookup()
		assert.True(t, ok, "Lookup state")
		assert.Equal(t, want, v, "Lookup value")
	}
}

//nolint:unused
func assertDisabled[T any](f Feature[T]) func(*testing.T) {
	var zero T
	return func(t *testing.T) {
		t.Helper()

		assert.False(t, f.Enabled(), "enabled")

		v, ok := f.Lookup()
		assert.False(t, ok, "Lookup state")
		assert.Equal(t, zero, v, "Lookup value")
	}
}

func TestMetricExportBatchSize(t *testing.T) {
	tests := []struct {
		name    string
		value   string
		enabled bool
		want    int
	}{
		{
			name:    "empty",
			value:   "",
			enabled: false,
			want:    0,
		},
		{
			name:    "invalid",
			value:   "invalid",
			enabled: false,
			want:    0,
		},
		{
			name:    "zero",
			value:   "0",
			enabled: false,
			want:    0,
		},
		{
			name:    "negative",
			value:   "-10",
			enabled: false,
			want:    0,
		},
		{
			name:    "valid small",
			value:   "10",
			enabled: true,
			want:    10,
		},
		{
			name:    "valid large",
			value:   "200",
			enabled: true,
			want:    200,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Setenv(MetricExportBatchSize.Key(), tt.value)
			assert.Equal(t, tt.enabled, MetricExportBatchSize.Enabled())
			got, ok := MetricExportBatchSize.Lookup()
			assert.Equal(t, tt.enabled, ok)
			assert.Equal(t, tt.want, got)
		})
	}
}
