Skip to content

Commit

Permalink
SDA-4770 Adaptations on openfin integration (#2268) (#2269)
Browse files Browse the repository at this point in the history
* SDA-4770 Adaptations on openfin integration

* SDA-4770 Adaptations on openfin integration
  • Loading branch information
sbenmoussati authored Jan 22, 2025
1 parent c9326fd commit 72be5a4
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 14 deletions.
24 changes: 24 additions & 0 deletions spec/mainApiHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jest.mock('../src/app/openfin-handler', () => {
getAllClientsInContextGroup: jest.fn(),
registerIntentHandler: jest.fn(),
unregisterIntentHandler: jest.fn(),
fireIntentForContext: jest.fn(),
removeClientFromContextGroup: jest.fn(),
},
};
});
Expand Down Expand Up @@ -712,5 +714,27 @@ describe('main api handler', () => {

expect(spy).toHaveBeenCalledTimes(1);
});

it('should call `fireIntentForContext`', () => {
const spy = jest.spyOn(openfinHandler, 'fireIntentForContext');
const value = {
cmd: apiCmds.openfinFireIntentForContext,
};

ipcMain.send(apiName.symphonyApi, value);

expect(spy).toHaveBeenCalledTimes(1);
});

it('should call `removeClientFromContextGroup`', () => {
const spy = jest.spyOn(openfinHandler, 'removeClientFromContextGroup');
const value = {
cmd: apiCmds.openfinRemoveClientFromContextGroup,
};

ipcMain.send(apiName.symphonyApi, value);

expect(spy).toHaveBeenCalledTimes(1);
});
});
});
87 changes: 77 additions & 10 deletions spec/openfinHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jest.mock('@openfin/node-adapter', () => ({
getAllClientsInContextGroup: jest.fn(),
joinContextGroup: jest.fn(),
getContextGroups: jest.fn(),
fireIntentForContext: jest.fn(),
removeClientFromContextGroup: jest.fn(),
}),
},
});
Expand Down Expand Up @@ -50,24 +52,57 @@ describe('Openfin', () => {
});

it('should not be connected', () => {
const info = openfinHandler.getInfo();
const connectionStatus = openfinHandler.getConnectionStatus();

expect(info.isConnected).toBeFalsy();
expect(connectionStatus.isConnected).toBeFalsy();
});

it('should connect', async () => {
const connectSyncSpy = jest.spyOn(connectMock.Interop, 'connectSync');

await openfinHandler.connect();
const info = openfinHandler.getInfo();
const isConnected = openfinHandler.getConnectionStatus();
const connectionStatus = openfinHandler.getConnectionStatus();

expect(connect).toHaveBeenCalled();
expect(connectSyncSpy).toHaveBeenCalledTimes(1);
expect(info.isConnected).toBeTruthy();
expect(isConnected).toBeTruthy();
expect(connectionStatus.isConnected).toBeTruthy();
});

it('should reject and return false if connection times out', async () => {
jest.useFakeTimers();
const connectSyncSpy = jest
.spyOn(connectMock.Interop, 'connectSync')
.mockImplementationOnce((_channelName) => {
return new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, 12000);
});
});

const connectionTimeoutSpy = jest.spyOn(global, 'setTimeout');

let connectionStatus;

const connectPromise = openfinHandler.connect();
const resultPromise = connectPromise.then((res) => {
connectionStatus = res;
});

jest.advanceTimersByTime(10000);

expect(connectionStatus).toBeUndefined();

await resultPromise;

expect(connectionStatus.isConnected).toBe(false);

expect(connectionTimeoutSpy).toHaveBeenCalledTimes(2);
expect(connectionTimeoutSpy.mock.calls[0][1]).toBeGreaterThanOrEqual(10000);

expect(connectSyncSpy).toHaveBeenCalledTimes(1);

jest.useRealTimers();
});

