Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

fix: should export all files by index and initialize by core #284

Merged
merged 4 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion __mocks__/config.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export const MOCK_CONFIG: Configuration = {
ablyKey: 'unit-test-ably-key',
apiKey: 'unit-test-api-key',
apiUrl: 'unit-test-api-url',
conferenceLayerUrl: 'unit-test-conference-layer-url',
conferenceLayerUrl: 'https://unit-test-conference-layer-url',
environment: EnvironmentTypes.DEV,
roomId: 'unit-test-room-id',
debug: true,
};
1 change: 1 addition & 0 deletions __mocks__/realtime.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const MOCK_REALTIME_SERVICE: AblyRealtimeService = {
} as unknown as AblyRealtimeService;

export const ABLY_REALTIME_MOCK = {
isLocalParticipantHost: true,
setGather: jest.fn(),
setParticipantData: jest.fn(),
setSyncProperty: jest.fn(),
Expand Down
135 changes: 131 additions & 4 deletions src/core/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,134 @@
import * as Core from '.';
import { Group, Participant } from '../common/types/participant.types';
import { SuperVizSdkOptions } from '../common/types/sdk-options.types';
import ApiService from '../services/api';
import RemoteConfigService from '../services/remote-config-service';

describe('Core', () => {
test('should be export Laucher', () => {
expect(Core.Laucher).toBeDefined();
import sdk from '.';

const REMOTE_CONFIG_MOCK = {
apiUrl: 'https://dev.nodeapi.superviz.com',
conferenceLayerUrl: 'https://video-conference-layer.superviz.com/14.0.1-rc.2/index.html',
};

const COMMUNICATOR_INSTANCE_MOCK = {
setSyncProperty: jest.fn(),
subscribe: jest.fn(),
unsubscribe: jest.fn(),
destroy: jest.fn(),
follow: jest.fn(),
fetchSyncProperty: jest.fn(),
gather: jest.fn(),
goTo: jest.fn(),
toggleMeetingSetup: jest.fn(),
toggleMicrophone: jest.fn(),
toggleCam: jest.fn(),
toggleScreenShare: jest.fn(),
hangUp: jest.fn(),
toggleChat: jest.fn(),
startTranscription: jest.fn(),
stopTranscription: jest.fn(),
loadPlugin: jest.fn(),
unloadPlugin: jest.fn(),
};

const UNIT_TEST_API_KEY = 'unit-test-api-key';

const SIMPLE_INITIALIZATION_MOCK: SuperVizSdkOptions = {
roomId: 'unit-test-room-id',
participant: {
id: 'unit-test-participant-id',
name: 'unit-test-participant-name',
},
group: {
name: 'unit-test-group-test-name',
id: 'unit-test-group-test-id',
},
};

jest.mock('../services/api');
jest.mock('../services/auth-service', () => ({
__esModule: true,
default: jest.fn().mockImplementation((_, apiKey: string) => {
if (apiKey === UNIT_TEST_API_KEY) {
return true;
}

return false;
}),
}));
jest.mock('../services/remote-config-service');
jest.mock('../services/communicator', () => ({
__esModule: true,
default: jest.fn().mockImplementation(() => {
return COMMUNICATOR_INSTANCE_MOCK;
}),
}));

beforeEach(() => {
jest.clearAllMocks();
});

describe('root export', () => {
test('should export init function', () => {
expect(sdk).toEqual(expect.any(Function));
});
});

describe('initialization errors', () => {
test('should throw an error if no API key is provided', async () => {
await expect(sdk('', SIMPLE_INITIALIZATION_MOCK)).rejects.toThrow('API key is required');
});

test('should throw an error if API key is invalid', async () => {
RemoteConfigService.getRemoteConfig = jest.fn().mockResolvedValue(REMOTE_CONFIG_MOCK);

await expect(sdk('invalid-api-key', SIMPLE_INITIALIZATION_MOCK)).rejects.toThrow(
'Failed to validate API key',
);
});

test('should throw an error if no options are provided', async () => {
await expect(
sdk(UNIT_TEST_API_KEY, undefined as unknown as SuperVizSdkOptions),
).rejects.toThrow('Options is required');
});

test('should throw an error if no room id is provided', async () => {
await expect(
sdk(UNIT_TEST_API_KEY, { ...SIMPLE_INITIALIZATION_MOCK, roomId: '' }),
).rejects.toThrow('Room id is required');
});

test('should throw an error if no participant id is provided', async () => {
await expect(
sdk(UNIT_TEST_API_KEY, {
...SIMPLE_INITIALIZATION_MOCK,
participant: { name: 'unit-test-participant-name' } as Participant,
}),
).rejects.toThrow('Participants fields is required');

await expect(
sdk(UNIT_TEST_API_KEY, {
...SIMPLE_INITIALIZATION_MOCK,
participant: undefined as unknown as Participant,
}),
).rejects.toThrow('Participants fields is required');
});

test('should throw an error if no group name is provided', async () => {
await expect(
sdk(UNIT_TEST_API_KEY, {
...SIMPLE_INITIALIZATION_MOCK,
group: { id: 'unit-test-group-test-id' } as Group,
}),
).rejects.toThrow('Group fields is required');
});

test('should throw an error if envoriment is invalid', async () => {
ApiService.fetchConfig = jest.fn().mockResolvedValue(undefined);

expect(sdk(UNIT_TEST_API_KEY, SIMPLE_INITIALIZATION_MOCK)).rejects.toThrow(
'Failed to load configuration from server',
);
});
});
89 changes: 88 additions & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,88 @@
export { default as Laucher } from './laucher';
import { debug } from 'debug';

import { SuperVizSdkOptions } from '../common/types/sdk-options.types';
import ApiService from '../services/api';
import AuthService from '../services/auth-service';
import { BrowserService } from '../services/browser';
import config from '../services/config';
import RemoteConfigService from '../services/remote-config-service';
import { SuperVizSdk } from '../types';

import LauncherFacade from './launcher';

/**
* @function validateOptions
* @description Validate the options passed to the SDK
* @param {SuperVizSdkOptions} param
* @returns {void}
*/
const validateOptions = ({ group, participant, roomId }: SuperVizSdkOptions): void => {
if (!group || !group.name || !group.id) {
throw new Error('Group fields is required');
}

if (!participant || !participant.id) {
throw new Error('Participants fields is required');
}

if (!roomId) {
throw new Error('Room id is required');
}
};

/**
* @function init
* @description Initialize the SDK
* @param apiKey - API key
* @param options - SDK options
* @returns {SuperVizSdk}
*/
const init = async (apiKey: string, options: SuperVizSdkOptions): Promise<SuperVizSdk> => {
const validApiKey = apiKey && apiKey.trim();

if (!validApiKey) throw new Error('API key is required');

if (!options) throw new Error('Options is required');

validateOptions(options);

if (options.debug) {
debug.enable('*');
} else {
debug.disable();
}

const { apiUrl, conferenceLayerUrl } = await RemoteConfigService.getRemoteConfig(
options.environment,
);

const isValid = await AuthService(apiUrl, apiKey);

if (!isValid) {
throw new Error('Failed to validate API key');
}

const environment = await ApiService.fetchConfig(apiUrl, apiKey);

if (!environment || !environment.ablyKey) {
throw new Error('Failed to load configuration from server');
}

const { ablyKey } = environment;

config.setConfig({
apiUrl,
ablyKey,
apiKey,
conferenceLayerUrl,
environment,
roomId: options.roomId,
debug: options.debug,
});

return LauncherFacade(
Object.assign({}, options, { apiKey, ablyKey, conferenceLayerUrl, apiUrl }),
);
};

export default init;
Loading
Loading