From 2c5bc234415d2c2cbbc7fefcfea3e70a34f53637 Mon Sep 17 00:00:00 2001 From: Jeremy LaCivita Date: Thu, 2 May 2024 12:45:19 -0400 Subject: [PATCH] Revert tests back, to 1.0 --- package-lock.json | 2 +- src/sdks/core/package.json | 6 +- src/sdks/core/src/js/sdk/Device/index.mjs | 2 +- src/sdks/core/src/js/sdk/Lifecycle/index.mjs | 6 +- src/sdks/core/src/js/sdk/Metrics/index.mjs | 6 +- src/sdks/core/test/suite/declarations.test.ts | 6 +- src/sdks/core/test/suite/device.test.ts | 16 ++--- src/sdks/core/test/suite/discovery.test.ts | 10 +-- src/sdks/core/test/suite/federation.test.ts | 67 ++++++++----------- .../core/test/suite/lazy-transport.test.ts | 45 +++---------- src/sdks/core/test/suite/lifecycle.test.ts | 4 +- .../test/suite/listeners-transport.test.ts | 18 ++--- .../test/suite/synchronous-transport.test.ts | 67 +++++++++---------- test/helpers/synchronous-transport.mjs | 11 ++- 14 files changed, 115 insertions(+), 151 deletions(-) diff --git a/package-lock.json b/package-lock.json index 62639a332..5aa6bf4a8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,7 +37,7 @@ }, "../firebolt-openrpc": { "name": "@firebolt-js/openrpc", - "version": "3.0.0-next.1", + "version": "2.3.0-next.1", "dev": true, "license": "Apache-2.0", "dependencies": { diff --git a/src/sdks/core/package.json b/src/sdks/core/package.json index e008b74cb..253e8cb0c 100644 --- a/src/sdks/core/package.json +++ b/src/sdks/core/package.json @@ -9,9 +9,9 @@ }, "type": "module", "scripts": { - "validate": "npx firebolt-openrpc validate --input ./dist/firebolt-open-rpc.json", - "sdk": "npx firebolt-openrpc sdk --server ./dist/firebolt-open-rpc.json --client ./dist/firebolt-app-open-rpc.json --template ./src/js --output ./build/javascript/src", - "native": "npx firebolt-openrpc sdk --server ./dist/firebolt-open-rpc.json --client ./dist/firebolt-app-open-rpc.json --template ./src/cpp --output ./build/c/src --static-module Platform --language ../../../node_modules/@firebolt-js/openrpc/languages/cpp", + "validate": "npx firebolt-openrpc validate --input ./dist/firebolt-core-open-rpc.json", + "sdk": "npx firebolt-openrpc sdk --input ./dist/firebolt-core-open-rpc.json --template ./src/js --output ./build/javascript/src --static-module Platform", + "native": "npx firebolt-openrpc sdk --input ./dist/firebolt-core-open-rpc.json --template ./src/cpp --output ./build/c/src --static-module Platform --language ../../../node_modules/@firebolt-js/openrpc/languages/c", "compile": "cd ../../.. && npm run compile", "slice": "npm run slice:server; npm run slice:client", "slice:server": "npx firebolt-openrpc slice -i ../../../dist/firebolt-open-rpc.json --sdk ./sdk.config.json -o ./dist/firebolt-open-rpc.json", diff --git a/src/sdks/core/src/js/sdk/Device/index.mjs b/src/sdks/core/src/js/sdk/Device/index.mjs index 8d832eb2e..86f9fb39a 100644 --- a/src/sdks/core/src/js/sdk/Device/index.mjs +++ b/src/sdks/core/src/js/sdk/Device/index.mjs @@ -22,7 +22,7 @@ function version() { return new Promise( (resolve, reject) => { - Gateway.request('Device.version').then( v => { + Transport.send('device', 'version').then( v => { v = v || {} v.sdk = v.sdk || {} v.sdk.major = parseInt('${major}') diff --git a/src/sdks/core/src/js/sdk/Lifecycle/index.mjs b/src/sdks/core/src/js/sdk/Lifecycle/index.mjs index 1aaccd433..e0184ff91 100644 --- a/src/sdks/core/src/js/sdk/Lifecycle/index.mjs +++ b/src/sdks/core/src/js/sdk/Lifecycle/index.mjs @@ -31,10 +31,10 @@ export const store = { async function ready() { let readyRes; - prioritize('Lifecycle', (event, value) => { + await prioritize('Lifecycle', (event, value) => { store._current = event }) - readyRes = await Gateway.request('Lifecycle.ready', {}) + readyRes =await Transport.send('lifecycle', 'ready', {}) setTimeout(_ => { logReady() }) @@ -49,7 +49,7 @@ function state() { function finished() { if (store.current === 'unloading') { - return Gateway.request('Lifecycle.finished') + return Transport.send('lifecycle', 'finished') } else { throw 'Cannot call finished() except when in the unloading transition' } diff --git a/src/sdks/core/src/js/sdk/Metrics/index.mjs b/src/sdks/core/src/js/sdk/Metrics/index.mjs index b4955cca3..630914358 100644 --- a/src/sdks/core/src/js/sdk/Metrics/index.mjs +++ b/src/sdks/core/src/js/sdk/Metrics/index.mjs @@ -21,15 +21,15 @@ /* ${INITIALIZATION} */ function ready() { - return Gateway.request('Metrics.ready', {}) + return Transport.send('metrics', 'ready', {}) } function signIn() { - return Gateway.request('Metrics.signIn', {}) + return Transport.send('metrics', 'signIn', {}) } function signOut() { - return Gateway.request('Metrics.signOut', {}) + return Transport.send('metrics', 'signOut', {}) } diff --git a/src/sdks/core/test/suite/declarations.test.ts b/src/sdks/core/test/suite/declarations.test.ts index 2933329c7..8ec319eeb 100644 --- a/src/sdks/core/test/suite/declarations.test.ts +++ b/src/sdks/core/test/suite/declarations.test.ts @@ -2,7 +2,7 @@ import Setup from "../../../../../test/Setup" import { sent } from "../../../../../test/Setup" import { test, expect } from "@jest/globals" -import { Lifecycle, Device, Discovery, Entertainment } from "../../build/javascript/src/firebolt"; +import { Lifecycle, Device, Discovery } from "../../build/javascript/src/firebolt"; let listenerId:number @@ -39,7 +39,7 @@ const result: Discovery.EntityInfoResult = { "identifiers": { "entityId": "123" }, - "programType": Entertainment.ProgramType.MOVIE, + "programType": Discovery.ProgramType.MOVIE, "title": "A title" }, "expires": "" @@ -67,7 +67,7 @@ test('entityInfo pull', () => { return Promise.resolve(result) }) - Setup.emit('Discovery', 'pullEntityInfo', { + Setup.emit('discovery', 'pullEntityInfo', { correlationId: '123', parameters: { entityId: '123' diff --git a/src/sdks/core/test/suite/device.test.ts b/src/sdks/core/test/suite/device.test.ts index 84ddfd537..7f9601cac 100644 --- a/src/sdks/core/test/suite/device.test.ts +++ b/src/sdks/core/test/suite/device.test.ts @@ -17,33 +17,33 @@ */ import { test, expect } from "@jest/globals"; -import { Device, Types } from "../../build/javascript/src/firebolt"; +import { Device } from "../../build/javascript/src/firebolt"; test("Device.version()", () => { const debug: string = "Non-parsable build info for error logging only." - const os:Types.SemanticVersion = { + const os:Device.SemanticVersion = { major: 0, minor: 1, patch: 0, readable: "Firebolt OS v0.1.0" } - const sdk:Types.SemanticVersion = { + const sdk:Device.SemanticVersion = { major: 0, minor: 8, patch: 0, readable: "The Firebolt JS SDK", } - const firmware:Types.SemanticVersion = { + const firmware:Device.SemanticVersion = { major: 1, minor: 2, patch: 3, readable: "Device Firmware v1.2.3" } - const api:Types.SemanticVersion = { + const api:Device.SemanticVersion = { major: 0, minor: 8, patch: 0, @@ -112,17 +112,17 @@ test("Device.make()", () => { }); test("Device.hdcp()", () => { - const expectedOutput: Types.BooleanMap = { + const expectedOutput: Device.BooleanMap = { "hdcp1.4": true, "hdcp2.2": true, }; - return Device.hdcp().then((res: Types.BooleanMap) => { + return Device.hdcp().then((res: Device.BooleanMap) => { expect(res).toEqual(expectedOutput); }); }); test("Device.hdcp(subscriber)", () => { - return Device.hdcp((supportedHdrProfiles: Types.BooleanMap) => {}).then( + return Device.hdcp((supportedHdrProfiles: Device.BooleanMap) => {}).then( (res: number) => { expect(res > 0).toBe(true); } diff --git a/src/sdks/core/test/suite/discovery.test.ts b/src/sdks/core/test/suite/discovery.test.ts index 445d3ac47..638c57432 100644 --- a/src/sdks/core/test/suite/discovery.test.ts +++ b/src/sdks/core/test/suite/discovery.test.ts @@ -17,7 +17,7 @@ */ import { test, expect } from "@jest/globals"; -import { Discovery, Entertainment } from "../../build/javascript/src/firebolt"; +import { Discovery } from "../../build/javascript/src/firebolt"; test("watched(entityId)", () => { return Discovery.watched("abc").then((success: boolean) => { @@ -50,7 +50,7 @@ test("entityInfo(EntityInfoResult)", () => { }, title: "Test", entityType: "program", - programType: Entertainment.ProgramType.MOVIE, + programType: Discovery.ProgramType.MOVIE, }, }; return Discovery.entityInfo(dummyData).then((res: boolean) => { @@ -65,7 +65,7 @@ test("watchNext(title?: LocalizedString, identifiers: ContentIdentifiers, expire }); test("entitlements(entitlements: Entitlement[])", () => { - const dummyData: Array = [ + const dummyData: Array = [ { entitlementId: "test123", startTime: `${new Date().getTime()}`, @@ -84,7 +84,7 @@ test("launch(appId)", () => { }); test("signIn(appId)", () => { - const dummyData: Array = [ + const dummyData: Array = [ { entitlementId: "test123", startTime: `${new Date().getTime()}`, @@ -113,7 +113,7 @@ test("purchasedContent()", () => { }, title: "TEST", entityType: "program", - programType: Entertainment.ProgramType.MOVIE, + programType: Discovery.ProgramType.MOVIE, }, ], }; diff --git a/src/sdks/core/test/suite/federation.test.ts b/src/sdks/core/test/suite/federation.test.ts index a80fc180d..6e8dab8e1 100644 --- a/src/sdks/core/test/suite/federation.test.ts +++ b/src/sdks/core/test/suite/federation.test.ts @@ -20,12 +20,12 @@ // setup for Firebolt SDK/TL handshake import { test, expect, beforeAll } from "@jest/globals"; -import { Lifecycle, Discovery, Entertainment, Types } from '../../build/javascript/src/firebolt' +import { Lifecycle, Discovery } from '../../build/javascript/src/firebolt' // holds test transport layer state, e.g. callback const state = { - callback:(a:object) => {} + callback:(a:string) => {} } let pullEntityInfoListenCount = 0 @@ -40,8 +40,9 @@ let secondRegistrationFailed = false beforeAll(() => { return new Promise( (resolve, reject) => { const transport = { - send: function(json: any) { + send: function(message: string) { sendCalled = true + const json = JSON.parse(message) if (json.method.toLowerCase() === 'discovery.onpullentityinfo') { // we'll assert on this later... pullEntityInfoListenCount++ @@ -53,12 +54,12 @@ beforeAll(() => { id: json.id, result: { listening: true, - event: 'Discovery.onPullEntityInfo' + event: 'discovery.onPullEntityInfo' } } // catching errors, so all tests don't fail if this breaks try { - state.callback(response) + state.callback(JSON.stringify(response)) } catch (err) { throw err @@ -70,31 +71,27 @@ beforeAll(() => { try { response = { jsonrpc: '2.0', - method: 'Discovery.pullEntityInfo', - params: { - value: { - correlationId: correlationId, - parameters: { - entityId: "345" - } + id: json.id, + result: { + correlationId: correlationId, + parameters: { + entityId: "345" } } } - state.callback(response) + state.callback(JSON.stringify(response)) - state.callback({ + state.callback(JSON.stringify({ jsonrpc: '2.0', - method: 'Discovery.pullEntityInfo', - params: { - value: { - correlationId: 'this-will-fail', - parameters: { - entityId: "this-will-fail" - } + id: json.id, + result: { + correlationId: 'this-will-fail', + parameters: { + entityId: "this-will-fail" } } - }) + })) } catch (err) { throw err @@ -114,24 +111,21 @@ beforeAll(() => { else if (!json.params.correlationId && json.params.result.entity.identifiers.entityId === "PUSH:345") { entityInfoPushed = true } - - setTimeout(() => { - state.callback({ - jsonrpc: '2.0', - id: json.id, - result: true - }) - }, 100) + state.callback(JSON.stringify({ + jsonrpc: '2.0', + id: json.id, + result: true + })) } }, - receive: function(callback: (a:object) => void) { + receive: function(callback: (a:string) => void) { // store the callback state.callback = callback } } const win:any = window; - win.__firebolt.transport = transport + win.__firebolt.setTransportLayer(transport) const result:Discovery.EntityInfoResult = { "expires": "2025-01-01T00:00:00.000Z", @@ -140,7 +134,7 @@ beforeAll(() => { "entityId": "PUSH:345" }, "entityType": "program", - "programType": Entertainment.ProgramType.MOVIE, + "programType": Discovery.ProgramType.MOVIE, "title": "Cool Runnings", "synopsis": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Pulvinar sapien et ligula ullamcorper malesuada proin libero nunc.", "releaseDate": "1993-01-01T00:00:00.000Z", @@ -162,9 +156,9 @@ beforeAll(() => { "expires": "2025-01-01T00:00:00.000Z", "entitled": true, "entitledExpires": "2025-01-01T00:00:00.000Z", - "offeringType": Entertainment.OfferingType.BUY, + "offeringType": Discovery.OfferingType.BUY, "price": 2.99, - "audioProfile": [Types.AudioProfile.DOLBY_ATMOS], + "audioProfile": [Discovery.AudioProfile.DOLBY_ATMOS], "videoQuality": ["UHD"], "audioLanguages": [ "en" @@ -186,8 +180,6 @@ beforeAll(() => { // Setup a callback that returns the correct payload Discovery.entityInfo((parameters:Discovery.EntityInfoParameters) => { - console.dir(parameters) - if (parameters.entityId === 'this-will-fail') { throw "Intentional Test failure" } @@ -217,7 +209,6 @@ test('Transport was sent each listener only once', () => { }); test('Entity Info was pulled from the app', ()=> { - expect(callbackWiredUp).toBe(true) expect(entityInfoPulled).toBe(true) expect(entityInfoReceived).toBe(true) }); diff --git a/src/sdks/core/test/suite/lazy-transport.test.ts b/src/sdks/core/test/suite/lazy-transport.test.ts index f9e75cd9b..ebfb739fe 100644 --- a/src/sdks/core/test/suite/lazy-transport.test.ts +++ b/src/sdks/core/test/suite/lazy-transport.test.ts @@ -20,43 +20,13 @@ // setup for Firebolt SDK/TL handshake const win = globalThis || window -let _queue = [] -let _callback:Function -let target - -const queue = { - - send: function(json) { - if (target) { - target.send(json) - } - else { - _queue.push(json) - } - }, - - receive: function (callback) { - _callback = callback - }, - - flush: function (transport:any) { - target = transport - transport.receive(_callback) - _queue.forEach(item => transport.send(item)) - _queue = null - } -} - -// set up a queue to hold all Firebolt messages -win.__firebolt.transport = queue - import Setup from '../../../../../test/Setup' import { beforeAll, test, expect } from '@jest/globals'; -import { Lifecycle, Discovery } from "../../build/javascript/src/firebolt"; +import { Lifecycle, Discovery } from "../../build/javascript/src/firebolt"; // holds test transport layer state, e.g. callback type stateType = { - callback: (arg0: any) => void | null + callback: (arg0: string) => void | null } const state:stateType = { @@ -69,8 +39,10 @@ let callbackWiredUp = false let sendCalled = false const transport = { - send: function(json) { + send: function(message) { sendCalled = true + const json = JSON.parse(message) + console.log('transport.send: ' + json.method) if (json.method.toLowerCase() === 'lifecycle.ready') { // we'll assert on this later... navigateToListenCount++ @@ -80,12 +52,12 @@ const transport = { let response = { jsonrpc: '2.0', id: json.id, - result: null + result: true } // catching errors, so all tests don't fail if this breaks try { // send back the onInactive event immediately, to test for race conditions - state.callback(response) + state.callback(JSON.stringify(response)) } catch (err) { // fail silenetly (the boolean-based tests below will figure it out...) @@ -97,6 +69,7 @@ const transport = { } }, receive: function(callback) { + console.log('transport.receive') // store the callback state.callback = callback } @@ -119,7 +92,7 @@ beforeAll(()=> { Lifecycle.ready() - queue.flush(transport) + win.__firebolt.setTransportLayer(transport) return p }) diff --git a/src/sdks/core/test/suite/lifecycle.test.ts b/src/sdks/core/test/suite/lifecycle.test.ts index e5d299e34..00d777575 100644 --- a/src/sdks/core/test/suite/lifecycle.test.ts +++ b/src/sdks/core/test/suite/lifecycle.test.ts @@ -26,9 +26,9 @@ let readyMetricCalled: boolean = false; let readyMetricCalledAfterResolve: boolean = false; testHarness.onSend = function (module: string, method: string) { - if (module === "Lifecycle" && method === "ready") { + if (module === "lifecycle" && method === "ready") { readyCalled = true; - } else if (module === "Metrics" && method === "ready") { + } else if (module === "metrics" && method === "ready") { readyMetricCalled = true; if (readyResolved) { diff --git a/src/sdks/core/test/suite/listeners-transport.test.ts b/src/sdks/core/test/suite/listeners-transport.test.ts index d916f624c..bec4db1b7 100644 --- a/src/sdks/core/test/suite/listeners-transport.test.ts +++ b/src/sdks/core/test/suite/listeners-transport.test.ts @@ -19,6 +19,7 @@ const win = globalThis || window; import { test, expect } from "@jest/globals"; +import { Lifecycle, Discovery } from "../../build/javascript/src/firebolt"; // holds test transport layer state, e.g. callback const state = { @@ -30,8 +31,9 @@ let callbackWiredUp: boolean = false; let sendCalled: boolean = false; const transport = { - send: function (json: any) { + send: function (message: string) { sendCalled = true; + const json = JSON.parse(message); if (json.method.toLowerCase() === "discovery.onnavigateto") { // we'll assert on this later... navigateToListenCount++; @@ -59,21 +61,14 @@ const transport = { }, }; -win.__firebolt = win.__firebolt || {} -const transportAlreadyExisted = !!win.__firebolt.transport - -win.__firebolt = { - transport -} - -import { Lifecycle, Discovery, Intents } from "../../build/javascript/src/firebolt"; +win.__firebolt.setTransportLayer(transport); // listen twice, using event-specific call FIRST -Discovery.listen("navigateTo", (value: Intents.NavigationIntent) => { +Discovery.listen("navigateTo", (value: Discovery.NavigationIntent) => { callbackWiredUp = true; }); -Discovery.listen("navigateTo", (value: Intents.NavigationIntent) => { +Discovery.listen("navigateTo", (value: Discovery.NavigationIntent) => { /* this just adds more listen calls to make sure we don't spam */ }); Discovery.listen((event: string, value: object) => { @@ -87,7 +82,6 @@ Lifecycle.ready(); test("Transport injected after SDK", () => { expect(callbackWiredUp).toBe(true); - expect(transportAlreadyExisted).toBe(false) }); test("Transport send method working", () => { diff --git a/src/sdks/core/test/suite/synchronous-transport.test.ts b/src/sdks/core/test/suite/synchronous-transport.test.ts index 2694f8832..f7ab896d4 100644 --- a/src/sdks/core/test/suite/synchronous-transport.test.ts +++ b/src/sdks/core/test/suite/synchronous-transport.test.ts @@ -16,39 +16,36 @@ * SPDX-License-Identifier: Apache-2.0 */ -const win: any = window; +import transport from "../../../../../test/helpers/synchronous-transport.mjs"; -let sendCalled = false -let inactiveCalled = false -let _callback: Function; -let firstId: Number +// These all get set synchronously, so we'll update them as they happen +let sendCalled: boolean = false; +let inactiveListened: boolean = false; +let callbackWiredUp: boolean = false; -win.__firebolt = { - transport: { - send: function(json: any) { - if (firstId === undefined) { - firstId = json.id - } - sendCalled = true - if (json.method === 'Lifecycle.onInactive') { - inactiveCalled = true - } - else if (json.method === 'Device.name') { - console.dir(json) - _callback && setTimeout(() => { - _callback({ - jsonrpc: '2.0', - id: json.id, - result: 'Test Name' - }) - }, 100) - } - }, - receive: function(callback: Function) { - _callback = callback +transport.onSend((json) => { + // we'll assert on this later... + sendCalled = true; + if (json.method.toLowerCase() === "device.name") { + // we'll assert on this later... + inactiveListened = true; + + // we'll assert on this later... + callbackWiredUp = true; + let response = { + jsonrpc: "2.0", + id: json.id, + result: "Test Name", + }; + // catching errors, so all tests don't fail if this breaks + try { + // send back the onInactive event immediately, to test for race conditions + transport.response(response); + } catch (err) { + // fail silenetly (the boolean-based tests below will figure it out...) } } -} +}); import { test, expect, beforeAll } from "@jest/globals"; import { Lifecycle, Device } from "../../build/javascript/src/firebolt"; @@ -63,11 +60,7 @@ beforeAll(() => { }); test("Transport injected before SDK", () => { - // NOTE: this assumes an implementation detail that we start at 1 (we do at time of this test writing) - // this isn't the best test, since there's no requirement that we start at 1 or even use numbers - // at all (could be strings or even null). - // if this test ever fails, we should find a better way to test that we didn't miss any requests - expect(firstId).toBe(1) + expect(transport.instantiatedBeforeSdk()).toBe(true); }); test("Transport send method working", () => { @@ -75,7 +68,11 @@ test("Transport send method working", () => { }); test("Transport was sent `Lifecycle.onInactive` listener", () => { - expect(inactiveCalled).toBe(true); + expect( + !!transport + .history() + .find((json) => json.method.toLowerCase() === "lifecycle.oninactive") + ).toBe(true); }); test("Transport `receive` callback wired up", () => { diff --git a/test/helpers/synchronous-transport.mjs b/test/helpers/synchronous-transport.mjs index 7f411aadd..9cef2b153 100644 --- a/test/helpers/synchronous-transport.mjs +++ b/test/helpers/synchronous-transport.mjs @@ -7,7 +7,7 @@ let receiveCallback let first = false let _history = [] -window.__firebolt.transport = { +const transport = { send: function(message) { const json = JSON.parse(message) _history.push(json) @@ -31,4 +31,13 @@ window.__firebolt.transport = { } } +if (!window.__firebolt.setTransportLayer) { + first = true +} + +window.__firebolt.getTransportLayer = function() { + // we'll assert on this later... + return transport +} + export default transport \ No newline at end of file