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 2.2 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
  1. import { Telemetry } from '../src/telemetry';
  2. import packageJson from '../package.json';
  3. import { fetchWithTimeout } from '../src/fetch';
  4. jest.mock('../src/fetch', () => ({
  5. fetchWithTimeout: jest.fn(),
  6. }));
  7. jest.mock('../src/util', () => ({
  8. maybeRecordFirstRun: jest.fn(),
  9. }));
  10. jest.mock('../package.json', () => ({
  11. version: '1.0.0',
  12. }));
  13. describe('Telemetry', () => {
  14. let originalEnv: NodeJS.ProcessEnv;
  15. beforeEach(() => {
  16. originalEnv = process.env;
  17. process.env = { ...originalEnv };
  18. (fetchWithTimeout as jest.Mock).mockClear();
  19. });
  20. afterEach(() => {
  21. process.env = originalEnv;
  22. });
  23. it('should not record events when telemetry is disabled', () => {
  24. process.env.PROMPTFOO_DISABLE_TELEMETRY = '1';
  25. const telemetry = new Telemetry();
  26. telemetry.record('eval_ran', { foo: 'bar' });
  27. expect(telemetry['events']).toHaveLength(0);
  28. });
  29. it('should record events when telemetry is enabled', () => {
  30. const telemetry = new Telemetry();
  31. telemetry.record('eval_ran', { foo: 'bar' });
  32. expect(telemetry['events']).toHaveLength(1);
  33. expect(telemetry['events'][0]).toEqual({
  34. event: 'eval_ran',
  35. packageVersion: packageJson.version,
  36. properties: { foo: 'bar' },
  37. });
  38. });
  39. it('should send events and clear events array when telemetry is enabled and send is called', async () => {
  40. (fetchWithTimeout as jest.Mock).mockResolvedValue({ ok: true });
  41. const telemetry = new Telemetry();
  42. telemetry.record('eval_ran', { foo: 'bar' });
  43. await telemetry.send();
  44. expect(fetchWithTimeout).toHaveBeenCalledWith(
  45. 'https://api.promptfoo.dev/telemetry',
  46. {
  47. method: 'POST',
  48. headers: {
  49. 'Content-Type': 'application/json',
  50. },
  51. body: JSON.stringify([
  52. { event: 'eval_ran', packageVersion: '1.0.0', properties: { foo: 'bar' } },
  53. ]),
  54. },
  55. 1000,
  56. );
  57. expect(telemetry['events']).toHaveLength(0);
  58. });
  59. it('should not send events when telemetry is disabled and send is called', async () => {
  60. process.env.PROMPTFOO_DISABLE_TELEMETRY = '1';
  61. const telemetry = new Telemetry();
  62. telemetry.record('eval_ran', { foo: 'bar' });
  63. await telemetry.send();
  64. expect(fetchWithTimeout).not.toHaveBeenCalled();
  65. });
  66. });
Tip!

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

Comments

Loading...