Skip to content

Commit

Permalink
fix node tests for file storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed Oct 24, 2024
1 parent 4fc7a13 commit a6a0e8c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 80 deletions.
91 changes: 46 additions & 45 deletions packages/file/__tests__/deno.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
import { session } from 'grammy';
import { expect } from 'https://deno.land/x/[email protected]/mod.ts';
import { FileAdapter } from '../src/mod.ts';
import { createMessage, createBot } from '../../../libs/utils/src/mod.ts';
import { createBot, createMessage } from '@grammyjs/storage-utils';
import path from 'node:path';
import { existsSync } from 'node:fs';
import { rm as remove } from 'node:fs/promises';

const dirPath = path.resolve(Deno.cwd(), 'sessions');
const cleanDir = () => Deno.remove(dirPath, { recursive: true });
const cleanDir = () => remove(dirPath, { recursive: true });

Deno.test('Should create sessions dir', async () => {
new FileAdapter({ dirName: 'sessions' });
expect(existsSync(dirPath)).toBe(true);
new FileAdapter({ dirName: 'sessions' });
expect(existsSync(dirPath)).toBe(true);

await cleanDir();
await cleanDir();
});

Deno.test('Pizza counter tests', async () => {
const bot = createBot();

bot.use(session({
initial: () => ({ pizzaCount: 0 }),
storage: new FileAdapter({ dirName: 'sessions' }),
}));

bot.hears('first', (ctx) => {
expect(ctx.session.pizzaCount).toEqual(0);
ctx.session.pizzaCount = Number(ctx.session.pizzaCount) + 1;
});
bot.hears('second', (ctx) => {
expect(ctx.session.pizzaCount).toEqual(1);
});
await bot.handleUpdate(createMessage(bot, 'first').update);
await bot.handleUpdate(createMessage(bot, 'second').update);

await cleanDir();
const bot = createBot();

bot.use(session({
initial: () => ({ pizzaCount: 0 }),
storage: new FileAdapter({ dirName: 'sessions' }),
}));

bot.hears('first', (ctx) => {
expect(ctx.session.pizzaCount).toEqual(0);
ctx.session.pizzaCount = Number(ctx.session.pizzaCount) + 1;
});

bot.hears('second', (ctx) => {
expect(ctx.session.pizzaCount).toEqual(1);
});

await bot.handleUpdate(createMessage(bot, 'first').update);
await bot.handleUpdate(createMessage(bot, 'second').update);

await cleanDir();
});

Deno.test('Simple string tests', async () => {
const bot = createBot(false);

bot.use(session({
initial: () => 'test',
storage: new FileAdapter({ dirName: 'sessions' }),
}));

bot.hears('first', async (ctx) => {
ctx.session = `${ctx.session} edited`;
});
bot.hears('second', async (ctx) => {
expect(ctx.session).toEqual('test edited');
});
await bot.handleUpdate(createMessage(bot, 'first').update);
await bot.handleUpdate(createMessage(bot, 'second').update);

await cleanDir();
const bot = createBot(false);

bot.use(session({
initial: () => 'test',
storage: new FileAdapter({ dirName: 'sessions' }),
}));

bot.hears('first', async (ctx) => {
ctx.session = `${ctx.session} edited`;
});

bot.hears('second', async (ctx) => {
expect(ctx.session).toEqual('test edited');
});

await bot.handleUpdate(createMessage(bot, 'first').update);
await bot.handleUpdate(createMessage(bot, 'second').update);

await cleanDir();
});
69 changes: 35 additions & 34 deletions packages/file/__tests__/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,58 @@ import { existsSync } from 'node:fs';

const dirPath = resolve(cwd(), 'sessions');
const cleanDir = async () => {
await remove(dirPath).catch(() => null);
await remove(dirPath, { recursive: true });
};

test('Should create sessions dir', async () => {
await cleanDir();
await cleanDir();

new FileAdapter({ dirName: 'sessions' });
assert.ok(existsSync(dirPath));
new FileAdapter({ dirName: 'sessions' });
assert.ok(existsSync(dirPath));
});

test('Pizza counter tests', async () => {
await cleanDir();
await cleanDir();

const bot = createBot();
const bot = createBot();

bot.use(session({
initial: () => ({ pizzaCount: 0 }),
storage: new FileAdapter({ dirName: 'sessions' }),
}));
bot.use(session({
initial: () => ({ pizzaCount: 0 }),
storage: new FileAdapter({ dirName: 'sessions' }),
}));

bot.hears('first', (ctx) => {
assert.equal(ctx.session.pizzaCount, 0);
ctx.session.pizzaCount = Number(ctx.session.pizzaCount) + 1;
});
bot.hears('first', (ctx) => {
console.log('session', ctx.session);
assert.equal(ctx.session.pizzaCount, 0);
ctx.session.pizzaCount = ctx.session.pizzaCount + 1;
});

bot.hears('second', (ctx) => {
assert.equal(ctx.session.pizzaCount, 1);
});
bot.hears('second', (ctx) => {
assert.equal(ctx.session.pizzaCount, 1);
});

await bot.handleUpdate(createMessage(bot, 'first').update);
await bot.handleUpdate(createMessage(bot, 'second').update);
await bot.handleUpdate(createMessage(bot, 'first').update);
await bot.handleUpdate(createMessage(bot, 'second').update);
});

test('Simple string tests', async () => {
await cleanDir();
await cleanDir();

const bot = createBot(false);
const bot = createBot(false);

bot.use(session({
initial: () => 'test',
storage: new FileAdapter({ dirName: 'sessions' }),
}));
bot.use(session({
initial: () => 'test',
storage: new FileAdapter({ dirName: 'sessions' }),
}));

bot.hears('first', async (ctx) => {
ctx.session = `${ctx.session} edited`;
});
bot.hears('first', (ctx) => {
ctx.session = `${ctx.session} edited`;
});

bot.hears('second', async (ctx) => {
assert.equal(ctx.session, 'test edited');
});
bot.hears('second', (ctx) => {
assert.equal(ctx.session, 'test edited');
});

await bot.handleUpdate(createMessage(bot, 'first').update);
await bot.handleUpdate(createMessage(bot, 'second').update);
});
await bot.handleUpdate(createMessage(bot, 'first').update);
await bot.handleUpdate(createMessage(bot, 'second').update);
});
3 changes: 3 additions & 0 deletions packages/file/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "@grammyjs/storage-file",
"exports": "./src/mod.ts",
"tasks": {
"test": "deno test --allow-import --allow-write --no-check --allow-read --unstable ./__tests__/deno.ts"
},
"imports": {
"grammy": "https://lib.deno.dev/x/[email protected]/mod.ts"
}
Expand Down
1 change: 0 additions & 1 deletion packages/file/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
],
"scripts": {
"test": "tsx __tests__/node.ts",
"test:deno": "deno test --allow-import --allow-write --no-check --allow-read --unstable ./__tests__/deno.ts",
"prebuild": "rimraf dist",
"build": "tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json && pnpm postbuild",
"postbuild": "tsx ../../tools/postBuildFixup.ts --path=dist",
Expand Down

0 comments on commit a6a0e8c

Please sign in to comment.