Skip to content

Commit

Permalink
Add unit tests for /notify
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumManiac committed Feb 14, 2024
1 parent 2a465cd commit f1d4299
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/listeners/commands/notifyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getDefaultSlackChannels, parseEscapedSlashCommandChannel } from "../../
import { slackWorkspaceUrl } from "../../common/constants";
import { SlackLogger } from "../../classes/SlackLogger";

type NotifyParameters = {
export type NotifyParameters = {
messageUrl: string;
pingChannels: boolean;
channels: SlackChannel[] | "default";
Expand Down Expand Up @@ -69,15 +69,16 @@ export default async function notifyCommandHandler({
* @param command The arguments of the `/notify` command
* @returns The parameters for the notify command
*/
function parseNotifyCommand(command: string): NotifyParameters {
export function parseNotifyCommand(command: string): NotifyParameters {
const tokens = command.split(" ");

if (tokens.length == 0) {
if (tokens.length == 1 && tokens[0].trim() == "") {
throw new Error("Please provide a message to send. Usage: `/notify <messageURL>`");
}

// Check if first token is a valid URL
const messageUrl = tokens.shift() as string;
// Links will be wrapped in < and >, so we remove those
const messageUrl = (tokens.shift() as string).replace(/<|>/g, "");
if (!messageUrl.startsWith(`${slackWorkspaceUrl}/archives/`)) {
throw new Error("Please provide a valid message URL from this Slack workspace as the first argument.");
}
Expand Down
Empty file removed src/utils/notify.ts
Empty file.
80 changes: 80 additions & 0 deletions tests/unit/commands/notifyCommand.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { parseNotifyCommand } from "../../../src/listeners/commands/notifyCommand";
import { NotifyParameters } from "../../../src/listeners/commands/notifyCommand";
import SlackChannel from "../../../src/classes/SlackChannel";

describe("parseNotifyCommand", () => {
it("parses the /notify command", () => {
const commandArgs = "https://waterloorocketrydev.slack.com/archives/C015FXXXXXX/p1707843500000000";
const expected: NotifyParameters = {
messageUrl: "https://waterloorocketrydev.slack.com/archives/C015FXXXXXX/p1707843500000000",
pingChannels: false,
channels: "default",
};

expect(parseNotifyCommand(commandArgs)).toEqual(expected);
});

it("parses the /notify command when link is not plaintext", () => {
const commandArgs = "<https://waterloorocketrydev.slack.com/archives/C015FXXXXXX/p1707843500000000>";
const expected: NotifyParameters = {
messageUrl: "https://waterloorocketrydev.slack.com/archives/C015FXXXXXX/p1707843500000000",
pingChannels: false,
channels: "default",
};

expect(parseNotifyCommand(commandArgs)).toEqual(expected);
});

it("parses the /notify command with pinging channels", () => {
const commandArgs =
"https://waterloorocketrydev.slack.com/archives/C015FXXXXXX/p1707843500000000 ping <#C12345678|channel-name> <#C12345679|channel-name2>";
const expected: NotifyParameters = {
messageUrl: "https://waterloorocketrydev.slack.com/archives/C015FXXXXXX/p1707843500000000",
pingChannels: true,
channels: [new SlackChannel("channel-name", "C12345678"), new SlackChannel("channel-name2", "C12345679")],
};

expect(parseNotifyCommand(commandArgs)).toEqual(expected);
});

it("parses the /notify command with the default channels", () => {
const commandArgs = "https://waterloorocketrydev.slack.com/archives/C015FXXXXXX/p1707843500000000";
const expected: NotifyParameters = {
messageUrl: "https://waterloorocketrydev.slack.com/archives/C015FXXXXXX/p1707843500000000",
pingChannels: false,
channels: "default",
};

expect(parseNotifyCommand(commandArgs)).toEqual(expected);
});

it("parses the /notify command with pinging default channels", () => {
const commandArgs = "https://waterloorocketrydev.slack.com/archives/C015FXXXXXX/p1707843500000000 ping";
const expected: NotifyParameters = {
messageUrl: "https://waterloorocketrydev.slack.com/archives/C015FXXXXXX/p1707843500000000",
pingChannels: true,
channels: "default",
};

expect(parseNotifyCommand(commandArgs)).toEqual(expected);
});

it("throws an error when the command is empty", () => {
const commandArgs = "";
expect(() => parseNotifyCommand(commandArgs)).toThrow(
"Please provide a message to send. Usage: `/notify <messageURL>`",
);
});

it("throws an error when the URL is invalid", () => {
const commandArgs = "https://google.com";
expect(() => parseNotifyCommand(commandArgs)).toThrow(
"Please provide a valid message URL from this Slack workspace as the first argument.",
);
});

it("throws an error when the channels are invalid", () => {
const commandArgs = "https://waterloorocketrydev.slack.com/archives/C015FXXXXXX/p1707843500000000 #C12345678";
expect(() => parseNotifyCommand(commandArgs)).toThrow();
});
});
18 changes: 18 additions & 0 deletions tests/unit/utils/channels.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { parseEscapedSlashCommandChannel } from "../../../src/utils/channels";

describe("parseEscapedSlashCommandChannel", () => {
it("parses the escaped channel text from a Slack command and returns the Slack channel", () => {
const text = "<#C12345678|channel-name>";
const expected = {
name: "channel-name",
id: "C12345678",
};

expect(parseEscapedSlashCommandChannel(text)).toEqual(expected);
});

it("Throws an error when the text is not in the correct format", () => {
const text = "channel-name";
expect(() => parseEscapedSlashCommandChannel(text)).toThrow(`could not parse escaped channel text: ${text}`);
});
});

0 comments on commit f1d4299

Please sign in to comment.