Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Refactor SlashCommands to not use MatrixClientPeg (#10905)
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy authored May 25, 2023
1 parent 192e6f6 commit 796ed35
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 170 deletions.
267 changes: 117 additions & 150 deletions src/SlashCommands.tsx

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion src/components/views/rooms/EditMessageComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,13 @@ class EditMessageComposer extends React.Component<IEditMessageComposerProps, ISt
const [cmd, args, commandText] = getSlashCommand(this.model);
if (cmd) {
const threadId = editedEvent?.getThread()?.id || null;
const [content, commandSuccessful] = await runSlashCommand(cmd, args, roomId, threadId);
const [content, commandSuccessful] = await runSlashCommand(
MatrixClientPeg.get(),
cmd,
args,
roomId,
threadId,
);
if (!commandSuccessful) {
return; // errored
}
Expand Down
1 change: 1 addition & 0 deletions src/components/views/rooms/SendMessageComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ export class SendMessageComposer extends React.Component<ISendMessageComposerPro

let commandSuccessful: boolean;
[content, commandSuccessful] = await runSlashCommand(
MatrixClientPeg.get(),
cmd,
args,
this.props.room.roomId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export async function sendMessage(
if (cmd) {
const threadId = relation?.rel_type === THREAD_RELATION_TYPE.name ? relation?.event_id : null;
let commandSuccessful: boolean;
[content, commandSuccessful] = await runSlashCommand(cmd, args, roomId, threadId ?? null);
[content, commandSuccessful] = await runSlashCommand(mxClient, cmd, args, roomId, threadId ?? null);

if (!commandSuccessful) {
return; // errored
Expand Down
4 changes: 3 additions & 1 deletion src/editor/commands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
import React from "react";
import { logger } from "matrix-js-sdk/src/logger";
import { IContent } from "matrix-js-sdk/src/models/event";
import { MatrixClient } from "matrix-js-sdk/src/matrix";

import EditorModel from "./model";
import { Type } from "./parts";
Expand Down Expand Up @@ -58,12 +59,13 @@ export function getSlashCommand(model: EditorModel): [Command | undefined, strin
}

export async function runSlashCommand(
matrixClient: MatrixClient,
cmd: Command,
args: string | undefined,
roomId: string,
threadId: string | null,
): Promise<[content: IContent | null, success: boolean]> {
const result = cmd.run(roomId, threadId, args);
const result = cmd.run(matrixClient, roomId, threadId, args);
let messageContent: IContent | null = null;
let error: any = result.error;
if (result.promise) {
Expand Down
76 changes: 59 additions & 17 deletions test/SlashCommands-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import { mocked } from "jest-mock";

import { Command, Commands, getCommand } from "../src/SlashCommands";
import { createTestClient } from "./test-utils";
import { MatrixClientPeg } from "../src/MatrixClientPeg";
import { LocalRoom, LOCAL_ROOM_ID_PREFIX } from "../src/models/LocalRoom";
import SettingsStore from "../src/settings/SettingsStore";
import LegacyCallHandler from "../src/LegacyCallHandler";
import { SdkContextClass } from "../src/contexts/SDKContext";
import Modal from "../src/Modal";
import WidgetUtils from "../src/utils/WidgetUtils";
import { WidgetType } from "../src/widgets/WidgetType";

describe("SlashCommands", () => {
let client: MatrixClient;
Expand Down Expand Up @@ -57,7 +59,6 @@ describe("SlashCommands", () => {
jest.clearAllMocks();

client = createTestClient();
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(client);

room = new Room(roomId, client, client.getUserId()!);
localRoom = new LocalRoom(localRoomId, client, client.getUserId()!);
Expand All @@ -70,9 +71,16 @@ describe("SlashCommands", () => {
const command = getCommand("/topic pizza");
expect(command.cmd).toBeDefined();
expect(command.args).toBeDefined();
await command.cmd!.run("room-id", null, command.args);
await command.cmd!.run(client, "room-id", null, command.args);
expect(client.setRoomTopic).toHaveBeenCalledWith("room-id", "pizza", undefined);
});

it("should show topic modal if no args passed", async () => {
const spy = jest.spyOn(Modal, "createDialog");
const command = getCommand("/topic")!;
await command.cmd!.run(client, roomId, null);
expect(spy).toHaveBeenCalled();
});
});

describe.each([
Expand Down Expand Up @@ -104,12 +112,12 @@ describe("SlashCommands", () => {
describe("isEnabled", () => {
it("should return true for Room", () => {
setCurrentRoom();
expect(command.isEnabled()).toBe(true);
expect(command.isEnabled(client)).toBe(true);
});

it("should return false for LocalRoom", () => {
setCurrentLocalRoon();
expect(command.isEnabled()).toBe(false);
expect(command.isEnabled(client)).toBe(false);
});
});
});
Expand All @@ -127,12 +135,12 @@ describe("SlashCommands", () => {

it("should return true for Room", () => {
setCurrentRoom();
expect(command.isEnabled()).toBe(true);
expect(command.isEnabled(client)).toBe(true);
});

it("should return false for LocalRoom", () => {
setCurrentLocalRoon();
expect(command.isEnabled()).toBe(false);
expect(command.isEnabled(client)).toBe(false);
});
});

Expand All @@ -143,12 +151,12 @@ describe("SlashCommands", () => {

it("should return false for Room", () => {
setCurrentRoom();
expect(command.isEnabled()).toBe(false);
expect(command.isEnabled(client)).toBe(false);
});

it("should return false for LocalRoom", () => {
setCurrentLocalRoon();
expect(command.isEnabled()).toBe(false);
expect(command.isEnabled(client)).toBe(false);
});
});
});
Expand All @@ -169,12 +177,12 @@ describe("SlashCommands", () => {

it("should return true for Room", () => {
setCurrentRoom();
expect(command.isEnabled()).toBe(true);
expect(command.isEnabled(client)).toBe(true);
});

it("should return false for LocalRoom", () => {
setCurrentLocalRoon();
expect(command.isEnabled()).toBe(false);
expect(command.isEnabled(client)).toBe(false);
});
});

Expand All @@ -187,12 +195,12 @@ describe("SlashCommands", () => {

it("should return false for Room", () => {
setCurrentRoom();
expect(command.isEnabled()).toBe(false);
expect(command.isEnabled(client)).toBe(false);
});

it("should return false for LocalRoom", () => {
setCurrentLocalRoon();
expect(command.isEnabled()).toBe(false);
expect(command.isEnabled(client)).toBe(false);
});
});
});
Expand All @@ -209,7 +217,7 @@ describe("SlashCommands", () => {
const command = getCommand("/part #foo:bar");
expect(command.cmd).toBeDefined();
expect(command.args).toBeDefined();
await command.cmd!.run("room-id", null, command.args);
await command.cmd!.run(client, "room-id", null, command.args);
expect(client.leaveRoomChain).toHaveBeenCalledWith("room-id", expect.anything());
});

Expand All @@ -223,7 +231,7 @@ describe("SlashCommands", () => {
const command = getCommand("/part #foo:bar");
expect(command.cmd).toBeDefined();
expect(command.args).toBeDefined();
await command.cmd!.run("room-id", null, command.args!);
await command.cmd!.run(client, "room-id", null, command.args!);
expect(client.leaveRoomChain).toHaveBeenCalledWith("room-id", expect.anything());
});
});
Expand All @@ -232,11 +240,45 @@ describe("SlashCommands", () => {
const command = findCommand(commandName)!;

it("should return usage if no args", () => {
expect(command.run(roomId, null, undefined).error).toBe(command.getUsage());
expect(command.run(client, roomId, null, undefined).error).toBe(command.getUsage());
});

it("should make things rainbowy", () => {
return expect(command.run(roomId, null, "this is a test message").promise).resolves.toMatchSnapshot();
return expect(
command.run(client, roomId, null, "this is a test message").promise,
).resolves.toMatchSnapshot();
});
});

describe.each(["shrug", "tableflip", "unflip", "lenny"])("/%s", (commandName: string) => {
const command = findCommand(commandName)!;

it("should match snapshot with no args", () => {
return expect(command.run(client, roomId, null).promise).resolves.toMatchSnapshot();
});

it("should match snapshot with args", () => {
return expect(
command.run(client, roomId, null, "this is a test message").promise,
).resolves.toMatchSnapshot();
});
});

describe("/addwidget", () => {
it("should parse html iframe snippets", async () => {
jest.spyOn(WidgetUtils, "canUserModifyWidgets").mockReturnValue(true);
const spy = jest.spyOn(WidgetUtils, "setRoomWidget");
const command = findCommand("addwidget")!;
await command.run(client, roomId, null, '<iframe src="https://element.io"></iframe>');
expect(spy).toHaveBeenCalledWith(
client,
roomId,
expect.any(String),
WidgetType.CUSTOM,
"https://element.io",
"Custom",
{},
);
});
});
});
56 changes: 56 additions & 0 deletions test/__snapshots__/SlashCommands-test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`SlashCommands /lenny should match snapshot with args 1`] = `
{
"body": "( ͡° ͜ʖ ͡°) this is a test message",
"msgtype": "m.text",
}
`;

exports[`SlashCommands /lenny should match snapshot with no args 1`] = `
{
"body": "( ͡° ͜ʖ ͡°)",
"msgtype": "m.text",
}
`;

exports[`SlashCommands /rainbow should make things rainbowy 1`] = `
{
"body": "this is a test message",
Expand All @@ -17,3 +31,45 @@ exports[`SlashCommands /rainbowme should make things rainbowy 1`] = `
"msgtype": "m.emote",
}
`;

exports[`SlashCommands /shrug should match snapshot with args 1`] = `
{
"body": "¯\\_(ツ)_/¯ this is a test message",
"msgtype": "m.text",
}
`;

exports[`SlashCommands /shrug should match snapshot with no args 1`] = `
{
"body": "¯\\_(ツ)_/¯",
"msgtype": "m.text",
}
`;

exports[`SlashCommands /tableflip should match snapshot with args 1`] = `
{
"body": "(╯°□°)╯︵ ┻━┻ this is a test message",
"msgtype": "m.text",
}
`;

exports[`SlashCommands /tableflip should match snapshot with no args 1`] = `
{
"body": "(╯°□°)╯︵ ┻━┻",
"msgtype": "m.text",
}
`;

exports[`SlashCommands /unflip should match snapshot with args 1`] = `
{
"body": "┬──┬ ノ( ゜-゜ノ) this is a test message",
"msgtype": "m.text",
}
`;

exports[`SlashCommands /unflip should match snapshot with no args 1`] = `
{
"body": "┬──┬ ノ( ゜-゜ノ)",
"msgtype": "m.text",
}
`;

0 comments on commit 796ed35

Please sign in to comment.