Skip to content

Commit

Permalink
Merge pull request #17 from NID-roid/chore/refactor-index-test
Browse files Browse the repository at this point in the history
"Refactor handleMessageCreate to improve message content matching"
  • Loading branch information
k-taro56 authored Jun 3, 2024
2 parents e969684 + 85b1e3f commit 1d6a7b2
Showing 1 changed file with 51 additions and 20 deletions.
71 changes: 51 additions & 20 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,37 @@ describe('handleMessageCreate', () => {
const client = { user: {} } as unknown as Client;
const handleMessageCreateCurried = handleMessageCreate(client);

const createMockMessage = (
content: string,
isBot: boolean,
channelType: ChannelType,
mentions?: { has: () => boolean },
) =>
({
const createMockMessage = ({
content,
channelType,
isBot = false,
isMentionedMe = false,
}: {
content: string;
channelType: ChannelType;
isBot?: boolean;
isMentionedMe?: boolean;
}) => {
return {
content,
author: { bot: isBot, displayAvatarURL: mockDisplayAvatarURL },
channel: { type: channelType },
react: mockReact,
reply: mockReply,
mentions: { users: mentions },
}) as unknown as Message;
mentions: { users: { has: () => isMentionedMe } },
} as unknown as Message;
};

beforeEach(() => {
jest.clearAllMocks();
});

it('should not reply to messages from bots', async () => {
const message = createMockMessage('Hello', true, ChannelType.DM);
const message = createMockMessage({
content: 'Hello',
channelType: ChannelType.DM,
isBot: true,
});
await handleMessageCreateCurried(message);

if (isSetAuditLogWebHook) {
Expand All @@ -65,7 +75,10 @@ describe('handleMessageCreate', () => {
});

it('should react with specific emojis when content includes "代表"', async () => {
const message = createMockMessage('Hello 代表', true, ChannelType.DM);
const message = createMockMessage({
content: 'Hello 代表',
channelType: ChannelType.DM,
});
await handleMessageCreateCurried(message);

if (isSetAuditLogWebHook) {
Expand All @@ -77,34 +90,45 @@ describe('handleMessageCreate', () => {
});

it('replies with a specific URL and reacts when the message content is "!sasudai"', async () => {
const message = createMockMessage('!sasudai', true, ChannelType.DM);
const message = createMockMessage({
content: '!sasudai',
channelType: ChannelType.DM,
});
await handleMessageCreateCurried(message);

expect(mockReply).toHaveBeenCalledWith(
'https://x.com/STECH_FES/status/1773995315420631265',
);

expectReactionsToHaveBeenCalled(mockReact);

expect(fetch).not.toHaveBeenCalled();
});

it('should reply to direct messages if not from a bot', async () => {
const message = createMockMessage('Hello', false, ChannelType.DM);
const message = createMockMessage({
content: 'Hello',
channelType: ChannelType.DM,
});
await handleMessageCreateCurried(message);

if (isSetAuditLogWebHook) {
expect(fetch).toHaveBeenCalled();
} else {
expect(fetch).not.toHaveBeenCalled();
}

expect(mockReply).toHaveBeenCalledWith(
process.env.DM_MESSAGE_CONTENT ?? '',
);
});

it('should not reply if the message author is a bot', async () => {
const message = createMockMessage('', true, ChannelType.GuildText, {
has: () => true,
const message = createMockMessage({
content: '',
channelType: ChannelType.GuildText,
isBot: true,
isMentionedMe: true,
});

await handleMessageCreateCurried(message);
Expand All @@ -114,8 +138,10 @@ describe('handleMessageCreate', () => {
});

it('should reply to mentions if not from a bot', async () => {
const message = createMockMessage('', false, ChannelType.GuildText, {
has: () => true,
const message = createMockMessage({
content: '',
channelType: ChannelType.GuildText,
isMentionedMe: true,
});

await handleMessageCreateCurried(message);
Expand All @@ -131,7 +157,10 @@ describe('handleMessageCreate', () => {
// biome-ignore lint/performance/noDelete: Test undefined env vars to ensure the default value is used
delete process.env.DM_MESSAGE_CONTENT;

const message = createMockMessage('Hello', false, ChannelType.DM);
const message = createMockMessage({
content: 'Hello',
channelType: ChannelType.DM,
});
await handleMessageCreateCurried(message);

expect(mockReply).toHaveBeenCalledWith('');
Expand All @@ -141,8 +170,10 @@ describe('handleMessageCreate', () => {
// biome-ignore lint/performance/noDelete: Test undefined env vars to ensure the default value is used
delete process.env.MENTION_MESSAGE_CONTENT;

const message = createMockMessage('', false, ChannelType.GuildText, {
has: () => true,
const message = createMockMessage({
content: '',
channelType: ChannelType.GuildText,
isMentionedMe: true,
});

await handleMessageCreateCurried(message);
Expand Down

0 comments on commit 1d6a7b2

Please sign in to comment.