From 3b3dcae5ed82cf9caa620cd57e8454fb20bc9b8c Mon Sep 17 00:00:00 2001 From: moonstar-x Date: Wed, 20 Nov 2024 16:57:49 -0500 Subject: [PATCH] Updated tests to get to 100% coverage. --- src/base/presence/PresenceManager.spec.ts | 23 +++++++++++++++++++ src/base/presence/PresenceResolver.ts | 2 +- src/commands/ConfigureCommand.spec.ts | 8 ++++++- src/config/app.spec.ts | 7 ++++++ .../gameOffers/classes/OffersNotifier.spec.ts | 11 +++++++++ src/i18n/translate.ts | 4 ++-- src/services/database/migration.ts | 4 ---- 7 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/base/presence/PresenceManager.spec.ts b/src/base/presence/PresenceManager.spec.ts index 06fee13..1913e05 100644 --- a/src/base/presence/PresenceManager.spec.ts +++ b/src/base/presence/PresenceManager.spec.ts @@ -95,6 +95,19 @@ describe('Base > Presence > PresenceManager', () => { expect(clearIntervalSpy).toHaveBeenCalledWith(oldHandle); expect((manager as unknown as { intervalHandle: number }).intervalHandle).not.toBe(oldHandle); }); + + it('should call manager.update on an interval.', async () => { + jest.useFakeTimers(); + const updateSpy = jest.spyOn(manager, 'update'); + updateSpy.mockImplementation(() => Promise.resolve()); + + await manager.setRefreshInterval(1000); + jest.advanceTimersByTime(3000); + + expect(manager.update).toHaveBeenCalled(); + jest.clearAllTimers(); + jest.useRealTimers(); + }); }); describe('setPresence()', () => { @@ -117,6 +130,16 @@ describe('Base > Presence > PresenceManager', () => { expect(logger.info).toHaveBeenCalledWith('Presence changed to: something'); }); + it('should log presence change even if no user exists.', () => { + const oldUser = client.user; + client.user = null; + + manager.setPresence('something'); + expect(logger.info).toHaveBeenCalledWith('Presence changed to: something'); + + client.user = oldUser; + }); + it('should log error if presence change fails.', () => { const expectedError = new Error('Oops'); (client.user!.setPresence as jest.Mock).mockImplementationOnce(() => { diff --git a/src/base/presence/PresenceResolver.ts b/src/base/presence/PresenceResolver.ts index e8d2226..487adcd 100644 --- a/src/base/presence/PresenceResolver.ts +++ b/src/base/presence/PresenceResolver.ts @@ -69,7 +69,7 @@ export class PresenceResolver { case 'uptime': return this.getUptime(); default: - throw new Error('Invalid presence name provided.'); + return Promise.resolve(''); } } diff --git a/src/commands/ConfigureCommand.spec.ts b/src/commands/ConfigureCommand.spec.ts index 55cfe66..73c24e1 100644 --- a/src/commands/ConfigureCommand.spec.ts +++ b/src/commands/ConfigureCommand.spec.ts @@ -87,7 +87,10 @@ describe('Commands > ConfigureCommand', () => { update: jest.fn().mockImplementation(() => Promise.resolve()) }; const followUpResponseMock = { - awaitMessageComponent: jest.fn().mockResolvedValue(userResponseMock) + awaitMessageComponent: jest.fn().mockImplementation(({ filter }) => { + filter({ user: { id: 1 }, customId: 'configure-storefronts-enable' }); + return userResponseMock; + }) }; const interaction = { reply: jest.fn(), @@ -96,6 +99,9 @@ describe('Commands > ConfigureCommand', () => { guildId: '1267881983548063785', options: { getSubcommand: jest.fn().mockReturnValue('storefronts') + }, + user: { + id: 1 } } as unknown as GuildChatInputCommandInteraction; diff --git a/src/config/app.spec.ts b/src/config/app.spec.ts index ffa3016..c22923e 100644 --- a/src/config/app.spec.ts +++ b/src/config/app.spec.ts @@ -63,6 +63,13 @@ describe('Config > App', () => { expect(config.DISCORD_SHARDING_COUNT).toBe('auto'); }); + it('should export valid DISCORD_SHARDING_COUNT if set to auto.', () => { + process.env = { ...mockedEnv, DISCORD_SHARDING_COUNT: 'auto' }; + resetModule(); + + expect(config.DISCORD_SHARDING_COUNT).toBe('auto'); + }); + it('should export valid REDIS_URI.', () => { expect(config.REDIS_URI).toBe('redis://localhost:6379'); }); diff --git a/src/features/gameOffers/classes/OffersNotifier.spec.ts b/src/features/gameOffers/classes/OffersNotifier.spec.ts index 9a5811e..6161838 100644 --- a/src/features/gameOffers/classes/OffersNotifier.spec.ts +++ b/src/features/gameOffers/classes/OffersNotifier.spec.ts @@ -166,6 +166,17 @@ describe('Features > GameOffers > Classes > OffersNotifier', () => { guild.shardId = 0; }); + it('should send the message if client is not sharded.', async () => { + const oldShard = client.shard; + client.shard = null; + + await triggerRef.trigger!(); + + expect(channel.send).toHaveBeenCalled(); + + client.shard = oldShard; + }); + it('should not send the message if channel is null.', async () => { (guild.channels.fetch as jest.Mock).mockResolvedValueOnce(null); await triggerRef.trigger!(); diff --git a/src/i18n/translate.ts b/src/i18n/translate.ts index 66b7a34..33310b2 100644 --- a/src/i18n/translate.ts +++ b/src/i18n/translate.ts @@ -28,14 +28,14 @@ export const MESSAGE_KEY_TO_LOCALE: Partial> = { }; const castLocaleStrings = localeStrings as LocaleMessageMap; -const getMessage = (locale: Locale, key: MessageKey, useDefault: boolean = true): IntlMessageFormat => { +const getMessage = (locale: Locale, key: MessageKey, useDefault: boolean): IntlMessageFormat => { const messagesForLocale = castLocaleStrings[locale]; if (!messagesForLocale) { throw new TranslatorError(`No messages for locale ${locale} exist.`); } const message = messagesForLocale[key]; - const defaultMessage = castLocaleStrings[DEFAULT_LOCALE]?.[key]; + const defaultMessage = castLocaleStrings[DEFAULT_LOCALE]![key]; // Note: We can safely null-assert here because DEFAULT_LOCALE is a key of localStrings. const messageToReturn = useDefault ? message ?? defaultMessage : message; if (!messageToReturn) { diff --git a/src/services/database/migration.ts b/src/services/database/migration.ts index 3044c27..3cb1817 100644 --- a/src/services/database/migration.ts +++ b/src/services/database/migration.ts @@ -58,10 +58,6 @@ export const runMigrations = async (migrationsDirectory: string): Promise