forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreferrals.test.ts
More file actions
51 lines (43 loc) · 1.63 KB
/
Copy pathreferrals.test.ts
File metadata and controls
51 lines (43 loc) · 1.63 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
import { beforeEach, describe, expect, it, vi } from 'vitest';
const { getIdToken } = vi.hoisted(() => ({ getIdToken: vi.fn() }));
vi.mock('@/lib/firebase', () => ({ getIdToken }));
import { claimReferralTrial, parseReferralEnvironment } from '@/lib/referrals';
describe('web referral handoff', () => {
beforeEach(() => {
getIdToken.mockReset();
vi.unstubAllGlobals();
});
it('accepts only the backend environments emitted by referral capture', () => {
expect(parseReferralEnvironment('prod')).toBe('prod');
expect(parseReferralEnvironment('dev')).toBe('dev');
expect(parseReferralEnvironment('https://attacker.example')).toBeNull();
expect(parseReferralEnvironment(null)).toBeNull();
});
it('claims with the signed-in user token', async () => {
getIdToken.mockResolvedValue('test-token');
const fetchMock = vi.fn(
async () =>
new Response(JSON.stringify({ claimed: true, trial_days: 30 }), {
headers: { 'content-type': 'application/json' },
}),
);
vi.stubGlobal('fetch', fetchMock);
await expect(claimReferralTrial('ref1.test.code', 'prod')).resolves.toEqual({
claimed: true,
trial_days: 30,
});
expect(fetchMock).toHaveBeenCalledWith(
'/api/referrals/claim',
expect.objectContaining({
method: 'POST',
headers: expect.objectContaining({ Authorization: 'Bearer test-token' }),
}),
);
});
it('fails closed without an authenticated user', async () => {
getIdToken.mockResolvedValue(null);
await expect(claimReferralTrial('ref1.test.code', 'prod')).rejects.toThrow(
'Not authenticated',
);
});
});