Skip to content

Commit

Permalink
test: cover prefixes and custom session keys
Browse files Browse the repository at this point in the history
  • Loading branch information
KnorpelSenf committed Nov 13, 2024
1 parent deb2a5a commit 3d39dcc
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions test/convenience/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,50 @@ describe("session", () => {
assertEquals(storage.delete.calls[0].args, ["42"]);
});

it("should work with custom session keys", async () => {
let val: number | undefined = 0;
const storage = {
read: spy((_key: string) => val),
write: spy((_key: string, value: number) => {
val = value;
}),
delete: spy((_key: string) => {
val = undefined;
}),
};
type C = Context & SessionFlavor<number>;
const composer = new Composer<C>();
let ctx = { chatId: 42 } as C;
composer.use(
session({
prefix: "xyz",
getSessionKey: (ctx) =>
ctx.chatId ? (ctx.chatId ** 2).toString() : undefined,
storage,
}),
)
.use((ctx) => {
if (ctx.session === 0) ctx.session = 1;
else ctx.session = null;
});

await composer.middleware()(ctx, next);
ctx = { chatId: 42 } as C;
await composer.middleware()(ctx, next);

assertEquals(storage.read.calls.length, 2);
assertEquals(storage.read.calls[0].args, ["xyz-1764"]);
assertEquals(storage.read.calls[1].args, ["xyz-1764"]);
assertEquals(storage.read.calls[0].returned, 0);
assertEquals(storage.read.calls[1].returned, 1);

assertEquals(storage.write.calls.length, 1);
assertEquals(storage.write.calls[0].args, ["xyz-1764", 1]);

assertEquals(storage.delete.calls.length, 1);
assertEquals(storage.delete.calls[0].args, ["xyz-1764"]);
});

it("should do IO with objects", async () => {
let val: Record<string, number> | undefined;
const storage = {
Expand Down

0 comments on commit 3d39dcc

Please sign in to comment.