Skip to content

Commit

Permalink
add test's for setMyCommands utils functions and fuzzyMatch regex com…
Browse files Browse the repository at this point in the history
…mands
  • Loading branch information
carafelix authored and roziscoding committed May 18, 2024
1 parent 7e4452f commit db51f08
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
88 changes: 88 additions & 0 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,92 @@ describe("Commands", () => {
);
});
});
describe("setMyCommands utils", () => {
describe("toSingleScopeArgs", () => {
it("should omit regex commands", () => {
const commands = new Commands();
commands.command("test", "handler1", (_) => _);
commands.command("test2", "handler2", (_) => _);
commands.command(/omitMe_\d\d/, "handler3", (_) => _);
const params = commands.toSingleScopeArgs({
type: "chat",
chat_id: 10,
});
assertEquals(params, [
{
scope: { type: "chat", chat_id: 10 },
language_code: undefined,
commands: [
{ command: "test", description: "handler1" },
{ command: "test2", description: "handler2" },
],
},
]);
});
it("should return an array with the localized versions of commands", () => {
const commands = new Commands();
commands.command("test", "handler1", (_) => _).localize(
"es",
"prueba1",
"resolvedor1",
);
commands.command("test2", "handler2", (_) => _);
commands.command(/omitMe_\d\d/, "handler3", (_) => _).localize(
"es",
/omiteme_\d/,
"resolvedor3",
);

const params = commands.toSingleScopeArgs({
type: "chat",
chat_id: 10,
});
assertEquals(params, [
{
scope: { type: "chat", chat_id: 10 },
language_code: undefined,
commands: [
{ command: "test", description: "handler1" },
{ command: "test2", description: "handler2" },
],
},
{
scope: {
chat_id: 10,
type: "chat",
},
language_code: "es",
commands: [
{
command: "prueba1",
description: "resolvedor1",
},
{
command: "test2",
description: "handler2",
},
],
},
]);
});
it("should omit commands with no handler", () => {
const commands = new Commands();
commands.command("test", "handler", (_) => _);
commands.command("omitme", "nohandler");
const params = commands.toSingleScopeArgs({
type: "chat",
chat_id: 10,
});
assertEquals(params, [
{
scope: { type: "chat", chat_id: 10 },
language_code: undefined,
commands: [
{ command: "test", description: "handler" },
],
},
]);
});
});
});
});
25 changes: 25 additions & 0 deletions test/jaroWrinkler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,30 @@ describe("Jaro-Wrinkler Algorithm", () => {

assertEquals(fuzzyMatch("xyz", cmds, {}), null);
});

it("should work for simple regex commands", () => {
const cmds = new Commands<Context>();
cmds.command(
/magical_\d/,
"Magical Command",
).addToScope(
{ type: "all_private_chats" },
(ctx) => ctx.reply(`Hello, ${ctx.chat.first_name}!`),
);
assertEquals(fuzzyMatch("magcal", cmds, {}), "magical_\\d");
});
it("should work for localized regex", () => {
const cmds = new Commands<Context>();
cmds.command(
/magical_(a|b)/,
"Magical Command",
).addToScope(
{ type: "all_private_chats" },
(ctx) => ctx.reply(`Hello, ${ctx.chat.first_name}!`),
).localize("es", /magico_(c|d)/, "Comando Mágico");

assertEquals(fuzzyMatch("magici_c", cmds, {}), "magico_(c|d)");
assertEquals(fuzzyMatch("magici_a", cmds, {}), "magical_(a|b)");
});
});
});

0 comments on commit db51f08

Please sign in to comment.