Skip to content

Commit

Permalink
add unit tests for embed builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Starman3787 committed Jul 17, 2024
1 parent e9c2c4a commit ff60962
Show file tree
Hide file tree
Showing 3 changed files with 390 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ module.exports.AUTO_MODERATION_ACTION_TYPES = {
module.exports.GATEWAY_RECONNECT_CLOSE_CODES = [4000, 4001, 4002, 4003, 4005, 4007, 4008, 4009];
module.exports.LIMITS = {
MAX_EMBED_SIZE: 6000,
MAX_EMBED_DESCRIPTION: 4096
MAX_EMBED_DESCRIPTION: 4096,
MAX_EMBED_TITLE: 256,
MAX_EMBED_FIELDS: 25,
MAX_EMBED_FIELD_NAME: 256,
MAX_EMBED_FIELD_VALUE: 1024,
MAX_EMBED_FOOTER_TEXT: 2048,
MAX_EMBED_AUTHOR_NAME: 256,
};
module.exports.VERSION = require("../package.json").version;
47 changes: 40 additions & 7 deletions src/util/builder/embedBuilder.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { LIMITS } = require("../../constants");
const hexToInt = require("../general/hexToInt");

/**
Expand All @@ -19,8 +20,12 @@ class Embed {
* @returns {Embed}
*/
setTitle(title) {

if (!title)
throw new TypeError("GLUON: Embed title must be provided.");

this.title =
title && title.length > 256 ? `${title.substring(0, 253)}...` : title;
title && title.length > LIMITS.MAX_EMBED_TITLE ? `${title.substring(0, LIMITS.MAX_EMBED_TITLE - 3)}...` : title;

return this;
}
Expand All @@ -31,8 +36,12 @@ class Embed {
* @returns {Embed}
*/
setDescription(text) {

if (!text)
throw new TypeError("GLUON: Embed description must be provided.");

this.description =
text && text.length > 4096 ? `${text.substring(0, 4093)}...` : text;
text && text.length > LIMITS.MAX_EMBED_DESCRIPTION ? `${text.substring(0, LIMITS.MAX_EMBED_DESCRIPTION - 3)}...` : text;

return this;
}
Expand All @@ -43,6 +52,10 @@ class Embed {
* @returns {Embed}
*/
setURL(url) {

if (!url)
throw new TypeError("GLUON: Embed url must be provided.");

this.url = url;

return this;
Expand All @@ -66,6 +79,10 @@ class Embed {
* @returns {Embed}
*/
setColor(colour) {

if (!colour)
throw new TypeError("GLUON: Embed colour must be provided.");

if (typeof colour == "string") {
if (colour[0] == "#") colour = colour.substring(1);

Expand All @@ -81,6 +98,10 @@ class Embed {
* @returns {Embed}
*/
setThumbnail(url) {

if (!url)
throw new TypeError("GLUON: Embed thumbnail url must be provided.");

this.thumbnail = {
url,
};
Expand All @@ -95,8 +116,12 @@ class Embed {
* @returns {Embed}
*/
setFooter(text, icon) {

if (!text)
throw new TypeError("GLUON: Embed footer text must be provided.");

this.footer = {
text: text && text.length > 2048 ? `${text.substring(0, 2045)}...` : text,
text: text && text.length > LIMITS.MAX_EMBED_FOOTER_TEXT ? `${text.substring(0, LIMITS.MAX_EMBED_FOOTER_TEXT - 3)}...` : text,
};
if (icon) this.footer.icon_url = icon;

Expand All @@ -111,9 +136,13 @@ class Embed {
* @returns {Embed}
*/
setAuthor(name, url, icon_url) {

if (!name)
throw new TypeError("GLUON: Embed author name must be provided.");

this.author = {};

if (name) this.author.name = name;
if (name) this.author.name = (name && name.length > LIMITS.MAX_EMBED_AUTHOR_NAME ? `${name.substring(0, LIMITS.MAX_EMBED_AUTHOR_NAME - 3)}...` : name);
if (url) this.author.url = url;
if (icon_url) this.author.icon_url = icon_url;

Expand All @@ -128,12 +157,16 @@ class Embed {
* @returns {Embed}
*/
addField(name, value, inline = false) {
if (this.fields.length == 25) return this;
if (this.fields.length == LIMITS.MAX_EMBED_FIELDS)
throw new RangeError(`GLUON: Embed fields cannot exceed ${LIMITS.MAX_EMBED_FIELDS} fields.`);

if (!name || !value)
throw new TypeError("GLUON: Embed field name and value must be provided.");

this.fields.push({
name: name && name.length > 256 ? `${name.substring(0, 253)}...` : name,
name: name && name.length > LIMITS.MAX_EMBED_FIELD_NAME ? `${name.substring(0, LIMITS.MAX_EMBED_FIELD_NAME - 3)}...` : name,
value:
value && value.length > 1024 ? `${value.substring(0, 1021)}...` : value,
value && value.length > LIMITS.MAX_EMBED_FIELD_VALUE ? `${value.substring(0, LIMITS.MAX_EMBED_FIELD_VALUE - 3)}...` : value,
inline,
});

Expand Down
Loading

0 comments on commit ff60962

Please sign in to comment.