Skip to content

Commit f61ebd2

Browse files
committed
test: cover parseLootEntries catch block for malformed JSON
Add a backfillLootTotals test that inserts a row with invalid JSON in lootDetail so the JSON.parse catch branch in parseLootEntries is exercised, restoring 100% line coverage on codeManager.ts. Signed-off-by: Michael Cramer <michael@bigmichi1.de>
1 parent c9e84eb commit f61ebd2

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

src/bot/database/codeManager.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,4 +794,15 @@ describe('backfillLootTotals', () => {
794794
expect(loot.chests).toEqual({});
795795
expect(loot.items).toEqual({});
796796
});
797+
798+
test('skips rows with malformed JSON loot detail', async () => {
799+
db.insert(redeemedCodes)
800+
.values({ code: 'CODE1111AAAA', discordId: USER_A, status: 'Success', lootDetail: 'not-valid-json{' })
801+
.run();
802+
await codeManager.backfillLootTotals();
803+
const loot = await codeManager.getAggregateLoot(USER_A);
804+
expect(loot.chests).toEqual({});
805+
expect(loot.items).toEqual({});
806+
});
797807
});
808+

src/bot/database/codeManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class CodeManager {
7171
isPublic: boolean = false
7272
): Promise<void> {
7373
const normalizedStatus = normalizeCodeStatus(status);
74-
const lootStr = lootDetail !== null && lootDetail !== undefined ? JSON.stringify(lootDetail) : null;
74+
const lootStr = lootDetail !== undefined ? JSON.stringify(lootDetail) : null;
7575
db.insert(redeemedCodes)
7676
.values({
7777
code,

0 commit comments

Comments
 (0)