|
| 1 | +import { describe, test, expect, beforeAll, beforeEach, afterAll } from 'bun:test'; |
| 2 | +import { db, initializeDatabase } from './db'; |
| 3 | +import { backfillOperations } from './schema/index'; |
| 4 | +import { backfillManager } from './backfillManager'; |
| 5 | + |
| 6 | +const USER_A = 'discord-backfill-a'; |
| 7 | +const USER_B = 'discord-backfill-b'; |
| 8 | + |
| 9 | +beforeAll(() => { |
| 10 | + initializeDatabase(); |
| 11 | +}); |
| 12 | + |
| 13 | +beforeEach(() => { |
| 14 | + db.delete(backfillOperations).run(); |
| 15 | + // Reset the in-memory lock by force if any test left it set. |
| 16 | + // We do this by exploiting the fact that updateBackfill resets backfillInProgress. |
| 17 | + // If the flag is stuck, insert a dummy row and complete it. |
| 18 | + if (backfillManager.isBackfillInProgress()) { |
| 19 | + const row = db |
| 20 | + .insert(backfillOperations) |
| 21 | + .values({ initiatedBy: '__reset__', status: 'in_progress' }) |
| 22 | + .returning({ id: backfillOperations.id }) |
| 23 | + .get(); |
| 24 | + if (row) { |
| 25 | + backfillManager.updateBackfill(row.id, 0, 0, 'completed'); |
| 26 | + } |
| 27 | + db.delete(backfillOperations).run(); |
| 28 | + } |
| 29 | +}); |
| 30 | + |
| 31 | +afterAll(() => { |
| 32 | + db.delete(backfillOperations).run(); |
| 33 | +}); |
| 34 | + |
| 35 | +// --------------------------------------------------------------------------- |
| 36 | +// isBackfillInProgress |
| 37 | +// --------------------------------------------------------------------------- |
| 38 | +describe('isBackfillInProgress', () => { |
| 39 | + test('returns false when no backfill is running', () => { |
| 40 | + expect(backfillManager.isBackfillInProgress()).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + test('returns true after startBackfill is called', async () => { |
| 44 | + await backfillManager.startBackfill(USER_A); |
| 45 | + expect(backfillManager.isBackfillInProgress()).toBe(true); |
| 46 | + // Clean up the flag |
| 47 | + const rows = db.select().from(backfillOperations).all(); |
| 48 | + await backfillManager.updateBackfill(rows[0]!.id, 0, 0, 'completed'); |
| 49 | + }); |
| 50 | + |
| 51 | + test('returns false after updateBackfill completes', async () => { |
| 52 | + const id = await backfillManager.startBackfill(USER_A); |
| 53 | + await backfillManager.updateBackfill(id, 5, 3, 'completed'); |
| 54 | + expect(backfillManager.isBackfillInProgress()).toBe(false); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +// --------------------------------------------------------------------------- |
| 59 | +// startBackfill |
| 60 | +// --------------------------------------------------------------------------- |
| 61 | +describe('startBackfill', () => { |
| 62 | + test('inserts an in_progress row and returns its id', async () => { |
| 63 | + const id = await backfillManager.startBackfill(USER_A); |
| 64 | + expect(id).toBeGreaterThan(0); |
| 65 | + const rows = db.select().from(backfillOperations).all(); |
| 66 | + expect(rows).toHaveLength(1); |
| 67 | + expect(rows[0]!.initiatedBy).toBe(USER_A); |
| 68 | + expect(rows[0]!.status).toBe('in_progress'); |
| 69 | + await backfillManager.updateBackfill(id, 0, 0, 'completed'); |
| 70 | + }); |
| 71 | + |
| 72 | + test('throws when a backfill is already in progress', async () => { |
| 73 | + const id = await backfillManager.startBackfill(USER_A); |
| 74 | + await expect(backfillManager.startBackfill(USER_B)).rejects.toThrow( |
| 75 | + 'A backfill operation is already in progress' |
| 76 | + ); |
| 77 | + await backfillManager.updateBackfill(id, 0, 0, 'completed'); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +// --------------------------------------------------------------------------- |
| 82 | +// updateBackfill |
| 83 | +// --------------------------------------------------------------------------- |
| 84 | +describe('updateBackfill', () => { |
| 85 | + test('updates row with codesFound and codesRedeemed', async () => { |
| 86 | + const id = await backfillManager.startBackfill(USER_A); |
| 87 | + await backfillManager.updateBackfill(id, 10, 7, 'completed'); |
| 88 | + const row = db.select().from(backfillOperations).get(); |
| 89 | + expect(row?.codesFound).toBe(10); |
| 90 | + expect(row?.codesRedeemed).toBe(7); |
| 91 | + expect(row?.status).toBe('completed'); |
| 92 | + }); |
| 93 | + |
| 94 | + test('marks status as failed', async () => { |
| 95 | + const id = await backfillManager.startBackfill(USER_A); |
| 96 | + await backfillManager.updateBackfill(id, 0, 0, 'failed'); |
| 97 | + const row = db.select().from(backfillOperations).get(); |
| 98 | + expect(row?.status).toBe('failed'); |
| 99 | + }); |
| 100 | + |
| 101 | + test('resets backfillInProgress flag', async () => { |
| 102 | + const id = await backfillManager.startBackfill(USER_A); |
| 103 | + expect(backfillManager.isBackfillInProgress()).toBe(true); |
| 104 | + await backfillManager.updateBackfill(id, 0, 0, 'completed'); |
| 105 | + expect(backfillManager.isBackfillInProgress()).toBe(false); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +// --------------------------------------------------------------------------- |
| 110 | +// getBackfillById |
| 111 | +// --------------------------------------------------------------------------- |
| 112 | +describe('getBackfillById', () => { |
| 113 | + test('returns undefined for non-existent id', async () => { |
| 114 | + const result = await backfillManager.getBackfillById(9999); |
| 115 | + expect(result).toBeUndefined(); |
| 116 | + }); |
| 117 | + |
| 118 | + test('returns the row with the matching id', async () => { |
| 119 | + const id = await backfillManager.startBackfill(USER_A); |
| 120 | + await backfillManager.updateBackfill(id, 3, 2, 'completed'); |
| 121 | + const row = await backfillManager.getBackfillById(id); |
| 122 | + expect(row).toBeDefined(); |
| 123 | + expect(row!.id).toBe(id); |
| 124 | + expect(row!.initiatedBy).toBe(USER_A); |
| 125 | + expect(row!.codesFound).toBe(3); |
| 126 | + }); |
| 127 | +}); |
| 128 | + |
| 129 | +// --------------------------------------------------------------------------- |
| 130 | +// getLastBackfill |
| 131 | +// --------------------------------------------------------------------------- |
| 132 | +describe('getLastBackfill', () => { |
| 133 | + test('returns undefined when no completed backfills exist', async () => { |
| 134 | + expect(await backfillManager.getLastBackfill()).toBeUndefined(); |
| 135 | + }); |
| 136 | + |
| 137 | + test('returns undefined when only in_progress operations exist', async () => { |
| 138 | + const id = await backfillManager.startBackfill(USER_A); |
| 139 | + expect(await backfillManager.getLastBackfill()).toBeUndefined(); |
| 140 | + await backfillManager.updateBackfill(id, 0, 0, 'completed'); |
| 141 | + }); |
| 142 | + |
| 143 | + test('returns a completed backfill when one exists', async () => { |
| 144 | + const id = await backfillManager.startBackfill(USER_A); |
| 145 | + await backfillManager.updateBackfill(id, 5, 3, 'completed'); |
| 146 | + const last = await backfillManager.getLastBackfill(); |
| 147 | + expect(last).toBeDefined(); |
| 148 | + expect(last!.status).toBe('completed'); |
| 149 | + expect(last!.codesFound).toBe(5); |
| 150 | + }); |
| 151 | +}); |
| 152 | + |
| 153 | +// --------------------------------------------------------------------------- |
| 154 | +// canUserInitiateBackfill |
| 155 | +// --------------------------------------------------------------------------- |
| 156 | +describe('canUserInitiateBackfill', () => { |
| 157 | + test('returns true when user has no previous backfills', async () => { |
| 158 | + expect(await backfillManager.canUserInitiateBackfill(USER_A)).toBe(true); |
| 159 | + }); |
| 160 | + |
| 161 | + test('returns false immediately after a completed backfill (within 1-hour window)', async () => { |
| 162 | + const id = await backfillManager.startBackfill(USER_A); |
| 163 | + await backfillManager.updateBackfill(id, 0, 0, 'completed'); |
| 164 | + // completedAt is CURRENT_TIMESTAMP — definitely within 1 hour |
| 165 | + expect(await backfillManager.canUserInitiateBackfill(USER_A)).toBe(false); |
| 166 | + }); |
| 167 | + |
| 168 | + test('returns true for a different user even if USER_A is within cooldown', async () => { |
| 169 | + const id = await backfillManager.startBackfill(USER_A); |
| 170 | + await backfillManager.updateBackfill(id, 0, 0, 'completed'); |
| 171 | + expect(await backfillManager.canUserInitiateBackfill(USER_B)).toBe(true); |
| 172 | + }); |
| 173 | + |
| 174 | + test('returns true when user only has in_progress or failed operations', async () => { |
| 175 | + const id = await backfillManager.startBackfill(USER_A); |
| 176 | + await backfillManager.updateBackfill(id, 0, 0, 'failed'); |
| 177 | + expect(await backfillManager.canUserInitiateBackfill(USER_A)).toBe(true); |
| 178 | + }); |
| 179 | +}); |
| 180 | + |
| 181 | +// --------------------------------------------------------------------------- |
| 182 | +// shouldRunStartupBackfill |
| 183 | +// --------------------------------------------------------------------------- |
| 184 | +describe('shouldRunStartupBackfill', () => { |
| 185 | + test('returns true when no backfills have ever run', async () => { |
| 186 | + expect(await backfillManager.shouldRunStartupBackfill()).toBe(true); |
| 187 | + }); |
| 188 | + |
| 189 | + test('returns false immediately after a recent completed backfill (within 6-hour window)', async () => { |
| 190 | + const id = await backfillManager.startBackfill(USER_A); |
| 191 | + await backfillManager.updateBackfill(id, 0, 0, 'completed'); |
| 192 | + expect(await backfillManager.shouldRunStartupBackfill()).toBe(false); |
| 193 | + }); |
| 194 | +}); |
| 195 | + |
| 196 | +// --------------------------------------------------------------------------- |
| 197 | +// hasUserBackfillOperations |
| 198 | +// --------------------------------------------------------------------------- |
| 199 | +describe('hasUserBackfillOperations', () => { |
| 200 | + test('returns false when user has no backfill operations', async () => { |
| 201 | + expect(await backfillManager.hasUserBackfillOperations(USER_A)).toBe(false); |
| 202 | + }); |
| 203 | + |
| 204 | + test('returns true when user has at least one operation', async () => { |
| 205 | + const id = await backfillManager.startBackfill(USER_A); |
| 206 | + expect(await backfillManager.hasUserBackfillOperations(USER_A)).toBe(true); |
| 207 | + await backfillManager.updateBackfill(id, 0, 0, 'completed'); |
| 208 | + }); |
| 209 | + |
| 210 | + test('returns false for a different user', async () => { |
| 211 | + const id = await backfillManager.startBackfill(USER_A); |
| 212 | + expect(await backfillManager.hasUserBackfillOperations(USER_B)).toBe(false); |
| 213 | + await backfillManager.updateBackfill(id, 0, 0, 'completed'); |
| 214 | + }); |
| 215 | +}); |
| 216 | + |
| 217 | +// --------------------------------------------------------------------------- |
| 218 | +// hasUserActiveBackfill |
| 219 | +// --------------------------------------------------------------------------- |
| 220 | +describe('hasUserActiveBackfill', () => { |
| 221 | + test('returns false when no active backfill exists for user', async () => { |
| 222 | + expect(await backfillManager.hasUserActiveBackfill(USER_A)).toBe(false); |
| 223 | + }); |
| 224 | + |
| 225 | + test('returns true when user has an in_progress backfill', async () => { |
| 226 | + const id = await backfillManager.startBackfill(USER_A); |
| 227 | + expect(await backfillManager.hasUserActiveBackfill(USER_A)).toBe(true); |
| 228 | + await backfillManager.updateBackfill(id, 0, 0, 'completed'); |
| 229 | + }); |
| 230 | + |
| 231 | + test('returns false after the backfill completes', async () => { |
| 232 | + const id = await backfillManager.startBackfill(USER_A); |
| 233 | + await backfillManager.updateBackfill(id, 0, 0, 'completed'); |
| 234 | + expect(await backfillManager.hasUserActiveBackfill(USER_A)).toBe(false); |
| 235 | + }); |
| 236 | +}); |
| 237 | + |
| 238 | +// --------------------------------------------------------------------------- |
| 239 | +// deleteUserBackfillOperations |
| 240 | +// --------------------------------------------------------------------------- |
| 241 | +describe('deleteUserBackfillOperations', () => { |
| 242 | + test('returns 0 when user has no operations', async () => { |
| 243 | + const count = await backfillManager.deleteUserBackfillOperations(USER_A); |
| 244 | + expect(count).toBe(0); |
| 245 | + }); |
| 246 | + |
| 247 | + test('deletes all operations for the user and returns the count', async () => { |
| 248 | + const id1 = await backfillManager.startBackfill(USER_A); |
| 249 | + await backfillManager.updateBackfill(id1, 1, 1, 'completed'); |
| 250 | + const id2 = await backfillManager.startBackfill(USER_A); |
| 251 | + await backfillManager.updateBackfill(id2, 2, 2, 'completed'); |
| 252 | + const count = await backfillManager.deleteUserBackfillOperations(USER_A); |
| 253 | + expect(count).toBe(2); |
| 254 | + expect(db.select().from(backfillOperations).all()).toHaveLength(0); |
| 255 | + }); |
| 256 | + |
| 257 | + test('does not delete operations belonging to another user', async () => { |
| 258 | + const idA = await backfillManager.startBackfill(USER_A); |
| 259 | + await backfillManager.updateBackfill(idA, 0, 0, 'completed'); |
| 260 | + const idB = await backfillManager.startBackfill(USER_B); |
| 261 | + await backfillManager.updateBackfill(idB, 0, 0, 'completed'); |
| 262 | + await backfillManager.deleteUserBackfillOperations(USER_A); |
| 263 | + const remaining = db.select().from(backfillOperations).all(); |
| 264 | + expect(remaining).toHaveLength(1); |
| 265 | + expect(remaining[0]!.initiatedBy).toBe(USER_B); |
| 266 | + }); |
| 267 | +}); |
0 commit comments