-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbilling.usage.unit.tests.js
More file actions
149 lines (117 loc) · 4.86 KB
/
Copy pathbilling.usage.unit.tests.js
File metadata and controls
149 lines (117 loc) · 4.86 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
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
141
142
143
144
145
146
147
148
149
/**
* Module dependencies.
*/
import { jest, describe, test, beforeEach, afterEach, expect } from '@jest/globals';
import schema from '../models/billing.usage.schema.js';
/**
* Unit tests
*/
describe('BillingUsage unit tests:', () => {
describe('Schema validation', () => {
let usage;
beforeEach(() => {
usage = {
organizationId: '507f1f77bcf86cd799439011',
month: '2026-03',
counters: { executions: 10 },
};
});
test('should be valid with correct data', () => {
const result = schema.BillingUsage.safeParse(usage);
expect(result.error).toBeFalsy();
expect(result.data.organizationId).toBe('507f1f77bcf86cd799439011');
expect(result.data.month).toBe('2026-03');
expect(result.data.counters.executions).toBe(10);
});
test('should show error when organizationId is missing', () => {
usage.organizationId = '';
const result = schema.BillingUsage.safeParse(usage);
expect(result.error).toBeDefined();
});
test('should show error when organizationId is invalid', () => {
usage.organizationId = 'not-valid';
const result = schema.BillingUsage.safeParse(usage);
expect(result.error).toBeDefined();
});
test('should show error when month format is invalid', () => {
usage.month = '2026/03';
const result = schema.BillingUsage.safeParse(usage);
expect(result.error).toBeDefined();
});
test('should show error when month is missing', () => {
delete usage.month;
const result = schema.BillingUsage.safeParse(usage);
expect(result.error).toBeDefined();
});
test('should reject semantically invalid month 2026-00', () => {
usage.month = '2026-00';
const result = schema.BillingUsage.safeParse(usage);
expect(result.error).toBeDefined();
});
test('should reject semantically invalid month 2026-13', () => {
usage.month = '2026-13';
const result = schema.BillingUsage.safeParse(usage);
expect(result.error).toBeDefined();
});
test('should default counters to empty object', () => {
delete usage.counters;
const result = schema.BillingUsage.safeParse(usage);
expect(result.error).toBeFalsy();
expect(result.data.counters).toEqual({});
});
});
describe('Service layer', () => {
let BillingUsageService;
let mockUsageRepository;
const orgId = '507f1f77bcf86cd799439011';
beforeEach(async () => {
jest.resetModules();
mockUsageRepository = {
get: jest.fn(),
increment: jest.fn(),
reset: jest.fn(),
};
jest.unstable_mockModule('../repositories/billing.usage.repository.js', () => ({
default: mockUsageRepository,
}));
const mod = await import('../services/billing.usage.service.js');
BillingUsageService = mod.default;
});
afterEach(() => {
jest.restoreAllMocks();
});
test('increment should create document on first call (upsert)', async () => {
const created = { organizationId: orgId, month: '2026-03', counters: { executions: 1 } };
mockUsageRepository.increment.mockResolvedValue(created);
const result = await BillingUsageService.increment(orgId, 'executions', 1);
expect(mockUsageRepository.increment).toHaveBeenCalledWith(orgId, expect.stringMatching(/^\d{4}-\d{2}$/), 'executions', 1);
expect(result.counters.executions).toBe(1);
});
test('increment should atomically increase counter', async () => {
const updated = { organizationId: orgId, month: '2026-03', counters: { executions: 5 } };
mockUsageRepository.increment.mockResolvedValue(updated);
const result = await BillingUsageService.increment(orgId, 'executions', 3);
expect(result.counters.executions).toBe(5);
});
test('get should return empty counters for new org/month', async () => {
mockUsageRepository.get.mockResolvedValue(null);
const result = await BillingUsageService.get(orgId);
expect(result.counters).toEqual({});
expect(result.organizationId).toBe(orgId);
});
test('get should return existing usage document', async () => {
const existing = { organizationId: orgId, month: '2026-03', counters: { executions: 42, aiCalls: 5 } };
mockUsageRepository.get.mockResolvedValue(existing);
const result = await BillingUsageService.get(orgId);
expect(result.counters.executions).toBe(42);
expect(result.counters.aiCalls).toBe(5);
});
test('reset should clear counters', async () => {
const resetDoc = { organizationId: orgId, month: '2026-03', counters: {} };
mockUsageRepository.reset.mockResolvedValue(resetDoc);
const result = await BillingUsageService.reset(orgId);
expect(mockUsageRepository.reset).toHaveBeenCalledWith(orgId, expect.stringMatching(/^\d{4}-\d{2}$/));
expect(result.counters).toEqual({});
});
});
});