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: allow getSessionKey to use extended ctx type #390

Merged
merged 8 commits into from
Mar 12, 2023
Prev Previous commit
Next Next commit
Revert "chore: test CI coverage tool"
This reverts commit be4e25a.
  • Loading branch information
KnorpelSenf committed Mar 12, 2023
commit 07fdf29e90be8c29fbe745dc920106f0501d4267
30 changes: 30 additions & 0 deletions test/composer.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Composer, Context } from "../src/mod.ts";

function _f<C extends Context>() {
const c = new Composer<C & { state: 1 }>();
c.use((ctx) => {
if (ctx.has(":contact")) {
ctx.msg.contact.phone_number;
ctx.state;
}
if (ctx.hasText("123")) {
ctx.match.includes;
}
if (ctx.hasCommand("123")) {
ctx.match.charCodeAt;
}
if (ctx.hasChatType("private")) {
ctx.chat.type;
}
if (ctx.hasGameQuery("123")) {
ctx.callbackQuery.game_short_name;
}
if (ctx.hasInlineQuery("123")) {
ctx.inlineQuery.id;
}
ctx.state;
});
c.command("c", (ctx) => {
ctx.match.charCodeAt;
});
}
71 changes: 71 additions & 0 deletions test/composer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Composer, type MiddlewareFn } from "../src/composer.ts";
import { type Context } from "../src/mod.ts";
import {
assertEquals,
assertRejects,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { type Spy, spy } from "https://deno.land/[email protected]/testing/mock.ts";
import {
beforeEach,
describe,
it,
} from "https://deno.land/[email protected]/testing/bdd.ts";

describe("Composer", () => {
let composer: Composer<Context>;
const ctx = { message: { text: "" } } as Context;
const next = () => Promise.resolve();
let middleware: Spy<MiddlewareFn<Context>>;

beforeEach(() => {
composer = new Composer();
middleware = spy((_ctx) => {});
});

it("should call handlers", async () => {
composer.use(middleware);
await composer.middleware()(ctx, next);
assertEquals(middleware.calls[0].args[0], ctx);
});

it("should call constructor handlers", async () => {
composer = new Composer(middleware);
await composer.middleware()(ctx, next);
assertEquals(middleware.calls[0].args[0], ctx);
});

it("should call multiple handlers", async () => {
composer.use((_, next) => next(), (_, next) => next(), middleware);
await composer.middleware()(ctx, next);
assertEquals(middleware.calls[0].args[0], ctx);
});

it("should call multiple handlers in different calls", async () => {
composer.use((_, next) => next(), (_, next) => next());
composer.use((_, next) => next(), (_, next) => next());
composer.use((_, next) => next(), (_, next) => next(), middleware);
await composer.middleware()(ctx, next);
assertEquals(middleware.calls[0].args[0], ctx);
});

it("should call sub-trees", async () => {
composer.use((_, next) => next())
.use((_, next) => next(), middleware);
await composer.middleware()(ctx, next);
assertEquals(middleware.calls[0].args[0], ctx);
});

it("should allow errors to bubble up", () => {
composer.use((_, next) => next())
.use((_, next) => next(), () => {
throw new Error("evil");
});
assertRejects(
async () => await composer.middleware()(ctx, next),
Error,
"evil",
);
});
});

// TODO: add tests for the filtering logic in all remaining composer methods
Loading