event_test.go

  1package event
  2
  3// These tests verify that the Error function correctly handles various
  4// scenarios. These tests will not log anything.
  5
  6import (
  7	"reflect"
  8	"testing"
  9
 10	"github.com/posthog/posthog-go"
 11)
 12
 13func TestError(t *testing.T) {
 14	t.Run("returns early when client is nil", func(t *testing.T) {
 15		// This test verifies that when the PostHog client is not initialized
 16		// the Error function safely returns early without attempting to
 17		// enqueue any events. This is important during initialization or when
 18		// metrics are disabled, as we don't want the error reporting mechanism
 19		// itself to cause panics.
 20		originalClient := client
 21		defer func() {
 22			client = originalClient
 23		}()
 24
 25		client = nil
 26		Error("test error", "key", "value")
 27	})
 28
 29	t.Run("handles nil client without panicking", func(t *testing.T) {
 30		// This test covers various edge cases where the error value might be
 31		// nil, a string, or an error type.
 32		originalClient := client
 33		defer func() {
 34			client = originalClient
 35		}()
 36
 37		client = nil
 38		Error(nil)
 39		Error("some error")
 40		Error(newDefaultTestError("runtime error"), "key", "value")
 41	})
 42
 43	t.Run("handles error with properties", func(t *testing.T) {
 44		// This test verifies that the Error function can handle additional
 45		// key-value properties that provide context about the error. These
 46		// properties are typically passed when recovering from panics (i.e.,
 47		// panic name, function name).
 48		//
 49		// Even with these additional properties, the function should handle
 50		// them gracefully without panicking.
 51		originalClient := client
 52		defer func() {
 53			client = originalClient
 54		}()
 55
 56		client = nil
 57		Error("test error",
 58			"type", "test",
 59			"severity", "high",
 60			"source", "unit-test",
 61		)
 62	})
 63}
 64
 65func TestPairsToProps(t *testing.T) {
 66	t.Run("sets valid key value pairs", func(t *testing.T) {
 67		got := pairsToProps("foo", "bar", "count", 3)
 68		want := posthog.NewProperties().
 69			Set("foo", "bar").
 70			Set("count", 3)
 71		if !reflect.DeepEqual(got, want) {
 72			t.Fatalf("pairsToProps() = %#v, want %#v", got, want)
 73		}
 74	})
 75
 76	t.Run("returns empty properties for odd pairs", func(t *testing.T) {
 77		got := pairsToProps("foo", "bar", "count")
 78		if len(got) != 0 {
 79			t.Fatalf("pairsToProps() should return empty properties, got %#v", got)
 80		}
 81	})
 82
 83	t.Run("ignores non-string key and continues", func(t *testing.T) {
 84		got := pairsToProps(123, "bad", "ok", true)
 85		want := posthog.NewProperties().Set("ok", true)
 86		if !reflect.DeepEqual(got, want) {
 87			t.Fatalf("pairsToProps() = %#v, want %#v", got, want)
 88		}
 89	})
 90}
 91
 92// newDefaultTestError creates a test error that mimics runtime panic
 93// errors. This helps us testing that the Error function can handle various
 94// error types, including those that might be passed from a panic recovery
 95// scenario.
 96func newDefaultTestError(s string) error {
 97	return testError(s)
 98}
 99
100type testError string
101
102func (e testError) Error() string {
103	return string(e)
104}