From 4666a5ee56846f14dd1ba79dab82891505b5a2ec Mon Sep 17 00:00:00 2001 From: kpears201 Date: Wed, 1 Nov 2023 13:02:25 -0700 Subject: [PATCH] fix: Provider error (#202) * fix: Errors from providers not sent through transport * fix: Errors from providers not sent through transport --- package-lock.json | 16 +-- package.json | 4 +- src/sdks/manage/test/suite/keyboard.test.ts | 143 +++++++++++++++++++- 3 files changed, 147 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index 670471d79..27619df47 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,7 @@ "devDependencies": { "@commitlint/cli": "^17.0.3", "@commitlint/config-conventional": "^17.0.3", - "@firebolt-js/openrpc": "2.1.0", + "@firebolt-js/openrpc": "2.1.1", "@firebolt-js/schemas": "2.0.0", "@saithodev/semantic-release-backmerge": "^3.2.0", "@semantic-release/changelog": "^6.0.1", @@ -1062,9 +1062,9 @@ "link": true }, "node_modules/@firebolt-js/openrpc": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/@firebolt-js/openrpc/-/openrpc-2.1.0.tgz", - "integrity": "sha512-xWyaYJSrORLtLISOlR1+b1PUAS6OKo952tf49cPWnWR5xoCRZa2DIXy9QBWcAKpFrokMtWIggu9R5rqPrZH+Rw==", + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/@firebolt-js/openrpc/-/openrpc-2.1.1.tgz", + "integrity": "sha512-rWwb23ehZpKMQS8TJZiyKo6ziF1Zae/tgDXprTwuwF6vPhHV6vgb/HO1M6o+YI54VDEPDozHCL+IhN+KkT9ucw==", "dev": true, "dependencies": { "ajv": "^8.3.0", @@ -17862,9 +17862,9 @@ } }, "@firebolt-js/openrpc": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/@firebolt-js/openrpc/-/openrpc-2.1.0.tgz", - "integrity": "sha512-xWyaYJSrORLtLISOlR1+b1PUAS6OKo952tf49cPWnWR5xoCRZa2DIXy9QBWcAKpFrokMtWIggu9R5rqPrZH+Rw==", + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/@firebolt-js/openrpc/-/openrpc-2.1.1.tgz", + "integrity": "sha512-rWwb23ehZpKMQS8TJZiyKo6ziF1Zae/tgDXprTwuwF6vPhHV6vgb/HO1M6o+YI54VDEPDozHCL+IhN+KkT9ucw==", "dev": true, "requires": { "ajv": "^8.3.0", @@ -29310,4 +29310,4 @@ "dev": true } } -} +} \ No newline at end of file diff --git a/package.json b/package.json index a1126d0c9..990ffee26 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "devDependencies": { "@commitlint/cli": "^17.0.3", "@commitlint/config-conventional": "^17.0.3", - "@firebolt-js/openrpc": "2.1.0", + "@firebolt-js/openrpc": "2.1.1", "@firebolt-js/schemas": "2.0.0", "@saithodev/semantic-release-backmerge": "^3.2.0", "@semantic-release/changelog": "^6.0.1", @@ -67,4 +67,4 @@ "sdk" ], "license": "Apache-2.0" -} +} \ No newline at end of file diff --git a/src/sdks/manage/test/suite/keyboard.test.ts b/src/sdks/manage/test/suite/keyboard.test.ts index 501a10479..c40b7d40e 100644 --- a/src/sdks/manage/test/suite/keyboard.test.ts +++ b/src/sdks/manage/test/suite/keyboard.test.ts @@ -16,15 +16,97 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { test, expect } from "@jest/globals"; -import { Keyboard } from "../../build/javascript/src/firebolt-manage"; +import { test, expect, beforeAll } from "@jest/globals"; +import { Keyboard, Settings } from "../../build/javascript/src/firebolt-manage"; + +const state = { + cb: null, + eventId: null, + pending: [] +} + +class MockProviderBroker { + + constructor() { + } + + send(msg) { + let parsed = JSON.parse(msg) + if (parsed.method === 'keyboard.onRequestStandard') { + state.eventId = parsed.id + } + if ((parsed.method === 'keyboard.standardResponse') || (parsed.method === 'keyboard.standardError')) { + let pending = state.pending.find(p => p.correlationId === parsed.params.correlationId) + state.pending = state.pending.filter(p => p.correlationId === parsed.params.correlationId) + if (pending) { + pending.callback(parsed) + } + } + } + + receive(callback) { + state.cb = callback + } + + async triggerProvider(msg, providerCallback) { + let fullMsg = { + jsonrpc: '2.0', + id: state.eventId, + result: { + correlationId: '' + Math.round((Math.random() * 1000000)), + parameters: msg + } + } + state.pending.push({ + correlationId: fullMsg.result.correlationId, + callback: providerCallback + }) + state.cb(JSON.stringify(fullMsg)) + } +} +const broker = new MockProviderBroker() +let provider = null + +beforeAll(async () => { + Settings.setLogLevel('DEBUG') + window['__firebolt'].setTransportLayer(new MockProviderBroker()) + provider = new DelegatingKBProvider(new KBProvider()) + await Keyboard.provide("xrn:firebolt:capability:input:keyboard", provider); +}) + +class DelegatingKBProvider implements Keyboard.KeyboardInputProvider { + delegate: Keyboard.KeyboardInputProvider; + constructor(delegate: Keyboard.KeyboardInputProvider) { + this.delegate = delegate; + } + standard( + parameters: Keyboard.KeyboardParameters, + session: Keyboard.FocusableProviderSession + ): Promise { + return this.delegate.standard(parameters, session) + } + password( + parameters: Keyboard.KeyboardParameters, + session: Keyboard.FocusableProviderSession + ): Promise { + return this.delegate.password(parameters, session) + } + email( + parameters: Keyboard.KeyboardParameters, + session: Keyboard.FocusableProviderSession + ): Promise { + return this.delegate.email(parameters, session) + } +} class KBProvider implements Keyboard.KeyboardInputProvider { standard( parameters: Keyboard.KeyboardParameters, session: Keyboard.FocusableProviderSession ): Promise { - return Promise.resolve(null); + return Promise.resolve({ + text: 'foo' + }); } password( parameters: Keyboard.KeyboardParameters, @@ -40,9 +122,41 @@ class KBProvider implements Keyboard.KeyboardInputProvider { } } -test("Keyboard.provide() declarations", () => { - Keyboard.provide("xrn:firebolt:capability:input:keyboard", new KBProvider()); - expect(1).toBe(1); +class KBProviderWithError implements Keyboard.KeyboardInputProvider { + async standard( + parameters: Keyboard.KeyboardParameters, + session: Keyboard.FocusableProviderSession + ): Promise { + throw new Error('failed') + } + async password( + parameters: Keyboard.KeyboardParameters, + session: Keyboard.FocusableProviderSession + ): Promise { + throw new Error('failed') + } + async email( + parameters: Keyboard.KeyboardParameters, + session: Keyboard.FocusableProviderSession + ): Promise { + throw new Error('failed') + } +} + +test("Keyboard.provide() declarations", async () => { + let callback = null; + let promise: Promise = new Promise((resolve, reject) => { + callback = resolve + }) + provider.delegate = new KBProvider() + await broker.triggerProvider({ + message: 'Enter name', + type: 'standard' + }, callback) + let result = await promise + console.log(result) + expect(result.method).toStrictEqual('keyboard.standardResponse') + expect(result.params.result.text).toStrictEqual('foo') }); test("Keyboard.provide() with blank object", () => { @@ -51,6 +165,23 @@ test("Keyboard.provide() with blank object", () => { }).toThrow(); }); +test("Keyboard.provide() with error response", async () => { + let callback = null; + let promise: Promise = new Promise((resolve, reject) => { + callback = resolve + }) + provider.delegate = new KBProviderWithError() + await broker.triggerProvider({ + message: 'Enter name', + type: 'standard' + }, callback) + let result = await promise + console.log(result) + expect(result.method).toStrictEqual('keyboard.standardError') + expect(result.params.error.message).toStrictEqual('failed') + expect(result.params.error.code).toStrictEqual(1000) +}); + // Events Test cases // test("Keyboard.listen() for requestEmail event", () => {