Skip to content

Commit 43e5173

Browse files
committed
test: make hierarchical assertions order-insensitive
1 parent 090fa44 commit 43e5173

2 files changed

Lines changed: 54 additions & 23 deletions

File tree

packages/commandkit/src/app/handlers/AppCommandHandler.hierarchical.test.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,20 @@ export async function message() {}
184184
handler.getCommandsArray().map((command) => command.command.name),
185185
).toEqual(['ping']);
186186

187-
expect(
188-
handler.getRuntimeCommandsArray().map((command) => {
187+
const runtimeRouteKeys = handler
188+
.getRuntimeCommandsArray()
189+
.map((command) => {
189190
return (command.data.command as Record<string, any>).__routeKey;
190-
}),
191-
).toEqual(['ping', 'admin.moderation.ban', 'admin.moderation.kick']);
191+
});
192+
193+
expect(runtimeRouteKeys).toHaveLength(3);
194+
expect(runtimeRouteKeys).toEqual(
195+
expect.arrayContaining([
196+
'ping',
197+
'admin.moderation.ban',
198+
'admin.moderation.kick',
199+
]),
200+
);
192201

193202
const preparedFlat = await handler.prepareCommandRun(
194203
createChatInputInteraction('ping'),

packages/commandkit/src/app/register/CommandRegistrar.hierarchical.test.ts

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,34 +116,56 @@ describe('Hierarchical command registration', () => {
116116
const admin = registrationCommands.find(
117117
(entry) => entry.name === 'admin',
118118
);
119+
const moderationGroup = admin?.options?.[0] as
120+
| {
121+
name: string;
122+
options?: Array<{
123+
name: string;
124+
description: string;
125+
options: Array<{
126+
name: string;
127+
type: ApplicationCommandOptionType;
128+
}>;
129+
type: ApplicationCommandOptionType;
130+
}>;
131+
type: ApplicationCommandOptionType;
132+
}
133+
| undefined;
119134

120135
expect(ping?.type).toBe(ApplicationCommandType.ChatInput);
121136
expect(admin?.type).toBe(ApplicationCommandType.ChatInput);
122137
expect(admin?.description).toBe('Admin');
123-
expect(admin?.options).toEqual([
138+
expect(moderationGroup?.name).toBe('moderation');
139+
expect(moderationGroup?.type).toBe(
140+
ApplicationCommandOptionType.SubcommandGroup,
141+
);
142+
143+
const normalizedSubcommands = [...(moderationGroup?.options ?? [])]
144+
.map((option) => ({
145+
...option,
146+
options: [...(option.options ?? [])].sort((a, b) =>
147+
a.name.localeCompare(b.name),
148+
),
149+
}))
150+
.sort((a, b) => a.name.localeCompare(b.name));
151+
152+
expect(normalizedSubcommands).toEqual([
124153
{
125-
description: 'Moderation',
126-
name: 'moderation',
154+
description: 'Ban',
155+
name: 'ban',
127156
options: [
128157
{
129-
description: 'Ban',
130-
name: 'ban',
131-
options: [
132-
{
133-
name: 'reason',
134-
type: ApplicationCommandOptionType.String,
135-
},
136-
],
137-
type: ApplicationCommandOptionType.Subcommand,
138-
},
139-
{
140-
description: 'Kick',
141-
name: 'kick',
142-
options: [],
143-
type: ApplicationCommandOptionType.Subcommand,
158+
name: 'reason',
159+
type: ApplicationCommandOptionType.String,
144160
},
145161
],
146-
type: ApplicationCommandOptionType.SubcommandGroup,
162+
type: ApplicationCommandOptionType.Subcommand,
163+
},
164+
{
165+
description: 'Kick',
166+
name: 'kick',
167+
options: [],
168+
type: ApplicationCommandOptionType.Subcommand,
147169
},
148170
]);
149171

0 commit comments

Comments
 (0)