spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / mods / telemetry / tests / register.test.ts
139 lines · 3.3 KBRaw
1import { describe, expect, mock, test, tier } from 'claude-code/testing'
2
3import Fixtures from './fixtures'
4
5tier('builtin')
6
7describe('register', () => {
8 test(
9 'a $.telemetry.log call from a plugin posts one first-party row',
10 { plugins: [Fixtures.recording] },
11 async ($, on) => {
12 mock.env(on, { USER_TYPE: 'ant' })
13
14 const posts = Fixtures.firstPartySession(on)
15
16 expect(
17 await $.command.run(Fixtures.record(Fixtures.surveyAnswer())),
18 ).toEqual({ text: 'sent' })
19
20 expect(posts.map(post => post.init)).toMatchObject([
21 { method: 'POST', auth: 'the-handle' },
22 ])
23
24 expect(posts.map(Fixtures.batchOf)).toMatchObject([
25 {
26 events: [
27 {
28 event_data: {
29 event_name: 'tengu_plugin_survey_answered',
30 session_id: 'the-session',
31 model: 'the-model',
32 user_type: 'ant',
33 },
34 },
35 ],
36 },
37 ])
38 },
39 )
40
41 test(
42 'a row already named tengu_ is sent under its own name',
43 { plugins: [Fixtures.recording] },
44 async ($, on) => {
45 mock.env(on, {})
46
47 const posts = Fixtures.firstPartySession(on)
48
49 await $.command.run(
50 Fixtures.record({
51 ...Fixtures.surveyAnswer(),
52 event: 'tengu_repl_diff_panel_shown',
53 }),
54 )
55
56 expect(posts.map(Fixtures.batchOf)).toMatchObject([
57 {
58 events: [
59 {
60 event_data: {
61 event_name: 'tengu_repl_diff_panel_shown',
62 user_type: 'external',
63 },
64 },
65 ],
66 },
67 ])
68 },
69 )
70
71 test(
72 'nothing is sent for a person who asked not to be tracked',
73 { plugins: [Fixtures.recording] },
74 async ($, on) => {
75 mock.env(on, { DO_NOT_TRACK: '1' })
76
77 const posts = Fixtures.firstPartySession(on)
78
79 const { text } = await $.command.run(
80 Fixtures.record(Fixtures.surveyAnswer()),
81 )
82
83 expect({ text, posts }).toEqual({ text: 'sent', posts: [] })
84 },
85 )
86
87 test(
88 'nothing is sent on a third-party provider',
89 { plugins: [Fixtures.recording] },
90 async ($, on) => {
91 mock.env(on, { CLAUDE_CODE_USE_BEDROCK: '1' })
92
93 const posts = Fixtures.firstPartySession(on)
94
95 await $.command.run(Fixtures.record(Fixtures.surveyAnswer()))
96
97 expect(posts).toEqual([])
98 },
99 )
100
101 test(
102 'a session with no first-party credential is refused, nothing sent',
103 { plugins: [Fixtures.recording] },
104 async ($, on) => {
105 mock.env(on, {})
106
107 const posts = Fixtures.firstPartySession(on, null)
108
109 const { text } = await $.command.run(
110 Fixtures.record(Fixtures.surveyAnswer()),
111 )
112
113 expect(text).toEndWith(
114 '$.telemetry.log: this session has no first-party credential to ' +
115 'authorize',
116 )
117
118 expect(posts).toEqual([])
119 },
120 )
121
122 test(
123 'free text in a row is refused, nothing sent',
124 { plugins: [Fixtures.recording] },
125 async ($, on) => {
126 mock.env(on, {})
127
128 const posts = Fixtures.firstPartySession(on)
129
130 const { text } = await $.command.run({
131 ...Fixtures.record(Fixtures.surveyAnswer()),
132 args: '{"event":"survey_answered","props":{"note":"hello world"}}',
133 })
134
135 expect(text).toContain('props.note: free text is refused')
136 expect(posts).toEqual([])
137 },
138 )
139})