// Code generated by mdatagen. DO NOT EDIT.

package metadata

import (
	"path/filepath"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	"github.com/stretchr/testify/require"

	"go.opentelemetry.io/collector/confmap"
	"go.opentelemetry.io/collector/confmap/confmaptest"
)

func TestMetricsBuilderConfig(t *testing.T) {
	tests := []struct {
		name string
		want MetricsBuilderConfig
	}{
		{
			name: "default",
			want: NewDefaultMetricsBuilderConfig(),
		},
		{
			name: "all_set",
			want: MetricsBuilderConfig{
				Metrics: MetricsConfig{
					LinuxMemoryAvailable: LinuxMemoryAvailableMetricConfig{
						Enabled: true,
					},
					SystemCPUFoo: SystemCPUFooMetricConfig{
						Enabled: true,
					},
					SystemCPUUtilization: SystemCPUUtilizationMetricConfig{
						Enabled:             true,
						AggregationStrategy: AggregationStrategyAvg,
						EnabledAttributes:   []SystemCPUUtilizationMetricAttributeKey{SystemCPUUtilizationMetricAttributeKeyCPU, SystemCPUUtilizationMetricAttributeKeyState},
					},
					SystemCPUUtilizationV1: SystemCPUUtilizationV1MetricConfig{
						Enabled:             true,
						AggregationStrategy: AggregationStrategySum,
						EnabledAttributes:   []SystemCPUUtilizationV1MetricAttributeKey{SystemCPUUtilizationV1MetricAttributeKeyCPULogicalNumber, SystemCPUUtilizationV1MetricAttributeKeyState},
					},
					SystemMemoryLinuxAvailable: SystemMemoryLinuxAvailableMetricConfig{
						Enabled: true,
					},
				},
			},
		},
		{
			name: "none_set",
			want: MetricsBuilderConfig{
				Metrics: MetricsConfig{
					LinuxMemoryAvailable: LinuxMemoryAvailableMetricConfig{
						Enabled: false,
					},
					SystemCPUFoo: SystemCPUFooMetricConfig{
						Enabled: false,
					},
					SystemCPUUtilization: SystemCPUUtilizationMetricConfig{
						Enabled:             false,
						AggregationStrategy: AggregationStrategyAvg,
						EnabledAttributes:   []SystemCPUUtilizationMetricAttributeKey{SystemCPUUtilizationMetricAttributeKeyCPU, SystemCPUUtilizationMetricAttributeKeyState},
					},
					SystemCPUUtilizationV1: SystemCPUUtilizationV1MetricConfig{
						Enabled:             false,
						AggregationStrategy: AggregationStrategySum,
						EnabledAttributes:   []SystemCPUUtilizationV1MetricAttributeKey{SystemCPUUtilizationV1MetricAttributeKeyCPULogicalNumber, SystemCPUUtilizationV1MetricAttributeKeyState},
					},
					SystemMemoryLinuxAvailable: SystemMemoryLinuxAvailableMetricConfig{
						Enabled: false,
					},
				},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			cfg := loadMetricsBuilderConfig(t, tt.name)
			diff := cmp.Diff(tt.want, cfg, cmpopts.IgnoreUnexported(LinuxMemoryAvailableMetricConfig{}, SystemCPUFooMetricConfig{}, SystemCPUUtilizationMetricConfig{}, SystemCPUUtilizationV1MetricConfig{}, SystemMemoryLinuxAvailableMetricConfig{}))
			require.Emptyf(t, diff, "Config mismatch (-expected +actual):\n%s", diff)
		})
	}
}

func TestSystemCPUUtilizationMetricsConfig_Validate(t *testing.T) {
	cfg := DefaultMetricsConfig().SystemCPUUtilization
	require.NoError(t, cfg.Validate())

	cfg.EnabledAttributes = []SystemCPUUtilizationMetricAttributeKey{"invalid"}
	require.ErrorContains(t, cfg.Validate(), "metric system.cpu.utilization doesn't have an attribute invalid, valid attributes: [cpu, state]")

	cfg = DefaultMetricsConfig().SystemCPUUtilization
	cfg.AggregationStrategy = "invalid"
	require.ErrorContains(t, cfg.Validate(), "invalid aggregation strategy")
}

func TestSystemCPUUtilizationV1MetricsConfig_Validate(t *testing.T) {
	cfg := DefaultMetricsConfig().SystemCPUUtilizationV1
	require.NoError(t, cfg.Validate())

	cfg.EnabledAttributes = []SystemCPUUtilizationV1MetricAttributeKey{"invalid"}
	require.ErrorContains(t, cfg.Validate(), "metric system.cpu.utilization@v1 doesn't have an attribute invalid, valid attributes: [cpu.logical_number, state]")

	cfg = DefaultMetricsConfig().SystemCPUUtilizationV1
	cfg.AggregationStrategy = "invalid"
	require.ErrorContains(t, cfg.Validate(), "invalid aggregation strategy")
}

func loadMetricsBuilderConfig(t *testing.T, name string) MetricsBuilderConfig {
	cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
	require.NoError(t, err)
	sub, err := cm.Sub(name)
	require.NoError(t, err)
	cfg := NewDefaultMetricsBuilderConfig()
	require.NoError(t, sub.Unmarshal(&cfg, confmap.WithIgnoreUnused()))
	return cfg
}
