Skip to content

Commit

Permalink
Await / promises fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoffat committed May 16, 2024
1 parent 36abf6c commit e0437ab
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 86 deletions.
172 changes: 88 additions & 84 deletions src/test/v2.0/advanced/fdc3.raiseIntent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,42 @@ export default () =>
}
});

const RaiseIntentSingleResolve = "(2.0-RaiseIntentSingleResolve) Should start app intent-a when raising intent 'aTestingIntent1' with context 'testContextX'";
it(RaiseIntentSingleResolve, async () => {
errorListener = await control.listenForError();
const result = control.receiveContext("aTestingIntent-listener-triggered");
const intentResolution = await control.raiseIntent(Intent.aTestingIntent, ContextType.testContextX);
control.validateIntentResolution(IntentApp.IntentAppA, intentResolution);
await result;
});

const RaiseIntentTargetedAppResolve = "(2.0-RaiseIntentTargetedAppResolve) Should start app intent-b when raising intent 'sharedTestingIntent1' with context 'testContextX'";
it(RaiseIntentTargetedAppResolve, async () => {
errorListener = await control.listenForError();
const result = await control.receiveContext("sharedTestingIntent1-listener-triggered");
const intentResolution = await control.raiseIntent(Intent.sharedTestingIntent1, ContextType.testContextX, {
appId: IntentApp.IntentAppB,
});
control.validateIntentResolution(IntentApp.IntentAppB, intentResolution);
await result;
});

const RaiseIntentTargetedInstanceResolveOpen = "(2.0-RaiseIntentTargetedInstanceResolveOpen) Should target running instance of intent-a app when raising intent 'aTestingIntent1' with context 'testContextX' after opening intent-a app";
it(RaiseIntentTargetedInstanceResolveOpen, async () => {
// add app control listeners
errorListener = await control.listenForError();
const confirmAppOpened = control.receiveContext("intent-app-a-opened");
const result = control.receiveContext("aTestingIntent-listener-triggered");

const appIdentifier = await control.openIntentApp(IntentApp.IntentAppA);
await confirmAppOpened;

const intentResolution = await control.raiseIntent(Intent.aTestingIntent, ContextType.testContextX, appIdentifier);
await result;
control.validateIntentResolution(IntentApp.IntentAppA, intentResolution);
const instances = await control.findInstances(IntentApp.IntentAppA);
control.validateInstances(instances, 1, appIdentifier.instanceId, (await result).instanceId);
});
// const RaiseIntentSingleResolve = "(2.0-RaiseIntentSingleResolve) Should start app intent-a when raising intent 'aTestingIntent1' with context 'testContextX'";
// it(RaiseIntentSingleResolve, async () => {
// errorListener = await control.listenForError();
// const result = control.receiveContext("aTestingIntent-listener-triggered");
// const intentResolution = await control.raiseIntent(Intent.aTestingIntent, ContextType.testContextX);
// control.validateIntentResolution(IntentApp.IntentAppA, intentResolution);
// await result;
// });

// const RaiseIntentTargetedAppResolve = "(2.0-RaiseIntentTargetedAppResolve) Should start app intent-b when raising intent 'sharedTestingIntent1' with context 'testContextX'";
// it(RaiseIntentTargetedAppResolve, async () => {
// errorListener = await control.listenForError();
// const result = control.receiveContext("sharedTestingIntent1-listener-triggered");
// const intentResolution = await control.raiseIntent(Intent.sharedTestingIntent1, ContextType.testContextX, {
// appId: IntentApp.IntentAppB,
// });
// control.validateIntentResolution(IntentApp.IntentAppB, intentResolution);
// await result;
// });

// const RaiseIntentTargetedInstanceResolveOpen = "(2.0-RaiseIntentTargetedInstanceResolveOpen) Should target running instance of intent-a app when raising intent 'aTestingIntent1' with context 'testContextX' after opening intent-a app";
// it(RaiseIntentTargetedInstanceResolveOpen, async () => {
// // add app control listeners
// errorListener = await control.listenForError();
// const confirmAppOpened = control.receiveContext("intent-app-a-opened");
// const result = control.receiveContext("aTestingIntent-listener-triggered");

// const appIdentifier = await control.openIntentApp(IntentApp.IntentAppA);
// await confirmAppOpened;

