diff --git a/src/renderer/App.tsx b/src/renderer/App.tsx index 804c636..c37cfea 100644 --- a/src/renderer/App.tsx +++ b/src/renderer/App.tsx @@ -109,7 +109,7 @@ interface MegaDialogAccount { password: string; } -interface AccountDialogState { +export interface AccountDialogState { mode: "create" | "edit"; kind: AccountKind | null; token: string; @@ -647,7 +647,7 @@ function createAccountDialogState(mode: "create" | "edit", kind: AccountKind | n } } -function applyAccountDialogToSettings(settings: AppSettings, dialog: AccountDialogState): AppSettings { +export function applyAccountDialogToSettings(settings: AppSettings, dialog: AccountDialogState): AppSettings { if (!dialog.kind) { return settings; } @@ -682,7 +682,7 @@ function applyAccountDialogToSettings(settings: AppSettings, dialog: AccountDial const firstPassword = megaParsed.length > 0 ? megaParsed[0].password : ""; const validIds = new Set(megaParsed.map((a) => a.id)); const megaDebridDisabledAccountIds = (dialog.megaDisabledIds || []).filter((id) => validIds.has(id)); - return { ...settings, megaCredentials: megaSerialized, megaLogin: firstLogin, megaPassword: firstPassword, megaDebridApiEnabled: true, megaDebridPreferApi: true, megaDebridDisabledAccountIds, providerDailyLimitBytes: nextProviderDailyLimitBytes }; + return { ...settings, megaCredentials: megaSerialized, megaLogin: firstLogin, megaPassword: firstPassword, megaDebridApiEnabled: true, megaDebridDisabledAccountIds, providerDailyLimitBytes: nextProviderDailyLimitBytes }; } case "megadebrid-web": { const megaSerialized = serializeMegaDebridAccounts(dialog.megaAccounts); @@ -691,7 +691,7 @@ function applyAccountDialogToSettings(settings: AppSettings, dialog: AccountDial const firstPassword = megaParsed.length > 0 ? megaParsed[0].password : ""; const validIds = new Set(megaParsed.map((a) => a.id)); const megaDebridDisabledAccountIds = (dialog.megaDisabledIds || []).filter((id) => validIds.has(id)); - return { ...settings, megaCredentials: megaSerialized, megaLogin: firstLogin, megaPassword: firstPassword, megaDebridWebEnabled: true, megaDebridPreferApi: false, megaDebridDisabledAccountIds, providerDailyLimitBytes: nextProviderDailyLimitBytes }; + return { ...settings, megaCredentials: megaSerialized, megaLogin: firstLogin, megaPassword: firstPassword, megaDebridWebEnabled: true, megaDebridDisabledAccountIds, providerDailyLimitBytes: nextProviderDailyLimitBytes }; } case "bestdebrid-api": return { ...settings, bestToken: token, bestDebridUseWebLogin: false, providerDailyLimitBytes: nextProviderDailyLimitBytes }; diff --git a/tests/account-dialog-prefer-api.test.ts b/tests/account-dialog-prefer-api.test.ts new file mode 100644 index 0000000..bf57c79 --- /dev/null +++ b/tests/account-dialog-prefer-api.test.ts @@ -0,0 +1,35 @@ +import { describe, expect, it } from "vitest"; +import { applyAccountDialogToSettings, AccountDialogState } from "../src/renderer/App"; +import { defaultSettings } from "../src/main/constants"; + +function megaDialog(kind: "megadebrid-api" | "megadebrid-web"): AccountDialogState { + return { + mode: "edit", + kind, + token: "", + login: "", + password: "", + dailyLimitGb: "", + keyDailyLimitGbById: {}, + megaAccounts: [{ login: "user@x", password: "pw" }], + megaNewLogin: "", + megaNewPassword: "", + megaDisabledIds: [] + }; +} + +describe("applyAccountDialogToSettings — keeps the user's Mega preferApi choice", () => { + it("does not flip megaDebridPreferApi to true when editing the API account", () => { + const settings = { ...defaultSettings(), megaDebridApiEnabled: true, megaDebridWebEnabled: true, megaDebridPreferApi: false }; + const next = applyAccountDialogToSettings(settings, megaDialog("megadebrid-api")); + expect(next.megaDebridApiEnabled).toBe(true); + expect(next.megaDebridPreferApi).toBe(false); + }); + + it("does not flip megaDebridPreferApi to false when editing the Web account", () => { + const settings = { ...defaultSettings(), megaDebridApiEnabled: true, megaDebridWebEnabled: true, megaDebridPreferApi: true }; + const next = applyAccountDialogToSettings(settings, megaDialog("megadebrid-web")); + expect(next.megaDebridWebEnabled).toBe(true); + expect(next.megaDebridPreferApi).toBe(true); + }); +});