fix: Switch to biome & vitest (#726)

This commit is contained in:
Nerivec
2025-03-23 21:30:14 +01:00
committed by GitHub
parent 44c5bbcc1c
commit 94b76a910a
48 changed files with 1762 additions and 4075 deletions
+85 -81
View File
@@ -1,13 +1,13 @@
import type {RepoImageMeta} from '../src/types';
import type {RepoImageMeta} from "../src/types";
import {existsSync, mkdirSync, readFileSync, rmSync} from 'fs';
import {existsSync, mkdirSync, readFileSync, rmSync} from "node:fs";
import * as common from '../src/common';
import {processFirmwareImage, ProcessFirmwareImageStatus} from '../src/process_firmware_image';
import {type MockInstance, afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi} from "vitest";
import * as common from "../src/common";
import {ProcessFirmwareImageStatus, processFirmwareImage} from "../src/process_firmware_image";
import {
BASE_IMAGES_TEST_DIR_PATH,
getAdjustedContent,
getImageOriginalDirPath,
IMAGES_TEST_DIR,
IMAGE_INVALID,
IMAGE_TAR,
IMAGE_TAR_METAS,
@@ -17,33 +17,36 @@ import {
IMAGE_V13_1_METAS,
IMAGE_V14_1,
IMAGE_V14_1_METAS,
IMAGES_TEST_DIR,
PREV_IMAGES_TEST_DIR_PATH,
getAdjustedContent,
getImageOriginalDirPath,
useImage,
withExtraMetas,
} from './data.test';
} from "./data.test";
describe('Process Firmware Image', () => {
describe("Process Firmware Image", () => {
let baseManifest: RepoImageMeta[];
let prevManifest: RepoImageMeta[];
let consoleErrorSpy: jest.SpyInstance;
let consoleLogSpy: jest.SpyInstance;
let readManifestSpy: jest.SpyInstance;
let writeManifestSpy: jest.SpyInstance;
let addImageToBaseSpy: jest.SpyInstance;
let addImageToPrevSpy: jest.SpyInstance;
let fetchSpy: jest.SpyInstance;
let setTimeoutSpy: jest.SpyInstance;
let consoleErrorSpy: MockInstance;
let consoleLogSpy: MockInstance;
let readManifestSpy: MockInstance;
let writeManifestSpy: MockInstance;
let addImageToBaseSpy: MockInstance;
let addImageToPrevSpy: MockInstance;
let fetchSpy: MockInstance;
let setTimeoutSpy: MockInstance;
let fetchReturnedStatus: {ok: boolean; status: number; body?: object} = {ok: true, status: 200, body: {}};
const getManifest = (fileName: string): RepoImageMeta[] => {
if (fileName === common.BASE_INDEX_MANIFEST_FILENAME) {
return baseManifest;
} else if (fileName === common.PREV_INDEX_MANIFEST_FILENAME) {
return prevManifest;
} else {
throw new Error(`${fileName} not supported`);
}
if (fileName === common.PREV_INDEX_MANIFEST_FILENAME) {
return prevManifest;
}
throw new Error(`${fileName} not supported`);
};
const setManifest = (fileName: string, content: RepoImageMeta[]): void => {
@@ -71,7 +74,7 @@ describe('Process Firmware Image', () => {
return newMeta;
};
const expectNoChanges = (noReadManifest: boolean = false): void => {
const expectNoChanges = (noReadManifest = false): void => {
if (noReadManifest) {
expect(readManifestSpy).toHaveBeenCalledTimes(0);
} else {
@@ -84,7 +87,7 @@ describe('Process Firmware Image', () => {
expect(writeManifestSpy).toHaveBeenCalledTimes(0);
};
const expectWriteNoChanges = (inBase: boolean = true, inPrev: boolean = true): void => {
const expectWriteNoChanges = (inBase = true, inPrev = true): void => {
if (inBase) {
expect(writeManifestSpy).toHaveBeenNthCalledWith(
1,
@@ -115,19 +118,20 @@ describe('Process Firmware Image', () => {
setTimeoutSpy.mockRestore();
rmSync(BASE_IMAGES_TEST_DIR_PATH, {recursive: true, force: true});
rmSync(PREV_IMAGES_TEST_DIR_PATH, {recursive: true, force: true});
rmSync(IMAGES_TEST_DIR, {recursive: true, force: true});
});
beforeEach(() => {
resetManifests();
fetchReturnedStatus = {ok: true, status: 200, body: {}};
consoleErrorSpy = jest.spyOn(console, 'error');
consoleLogSpy = jest.spyOn(console, 'log');
readManifestSpy = jest.spyOn(common, 'readManifest').mockImplementation(getManifest);
writeManifestSpy = jest.spyOn(common, 'writeManifest').mockImplementation(setManifest);
addImageToBaseSpy = jest.spyOn(common, 'addImageToBase');
addImageToPrevSpy = jest.spyOn(common, 'addImageToPrev');
fetchSpy = jest.spyOn(global, 'fetch').mockImplementation(
consoleErrorSpy = vi.spyOn(console, "error");
consoleLogSpy = vi.spyOn(console, "log");
readManifestSpy = vi.spyOn(common, "readManifest").mockImplementation(getManifest);
writeManifestSpy = vi.spyOn(common, "writeManifest").mockImplementation(setManifest);
addImageToBaseSpy = vi.spyOn(common, "addImageToBase");
addImageToPrevSpy = vi.spyOn(common, "addImageToPrev");
fetchSpy = vi.spyOn(global, "fetch").mockImplementation(
// @ts-expect-error mocked as needed
(input) => {
return {
@@ -139,7 +143,7 @@ describe('Process Firmware Image', () => {
};
},
);
setTimeoutSpy = jest.spyOn(global, 'setTimeout').mockImplementation(
setTimeoutSpy = vi.spyOn(global, "setTimeout").mockImplementation(
// @ts-expect-error mock
(fn) => {
fn();
@@ -152,72 +156,72 @@ describe('Process Firmware Image', () => {
rmSync(PREV_IMAGES_TEST_DIR_PATH, {recursive: true, force: true});
});
it('failure with fetch ok', async () => {
it("failure with fetch ok", async () => {
fetchReturnedStatus.ok = false;
fetchReturnedStatus.status = 429;
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_V14_1, IMAGE_V14_1);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.REQUEST_FAILED);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.RequestFailed);
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringContaining(`Invalid response from ${IMAGE_V14_1} status=${fetchReturnedStatus.status}.`),
);
expectNoChanges(false);
});
it('failure with fetch body', async () => {
it("failure with fetch body", async () => {
fetchReturnedStatus.body = undefined;
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_V14_1, IMAGE_V14_1);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.REQUEST_FAILED);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.RequestFailed);
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringContaining(`Invalid response from ${IMAGE_V14_1} status=${fetchReturnedStatus.status}.`),
);
expectNoChanges(false);
});
it('failure with invalid OTA file', async () => {
it("failure with invalid OTA file", async () => {
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_INVALID, IMAGE_INVALID);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.ERROR);
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining(`Not a valid OTA fil`));
expect(status).toStrictEqual(ProcessFirmwareImageStatus.Error);
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining("Not a valid OTA fil"));
expectNoChanges(false);
});
it('failure with identical OTA file', async () => {
it("failure with identical OTA file", async () => {
setManifest(common.BASE_INDEX_MANIFEST_FILENAME, [IMAGE_V14_1_METAS]);
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_V14_1, IMAGE_V14_1);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.SUCCESS);
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining(`Base manifest already has version`));
expect(status).toStrictEqual(ProcessFirmwareImageStatus.Success);
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining("Base manifest already has version"));
expect(writeManifestSpy).toHaveBeenNthCalledWith(1, common.PREV_INDEX_MANIFEST_FILENAME, getManifest(common.PREV_INDEX_MANIFEST_FILENAME));
expect(writeManifestSpy).toHaveBeenNthCalledWith(2, common.BASE_INDEX_MANIFEST_FILENAME, getManifest(common.BASE_INDEX_MANIFEST_FILENAME));
expectWriteNoChanges();
});
it('failure with older OTA file that has identical in prev', async () => {
it("failure with older OTA file that has identical in prev", async () => {
setManifest(common.BASE_INDEX_MANIFEST_FILENAME, [IMAGE_V14_1_METAS]);
setManifest(common.PREV_INDEX_MANIFEST_FILENAME, [IMAGE_V13_1_METAS]);
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_V13_1, IMAGE_V13_1);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.SUCCESS);
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining(`an equal or better match is already present in prev manifest`));
expect(status).toStrictEqual(ProcessFirmwareImageStatus.Success);
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining("an equal or better match is already present in prev manifest"));
expectWriteNoChanges();
});
it('failure with older OTA file that has newer in prev', async () => {
it("failure with older OTA file that has newer in prev", async () => {
setManifest(common.BASE_INDEX_MANIFEST_FILENAME, [IMAGE_V14_1_METAS]);
setManifest(common.PREV_INDEX_MANIFEST_FILENAME, [IMAGE_V13_1_METAS]);
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_V12_1, IMAGE_V12_1);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.SUCCESS);
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining(`an equal or better match is already present in prev manifest`));
expect(status).toStrictEqual(ProcessFirmwareImageStatus.Success);
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining("an equal or better match is already present in prev manifest"));
expectWriteNoChanges();
});
it('success into base', async () => {
it("success into base", async () => {
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_V14_1, IMAGE_V14_1);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.SUCCESS);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.Success);
expect(readManifestSpy).toHaveBeenCalledWith(common.BASE_INDEX_MANIFEST_FILENAME);
expect(readManifestSpy).toHaveBeenCalledWith(common.PREV_INDEX_MANIFEST_FILENAME);
expect(addImageToBaseSpy).toHaveBeenCalledTimes(1);
@@ -226,12 +230,12 @@ describe('Process Firmware Image', () => {
expect(writeManifestSpy).toHaveBeenCalledWith(common.BASE_INDEX_MANIFEST_FILENAME, [withOriginalUrl(IMAGE_V14_1, IMAGE_V14_1_METAS)]);
});
it('success into prev', async () => {
it("success into prev", async () => {
setManifest(common.BASE_INDEX_MANIFEST_FILENAME, [withOriginalUrl(IMAGE_V14_1, IMAGE_V14_1_METAS)]);
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_V13_1, IMAGE_V13_1);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.SUCCESS);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.Success);
expect(readManifestSpy).toHaveBeenCalledWith(common.BASE_INDEX_MANIFEST_FILENAME);
expect(readManifestSpy).toHaveBeenCalledWith(common.PREV_INDEX_MANIFEST_FILENAME);
expect(addImageToBaseSpy).toHaveBeenCalledTimes(0);
@@ -241,13 +245,13 @@ describe('Process Firmware Image', () => {
expect(writeManifestSpy).toHaveBeenCalledWith(common.PREV_INDEX_MANIFEST_FILENAME, [withOriginalUrl(IMAGE_V13_1, IMAGE_V13_1_METAS)]);
});
it('success with newer than current without existing prev', async () => {
it("success with newer than current without existing prev", async () => {
setManifest(common.BASE_INDEX_MANIFEST_FILENAME, [withOriginalUrl(IMAGE_V13_1, IMAGE_V13_1_METAS)]);
useImage(IMAGE_V13_1, BASE_IMAGES_TEST_DIR_PATH);
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_V14_1, IMAGE_V14_1);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.SUCCESS);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.Success);
expect(readManifestSpy).toHaveBeenCalledTimes(2);
expect(addImageToBaseSpy).toHaveBeenCalledTimes(1);
expect(addImageToPrevSpy).toHaveBeenCalledTimes(0);
@@ -256,7 +260,7 @@ describe('Process Firmware Image', () => {
expect(writeManifestSpy).toHaveBeenCalledWith(common.PREV_INDEX_MANIFEST_FILENAME, [withOriginalUrl(IMAGE_V13_1, IMAGE_V13_1_METAS)]);
});
it('success with newer than current with existing prev', async () => {
it("success with newer than current with existing prev", async () => {
setManifest(common.BASE_INDEX_MANIFEST_FILENAME, [withOriginalUrl(IMAGE_V13_1, IMAGE_V13_1_METAS)]);
setManifest(common.PREV_INDEX_MANIFEST_FILENAME, [withOriginalUrl(IMAGE_V12_1, IMAGE_V12_1_METAS)]);
useImage(IMAGE_V13_1, BASE_IMAGES_TEST_DIR_PATH);
@@ -264,7 +268,7 @@ describe('Process Firmware Image', () => {
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_V14_1, IMAGE_V14_1);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.SUCCESS);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.Success);
expect(readManifestSpy).toHaveBeenCalledTimes(2);
expect(addImageToBaseSpy).toHaveBeenCalledTimes(1);
expect(addImageToPrevSpy).toHaveBeenCalledTimes(0);
@@ -273,7 +277,7 @@ describe('Process Firmware Image', () => {
expect(writeManifestSpy).toHaveBeenCalledWith(common.PREV_INDEX_MANIFEST_FILENAME, [withOriginalUrl(IMAGE_V13_1, IMAGE_V13_1_METAS)]);
});
it('success with older that is newer than prev', async () => {
it("success with older that is newer than prev", async () => {
setManifest(common.BASE_INDEX_MANIFEST_FILENAME, [withOriginalUrl(IMAGE_V14_1, IMAGE_V14_1_METAS)]);
setManifest(common.PREV_INDEX_MANIFEST_FILENAME, [withOriginalUrl(IMAGE_V12_1, IMAGE_V12_1_METAS)]);
useImage(IMAGE_V14_1, BASE_IMAGES_TEST_DIR_PATH);
@@ -281,7 +285,7 @@ describe('Process Firmware Image', () => {
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_V13_1, IMAGE_V13_1);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.SUCCESS);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.Success);
expect(readManifestSpy).toHaveBeenCalledTimes(2);
expect(addImageToBaseSpy).toHaveBeenCalledTimes(0);
expect(addImageToPrevSpy).toHaveBeenCalledTimes(1);
@@ -290,13 +294,13 @@ describe('Process Firmware Image', () => {
expect(writeManifestSpy).toHaveBeenCalledWith(common.PREV_INDEX_MANIFEST_FILENAME, [withOriginalUrl(IMAGE_V13_1, IMAGE_V13_1_METAS)]);
});
it('success with newer with missing file', async () => {
it("success with newer with missing file", async () => {
setManifest(common.BASE_INDEX_MANIFEST_FILENAME, [withOriginalUrl(IMAGE_V13_1, IMAGE_V13_1_METAS)]);
// useImage(IMAGE_V13_1, BASE_IMAGES_TEST_DIR_PATH);
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_V14_1, IMAGE_V14_1);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.SUCCESS);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.Success);
expect(readManifestSpy).toHaveBeenCalledTimes(2);
expect(addImageToBaseSpy).toHaveBeenCalledTimes(1);
expect(addImageToPrevSpy).toHaveBeenCalledTimes(0);
@@ -305,34 +309,34 @@ describe('Process Firmware Image', () => {
expect(writeManifestSpy).toHaveBeenCalledWith(common.PREV_INDEX_MANIFEST_FILENAME, []);
});
it('success with extra metas', async () => {
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_V14_1, IMAGE_V14_1, {manufacturerName: ['lixee']});
it("success with extra metas", async () => {
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_V14_1, IMAGE_V14_1, {manufacturerName: ["lixee"]});
expect(status).toStrictEqual(ProcessFirmwareImageStatus.SUCCESS);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.Success);
expect(readManifestSpy).toHaveBeenCalledWith(common.BASE_INDEX_MANIFEST_FILENAME);
expect(readManifestSpy).toHaveBeenCalledWith(common.PREV_INDEX_MANIFEST_FILENAME);
expect(addImageToBaseSpy).toHaveBeenCalledTimes(1);
expect(addImageToPrevSpy).toHaveBeenCalledTimes(0);
expect(writeManifestSpy).toHaveBeenCalledTimes(2);
expect(writeManifestSpy).toHaveBeenCalledWith(common.BASE_INDEX_MANIFEST_FILENAME, [
withOriginalUrl(IMAGE_V14_1, withExtraMetas(IMAGE_V14_1_METAS, {manufacturerName: ['lixee']})),
withOriginalUrl(IMAGE_V14_1, withExtraMetas(IMAGE_V14_1_METAS, {manufacturerName: ["lixee"]})),
]);
});
it('success with all extra metas', async () => {
it("success with all extra metas", async () => {
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_V14_1, IMAGE_V14_1, {
originalUrl: `https://example.com/${IMAGE_V14_1}`,
force: false,
hardwareVersionMax: 2,
hardwareVersionMin: 1,
manufacturerName: ['lixee'],
manufacturerName: ["lixee"],
maxFileVersion: 5,
minFileVersion: 3,
modelId: 'bogus',
releaseNotes: 'bugfixes',
modelId: "bogus",
releaseNotes: "bugfixes",
});
expect(status).toStrictEqual(ProcessFirmwareImageStatus.SUCCESS);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.Success);
expect(readManifestSpy).toHaveBeenCalledWith(common.BASE_INDEX_MANIFEST_FILENAME);
expect(readManifestSpy).toHaveBeenCalledWith(common.PREV_INDEX_MANIFEST_FILENAME);
expect(addImageToBaseSpy).toHaveBeenCalledTimes(1);
@@ -345,24 +349,24 @@ describe('Process Firmware Image', () => {
force: false,
hardwareVersionMax: 2,
hardwareVersionMin: 1,
manufacturerName: ['lixee'],
manufacturerName: ["lixee"],
maxFileVersion: 5,
minFileVersion: 3,
modelId: 'bogus',
releaseNotes: 'bugfixes',
modelId: "bogus",
releaseNotes: "bugfixes",
}),
),
]);
});
it('success with tar', async () => {
it("success with tar", async () => {
if (!existsSync(common.TMP_DIR)) {
mkdirSync(common.TMP_DIR, {recursive: true});
}
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_TAR, IMAGE_TAR, {}, true, (f) => f.endsWith('.ota'));
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_TAR, IMAGE_TAR, {}, true, (f) => f.endsWith(".ota"));
expect(status).toStrictEqual(ProcessFirmwareImageStatus.SUCCESS);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.Success);
expect(readManifestSpy).toHaveBeenCalledWith(common.BASE_INDEX_MANIFEST_FILENAME);
expect(readManifestSpy).toHaveBeenCalledWith(common.PREV_INDEX_MANIFEST_FILENAME);
expect(addImageToBaseSpy).toHaveBeenCalledTimes(1);
@@ -373,28 +377,28 @@ describe('Process Firmware Image', () => {
rmSync(common.TMP_DIR, {recursive: true, force: true});
});
it('failure with invalid tar', async () => {
it("failure with invalid tar", async () => {
if (!existsSync(common.TMP_DIR)) {
mkdirSync(common.TMP_DIR, {recursive: true});
}
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_INVALID, IMAGE_INVALID, {}, true, (f) => f.endsWith('.ota'));
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_INVALID, IMAGE_INVALID, {}, true, (f) => f.endsWith(".ota"));
expect(status).toStrictEqual(ProcessFirmwareImageStatus.TAR_NO_IMAGE);
expect(status).toStrictEqual(ProcessFirmwareImageStatus.TarNoImage);
expectNoChanges(true);
rmSync(common.TMP_DIR, {recursive: true, force: true});
});
it('failure with extract tar (missing dir)', async () => {
it("failure with extract tar (missing dir)", async () => {
// if (!existsSync(common.TMP_DIR)) {
// mkdirSync(common.TMP_DIR, {recursive: true});
// }
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_TAR, IMAGE_TAR, {}, true, (f) => f.endsWith('.ota'));
const status = await processFirmwareImage(IMAGES_TEST_DIR, IMAGE_TAR, IMAGE_TAR, {}, true, (f) => f.endsWith(".ota"));
expect(status).toStrictEqual(ProcessFirmwareImageStatus.TAR_NO_IMAGE);
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.objectContaining({syscall: 'chdir', code: 'ENOENT'}));
expect(status).toStrictEqual(ProcessFirmwareImageStatus.TarNoImage);
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.objectContaining({syscall: "chdir", code: "ENOENT"}));
expectNoChanges(false);
rmSync(common.TMP_DIR, {recursive: true, force: true});