-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage-analytics.test.ts
More file actions
92 lines (77 loc) · 2.74 KB
/
Copy pathusage-analytics.test.ts
File metadata and controls
92 lines (77 loc) · 2.74 KB
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
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { track } from './usage-analytics';
describe('usage-analytics', () => {
let fetchMock: ReturnType<typeof vi.fn>;
beforeEach(() => {
fetchMock = vi.fn().mockResolvedValue({ ok: true });
vi.stubGlobal('fetch', fetchMock);
});
afterEach(() => {
vi.unstubAllGlobals();
delete process.env['ILN_ANALYTICS'];
delete process.env['ILN_ANALYTICS_ENDPOINT'];
});
it('does not call fetch when ILN_ANALYTICS is not set', () => {
track('getInvoice', 'testnet', true);
expect(fetchMock).not.toHaveBeenCalled();
});
it('does not call fetch when ILN_ANALYTICS=0', () => {
process.env['ILN_ANALYTICS'] = '0';
track('getInvoice', 'testnet', true);
expect(fetchMock).not.toHaveBeenCalled();
});
it('calls fetch when ILN_ANALYTICS=1', () => {
process.env['ILN_ANALYTICS'] = '1';
track('getInvoice', 'testnet', true);
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it('sends correct method and network', () => {
process.env['ILN_ANALYTICS'] = '1';
track('submitInvoice', 'mainnet', true);
const [, init] = fetchMock.mock.calls[0];
const body = JSON.parse(init.body);
expect(body.method).toBe('submitInvoice');
expect(body.network).toBe('mainnet');
expect(body.success).toBe(true);
expect(body.version).toBeTruthy();
});
it('includes errorCode on failure', () => {
process.env['ILN_ANALYTICS'] = '1';
track('fundInvoice', 'testnet', false, 'NetworkError');
const [, init] = fetchMock.mock.calls[0];
const body = JSON.parse(init.body);
expect(body.success).toBe(false);
expect(body.errorCode).toBe('NetworkError');
});
it('never includes PII fields in the payload', () => {
process.env['ILN_ANALYTICS'] = '1';
track('getInvoice', 'testnet', true);
const [, init] = fetchMock.mock.calls[0];
const body = JSON.parse(init.body);
const piiFields = [
'address',
'freelancer',
'payer',
'funder',
'secretKey',
'publicKey',
'amount',
'invoiceId',
];
for (const field of piiFields) {
expect(body).not.toHaveProperty(field);
}
});
it('uses custom endpoint when ILN_ANALYTICS_ENDPOINT is set', () => {
process.env['ILN_ANALYTICS'] = '1';
process.env['ILN_ANALYTICS_ENDPOINT'] = 'https://custom.example.com/event';
track('markPaid', 'testnet', true);
const [url] = fetchMock.mock.calls[0];
expect(url).toBe('https://custom.example.com/event');
});
it('does not throw when fetch rejects', async () => {
process.env['ILN_ANALYTICS'] = '1';
fetchMock.mockRejectedValueOnce(new Error('network failure'));
expect(() => track('getInvoice', 'testnet', true)).not.toThrow();
});
});