forked from crackedstudio/tikka
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.controller.spec.ts
More file actions
386 lines (316 loc) · 11.5 KB
/
Copy pathstats.controller.spec.ts
File metadata and controls
386 lines (316 loc) · 11.5 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import { Test, TestingModule } from '@nestjs/testing';
import { StatsController } from './stats.controller';
import { StatsService, TransparencyStats, VerifyResult } from './stats.service';
import {
IndexerPlatformStats,
IndexerTransparencyEntry,
} from '../../../services/indexer/indexer.service';
describe('StatsController', () => {
let controller: StatsController;
let service: jest.Mocked<StatsService>;
beforeEach(async () => {
const mockService = {
getPlatformStats: jest.fn(),
getTransparencyStats: jest.fn(),
verifyDraw: jest.fn(),
} as any;
const module: TestingModule = await Test.createTestingModule({
controllers: [StatsController],
providers: [{ provide: StatsService, useValue: mockService }],
}).compile();
controller = module.get<StatsController>(StatsController);
service = module.get<StatsService>(StatsService) as jest.Mocked<StatsService>;
});
afterEach(() => jest.clearAllMocks());
describe('GET /stats/platform', () => {
it('should return platform aggregates', async () => {
const mockStats: IndexerPlatformStats = {
date: '2026-06-27',
total_raffles: 100,
total_tickets: 5000,
total_volume_xlm: '250000000000',
unique_participants: 1000,
prizes_distributed_xlm: '50000000000',
};
service.getPlatformStats.mockResolvedValue(mockStats);
const result = await controller.getPlatformStats();
expect(result).toEqual(mockStats);
expect(service.getPlatformStats).toHaveBeenCalled();
});
it('should return correct field types', async () => {
const mockStats: IndexerPlatformStats = {
date: '2026-06-27',
total_raffles: 150,
total_tickets: 7500,
total_volume_xlm: '375000000000',
unique_participants: 1500,
prizes_distributed_xlm: '75000000000',
};
service.getPlatformStats.mockResolvedValue(mockStats);
const result = await controller.getPlatformStats();
expect(typeof result.total_raffles).toBe('number');
expect(typeof result.total_tickets).toBe('number');
expect(typeof result.total_volume_xlm).toBe('string');
expect(typeof result.unique_participants).toBe('number');
expect(typeof result.prizes_distributed_xlm).toBe('string');
});
});
describe('GET /stats/transparency', () => {
it('should return transparency stats with oracle key and audit log', async () => {
const mockEntry: IndexerTransparencyEntry = {
id: 'entry-1',
timestamp: '2026-06-27T10:30:00Z',
raffle_id: 10,
request_id: 'req-123',
oracle_id: 'oracle-001',
seed: 'seed-hex',
proof: 'proof-hex',
tx_hash: 'tx-hash',
method: 'VRF',
};
const mockStats: TransparencyStats = {
date: '2026-06-27',
total_raffles: 100,
total_tickets: 5000,
total_volume_xlm: '250000000000',
unique_participants: 1000,
prizes_distributed_xlm: '50000000000',
oracle_public_key: '0xabcd1234',
draws_completed: 25,
recent_audit_log: [mockEntry],
};
service.getTransparencyStats.mockResolvedValue(mockStats);
const result = await controller.getTransparencyStats();
expect(result).toHaveProperty('oracle_public_key', '0xabcd1234');
expect(result).toHaveProperty('draws_completed', 25);
expect(result.recent_audit_log).toHaveLength(1);
expect(result.recent_audit_log[0].raffle_id).toBe(10);
});
it('should return 200 status code', async () => {
const mockStats: TransparencyStats = {
date: '2026-06-27',
total_raffles: 50,
total_tickets: 2500,
total_volume_xlm: '125000000000',
unique_participants: 500,
prizes_distributed_xlm: '25000000000',
oracle_public_key: '0xtest',
draws_completed: 10,
recent_audit_log: [],
};
service.getTransparencyStats.mockResolvedValue(mockStats);
// NestJS controller methods implicitly return 200 on success
const result = await controller.getTransparencyStats();
expect(result).toBeDefined();
});
it('should include platform stats fields', async () => {
const mockStats: TransparencyStats = {
date: '2026-06-27',
total_raffles: 75,
total_tickets: 3750,
total_volume_xlm: '187500000000',
unique_participants: 750,
prizes_distributed_xlm: '37500000000',
oracle_public_key: '0xkey',
draws_completed: 15,
recent_audit_log: [],
};
service.getTransparencyStats.mockResolvedValue(mockStats);
const result = await controller.getTransparencyStats();
expect(result).toHaveProperty('total_raffles', 75);
expect(result).toHaveProperty('total_tickets', 3750);
expect(result).toHaveProperty('total_volume_xlm', '187500000000');
expect(result).toHaveProperty('unique_participants', 750);
expect(result).toHaveProperty('prizes_distributed_xlm', '37500000000');
});
it('should include recent audit log entries', async () => {
const mockEntries: IndexerTransparencyEntry[] = [
{
id: 'entry-1',
timestamp: '2026-06-27T10:30:00Z',
raffle_id: 1,
request_id: 'req-1',
oracle_id: 'oracle-001',
seed: 'seed-1',
proof: 'proof-1',
tx_hash: 'tx-1',
method: 'VRF',
},
{
id: 'entry-2',
timestamp: '2026-06-27T10:29:00Z',
raffle_id: 2,
request_id: 'req-2',
oracle_id: 'oracle-001',
seed: 'seed-2',
proof: 'proof-2',
tx_hash: 'tx-2',
method: 'PRNG',
},
];
const mockStats: TransparencyStats = {
date: '2026-06-27',
total_raffles: 100,
total_tickets: 5000,
total_volume_xlm: '250000000000',
unique_participants: 1000,
prizes_distributed_xlm: '50000000000',
oracle_public_key: '0xkey',
draws_completed: 2,
recent_audit_log: mockEntries,
};
service.getTransparencyStats.mockResolvedValue(mockStats);
const result = await controller.getTransparencyStats();
expect(result.recent_audit_log).toHaveLength(2);
expect(result.recent_audit_log[0].raffle_id).toBe(1);
expect(result.recent_audit_log[1].method).toBe('PRNG');
});
it('should return empty audit log when none available', async () => {
const mockStats: TransparencyStats = {
date: '2026-06-27',
total_raffles: 10,
total_tickets: 500,
total_volume_xlm: '25000000000',
unique_participants: 100,
prizes_distributed_xlm: '5000000000',
oracle_public_key: '0xkey',
draws_completed: 0,
recent_audit_log: [],
};
service.getTransparencyStats.mockResolvedValue(mockStats);
const result = await controller.getTransparencyStats();
expect(result.recent_audit_log).toEqual([]);
expect(result.draws_completed).toBe(0);
});
});
describe('POST /stats/verify', () => {
const testPayload = {
oracle_public_key: '0xabcd1234',
request_id: 'req-123',
proof: 'proof-hex-456',
seed: 'seed-hex-789',
};
it('should return verified: true for valid proof', async () => {
const mockResult: VerifyResult = { valid: true };
service.verifyDraw.mockResolvedValue(mockResult);
const result = await controller.verifyDraw(
testPayload.oracle_public_key,
testPayload.request_id,
testPayload.proof,
testPayload.seed,
);
expect(result.valid).toBe(true);
expect(result.reason).toBeUndefined();
});
it('should return verified: false for invalid proof', async () => {
const mockResult: VerifyResult = {
valid: false,
reason: 'Invalid proof signature',
};
service.verifyDraw.mockResolvedValue(mockResult);
const result = await controller.verifyDraw(
testPayload.oracle_public_key,
testPayload.request_id,
'invalid-proof',
testPayload.seed,
);
expect(result.valid).toBe(false);
expect(result.reason).toBe('Invalid proof signature');
});
it('should return 200 status code for valid input', async () => {
const mockResult: VerifyResult = { valid: true };
service.verifyDraw.mockResolvedValue(mockResult);
// NestJS controller with @HttpCode(200) returns 200
const result = await controller.verifyDraw(
testPayload.oracle_public_key,
testPayload.request_id,
testPayload.proof,
testPayload.seed,
);
expect(result).toBeDefined();
});
it('should return 200 status code for invalid input', async () => {
const mockResult: VerifyResult = {
valid: false,
reason: 'Invalid hex',
};
service.verifyDraw.mockResolvedValue(mockResult);
// Should still return 200, not 500
const result = await controller.verifyDraw(
'not-hex',
testPayload.request_id,
testPayload.proof,
testPayload.seed,
);
expect(result.valid).toBe(false);
});
it('should pass all 4 parameters to service', async () => {
service.verifyDraw.mockResolvedValue({ valid: false });
await controller.verifyDraw(
testPayload.oracle_public_key,
testPayload.request_id,
testPayload.proof,
testPayload.seed,
);
expect(service.verifyDraw).toHaveBeenCalledWith(
testPayload.oracle_public_key,
testPayload.request_id,
testPayload.proof,
testPayload.seed,
);
});
it('should handle seed/proof mismatch', async () => {
const mockResult: VerifyResult = {
valid: false,
reason: 'Seed does not match SHA-256(proof)',
};
service.verifyDraw.mockResolvedValue(mockResult);
const result = await controller.verifyDraw(
testPayload.oracle_public_key,
testPayload.request_id,
testPayload.proof,
'wrong-seed',
);
expect(result.valid).toBe(false);
expect(result.reason).toContain('Seed');
});
it('should include reason message on failure', async () => {
const mockResult: VerifyResult = {
valid: false,
reason: 'Invalid public key format',
};
service.verifyDraw.mockResolvedValue(mockResult);
const result = await controller.verifyDraw(
'malformed-key',
testPayload.request_id,
testPayload.proof,
testPayload.seed,
);
expect(result.reason).toBeDefined();
expect(typeof result.reason).toBe('string');
});
it('should accept hex-encoded strings', async () => {
service.verifyDraw.mockResolvedValue({ valid: false });
await controller.verifyDraw(
'0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789',
'req-hex-encoded',
'0x1234567890abcdef',
'0xfedcba0987654321',
);
expect(service.verifyDraw).toHaveBeenCalled();
});
it('should cache responses for repeated calls', async () => {
const mockResult: VerifyResult = { valid: true };
service.verifyDraw.mockResolvedValue(mockResult);
await controller.verifyDraw(
testPayload.oracle_public_key,
testPayload.request_id,
testPayload.proof,
testPayload.seed,
);
// In real scenario with caching, second call would use cache
// Service mock doesn't implement caching, but verifyDraw in service does
expect(service.verifyDraw).toHaveBeenCalledTimes(1);
});
});
});