Skip to content

Commit

Permalink
Completed user channel tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoffat committed Nov 30, 2023
1 parent 6490d56 commit d0de9cd
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/da/dist/channels/DefaultChannel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export declare class DefaultChannel implements StatefulChannel {
readonly type: "user" | "app" | "private";
readonly displayMetadata?: DisplayMetadata | undefined;
readonly latestContextMap: Map<string, Context>;
readonly latestContext: Context | null;
private latestContext;
readonly listeners: Listener[];
constructor(messaging: Messaging, id: string, type: "user" | "app" | "private", displayMetadata?: DisplayMetadata);
broadcast(context: Context): Promise<void>;
Expand Down
5 changes: 5 additions & 0 deletions packages/da/dist/channels/DefaultChannel.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion packages/da/src/channels/DefaultChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ export class DefaultChannel implements StatefulChannel {
readonly displayMetadata?: DisplayMetadata | undefined;

readonly latestContextMap: Map<string, Context> = new Map()
readonly latestContext: Context | null = null
private latestContext: Context | null = null
readonly listeners: Listener[] = []

constructor(messaging: Messaging, id: string, type: "user" | "app" | "private", displayMetadata? : DisplayMetadata) {
this.messaging = messaging
this.id = id
this.type = type
this.displayMetadata = displayMetadata
this.addContextListenerInner(null, (ctx) => {
this.latestContextMap.set(ctx.type, ctx);
this.latestContext = ctx;
})
}

broadcast(context: Context): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion packages/da/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/testing/dist/cucumber-report.html

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions packages/testing/dist/step-definitions/channels.steps.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 29 additions & 27 deletions packages/testing/test/features/user-channels.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Feature: Basic User Channels Support

Background: Desktop Agent API
Given A Basic API Setup
Given "instrumentMessageOne" is a "fdc3.instrument" broadcastRequest message on channel "one"

Scenario: List User Channels

Expand Down Expand Up @@ -35,37 +36,38 @@ Background: Desktop Agent API
Given "resultHandler" pipes context to "contexts"
When I call the API "joinUserChannel" with parameter "one"
And I call the API "addContextListener" with parameters "fdc3.instrument" and "{resultHandler}"
And messaging receives a "broadcastRequest" with payload:
"""
{
"channelId" : "one",
"context" : {
"type": "fdc3.instrument",
"name": "Apple",
"id" : {
"ticker": "AAPL"
}
}
}
"""
And messaging receives "{instrumentMessageOne}"
Then "contexts" is an array of objects with the following contents
| id.ticker | type | name |
| AAPL | fdc3.instrument | Apple |

Scenario: If you haven't joined a channel, your listener receives nothing
Given "resultHandler" pipes context to "contexts"
When I call the API "addContextListener" with parameters "fdc3.instrument" and "{resultHandler}"
And messaging receives a "broadcastRequest" with payload:
"""
{
"channelId" : "one",
"context" : {
"type": "fdc3.instrument",
"name": "Apple",
"id" : {
"ticker": "AAPL"
}
}
}
"""
Then "contexts" is empty
And messaging receives "{instrumentMessageOne}"
Then "contexts" is empty

Scenario: Adding a listener to a user channel replays Context

Although the message is sent before the listener is added, history from the channel will get replayed

Given "resultHandler" pipes context to "contexts"
When messaging receives "{instrumentMessageOne}"
And I call the API "joinUserChannel" with parameter "one"
And I call the API "addContextListener" with parameters "fdc3.instrument" and "{resultHandler}"
Then "contexts" is an array of objects with the following contents
| id.ticker | type | name |
| AAPL | fdc3.instrument | Apple |

Scenario: Joining a user channel replays Context to listeners

Although the message is sent before the channel is joined, history from the channel will get replayed
to the listener

Given "resultHandler" pipes context to "contexts"
When messaging receives "{instrumentMessageOne}"
And I call the API "addContextListener" with parameters "fdc3.instrument" and "{resultHandler}"
And I call the API "joinUserChannel" with parameter "one"
Then "contexts" is an array of objects with the following contents
| id.ticker | type | name |
| AAPL | fdc3.instrument | Apple |
31 changes: 30 additions & 1 deletion packages/testing/test/step-definitions/channels.steps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { Given, Then, When } from '@cucumber/cucumber'
import { Context } from '@finos/fdc3';
import { handleResolve } from '../support/matching';

const contextMap : Record<string, any> = {
"fdc3.instrument": {
"type": "fdc3.instrument",
"name": "Apple",
"id" : {
"ticker": "AAPL"
}
}
}

Given('{string} is a {string} broadcastRequest message on channel {string}', function(field: string, type: string, channel: string) {
const message = {
meta: this.messaging.createMeta(),
payload: {
"channelId" : channel,
"context" : contextMap[type]
},
type: "broadcastRequest"
}

this[field] = message;
})


Given('{string} pipes context to {string}', function(contextHandlerName, field) {
this[field] = []
Expand All @@ -19,4 +44,8 @@ When('messaging receives a {string} with payload:', function (type, docString) {
this.messaging.receive(message, this.log);
});


When('messaging receives {string}', function (field) {
const message = handleResolve(field, this)
this.log(`Sending: ${JSON.stringify(message)}`)
this.messaging.receive(message, this.log);
});

0 comments on commit d0de9cd

Please sign in to comment.