Skip to content

Commit

Permalink
add test's for command merging
Browse files Browse the repository at this point in the history
  • Loading branch information
carafelix committed May 20, 2024
1 parent 40c8c1d commit 28c101d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function commands<C extends Context>() {
chat_id: ctx.chat!.id,
}));

const mergedCommands = mergeMyCommandsParams(commandsParams);
const mergedCommands = _mergeMyCommandsParams(commandsParams);

await Promise.all(
mergedCommands
Expand Down Expand Up @@ -78,7 +78,7 @@ export function commands<C extends Context>() {
* @returns an array containing all commands to be set on ctx
*/

function mergeMyCommandsParams(
export function _mergeMyCommandsParams(
commandParams: SetMyCommandsParams[][],
): SetMyCommandsParams[] {
if (!commandParams.flat().length) return [];
Expand Down
104 changes: 104 additions & 0 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Commands } from "../src/commands.ts";
import { _mergeMyCommandsParams } from "../src/mod.ts";
import { assertEquals, describe, it } from "./deps.test.ts";

describe("Commands", () => {
Expand Down Expand Up @@ -130,5 +131,108 @@ describe("Commands", () => {
]);
});
});
describe("_mergeMyCommandsParams", () => {
it("should merge command's from different Commands instances", () => {
const a = new Commands();
a.command("a", "test a", (_) => _);
const b = new Commands();
b.command("b", "test b", (_) => _);
const c = new Commands();
c.command("c", "test c", (_) => _);

const allParams = [a, b, c].map((
commands,
) => commands.toSingleScopeArgs({
type: "chat",
chat_id: 10,
}));

const mergedCommands = _mergeMyCommandsParams(allParams);

assertEquals(mergedCommands, [
{
scope: { type: "chat", chat_id: 10 },
language_code: undefined,
commands: [
{ command: "c", description: "test c" },
{ command: "b", description: "test b" },
{ command: "a", description: "test a" },
],
},
]);
});
it("should merge for localized scopes", () => {
const a = new Commands();
a.command("a", "test a", (_) => _);
a.command("a1", "test a1", (_) => _).localize(
"es",
"localA1",
"prueba a1 localizada",
);
a.command("a2", "test a2", (_) => _).localize(
"fr",
"localiseA2",
"test a2 localisé",
);

const b = new Commands();
b.command("b", "test b", (_) => _)
.localize("es", "localB", "prueba b localizada")
.localize("fr", "localiseB", "prueba b localisé");

const allParams = [a, b].map((
commands,
) => commands.toSingleScopeArgs({
type: "chat",
chat_id: 10,
}));

const mergedCommands = _mergeMyCommandsParams(allParams);
assertEquals(mergedCommands, [
{
scope: { type: "chat", chat_id: 10 },
language_code: undefined,
commands: [
{ command: "b", description: "test b" },
{ command: "a", description: "test a" },
{ command: "a1", description: "test a1" },
{ command: "a2", description: "test a2" },
],
},
{
scope: { type: "chat", chat_id: 10 },
language_code: "es",
commands: [
{ command: "a", description: "test a" },
{
command: "localA1",
description: "prueba a1 localizada",
},
{ command: "a2", description: "test a2" },
{
command: "localB",
description: "prueba b localizada",
},
],
},
{
scope: { type: "chat", chat_id: 10 },
language_code: "fr",
commands: [
{ command: "a", description: "test a" },
{ command: "a1", description: "test a1" },
{
command: "localiseA2",
description: "test a2 localisé",
},
{
command: "localiseB",
description: "prueba b localisé",
},
],
},
]);
});
});
});
});

0 comments on commit 28c101d

Please sign in to comment.