Skip to content

Commit dfd1c5b

Browse files
added test cases for /mentioneachuser discord command
1 parent dffed18 commit dfd1c5b

File tree

3 files changed

+53
-19
lines changed

3 files changed

+53
-19
lines changed

src/controllers/mentionEachUser.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,20 @@ export async function mentionEachUser(
2323
) {
2424
const getMembersInServerResponse = await getMembersInServer(env);
2525
const roleId = transformedArgument.roleToBeTaggedObj.value;
26-
const msgToBeSent = transformedArgument?.displayMessageObj?.value;
26+
const customMessage = transformedArgument?.displayMessageObj?.value; // Get custom message
2727
const dev = transformedArgument?.dev?.value || false;
2828

2929
const usersWithMatchingRole = filterUserByRoles(
3030
getMembersInServerResponse as UserArray[],
3131
roleId
3232
);
3333

34-
const payload = {
35-
channelId: transformedArgument.channelId,
36-
roleId: roleId,
37-
message: msgToBeSent,
38-
usersWithMatchingRole,
39-
};
40-
41-
// Construct the message with the role mention and user list
42-
const roleTag = `<@&${roleId}>`;
34+
// Use the custom message if provided, otherwise construct the default message
4335
let responseMessage;
4436
if (usersWithMatchingRole.length === 0) {
45-
responseMessage = `No users found with the ${roleTag} role.`;
37+
responseMessage = customMessage || "Sorry no user found under this role."; // Use custom message if available
4638
} else {
39+
const roleTag = `<@&${roleId}>`;
4740
const userList = usersWithMatchingRole.join(", ");
4841
responseMessage = `The users with ${roleTag} roles are: ${userList}`;
4942
}
@@ -54,14 +47,14 @@ export async function mentionEachUser(
5447
ctx.waitUntil(
5548
mentionEachUserInMessage({
5649
message: responseMessage,
57-
userIds: payload.usersWithMatchingRole,
58-
channelId: payload.channelId,
50+
userIds: usersWithMatchingRole,
51+
channelId: transformedArgument.channelId,
5952
env,
6053
})
6154
);
62-
55+
6356
return discordTextResponse(
6457
`Found ${usersWithMatchingRole.length} users with matched role, mentioning them shortly...`
6558
);
6659
}
67-
}
60+
}

src/utils/guildRole.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export async function createGuildRole(
4444
body: JSON.stringify(data),
4545
});
4646
if (response.ok) {
47-
return await response.json();
47+
return (await response.json()) as string | guildRoleResponse;
4848
} else {
4949
return INTERNAL_SERVER_ERROR;
5050
}
@@ -125,7 +125,7 @@ export async function getGuildRoles(env: env): Promise<Array<Role>> {
125125
throw new Error(ROLE_FETCH_FAILED);
126126
}
127127

128-
const guildRoles: Array<GuildRole> = await response.json();
128+
const guildRoles: Array<GuildRole> = (await response.json()) as GuildRole[];
129129

130130
return guildRoles.map((role) => ({
131131
id: role.id,
@@ -143,6 +143,7 @@ export async function getGuildRoleByName(
143143
const roles = await getGuildRoles(env);
144144
return roles?.find((role) => role.name === roleName);
145145
}
146+
import { Response } from "@cloudflare/workers-types";
146147

147148
export async function mentionEachUserInMessage({
148149
message,
@@ -173,10 +174,12 @@ export async function mentionEachUserInMessage({
173174
content: `${message ? message + " " : ""} ${userId}`,
174175
}),
175176
}).then((response) => {
176-
const rateLimitRemaining = parseRateLimitRemaining(response);
177+
const rateLimitRemaining = parseRateLimitRemaining(
178+
response as unknown as Response
179+
);
177180
if (rateLimitRemaining === 0) {
178181
waitTillNextAPICall = Math.max(
179-
parseResetAfter(response),
182+
parseResetAfter(response as unknown as Response),
180183
waitTillNextAPICall
181184
);
182185
}

tests/unit/handlers/mentionEachUser.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,42 @@ describe("Test mention each function", () => {
126126
const expectedResponse = `${returnString} ${usersWithMatchingRole}`;
127127
expect(response).toBe(expectedResponse);
128128
});
129+
130+
// New test cases in the same style
131+
132+
it("should handle custom message when provided", async () => {
133+
const env = {
134+
BOT_PUBLIC_KEY: "xyz",
135+
DISCORD_GUILD_ID: "123",
136+
DISCORD_TOKEN: "abc",
137+
};
138+
const customArg = {
139+
...transformedArgument,
140+
displayMessageObj: {
141+
name: "displayMessage",
142+
type: 3,
143+
value: "Custom message:",
144+
},
145+
};
146+
const response = mentionEachUser(customArg, env, ctx);
147+
expect(response).toBeInstanceOf(Promise);
148+
const textMessage: { data: { content: string } } = await response.then(
149+
(res) => res.json()
150+
);
151+
expect(textMessage.data.content).toContain("Custom message:");
152+
});
153+
154+
it("should handle empty user array", () => {
155+
const roleId = "123456";
156+
const optionsArray = [] as any[];
157+
const response = filterUserByRoles(optionsArray, roleId);
158+
expect(response).toStrictEqual([]);
159+
});
160+
161+
it("should handle message with no matching users", () => {
162+
const usersWithMatchingRole = [] as string[];
163+
const msgToBeSent = "No users found:";
164+
const response = checkDisplayType({ usersWithMatchingRole, msgToBeSent });
165+
expect(response).toBe("Sorry no user found under this role.");
166+
});
129167
});

0 commit comments

Comments
 (0)