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 3.7 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
137
138
139
140
  1. import { PostHog } from 'posthog-node';
  2. import { fetchWithTimeout } from '../src/fetch';
  3. import { Telemetry } from '../src/telemetry';
  4. // Mock PostHog
  5. jest.mock('posthog-node', () => {
  6. const mockCapture = jest.fn();
  7. const mockIdentify = jest.fn();
  8. return {
  9. PostHog: jest.fn().mockImplementation(() => ({
  10. capture: mockCapture,
  11. identify: mockIdentify,
  12. })),
  13. };
  14. });
  15. // Mock fetch
  16. jest.mock('../src/fetch', () => ({
  17. fetchWithTimeout: jest.fn(),
  18. }));
  19. // Mock crypto
  20. jest.mock('crypto', () => ({
  21. randomUUID: jest.fn().mockReturnValue('test-uuid'),
  22. }));
  23. // Mock globalConfig
  24. jest.mock('../src/globalConfig/globalConfig', () => ({
  25. readGlobalConfig: jest
  26. .fn()
  27. .mockReturnValue({ id: 'test-user-id', account: { email: 'test@example.com' } }),
  28. }));
  29. // Mock constants
  30. jest.mock('../src/constants', () => ({
  31. VERSION: '1.0.0',
  32. }));
  33. // Mock envars
  34. jest.mock('../src/envars', () => ({
  35. getEnvBool: jest.fn().mockImplementation((key) => {
  36. if (key === 'PROMPTFOO_DISABLE_TELEMETRY') {
  37. return process.env.PROMPTFOO_DISABLE_TELEMETRY === '1';
  38. }
  39. return false;
  40. }),
  41. getEnvString: jest.fn().mockImplementation((key) => {
  42. if (key === 'PROMPTFOO_POSTHOG_KEY') {
  43. return process.env.PROMPTFOO_POSTHOG_KEY || undefined;
  44. }
  45. if (key === 'PROMPTFOO_POSTHOG_HOST') {
  46. return process.env.PROMPTFOO_POSTHOG_HOST || undefined;
  47. }
  48. if (key === 'NODE_ENV') {
  49. return process.env.NODE_ENV || undefined;
  50. }
  51. return undefined;
  52. }),
  53. }));
  54. describe('Telemetry', () => {
  55. let originalEnv: NodeJS.ProcessEnv;
  56. let mockPostHogInstance: any;
  57. let mockFetch: jest.Mock;
  58. beforeEach(() => {
  59. originalEnv = process.env;
  60. process.env = { ...originalEnv };
  61. process.env.PROMPTFOO_POSTHOG_KEY = 'test-key';
  62. // Setup fetch mock
  63. mockFetch = jest.fn().mockResolvedValue({ ok: true });
  64. global.fetch = mockFetch;
  65. // Reset PostHog mock
  66. jest.mocked(PostHog).mockClear();
  67. mockPostHogInstance = {
  68. capture: jest.fn(),
  69. identify: jest.fn(),
  70. };
  71. jest.mocked(PostHog).mockImplementation(() => mockPostHogInstance);
  72. // Reset fetchWithTimeout mock
  73. jest.mocked(fetchWithTimeout).mockClear();
  74. });
  75. afterEach(() => {
  76. process.env = originalEnv;
  77. jest.resetAllMocks();
  78. });
  79. it('should not track events with PostHog when telemetry is disabled', () => {
  80. process.env.PROMPTFOO_DISABLE_TELEMETRY = '1';
  81. // Create telemetry instance with telemetry disabled
  82. const _telemetry = new Telemetry();
  83. // Record an event
  84. _telemetry.record('eval_ran', { foo: 'bar' });
  85. // PostHog capture should not be called
  86. expect(mockPostHogInstance.capture).not.toHaveBeenCalled();
  87. });
  88. it('should save consent successfully', async () => {
  89. jest.mocked(fetchWithTimeout).mockResolvedValue({ ok: true } as any);
  90. const _telemetry = new Telemetry();
  91. await _telemetry.saveConsent('test@example.com', { source: 'test' });
  92. expect(fetchWithTimeout).toHaveBeenCalledWith(
  93. 'https://api.promptfoo.dev/consent',
  94. {
  95. method: 'POST',
  96. headers: {
  97. 'Content-Type': 'application/json',
  98. },
  99. body: JSON.stringify({ email: 'test@example.com', metadata: { source: 'test' } }),
  100. },
  101. 1000,
  102. );
  103. });
  104. it('should handle failed consent save', async () => {
  105. jest.mocked(fetchWithTimeout).mockResolvedValue({ ok: false, statusText: 'Not Found' } as any);
  106. const _telemetry = new Telemetry();
  107. await _telemetry.saveConsent('test@example.com');
  108. expect(fetchWithTimeout).toHaveBeenCalledWith(
  109. 'https://api.promptfoo.dev/consent',
  110. expect.objectContaining({
  111. method: 'POST',
  112. body: expect.any(String),
  113. }),
  114. expect.any(Number),
  115. );
  116. });
  117. });
Tip!

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

Comments

Loading...