/[sudobot]/branches/8.x/tests/utils/embed.test.ts
ViewVC logotype

Annotation of /branches/8.x/tests/utils/embed.test.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 5958 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 import { faker } from "@faker-js/faker";
2     import { ColorResolvable, Colors, EmbedBuilder, User } from "discord.js";
3     import { beforeEach, describe, expect, it } from "vitest";
4     import { ChatInputCommandContext } from "../../src/services/CommandManager";
5     import { generateEmbed, guildInfo, userInfo } from "../../src/utils/embed";
6     import { createClient } from "../mocks/client.mock";
7     import { createGuild, createInvite } from "../mocks/guild.mock";
8     import { randomSnowflake } from "../mocks/snowflakes";
9    
10     describe("generateEmbed", () => {
11     let authorIconURL: string;
12     let footerIconURL: string;
13     let thumbnailURL: string;
14     let imageURL: string;
15     let videoURL: string;
16     let timestamp: Date;
17    
18     let options: ChatInputCommandContext["options"];
19    
20     beforeEach(() => {
21     authorIconURL = faker.image.avatar();
22     footerIconURL = faker.image.avatar();
23     thumbnailURL = faker.image.avatar();
24     imageURL = faker.image.avatar();
25     videoURL = faker.internet.url() + "1.mp4";
26     timestamp = new Date();
27    
28     options = {
29     getString: (field: string) => {
30     switch (field) {
31     case "author_name":
32     return "author";
33     case "author_iconurl":
34     return authorIconURL;
35     case "footer_text":
36     return "footer";
37     case "footer_iconurl":
38     return footerIconURL;
39     case "color":
40     return Colors.Red satisfies ColorResolvable;
41     case "title":
42     return "title";
43     case "description":
44     return "description";
45     case "thumbnail":
46     return thumbnailURL;
47     case "image":
48     return imageURL;
49     case "video":
50     return videoURL;
51     case "timestamp":
52     return timestamp.toISOString();
53     case "fields":
54     return "name: value";
55     default:
56     return undefined;
57     }
58     }
59     } as ChatInputCommandContext["options"];
60     });
61    
62     it("should generate an embed", () => {
63     const embed = generateEmbed(options);
64    
65     expect(embed).toEqual({
66     embed: new EmbedBuilder({
67     video: {
68     url: videoURL
69     }
70     })
71     .setAuthor({
72     name: "author",
73     iconURL: authorIconURL
74     })
75     .setTitle("title")
76     .setDescription("description")
77     .setThumbnail(thumbnailURL)
78     .setImage(imageURL)
79    
80     .setFooter({
81     text: "footer",
82     iconURL: footerIconURL
83     })
84     .setTimestamp(timestamp)
85     .addFields({
86     name: "name",
87     value: "value"
88     })
89     .setColor(Colors.Red)
90     });
91     });
92    
93     it("should return an error if the color is invalid", () => {
94     const original = options.getString;
95    
96     options.getString = ((field: string) => {
97     if (field === "color") {
98     return "invalid";
99     }
100    
101     return original(field);
102     }) as typeof options.getString;
103    
104     const embed = generateEmbed(options);
105    
106     expect(embed).toEqual({
107     error: "Invalid color given."
108     });
109     });
110    
111     it("should set the timestamp to the current date if the timestamp is 'current'", () => {
112     const original = options.getString;
113    
114     options.getString = ((field: string) => {
115     if (field === "timestamp") {
116     return "current";
117     }
118    
119     return original(field);
120     }) as typeof options.getString;
121    
122     const startTime = Date.now();
123     const embed = generateEmbed(options);
124     const endTime = Date.now();
125    
126     expect(new Date(embed.embed?.toJSON().timestamp ?? "").getTime()).greaterThanOrEqual(
127     startTime
128     );
129     expect(new Date(embed.embed?.toJSON().timestamp ?? "").getTime()).lessThanOrEqual(endTime);
130     });
131     });
132    
133     describe("userInfo", () => {
134     it("should return the user info", () => {
135     const user = {
136     id: randomSnowflake(),
137     username: faker.internet.userName(),
138     client: createClient(),
139     toString() {
140     return `<@${this.id}>`;
141     }
142     } as unknown as User;
143    
144     const info = userInfo(user);
145     expect(info).toBe(
146     `ID: ${user.id}\nUsername: ${user.username}\nMention: ${user.toString()}`
147     );
148     });
149    
150     it("should return the system user info if the user is the bot", () => {
151     const user = createClient().user;
152    
153     const info1 = userInfo(user, true);
154     const info2 = userInfo(user, false);
155    
156     expect(info1).toBe("System");
157     expect(info2).toBe(`Type: __System__\nMention: ${user.toString()}`);
158     });
159     });
160    
161     describe("guildInfo", () => {
162     it("should return the guild info", () => {
163     const guild = createGuild();
164     const invite = createInvite();
165    
166     guild.id = "123456789";
167     guild.name = "Test Guild";
168     guild.invites.cache.set("inviteCode", invite);
169    
170     const info = guildInfo(guild);
171    
172     expect(info).toBe(`ID: 123456789\nName: Test Guild\nInvite: ${invite.url}`);
173     });
174    
175     it("should return 'Unavailable' if the guild invite is not available", () => {
176     const guild = createGuild();
177    
178     guild.id = "987654321";
179     guild.name = "Another Guild";
180    
181     const info = guildInfo(guild);
182    
183     expect(info).toBe("ID: 987654321\nName: Another Guild\nInvite: *Unavailable*");
184     });
185     });

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26