mirror of
https://github.com/hanzoai/insights-go.git
synced 2026-09-30 22:46:12 +00:00
128 lines
3.4 KiB
Go
128 lines
3.4 KiB
Go
package insights
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestConfigZeroValue(t *testing.T) {
|
|
c := Config{}
|
|
|
|
if err := c.Validate(); err != nil {
|
|
t.Error("validating the zero-value configuration failed:", err)
|
|
}
|
|
}
|
|
|
|
func TestConfigValidateTrimsWhitespaceSensitiveFields(t *testing.T) {
|
|
c := Config{
|
|
Endpoint: " \nhttps://app.posthog.com\t ",
|
|
PersonalApiKey: " \ttest-personal-key\n ",
|
|
}
|
|
|
|
require.NoError(t, c.Validate())
|
|
require.Equal(t, "https://app.posthog.com", c.Endpoint)
|
|
require.Equal(t, "test-personal-key", c.PersonalApiKey)
|
|
}
|
|
|
|
func TestMakeConfigDefaultsWhitespaceEndpoint(t *testing.T) {
|
|
got := makeConfig(Config{Endpoint: " \n\t "})
|
|
require.Equal(t, DefaultEndpoint, got.Endpoint)
|
|
}
|
|
|
|
func TestConfigInvalidInterval(t *testing.T) {
|
|
c := Config{
|
|
Interval: -1 * time.Second,
|
|
}
|
|
|
|
if err := c.Validate(); err == nil {
|
|
t.Error("no error returned when validating a malformed config")
|
|
|
|
} else if e, ok := err.(ConfigError); !ok {
|
|
t.Error("invalid error returned when checking a malformed config:", err)
|
|
|
|
} else if e.Field != "Interval" || e.Value.(time.Duration) != (-1*time.Second) {
|
|
t.Error("invalid field error reported:", e)
|
|
}
|
|
}
|
|
|
|
func TestConfig_MaxRetries(t *testing.T) {
|
|
c := Config{}
|
|
require.NoError(t, c.Validate())
|
|
got := makeConfig(c)
|
|
require.Equal(t, 10, got.maxAttempts)
|
|
|
|
c.MaxRetries = Ptr[int](-1)
|
|
require.ErrorContains(t, c.Validate(),
|
|
"insights.NewWithConfig: max retries out of range [0,9] (insights.Config.MaxRetries: -1)")
|
|
got = makeConfig(c)
|
|
require.Equal(t, 10, got.maxAttempts)
|
|
|
|
c.MaxRetries = Ptr[int](10)
|
|
require.ErrorContains(t, c.Validate(),
|
|
"insights.NewWithConfig: max retries out of range [0,9] (insights.Config.MaxRetries: 10)")
|
|
got = makeConfig(c)
|
|
require.Equal(t, 10, got.maxAttempts)
|
|
|
|
c.MaxRetries = Ptr[int](5)
|
|
require.NoError(t, c.Validate())
|
|
got = makeConfig(c)
|
|
require.Equal(t, 6, got.maxAttempts)
|
|
}
|
|
|
|
func TestConfigInvalidBatchSize(t *testing.T) {
|
|
c := Config{
|
|
BatchSize: -1,
|
|
}
|
|
|
|
if err := c.Validate(); err == nil {
|
|
t.Error("no error returned when validating a malformed config")
|
|
|
|
} else if e, ok := err.(ConfigError); !ok {
|
|
t.Error("invalid error returned when checking a malformed config:", err)
|
|
|
|
} else if e.Field != "BatchSize" || e.Value.(int) != -1 {
|
|
t.Error("invalid field error reported:", e)
|
|
}
|
|
}
|
|
|
|
func TestConfigGetDisableGeoIP(t *testing.T) {
|
|
var (
|
|
c Config
|
|
tv = true
|
|
fv = false
|
|
)
|
|
require.True(t, c.GetDisableGeoIP())
|
|
c.DisableGeoIP = &tv
|
|
require.True(t, c.GetDisableGeoIP())
|
|
c.DisableGeoIP = &fv
|
|
require.False(t, c.GetDisableGeoIP())
|
|
}
|
|
|
|
func TestConfigCompression(t *testing.T) {
|
|
// CompressionNone (0) should be valid
|
|
c := Config{Compression: CompressionNone}
|
|
require.NoError(t, c.Validate())
|
|
|
|
// CompressionGzip (1) should be valid
|
|
c = Config{Compression: CompressionGzip}
|
|
require.NoError(t, c.Validate())
|
|
|
|
// Values > CompressionGzip should be invalid
|
|
c = Config{Compression: 2}
|
|
err := c.Validate()
|
|
require.Error(t, err)
|
|
configErr, ok := err.(ConfigError)
|
|
require.True(t, ok, "expected ConfigError")
|
|
require.Equal(t, "Compression", configErr.Field)
|
|
require.Equal(t, CompressionMode(2), configErr.Value)
|
|
require.Contains(t, configErr.Reason, "invalid compression mode")
|
|
|
|
// Higher invalid values should also fail
|
|
c = Config{Compression: 255}
|
|
err = c.Validate()
|
|
require.Error(t, err)
|
|
require.ErrorContains(t, err, "invalid compression mode")
|
|
}
|