From 02666e91393ec304e1098c01cd468889cccbd9d6 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Mon, 30 Dec 2024 14:22:18 +0100 Subject: [PATCH] fix(ignore): Fix 2 to 3 migration enabling frontend/availabilty/homeassistant when not set (#25349) --- lib/util/settingsMigration.ts | 7 ++++--- test/settingsMigration.test.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/util/settingsMigration.ts b/lib/util/settingsMigration.ts index 56908fe994..6663cd0e38 100644 --- a/lib/util/settingsMigration.ts +++ b/lib/util/settingsMigration.ts @@ -86,16 +86,17 @@ function setValue(currentSettings: any, path: string[], value: unknown, createPa function getValue(currentSettings: any, path: string[]): [validPath: boolean, value: unknown] { for (let i = 0; i < path.length; i++) { const key = path[i]; + const value = currentSettings[key]; if (i === path.length - 1) { - return [true, currentSettings[key]]; + return [value !== undefined, value]; } else { - if (!currentSettings[key]) { + if (!value) { // invalid path break; } - currentSettings = currentSettings[key]; + currentSettings = value; } } diff --git a/test/settingsMigration.test.ts b/test/settingsMigration.test.ts index d7a188db70..4955fd6b9e 100644 --- a/test/settingsMigration.test.ts +++ b/test/settingsMigration.test.ts @@ -807,6 +807,36 @@ describe('Settings Migration', () => { expect(migrationNotesContent).toContain(`[SPECIAL] Property 'frontend' is now always an object.`); expect(migrationNotesContent).toContain(`[SPECIAL] Property 'availability' is now always an object.`); }); + + it('Update when not set, tests that frontend/availability is not added when not set', () => { + // @ts-expect-error workaround + const beforeSettings = objectAssignDeep.noMutate({}, settings.getPersistedSettings()); + // @ts-expect-error workaround + const afterSettings = objectAssignDeep.noMutate({}, settings.getPersistedSettings()); + afterSettings.version = 3; + afterSettings.homeassistant = {enabled: false}; + + settings.set(['homeassistant'], false); + + expect(settings.getPersistedSettings()).toStrictEqual( + // @ts-expect-error workaround + objectAssignDeep.noMutate(beforeSettings, { + homeassistant: false, + }), + ); + + settingsMigration.migrateIfNecessary(); + + const migratedSettings = settings.getPersistedSettings(); + + expect(migratedSettings).toStrictEqual(afterSettings); + const migrationNotes = mockedData.joinPath('migration-2-to-3.log'); + expect(existsSync(migrationNotes)).toStrictEqual(true); + const migrationNotesContent = readFileSync(migrationNotes, 'utf8'); + expect(migrationNotesContent).toContain(`[SPECIAL] Property 'homeassistant' is now always an object.`); + expect(migrationNotesContent).not.toContain(`[SPECIAL] Property 'frontend' is now always an object.`); + expect(migrationNotesContent).not.toContain(`[SPECIAL] Property 'availability' is now always an object.`); + }); }); describe('Migrates v3 to v4', () => {