From 209a613c1f5ed4d4c82113a138e73fbdfcca5b8d Mon Sep 17 00:00:00 2001 From: Nerivec <62446222+Nerivec@users.noreply.github.com> Date: Sun, 17 Nov 2024 21:21:40 +0100 Subject: [PATCH] fix: minFileVersion/maxFileVersion incorrect matching --- src/common.ts | 4 +- tests/ghw_check_ota_pr.test.ts | 72 ++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/common.ts b/src/common.ts index b1983a2d..81f80760 100644 --- a/src/common.ts +++ b/src/common.ts @@ -151,8 +151,8 @@ export function findMatchImage( (i) => i.imageType === image.imageType && i.manufacturerCode === image.manufacturerCode && - (!i.minFileVersion || image.fileVersion >= i.minFileVersion) && - (!i.maxFileVersion || image.fileVersion <= i.maxFileVersion) && + extraMetas.minFileVersion === i.minFileVersion && + extraMetas.maxFileVersion === i.maxFileVersion && i.modelId === extraMetas.modelId && (!(i.manufacturerName && extraMetas.manufacturerName) || primitivesArrayEquals(i.manufacturerName, extraMetas.manufacturerName)), ); diff --git a/tests/ghw_check_ota_pr.test.ts b/tests/ghw_check_ota_pr.test.ts index e72746ac..bc0d2908 100644 --- a/tests/ghw_check_ota_pr.test.ts +++ b/tests/ghw_check_ota_pr.test.ts @@ -21,6 +21,7 @@ import { IMAGE_V14_1_METAS, IMAGE_V14_2, IMAGE_V14_2_METAS, + IMAGES_TEST_DIR, PREV_IMAGES_TEST_DIR_PATH, useImage, withExtraMetas, @@ -442,6 +443,77 @@ Text after end tag`); ]); }); + it('success with newer than current but minFileVersion keeps both', async () => { + filePaths = [useImage(IMAGE_V13_1), useImage(IMAGE_V14_1)]; + const newContext = withBody( + `Text before start tag \`\`\`json [{"fileName":"ZLinky_router_v14.ota", "minFileVersion": 16783874}] \`\`\` Text after end tag`, + ); + + // @ts-expect-error mock + await checkOtaPR(github, core, newContext); + + expect(readManifestSpy).toHaveBeenCalledTimes(2); + expect(addImageToBaseSpy).toHaveBeenCalledTimes(2); + expect(addImageToPrevSpy).toHaveBeenCalledTimes(0); + expect(writeManifestSpy).toHaveBeenCalledTimes(2); + expect(writeManifestSpy).toHaveBeenCalledWith(common.BASE_INDEX_MANIFEST_FILENAME, [ + withExtraMetas(IMAGE_V13_1_METAS, { + // @ts-expect-error override + url: `https://github.com/Koenkk/zigbee-OTA/raw/master/${common.BASE_IMAGES_DIR}/${IMAGES_TEST_DIR}/${IMAGE_V13_1}`, + }), + withExtraMetas(IMAGE_V14_1_METAS, {minFileVersion: 16783874}), + ]); + expect(writeManifestSpy).toHaveBeenCalledWith(common.PREV_INDEX_MANIFEST_FILENAME, []); + }); + + it('success with newer than current but maxFileVersion keeps both', async () => { + filePaths = [useImage(IMAGE_V13_1), useImage(IMAGE_V14_1)]; + const newContext = withBody( + `Text before start tag \`\`\`json [{"fileName":"ZLinky_router_v13.ota", "maxFileVersion": 16783873}] \`\`\` Text after end tag`, + ); + + // @ts-expect-error mock + await checkOtaPR(github, core, newContext); + + expect(readManifestSpy).toHaveBeenCalledTimes(2); + expect(addImageToBaseSpy).toHaveBeenCalledTimes(2); + expect(addImageToPrevSpy).toHaveBeenCalledTimes(0); + expect(writeManifestSpy).toHaveBeenCalledTimes(2); + expect(writeManifestSpy).toHaveBeenCalledWith(common.BASE_INDEX_MANIFEST_FILENAME, [ + withExtraMetas(IMAGE_V13_1_METAS, { + // @ts-expect-error override + url: `https://github.com/Koenkk/zigbee-OTA/raw/master/${common.BASE_IMAGES_DIR}/${IMAGES_TEST_DIR}/${IMAGE_V13_1}`, + maxFileVersion: 16783873, + }), + IMAGE_V14_1_METAS, + ]); + expect(writeManifestSpy).toHaveBeenCalledWith(common.PREV_INDEX_MANIFEST_FILENAME, []); + }); + + it('success with newer than current but maxFileVersion/minFileVersion keeps both', async () => { + filePaths = [useImage(IMAGE_V13_1), useImage(IMAGE_V14_1)]; + const newContext = withBody( + `Text before start tag \`\`\`json [{"fileName":"ZLinky_router_v13.ota", "maxFileVersion": 16783873},{"fileName":"ZLinky_router_v14.ota", "minFileVersion": 16783874}] \`\`\` Text after end tag`, + ); + + // @ts-expect-error mock + await checkOtaPR(github, core, newContext); + + expect(readManifestSpy).toHaveBeenCalledTimes(2); + expect(addImageToBaseSpy).toHaveBeenCalledTimes(2); + expect(addImageToPrevSpy).toHaveBeenCalledTimes(0); + expect(writeManifestSpy).toHaveBeenCalledTimes(2); + expect(writeManifestSpy).toHaveBeenCalledWith(common.BASE_INDEX_MANIFEST_FILENAME, [ + withExtraMetas(IMAGE_V13_1_METAS, { + // @ts-expect-error override + url: `https://github.com/Koenkk/zigbee-OTA/raw/master/${common.BASE_IMAGES_DIR}/${IMAGES_TEST_DIR}/${IMAGE_V13_1}`, + maxFileVersion: 16783873, + }), + withExtraMetas(IMAGE_V14_1_METAS, {minFileVersion: 16783874}), + ]); + expect(writeManifestSpy).toHaveBeenCalledWith(common.PREV_INDEX_MANIFEST_FILENAME, []); + }); + it('failure with invalid extra metas', async () => { filePaths = [useImage(IMAGE_V14_1)]; const newContext = withBody(`Text before start tag \`\`\`json {"manufacturerName": "myManuf"} \`\`\` Text after end tag`);