Skip to content

Commit

Permalink
test: add a test case validating followers
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustrb committed Dec 19, 2024
1 parent 65ae213 commit 664599f
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions apps/meteor/tests/end-to-end/api/chat.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { Credentials } from '@rocket.chat/api-client';
import type { IMessage, IRoom, IThreadMessage, IUser } from '@rocket.chat/core-typings';
import type { IMessage, IRoom, ISubscription, IThreadMessage, IUser } from '@rocket.chat/core-typings';
import { Random } from '@rocket.chat/random';
import { expect } from 'chai';
import { after, before, beforeEach, describe, it } from 'mocha';
import type { Response } from 'supertest';

import { getCredentials, api, request, credentials } from '../../data/api-data';
import { sendSimpleMessage, deleteMessage } from '../../data/chat.helper';
import { followMessage, sendSimpleMessage, deleteMessage } from '../../data/chat.helper';
import { imgURL } from '../../data/interactions';
import { updatePermission, updateSetting } from '../../data/permissions.helper';
import { createRoom, deleteRoom, getSubscriptionByRoomId } from '../../data/rooms.helper';
import { addUserToRoom, createRoom, deleteRoom, getSubscriptionByRoomId } from '../../data/rooms.helper';
import { password } from '../../data/user';
import type { TestUser } from '../../data/users.helper';
import { createUser, deleteUser, login } from '../../data/users.helper';
Expand Down Expand Up @@ -2012,41 +2012,43 @@ describe('[Chat]', () => {
let parentThreadId: IMessage['_id'];

before(async () => {
otherUser = await createUser();
const username = `user${+new Date()}`;
otherUser = await createUser({ username });
otherUserCredentials = await login(otherUser.username, password);
parentThreadId = (await sendSimpleMessage({ roomId: testChannel._id })).body.message._id;
await addUserToRoom({ rid: testChannel._id, usernames: [otherUser.username] });
});

after(() => Promise.all([deleteUser(otherUser), deleteMessage({ msgId: parentThreadId, roomId: testChannel._id })]));

const expectNoUnreadThreadMessages = (s: ISubscription) => {
expect(s).to.have.property('tunread');
expect(s.tunread).to.be.an('array');
expect(s.tunread).to.deep.equal([]);
};

it('should reset the unread counter if the message was removed', async () => {
const { body } = await sendSimpleMessage({ roomId: testChannel._id, tmid: parentThreadId, userCredentials: otherUserCredentials });
const msgId = body.message._id;
await request
.post(api('chat.delete'))
.set(credentials)
.send({
roomId: testChannel._id,
msgId,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
});
const childrenMessageId = body.message._id;

await followMessage({ msgId: parentThreadId, requestCredentials: otherUserCredentials });
await deleteMessage({ msgId: childrenMessageId, roomId: testChannel._id });

const userWhoCreatedTheThreadSubscription = await getSubscriptionByRoomId(testChannel._id);

expectNoUnreadThreadMessages(userWhoCreatedTheThreadSubscription);
});

it('should reset the unread counter of users who followed the thread', async () => {
const { body } = await sendSimpleMessage({ roomId: testChannel._id, tmid: parentThreadId });
const childrenMessageId = body.message._id;

const [userWhoCreatedTheThreadSubscription, userWhoDeletedTheThreadSubscription] = await Promise.all([
getSubscriptionByRoomId(testChannel._id),
getSubscriptionByRoomId(testChannel._id, otherUserCredentials),
]);
await followMessage({ msgId: parentThreadId, requestCredentials: otherUserCredentials });
await deleteMessage({ msgId: childrenMessageId, roomId: testChannel._id });

expect(userWhoCreatedTheThreadSubscription).to.have.property('tunread');
expect(userWhoCreatedTheThreadSubscription.tunread).to.be.an('array');
expect(userWhoCreatedTheThreadSubscription.tunread).to.deep.equal([]);
const userWhoWasFollowingTheThreadSubscription = await getSubscriptionByRoomId(testChannel._id, otherUserCredentials);

// The user who deleted the thread should not have the thread marked as unread, since
// there was never a message in the thread that was not read by the user
expect(userWhoDeletedTheThreadSubscription).to.not.have.property('tunread');
expectNoUnreadThreadMessages(userWhoWasFollowingTheThreadSubscription);
});
});
});
Expand Down

0 comments on commit 664599f

Please sign in to comment.