Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor all regular expressions into super-expressive #63

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
14 changes: 7 additions & 7 deletions test/command-group.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommandGroup } from "../src/command-group.ts";
import { MyCommandParams } from "../src/mod.ts";
import { dummyCtx } from "./context.test.ts";
import { getDummyCtx } from "./utils.ts";
import {
assert,
assertEquals,
Expand Down Expand Up @@ -54,7 +54,7 @@ describe("CommandGroup", () => {
});
describe("setMyCommands", () => {
it("should throw if the update has no chat property", () => {
const ctx = dummyCtx({ noMessage: true });
const ctx = getDummyCtx({ noMessage: true });
const a = new CommandGroup();
assertRejects(() => ctx.setMyCommands(a));
});
Expand Down Expand Up @@ -294,7 +294,7 @@ describe("CommandGroup", () => {

it("should only consider as entities prefixes registered in the command instance", () => {
const text = "/papi hola papacito como estamos /papi /ecco";
let ctx = dummyCtx({
let ctx = getDummyCtx({
userInput: text,
});
const entities = ctx.getCommandEntities(a);
Expand All @@ -310,7 +310,7 @@ describe("CommandGroup", () => {
}
});
it("should get command entities for custom prefixes", () => {
let ctx = dummyCtx({
let ctx = getDummyCtx({
userInput: "/hi ?momi abcdfghi",
});
const entities = ctx.getCommandEntities(a);
Expand Down Expand Up @@ -339,15 +339,15 @@ describe("CommandGroup", () => {
]);
});
it("should throw if you call getCommandEntities on an update with no text", () => {
const ctx = dummyCtx({});
const ctx = getDummyCtx({});
assertThrows(() => ctx.getCommandEntities([a, b, c]));
});
it("should return an empty array if the Commands classes to check against do not have any command register", () => {
const ctx = dummyCtx({ userInput: "/papi" });
const ctx = getDummyCtx({ userInput: "/papi" });
assertEquals(ctx.getCommandEntities(c), []);
});
it("should work across multiple Commands instances", () => {
const ctx = dummyCtx({ userInput: "/papi superprefixmami" });
const ctx = getDummyCtx({ userInput: "/papi superprefixmami" });
assertEquals(
ctx.getCommandEntities([a, b]).map((entity) => entity.prefix),
["/", "superprefix"],
Expand Down
52 changes: 6 additions & 46 deletions test/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
import {
resolvesNext,
spy,
} from "https://deno.land/std@0.203.0/testing/mock.ts";
import { commands, type CommandsFlavor } from "../src/mod.ts";
import {
Api,
assert,
assertRejects,
Chat,
Context,
describe,
it,
Message,
Update,
User,
UserFromGetMe,
} from "./deps.test.ts";
import { commands } from "../src/mod.ts";
import { assert, assertRejects, describe, it } from "./deps.test.ts";
import { getDummyCtx } from "./utils.ts";

describe("commands", () => {
it("should install the setMyCommands method on the context", () => {
const context = dummyCtx({});
const context = getDummyCtx({});

const middleware = commands();
middleware(context, async () => {});

assert(context.setMyCommands);
});
it("should install the getNearestCommand method on the context", () => {
const context = dummyCtx({});
const context = getDummyCtx({});

const middleware = commands();
middleware(context, async () => {});
Expand All @@ -37,7 +22,7 @@ describe("commands", () => {

describe("setMyCommands", () => {
it("should throw an error if there is no chat", async () => {
const context = dummyCtx({ noMessage: true });
const context = getDummyCtx({ noMessage: true });

const middleware = commands();
middleware(context, async () => {});
Expand All @@ -50,28 +35,3 @@ describe("commands", () => {
});
});
});

export function dummyCtx({ userInput, language, noMessage }: {
userInput?: string;
language?: string;
noMessage?: boolean;
}) {
const u = { id: 42, first_name: "yo", language_code: language } as User;
const c = { id: 100, type: "private" } as Chat;
const m = noMessage ? undefined : ({
text: userInput,
from: u,
chat: c,
} as Message);
const update = {
message: m,
} as Update;
const api = {
raw: { setMyCommands: spy(resolvesNext([true] as const)) },
} as unknown as Api;
const me = { id: 42, username: "bot" } as UserFromGetMe;
const ctx = new Context(update, api, me) as CommandsFlavor<Context>;
const middleware = commands();
middleware(ctx, async () => {});
return ctx;
}
44 changes: 2 additions & 42 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,16 @@ import {
resolvesNext,
} from "https://deno.land/std@0.203.0/testing/mock.ts";
import { CommandGroup } from "../src/command-group.ts";
import { Bot } from "../src/deps.deno.ts";
import { Command, commands, CommandsFlavor } from "../src/mod.ts";
import { Command, commands } from "../src/mod.ts";
import {
Api,
assertRejects,
assertSpyCall,
Chat,
Context,
describe,
it,
Message,
spy,
Update,
User,
} from "./deps.test.ts";

const getBot = () =>
new Bot<Context & CommandsFlavor>("dummy_token", {
botInfo: {
id: 1,
is_bot: true,
username: "",
can_join_groups: true,
can_read_all_group_messages: true,
supports_inline_queries: true,
first_name: "",
can_connect_to_business: true,
has_main_web_app: false,
},
});

const getDummyUpdate = ({ userInput, language, noChat, chatType = "private" }: {
userInput?: string;
language?: string;
noChat?: boolean;
chatType?: Chat["type"];
} = {}) => {
const u = { id: 42, first_name: "yo", language_code: language } as User;
const c = { id: 100, type: chatType } as Chat;
const m = {
text: userInput,
from: u,
chat: noChat ? undefined : c,
} as Message;
const update = {
message: m,
} as Update;

return update;
};
import { getBot, getDummyUpdate } from "./utils.ts";

describe("Integration", () => {
describe("setCommands", () => {
Expand Down
42 changes: 21 additions & 21 deletions test/jaroWrinkler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
JaroWinklerDistance,
} from "../src/utils/jaro-winkler.ts";
import { CommandGroup } from "../src/mod.ts";
import { dummyCtx } from "./context.test.ts";
import { getDummyCtx } from "./utils.ts";
import {
assertEquals,
assertThrows,
Expand Down Expand Up @@ -344,13 +344,13 @@ describe("Jaro-Wrinkler Algorithm", () => {
cmds.command("entitle", "_", () => {});

it("should throw when no msg is given", () => {
let ctx = dummyCtx({});
let ctx = getDummyCtx({});
assertThrows(() => ctx.getNearestCommand(cmds));
});

describe("should ignore localization when set to, and search trough all commands", () => {
it("ignore even if the language is set", () => { // should this console.warn? or maybe use an overload?
let ctx = dummyCtx({
let ctx = getDummyCtx({
userInput: "/duci",
language: "es",
});
Expand All @@ -360,7 +360,7 @@ describe("Jaro-Wrinkler Algorithm", () => {
}),
"/duc",
);
ctx = dummyCtx({
ctx = getDummyCtx({
userInput: "/duki",
language: "es",
});
Expand All @@ -372,31 +372,31 @@ describe("Jaro-Wrinkler Algorithm", () => {
);
});
it("ignore when the language is not set", () => {
let ctx = dummyCtx({
let ctx = getDummyCtx({
userInput: "/duki",
language: "es",
});
assertEquals(
ctx.getNearestCommand(cmds, { ignoreLocalization: true }),
"/duke",
);
ctx = dummyCtx({
ctx = getDummyCtx({
userInput: "/macellaoo",
language: "es",
});
assertEquals(
ctx.getNearestCommand(cmds, { ignoreLocalization: true }),
"+macellaio",
);
ctx = dummyCtx({
ctx = getDummyCtx({
userInput: "/dadd",
language: "es",
});
assertEquals(
ctx.getNearestCommand(cmds, { ignoreLocalization: true }),
"?daddy",
);
ctx = dummyCtx({
ctx = getDummyCtx({
userInput: "/duk",
language: "es",
});
Expand All @@ -406,7 +406,7 @@ describe("Jaro-Wrinkler Algorithm", () => {
);
});
it("should not restrict itself to default", () => {
let ctx = dummyCtx({
let ctx = getDummyCtx({
userInput: "/duqu",
language: "es",
});
Expand All @@ -416,7 +416,7 @@ describe("Jaro-Wrinkler Algorithm", () => {
);
});
it("language not know, but ignore localization still matches the best similarity", () => {
let ctx = dummyCtx({
let ctx = getDummyCtx({
userInput: "/duqu",
language: "en-papacito",
});
Expand All @@ -426,15 +426,15 @@ describe("Jaro-Wrinkler Algorithm", () => {
);
});
it("should chose localization if not ignore", () => {
let ctx = dummyCtx({
let ctx = getDummyCtx({
userInput: "/duku",
language: "es",
});
assertEquals(
ctx.getNearestCommand(cmds),
"/duque",
);
ctx = dummyCtx({
ctx = getDummyCtx({
userInput: "/duk",
language: "fr",
});
Expand All @@ -446,12 +446,12 @@ describe("Jaro-Wrinkler Algorithm", () => {
});
describe("should not fail even if the language it's not know", () => {
it("should fallback to default", () => {
let ctx = dummyCtx({
let ctx = getDummyCtx({
userInput: "/duko",
language: "en-papacito",
});
assertEquals(ctx.getNearestCommand(cmds), "/duke");
ctx = dummyCtx({
ctx = getDummyCtx({
userInput: "/butxher",
language: "no-language",
});
Expand All @@ -460,21 +460,21 @@ describe("Jaro-Wrinkler Algorithm", () => {
});
describe("should work for commands with no localization, even when the language is set", () => {
it("ender", () => {
let ctx = dummyCtx({
let ctx = getDummyCtx({
userInput: "/endr",
language: "es",
});
assertEquals(ctx.getNearestCommand(cmds), "/ender");
});
it("endanger", () => {
let ctx = dummyCtx({
let ctx = getDummyCtx({
userInput: "/enanger",
language: "en",
});
assertEquals(ctx.getNearestCommand(cmds), "/endanger");
});
it("entitle", () => {
let ctx = dummyCtx({
let ctx = getDummyCtx({
userInput: "/entities",
language: "pt",
});
Expand All @@ -495,19 +495,19 @@ describe("Jaro-Wrinkler Algorithm", () => {
.localize("fr", "pere", "_");

it("should get the nearest between multiple command classes", () => {
let ctx = dummyCtx({
let ctx = getDummyCtx({
userInput: "/papi",
language: "es",
});
assertEquals(ctx.getNearestCommand([cmds, cmds2]), "/papa");
ctx = dummyCtx({
ctx = getDummyCtx({
userInput: "/pai",
language: "fr",
});
assertEquals(ctx.getNearestCommand([cmds, cmds2]), "/pain");
});
it("Without localization it should get the best between multiple command classes", () => {
let ctx = dummyCtx({
let ctx = getDummyCtx({
userInput: "/pana",
language: "???",
});
Expand All @@ -517,7 +517,7 @@ describe("Jaro-Wrinkler Algorithm", () => {
}),
"/pan",
);
ctx = dummyCtx({
ctx = getDummyCtx({
userInput: "/para",
language: "???",
});
Expand Down
Loading
Loading