Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gc committed Mar 10, 2024
1 parent 8693312 commit c698c77
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions tests/integration/allCommandsBase.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApplicationCommandOptionType } from 'discord.js';
import { randInt } from 'e';
import { randInt, shuffleArr } from 'e';
import { CommandOption } from 'mahoji/dist/lib/types';
import { test, vi } from 'vitest';

Expand Down Expand Up @@ -40,7 +40,18 @@ import { smeltingCommand } from '../../src/mahoji/commands/smelt';
import { stealCommand } from '../../src/mahoji/commands/steal';
import { toolsCommand } from '../../src/mahoji/commands/tools';
import { randomMock } from './setup';
import { createTestUser } from './util';
import { createTestUser, TestUser } from './util';

// Don't let any of these commands create an activity
vi.mock('../../../src/lib/util/addSubTaskToActivityTask', async () => {
const actual: any = await vi.importActual('../../../src/lib/util/addSubTaskToActivityTask');
return {
...actual,
default: async (args: any) => {
console.log(`Sending ${args}`);
}
};
});

const commands = [
activitiesCommand,
Expand Down Expand Up @@ -83,21 +94,33 @@ const commands = [
];

type CommandInput = Record<string, any>;
function generateCommandInputs(options: CommandOption[], currentPath: CommandInput = {}): CommandInput[] {
async function generateCommandInputs(
user: TestUser,
options: CommandOption[],
currentPath: CommandInput = {}
): Promise<CommandInput[]> {
let results: CommandInput[] = [];

for (const option of options) {
switch (option.type) {
case ApplicationCommandOptionType.SubcommandGroup:
case ApplicationCommandOptionType.Subcommand:
if (option.options) {
const subOptionsResults = generateCommandInputs(option.options);
const subOptionsResults = await generateCommandInputs(user, option.options);
subOptionsResults.forEach(subResult => {
results.push({ [option.name]: subResult });
});
}
break;
case ApplicationCommandOptionType.String:
if ('autocomplete' in option && option.autocomplete) {
const autoCompleteResults = await option.autocomplete('', { id: user.id } as any, {} as any);
shuffleArr(autoCompleteResults)
.slice(0, 5)
.forEach(result => {
results.push({ ...currentPath, [option.name]: result.value as string });
});
}
if (option.choices) {
option.choices.forEach(choice => {
results.push({ ...currentPath, [option.name]: choice.value });
Expand Down Expand Up @@ -135,26 +158,17 @@ function generateCommandInputs(options: CommandOption[], currentPath: CommandInp
return results;
}

// Don't let any of these commands create an activity
vi.mock('../../src/lib/util/addSubTaskToActivityTask', async () => {
const actual: any = await vi.importActual('../../src/lib/util/addSubTaskToActivityTask');
return {
...actual,
default: async (args: any) => {
console.log(`Sending ${args}`);
}
};
});

test('All Commands Base Test', async () => {
randomMock();
const user = await createTestUser();
for (const command of commands) {
const options = generateCommandInputs(command.options!);
const options = await generateCommandInputs(user, command.options!);
for (const option of options) {
try {
await user.runCommand(command, option);
console.log(`Ran command ${command.name}`);
const res = await user.runCommand(command, option);
console.log(`Ran command ${command.name}
Options: ${JSON.stringify(option)}
Result: ${res}`);
} catch (err) {
console.error(`Failed to run command ${command.name} with options ${JSON.stringify(option)}`);
}
Expand Down

0 comments on commit c698c77

Please sign in to comment.