Skip to content

Commit

Permalink
Handling package id management when custom xml is changed and re-uplo…
Browse files Browse the repository at this point in the history
…aded (project-chip#1456)

* creating new package id when crc mismatch instead of updating crc for existing package

* added relevant tests

* added some comments
  • Loading branch information
dhchandw authored Oct 16, 2024
1 parent 82dcb19 commit c958629
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 44 deletions.
80 changes: 49 additions & 31 deletions src-electron/zcl/zcl-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,23 @@ async function qualifyZclFile(
let filePath = info.filePath
let data = info.data
let actualCrc = info.crc
let pkg

let pkg = await queryPackage.getPackageByPathAndParent(
db,
filePath,
parentPackageId,
isCustom
)
if (isCustom) {
// if multiple instances of the same custom xml file are loaded, this will get the one in sync (latest)
pkg = await queryPackage.getPackageByPathAndType(
db,
filePath,
dbEnum.packageType.zclXmlStandalone
)
} else {
pkg = await queryPackage.getPackageByPathAndParent(
db,
filePath,
parentPackageId,
isCustom
)
}

if (pkg == null) {
// This is executed if there is no CRC in the database.
Expand All @@ -321,37 +331,45 @@ async function qualifyZclFile(
data: data,
packageId: parentPackageId == null ? packageId : parentPackageId
}
} else if (pkg.crc != actualCrc) {
// This is executed if CRC is found in the database but doesn't match the actual CRC.
env.logDebug(
`CRC mismatch for file ${pkg.path}, (${pkg.crc} vs ${actualCrc}) package id ${pkg.id}, parsing.`
)
await queryPackage.updatePackageIsInSync(db, pkg.id, false) // Mark the older package as out of sync

// creating a new package id, crc combination so older sessions using the package from the same path aren't affected
let packageId = await queryPackage.insertPathCrc(
db,
filePath,
actualCrc,
packageType,
parentPackageId
)
return {
filePath: filePath,
data: data,
packageId: parentPackageId == null ? packageId : parentPackageId
}
} else {
// This is executed if CRC is found in the database.
if (pkg.crc == actualCrc) {
// Sending data back when it is a custom xml
if (parentPackageId == null) {
return {
filePath: filePath,
data: data,
packageId: pkg.id,
customXmlReload: true,
crc: actualCrc
}
}
env.logDebug(
`CRC match for file ${pkg.path} (${pkg.crc}), skipping parsing.`
)
return {
error: `${pkg.path} skipped`,
packageId: pkg.id
}
} else {
env.logDebug(
`CRC missmatch for file ${pkg.path}, (${pkg.crc} vs ${actualCrc}) package id ${pkg.id}, parsing.`
)
await queryPackage.updatePathCrc(db, filePath, actualCrc, parentPackageId)
// This is executed if CRC is found in the database and matches the actual CRC.
// Sending data back when it is a custom xml
if (parentPackageId == null) {
return {
filePath: filePath,
data: data,
packageId: parentPackageId == null ? pkg.id : parentPackageId // Changing from package to pkg.id since package is not defined
packageId: pkg.id,
customXmlReload: true,
crc: actualCrc
}
}
env.logDebug(
`CRC match for file ${pkg.path} (${pkg.crc}), skipping parsing.`
)
return {
error: `${pkg.path} skipped`,
packageId: pkg.id
}
}
}

Expand Down
26 changes: 13 additions & 13 deletions test/custom-matter-xml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,31 +397,31 @@ test(
"// cluster: 0xFFF1FC20 Sample Custom Changed, text extension: ''"
)

expect(sdkExt).not.toContain(
"// cluster: 0xFFF1FC20 Sample Custom Cluster, text extension: ''"
)

// Not working as expected possibly due to bug - https://github.com/project-chip/zap/issues/1387
// expect(endpointConfig).not.toContain(
// " /* Endpoint: 1, Cluster: Sample Custom Cluster (server) */ \\"
// )

// Not working as expected possibly due to bug - https://github.com/project-chip/zap/issues/1391
// expect(sdkExt).not.toContain(
// "// cluster: 0xFFF1FC20 Sample Custom Cluster, text extension: ''"
// )

// Once bugs are resolved and based on expected behavior, the tests should pass
// Might need to toggle the cluster for the endpoint before generating
// expect(endpointConfig).toContain(
// ' /* Endpoint: 1, Cluster: Sample Custom Changed (server) */ \\'
// )

// Should also pass after bugs are resolved
// Verify that there are two instances of this package with only one in sync
// let customXmlPackages = await dbApi.dbAll(
// db,
// 'SELECT * FROM PACKAGE WHERE PATH = ?',
// [testUtil.testMattterCustomXml]
// )
// expect(customXmlPackages.length).toEqual(2)
// expect(customXmlPackages[0].IS_IN_SYNC ^ customXmlPackages[1].IS_IN_SYNC).toBeTruthy()
let customXmlPackages = await dbApi.dbAll(
db,
'SELECT * FROM PACKAGE WHERE PATH = ?',
[testUtil.testMattterCustomXml]
)
expect(customXmlPackages.length).toEqual(2)
expect(
customXmlPackages[0].IS_IN_SYNC ^ customXmlPackages[1].IS_IN_SYNC
).toBeTruthy()
} finally {
// restore original custom xml
fs.writeFileSync(testUtil.testMattterCustomXml, originalData, 'utf8')
Expand Down

0 comments on commit c958629

Please sign in to comment.