Skip to content

Commit

Permalink
core tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyTWF committed Oct 31, 2024
1 parent 9b63c56 commit 65946e1
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 478 deletions.
4 changes: 2 additions & 2 deletions test/core/chat.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Chat', () => {

it('should work using basic auth', async () => {
const chat = newChatClient({}, ablyRealtimeClient({}));
const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

// Send a message, and expect it to succeed
const message = await room.messages.send({ text: 'my message' });
Expand All @@ -68,7 +68,7 @@ describe('Chat', () => {

it('should work using msgpack', async () => {
const chat = newChatClient(undefined, ablyRealtimeClient({ useBinaryProtocol: true }));
const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

// Send a message, and expect it to succeed
const message = await room.messages.send({ text: 'my message' });
Expand Down
26 changes: 13 additions & 13 deletions test/core/messages.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ describe('messages integration', () => {
it<TestContext>('sets the agent version on the channel', async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const channel = (await room.messages.channel) as RealtimeChannelWithOptions;
const room = await getRandomRoom(chat);
const channel = room.messages.channel as RealtimeChannelWithOptions;

expect(channel.channelOptions.params).toEqual(expect.objectContaining({ agent: CHANNEL_OPTIONS_AGENT_STRING }));
});

it<TestContext>('should be able to send and receive chat messages', async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

// Attach the room
await room.attach();
Expand Down Expand Up @@ -82,7 +82,7 @@ describe('messages integration', () => {
it<TestContext>('should be able to retrieve chat history', async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

// Publish 3 messages
const message1 = await room.messages.send({ text: 'Hello there!' });
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('messages integration', () => {
it<TestContext>('should be able to paginate chat history', async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

// Publish 4 messages
const message1 = await room.messages.send({ text: 'Hello there!' });
Expand Down Expand Up @@ -167,7 +167,7 @@ describe('messages integration', () => {
it<TestContext>('should be able to paginate chat history, but backwards', async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

// Publish 4 messages
const message1 = await room.messages.send({ text: 'Hello there!' });
Expand Down Expand Up @@ -217,7 +217,7 @@ describe('messages integration', () => {
it<TestContext>('should be able to send, receive and query chat messages with metadata and headers', async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

// Subscribe to messages and add them to a list when they arrive
const messages: Message[] = [];
Expand Down Expand Up @@ -276,7 +276,7 @@ describe('messages integration', () => {
it<TestContext>('should be able to get history for listener from attached timeserial', async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

// Publish some messages
const message1 = await room.messages.send({ text: 'Hello there!' });
Expand Down Expand Up @@ -331,7 +331,7 @@ describe('messages integration', () => {
it<TestContext>('should be able to get history for listener with latest message timeserial', async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

// Subscribe to messages, which will also set up the listener subscription point
const { getPreviousMessages } = room.messages.subscribe(() => {});
Expand Down Expand Up @@ -372,7 +372,7 @@ describe('messages integration', () => {
it<TestContext>('should be able to get history for multiple listeners', async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

await room.messages.send({ text: 'Hello there!' });
await room.messages.send({ text: 'I have the high ground!' });
Expand All @@ -398,7 +398,7 @@ describe('messages integration', () => {
it<TestContext>('handles discontinuities', async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

// Attach the room
await room.attach();
Expand All @@ -409,7 +409,7 @@ describe('messages integration', () => {
discontinuityErrors.push(error);
});

const channelSuspendable = (await room.messages.channel) as Ably.RealtimeChannel & {
const channelSuspendable = room.messages.channel as Ably.RealtimeChannel & {
notifyState(state: 'suspended' | 'attached'): void;
};

Expand Down Expand Up @@ -447,7 +447,7 @@ describe('messages integration', () => {
it<TestContext>('handles the room being released before getPreviousMessages is called', async (context) => {
const chat = context.chat;
const roomId = randomRoomId();
const room = chat.rooms.get(roomId, RoomOptionsDefaults);
const room = await chat.rooms.get(roomId, RoomOptionsDefaults);

// Create a subscription to messages
room.messages.subscribe(() => {});
Expand Down
16 changes: 8 additions & 8 deletions test/core/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ const mockPaginatedResultWithItems = (items: Message[]): MockPaginatedResult =>
vi.mock('ably');

describe('Messages', () => {
beforeEach<TestContext>(async (context) => {
beforeEach<TestContext>((context) => {
context.realtime = new Ably.Realtime({ clientId: 'clientId', key: 'key' });
context.chatApi = new ChatApi(context.realtime, makeTestLogger());
context.room = makeRandomRoom({ chatApi: context.chatApi, realtime: context.realtime });
const channel = await context.room.messages.channel;
const channel = context.room.messages.channel;
context.emulateBackendPublish = channelEventEmitter(channel);
context.emulateBackendStateChange = channelStateEventEmitter(channel);
});
Expand Down Expand Up @@ -393,7 +393,7 @@ describe('Messages', () => {
return Promise.resolve(mockPaginatedResultWithItems([]));
});

const msgChannel = await room.messages.channel;
const msgChannel = room.messages.channel;

// Force ts to recognize the channel properties
const channel = msgChannel as RealtimeChannel & {
Expand Down Expand Up @@ -453,7 +453,7 @@ describe('Messages', () => {
return Promise.resolve(mockPaginatedResultWithItems([]));
});

const msgChannel = await room.messages.channel;
const msgChannel = room.messages.channel;

// Force ts to recognize the channel properties
const channel = msgChannel as RealtimeChannel & {
Expand Down Expand Up @@ -490,7 +490,7 @@ describe('Messages', () => {
return Promise.resolve(mockPaginatedResultWithItems([]));
});

const msgChannel = await room.messages.channel;
const msgChannel = room.messages.channel;
const channel = msgChannel as RealtimeChannel & {
properties: {
attachSerial: string | undefined;
Expand Down Expand Up @@ -587,7 +587,7 @@ describe('Messages', () => {
return Promise.resolve(mockPaginatedResultWithItems([]));
});

const msgChannel = await room.messages.channel;
const msgChannel = room.messages.channel;
const channel = msgChannel as RealtimeChannel & {
properties: {
attachSerial: string | undefined;
Expand Down Expand Up @@ -675,7 +675,7 @@ describe('Messages', () => {
return Promise.resolve(mockPaginatedResultWithItems([]));
});

const msgChannel = await room.messages.channel;
const msgChannel = room.messages.channel;
const channel = msgChannel as RealtimeChannel & {
properties: {
attachSerial: string | undefined;
Expand Down Expand Up @@ -778,7 +778,7 @@ describe('Messages', () => {
// Create a room instance
const { room } = context;

const msgChannel = await room.messages.channel;
const msgChannel = room.messages.channel;
const channel = msgChannel as RealtimeChannel & {
properties: {
attachSerial: string | undefined;
Expand Down
12 changes: 6 additions & 6 deletions test/core/occupancy.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ describe('occupancy', () => {
it<TestContext>('should be able to get the occupancy of a chat room', { timeout: TEST_TIMEOUT }, async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

// Get the occupancy of the room, instantaneously it will be 0
await waitForExpectedInstantaneousOccupancy(room, {
connections: 0,
presenceMembers: 0,
});

const { name: channelName } = await room.messages.channel;
const { name: channelName } = room.messages.channel;

// In a separate realtime client, attach to the same room
const realtimeClient = ablyRealtimeClientWithToken();
Expand Down Expand Up @@ -109,7 +109,7 @@ describe('occupancy', () => {
it<TestContext>('allows subscriptions to inband occupancy', { timeout: TEST_TIMEOUT }, async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

// Subscribe to occupancy
const occupancyUpdates: OccupancyEvent[] = [];
Expand All @@ -132,7 +132,7 @@ describe('occupancy', () => {

// In a separate realtime client, attach to the same room
const realtimeClient = ablyRealtimeClientWithToken();
const { name: channelName } = await room.messages.channel;
const { name: channelName } = room.messages.channel;
const realtimeChannel = realtimeClient.channels.get(channelName);
await realtimeChannel.attach();
await realtimeChannel.presence.enter();
Expand All @@ -151,7 +151,7 @@ describe('occupancy', () => {
it<TestContext>('handles discontinuities', async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

// Attach the room
await room.attach();
Expand All @@ -162,7 +162,7 @@ describe('occupancy', () => {
discontinuityErrors.push(error);
});

const channelSuspendable = (await room.messages.channel) as Ably.RealtimeChannel & {
const channelSuspendable = room.messages.channel as Ably.RealtimeChannel & {
notifyState(state: 'suspended' | 'attached'): void;
};

Expand Down
4 changes: 2 additions & 2 deletions test/core/occupancy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ interface TestContext {
vi.mock('ably');

describe('Occupancy', () => {
beforeEach<TestContext>(async (context) => {
beforeEach<TestContext>((context) => {
context.realtime = new Ably.Realtime({ clientId: 'clientId', key: 'key' });
context.chatApi = new ChatApi(context.realtime, makeTestLogger());
context.room = makeRandomRoom({ chatApi: context.chatApi, realtime: context.realtime });
const channel = await context.room.occupancy.channel;
const channel = context.room.occupancy.channel;
context.emulateOccupancyUpdate = channelEventEmitter(channel);
});

Expand Down
20 changes: 10 additions & 10 deletions test/core/presence.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ const waitForEvent = (

describe('UserPresence', { timeout: 10000 }, () => {
// Setup before each test, create a new Ably Realtime client and a new Room
beforeEach<TestContext>((context) => {
beforeEach<TestContext>(async (context) => {
context.realtime = ablyRealtimeClient();
const roomId = randomRoomId();
context.chat = newChatClient(undefined, context.realtime);
context.defaultTestClientId = context.realtime.auth.clientId;
context.chatRoom = context.chat.rooms.get(roomId, { presence: RoomOptionsDefaults.presence });
context.chatRoom = await context.chat.rooms.get(roomId, { presence: RoomOptionsDefaults.presence });
});

// Test for successful entering with clientId and custom user data
it<TestContext>('successfully enter presence with clientId and custom user data', async (context) => {
const messageChannel = await context.chatRoom.messages.channel;
const messageChannel = context.chatRoom.messages.channel;
const messageChannelName = messageChannel.name;
const enterEventPromise = waitForEvent(
context.realtime,
Expand All @@ -131,7 +131,7 @@ describe('UserPresence', { timeout: 10000 }, () => {

// Test for successful sending of presence update with clientId and custom user data
it<TestContext>('should successfully send presence update with clientId and custom user data', async (context) => {
const messageChannel = await context.chatRoom.messages.channel;
const messageChannel = context.chatRoom.messages.channel;
const messageChannelName = messageChannel.name;
const enterEventPromise = waitForEvent(context.realtime, 'update', messageChannelName, (member) => {
expect(member.clientId, 'client id should be equal to defaultTestClientId').toEqual(context.defaultTestClientId);
Expand All @@ -150,7 +150,7 @@ describe('UserPresence', { timeout: 10000 }, () => {

// Test for successful leaving of presence
it<TestContext>('should successfully leave presence', async (context) => {
const messageChannel = await context.chatRoom.messages.channel;
const messageChannel = context.chatRoom.messages.channel;
const messageChannelName = messageChannel.name;
const enterEventPromise = waitForEvent(
context.realtime,
Expand All @@ -175,7 +175,7 @@ describe('UserPresence', { timeout: 10000 }, () => {

// Test for successful fetching of presence users
it<TestContext>('should successfully fetch presence users ', async (context) => {
const { name: channelName } = await context.chatRoom.messages.channel;
const { name: channelName } = context.chatRoom.messages.channel;

// Connect 3 clients to the same channel
const client1 = ablyRealtimeClient({ clientId: 'clientId1' }).channels.get(channelName);
Expand Down Expand Up @@ -388,7 +388,7 @@ describe('UserPresence', { timeout: 10000 }, () => {
discontinuityErrors.push(error);
});

const channelSuspendable = (await room.presence.channel) as Ably.RealtimeChannel & {
const channelSuspendable = room.presence.channel as Ably.RealtimeChannel & {
notifyState(state: 'suspended' | 'attached'): void;
};

Expand Down Expand Up @@ -426,7 +426,7 @@ describe('UserPresence', { timeout: 10000 }, () => {
it<TestContext>('prevents presence entry if room option prevents it', async (context) => {
const { chat } = context;

const room = chat.rooms.get(randomRoomId(), { presence: { enter: false } });
const room = await chat.rooms.get(randomRoomId(), { presence: { enter: false } });

await room.attach();

Expand All @@ -437,7 +437,7 @@ describe('UserPresence', { timeout: 10000 }, () => {
it<TestContext>('does not receive presence events if room option prevents it', async (context) => {
const { chat } = context;

const room = chat.rooms.get(randomRoomId(), { presence: { subscribe: false } });
const room = await chat.rooms.get(randomRoomId(), { presence: { subscribe: false } });

await room.attach();

Expand All @@ -449,7 +449,7 @@ describe('UserPresence', { timeout: 10000 }, () => {

// We need to create another chat client and enter presence on the same room
const chat2 = newChatClient();
const room2 = chat2.rooms.get(room.roomId, { presence: { enter: true } });
const room2 = await chat2.rooms.get(room.roomId, { presence: { enter: true } });

// Entering presence
await room2.attach();
Expand Down
2 changes: 1 addition & 1 deletion test/core/room-lifecycle-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const makeMockContributor = (
discontinuityDetected() {},
attachmentErrorCode,
detachmentErrorCode,
channel: Promise.resolve(channel),
channel: channel,
},
emulateStateChange(change: Ably.ChannelStateChange, update?: boolean) {
vi.spyOn(contributor.channel, 'state', 'get').mockReturnValue(change.current);
Expand Down
10 changes: 5 additions & 5 deletions test/core/room-reactions.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ describe('room-level reactions integration test', () => {
it<TestContext>('sets the agent version on the channel', async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const channel = (await room.messages.channel) as RealtimeChannelWithOptions;
const room = await getRandomRoom(chat);
const channel = room.messages.channel as RealtimeChannelWithOptions;

expect(channel.channelOptions.params).toEqual(expect.objectContaining({ agent: CHANNEL_OPTIONS_AGENT_STRING }));
});

it<TestContext>('sends and receives a reaction', async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

const expectedReactions = ['like', 'like', 'love', 'hate'];
const reactions: string[] = [];
Expand All @@ -81,7 +81,7 @@ describe('room-level reactions integration test', () => {
it<TestContext>('handles discontinuities', async (context) => {
const { chat } = context;

const room = getRandomRoom(chat);
const room = await getRandomRoom(chat);

// Attach the room
await room.attach();
Expand All @@ -92,7 +92,7 @@ describe('room-level reactions integration test', () => {
discontinuityErrors.push(error);
});

const channelSuspendable = (await room.reactions.channel) as Ably.RealtimeChannel & {
const channelSuspendable = room.reactions.channel as Ably.RealtimeChannel & {
notifyState(state: 'suspended' | 'attached'): void;
};

Expand Down
Loading

0 comments on commit 65946e1

Please sign in to comment.