|
| 1 | +import "jasmine"; |
| 2 | +import { ActivityTracker, BotCommand, BotCommandHandler, CommandArguments } from "../../src/index"; |
| 3 | +import { WhoisInfo, PresenceEventContent, MatrixClient } from "matrix-bot-sdk"; |
| 4 | + |
| 5 | + |
| 6 | +describe("BotCommands", () => { |
| 7 | + it("does not construct without commands", () => { |
| 8 | + expect(() => new BotCommandHandler({}, undefined)).toThrowError('Prototype did not have any bot commands bound'); |
| 9 | + }); |
| 10 | + |
| 11 | + it("to process a simple command", async () => { |
| 12 | + let called = false; |
| 13 | + |
| 14 | + class SimpleBotCommander { |
| 15 | + @BotCommand({ help: "Some help", name: "simple-command"}) |
| 16 | + public simpleCommand(data: CommandArguments<null>): void { |
| 17 | + called = true; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + const handler = new BotCommandHandler(new SimpleBotCommander()); |
| 22 | + await handler.handleCommand("simple-command", null); |
| 23 | + expect(called).toBeTrue(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("to process a simple command with augments", async () => { |
| 27 | + let called: any = undefined; |
| 28 | + |
| 29 | + class SimpleBotCommander { |
| 30 | + @BotCommand({ help: "Some help", name: "simple-command", requiredArgs: ["foo", "bar"]}) |
| 31 | + public simpleCommand(data: CommandArguments<{some: string}>): void { |
| 32 | + called = data; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + const handler = new BotCommandHandler(new SimpleBotCommander()); |
| 37 | + await handler.handleCommand("simple-command abc def", {some: "context"}); |
| 38 | + const expectedResult = { |
| 39 | + args: ["abc", "def"], |
| 40 | + request: { |
| 41 | + some: "context", |
| 42 | + } |
| 43 | + } |
| 44 | + expect(called).toEqual(expectedResult); |
| 45 | + }); |
| 46 | + |
| 47 | + it("to process a simple command with optional parameters", async () => { |
| 48 | + let called: any = undefined; |
| 49 | + |
| 50 | + class SimpleBotCommander { |
| 51 | + @BotCommand({ help: "Some help", name: "simple-command", requiredArgs: ["foo", "bar"], optionalArgs: ["baz"]}) |
| 52 | + public simpleCommand(data: CommandArguments<{some: string}>): void { |
| 53 | + called = data; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + const handler = new BotCommandHandler(new SimpleBotCommander()); |
| 58 | + await handler.handleCommand("simple-command abc def", {some: "context"}); |
| 59 | + expect(called).toEqual({ |
| 60 | + args: ["abc", "def"], |
| 61 | + request: { |
| 62 | + some: "context", |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + await handler.handleCommand("simple-command abc def ghi", {some: "context"}); |
| 67 | + expect(called).toEqual({ |
| 68 | + args: ["abc", "def", "ghi"], |
| 69 | + request: { |
| 70 | + some: "context", |
| 71 | + } |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it("to process a command and a subcommand", async () => { |
| 76 | + let commandCalled: any = undefined; |
| 77 | + let subCommandCalled: any = undefined; |
| 78 | + |
| 79 | + class SimpleBotCommander { |
| 80 | + @BotCommand({ help: "Some help", name: "simple-command", requiredArgs: ["foo"]}) |
| 81 | + public simpleCommand(data: CommandArguments<{some: string}>): void { |
| 82 | + commandCalled = data; |
| 83 | + } |
| 84 | + @BotCommand({ help: "Some help", name: "simple-command with-a-subcommand", requiredArgs: ["foo"]}) |
| 85 | + public simpleSubCommand(data: CommandArguments<{some: string}>): void { |
| 86 | + subCommandCalled = data; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + const handler = new BotCommandHandler(new SimpleBotCommander()); |
| 91 | + await handler.handleCommand("simple-command abc", undefined); |
| 92 | + expect(commandCalled).toEqual({ |
| 93 | + args: ["abc"], |
| 94 | + request: undefined, |
| 95 | + }); |
| 96 | + |
| 97 | + await handler.handleCommand("simple-command with-a-subcommand def", undefined); |
| 98 | + expect(subCommandCalled).toEqual({ |
| 99 | + args: ["def"], |
| 100 | + request: undefined, |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should produce useful help output", async () => { |
| 105 | + class SimpleBotCommander { |
| 106 | + @BotCommand({ help: "No help at all", name: "very-simple-command"}) |
| 107 | + public verySimpleCommand(data: CommandArguments<{some: string}>): void { |
| 108 | + } |
| 109 | + @BotCommand({ help: "Some help", name: "simple-command", requiredArgs: ["requiredArg1"]}) |
| 110 | + public simpleCommand(data: CommandArguments<{some: string}>): void { |
| 111 | + } |
| 112 | + @BotCommand({ help: "Even better help", name: "simple-command with-a-subcommand", requiredArgs: ["requiredArg1"], optionalArgs: ["optionalArg1"]}) |
| 113 | + public simpleSubCommand(data: CommandArguments<{some: string}>): void { |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + const handler = new BotCommandHandler(new SimpleBotCommander()); |
| 118 | + expect(handler.helpMessage.format).toEqual("org.matrix.custom.html"); |
| 119 | + expect(handler.helpMessage.msgtype).toEqual("m.notice"); |
| 120 | + expect(handler.helpMessage.body).toContain("Commands:"); |
| 121 | + // Rough formatting match |
| 122 | + expect(handler.helpMessage.body).toContain("- `very-simple-command` - No help at all"); |
| 123 | + expect(handler.helpMessage.body).toContain("- `simple-command` requiredArg1 - Some help"); |
| 124 | + expect(handler.helpMessage.body).toContain("- `simple-command with-a-subcommand` requiredArg1 [optionalArg1] - Even better help"); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
0 commit comments