// const intentResolution = await control.raiseIntent(Intent.aTestingIntent, ContextType.testContextX, appIdentifier);
// await result;
// control.validateIntentResolution(IntentApp.IntentAppA, intentResolution);
// const instances = await control.findInstances(IntentApp.IntentAppA);
// control.validateInstances(instances, 1, appIdentifier.instanceId, (await result).instanceId);
// });

const RaiseIntentTargetedInstanceResolveFindInstances = "(2.0-RaiseIntentTargetedInstanceResolveFindInstances) Should start app intent-a when targeted by raising intent 'aTestingIntent1' with context 'testContextX'";
it(RaiseIntentTargetedInstanceResolveFindInstances, async () => {
Expand All @@ -65,7 +65,11 @@ export default () =>
const confirmAppOpened = control.receiveContext("intent-app-a-opened");
const result = control.receiveContext("aTestingIntent-listener-triggered");

const appOpen = control.receiveContext(ControlContextType.intentAppAOpened, 2000, 1)
const appIdentifier = await control.openIntentApp(IntentApp.IntentAppA);

await appOpen

const instances = await control.findInstances(IntentApp.IntentAppA);
control.validateInstances(instances, 1, appIdentifier.instanceId);
await confirmAppOpened;
Expand All @@ -78,52 +82,52 @@ export default () =>
expect(instances2.length).to.be.equal(1); //make sure no other instance is started
});

const PrivateChannelsAreNotAppChannels = "(2.0-PrivateChannelsAreNotAppChannels) Cannot create an app channel using a private channel id";
it(PrivateChannelsAreNotAppChannels, async () => {
errorListener = await control.listenForError();
const privChan = await control.createPrivateChannel();
control.validatePrivateChannel(privChan);
const privChan2 = await control.createPrivateChannel();
control.validatePrivateChannel(privChan2);

expect(privChan.id).to.not.be.equal(privChan2.id); //check that the ids of both private channels are different
try {
await control.createAppChannel(privChan.id);
assert.fail("No error was not thrown when calling fdc3.getOrCreateChannel(privateChannel.id)");
} catch (ex) {
expect(ex).to.have.property("message", ChannelError.AccessDenied, `Incorrect error received when calling fdc3.getOrCreateChannel(privateChannel.id). Expected AccessDenied, got ${ex.message}`);
}

const intentResolution = await control.raiseIntent(Intent.privateChannelIsPrivate, ContextType.privateChannelDetails, undefined, undefined, { key: privChan2.id });
control.validateIntentResolution(IntentApp.IntentAppJ, intentResolution);
let result = await control.getIntentResult(intentResolution);
control.validateIntentResult(result, IntentResultType.Context, ContextType.privateChannelDetails);
});

const PrivateChannelsLifecycleEvents = "(2.0-PrivateChannelsLifecycleEvents) PrivateChannel lifecycle events are triggered when expected";
it(PrivateChannelsLifecycleEvents, async () => {
errorListener = await control.listenForError();
const onUnsubscribeReceiver = control.receiveContext("onUnsubscribeTriggered");
const intentResolution = await control.raiseIntent(Intent.kTestingIntent, ContextType.testContextX, {
appId: IntentApp.IntentAppK,
});
control.validateIntentResolution(IntentApp.IntentAppK, intentResolution);
let result = await control.getIntentResult(intentResolution);
control.validateIntentResult(result, IntentResultType.PrivateChannel);
let listener = await control.receiveContextStreamFromMockApp(<PrivateChannel>result, 1, 5);
control.unsubscribeListener(listener);
await onUnsubscribeReceiver; //should receive context from privChannel.onUnsubscribe in mock app
const textContextXReceiver = control.receiveContext(ContextType.testContextX);
control.privateChannelBroadcast(<PrivateChannel>result, ContextType.testContextX);
await textContextXReceiver;
const onUnsubscribeReceiver2 = control.receiveContext("onUnsubscribeTriggered");
const onDisconnectReceiver = control.receiveContext("onDisconnectTriggered");
let listener2 = await control.receiveContextStreamFromMockApp(<PrivateChannel>result, 6, 10);
control.disconnectPrivateChannel(<PrivateChannel>result);

//confirm that onUnsubscribe and onDisconnect were triggered in intent-k
await onUnsubscribeReceiver2;
await onDisconnectReceiver;
control.unsubscribeListener(listener2);
});
// const PrivateChannelsAreNotAppChannels = "(2.0-PrivateChannelsAreNotAppChannels) Cannot create an app channel using a private channel id";
// it(PrivateChannelsAreNotAppChannels, async () => {
// errorListener = await control.listenForError();
// const privChan = await control.createPrivateChannel();
// control.validatePrivateChannel(privChan);
// const privChan2 = await control.createPrivateChannel();
// control.validatePrivateChannel(privChan2);

