Skip to content

Commit

Permalink
Still failing some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoffat committed Jun 7, 2024
1 parent 9da8c76 commit 3f0bc09
Show file tree
Hide file tree
Showing 23 changed files with 198 additions and 94 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@kite9/web-fdc3",
"private": true,
"version": "0.0.27",
"version": "0.0.29",
"workspaces": [
"packages/*"
],
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@kite9/client",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"version": "0.0.27",
"version": "0.0.29",
"scripts": {
"build": "tsc --module es2022",
"clean": "rimraf dist; rimraf cucumber-report.html; rimraf coverage"
Expand Down
2 changes: 1 addition & 1 deletion packages/da-proxy/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kite9/da-proxy",
"version": "0.0.27",
"version": "0.0.29",
"files": [
"dist"
],
Expand Down
37 changes: 26 additions & 11 deletions packages/da-proxy/src/channels/DefaultChannelSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { DefaultChannel } from "./DefaultChannel";
import { StatefulChannel } from "./StatefulChannel";
import { DefaultContextListener } from "../listeners/DefaultContextListener";
import { ContextElement } from "@finos/fdc3/dist/bridging/BridgingTypes";
import { RegisterChannelAgentRequest, RegisterChannelAgentResponse } from "@kite9/fdc3-common";

export class DefaultChannelSupport implements ChannelSupport {

readonly messaging: Messaging
protected userChannel: StatefulChannel | null
protected userChannelState: StatefulChannel[]
protected userChannelListeners: DefaultContextListener[] = []
protected readonly privateChannelIds: string[] = []

constructor(messaging: Messaging, userChannelState: StatefulChannel[], initialChannelId: string | null) {
this.messaging = messaging;
Expand Down Expand Up @@ -52,19 +52,34 @@ export class DefaultChannelSupport implements ChannelSupport {
}
}

getOrCreate(id: string): Promise<Channel> {
if (this.privateChannelIds.find(c => c == id)) {
return Promise.reject(new Error(ChannelError.AccessDenied))
} else {
const out = new DefaultChannel(this.messaging, id, "app", this.getDisplayMetadata(id))
return Promise.resolve(out)
async registerChannel(channelId: string, type: "user" | "app" | "private"): Promise<void> {
const response = await this.messaging.exchange<RegisterChannelAgentResponse>({
meta: this.messaging.createMeta(),
type: 'registerChannelRequest',
payload: {
channelId,
type
}
} as RegisterChannelAgentRequest,
'registerChannelResponse')

const error = response.payload.error
if (error) {
throw new Error(error)
}
}

createPrivateChannel(): Promise<PrivateChannel> {
const out = new DefaultPrivateChannel(this.messaging, this.messaging.createUUID())
this.privateChannelIds.push(out.id)
return Promise.resolve(out);

async getOrCreate(id: string): Promise<Channel> {
await this.registerChannel(id, 'app')
const out = new DefaultChannel(this.messaging, id, "app", this.getDisplayMetadata(id))
return out
}

async createPrivateChannel(): Promise<PrivateChannel> {
const id = this.messaging.createUUID()
await this.registerChannel(id, 'private')
return new DefaultPrivateChannel(this.messaging, id)
}

leaveUserChannel(): Promise<void> {
Expand Down
22 changes: 16 additions & 6 deletions packages/da-proxy/src/intents/DefaultIntentSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ export class DefaultIntentSupport implements IntentSupport {
return result.payload.appIntents
}

private async createResultPromise(messageOut: RaiseIntentAgentRequest): Promise<IntentResult> {
const rp = await this.messaging.waitFor<RaiseIntentResultAgentResponse>(m => (
(m.meta.requestUuid == messageOut.meta.requestUuid) &&
(m.type == 'raiseIntentResultResponse')))

if (!rp) {
// probably a timeout
return;
} else {
const ir = await convertIntentResult(rp, this.messaging)
this.intentResolver.intentChosen(ir)
return ir
}
}

private async raiseSpecificIntent(intent: string, context: Context, app: AppIdentifier): Promise<IntentResolution> {
const messageOut: RaiseIntentAgentRequest = {
type: "raiseIntentRequest",
Expand All @@ -93,12 +108,7 @@ export class DefaultIntentSupport implements IntentSupport {
meta: this.messaging.createMeta() as RaiseIntentAgentRequestMeta
}

const resultPromise = this.messaging.waitFor<RaiseIntentResultAgentResponse>(m => (
(m.meta.requestUuid == messageOut.meta.requestUuid) &&
(m.type == 'raiseIntentResultResponse')))
.then(ir => convertIntentResult(ir, this.messaging))
.then(ir => this.intentResolver.intentChosen(ir))

const resultPromise = this.createResultPromise(messageOut)
const resolution = await this.messaging.exchange(messageOut, "raiseIntentResponse") as RaiseIntentAgentResponse
const error = (resolution as any as RaiseIntentAgentErrorResponse).payload.error

Expand Down
48 changes: 48 additions & 0 deletions packages/da-proxy/test/features/app-channels.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Feature: Channel Listeners Support

Background: Desktop Agent API
Given A Desktop Agent in "api1"
Given "instrumentMessageOne" is a "broadcastRequest" message on channel "channel-name" with context "fdc3.instrument"
Given "countryMessageOne" is a "broadcastRequest" message on channel "channel-name" with context "fdc3.country"
Given "instrumentContext" is a "fdc3.instrument" context
# Scenario: Configuring two context listeners should mean they both pick up data
# Given "resultHandler" pipes context to "contexts"
# When I call "api1" with "getOrCreateChannel" with parameter "channel-name"
# And I refer to "result" as "channel1"
# And I call "channel1" with "addContextListener" with parameters "fdc3.instrument" and "{resultHandler}"
# And I call "channel1" with "addContextListener" with parameters "fdc3.instrument" and "{resultHandler}"
# And messaging receives "{instrumentMessageOne}"
# Then "{contexts}" is an array of objects with the following contents
# | id.ticker | type | name |
# | AAPL | fdc3.instrument | Apple |
# | AAPL | fdc3.instrument | Apple |
# Scenario: I can create a listener which listens for any context type
# In this version we are using the deprecated no-args approach
# Given "resultHandler" pipes context to "contexts"
# When I call "api1" with "getOrCreateChannel" with parameter "channel-name"
# And I refer to "result" as "channel1"
# And I call "channel1" with "addContextListener" with parameter "{resultHandler}"
# And messaging receives "{instrumentMessageOne}"
# And messaging receives "{countryMessageOne}"
# Then "{contexts}" is an array of objects with the following contents
# | type | name |
# | fdc3.instrument | Apple |
# | fdc3.country | Sweden |
# Scenario: I can create a listener which listens for any context type
# In this version we are using the non-deprecated 2 args approach
# Given "resultHandler" pipes context to "contexts"
# When I call "api1" with "getOrCreateChannel" with parameter "channel-name"
# And I refer to "result" as "channel1"
# And I call "channel1" with "addContextListener" with parameters "{null}" and "{resultHandler}"
# And messaging receives "{instrumentMessageOne}"
# And messaging receives "{countryMessageOne}"
# Then "{contexts}" is an array of objects with the following contents
# | type | name |
# | fdc3.instrument | Apple |
# | fdc3.country | Sweden |

Scenario: I can't register an app channel with the same ID as a private channel
When I call "api1" with "createPrivateChannel"
And I refer to "result" as "privateChannel"
And I call "api1" with "getOrCreateChannel" with parameter "{privateChannel.id}"
Then "{result}" is an error with message "AccessDenied"
4 changes: 3 additions & 1 deletion packages/da-proxy/test/support/TestMessaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { GetAppMetadata } from "./responses/GetAppMetadata";
import { FindInstances } from "./responses/FindInstances";
import { Open } from "./responses/Open";
import { Handshake } from "./responses/Handshake";
import { RegisterChannel } from "./responses/RegisterChannel";

export interface IntentDetail {
app?: AppIdentifier,
Expand Down Expand Up @@ -87,7 +88,8 @@ export class TestMessaging extends AbstractMessaging {
new GetAppMetadata(),
new FindInstances(),
new Open(),
new Handshake()
new Handshake(),
new RegisterChannel()
]

constructor(channelState: { [key: string]: ContextElement[] }) {
Expand Down
49 changes: 49 additions & 0 deletions packages/da-proxy/test/support/responses/RegisterChannel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { AutomaticResponse, TestMessaging } from "../TestMessaging";
import { RegisterChannelAgentRequest, RegisterChannelAgentResponse } from "@kite9/fdc3-common";
import { ChannelError } from "@finos/fdc3";

type ChannelType = { [channelId: string]: 'user' | 'app' | 'private' }

export class RegisterChannel implements AutomaticResponse {

private type: ChannelType = {}


filter(t: string) {
return t == 'registerChannelRequest'
}

action(input: object, m: TestMessaging) {
const out = this.registerChannel(input as RegisterChannelAgentRequest)

setTimeout(() => { m.receive(out as any) }, 100)
return Promise.resolve()
}

registerChannel(r: RegisterChannelAgentRequest): RegisterChannelAgentResponse {
const id = r.payload.channelId
const type = r.payload.type

const existingType = this.type[id]

if ((existingType) && (existingType != type)) {
// channel already exists
return {
type: "registerChannelResponse",
meta: r.meta,
payload: {
error: ChannelError.AccessDenied
}
}

} else {
this.type[id] = type
return {
type: "registerChannelResponse",
meta: r.meta,
payload: {
}
}
}
}
}
50 changes: 0 additions & 50 deletions packages/da-proxy/test/unused-features/app-channels.feature

This file was deleted.

2 changes: 1 addition & 1 deletion packages/da-server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kite9/da-server",
"version": "0.0.27",
"version": "0.0.29",
"files": [
"dist"
],
Expand Down
34 changes: 32 additions & 2 deletions packages/da-server/src/handlers/BroadcastHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
PrivateChannelOnUnsubscribeAgentRequest,
PrivateChannelBroadcastAgentRequest
} from "@finos/fdc3/dist/bridging/BridgingTypes";
import { ContextElement } from "@finos/fdc3";
import { OnAddContextListenerAgentRequest, OnUnsubscribeAgentRequest } from "@kite9/fdc3-common";
import { ChannelError, ContextElement } from "@finos/fdc3";
import { OnAddContextListenerAgentRequest, OnUnsubscribeAgentRequest, RegisterChannelAgentRequest } from "@kite9/fdc3-common";

type ListenerRegistration = {
appId: string,
Expand Down Expand Up @@ -36,11 +36,13 @@ function createListenerRegistration(msg:
}

type ChannelState = { [channelId: string]: ContextElement[] }
type ChannelType = { [channelId: string]: 'user' | 'app' | 'private' }

export class BroadcastHandler implements MessageHandler {

private regs: ListenerRegistration[] = []
private state: ChannelState = {}
private type: ChannelType = {}
private readonly desktopAgentName: string

constructor(name: string) {
Expand All @@ -49,6 +51,10 @@ export class BroadcastHandler implements MessageHandler {

accept(msg: any, sc: ServerContext, from: AppMetadata) {
switch (msg.type as string | null) {
// channel registration
case 'registerChannelRequest': return this.registerChannel(msg as RegisterChannelAgentRequest, sc, from)

// private channels
case 'PrivateChannel.broadcast': return this.handleBroadcast(msg as PrivateChannelBroadcastAgentRequest, sc)
case 'PrivateChannel.onAddContextListener': return this.handleOnAddContextListener(msg as PrivateChannelOnAddContextListenerAgentRequest, sc)
case 'PrivateChannel.onUnsubscribe': return this.handleOnUnsubscribe(msg as PrivateChannelOnUnsubscribeAgentRequest, sc)
Expand Down Expand Up @@ -154,6 +160,30 @@ export class BroadcastHandler implements MessageHandler {
}

}

registerChannel(r: RegisterChannelAgentRequest, sc: ServerContext, from: AppMetadata) {
const id = r.payload.channelId
const type = r.payload.type

const existingType = this.type[id]

if ((existingType) && (existingType != type)) {
sc.post({
type: "registerChannelResponse",
payload: {
error: ChannelError.AccessDenied
}
}, from)
} else {
this.type[id] = type
sc.post({
type: "registerChannelResponse",
payload: {
}
}, from)
}
}

}


2 changes: 1 addition & 1 deletion packages/fdc3-common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kite9/fdc3-common",
"version": "0.0.27",
"version": "0.0.29",
"files": [
"dist"
],
Expand Down
Loading

0 comments on commit 3f0bc09

Please sign in to comment.