diff --git a/src-electron/ide-integration/studio-rest-api.js b/src-electron/ide-integration/studio-rest-api.js index 4c997b945a..008ae79e34 100644 --- a/src-electron/ide-integration/studio-rest-api.js +++ b/src-electron/ide-integration/studio-rest-api.js @@ -28,10 +28,21 @@ const queryNotification = require('../db/query-session-notification.js') const wsServer = require('../server/ws-server.js') const dbEnum = require('../../src-shared/db-enum.js') const { StatusCodes } = require('http-status-codes') -const zcl = require('./zcl.js') +const zclComponents = require('./zcl-components.js') import WebSocket from 'ws' import { projectName } from '../util/studio-util' +const StudioRestAPI = { + GetProjectInfo: '/rest/clic/components/all/project/', + AddComponent: '/rest/clic/component/add/project/', + RemoveComponent: '/rest/clic/component/remove/project/', + DependsComponent: '/rest/clic/component/depends/project/', +} + +const StudioWsAPI = { + WsServerNotification: '/ws/clic/server/notifications/project/', +} + const localhost = 'http://127.0.0.1:' const wsLocalhost = 'ws://127.0.0.1:' @@ -198,7 +209,7 @@ async function updateComponentByClusterIdAndComponentId( // retrieve components to enable let promises = [] if (clusterId) { - let ids = zcl + let ids = zclComponents .getComponentIdsByCluster(db, sessionId, clusterId, side) .then((response) => Promise.resolve(response.componentIds)) promises.push(ids) @@ -516,3 +527,4 @@ exports.initIdeIntegration = initIdeIntegration exports.deinitIdeIntegration = deinitIdeIntegration exports.sendSessionCreationErrorStatus = sendSessionCreationErrorStatus exports.sendComponentUpdateStatus = sendComponentUpdateStatus +exports.isComponentTogglingDisabled = isComponentTogglingDisabled diff --git a/src-electron/ide-integration/zcl.js b/src-electron/ide-integration/zcl-components.js similarity index 67% rename from src-electron/ide-integration/zcl.js rename to src-electron/ide-integration/zcl-components.js index 9dc66c7b15..8110021bd1 100644 --- a/src-electron/ide-integration/zcl.js +++ b/src-electron/ide-integration/zcl-components.js @@ -21,10 +21,11 @@ * @module REST API: user data */ -const queryPackage = require('../db/query-package.js') -const queryZcl = require('../db/query-zcl.js') -const dbEnum = require('../../src-shared/db-enum.js') -const util = require('../util/util.js') +const queryPackage = require('../db/query-package') +const queryZcl = require('../db/query-zcl') +const dbEnum = require('../../src-shared/db-enum') +const util = require('../util/util') +const env = require('../util/env') /** * Promise that return a list of component Ids required by a specific cluster @@ -34,27 +35,29 @@ const util = require('../util/util.js') * @param {*} side * @returns {*} - {componentIds, clusterId, clusterLabel, side} */ -function getComponentIdsByCluster(db, sessionId, clusterId, side) { +async function getComponentIdsByCluster(db, sessionId, clusterId, side) { // enable components - let extensions = undefined - return queryPackage - .getSessionPackagesByType( - db, - sessionId, - dbEnum.packageType.genTemplatesJson - ) - .then((pkgs) => (pkgs.length == 0 ? null : pkgs[0].id)) - .then((id) => { - return queryPackage.selectPackageExtension( - db, - id, - dbEnum.packageExtensionEntity.cluster - ) - }) - .then((ext) => { - extensions = ext - return queryZcl.selectClusterById(db, clusterId) - }) + let pkgs = await queryPackage.getSessionPackagesByType( + db, + sessionId, + dbEnum.packageType.genTemplatesJson + ) + let id = pkgs.length == 0 ? null : pkgs[0].id + if (id == null) { + return { + componentIds: [], + clusterId, + side, + } + } + + let extensions = await queryPackage.selectPackageExtension( + db, + id, + dbEnum.packageExtensionEntity.cluster + ) + return queryZcl + .selectClusterById(db, clusterId) .then((cluster) => { let componentIds = [] if (cluster) { @@ -71,27 +74,29 @@ function getComponentIdsByCluster(db, sessionId, clusterId, side) { } }) } else { - console.log(`Failed to retrieve cluster via clusterId(${clusterId}).`) + env.logWarning( + `Failed to retrieve cluster via clusterId(${clusterId}).` + ) } - return Promise.resolve({ + return { componentIds, clusterId, clusterLabel: cluster.label.toLowerCase(), side, - }) + } }) .catch((err) => { - console.log( + env.logWarning( `Failed to retrieve component ids required by clusterId(${clusterId}) from cluster extension mapping.`, err ) - return Promise.resolve({ + return { componentIds: [], clusterId, side, - }) + } }) } diff --git a/test/ide-integration.test.js b/test/ide-integration.test.js new file mode 100644 index 0000000000..ddb59e697b --- /dev/null +++ b/test/ide-integration.test.js @@ -0,0 +1,104 @@ +/** + * + * Copyright (c) 2023 Silicon Labs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + * @jest-environment node + */ +const genEngine = require('../src-electron/generator/generation-engine') +const env = require('../src-electron/util/env') +const dbApi = require('../src-electron/db/db-api') +const queryPackage = require('../src-electron/db/query-package') +const zclLoader = require('../src-electron/zcl/zcl-loader') +const importJs = require('../src-electron/importexport/import') +const testUtil = require('./test-util') +const studioRestApi = require('../src-electron/ide-integration/studio-rest-api') +const zclComponents = require('../src-electron/ide-integration/zcl-components') + +let db +const templateCount = testUtil.testTemplate.zigbeeCount +const testFile = testUtil.zigbeeTestFile.threeEp + +beforeAll(async () => { + env.setDevelopmentEnv() + let file = env.sqliteTestFile('ide-integration') + db = await dbApi.initDatabaseAndLoadSchema( + file, + env.schemaFile(), + env.zapVersion() + ) + return zclLoader.loadZcl(db, env.builtinSilabsZclMetafile()) +}, testUtil.timeout.medium()) + +afterAll(() => dbApi.closeDatabase(db), testUtil.timeout.short()) + +let templateContext + +test( + 'Load templates', + async () => { + let context = await genEngine.loadTemplates( + db, + testUtil.testTemplate.zigbee + ) + expect(context.crc).not.toBeNull() + expect(context.templateData).not.toBeNull() + expect(context.templateData.name).toEqual('Test templates') + expect(context.templateData.version).toEqual('test-v1') + expect(context.templateData.templates.length).toEqual(templateCount) + expect(context.packageId).not.toBeNull() + templateContext = context + }, + testUtil.timeout.medium() +) + +test( + 'Validate package loading', + async () => { + templateContext.packages = await queryPackage.getPackageByParent( + templateContext.db, + templateContext.packageId + ) + expect(templateContext.packages.length).toBe(templateCount - 1 + 2) // -1 for ignored one, one for helper and one for overridable + }, + testUtil.timeout.short() +) + +test( + `Ide integration`, + async () => { + let { sessionId, errors, warnings } = await importJs.importDataFromFile( + db, + testFile + ) + expect(errors.length).toBe(0) + expect(warnings.length).toBe(0) + expect(sessionId).not.toBeNull() + + let x = await studioRestApi.integrationEnabled(db, sessionId) + let y = await studioRestApi.isComponentTogglingDisabled(db, sessionId) + expect(x).toBeFalsy() + expect(y).toBeFalsy() + + let res = await zclComponents.getComponentIdsByCluster( + db, + sessionId, + 1, + 'client' + ) + expect(res.componentIds.length).toBe(0) + }, + testUtil.timeout.long() +)