From b0b39c7dc54363d40eba295fde558f0836a658e2 Mon Sep 17 00:00:00 2001 From: Bruce Berrios <58147810+Bruception@users.noreply.github.com> Date: Fri, 24 Jun 2022 05:16:53 -0400 Subject: [PATCH] Fix leaderboard update bug (#3217) * Fix leaderboard update bug * Add test for sanitize string --- backend/__tests__/utils/misc.spec.ts | 37 ++++++++++++++++++++++++++++ backend/src/dal/leaderboards.ts | 4 +-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/backend/__tests__/utils/misc.spec.ts b/backend/__tests__/utils/misc.spec.ts index 3ce38909f..573a17cbc 100644 --- a/backend/__tests__/utils/misc.spec.ts +++ b/backend/__tests__/utils/misc.spec.ts @@ -168,4 +168,41 @@ describe("Misc Utils", () => { expect(misc.flattenObjectDeep(obj)).toEqual(expected); }); }); + + it("sanitizeString", () => { + const testCases = [ + { + input: "h̶̼͔̭͈̏́̀́͋͜ͅe̵̺̞̦̫̫͔̋́̅̅̃̀͝͝ļ̶̬̯͚͇̺͍̞̫̟͖͋̓͛̆̒̓͜ĺ̴̗̘͇̬̆͂͌̈͊͝͝ỡ̴̡̦̩̠̞̐̃͆̚͠͝", + expected: "hello", + }, + { + input: "hello", + expected: "hello", + }, + { + input: "hel lo", + expected: "hel lo", + }, + { + input: " hel lo ", + expected: "hel lo", + }, + { + input: "", + expected: "", + }, + { + input: " \n\n\n", + expected: "", + }, + { + input: undefined, + expected: undefined, + }, + ]; + + testCases.forEach(({ input, expected }) => { + expect(misc.sanitizeString(input)).toEqual(expected); + }); + }); }); diff --git a/backend/src/dal/leaderboards.ts b/backend/src/dal/leaderboards.ts index f6bad3b77..5f7b70d41 100644 --- a/backend/src/dal/leaderboards.ts +++ b/backend/src/dal/leaderboards.ts @@ -63,7 +63,7 @@ export async function update( const str = `lbPersonalBests.${mode}.${mode2}.${language}`; const start1 = performance.now(); const lb = await db - .collection("users") + .collection("users") .aggregate( [ { @@ -120,7 +120,7 @@ export async function update( } // extract selected badge - if (lbEntry.badges) { + if (lbEntry.badges?.length) { lbEntry.badgeId = lbEntry.badges.filter( (badge) => badge.selected === true )[0].id;