Repository navigation
test(bot): cover untested moderation and download commands #1858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3e78edc
test(bot): cover untested moderation and download commands
LucasSantana-Dev c01c358
Merge branch 'main' into test/moderation-download-coverage
LucasSantana-Dev 8431071
Merge branch 'main' into test/moderation-download-coverage
LucasSantana-Dev 59822e4
test(bot): drop unused embedbuilder import (codeql)
LucasSantana-Dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
363 changes: 363 additions & 0 deletions
363
packages/bot/src/functions/download/commands/download/command.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,363 @@ | ||
| import { describe, test, expect, jest, beforeEach } from '@jest/globals' | ||
|
|
||
| const interactionReplyMock = jest.fn() | ||
| const downloadServiceMock = { | ||
| executeDownload: jest.fn(), | ||
| } | ||
|
|
||
| jest.mock('../../../../utils/general/interactionReply', () => ({ | ||
| interactionReply: (...args: any[]) => interactionReplyMock(...args), | ||
| })) | ||
|
|
||
| jest.mock('./service', () => ({ | ||
| DownloadCommandService: jest.fn(() => downloadServiceMock), | ||
| })) | ||
|
|
||
| import downloadCommand from './command' | ||
|
|
||
| function createMockUser(id = 'user-123') { | ||
| return { | ||
| id, | ||
| } | ||
| } | ||
|
|
||
| function createInteraction({ | ||
| userId = 'user-123', | ||
| user = null as any, | ||
| guildId = 'guild-123', | ||
| url = 'https://example.com/video.mp4', | ||
| } = {}) { | ||
| const interaction = { | ||
| user: user || createMockUser(userId), | ||
| guildId, | ||
| options: { | ||
| getString: jest.fn((name: string, required?: boolean) => | ||
| name === 'url' ? url : null, | ||
| ), | ||
| }, | ||
| } | ||
|
|
||
| return interaction as any | ||
| } | ||
|
|
||
| describe('download command', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| test('shows processing message immediately', async () => { | ||
| const interaction = createInteraction() | ||
| downloadServiceMock.executeDownload.mockResolvedValue({ | ||
| success: true, | ||
| fileName: 'video.mp4', | ||
| fileSize: 5242880, // 5 MB | ||
| }) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| // First reply should be the processing message | ||
| expect(interactionReplyMock).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| content: expect.objectContaining({ | ||
| content: expect.stringContaining('Processing'), | ||
| }), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('extracts URL from interaction options', async () => { | ||
| const testUrl = 'https://youtube.com/watch?v=dQw4w9WgXcQ' | ||
| const interaction = createInteraction({ url: testUrl }) | ||
| downloadServiceMock.executeDownload.mockResolvedValue({ | ||
| success: true, | ||
| fileName: 'video.mp4', | ||
| fileSize: 5242880, | ||
| }) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| expect(downloadServiceMock.executeDownload).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| url: testUrl, | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('passes user ID to download service', async () => { | ||
| const userId = 'user-999' | ||
| const interaction = createInteraction({ userId }) | ||
| downloadServiceMock.executeDownload.mockResolvedValue({ | ||
| success: true, | ||
| fileName: 'video.mp4', | ||
| fileSize: 5242880, | ||
| }) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| expect(downloadServiceMock.executeDownload).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| userId, | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('passes guild ID to download service', async () => { | ||
| const guildId = 'guild-456' | ||
| const interaction = createInteraction({ guildId }) | ||
| downloadServiceMock.executeDownload.mockResolvedValue({ | ||
| success: true, | ||
| fileName: 'video.mp4', | ||
| fileSize: 5242880, | ||
| }) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| expect(downloadServiceMock.executeDownload).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| guildId, | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('sets format to video by default', async () => { | ||
| const interaction = createInteraction() | ||
| downloadServiceMock.executeDownload.mockResolvedValue({ | ||
| success: true, | ||
| fileName: 'video.mp4', | ||
| fileSize: 5242880, | ||
| }) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| expect(downloadServiceMock.executeDownload).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| format: 'video', | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('shows success message with file name and size on successful download', async () => { | ||
| const interaction = createInteraction() | ||
| const fileName = 'my-video.mp4' | ||
| const fileSize = 52428800 // 50 MB | ||
| downloadServiceMock.executeDownload.mockResolvedValue({ | ||
| success: true, | ||
| fileName, | ||
| fileSize, | ||
| }) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| expect(interactionReplyMock).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| content: expect.objectContaining({ | ||
| content: expect.stringContaining('Downloaded'), | ||
| }), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('formats file size correctly for success message', async () => { | ||
| const interaction = createInteraction() | ||
| downloadServiceMock.executeDownload.mockResolvedValue({ | ||
| success: true, | ||
| fileName: 'video.mp4', | ||
| fileSize: 5242880, // 5 MB | ||
| }) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| // Should format 5242880 bytes as ~5.00 MB | ||
| expect(interactionReplyMock).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| content: expect.objectContaining({ | ||
| content: expect.stringContaining('.00 MB'), | ||
| }), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('handles downloads larger than 1 GB', async () => { | ||
| const interaction = createInteraction() | ||
| const largeSize = 1073741824 // 1 GB | ||
| downloadServiceMock.executeDownload.mockResolvedValue({ | ||
| success: true, | ||
| fileName: 'large-video.mp4', | ||
| fileSize: largeSize, | ||
| }) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| expect(interactionReplyMock).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| content: expect.objectContaining({ | ||
| content: expect.stringContaining('1024.00 MB'), | ||
| }), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('shows error message when download fails', async () => { | ||
| const interaction = createInteraction() | ||
| downloadServiceMock.executeDownload.mockResolvedValue({ | ||
| success: false, | ||
| error: 'URL not accessible', | ||
| }) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| expect(interactionReplyMock).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| content: expect.objectContaining({ | ||
| content: expect.stringContaining('Download failed'), | ||
| }), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('includes error message in failure response', async () => { | ||
| const interaction = createInteraction() | ||
| const errorMessage = 'Invalid URL format' | ||
| downloadServiceMock.executeDownload.mockResolvedValue({ | ||
| success: false, | ||
| error: errorMessage, | ||
| }) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| expect(interactionReplyMock).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| content: expect.objectContaining({ | ||
| content: expect.stringContaining(errorMessage), | ||
| }), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('marks error response as ephemeral', async () => { | ||
| const interaction = createInteraction() | ||
| downloadServiceMock.executeDownload.mockResolvedValue({ | ||
| success: false, | ||
| error: 'Download failed', | ||
| }) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| // Find the reply call with the error | ||
| const errorCall = interactionReplyMock.mock.calls.find((call) => | ||
| call[0].content.content.includes('Download failed'), | ||
| ) | ||
| expect(errorCall[0]).toHaveProperty('content.ephemeral', true) | ||
| }) | ||
|
|
||
| test('handles unexpected errors during download', async () => { | ||
| const interaction = createInteraction() | ||
| downloadServiceMock.executeDownload.mockRejectedValue( | ||
| new Error('Network error'), | ||
| ) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| expect(interactionReplyMock).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| content: expect.objectContaining({ | ||
| content: expect.stringContaining('An error occurred'), | ||
| }), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('marks unexpected error response as ephemeral', async () => { | ||
| const interaction = createInteraction() | ||
| downloadServiceMock.executeDownload.mockRejectedValue( | ||
| new Error('Network error'), | ||
| ) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| // Find the reply call with the error | ||
| const errorCall = interactionReplyMock.mock.calls.find((call) => | ||
| call[0].content.content.includes('An error occurred'), | ||
| ) | ||
| expect(errorCall[0]).toHaveProperty('content.ephemeral', true) | ||
| }) | ||
|
|
||
| test('formats zero file size gracefully', async () => { | ||
| const interaction = createInteraction() | ||
| downloadServiceMock.executeDownload.mockResolvedValue({ | ||
| success: true, | ||
| fileName: 'empty.mp4', | ||
| fileSize: 0, | ||
| }) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| expect(interactionReplyMock).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| content: expect.objectContaining({ | ||
| content: expect.stringContaining('Unknown'), | ||
| }), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('handles undefined file size', async () => { | ||
| const interaction = createInteraction() | ||
| downloadServiceMock.executeDownload.mockResolvedValue({ | ||
| success: true, | ||
| fileName: 'video.mp4', | ||
| fileSize: undefined, | ||
| }) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| expect(interactionReplyMock).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| content: expect.objectContaining({ | ||
| content: expect.stringContaining('Unknown'), | ||
| }), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('has correct command metadata', () => { | ||
| expect(downloadCommand.data.name).toBe('download') | ||
| expect(downloadCommand.data.description).toContain('Download media') | ||
| }) | ||
|
|
||
| test('handles guild ID as undefined', async () => { | ||
| const interaction = createInteraction({ guildId: null as any }) | ||
| downloadServiceMock.executeDownload.mockResolvedValue({ | ||
| success: true, | ||
| fileName: 'video.mp4', | ||
| fileSize: 5242880, | ||
| }) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| expect(downloadServiceMock.executeDownload).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| guildId: undefined, | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('sends multiple replies in correct order', async () => { | ||
| const interaction = createInteraction() | ||
| downloadServiceMock.executeDownload.mockResolvedValue({ | ||
| success: true, | ||
| fileName: 'video.mp4', | ||
| fileSize: 5242880, | ||
| }) | ||
|
|
||
| await downloadCommand.execute({ interaction }) | ||
|
|
||
| // Should have at least 2 calls: processing and then success/failure | ||
| expect(interactionReplyMock.mock.calls.length).toBeGreaterThanOrEqual(2) | ||
|
|
||
| // First call should be processing message | ||
| expect(interactionReplyMock.mock.calls[0][0].content.content).toContain( | ||
| 'Processing', | ||
| ) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.