diff --git a/tests/ably/ably-user-login.test.ts b/tests/ably/ably-user-login.test.ts index 2e98602..77ec69b 100644 --- a/tests/ably/ably-user-login.test.ts +++ b/tests/ably/ably-user-login.test.ts @@ -39,7 +39,7 @@ describe('AblyUserLogin', () => { }); }); - test('user logs in without previous (guest) channels', async () => { + test('user logs in without previous (public) channels', async () => { let connectionStates : Array= [] // Initial clientId is null expect(mockAuthServer.clientId).toBeNull(); @@ -70,7 +70,7 @@ describe('AblyUserLogin', () => { expect(echo.connector.ablyAuth.existingToken().clientId).toBe('sacOO7@github.com'); }); - test('user logs in with previous (guest) channels', async () => { + test('user logs in with previous (public) channels', async () => { let connectionStates : Array= [] let publicChannelStates : Array= [] diff --git a/tests/ably/setup/mock-auth-server.ts b/tests/ably/setup/mock-auth-server.ts index 8d244a6..c2f806b 100644 --- a/tests/ably/setup/mock-auth-server.ts +++ b/tests/ably/setup/mock-auth-server.ts @@ -1,19 +1,25 @@ -import { isNullOrUndefinedOrEmpty, parseJwt } from '../../../src/channel/ably/utils'; +import { isNullOrUndefinedOrEmpty, parseJwt, toBase64, toText } from '../../../src/channel/ably/utils'; import * as Ably from 'ably/promises'; import * as jwt from 'jsonwebtoken'; type channels = Array; +/** + * MockAuthServer mimicks {@link https://github.com/ably/laravel-broadcaster/blob/main/src/AblyBroadcaster.php AblyBroadcaster.php}. + * Aim is to keep implementation and behaviour in sync with AblyBroadcaster.php, so that it can be tested + * without running actual PHP server. + */ export class MockAuthServer { keyName: string; keySecret: string; ablyClient: Ably.Rest; clientId: string | null = 'sacOO7@github.com'; - userInfo = { id: 'sacOO7@github.com', name: 'sacOO7' }; shortLived: channels; banned: channels; + userInfo = { id: 'sacOO7@github.com', name: 'sacOO7' }; // Used for presence + constructor(apiKey: string, environment = 'sandbox') { const keys = apiKey.split(':'); this.keyName = keys[0]; @@ -21,8 +27,23 @@ export class MockAuthServer { this.ablyClient = new Ably.Rest({ key: apiKey, environment }); } + /** + * Broadcast to all clients subscribed to given channel. + */ broadcast = async (channelName: string, eventName: string, message: string) => { - await this.ablyClient.channels.get(channelName).publish(eventName, message); + await this.broadcastToOthers("", {channelName, eventName, payload: message}); + }; + + /** + * Broadcast on behalf of a given realtime client. + */ + broadcastToOthers = async (socketId: string, {channelName, eventName, payload}) => { + let protoMsg = {name: eventName, data: payload}; + if (!isNullOrUndefinedOrEmpty(socketId)) { + const socketIdObj = JSON.parse(toText(socketId)); + protoMsg = {...protoMsg, ...socketIdObj} + } + await this.ablyClient.channels.get(channelName).publish(protoMsg); }; tokenInvalidOrExpired = (serverTime, token) => { diff --git a/tests/ably/setup/utils.ts b/tests/ably/setup/utils.ts index 9213fde..5d9a964 100644 --- a/tests/ably/setup/utils.ts +++ b/tests/ably/setup/utils.ts @@ -1,4 +1,5 @@ -import { httpRequestAsync } from '../../../src/channel/ably/utils'; +import { httpRequestAsync} from '../../../src/channel/ably/utils'; +export { toBase64 } from '../../../src/channel/ably/utils'; const safeAssert = (assertions: Function, done: Function, finalAssertion = false) => { try { @@ -17,8 +18,6 @@ export const execute = (fn: Function, times: number) => { } }; -export const toBase64 = (text: string) => Buffer.from(text, 'binary').toString('base64'); - export const httpPostAsync = async (url: string, postData: any) => { postData = JSON.stringify(postData); let postOptions = {