// expect(privChan.id).to.not.be.equal(privChan2.id); //check that the ids of both private channels are different
// try {
// await control.createAppChannel(privChan.id);
// assert.fail("No error was not thrown when calling fdc3.getOrCreateChannel(privateChannel.id)");
// } catch (ex) {
// expect(ex).to.have.property("message", ChannelError.AccessDenied, `Incorrect error received when calling fdc3.getOrCreateChannel(privateChannel.id). Expected AccessDenied, got ${ex.message}`);
// }

// const intentResolution = await control.raiseIntent(Intent.privateChannelIsPrivate, ContextType.privateChannelDetails, undefined, undefined, { key: privChan2.id });
// control.validateIntentResolution(IntentApp.IntentAppJ, intentResolution);
// let result = await control.getIntentResult(intentResolution);
// control.validateIntentResult(result, IntentResultType.Context, ContextType.privateChannelDetails);
// });

// const PrivateChannelsLifecycleEvents = "(2.0-PrivateChannelsLifecycleEvents) PrivateChannel lifecycle events are triggered when expected";
// it(PrivateChannelsLifecycleEvents, async () => {
// errorListener = await control.listenForError();
// const onUnsubscribeReceiver = control.receiveContext("onUnsubscribeTriggered");
// const intentResolution = await control.raiseIntent(Intent.kTestingIntent, ContextType.testContextX, {
// appId: IntentApp.IntentAppK,
// });
// control.validateIntentResolution(IntentApp.IntentAppK, intentResolution);
// let result = await control.getIntentResult(intentResolution);
// control.validateIntentResult(result, IntentResultType.PrivateChannel);
// let listener = await control.receiveContextStreamFromMockApp(<PrivateChannel>result, 1, 5);
// control.unsubscribeListener(listener);
// await onUnsubscribeReceiver; //should receive context from privChannel.onUnsubscribe in mock app
// const textContextXReceiver = control.receiveContext(ContextType.testContextX);
// control.privateChannelBroadcast(<PrivateChannel>result, ContextType.testContextX);
// await textContextXReceiver;
// const onUnsubscribeReceiver2 = control.receiveContext("onUnsubscribeTriggered");
// const onDisconnectReceiver = control.receiveContext("onDisconnectTriggered");
// let listener2 = await control.receiveContextStreamFromMockApp(<PrivateChannel>result, 6, 10);
// control.disconnectPrivateChannel(<PrivateChannel>result);

// //confirm that onUnsubscribe and onDisconnect were triggered in intent-k
// await onUnsubscribeReceiver2;
// await onDisconnectReceiver;
// control.unsubscribeListener(listener2);
// });
});
2 changes: 1 addition & 1 deletion src/test/v2.0/fdc3-2_0-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const waitForContext = async (contextType: string, testId: string, channe
}
}, 500)

return await channel.addContextListener(contextType, ctx => {
return channel.addContextListener(contextType, ctx => {
if (ctx['testId'] == testId) {
console.log(`Received ${contextType}`)
count--;
Expand Down
4 changes: 3 additions & 1 deletion src/test/v2.0/support/intent-support-2.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export class RaiseIntentControl2_0 {
const { promise: sleepPromise, timeout: theTimeout } = sleep(waitTime ?? constants.WaitTime);
timeout = theTimeout;
await sleepPromise;
reject(new Error("No context received. Listener expected to receive context of type " + contextType + " from mock app"));
if (count > 0) {
reject(new Error("No context received. Listener expected to receive context of type " + contextType + " from mock app"));
}
});
}

Expand Down

0 comments on commit e0437ab

Please sign in to comment.