-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
84 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters