package telemetry_test import ( "crypto/sha256" "fmt" "testing" "github.com/google/uuid" "github.com/stretchr/testify/require" "github.com/coder/coder/v2/coderd/database" "github.com/coder/coder/v2/coderd/database/dbfake" "github.com/coder/coder/v2/coderd/database/dbgen" "github.com/coder/coder/v2/coderd/database/dbtestutil" "github.com/coder/coder/v2/coderd/telemetry" "github.com/coder/coder/v2/coderd/util/ptr" "github.com/coder/coder/v2/testutil" ) func TestConvertWorkspaceBuild_AgentRelay(t *testing.T) { t.Parallel() hash := func(s string) string { return fmt.Sprintf("%x", sha256.Sum256([]byte(s))) } tests := []struct { name string params map[string]string want *telemetry.WorkspaceBuildAgentRelay }{ { name: "NoParameters", params: nil, want: nil, }, { name: "NotRelay", params: map[string]string{ "region": "us-east", "agent_relay_pool": "pool", }, want: nil, }, { name: "ClaudeCode", params: map[string]string{ "agent_relay_session_id": "session-1", "agent_relay_pool": "claude-pool", "agent_relay_attempt": "2", "agent_relay_client_platform": "claude_in_slack", "agent_relay_claude_code_lock_to_account": "true", }, want: &telemetry.WorkspaceBuildAgentRelay{ Provider: telemetry.AgentRelayProviderClaudeCode, ClientPlatform: "claude_in_slack", SessionIDHash: hash("session-1"), PoolHash: hash("claude-pool"), Attempt: ptr.Ref(2), }, }, { // Attempt 0 is the first dispatch and must stay distinct from a // missing attempt. name: "AttemptZero", params: map[string]string{ "agent_relay_session_id": "session-1", "agent_relay_attempt": "0", }, want: &telemetry.WorkspaceBuildAgentRelay{ Provider: telemetry.AgentRelayProviderUnknown, SessionIDHash: hash("session-1"), Attempt: ptr.Ref(0), }, }, { // A template default stores an empty session ID on builds a // person creates by hand. name: "EmptySessionID", params: map[string]string{ "agent_relay_session_id": "", "agent_relay_pool": "pool", }, want: nil, }, { name: "ClientPlatformNormalized", params: map[string]string{ "agent_relay_session_id": "session-1", "agent_relay_client_platform": "Claude-In-Slack