diff --git a/src/leaderboards.js b/src/leaderboards.js index e6bb96d1..3ac4e827 100644 --- a/src/leaderboards.js +++ b/src/leaderboards.js @@ -18,8 +18,10 @@ const onStop = async (year, day, part, name) => { // Use the year and day parameter to update a single leaderboard const result = await updateLeaderboards({ year, day }); + console.debug(result); + // Return true if the user was invited to the channel for requested day - return result.sent.any(r => r.aocUser === name && r.year === year && r.day === day); + return result.sent.some(r => r.aocUser === name && r.year === year && r.day === day); }; const updateLeaderboards = async (selection = {}) => { diff --git a/unit-tests/leaderboards.test.js b/unit-tests/leaderboards.test.js index 83392c52..ad7d132a 100644 --- a/unit-tests/leaderboards.test.js +++ b/unit-tests/leaderboards.test.js @@ -1,6 +1,6 @@ 'use strict'; -const { handler, updateLeaderboards } = require('../src/leaderboards'); +const { handler, onStop, updateLeaderboards } = require('../src/leaderboards'); const invites = require('../src/invites'); jest.mock('../src/invites'); @@ -25,6 +25,39 @@ beforeEach(() => { logs.logActivity.mockReset(); }); +describe('onStop', () => { + test('triggers leaderboard update', async () => { + years.getYears.mockResolvedValueOnce(new Set()); + + await expect(onStop(1945, 11, 2, 'sOmE oNe')).resolves.toBe(false); + }); + + test('reports invitation sent to this user', async () => { + years.getYears.mockResolvedValueOnce(new Set([1945])); + + network.getLeaderboard.mockResolvedValueOnce({ lEaDeRbOaRd2020: true }); + invites.processInvites.mockResolvedValueOnce({ sent: [{ aocUser: 'sOmE oNe', year: 1945, day: 11 }], failed: [] }); + boardPublish.publishBoards.mockResolvedValueOnce({ created: [], updated: [] }); + + await expect(onStop(1945, 11, 2, 'sOmE oNe')).resolves.toBe(true); + }); + + test('reports invitation not sent to this user', async () => { + years.getYears.mockResolvedValueOnce(new Set([1945])); + + network.getLeaderboard.mockResolvedValueOnce({ lEaDeRbOaRd2020: true }); + invites.processInvites.mockResolvedValueOnce({ sent: [{ aocUser: 'aNoThEr OnE', year: 1945, day: 11 }], failed: [] }); + boardPublish.publishBoards.mockResolvedValueOnce({ created: [], updated: [] }); + + await expect(onStop(1945, 11, 2, 'sOmE oNe')).resolves.toBe(false); + }); + + test('fails after an error while updating leaderboards', async () => { + years.getYears.mockRejectedValueOnce(new Error('sOmEeRrOr')); + await expect(onStop(1945, 11, 2, 'sOmE oNe')).rejects.toThrow('sOmEeRrOr'); + }); +}); + describe('leaderboards.handler', () => { test('succeeds updating the leaderboard', async () => { years.getYears.mockResolvedValueOnce(new Set([2021, 2020]));