Skip to content

Commit

Permalink
feat: add test coverage for deleting and filtering redacted message
Browse files Browse the repository at this point in the history
  • Loading branch information
domw30 committed Oct 12, 2023
1 parent 96548f0 commit 24c1b12
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
94 changes: 94 additions & 0 deletions src/lib/chat/matrix-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,98 @@ describe('matrix client', () => {
});
});
});

describe('deleteMessageByRoomId', () => {
it('deletes a message by room ID and message ID', async () => {
const messageId = 123456;
const channelId = '!abcdefg';
const redactEvent = jest.fn().mockResolvedValue({});

const client = subject({
createClient: jest.fn(() => getSdkClient({ redactEvent })),
});

await client.connect(null, 'token');
await client.deleteMessageByRoomId(channelId, messageId);

expect(redactEvent).toHaveBeenCalledWith(channelId, messageId.toString());
});
});

describe('getMessagesByChannelId', () => {
it('filters out redacted messages', async () => {
const getUser = jest.fn().mockReturnValue({ displayName: 'Mock User' });
const createMessagesRequest = jest.fn().mockResolvedValue({
chunk: [
{
type: 'm.room.message',
content: { body: 'message 1', msgtype: 'm.text' },
event_id: 'message-id-1',
unsigned: { redacted_because: {} }, // Indicates the message has been redacted.
},
{
type: 'm.room.message',
content: { body: 'message 2', msgtype: 'm.text' },
event_id: 'message-id-2',
},
],
});

const client = subject({
createClient: jest.fn(() => getSdkClient({ createMessagesRequest, getUser })),
});

await client.connect(null, 'token');
const { messages: fetchedMessages } = await client.getMessagesByChannelId('channel-id');

expect(fetchedMessages).toHaveLength(1);
expect(fetchedMessages[0].message).toEqual('message 2');
});

it('fetches messages successfully', async () => {
const getUser = jest.fn().mockReturnValue({ displayName: 'Mock User' });
const createMessagesRequest = jest.fn().mockResolvedValue({
chunk: [
{
type: 'm.room.message',
content: { body: 'message 1', msgtype: 'm.text' },
event_id: 'message-id-1',
},
{
type: 'm.room.message',
content: { body: 'message 2', msgtype: 'm.text' },
event_id: 'message-id-2',
},
{
type: 'm.room.message',
content: { body: 'message 3', msgtype: 'm.text' },
event_id: 'message-id-3',
},
],
});

const client = subject({
createClient: jest.fn(() => getSdkClient({ createMessagesRequest, getUser })),
});

await client.connect(null, 'token');
const { messages: fetchedMessages } = await client.getMessagesByChannelId('channel-id');

expect(fetchedMessages).toHaveLength(3);
});

it('returns an empty array if no messages are found', async () => {
const getUser = jest.fn().mockReturnValue({ displayName: 'Mock User' });
const createMessagesRequest = jest.fn().mockResolvedValue({ chunk: [] });

const client = subject({
createClient: jest.fn(() => getSdkClient({ createMessagesRequest, getUser })),
});

await client.connect(null, 'token');
const { messages: fetchedMessages } = await client.getMessagesByChannelId('channel-id');

expect(fetchedMessages).toHaveLength(0);
});
});
});
3 changes: 1 addition & 2 deletions src/lib/chat/matrix-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ export class MatrixClient implements IChatClient {
await this.waitForConnection();
const { chunk } = await this.matrix.createMessagesRequest(channelId, null, 50, Direction.Backward);
const messages = chunk.filter((m) => m.type === EventType.RoomMessage && !m?.unsigned?.redacted_because);

const mappedMessages = [];
for (const message of messages) {
mappedMessages.push(mapMatrixMessage(message, this.matrix));
Expand Down Expand Up @@ -210,7 +209,7 @@ export class MatrixClient implements IChatClient {
}

async deleteMessageByRoomId(channelId: string, messageId: number): Promise<any> {
await this.matrix.redactEvent(channelId, messageId.toString(), null);
await this.matrix.redactEvent(channelId, messageId.toString());
}

async fetchConversationsWithUsers(users: User[]) {
Expand Down

0 comments on commit 24c1b12

Please sign in to comment.