Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions test/braintrust/trace/span_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ def test_init_with_filter_ai_spans_keeps_ai_spans
tracer_provider = OpenTelemetry::SDK::Trace::TracerProvider.new

Braintrust.init(
api_key: "test-key",
api_key: "test-api-key",
set_global: false,
blocking_login: false,
enable_tracing: true,
tracer_provider: tracer_provider,
exporter: exporter,
Expand Down Expand Up @@ -214,9 +213,8 @@ def setup_with_filter(filter_ai_spans: false, span_filter_funcs: [])
tracer_provider = OpenTelemetry::SDK::Trace::TracerProvider.new

Braintrust.init(
api_key: "test-key",
api_key: "test-api-key",
set_global: false,
blocking_login: false,
enable_tracing: true,
tracer_provider: tracer_provider,
exporter: exporter,
Expand Down
6 changes: 3 additions & 3 deletions test/braintrust/without_openai_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ def test_core_functionality_without_openai
skip "Test only runs in without-openai appraisal" if openai_available?

# Test that we can initialize Braintrust without tracing (no OpenAI needed)
# Note: "test-api-key" triggers fake auth to avoid HTTP requests
state = Braintrust.init(
api_key: "test-key",
api_key: "test-api-key",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constant?

set_global: false,
blocking_login: false,
enable_tracing: false
)

assert_instance_of Braintrust::State, state
assert_equal "test-key", state.api_key
assert_equal "test-api-key", state.api_key
end

def test_openai_require_fails_without_gem
Expand Down
23 changes: 23 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@

# Test helpers for OpenTelemetry tracing
module TracingTestHelper
# =============================================================================
# IMPORTANT: Magic Test API Key
# =============================================================================
#
# When calling Braintrust.init in tests, use api_key: "test-api-key" to trigger
# fake authentication that avoids HTTP requests. This magic key is handled in
# lib/braintrust/api/internal/auth.rb and returns fake org info immediately.
#
# Without this magic key, Braintrust.init spawns a background login thread that
# can cause WebMock errors after tests complete (orphan thread race condition).
#
# Example:
# Braintrust.init(api_key: "test-api-key", set_global: false, enable_tracing: true)
#
# TODO: Future work - move this magic key handling out of production code and into
# test helpers instead. Options include:
# 1. A test-only initializer that provides org_id directly (skips login entirely)
# 2. Dependency injection for the Auth module in tests
# 3. Environment-based test mode detection
#
# See: lib/braintrust/api/internal/auth.rb for the magic key implementation
# =============================================================================

# Wrapper for OpenTelemetry test setup
class OtelTestRig
attr_reader :tracer_provider, :exporter, :state
Expand Down