Files
insights-go/error_tracking_stack_trace_test.go
T
zeekay b4e1366340 rebrand: export Insights types instead of PostHog
Rename Go package from posthog to insights across all 80+ files.
Consumers now import as `insights "github.com/hanzoai/insights-go"`
and use insights.Client, insights.Config, insights.Capture, etc.

- Rename posthog.go -> insights.go, posthog_test.go -> insights_test.go
- Update package declaration from posthog to insights in all .go files
- Update SDKName from posthog-go to insights-go
- Update DefaultEndpoint from app.posthog.com to insights.hanzo.ai
- Update all FieldError/ConfigError type strings
- Update User-Agent headers, log prefixes, comments, and docs
- Update JSON test fixtures to match new SDK name
- Update CLI, examples, extras, and sdk_compliance_adapter
- Fix pre-existing go vet warnings (non-constant format strings)
2026-03-13 19:54:58 -07:00

198 lines
7.4 KiB
Go

package insights
import (
"path/filepath"
"runtime"
"testing"
)
func TestSimpleInAppDecider(t *testing.T) {
tests := map[string]struct {
filePath string
expected bool
}{
"toolchain under module cache => not in app": {
filePath: filepath.Join("gopath", "pkg", "mod", "golang.org", "[email protected]", "src", "net", "http", "server.go"),
expected: false,
},
"vendored dependency => not in app": {
filePath: filepath.Join("myrepo", "vendor", "github.com", "gin-gonic", "gin", "gin.go"),
expected: false,
},
"general module cache => not in app": {
filePath: filepath.Join("gopath", "pkg", "mod", "github.com", "someone", "[email protected]", "x.go"),
expected: false,
},
"normal repo file => in app": {
filePath: filepath.Join("Users", "me", "dev", "repo", "pkg", "foo", "bar.go"),
expected: true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
actual := SimpleInAppDecider(runtime.Frame{File: tc.filePath})
if actual != tc.expected {
t.Fatalf("SimpleInAppDecider(%q) = %v, want %v",
tc.filePath, actual, tc.expected)
}
})
}
}
// go:inline - Avoid optimizations which might break the stack trace
func recursiveChain(ex DefaultStackTraceExtractor, skip, depth int) []StackFrame {
if depth <= 0 {
st := ex.GetStackTrace(skip)
if st == nil {
return nil
}
return st.Frames
}
return recursiveChain(ex, skip, depth-1)
}
func TestDefaultStackTraceExtractor_GetStackTrace(t *testing.T) {
extractor := DefaultStackTraceExtractor{InAppDecider: SimpleInAppDecider}
tests := map[string]struct {
functionCallDepth int
skip int
expectedNames []string
}{
"the basic happy path with no skip or function depth": {
functionCallDepth: 0,
skip: 0,
expectedNames: []string{
"runtime.Callers",
"github.com/hanzoai/insights-go.DefaultStackTraceExtractor.GetStackTrace",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.TestDefaultStackTraceExtractor_GetStackTrace.func1",
"testing.tRunner",
"runtime.goexit",
},
},
"skipping will remove entries from the head of the list, useful to omit internal function calls that add no value": {
functionCallDepth: 0,
skip: 3,
expectedNames: []string{
"github.com/hanzoai/insights-go.TestDefaultStackTraceExtractor_GetStackTrace.func1",
"testing.tRunner",
"runtime.goexit",
},
},
"calls with a very high skip level should return an empty stack trace": {
functionCallDepth: 0,
skip: 300,
expectedNames: []string{},
},
"if we have a longer function call depth, they will all show up in the trace": {
functionCallDepth: 5,
skip: 0,
expectedNames: []string{
"runtime.Callers",
"github.com/hanzoai/insights-go.DefaultStackTraceExtractor.GetStackTrace",
// +1 total for the 'actual' function call
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.TestDefaultStackTraceExtractor_GetStackTrace.func1",
"testing.tRunner",
"runtime.goexit",
},
},
"once we exceed the pre-configured limit of 64, we will start to drop traces at the tail end": {
functionCallDepth: 77,
skip: 0,
expectedNames: []string{
"runtime.Callers",
"github.com/hanzoai/insights-go.DefaultStackTraceExtractor.GetStackTrace",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
"github.com/hanzoai/insights-go.recursiveChain",
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
traces := recursiveChain(extractor, tc.skip, tc.functionCallDepth)
if len(traces) != len(tc.expectedNames) {
t.Fatalf("trace length does not match expectations: got=%v want=%v",
len(traces), len(tc.expectedNames))
}
for i := range traces {
if traces[i].Function != tc.expectedNames[i] {
t.Fatalf("trace resolved name does not match expectations (idx=%d): got=%v want=%v",
i, traces[i].Function, tc.expectedNames[i])
}
}
})
}
}