/[sudobot]/trunk/tests/framework/arguments/UserArgument.test.ts
ViewVC logotype

Contents of /trunk/tests/framework/arguments/UserArgument.test.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 575 - (show annotations)
Mon Jul 29 17:59:26 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 2779 byte(s)
chore: add trunk
1 import { Casted } from "@framework/arguments/Argument";
2 import UserArgument from "@framework/arguments/UserArgument";
3 import BaseClient from "@framework/client/BaseClient";
4 import * as entityUtils from "@framework/utils/entities";
5 import { Snowflake, User } from "discord.js";
6 import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
7 import { createApplication } from "../../mocks/application.mock";
8 import { randomSnowflake } from "../../mocks/snowflakes";
9 import { initialize } from "./ArgumentTestUtils";
10
11 const fetchUser = vi
12 .spyOn(entityUtils, "fetchUser")
13 .mockImplementation(async (client: BaseClient<boolean>, userId: Snowflake) => {
14 if (userId === "11111111111111111") {
15 return null;
16 }
17
18 return {
19 [Symbol.hasInstance]: (obj: unknown) => obj === User,
20 id: userId,
21 client
22 } as unknown as User;
23 });
24
25 describe("UserArgument", () => {
26 const { client } = createApplication();
27
28 let userId: Snowflake, guildId: Snowflake;
29 let data: ReturnType<typeof initialize>;
30
31 beforeEach(() => {
32 userId = randomSnowflake();
33 guildId = randomSnowflake();
34 });
35
36 afterEach(() => {
37 fetchUser.mockClear();
38 });
39
40 it("should parse a user argument with mentions", async () => {
41 data = initialize({
42 content: `test <@${userId}>`,
43 userId,
44 guildId,
45 prefix: "!"
46 });
47
48 const result = (await UserArgument.performCast(
49 data.context,
50 data.content,
51 data.argv,
52 data.argv[1],
53 0,
54 "testarg",
55 undefined,
56 true
57 )) as Casted<User>;
58
59 expect(result.value?.getRawValue()).toBe(`<@${userId}>`);
60 expect(result.value?.getValue().id).toBe(userId);
61 expect(result.error).toBeUndefined();
62 expect(fetchUser).toBeCalledWith(client, userId);
63 expect(fetchUser).toBeCalledTimes(1);
64 });
65
66 it("should parse a user argument with snowflake IDs", async () => {
67 data = initialize({
68 content: `test ${userId}`,
69 userId,
70 guildId,
71 prefix: "!"
72 });
73
74 const result = (await UserArgument.performCast(
75 data.context,
76 data.content,
77 data.argv,
78 data.argv[1],
79 0,
80 "testarg",
81 undefined,
82 true
83 )) as Casted<User>;
84
85 expect(result.value?.getRawValue()).toBe(`${userId}`);
86 expect(result.value?.getValue().id).toBe(userId);
87 expect(result.error).toBeUndefined();
88 expect(fetchUser).toBeCalledWith(client, userId);
89 expect(fetchUser).toBeCalledTimes(1);
90 });
91 });

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26