Skip to content

Commit

Permalink
Some tests fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gpont committed Aug 17, 2024
1 parent 18cf3b3 commit 9447a7b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 56 deletions.
122 changes: 66 additions & 56 deletions src/controllers/bot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('Bot Commands', () => {
chat: { id: createChat() },
text: '/start',
from: { username: 'test' },
} as unknown as TelegramBot.Message;
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -85,7 +85,7 @@ describe('Bot Commands', () => {
const msg = {
chat: { id: createChat() },
text: '/help',
} as unknown as TelegramBot.Message;
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -100,34 +100,34 @@ describe('Bot Commands', () => {
it('should create a new group', async () => {
const sendMessage = jest.spyOn(bot, 'sendMessage');
const chatId = createChat();
const user = await createUser(String(chatId));
const msg = {
chat: { id: chatId },
text: '/create_group',
user: { username: String(chatId) },
} as unknown as TelegramBot.Message;
const user = await createUser(String(chatId));
from: { username: user?.username },
} as TelegramBot.Message;

await emitMsg(msg);

const group = await findGroupByUserId(user?.id ?? 0);
expect(group).not.toBeNull();
expect(sendMessage).toHaveBeenCalledWith(
msg.chat.id,
expect.stringContaining(
`Группа создана! Код для присоединения: ${msg.chat.id}`,
),
expect.stringContaining(`Группа создана! Код для присоединения:`),
);
});

it('should error with duplicate group code', async () => {
const chatId = createChat();
await createGroup();
it('should error with existing group', async () => {
const sendMessage = jest.spyOn(bot, 'sendMessage');
const chatId = createChat();
const user = await createUser(String(chatId));
const group = await createGroup();
await addUserToGroup(group.id, user?.id ?? 0);
const msg = {
chat: { id: chatId },
text: '/create_group',
from: { username: 'test' },
} as unknown as TelegramBot.Message;
from: { username: user?.username },
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -148,7 +148,7 @@ describe('Bot Commands', () => {
chat: { id: chatId },
text: `/join_group ${group.code}`,
from: { username: user?.username },
} as unknown as TelegramBot.Message;
} as TelegramBot.Message;
await addUserToGroup(group.id, user?.id ?? 0);

await emitMsg(msg);
Expand All @@ -167,7 +167,7 @@ describe('Bot Commands', () => {
chat: { id: createChat() },
text: '/join_group ',
from: { username: user?.username },
} as unknown as TelegramBot.Message;
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -179,11 +179,12 @@ describe('Bot Commands', () => {

it('should error without username', async () => {
const sendMessage = jest.spyOn(bot, 'sendMessage');
const group = await createGroup();
const msg = {
chat: { id: createChat() },
text: '/join_group 1',
text: `/join_group ${group.code}`,
from: {},
} as unknown as TelegramBot.Message;
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -201,7 +202,7 @@ describe('Bot Commands', () => {
chat: { id: chatId },
text: '/join_group 9999',
from: { username: user?.username },
} as unknown as TelegramBot.Message;
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -223,7 +224,7 @@ describe('Bot Commands', () => {
chat: { id: chatId },
text: `/leave_group`,
from: { username: user?.username },
} as unknown as TelegramBot.Message;
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -240,14 +241,15 @@ describe('Bot Commands', () => {
describe('suggest', () => {
it('should suggest a movie', async () => {
const sendMessage = jest.spyOn(bot, 'sendMessage');
const chatId = createChat();
const group = await createGroup();
const user = await createUser(String(chatId));
await addUserToGroup(group.id, user?.id ?? 0);
const msg = {
chat: { id: createChat() },
chat: { id: chatId },
text: '/suggest Inception',
from: { username: user?.username ?? 0 },
} as unknown as TelegramBot.Message;
const group = await createGroup();
await addUserToGroup(group.id, user?.id ?? 0);
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -265,7 +267,7 @@ describe('Bot Commands', () => {
const msg = {
chat: { id: createChat() },
text: '/suggest Inception',
} as unknown as TelegramBot.Message;
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -282,7 +284,7 @@ describe('Bot Commands', () => {
chat: { id: createChat() },
text: '/suggest ',
from: { username: user?.username ?? 0 },
} as unknown as TelegramBot.Message;
} as TelegramBot.Message;
const group = await createGroup();
await addUserToGroup(group.id, user?.id ?? 0);

Expand All @@ -305,7 +307,7 @@ describe('Bot Commands', () => {
chat: { id: createChat() },
text: '/vote 1',
from: { username: user?.username ?? '' },
} as unknown as TelegramBot.Message;
} as TelegramBot.Message;
const group = await createGroup();
await addUserToGroup(group.id, user?.id ?? 0);
const insertedMovie = await suggestMovie(
Expand Down Expand Up @@ -341,7 +343,7 @@ describe('Bot Commands', () => {
chat: { id: chatId },
text: '/vote 1',
from: { username: user?.username ?? '' },
} as unknown as TelegramBot.Message;
} as TelegramBot.Message;
const insertedMovie = await suggestMovie(
'Inception',
user?.id ?? 0,
Expand Down Expand Up @@ -375,7 +377,7 @@ describe('Bot Commands', () => {
chat: { id: chatId },
text: '/vote',
from: { username: user?.username ?? '' },
} as unknown as TelegramBot.Message;
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -395,7 +397,7 @@ describe('Bot Commands', () => {
chat: { id: chatId },
text: '/vote qwe',
from: { username: user?.username ?? '' },
} as unknown as TelegramBot.Message;
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -409,13 +411,15 @@ describe('Bot Commands', () => {
describe('list', () => {
it('should print current list of movies', async () => {
const sendMessage = jest.spyOn(bot, 'sendMessage');
const msg = {
chat: { id: createChat() },
text: '/list',
} as unknown as TelegramBot.Message;
const chatId = createChat();
const group = await createGroup();
const user = await createUser(String(chatId));
await addUserToGroup(group.id, user?.id ?? 0);
const msg = {
chat: { id: chatId },
text: '/list',
from: { username: user?.username ?? '' },
} as TelegramBot.Message;
const movie = await suggestMovie(
'Inception',
user?.id ?? 0,
Expand All @@ -434,11 +438,13 @@ describe('Bot Commands', () => {

it('should error if user not in group', async () => {
const sendMessage = jest.spyOn(bot, 'sendMessage');
const chatId = createChat();
const user = await createUser(String(chatId));
const msg = {
chat: { id: createChat() },
text: '/list',
} as unknown as TelegramBot.Message;
await createUser(String(msg.chat.id));
from: { username: user?.username ?? '' },
} as TelegramBot.Message;

await emitMsg(msg);
expect(sendMessage).toHaveBeenCalledWith(
Expand All @@ -457,8 +463,8 @@ describe('Bot Commands', () => {
const msg = {
chat: { id: chatId },
text: '/watched 1',
from: { id: user?.id ?? 0 },
} as unknown as TelegramBot.Message;
from: { username: user?.username ?? '' },
} as TelegramBot.Message;
await addUserToGroup(group.id, user?.id ?? 0);
const insertedMovie = await suggestMovie(
'Inception',
Expand Down Expand Up @@ -488,7 +494,7 @@ describe('Bot Commands', () => {
const msg = {
chat: { id: createChat() },
text: '/watched ',
} as unknown as TelegramBot.Message;
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -500,10 +506,13 @@ describe('Bot Commands', () => {

it('should error if user not in group', async () => {
const sendMessage = jest.spyOn(bot, 'sendMessage');
const chatId = createChat();
const user = await createUser(String(chatId));
const msg = {
chat: { id: createChat() },
text: '/watched 9998',
} as unknown as TelegramBot.Message;
from: { username: user?.username ?? '' },
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -516,13 +525,14 @@ describe('Bot Commands', () => {
it('should error if wrong movie code', async () => {
const sendMessage = jest.spyOn(bot, 'sendMessage');
const chatId = createChat();
const msg = {
chat: { id: chatId },
text: '/watched 9999',
} as unknown as TelegramBot.Message;
const group = await createGroup();
const user = await createUser(String(chatId));
await addUserToGroup(group.id, user?.id ?? 0);
const msg = {
chat: { id: chatId },
text: '/watched 9999',
from: { username: user?.username ?? '' },
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -539,12 +549,12 @@ describe('Bot Commands', () => {
const chatId = createChat();
const group = await createGroup();
const user = await createUser(String(chatId));
await addUserToGroup(group.id, user?.id ?? 0);
const msg = {
chat: { id: chatId },
text: '/veto 1',
from: { id: user?.id ?? 0 },
} as unknown as TelegramBot.Message;
await addUserToGroup(group.id, user?.id ?? 0);
from: { username: user?.username ?? '' },
} as TelegramBot.Message;
const insertedMovie = await suggestMovie(
'Inception',
user?.id ?? 0,
Expand Down Expand Up @@ -573,7 +583,7 @@ describe('Bot Commands', () => {
const msg = {
chat: { id: chatId },
text: '/veto ',
} as unknown as TelegramBot.Message;
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -591,8 +601,8 @@ describe('Bot Commands', () => {
const msg = {
chat: { id: chatId },
text: '/veto 9999',
from: { id: user?.id ?? 0 },
} as unknown as TelegramBot.Message;
from: { username: user?.username ?? '' },
} as TelegramBot.Message;
await addUserToGroup(group.id, user?.id ?? 0);

await emitMsg(msg);
Expand All @@ -610,12 +620,12 @@ describe('Bot Commands', () => {
const chatId = createChat();
const group = await createGroup();
const user = await createUser(String(chatId));
await addUserToGroup(group.id, user?.id ?? 0);
const msg = {
chat: { id: chatId },
text: '/random',
from: { id: user?.id ?? 0 },
} as unknown as TelegramBot.Message;
await addUserToGroup(group.id, user?.id ?? 0);
from: { username: user?.username ?? '' },
} as TelegramBot.Message;
await suggestMovie(
'Inception',
user?.id ?? 0,
Expand All @@ -640,8 +650,8 @@ describe('Bot Commands', () => {
const msg = {
chat: { id: chatId },
text: '/random',
from: { id: user?.id ?? 0 },
} as unknown as TelegramBot.Message;
from: { username: user?.username ?? '' },
} as TelegramBot.Message;

await emitMsg(msg);

Expand All @@ -659,8 +669,8 @@ describe('Bot Commands', () => {
const msg = {
chat: { id: chatId },
text: '/random',
from: { id: user?.id ?? 0 },
} as unknown as TelegramBot.Message;
from: { username: user?.username ?? '' },
} as TelegramBot.Message;
await addUserToGroup(group.id, user?.id ?? 0);

await emitMsg(msg);
Expand Down
1 change: 1 addition & 0 deletions src/controllers/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const rawBotHandlers: TRule[] = [
return;
}
const group = await createGroup();
await addUserToGroup(group.id, user.id);
bot.sendMessage(chatId, `${texts.group_created} ${group.code}`);
},
],
Expand Down

0 comments on commit 9447a7b

Please sign in to comment.