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