|
| 1 | +// SPDX-FileCopyrightText: Copyright 2026 Dash0 Inc. |
| 2 | + |
| 3 | +//go:build e2e |
| 4 | + |
| 5 | +package e2e |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "os" |
| 13 | + "os/exec" |
| 14 | + "path/filepath" |
| 15 | + "sync" |
| 16 | + "testing" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/stretchr/testify/assert" |
| 20 | + "github.com/stretchr/testify/require" |
| 21 | +) |
| 22 | + |
| 23 | +// TestE2EHookInvocation simulates what Claude Code does when firing a hook: |
| 24 | +// builds the binary, invokes on-event.sh with a SessionStart event on stdin, |
| 25 | +// and verifies the mock OTLP server receives the connectivity check. |
| 26 | +func TestE2EHookInvocation(t *testing.T) { |
| 27 | + pluginDir := findPluginDir(t) |
| 28 | + |
| 29 | + // Build the binary fresh. |
| 30 | + binDir := t.TempDir() |
| 31 | + binary := filepath.Join(binDir, "on-event-test-linux-amd64") |
| 32 | + build := exec.Command("go", "build", "-o", binary, "./cmd/on-event") |
| 33 | + build.Dir = pluginDir |
| 34 | + out, err := build.CombinedOutput() |
| 35 | + require.NoError(t, err, "build failed: %s", string(out)) |
| 36 | + |
| 37 | + var ( |
| 38 | + mu sync.Mutex |
| 39 | + requests []capturedRequest |
| 40 | + ) |
| 41 | + |
| 42 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 43 | + body, _ := io.ReadAll(r.Body) |
| 44 | + mu.Lock() |
| 45 | + requests = append(requests, capturedRequest{ |
| 46 | + path: r.URL.Path, |
| 47 | + auth: r.Header.Get("Authorization"), |
| 48 | + body: body, |
| 49 | + method: r.Method, |
| 50 | + }) |
| 51 | + mu.Unlock() |
| 52 | + w.WriteHeader(http.StatusOK) |
| 53 | + })) |
| 54 | + defer srv.Close() |
| 55 | + |
| 56 | + dataDir := t.TempDir() |
| 57 | + |
| 58 | + t.Run("SessionStart fires connectivity check", func(t *testing.T) { |
| 59 | + event := `{"hook_event_name":"SessionStart","session_id":"e2e-test-session","model":"claude-opus-4-7"}` |
| 60 | + runBinary(t, binary, event, dataDir, srv.URL) |
| 61 | + |
| 62 | + time.Sleep(500 * time.Millisecond) |
| 63 | + |
| 64 | + mu.Lock() |
| 65 | + defer mu.Unlock() |
| 66 | + |
| 67 | + var traceReqs []capturedRequest |
| 68 | + for _, r := range requests { |
| 69 | + if r.path == "/v1/traces" { |
| 70 | + traceReqs = append(traceReqs, r) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + assert.NotEmpty(t, traceReqs, "connectivity check should hit /v1/traces on SessionStart") |
| 75 | + if len(traceReqs) > 0 { |
| 76 | + assert.Equal(t, "Bearer e2e-test-token", traceReqs[0].auth) |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + t.Run("full turn produces chat and tool spans", func(t *testing.T) { |
| 81 | + mu.Lock() |
| 82 | + requests = nil |
| 83 | + mu.Unlock() |
| 84 | + |
| 85 | + // UserPromptSubmit — creates trace context. |
| 86 | + runBinary(t, binary, `{"hook_event_name":"UserPromptSubmit","session_id":"e2e-test-session","prompt":"hello"}`, dataDir, srv.URL) |
| 87 | + |
| 88 | + // PostToolUse — emits a tool span. |
| 89 | + runBinary(t, binary, `{"hook_event_name":"PostToolUse","session_id":"e2e-test-session","tool_name":"Bash","tool_use_id":"tu1","tool_input":"ls","tool_response":"file.txt","duration_ms":100}`, dataDir, srv.URL) |
| 90 | + |
| 91 | + // Stop — emits a chat span. |
| 92 | + runBinary(t, binary, `{"hook_event_name":"Stop","session_id":"e2e-test-session","model":"claude-opus-4-7","stop_reason":"end_turn"}`, dataDir, srv.URL) |
| 93 | + |
| 94 | + time.Sleep(500 * time.Millisecond) |
| 95 | + |
| 96 | + mu.Lock() |
| 97 | + defer mu.Unlock() |
| 98 | + |
| 99 | + var traceReqs []capturedRequest |
| 100 | + for _, r := range requests { |
| 101 | + if r.path == "/v1/traces" { |
| 102 | + traceReqs = append(traceReqs, r) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Expect at least 2 trace exports: tool span + chat span. |
| 107 | + assert.GreaterOrEqual(t, len(traceReqs), 2, "expected tool span + chat span") |
| 108 | + |
| 109 | + // Verify spans contain expected attributes. |
| 110 | + for _, r := range traceReqs { |
| 111 | + body := string(r.body) |
| 112 | + assert.Contains(t, body, "e2e-test-session", "span should contain conversation ID") |
| 113 | + } |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("SessionEnd cleans up session directory", func(t *testing.T) { |
| 117 | + sessionDir := filepath.Join(dataDir, "e2e-test-session") |
| 118 | + require.DirExists(t, sessionDir, "session dir should exist before SessionEnd") |
| 119 | + |
| 120 | + runBinary(t, binary, `{"hook_event_name":"SessionEnd","session_id":"e2e-test-session"}`, dataDir, srv.URL) |
| 121 | + |
| 122 | + assert.NoDirExists(t, sessionDir, "session dir should be cleaned up after SessionEnd") |
| 123 | + }) |
| 124 | +} |
| 125 | + |
| 126 | +func TestE2EFullFlowWithClaude(t *testing.T) { |
| 127 | + claudeBin, err := exec.LookPath("claude") |
| 128 | + if err != nil { |
| 129 | + t.Skip("claude CLI not found in PATH") |
| 130 | + } |
| 131 | + if os.Getenv("ANTHROPIC_API_KEY") == "" { |
| 132 | + t.Skip("ANTHROPIC_API_KEY not set — cannot run full Claude flow") |
| 133 | + } |
| 134 | + |
| 135 | + pluginDir := findPluginDir(t) |
| 136 | + |
| 137 | + var ( |
| 138 | + mu sync.Mutex |
| 139 | + requests []capturedRequest |
| 140 | + ) |
| 141 | + |
| 142 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 143 | + body, _ := io.ReadAll(r.Body) |
| 144 | + mu.Lock() |
| 145 | + requests = append(requests, capturedRequest{ |
| 146 | + path: r.URL.Path, |
| 147 | + auth: r.Header.Get("Authorization"), |
| 148 | + body: body, |
| 149 | + method: r.Method, |
| 150 | + }) |
| 151 | + mu.Unlock() |
| 152 | + w.WriteHeader(http.StatusOK) |
| 153 | + })) |
| 154 | + defer srv.Close() |
| 155 | + |
| 156 | + // Write a .env in a temp work dir so the plugin picks up our mock URL. |
| 157 | + workDir := t.TempDir() |
| 158 | + envContent := "DASH0_OTLP_URL=" + srv.URL + "\nDASH0_AUTH_TOKEN=e2e-test-token\n" |
| 159 | + require.NoError(t, os.WriteFile(filepath.Join(workDir, ".env"), []byte(envContent), 0o644)) |
| 160 | + |
| 161 | + cmd := exec.Command(claudeBin, "--print", "--plugin-dir", pluginDir) |
| 162 | + cmd.Stdin = nil // empty prompt — claude will just respond and exit |
| 163 | + cmd.Dir = workDir |
| 164 | + cmd.Env = os.Environ() |
| 165 | + |
| 166 | + output, _ := cmd.CombinedOutput() |
| 167 | + t.Logf("claude output: %s", string(output)) |
| 168 | + |
| 169 | + time.Sleep(3 * time.Second) |
| 170 | + |
| 171 | + mu.Lock() |
| 172 | + defer mu.Unlock() |
| 173 | + |
| 174 | + t.Logf("requests received: %d", len(requests)) |
| 175 | + for _, r := range requests { |
| 176 | + t.Logf(" %s %s (%d bytes)", r.method, r.path, len(r.body)) |
| 177 | + } |
| 178 | + |
| 179 | + assert.NotEmpty(t, requests, "expected at least one request to mock OTLP server from Claude session") |
| 180 | +} |
| 181 | + |
| 182 | +type capturedRequest struct { |
| 183 | + path string |
| 184 | + auth string |
| 185 | + body []byte |
| 186 | + method string |
| 187 | +} |
| 188 | + |
| 189 | +func runBinary(t *testing.T, binary, event, dataDir, otlpURL string) { |
| 190 | + t.Helper() |
| 191 | + cmd := exec.Command(binary) |
| 192 | + cmd.Stdin = stringReader(event) |
| 193 | + cmd.Env = []string{ |
| 194 | + "CLAUDE_PLUGIN_DATA=" + dataDir, |
| 195 | + "CLAUDE_PLUGIN_OPTION_OTLP_URL=" + otlpURL, |
| 196 | + "CLAUDE_PLUGIN_OPTION_AUTH_TOKEN=e2e-test-token", |
| 197 | + "CLAUDE_PLUGIN_OPTION_OMIT_USER_INFO=false", |
| 198 | + "CLAUDE_PLUGIN_OPTION_OMIT_IO=false", |
| 199 | + "HOME=" + os.Getenv("HOME"), |
| 200 | + "PATH=" + os.Getenv("PATH"), |
| 201 | + } |
| 202 | + out, err := cmd.CombinedOutput() |
| 203 | + if err != nil { |
| 204 | + t.Logf("binary output: %s (err: %v)", string(out), err) |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +func stringReader(s string) *os.File { |
| 209 | + r, w, _ := os.Pipe() |
| 210 | + go func() { |
| 211 | + w.Write([]byte(s)) |
| 212 | + w.Close() |
| 213 | + }() |
| 214 | + return r |
| 215 | +} |
| 216 | + |
| 217 | +func findPluginDir(t *testing.T) string { |
| 218 | + t.Helper() |
| 219 | + dir, err := os.Getwd() |
| 220 | + require.NoError(t, err) |
| 221 | + for { |
| 222 | + if _, err := os.Stat(filepath.Join(dir, ".claude-plugin", "plugin.json")); err == nil { |
| 223 | + return dir |
| 224 | + } |
| 225 | + parent := filepath.Dir(dir) |
| 226 | + if parent == dir { |
| 227 | + t.Fatal("could not find plugin root (no .claude-plugin/plugin.json)") |
| 228 | + } |
| 229 | + dir = parent |
| 230 | + } |
| 231 | +} |
| 232 | + |
| 233 | +// Unused but needed to satisfy json import. |
| 234 | +var _ = json.Marshal |
0 commit comments