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
|
- import { PostHog } from 'posthog-node';
- import { fetchWithTimeout } from '../src/fetch';
- import { Telemetry } from '../src/telemetry';
- // Mock PostHog
- jest.mock('posthog-node', () => {
- const mockCapture = jest.fn();
- const mockIdentify = jest.fn();
- return {
- PostHog: jest.fn().mockImplementation(() => ({
- capture: mockCapture,
- identify: mockIdentify,
- })),
- };
- });
- // Mock fetch
- jest.mock('../src/fetch', () => ({
- fetchWithTimeout: jest.fn(),
- }));
- // Mock crypto
- jest.mock('crypto', () => ({
- randomUUID: jest.fn().mockReturnValue('test-uuid'),
- }));
- // Mock globalConfig
- jest.mock('../src/globalConfig/globalConfig', () => ({
- readGlobalConfig: jest
- .fn()
- .mockReturnValue({ id: 'test-user-id', account: { email: 'test@example.com' } }),
- }));
- // Mock constants
- jest.mock('../src/constants', () => ({
- VERSION: '1.0.0',
- }));
- // Mock envars
- jest.mock('../src/envars', () => ({
- getEnvBool: jest.fn().mockImplementation((key) => {
- if (key === 'PROMPTFOO_DISABLE_TELEMETRY') {
- return process.env.PROMPTFOO_DISABLE_TELEMETRY === '1';
- }
- return false;
- }),
- getEnvString: jest.fn().mockImplementation((key) => {
- if (key === 'PROMPTFOO_POSTHOG_KEY') {
- return process.env.PROMPTFOO_POSTHOG_KEY || undefined;
- }
- if (key === 'PROMPTFOO_POSTHOG_HOST') {
- return process.env.PROMPTFOO_POSTHOG_HOST || undefined;
- }
- if (key === 'NODE_ENV') {
- return process.env.NODE_ENV || undefined;
- }
- return undefined;
- }),
- }));
- describe('Telemetry', () => {
- let originalEnv: NodeJS.ProcessEnv;
- let mockPostHogInstance: any;
- let mockFetch: jest.Mock;
- beforeEach(() => {
- originalEnv = process.env;
- process.env = { ...originalEnv };
- process.env.PROMPTFOO_POSTHOG_KEY = 'test-key';
- // Setup fetch mock
- mockFetch = jest.fn().mockResolvedValue({ ok: true });
- global.fetch = mockFetch;
- // Reset PostHog mock
- jest.mocked(PostHog).mockClear();
- mockPostHogInstance = {
- capture: jest.fn(),
- identify: jest.fn(),
- };
- jest.mocked(PostHog).mockImplementation(() => mockPostHogInstance);
- // Reset fetchWithTimeout mock
- jest.mocked(fetchWithTimeout).mockClear();
- });
- afterEach(() => {
- process.env = originalEnv;
- jest.resetAllMocks();
- });
- it('should not track events with PostHog when telemetry is disabled', () => {
- process.env.PROMPTFOO_DISABLE_TELEMETRY = '1';
- // Create telemetry instance with telemetry disabled
- const _telemetry = new Telemetry();
- // Record an event
- _telemetry.record('eval_ran', { foo: 'bar' });
- // PostHog capture should not be called
- expect(mockPostHogInstance.capture).not.toHaveBeenCalled();
- });
- it('should save consent successfully', async () => {
- jest.mocked(fetchWithTimeout).mockResolvedValue({ ok: true } as any);
- const _telemetry = new Telemetry();
- await _telemetry.saveConsent('test@example.com', { source: 'test' });
- expect(fetchWithTimeout).toHaveBeenCalledWith(
- 'https://api.promptfoo.dev/consent',
- {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({ email: 'test@example.com', metadata: { source: 'test' } }),
- },
- 1000,
- );
- });
- it('should handle failed consent save', async () => {
- jest.mocked(fetchWithTimeout).mockResolvedValue({ ok: false, statusText: 'Not Found' } as any);
- const _telemetry = new Telemetry();
- await _telemetry.saveConsent('test@example.com');
- expect(fetchWithTimeout).toHaveBeenCalledWith(
- 'https://api.promptfoo.dev/consent',
- expect.objectContaining({
- method: 'POST',
- body: expect.any(String),
- }),
- expect.any(Number),
- );
- });
- });
|