File size: 5,551 Bytes
eb3f11e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | import { Command } from "commander";
import { describe, expect, it, vi } from "vitest";
import { createMessageCliHelpers } from "./helpers.js";
import { registerMessageDiscordAdminCommands } from "./register.discord-admin.js";
type AdminCase = {
command: string;
action: string;
required: Record<string, string>;
optional?: Record<string, string>;
};
const cases: AdminCase[] = [
{ command: "role info", action: "role-info", required: { guildId: "guild-1" } },
{
command: "role add",
action: "role-add",
required: { guildId: "guild-1", userId: "user-1", roleId: "role-1" },
},
{
command: "role remove",
action: "role-remove",
required: { guildId: "guild-1", userId: "user-1", roleId: "role-1" },
},
{ command: "channel info", action: "channel-info", required: { target: "channel:123" } },
{ command: "channel list", action: "channel-list", required: { guildId: "guild-1" } },
{
command: "member info",
action: "member-info",
required: { userId: "user-1" },
optional: { guildId: "guild-1", channelId: "channel-1" },
},
{
command: "voice status",
action: "voice-status",
required: { guildId: "guild-1", userId: "user-1" },
},
{ command: "event list", action: "event-list", required: { guildId: "guild-1" } },
{
command: "event create",
action: "event-create",
required: { guildId: "guild-1", eventName: "QA event", startTime: "2026-09-11T12:00:00Z" },
optional: {
endTime: "2026-09-11T13:00:00Z",
desc: "Event description",
channelId: "channel-1",
location: "QA room",
eventType: "external",
image: "https://example.com/event.png",
},
},
{
command: "timeout",
action: "timeout",
required: { guildId: "guild-1", userId: "user-1" },
optional: { durationMin: "0", until: "2026-09-11T13:00:00Z", reason: "QA reason" },
},
{
command: "kick",
action: "kick",
required: { guildId: "guild-1", userId: "user-1" },
optional: { reason: "QA reason" },
},
{
command: "ban",
action: "ban",
required: { guildId: "guild-1", userId: "user-1" },
optional: { reason: "QA reason", deleteDays: "0" },
},
];
function flag(key: string) {
return "--" + key.replace(/[A-Z]/g, (letter) => "-" + letter.toLowerCase());
}
function argumentsFor(options: Record<string, string>) {
return Object.entries(options).flatMap(([key, value]) => [flag(key), value]);
}
function setup() {
const command = new Command()
.name("message")
.exitOverride()
.configureOutput({ writeErr() {}, writeOut() {} });
const runMessageAction = vi.fn(async () => {});
registerMessageDiscordAdminCommands(command, {
...createMessageCliHelpers("discord|matrix|msteams|slack"),
runMessageAction,
});
return { command, runMessageAction };
}
function leaf(command: Command, path: string) {
let current = command;
for (const name of path.split(" ")) {
const next = current.commands.find((candidate) => candidate.name() === name);
if (!next) {
throw new Error("Missing command " + path);
}
current = next;
}
return current;
}
describe("Discord-admin message registration", () => {
it.each(cases)(
"$command forwards the exact action, defaults and options",
async ({ command: path, action, required, optional = {} }) => {
const { command, runMessageAction } = setup();
await command.parseAsync(
[
...path.split(" "),
...argumentsFor(required),
...argumentsFor(optional),
"--channel",
"discord",
],
{ from: "user" },
);
expect(runMessageAction).toHaveBeenCalledExactlyOnceWith(action, {
json: false,
dryRun: false,
verbose: false,
...required,
...optional,
channel: "discord",
});
},
);
it.each(cases)(
"$command rejects every missing mandatory identifier before dispatch",
async ({ command: path, required }) => {
for (const missing of Object.keys(required)) {
const { command, runMessageAction } = setup();
const remaining = Object.fromEntries(
Object.entries(required).filter(([key]) => key !== missing),
);
await expect(
command.parseAsync([...path.split(" "), ...argumentsFor(remaining)], { from: "user" }),
).rejects.toMatchObject({ code: "commander.missingMandatoryOptionValue" });
expect(runMessageAction).not.toHaveBeenCalled();
}
},
);
it.each(cases)(
"$command keeps required, shared and leaf-only flags in help order",
({ command: path, required, optional = {} }) => {
const { command } = setup();
expect(leaf(command, path).options.map((option) => option.long)).toEqual([
...Object.keys(required).map(flag),
"--channel",
"--account",
"--json",
"--dry-run",
"--verbose",
...Object.keys(optional).map(flag),
]);
},
);
it.each([undefined, "discord", "slack"])(
"keeps guild and conversation optional for member info on %s",
async (channel) => {
const { command, runMessageAction } = setup();
const options = { userId: "user-1", ...(channel ? { channel } : {}) };
await command.parseAsync(["member", "info", ...argumentsFor(options)], { from: "user" });
expect(runMessageAction).toHaveBeenCalledExactlyOnceWith("member-info", {
json: false,
dryRun: false,
verbose: false,
...options,
});
},
);
});
|