From 28c101d38157c26ba7f9c0dface538f8d13e21cc Mon Sep 17 00:00:00 2001 From: Hero Protagonist Date: Mon, 20 May 2024 15:33:19 -0400 Subject: [PATCH] add test's for command merging --- src/context.ts | 4 +- test/commands.test.ts | 104 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 2 deletions(-) diff --git a/src/context.ts b/src/context.ts index eea0829..7c0fc92 100644 --- a/src/context.ts +++ b/src/context.ts @@ -50,7 +50,7 @@ export function commands() { chat_id: ctx.chat!.id, })); - const mergedCommands = mergeMyCommandsParams(commandsParams); + const mergedCommands = _mergeMyCommandsParams(commandsParams); await Promise.all( mergedCommands @@ -78,7 +78,7 @@ export function commands() { * @returns an array containing all commands to be set on ctx */ -function mergeMyCommandsParams( +export function _mergeMyCommandsParams( commandParams: SetMyCommandsParams[][], ): SetMyCommandsParams[] { if (!commandParams.flat().length) return []; diff --git a/test/commands.test.ts b/test/commands.test.ts index 47ffe88..1b220eb 100644 --- a/test/commands.test.ts +++ b/test/commands.test.ts @@ -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", () => { @@ -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é", + }, + ], + }, + ]); + }); + }); }); });