Skip to content

Commit

Permalink
fix: Provider tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jlacivita committed Jun 5, 2024
1 parent ba25420 commit 3eef17a
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 103 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"sdk": "npm run sdk --workspaces",
"docs": "npm run docs --workspaces",
"wiki": "npm run wiki --workspaces",
"test:setup": "npm run test:setup --workspaces",
"test:setup": "rm -rf test/transpiled-suite && npx tsc --target es6 --moduleResolution node --outDir test/transpiled-suite && npm run test:setup --workspaces",
"test": "npm run test:setup && NODE_OPTIONS=--experimental-vm-modules npx --config=jest.config.json --detectOpenHandles jest",
"clean": "rm -rf dist && npm run clean --workspaces",
"dist": "npm run fs:setup && npm run validate:each && npm run compile && npm run specification && npm run version && npm run dist:notest --workspaces && npm run test",
Expand Down
4 changes: 3 additions & 1 deletion src/sdks/core/test/suite/lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { jest, test, expect, beforeAll } from "@jest/globals";
import { testHarness } from "../../../../../test/Setup";
import { Lifecycle } from "../../build/javascript/src/firebolt";
import { Lifecycle, Settings } from "../../build/javascript/src/firebolt";

let readyResolved: boolean = false;
let readyCalled: boolean = false;
Expand All @@ -41,6 +41,8 @@ const callback = jest.fn();
const startupState: Lifecycle.LifecycleState = Lifecycle.state();

