// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package xvm

import (
	"testing"
	"time"

	"github.com/stretchr/testify/require"

	"github.com/luxfi/node/vms/xvm/network"
)

func TestParseConfig(t *testing.T) {
	tests := []struct {
		name           string
		configBytes    []byte
		expectedConfig Config
	}{
		{
			name:           "unspecified config",
			configBytes:    nil,
			expectedConfig: DefaultConfig,
		},
		{
			name:        "manually specified checksums enabled",
			configBytes: []byte(`{"checksums-enabled":true}`),
			expectedConfig: Config{
				Network:          network.DefaultConfig,
				ChecksumsEnabled: true,
				Config:           DefaultConfig.Config,
			},
		},
		{
			name:        "manually specified network value",
			configBytes: []byte(`{"network":{"max-validator-set-staleness":1}}`),
			expectedConfig: Config{
				Network: network.Config{
					MaxValidatorSetStaleness:                    time.Nanosecond,
					TargetGossipSize:                            network.DefaultConfig.TargetGossipSize,
					PushGossipPercentStake:                      network.DefaultConfig.PushGossipPercentStake,
					PushGossipNumValidators:                     network.DefaultConfig.PushGossipNumValidators,
					PushGossipNumPeers:                          network.DefaultConfig.PushGossipNumPeers,
					PushRegossipNumValidators:                   network.DefaultConfig.PushRegossipNumValidators,
					PushRegossipNumPeers:                        network.DefaultConfig.PushRegossipNumPeers,
					PushGossipDiscardedCacheSize:                network.DefaultConfig.PushGossipDiscardedCacheSize,
					PushGossipMaxRegossipFrequency:              network.DefaultConfig.PushGossipMaxRegossipFrequency,
					PushGossipFrequency:                         network.DefaultConfig.PushGossipFrequency,
					PullGossipPollSize:                          network.DefaultConfig.PullGossipPollSize,
					PullGossipFrequency:                         network.DefaultConfig.PullGossipFrequency,
					PullGossipThrottlingPeriod:                  network.DefaultConfig.PullGossipThrottlingPeriod,
					PullGossipThrottlingLimit:                   network.DefaultConfig.PullGossipThrottlingLimit,
					ExpectedBloomFilterElements:                 network.DefaultConfig.ExpectedBloomFilterElements,
					ExpectedBloomFilterFalsePositiveProbability: network.DefaultConfig.ExpectedBloomFilterFalsePositiveProbability,
					MaxBloomFilterFalsePositiveProbability:      network.DefaultConfig.MaxBloomFilterFalsePositiveProbability,
				},
				ChecksumsEnabled: DefaultConfig.ChecksumsEnabled,
				Config:           DefaultConfig.Config,
			},
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			require := require.New(t)

			config, err := ParseConfig(test.configBytes)
			require.NoError(err)
			require.Equal(test.expectedConfig, config)
		})
	}
}
