Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

telemetry.test.ts 4.0 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  1. import packageJson from '../package.json';
  2. import { fetchWithTimeout } from '../src/fetch';
  3. import { Telemetry } from '../src/telemetry';
  4. jest.mock('../src/fetch', () => ({
  5. fetchWithTimeout: jest.fn(),
  6. }));
  7. jest.mock('../package.json', () => ({
  8. version: '1.0.0',
  9. }));
  10. describe('Telemetry', () => {
  11. let originalEnv: NodeJS.ProcessEnv;
  12. beforeEach(() => {
  13. originalEnv = process.env;
  14. process.env = { ...originalEnv };
  15. jest.mocked(fetchWithTimeout).mockClear();
  16. });
  17. afterEach(() => {
  18. process.env = originalEnv;
  19. });
  20. it('should record only the "telemetry disabled" event when telemetry is disabled', () => {
  21. process.env.PROMPTFOO_DISABLE_TELEMETRY = '1';
  22. const telemetry = new Telemetry();
  23. telemetry.record('eval_ran', { foo: 'bar' });
  24. expect(telemetry['events']).toHaveLength(1);
  25. expect(telemetry['events'][0]).toEqual({
  26. event: 'feature_used',
  27. packageVersion: packageJson.version,
  28. properties: { feature: 'telemetry disabled' },
  29. });
  30. });
  31. it('should record events when telemetry is enabled', () => {
  32. delete process.env.PROMPTFOO_DISABLE_TELEMETRY;
  33. const telemetry = new Telemetry();
  34. telemetry.record('eval_ran', { foo: 'bar' });
  35. expect(telemetry['events']).toHaveLength(1);
  36. expect(telemetry['events'][0]).toEqual({
  37. event: 'eval_ran',
  38. packageVersion: packageJson.version,
  39. properties: { foo: 'bar' },
  40. });
  41. });
  42. it('should send events and clear events array when telemetry is enabled and send is called', async () => {
  43. delete process.env.PROMPTFOO_DISABLE_TELEMETRY;
  44. jest.mocked(fetchWithTimeout).mockResolvedValue({ ok: true } as any);
  45. const telemetry = new Telemetry();
  46. telemetry.record('eval_ran', { foo: 'bar' });
  47. await telemetry.send();
  48. expect(fetchWithTimeout).toHaveBeenCalledWith(
  49. 'https://api.promptfoo.dev/telemetry',
  50. {
  51. method: 'POST',
  52. headers: {
  53. 'Content-Type': 'application/json',
  54. },
  55. body: JSON.stringify([
  56. { event: 'eval_ran', packageVersion: '1.0.0', properties: { foo: 'bar' } },
  57. ]),
  58. },
  59. 1000,
  60. );
  61. expect(telemetry['events']).toHaveLength(0);
  62. });
  63. it('should send only the "telemetry disabled" event when telemetry is disabled and send is called', async () => {
  64. process.env.PROMPTFOO_DISABLE_TELEMETRY = '1';
  65. jest.mocked(fetchWithTimeout).mockResolvedValue({ ok: true } as any);
  66. const telemetry = new Telemetry();
  67. telemetry.record('eval_ran', { foo: 'bar' });
  68. await telemetry.send();
  69. expect(fetchWithTimeout).toHaveBeenCalledWith(
  70. 'https://api.promptfoo.dev/telemetry',
  71. {
  72. method: 'POST',
  73. headers: {
  74. 'Content-Type': 'application/json',
  75. },
  76. body: JSON.stringify([
  77. {
  78. event: 'feature_used',
  79. packageVersion: '1.0.0',
  80. properties: { feature: 'telemetry disabled' },
  81. },
  82. ]),
  83. },
  84. 1000,
  85. );
  86. expect(telemetry['events']).toHaveLength(0);
  87. });
  88. it('should send telemetry disabled event only once', async () => {
  89. process.env.PROMPTFOO_DISABLE_TELEMETRY = '1';
  90. jest.mocked(fetchWithTimeout).mockResolvedValue({ ok: true } as any);
  91. const telemetry = new Telemetry();
  92. telemetry.record('eval_ran', { foo: 'bar' });
  93. await telemetry.send();
  94. expect(fetchWithTimeout).toHaveBeenCalledTimes(1);
  95. expect(fetchWithTimeout).toHaveBeenCalledWith(
  96. 'https://api.promptfoo.dev/telemetry',
  97. {
  98. method: 'POST',
  99. headers: {
  100. 'Content-Type': 'application/json',
  101. },
  102. body: JSON.stringify([
  103. {
  104. event: 'feature_used',
  105. packageVersion: '1.0.0',
  106. properties: { feature: 'telemetry disabled' },
  107. },
  108. ]),
  109. },
  110. 1000,
  111. );
  112. // Record another event and send again
  113. telemetry.record('command_used', { command: 'test' });
  114. await telemetry.send();
  115. // Ensure fetchWithTimeout was not called again
  116. expect(fetchWithTimeout).toHaveBeenCalledTimes(1);
  117. });
  118. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...