|
| 1 | +import { describe, test, expect, beforeAll, beforeEach, spyOn } from 'bun:test'; |
| 2 | +import { db, initializeDatabase } from '../database/db'; |
| 3 | +import { users, redeemedCodes, pendingCodes, auditLog, lootTotals } from '../database/schema/index'; |
| 4 | +import { userManager } from '../database/userManager'; |
| 5 | +import { codeManager } from '../database/codeManager'; |
| 6 | +import { execute } from './stats'; |
| 7 | + |
| 8 | +// --------------------------------------------------------------------------- |
| 9 | +// Interaction mock helper |
| 10 | +// --------------------------------------------------------------------------- |
| 11 | + |
| 12 | +function makeInteraction(userId: string) { |
| 13 | + const editReplySpy = spyOn({ editReply: async (_: unknown) => {} }, 'editReply'); |
| 14 | + |
| 15 | + const interaction = { |
| 16 | + user: { id: userId, tag: `user#${userId}` }, |
| 17 | + deferred: false, |
| 18 | + deferReply: async () => { |
| 19 | + (interaction as any).deferred = true; |
| 20 | + }, |
| 21 | + editReply: editReplySpy, |
| 22 | + } as any; |
| 23 | + |
| 24 | + return { interaction, editReplySpy }; |
| 25 | +} |
| 26 | + |
| 27 | +// --------------------------------------------------------------------------- |
| 28 | +// Setup |
| 29 | +// --------------------------------------------------------------------------- |
| 30 | + |
| 31 | +beforeAll(() => { |
| 32 | + initializeDatabase(); |
| 33 | +}); |
| 34 | + |
| 35 | +beforeEach(() => { |
| 36 | + db.delete(auditLog).run(); |
| 37 | + db.delete(pendingCodes).run(); |
| 38 | + db.delete(redeemedCodes).run(); |
| 39 | + db.delete(lootTotals).run(); |
| 40 | + db.delete(users).run(); |
| 41 | +}); |
| 42 | + |
| 43 | +// --------------------------------------------------------------------------- |
| 44 | +// Happy path — empty database |
| 45 | +// --------------------------------------------------------------------------- |
| 46 | + |
| 47 | +describe('/stats – empty database', () => { |
| 48 | + test('defers reply and returns stats embed', async () => { |
| 49 | + const { interaction, editReplySpy } = makeInteraction('user-1'); |
| 50 | + |
| 51 | + await execute(interaction); |
| 52 | + |
| 53 | + expect(interaction.deferred).toBe(true); |
| 54 | + expect(editReplySpy).toHaveBeenCalledTimes(1); |
| 55 | + const reply = editReplySpy.mock.calls[0]![0] as any; |
| 56 | + const embed = reply.embeds[0].data; |
| 57 | + expect(embed.title).toContain('Server Stats'); |
| 58 | + }); |
| 59 | + |
| 60 | + test('shows zero codes and zero users when database is empty', async () => { |
| 61 | + const { interaction, editReplySpy } = makeInteraction('user-1'); |
| 62 | + |
| 63 | + await execute(interaction); |
| 64 | + |
| 65 | + const embed = (editReplySpy.mock.calls[0]![0] as any).embeds[0].data; |
| 66 | + const fields = embed.fields as Array<{ name: string; value: string }>; |
| 67 | + const codesField = fields.find(f => f.name.includes('Unique Codes')); |
| 68 | + const usersField = fields.find(f => f.name.includes('Registered Users')); |
| 69 | + expect(codesField?.value).toBe('0'); |
| 70 | + expect(usersField?.value).toBe('0'); |
| 71 | + }); |
| 72 | + |
| 73 | + test('shows "No loot data available" when no codes have been redeemed', async () => { |
| 74 | + const { interaction, editReplySpy } = makeInteraction('user-1'); |
| 75 | + |
| 76 | + await execute(interaction); |
| 77 | + |
| 78 | + const embed = (editReplySpy.mock.calls[0]![0] as any).embeds[0].data; |
| 79 | + const fields = embed.fields as Array<{ name: string; value: string }>; |
| 80 | + const lootField = fields.find(f => f.name.includes('Loot')); |
| 81 | + expect(lootField?.value).toBe('No loot data available'); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +// --------------------------------------------------------------------------- |
| 86 | +// Happy path — with data |
| 87 | +// --------------------------------------------------------------------------- |
| 88 | + |
| 89 | +describe('/stats – with redeemed codes and users', () => { |
| 90 | + beforeEach(async () => { |
| 91 | + await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'aaa' }); |
| 92 | + await userManager.saveCredentials({ discordId: 'user-2', userId: '222', userHash: 'bbb' }); |
| 93 | + await codeManager.addRedeemedCode('CODE-A', 'user-1', 'Success', [{ chest_type_id: 1, count: 2 }] as any); |
| 94 | + await codeManager.addRedeemedCode('CODE-B', 'user-2', 'Success', [{ chest_type_id: 2, count: 1 }] as any); |
| 95 | + await codeManager.addRedeemedCode('CODE-A', 'user-2', 'Success'); |
| 96 | + }); |
| 97 | + |
| 98 | + test('shows correct unique code count', async () => { |
| 99 | + const { interaction, editReplySpy } = makeInteraction('user-1'); |
| 100 | + |
| 101 | + await execute(interaction); |
| 102 | + |
| 103 | + const fields = (editReplySpy.mock.calls[0]![0] as any).embeds[0].data.fields as Array<{ name: string; value: string }>; |
| 104 | + const codesField = fields.find(f => f.name.includes('Unique Codes')); |
| 105 | + expect(codesField?.value).toBe('2'); |
| 106 | + }); |
| 107 | + |
| 108 | + test('shows correct total redemptions count', async () => { |
| 109 | + const { interaction, editReplySpy } = makeInteraction('user-1'); |
| 110 | + |
| 111 | + await execute(interaction); |
| 112 | + |
| 113 | + const fields = (editReplySpy.mock.calls[0]![0] as any).embeds[0].data.fields as Array<{ name: string; value: string }>; |
| 114 | + const redemptionsField = fields.find(f => f.name.includes('Total Redemptions')); |
| 115 | + expect(redemptionsField?.value).toBe('3'); |
| 116 | + }); |
| 117 | + |
| 118 | + test('shows correct registered user count', async () => { |
| 119 | + const { interaction, editReplySpy } = makeInteraction('user-1'); |
| 120 | + |
| 121 | + await execute(interaction); |
| 122 | + |
| 123 | + const fields = (editReplySpy.mock.calls[0]![0] as any).embeds[0].data.fields as Array<{ name: string; value: string }>; |
| 124 | + const usersField = fields.find(f => f.name.includes('Registered Users')); |
| 125 | + expect(usersField?.value).toBe('2'); |
| 126 | + }); |
| 127 | + |
| 128 | + test('aggregates loot across all users and codes', async () => { |
| 129 | + const { interaction, editReplySpy } = makeInteraction('user-1'); |
| 130 | + |
| 131 | + await execute(interaction); |
| 132 | + |
| 133 | + const fields = (editReplySpy.mock.calls[0]![0] as any).embeds[0].data.fields as Array<{ name: string; value: string }>; |
| 134 | + const lootField = fields.find(f => f.name.includes('Loot')); |
| 135 | + expect(lootField?.value).toContain('Silver Chest: 2'); |
| 136 | + expect(lootField?.value).toContain('Gold Chest: 1'); |
| 137 | + }); |
| 138 | + |
| 139 | + test('logs VIEWED_STATS to the audit log', async () => { |
| 140 | + const { interaction } = makeInteraction('user-1'); |
| 141 | + |
| 142 | + await execute(interaction); |
| 143 | + |
| 144 | + const rows = db.select().from(auditLog).all(); |
| 145 | + const statsRow = rows.find(r => r.action === 'VIEWED_STATS'); |
| 146 | + expect(statsRow).toBeDefined(); |
| 147 | + // discordId is null because /stats is public (no user registration required) |
| 148 | + expect(statsRow?.discordId).toBeNull(); |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
| 152 | +// --------------------------------------------------------------------------- |
| 153 | +// Error handling |
| 154 | +// --------------------------------------------------------------------------- |
| 155 | + |
| 156 | +describe('/stats – error handling', () => { |
| 157 | + test('replies with error embed when an exception is thrown', async () => { |
| 158 | + const editReplySpy = spyOn({ editReply: async (_: unknown) => {} }, 'editReply'); |
| 159 | + const interaction = { |
| 160 | + user: { id: 'user-1', tag: 'user#1' }, |
| 161 | + deferred: false, |
| 162 | + deferReply: async () => { throw new Error('network failure'); }, |
| 163 | + editReply: editReplySpy, |
| 164 | + } as any; |
| 165 | + |
| 166 | + await execute(interaction); |
| 167 | + |
| 168 | + expect(editReplySpy).toHaveBeenCalledTimes(1); |
| 169 | + const embed = (editReplySpy.mock.calls[0]![0] as any).embeds[0].data; |
| 170 | + expect(embed.title).toContain('Error'); |
| 171 | + }); |
| 172 | +}); |
0 commit comments