-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.test.ts
More file actions
94 lines (88 loc) · 3.19 KB
/
Copy pathusage.test.ts
File metadata and controls
94 lines (88 loc) · 3.19 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
93
94
/**
* @jest-environment node
*/
import { GET } from '@/app/api/auxillaries/usage/route';
import { createClient } from '@bitcode/supabase/ssr/server';
import { supabaseAdmin } from '@bitcode/supabase';
// Helper to create GET Request
function makeRequest(query: string) {
return new Request(`https://example.com/api/auxillaries/usage${query}`, { method: 'GET' });
}
jest.mock('@bitcode/supabase/ssr/server', () => ({
createClient: jest.fn()
}));
jest.mock('@bitcode/supabase', () => ({
supabaseAdmin: {
from: jest.fn()
}
}));
describe('User Usage GET', () => {
const mockUser = { id: 'u1' };
beforeEach(() => {
jest.resetAllMocks();
// Authenticated user
(createClient as jest.Mock).mockResolvedValue({
auth: { getUser: jest.fn().mockResolvedValue({ data: { user: mockUser }, error: null }) }
});
});
it('returns daily summary for a single event without aggregate param', async () => {
// Mock raw events builder
const events = [
{ created_at: '2023-01-01T05:00:00Z', change: 10, balance: 10 }
];
const builder: any = {
select: jest.fn().mockReturnThis(),
eq: jest.fn().mockReturnThis(),
order: jest.fn().mockReturnThis(),
then: (resolve: any) => resolve({ data: events, error: null })
};
(supabaseAdmin.from as jest.Mock).mockReturnValue(builder);
const res = await GET(makeRequest('')); // no aggregate
expect(res.status).toBe(200);
const data = await res.json();
expect(Array.isArray(data)).toBe(true);
expect(data).toHaveLength(1);
const summary = data[0];
expect(summary.date).toBe('2023-01-01');
expect(summary.purchased).toBe(10);
expect(summary.spent).toBe(0);
expect(summary.net).toBe(10);
expect(summary.balance).toBe(10);
});
it('returns aggregated daily summary when aggregate=daily', async () => {
const events = [
{ created_at: '2023-02-01T01:00:00Z', change: -5, balance: 95 },
{ created_at: '2023-02-01T12:00:00Z', change: 20, balance: 115 }
];
const builder: any = {
select: jest.fn().mockReturnThis(),
eq: jest.fn().mockReturnThis(),
order: jest.fn().mockReturnThis(),
gte: jest.fn().mockReturnThis(),
lte: jest.fn().mockReturnThis(),
then: (resolve: any) => resolve({ data: events, error: null })
};
(supabaseAdmin.from as jest.Mock).mockReturnValue(builder);
const res = await GET(makeRequest('?aggregate=daily&from=2023-02-01&to=2023-02-02'));
expect(res.status).toBe(200);
const data = await res.json();
expect(Array.isArray(data)).toBe(true);
expect(data).toHaveLength(1);
const summary = data[0];
expect(summary.period).toBe('2023-02-01');
expect(summary.purchased).toBe(20);
expect(summary.spent).toBe(5);
expect(summary.net).toBe(15);
expect(summary.balance).toBe(115);
});
it('returns 401 if unauthenticated', async () => {
// Mock auth fail
(createClient as jest.Mock).mockResolvedValue({
auth: { getUser: jest.fn().mockResolvedValue({ data: { user: null }, error: new Error('no auth') }) }
});
const res = await GET(makeRequest(''));
expect(res.status).toBe(401);
const json = await res.json();
expect(json.error).toBeDefined();
});
});