Skip to content

Commit

Permalink
only show desc if relevant
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanzhouyc committed Oct 31, 2024
1 parent dd4a32c commit d959924
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
12 changes: 12 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3650,6 +3650,7 @@ This module provides queries for features.
* [~evaluateWithParentheses(expr)](#module_DB API_ feature related queries..evaluateConformanceExpression..evaluateWithParentheses)
* [~checkMissingTerms(expression, elementMap)](#module_DB API_ feature related queries..checkMissingTerms) ⇒
* [~filterElementsContainingDesc(elements)](#module_DB API_ feature related queries..filterElementsContainingDesc) ⇒
* [~filterRelatedDescElements(elements, featureCode)](#module_DB API_ feature related queries..filterRelatedDescElements) ⇒
* [~generateWarningMessage(featureData, endpointId, missingTerms, featureMap, descElements)](#module_DB API_ feature related queries..generateWarningMessage) ⇒
* [~checkElementsToUpdate(elements, featureMap, featureData, endpointId)](#module_DB API_ feature related queries..checkElementsToUpdate) ⇒
* [~filterElementsToUpdate(elements, elementMap, featureCode)](#module_DB API_ feature related queries..filterElementsToUpdate) ⇒
Expand Down Expand Up @@ -3743,6 +3744,17 @@ Filter an array of elements by if any element has conformance containing the ter
| --- | --- |
| elements | <code>\*</code> |

<a name="module_DB API_ feature related queries..filterRelatedDescElements"></a>

### DB API: feature related queries~filterRelatedDescElements(elements, featureCode) ⇒
**Kind**: inner method of [<code>DB API: feature related queries</code>](#module_DB API_ feature related queries)
**Returns**: elements with conformance containing 'desc' and the feature code

| Param | Type |
| --- | --- |
| elements | <code>\*</code> |
| featureCode | <code>\*</code> |

<a name="module_DB API_ feature related queries..generateWarningMessage"></a>

### DB API: feature related queries~generateWarningMessage(featureData, endpointId, missingTerms, featureMap, descElements) ⇒
Expand Down
22 changes: 18 additions & 4 deletions src-electron/db/query-feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*/
const dbApi = require('./db-api.js')
const dbMapping = require('./db-mapping.js')
const querySessionNotification = require('./query-session-notification.js')

/**
* Get all device type features associated with a list of device type refs and an endpoint.
Expand Down Expand Up @@ -239,6 +238,20 @@ function filterElementsContainingDesc(elements) {
})
}

/**
*
* @export
* @param {*} elements
* @param {*} featureCode
* @returns elements with conformance containing 'desc' and the feature code
*/
function filterRelatedDescElements(elements, featureCode) {
return elements.filter((element) => {
let terms = element.conformance.match(/[A-Za-z][A-Za-z0-9]*/g)
return terms && terms.includes('desc') && terms.includes(featureCode)
})
}

/**
* Generate a warning message after processing conformance of the updated device type feature.
* Set flags to decide whether to show a popup warning or disable changes in the frontend.
Expand Down Expand Up @@ -386,9 +399,9 @@ function checkElementsToUpdate(elements, featureMap, featureData, endpointId) {
elementMap['Zigbee'] = 0

let descElements = {}
descElements.attributes = filterElementsContainingDesc(attributes)
descElements.commands = filterElementsContainingDesc(commands)
descElements.events = filterElementsContainingDesc(events)
descElements.attributes = filterRelatedDescElements(attributes, featureCode)
descElements.commands = filterRelatedDescElements(commands, featureCode)
descElements.events = filterRelatedDescElements(events, featureCode)

let missingTerms = checkMissingTerms(featureData.conformance, elementMap)
let warningInfo = generateWarningMessage(
Expand Down Expand Up @@ -467,3 +480,4 @@ exports.getFeaturesByDeviceTypeRefs = getFeaturesByDeviceTypeRefs
exports.checkElementsToUpdate = checkElementsToUpdate
exports.evaluateConformanceExpression = evaluateConformanceExpression
exports.filterElementsContainingDesc = filterElementsContainingDesc
exports.filterRelatedDescElements = filterRelatedDescElements
20 changes: 15 additions & 5 deletions test/feature.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,18 @@ test(
{ name: 'Element7', conformance: 'description' }
]

const result = queryFeature.filterElementsContainingDesc(elements)
let result = queryFeature.filterElementsContainingDesc(elements)
expect(result).toEqual([
{ name: 'Element1', conformance: 'desc' },
{ name: 'Element2', conformance: 'P, desc' },
{ name: 'Element3', conformance: '[desc & XY]' },
{ name: 'Element4', conformance: 'desc, [HS]' },
{ name: 'Element5', conformance: 'desc | optional' }
])

const featureCode = 'HS'
result = queryFeature.filterRelatedDescElements(elements, featureCode)
expect(result).toEqual([{ name: 'Element4', conformance: 'desc, [HS]' }])
},
testUtil.timeout.short()
)
Expand Down Expand Up @@ -389,9 +393,13 @@ test(
expect(result.eventsToUpdate.length).toBe(0)
featureMap['UNKNOWN'] = 0

// 4. test enable a feature with desc elements
// expect same warning flags as 3
let descElement = { name: 'DescElement', conformance: 'desc', included: 0 }
// 4. test enable a feature with desc elements with conformance
// containing desc and updated feature code, expect same warning flags as 3
let descElement = {
name: 'DescElement',
conformance: 'HS & desc',
included: 0
}
elements.attributes.push(descElement)
featureMap['HS'] = 1
result = queryFeature.checkElementsToUpdate(
Expand All @@ -412,8 +420,10 @@ test(
expect(result.eventsToUpdate.length).toBe(0)
featureMap['HS'] = 0

// 5. test enable a feature with unknown conformance and desc elements
// 5. test enable a feature with unknown conformance and relevant desc elements
// should have 2 warnings
elements.attributes.find((attr) => attr.name == 'DescElement').conformance =
'desc, [UNKNOWN]'
featureMap['UNKNOWN'] = 1
result = queryFeature.checkElementsToUpdate(
elements,
Expand Down

0 comments on commit d959924

Please sign in to comment.