beforeAll(() => {
Settings.setLogLevel('DEBUG')

Lifecycle.listen((event: string, _) => {
callback(event);
});
Expand Down
131 changes: 48 additions & 83 deletions src/sdks/manage/test/suite/keyboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,23 @@ import { Keyboard, Settings } from "../../build/javascript/src/firebolt-manage";
const state = {
cb: null,
eventId: null,
pending: []
pending: [],
id: 1
}

class MockProviderBroker {

constructor() {
}

send(msg) {
let parsed = JSON.parse(msg)
if (parsed.method === 'keyboard.onRequestStandard') {
state.eventId = parsed.id
send(parsed) {
if (parsed.method === 'Keyboard.provide') {
// do we do anything?
}
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)
}
let pending = state.pending.find(p => p.id === parsed.id)
if (pending) {
state.pending = state.pending.filter(p => p.id === parsed.id)
pending.callback(parsed)
}
}

Expand All @@ -49,97 +47,70 @@ class MockProviderBroker {
}

async triggerProvider(msg, providerCallback) {
state.id++
let fullMsg = {
jsonrpc: '2.0',
id: state.eventId,
result: {
correlationId: '' + Math.round((Math.random() * 1000000)),
parameters: msg
}
id: state.id,
method: "Keyboard.standard",
params: msg
}

state.pending.push({
correlationId: fullMsg.result.correlationId,
id: state.id,
callback: providerCallback
})
state.cb(JSON.stringify(fullMsg))
state.cb(fullMsg)
}
}
const broker = new MockProviderBroker()
let provider = null

beforeAll(async () => {
window['__firebolt'].transport = new MockProviderBroker()
Settings.setLogLevel('DEBUG')
window['__firebolt'].setTransportLayer(new MockProviderBroker())
provider = new DelegatingKBProvider(new KBProvider())
await Keyboard.provide("xrn:firebolt:capability:input:keyboard", provider);
Keyboard.provide(provider);
})

class DelegatingKBProvider implements Keyboard.KeyboardInputProvider {
delegate: Keyboard.KeyboardInputProvider;
constructor(delegate: Keyboard.KeyboardInputProvider) {
class DelegatingKBProvider implements Keyboard.Keyboard {
delegate: Keyboard.Keyboard;

constructor(delegate: Keyboard.Keyboard) {
this.delegate = delegate;
}
standard(
parameters: Keyboard.KeyboardParameters,
session: Keyboard.FocusableProviderSession
): Promise<Keyboard.KeyboardResult> {
return this.delegate.standard(parameters, session)

standard(message: string): Promise<string> {
return this.delegate.standard(message)
}
password(
parameters: Keyboard.KeyboardParameters,
session: Keyboard.FocusableProviderSession
): Promise<Keyboard.KeyboardResult> {
return this.delegate.password(parameters, session)
password(message: string): Promise<string> {
return this.delegate.password(message)
}
email(
parameters: Keyboard.KeyboardParameters,
session: Keyboard.FocusableProviderSession
): Promise<Keyboard.KeyboardResult> {
return this.delegate.email(parameters, session)
email(type: Keyboard.EmailUsage, message: string): Promise<string> {
return this.delegate.email(type, message)
}
}

class KBProvider implements Keyboard.KeyboardInputProvider {
standard(
parameters: Keyboard.KeyboardParameters,
session: Keyboard.FocusableProviderSession
): Promise<Keyboard.KeyboardResult> {
return Promise.resolve({
text: 'foo'
});
class KBProvider implements Keyboard.Keyboard {
standard(message: string): Promise<string> {
return Promise.resolve('foo');
}
password(
parameters: Keyboard.KeyboardParameters,
session: Keyboard.FocusableProviderSession
): Promise<Keyboard.KeyboardResult> {
password(message: string): Promise<string> {
return Promise.resolve(null);
}
email(
parameters: Keyboard.KeyboardParameters,
session: Keyboard.FocusableProviderSession
): Promise<Keyboard.KeyboardResult> {
email(type: Keyboard.EmailUsage, message: string): Promise<string> {
return Promise.resolve(null);
}
}

class KBProviderWithError implements Keyboard.KeyboardInputProvider {
async standard(
parameters: Keyboard.KeyboardParameters,
session: Keyboard.FocusableProviderSession
): Promise<Keyboard.KeyboardResult> {
throw new Error('failed')
class KBProviderWithError implements Keyboard.Keyboard {
standard(message: string): Promise<string> {
throw { message: 'failed', code: 1000} //new Error('failed')
}
async password(
parameters: Keyboard.KeyboardParameters,
session: Keyboard.FocusableProviderSession
): Promise<Keyboard.KeyboardResult> {
throw new Error('failed')
password(message: string): Promise<string> {
throw { message: 'failed', code: 1000} //new Error('failed')
}
async email(
parameters: Keyboard.KeyboardParameters,
session: Keyboard.FocusableProviderSession
): Promise<Keyboard.KeyboardResult> {
throw new Error('failed')
email(type: Keyboard.EmailUsage, message: string): Promise<string> {
throw { message: 'failed', code: 1000} //new Error('failed')
}
}

Expand All @@ -149,20 +120,14 @@ test("Keyboard.provide() declarations", async () => {
callback = resolve
})
provider.delegate = new KBProvider()
await broker.triggerProvider({
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", () => {
expect(() => {
Keyboard.provide("xrn:firebolt:capability:input:keyboard", {});
}).toThrow();
// expect(result.method).toStrictEqual('keyboard.standardResponse')
expect(result.result).toStrictEqual('foo')
});

test("Keyboard.provide() with error response", async () => {
Expand All @@ -171,15 +136,15 @@ test("Keyboard.provide() with error response", async () => {
callback = resolve
})
provider.delegate = new KBProviderWithError()
await broker.triggerProvider({
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)
// expect(result.method).toStrictEqual('keyboard.standardError')
expect(result.error.message).toStrictEqual('failed')
expect(result.error.code).toStrictEqual(1000)
});

// Events Test cases
Expand Down
20 changes: 4 additions & 16 deletions src/sdks/manage/test/suite/pinChallenge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,17 @@
import { test, expect } from "@jest/globals";
import { PinChallenge } from "../../build/javascript/src/firebolt-manage";

class PCProvider implements PinChallenge.ChallengeProvider {
challenge(
parameters: object,
session: PinChallenge.FocusableProviderSession
): Promise<PinChallenge.PinChallengeResult> {
return Promise.resolve(null);
class PCProvider implements PinChallenge.PinChallenge {
challenge(requestor: PinChallenge.ChallengeRequestor, pinSpace: 'purchase' | 'content', capability?: string): Promise<PinChallenge.PinChallengeResult> {
return Promise.resolve(null)
}
}

test("PinChallenge.provide() declarations", () => {
PinChallenge.provide(
"xrn:firebolt:capability:usergrant:pinchallenge",
new PCProvider()
);
PinChallenge.provide(new PCProvider());
expect(1).toBe(1);
});

test("PinChallenge.provide() with blank object", () => {
expect(() => {
PinChallenge.provide("xrn:firebolt:capability:usergrant:pinchallenge", {});
}).toThrow();
});

// Events Test cases

// test("PinChallenge.listen() for requestChallenge event", () => {
Expand Down
Loading

0 comments on commit 3eef17a

Please sign in to comment.