|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +vi.mock('../../src/logger.js', () => ({ |
| 4 | + info: vi.fn(), |
| 5 | + error: vi.fn(), |
| 6 | + warn: vi.fn(), |
| 7 | + debug: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock('../../src/modules/config.js', () => ({ |
| 11 | + getConfig: vi.fn().mockReturnValue({}), |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock('../../src/modules/welcomeOnboarding.js', () => ({ |
| 15 | + buildRoleMenuMessage: vi.fn().mockReturnValue(null), |
| 16 | + buildRulesAgreementMessage: vi.fn().mockReturnValue({ content: 'rules' }), |
| 17 | + normalizeWelcomeOnboardingConfig: vi.fn().mockReturnValue({}), |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock('../../src/utils/permissions.js', () => ({ |
| 21 | + isModerator: vi.fn().mockReturnValue(false), |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock('../../src/utils/safeSend.js', () => ({ |
| 25 | + safeSend: vi.fn().mockResolvedValue(undefined), |
| 26 | + safeEditReply: vi.fn().mockResolvedValue(undefined), |
| 27 | +})); |
| 28 | + |
| 29 | +import { PermissionsBitField } from 'discord.js'; |
| 30 | +import { adminOnly, data, execute } from '../../src/commands/welcome.js'; |
| 31 | +import { isModerator } from '../../src/utils/permissions.js'; |
| 32 | +import { safeEditReply } from '../../src/utils/safeSend.js'; |
| 33 | + |
| 34 | +function mockInteraction(overrides = {}) { |
| 35 | + return { |
| 36 | + member: { |
| 37 | + id: 'user-1', |
| 38 | + permissions: new PermissionsBitField(), |
| 39 | + }, |
| 40 | + user: { id: 'user-1' }, |
| 41 | + guildId: 'guild-1', |
| 42 | + guild: { |
| 43 | + channels: { |
| 44 | + cache: { get: vi.fn().mockReturnValue(null) }, |
| 45 | + fetch: vi.fn().mockResolvedValue(null), |
| 46 | + }, |
| 47 | + }, |
| 48 | + deferReply: vi.fn().mockResolvedValue(undefined), |
| 49 | + ...overrides, |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +describe('welcome command', () => { |
| 54 | + afterEach(() => { |
| 55 | + vi.clearAllMocks(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should export data with name "welcome"', () => { |
| 59 | + expect(data.name).toBe('welcome'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should export adminOnly = true', () => { |
| 63 | + expect(adminOnly).toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should reject non-admin non-moderator users', async () => { |
| 67 | + const interaction = mockInteraction(); |
| 68 | + |
| 69 | + await execute(interaction); |
| 70 | + |
| 71 | + expect(safeEditReply).toHaveBeenCalledWith( |
| 72 | + interaction, |
| 73 | + expect.objectContaining({ |
| 74 | + content: expect.stringContaining('moderator or administrator'), |
| 75 | + }), |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should show not configured messages when welcome config is empty', async () => { |
| 80 | + isModerator.mockReturnValueOnce(true); |
| 81 | + const interaction = mockInteraction(); |
| 82 | + |
| 83 | + await execute(interaction); |
| 84 | + |
| 85 | + expect(safeEditReply).toHaveBeenCalledWith( |
| 86 | + interaction, |
| 87 | + expect.objectContaining({ |
| 88 | + content: expect.stringContaining('not configured'), |
| 89 | + }), |
| 90 | + ); |
| 91 | + }); |
| 92 | +}); |
0 commit comments