it('should reject and return false if connection times out', async () => {
Expand Down Expand Up @@ -113,10 +148,13 @@ describe('Openfin', () => {

await openfinHandler.connect();
const customIntent = {
type: 'fdc3.contact',
name: 'Andy Young',
id: {
email: '[email protected]',
name: 'ViewContact',
context: {
type: 'fdc3.contact',
name: 'Andy Young',
id: {
email: '[email protected]',
},
},
};
await openfinHandler.fireIntent(customIntent);
Expand Down Expand Up @@ -169,4 +207,33 @@ describe('Openfin', () => {

expect(getAllClientsInContextGroupSpy).toHaveBeenCalledTimes(1);
});

it('should fire an intent for a given context', async () => {
const connectSyncMock = await connectMock.Interop.connectSync();
const fireIntentSpy = jest.spyOn(connectSyncMock, 'fireIntentForContext');

await openfinHandler.connect();
const context = {
type: 'fdc3.contact',
name: 'Andy Young',
id: {
email: '[email protected]',
},
};
await openfinHandler.fireIntentForContext(context);

expect(fireIntentSpy).toHaveBeenCalledTimes(1);
});

it('should remove client from context group', async () => {
const connectSyncMock = await connectMock.Interop.connectSync();
const fireIntentSpy = jest.spyOn(
connectSyncMock,
'removeClientFromContextGroup',
);

await openfinHandler.removeClientFromContextGroup();

expect(fireIntentSpy).toHaveBeenCalledTimes(1);
});
});
8 changes: 8 additions & 0 deletions src/app/main-api-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,12 @@ ipcMain.on(
case apiCmds.openfinUnregisterIntentHandler:
openfinHandler.unregisterIntentHandler(arg.intentName);
break;
case apiCmds.openfinFireIntentForContext:
openfinHandler.fireIntentForContext(arg.context);
break;
case apiCmds.openfinRemoveClientFromContextGroup:
openfinHandler.removeClientFromContextGroup();
break;
default:
break;
}
Expand Down Expand Up @@ -649,6 +655,8 @@ ipcMain.handle(
return openfinHandler.getContextGroups();
case apiCmds.openfinGetAllClientsInContextGroup:
return openfinHandler.getAllClientsInContextGroup(arg.contextGroupId);
case apiCmds.openfinGetClientInfo:
return openfinHandler.getClientInfo();
default:
break;
}
Expand Down
40 changes: 36 additions & 4 deletions src/app/openfin-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ export class OpenfinHandler {
const connectionTimeoutPromise = new Promise((_, reject) =>
setTimeout(() => {
logger.error(
`openfin-handler: Connection timeout after ${
timeoutValue / 1000
`openfin-handler: Connection timeout after ${timeoutValue / 1000
} seconds`,
);
return reject(
Expand Down Expand Up @@ -174,12 +173,45 @@ export class OpenfinHandler {
}

/**
* Returns connection status and provider name
* Returns provider name
*/
public getInfo() {
return {
provider: OPENFIN_PROVIDER,
isConnected: this.getConnectionStatus().isConnected,
fdc3Version: '',
optionalFeatures: {
OriginatingAppMetadata: false,
UserChannelMembershipAPIs: false,
DesktopAgentBridging: false,
},
appMetadata: null,
};
}

/**
* Fires an intent for a given context
* @param context
*/
public fireIntentForContext(context: any) {
this.interopClient.fireIntentForContext(context);
}

/**
* Removes a client from current context group
*/
public removeClientFromContextGroup() {
this.interopClient.removeClientFromContextGroup();
}

/**
* Returns client name
*
*/
public getClientInfo(): unknown {
const { openfin }: IConfig = config.getConfigFields(['openfin']);

return {
name: openfin?.uuid || '',
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/common/api-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ export enum apiCmds {
openfinJoinContextGroup = 'openfin-join-context-group',
openfinGetContextGroups = 'openfin-get-context-groups',
openfinGetAllClientsInContextGroup = 'openfin-get-all-clients-in-context-group',
openfinFireIntentForContext = 'openfin-fire-intent-for-context',
openfinRemoveClientFromContextGroup = 'openfin-remove-client-from-context-group',
openfinGetClientInfo = 'openfin-get-client-info',
}

export enum apiName {
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/preload-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ if (ssfWindow.ssf) {
joinContextGroup: ssfWindow.ssf.openfinJoinContextGroup,
getAllClientsInContextGroup:
ssfWindow.ssf.openfinGetAllClientsInContextGroup,
fireIntentForContext: ssfWindow.ssf.openfinFireIntentForContext,
removeClientFromContextGroup:
ssfWindow.ssf.openfinRemoveClientFromContextGroup,
getClientInfo: ssfWindow.ssf.openfinGetClientInfo,
});
}

Expand Down
30 changes: 30 additions & 0 deletions src/renderer/ssf-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,36 @@ export class SSFApi {
});
}

/**
* Fires an intent for a given context
* @param context
*/
public openfinFireIntentForContext(context: any): void {
local.ipcRenderer.send(apiName.symphonyApi, {
cmd: apiCmds.openfinFireIntentForContext,
context,
});
}

/**
* Removes client from current context group
*/
public openfinRemoveClientFromContextGroup() {
local.ipcRenderer.send(apiName.symphonyApi, {
cmd: apiCmds.openfinRemoveClientFromContextGroup,
});
}

/**
* Returns client info
*/
public async openfinGetClientInfo() {
const info = await local.ipcRenderer.invoke(apiName.symphonyApi, {
cmd: apiCmds.openfinGetClientInfo,
});
return info;
}

/**
*
* Returns Openfin connection status
Expand Down

0 comments on commit 72be5a4

Please sign in to comment.