package main

import (
	"os"
	"testing"
	"time"

	"git.frostfs.info/TrueCloudLab/frostfs-http-gw/resolver"
	"github.com/stretchr/testify/require"
)

func TestConfigReload(t *testing.T) {
	f, err := os.CreateTemp("", "conf")
	require.NoError(t, err)
	defer func() {
		require.NoError(t, os.Remove(f.Name()))
	}()

	confData := `
pprof:
  enabled: true

resolve_bucket:
  default_namespaces: [""]

resolve_order:
  - nns
`

	_, err = f.WriteString(confData)
	require.NoError(t, err)
	require.NoError(t, f.Close())

	cfg := settings()

	require.NoError(t, cfg.flags.Parse([]string{"--config", f.Name(), "--connect_timeout", "15s"}))
	require.NoError(t, cfg.reload())

	require.True(t, cfg.config().GetBool(cfgPprofEnabled))
	require.Equal(t, []string{""}, cfg.config().GetStringSlice(cfgResolveDefaultNamespaces))
	require.Equal(t, []string{resolver.NNSResolver}, cfg.config().GetStringSlice(cfgResolveOrder))
	require.Equal(t, 15*time.Second, cfg.config().GetDuration(cfgConTimeout))

	require.NoError(t, os.Truncate(f.Name(), 0))
	require.NoError(t, cfg.reload())

	require.False(t, cfg.config().GetBool(cfgPprofEnabled))
	require.Equal(t, []string{"", "root"}, cfg.config().GetStringSlice(cfgResolveDefaultNamespaces))
	require.Equal(t, []string{resolver.NNSResolver, resolver.DNSResolver}, cfg.config().GetStringSlice(cfgResolveOrder))
	require.Equal(t, 15*time.Second, cfg.config().GetDuration(cfgConTimeout))
}

func TestSetTLSEnabled(t *testing.T) {
	cfg := settings()

	require.NoError(t, cfg.flags.Parse([]string{"--" + cfgTLSCertFile, "tls.crt", "--" + cfgTLSKeyFile, "tls.key"}))
	require.NoError(t, cfg.reload())

	require.True(t, cfg.config().GetBool(cfgServer+".0."+cfgTLSEnabled))
}