Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions test/core/messages-reactions.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { ChatClient } from '../../src/core/chat.ts';
Expand Down Expand Up @@ -384,4 +384,126 @@
void room.detach();
void room2.detach();
});

it<TestContext>('should receive 4 summaries when second client adds and deletes two distinct reactions', async (context) => {
const { chat } = context;

const room = await getRandomRoom(chat);

// Attach the room
await room.attach();

// Send a message from first client
const message1 = await room.messages.send({ text: 'Hello there!' });

// Subscribe to reactions and add them to a list when they arrive
const found: MessageReactionSummaryEvent[] = [];

room.messages.reactions.subscribe((event) => {
found.push(event);
});

// Create second client
const client2 = newChatClient();
const room2 = await client2.rooms.get(room.roomId);

Check failure on line 408 in test/core/messages-reactions.integration.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unsafe argument of type error typed assigned to a parameter of type `string`
await room2.attach();

// Second client adds two distinct reactions
await room2.messages.reactions.add(message1, { type: MessageReactionType.Distinct, name: '👍' });

Check failure on line 412 in test/core/messages-reactions.integration.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unsafe call of a(n) `error` type typed value

Check failure on line 412 in test/core/messages-reactions.integration.test.ts

View workflow job for this annotation

GitHub Actions / test node 24

test/core/messages-reactions.integration.test.ts > message reactions integration > should receive 4 summaries when second client adds and deletes two distinct reactions

TypeError: room2.messages.reactions.add is not a function ❯ test/core/messages-reactions.integration.test.ts:412:36

Check failure on line 412 in test/core/messages-reactions.integration.test.ts

View workflow job for this annotation

GitHub Actions / test node 18

test/core/messages-reactions.integration.test.ts > message reactions integration > should receive 4 summaries when second client adds and deletes two distinct reactions

TypeError: room2.messages.reactions.add is not a function ❯ test/core/messages-reactions.integration.test.ts:412:36

Check failure on line 412 in test/core/messages-reactions.integration.test.ts

View workflow job for this annotation

GitHub Actions / test node 22

test/core/messages-reactions.integration.test.ts > message reactions integration > should receive 4 summaries when second client adds and deletes two distinct reactions

TypeError: room2.messages.reactions.add is not a function ❯ test/core/messages-reactions.integration.test.ts:412:36

Check failure on line 412 in test/core/messages-reactions.integration.test.ts

View workflow job for this annotation

GitHub Actions / test node 20

test/core/messages-reactions.integration.test.ts > message reactions integration > should receive 4 summaries when second client adds and deletes two distinct reactions

TypeError: room2.messages.reactions.add is not a function ❯ test/core/messages-reactions.integration.test.ts:412:36
await room2.messages.reactions.add(message1, { type: MessageReactionType.Distinct, name: '❤️' });

Check failure on line 413 in test/core/messages-reactions.integration.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unsafe call of a(n) `error` type typed value

// Wait for first two summaries (after adding reactions)
await vi.waitFor(
() => {
expect(found.length).toBeGreaterThanOrEqual(2);

// Check first summary (after adding 👍)
const firstSummary = found.find((e) =>
e.summary.messageSerial === message1.serial &&
e.summary.distinct?.['👍'] &&

Check failure on line 423 in test/core/messages-reactions.integration.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary optional chain on a non-nullish value
!e.summary.distinct?.['❤️']

Check failure on line 424 in test/core/messages-reactions.integration.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary optional chain on a non-nullish value
);
expect(firstSummary).toMatchObject({
type: MessageReactionEvents.Summary,

Check failure on line 427 in test/core/messages-reactions.integration.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unsafe member access .Summary on an `error` typed value

Check failure on line 427 in test/core/messages-reactions.integration.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unsafe assignment of an error typed value
summary: {
messageSerial: message1.serial,
distinct: {
'👍': {
total: 1,
clientIds: [client2.clientId],
},
},
},
});

// Check second summary (after adding ❤️)
const secondSummary = found.find((e) =>
e.summary.messageSerial === message1.serial &&
e.summary.distinct?.['👍'] &&

Check failure on line 442 in test/core/messages-reactions.integration.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary optional chain on a non-nullish value
e.summary.distinct?.['❤️']

Check failure on line 443 in test/core/messages-reactions.integration.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary optional chain on a non-nullish value
);
expect(secondSummary).toMatchObject({
type: MessageReactionEvents.Summary,

Check failure on line 446 in test/core/messages-reactions.integration.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unsafe assignment of an error typed value
summary: {
messageSerial: message1.serial,
distinct: {
'👍': {
total: 1,
clientIds: [client2.clientId],
},
'❤️': {
total: 1,
clientIds: [client2.clientId],
},
},
},
});
},
{ timeout: 30_000 },
);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be a good idea to clear the found array here?

// Second client deletes both reactions
await room2.messages.reactions.delete(message1, { type: MessageReactionType.Distinct, name: '👍' });
await room2.messages.reactions.delete(message1, { type: MessageReactionType.Distinct, name: '❤️' });

// Wait for all 4 summaries
await vi.waitFor(
() => {
expect(found.length).toBeGreaterThanOrEqual(4);

// Check third summary (after deleting 👍)
const thirdSummary = found.find((e) =>
e.summary.messageSerial === message1.serial &&
!e.summary.distinct?.['👍'] &&
e.summary.distinct?.['❤️']
);
expect(thirdSummary).toMatchObject({
type: MessageReactionEvents.Summary,
summary: {
messageSerial: message1.serial,
distinct: {
'❤️': {
total: 1,
clientIds: [client2.clientId],
},
},
},
});

// Check fourth summary (after deleting ❤️ - should be empty or not have these reactions)
const fourthSummary = found.findLast((e) => e.summary.messageSerial === message1.serial);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the third one is not at index 2 (third in list), you'll find the third one here.

Would it be better to just findIndex the third one, and get the other index for the fourth one?

We're talking about indexes 2 and 3 for those checks, or 0 and 1 if you clear the found array.

I guess the same logic for finding the summaries in the list can be done for the first two as well?

expect(fourthSummary).toMatchObject({
type: MessageReactionEvents.Summary,
summary: {
messageSerial: message1.serial,
distinct: {},
},
});
},
{ timeout: 30_000 },
);

void room.detach();
void room2.detach();
});
});
Loading