1 |
import { faker } from "@faker-js/faker"; |
2 |
import { Snowflake } from "discord.js"; |
3 |
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
4 |
import type Client from "../../src/core/Client"; |
5 |
import ConfigManager, { GuildConfigContainer } from "../../src/services/ConfigManager"; |
6 |
import { SystemConfigSchema } from "../../src/types/SystemConfigSchema"; |
7 |
import { protectSystemAdminsFromCommands } from "../../src/utils/troll"; |
8 |
import { createClient } from "../mocks/client.mock"; |
9 |
import { createMessage } from "../mocks/message.mock"; |
10 |
import { randomSnowflake } from "../mocks/snowflakes"; |
11 |
|
12 |
describe("troll functionalities", () => { |
13 |
let client: Client; |
14 |
let sysAdminId: Snowflake, guildId: Snowflake, normalUserId: Snowflake; |
15 |
|
16 |
beforeEach(() => { |
17 |
sysAdminId = randomSnowflake(); |
18 |
guildId = randomSnowflake(); |
19 |
normalUserId = randomSnowflake(); |
20 |
client = createClient(); |
21 |
client.configManager = { |
22 |
systemConfig: SystemConfigSchema.parse({ |
23 |
system_admins: [sysAdminId] |
24 |
}), |
25 |
config: { |
26 |
[guildId]: { |
27 |
commands: { |
28 |
bean_safe: [], |
29 |
shot_safe: [], |
30 |
fakeban_safe: [] |
31 |
} |
32 |
} |
33 |
} as unknown as GuildConfigContainer |
34 |
} as ConfigManager; |
35 |
}); |
36 |
|
37 |
afterEach(() => { |
38 |
vi.resetAllMocks(); |
39 |
}); |
40 |
|
41 |
it("should protect system admins and itself", async () => { |
42 |
const [sysAdminMessage] = createMessage(faker.lorem.sentence(), sysAdminId, guildId); |
43 |
const [selfMessage] = createMessage(faker.lorem.sentence(), client.user!.id, guildId); |
44 |
let count = 1; |
45 |
|
46 |
for (const key of ["bean_safe", "shot_safe", "fakeban_safe"] as const) { |
47 |
const sysAdminResult = await protectSystemAdminsFromCommands( |
48 |
client, |
49 |
sysAdminMessage, |
50 |
sysAdminId, |
51 |
key |
52 |
); |
53 |
const selfResult = await protectSystemAdminsFromCommands( |
54 |
client, |
55 |
selfMessage, |
56 |
sysAdminId, |
57 |
key |
58 |
); |
59 |
|
60 |
expect(sysAdminResult).toBe(true); |
61 |
expect(selfResult).toBe(true); |
62 |
expect(sysAdminMessage.reply).toHaveBeenCalledTimes(count); |
63 |
expect(selfMessage.reply).toHaveBeenCalledTimes(count); |
64 |
|
65 |
count++; |
66 |
} |
67 |
}); |
68 |
|
69 |
it("should ignore normal users", async () => { |
70 |
const [message] = createMessage(faker.lorem.sentence(), normalUserId, guildId); |
71 |
|
72 |
for (const key of ["bean_safe", "shot_safe", "fakeban_safe"] as const) { |
73 |
const result = await protectSystemAdminsFromCommands( |
74 |
client, |
75 |
message, |
76 |
normalUserId, |
77 |
key |
78 |
); |
79 |
|
80 |
expect(result).toBe(false); |
81 |
expect(message.reply).toHaveBeenCalledTimes(0); |
82 |
} |
83 |
}); |
84 |
}); |