|
| 1 | +import { describe, test, expect, beforeAll, beforeEach, spyOn } from 'bun:test'; |
| 2 | +import { MessageFlags } from 'discord.js'; |
| 3 | +import { db, initializeDatabase } from '../database/db'; |
| 4 | +import { users, redeemedCodes, pendingCodes, auditLog } from '../database/schema/index'; |
| 5 | +import { userManager } from '../database/userManager'; |
| 6 | +import { execute } from './notifications'; |
| 7 | + |
| 8 | +// --------------------------------------------------------------------------- |
| 9 | +// Interaction mock helpers |
| 10 | +// --------------------------------------------------------------------------- |
| 11 | + |
| 12 | +function makeInteraction( |
| 13 | + userId: string, |
| 14 | + options: Record<string, boolean | null> = {} |
| 15 | +) { |
| 16 | + const editReplySpy = spyOn({ editReply: async (_: unknown) => {} }, 'editReply'); |
| 17 | + const replySpy = spyOn({ reply: async (_: unknown) => {} }, 'reply'); |
| 18 | + |
| 19 | + const interaction = { |
| 20 | + user: { id: userId, tag: `user#${userId}` }, |
| 21 | + deferred: false, |
| 22 | + replied: false, |
| 23 | + deferReply: async () => { (interaction as any).deferred = true; }, |
| 24 | + editReply: editReplySpy, |
| 25 | + reply: replySpy, |
| 26 | + options: { |
| 27 | + getBoolean: (name: string) => options[name] ?? null, |
| 28 | + }, |
| 29 | + } as any; |
| 30 | + |
| 31 | + return { interaction, editReplySpy, replySpy }; |
| 32 | +} |
| 33 | + |
| 34 | +// --------------------------------------------------------------------------- |
| 35 | +// Setup |
| 36 | +// --------------------------------------------------------------------------- |
| 37 | + |
| 38 | +beforeAll(() => { |
| 39 | + initializeDatabase(); |
| 40 | +}); |
| 41 | + |
| 42 | +beforeEach(() => { |
| 43 | + db.delete(auditLog).run(); |
| 44 | + db.delete(pendingCodes).run(); |
| 45 | + db.delete(redeemedCodes).run(); |
| 46 | + db.delete(users).run(); |
| 47 | +}); |
| 48 | + |
| 49 | +// --------------------------------------------------------------------------- |
| 50 | +// No credentials |
| 51 | +// --------------------------------------------------------------------------- |
| 52 | + |
| 53 | +describe('/notifications – no credentials', () => { |
| 54 | + test('replies with error embed when user has no credentials', async () => { |
| 55 | + const { interaction, editReplySpy } = makeInteraction('unknown-user'); |
| 56 | + |
| 57 | + await execute(interaction); |
| 58 | + |
| 59 | + expect(editReplySpy).toHaveBeenCalledTimes(1); |
| 60 | + const reply = editReplySpy.mock.calls[0]![0] as any; |
| 61 | + expect(reply.embeds[0].data.title).toContain('No Credentials Found'); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +// --------------------------------------------------------------------------- |
| 66 | +// Show current settings (no options) |
| 67 | +// --------------------------------------------------------------------------- |
| 68 | + |
| 69 | +describe('/notifications – show current settings', () => { |
| 70 | + test('shows defaults when no options are provided', async () => { |
| 71 | + await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' }); |
| 72 | + const { interaction, editReplySpy } = makeInteraction('user-1'); |
| 73 | + |
| 74 | + await execute(interaction); |
| 75 | + |
| 76 | + expect(editReplySpy).toHaveBeenCalledTimes(1); |
| 77 | + const reply = editReplySpy.mock.calls[0]![0] as any; |
| 78 | + const embed = reply.embeds[0].data; |
| 79 | + expect(embed.title).toContain('Notification Preferences'); |
| 80 | + const fieldValues = embed.fields.map((f: any) => f.value); |
| 81 | + // dmOnCode default false, dmOnSuccess default true, dmOnFailure default false |
| 82 | + expect(fieldValues[0]).toContain('Off'); |
| 83 | + expect(fieldValues[1]).toContain('On'); |
| 84 | + expect(fieldValues[2]).toContain('Off'); |
| 85 | + }); |
| 86 | + |
| 87 | + test('shows updated values after preferences have been changed', async () => { |
| 88 | + await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' }); |
| 89 | + await userManager.setNotificationPreferences('user-1', { |
| 90 | + dmOnCode: true, |
| 91 | + dmOnSuccess: false, |
| 92 | + dmOnFailure: true, |
| 93 | + }); |
| 94 | + const { interaction, editReplySpy } = makeInteraction('user-1'); |
| 95 | + |
| 96 | + await execute(interaction); |
| 97 | + |
| 98 | + const embed = (editReplySpy.mock.calls[0]![0] as any).embeds[0].data; |
| 99 | + const fieldValues = embed.fields.map((f: any) => f.value); |
| 100 | + expect(fieldValues[0]).toContain('On'); |
| 101 | + expect(fieldValues[1]).toContain('Off'); |
| 102 | + expect(fieldValues[2]).toContain('On'); |
| 103 | + }); |
| 104 | +}); |
| 105 | + |
| 106 | +// --------------------------------------------------------------------------- |
| 107 | +// Update preferences |
| 108 | +// --------------------------------------------------------------------------- |
| 109 | + |
| 110 | +describe('/notifications – update preferences', () => { |
| 111 | + test('enables dmOnCode and reflects it in the reply embed', async () => { |
| 112 | + await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' }); |
| 113 | + const { interaction, editReplySpy } = makeInteraction('user-1', { dm_on_code: true }); |
| 114 | + |
| 115 | + await execute(interaction); |
| 116 | + |
| 117 | + // Verify DB was updated |
| 118 | + const creds = await userManager.getCredentials('user-1'); |
| 119 | + expect(creds?.dmOnCode).toBe(true); |
| 120 | + |
| 121 | + // Verify reply embed shows correct state |
| 122 | + const embed = (editReplySpy.mock.calls[0]![0] as any).embeds[0].data; |
| 123 | + expect(embed.title).toContain('Updated'); |
| 124 | + const fieldValues = embed.fields.map((f: any) => f.value); |
| 125 | + expect(fieldValues[0]).toContain('On'); // dmOnCode |
| 126 | + expect(fieldValues[1]).toContain('On'); // dmOnSuccess unchanged (default true) |
| 127 | + expect(fieldValues[2]).toContain('Off'); // dmOnFailure unchanged (default false) |
| 128 | + }); |
| 129 | + |
| 130 | + test('disables dmOnSuccess', async () => { |
| 131 | + await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' }); |
| 132 | + const { interaction, editReplySpy } = makeInteraction('user-1', { dm_on_success: false }); |
| 133 | + |
| 134 | + await execute(interaction); |
| 135 | + |
| 136 | + const creds = await userManager.getCredentials('user-1'); |
| 137 | + expect(creds?.dmOnSuccess).toBe(false); |
| 138 | + |
| 139 | + const embed = (editReplySpy.mock.calls[0]![0] as any).embeds[0].data; |
| 140 | + const fieldValues = embed.fields.map((f: any) => f.value); |
| 141 | + expect(fieldValues[1]).toContain('Off'); |
| 142 | + }); |
| 143 | + |
| 144 | + test('enables dmOnFailure', async () => { |
| 145 | + await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' }); |
| 146 | + const { interaction, editReplySpy } = makeInteraction('user-1', { dm_on_failure: true }); |
| 147 | + |
| 148 | + await execute(interaction); |
| 149 | + |
| 150 | + const creds = await userManager.getCredentials('user-1'); |
| 151 | + expect(creds?.dmOnFailure).toBe(true); |
| 152 | + |
| 153 | + const embed = (editReplySpy.mock.calls[0]![0] as any).embeds[0].data; |
| 154 | + const fieldValues = embed.fields.map((f: any) => f.value); |
| 155 | + expect(fieldValues[2]).toContain('On'); |
| 156 | + }); |
| 157 | + |
| 158 | + test('updates all three prefs at once', async () => { |
| 159 | + await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' }); |
| 160 | + const { interaction } = makeInteraction('user-1', { |
| 161 | + dm_on_code: true, |
| 162 | + dm_on_success: false, |
| 163 | + dm_on_failure: true, |
| 164 | + }); |
| 165 | + |
| 166 | + await execute(interaction); |
| 167 | + |
| 168 | + const creds = await userManager.getCredentials('user-1'); |
| 169 | + expect(creds?.dmOnCode).toBe(true); |
| 170 | + expect(creds?.dmOnSuccess).toBe(false); |
| 171 | + expect(creds?.dmOnFailure).toBe(true); |
| 172 | + }); |
| 173 | +}); |
| 174 | + |
| 175 | +// --------------------------------------------------------------------------- |
| 176 | +// Error path |
| 177 | +// --------------------------------------------------------------------------- |
| 178 | + |
| 179 | +describe('/notifications – error handling', () => { |
| 180 | + test('uses editReply when already deferred on error', async () => { |
| 181 | + await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' }); |
| 182 | + |
| 183 | + const { interaction, editReplySpy } = makeInteraction('user-1', { dm_on_code: true }); |
| 184 | + // Force an error after deferReply by making setNotificationPreferences throw |
| 185 | + const spy = spyOn(userManager, 'setNotificationPreferences').mockRejectedValueOnce( |
| 186 | + new Error('DB error') |
| 187 | + ); |
| 188 | + |
| 189 | + await execute(interaction); |
| 190 | + |
| 191 | + expect(editReplySpy).toHaveBeenCalled(); |
| 192 | + const reply = editReplySpy.mock.calls[0]![0] as any; |
| 193 | + expect(reply.content).toContain('error'); |
| 194 | + |
| 195 | + spy.mockRestore(); |
| 196 | + }); |
| 197 | + |
| 198 | + test('uses reply when not deferred on error', async () => { |
| 199 | + const { interaction, editReplySpy, replySpy } = makeInteraction('user-1', { dm_on_code: true }); |
| 200 | + |
| 201 | + // deferReply throws so deferred stays false, getCredentials is never called |
| 202 | + (interaction as any).deferReply = async () => { |
| 203 | + throw new Error('interaction expired'); |
| 204 | + }; |
| 205 | + |
| 206 | + await execute(interaction); |
| 207 | + |
| 208 | + expect(editReplySpy).not.toHaveBeenCalled(); |
| 209 | + expect(replySpy).toHaveBeenCalled(); |
| 210 | + const reply = replySpy.mock.calls[0]![0] as any; |
| 211 | + expect(reply.content).toContain('error'); |
| 212 | + expect(reply.flags).toBe(MessageFlags.Ephemeral); |
| 213 | + }); |
| 214 | +}); |
0 commit comments