Files
Multi-Debrid-Downloader/tests/account-edit.test.ts
T

116 lines
4.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { defaultSettings } from "../src/main/constants";
import { createRendererState } from "../src/main/renderer-state";
import { getMegaDebridAccountId } from "../src/shared/mega-debrid-accounts";
import {
buildAccountDeleteCommand,
buildAccountReplaceCommand,
createAccountEditState,
validateAccountEdit,
type AccountEditTarget
} from "../src/renderer/account-edit";
function megaTarget(identity: string): AccountEditTarget {
const accountId = getMegaDebridAccountId(identity);
return {
type: "mega",
rowKey: `mega-megadebrid-api-${accountId}`,
kind: "megadebrid-api",
service: "megadebrid-api",
accountId
};
}
describe("renderer-safe account editing", () => {
it("targets one concrete Real-Debrid API account for replace and delete", () => {
const settings = defaultSettings();
settings.realDebridApiTokens = JSON.stringify({
version: 1,
accounts: [
{ id: "rda_first", token: "fixture-token-first" },
{ id: "rda_second", token: "fixture-token-second" }
]
});
const renderer = createRendererState(settings);
const target: AccountEditTarget = {
type: "single",
rowKey: "rd-rda_second",
kind: "realdebrid-api",
service: "realdebrid",
provider: "realdebrid",
accountId: "rda_second"
};
const edit = createAccountEditState(target, renderer.accounts);
expect(buildAccountReplaceCommand({ ...edit, token: "replacement-token" })).toEqual(expect.objectContaining({
action: "replace",
kind: "realdebrid-api",
accountId: "rda_second",
secret: "replacement-token"
}));
expect(buildAccountDeleteCommand(target)).toEqual({
action: "delete",
kind: "realdebrid-api",
accountId: "rda_second"
});
});
it("opens an existing account without reading its stored secret", () => {
const identity = "safe-edit@example.test";
const state = createRendererState({
...defaultSettings(),
megaCredentials: `${identity}:fixture-stored-secret-4dH8`,
megaDebridApiCredentials: `${identity}:fixture-stored-secret-4dH8`,
megaDebridApiEnabled: true
});
const edit = createAccountEditState(megaTarget(identity), state.accounts);
expect(edit.login).toBe(identity);
expect(edit.password).toBe("");
expect(JSON.stringify(edit)).not.toContain("fixture-stored-secret-4dH8");
});
it("builds a replace command whose blank secret retains the main-process value", () => {
const identity = "safe-edit@example.test";
const renderer = createRendererState({
...defaultSettings(),
megaCredentials: `${identity}:fixture-stored-secret-4dH8`,
megaDebridApiCredentials: `${identity}:fixture-stored-secret-4dH8`,
megaDebridApiEnabled: true
});
const edit = createAccountEditState(megaTarget(identity), renderer.accounts);
expect(validateAccountEdit(edit, renderer.accounts)).toBeNull();
expect(buildAccountReplaceCommand(edit)).toEqual(expect.objectContaining({
action: "replace",
accountId: getMegaDebridAccountId(identity),
identity,
secret: ""
}));
});
it("rejects duplicate identities using safe account metadata", () => {
const first = "first-safe@example.test";
const second = "second-safe@example.test";
const renderer = createRendererState({
...defaultSettings(),
megaCredentials: `${first}:fixture-first-secret-1aB2\n${second}:fixture-second-secret-3cD4`,
megaDebridApiCredentials: `${first}:fixture-first-secret-1aB2\n${second}:fixture-second-secret-3cD4`,
megaDebridApiEnabled: true
});
const edit = { ...createAccountEditState(megaTarget(second), renderer.accounts), login: first };
expect(validateAccountEdit(edit, renderer.accounts)).toMatch(/bereits vorhanden/i);
});
it("builds an identity-only delete command", () => {
const target = megaTarget("delete-safe@example.test");
expect(buildAccountDeleteCommand(target)).toEqual({
action: "delete",
kind: "megadebrid-api",
accountId: target.type === "mega" ? target.accountId : ""
});
});
});