import { ConnectionLifecycle, LogLevel } from '@ably/chat'; import { describe, expect, it } from 'vitest'; import { newChatClient } from '../helper/chat'; import { testClientOptions } from '../helper/options'; import { baseRealtimeOptions } from '../helper/realtime-client'; import { getRandomRoom } from '../helper/room'; import { RealtimeWithOptions } from '../unified-clients/realtime-extensions'; import { PublicChatClient } from '../unified-clients/RpcChat'; import { VERSION } from '../unified-clients/version'; const waitForConnectionLifecycle = (chat: PublicChatClient, state: ConnectionLifecycle) => { return new Promise((resolve, reject) => { const { off } = chat.connection.status.onChange((change) => { if (change.current === state) { off(); resolve(); } }); // Set a timeout to reject the promise if the status is not reached setInterval(() => { off(); reject(new Error(`Connection status ${state} not reached`)); }, 5000); }); }; describe('Chat', () => { // test not supported in UTS yet it.skip('should set the agent string on client instantiation', () => { const chat = newChatClient(testClientOptions()); expect((chat.realtime as RealtimeWithOptions).options.agents).toEqual({ 'chat-js': VERSION }); }); // test not supported in UTS yet it.skip('should add a new agent string', () => { const chat = newChatClient(testClientOptions()); chat.addReactAgent(); expect((chat.realtime as RealtimeWithOptions).options.agents).toEqual({ 'chat-js': VERSION, 'chat-react': VERSION, }); }); it('should mix in the client options', () => { const chat = newChatClient(testClientOptions({ logLevel: LogLevel.Warn })); expect(chat.clientOptions.logLevel).toBe(LogLevel.Warn); }); // test not supported in UTS yet it.skip('should work using basic auth', async () => { const chat = newChatClient({}, baseRealtimeOptions({})); const room = getRandomRoom(chat); // Send a message, and expect it to succeed const message = await room.messages.send({ text: 'my message' }); expect(message).toEqual(expect.objectContaining({ text: 'my message', clientId: chat.clientId })); // Request occupancy, and expect it to succeed const occupancy = await room.occupancy.get(); expect(occupancy).toEqual(expect.objectContaining({ connections: 0, presenceMembers: 0 })); // Request history, and expect it to succeed const history = await room.messages.get({ limit: 1 }); expect(history.items).toEqual( expect.arrayContaining([expect.objectContaining({ text: 'my message', clientId: chat.clientId })]), ); }); // test not supported in UTS yet it.skip('should work using msgpack', async () => { const chat = newChatClient(undefined, baseRealtimeOptions({ useBinaryProtocol: true })); const room = getRandomRoom(chat); // Send a message, and expect it to succeed const message = await room.messages.send({ text: 'my message' }); expect(message).toEqual(expect.objectContaining({ text: 'my message', clientId: chat.clientId })); // Request occupancy, and expect it to succeed const occupancy = await room.occupancy.get(); expect(occupancy).toEqual(expect.objectContaining({ connections: 0, presenceMembers: 0 })); // Request history, and expect it to succeed const history = await room.messages.get({ limit: 1 }); expect(history.items).toEqual( expect.arrayContaining([expect.objectContaining({ text: 'my message', clientId: chat.clientId })]), ); }); // test not supported in UTS yet it.skip('should have a connection state', async () => { const chat = newChatClient(undefined, baseRealtimeOptions()); const realtime = chat.realtime; await waitForConnectionLifecycle(chat, ConnectionLifecycle.Connected); // Fail the connection by disconnecting realtime.close(); // Wait for the connection to fail await waitForConnectionLifecycle(chat, ConnectionLifecycle.Failed); }); });