refactor: derive setup promotion rules from plugins

This commit is contained in:
Peter Steinberger
2026-04-22 05:40:12 +01:00
parent db055a5c0d
commit 0c863124bb
3 changed files with 36 additions and 28 deletions

View File

@@ -22,23 +22,19 @@ describe("setup promotion helpers", () => {
getLoadedChannelPluginMock.mockReset();
});
it("keeps static named-account migration keys cheap", () => {
it("keeps static single-account migration keys cheap", () => {
const keys = resolveSingleAccountKeysToMove({
channelKey: "whatsapp",
channelKey: "demo",
channel: {
accounts: {
work: { enabled: true },
},
dmPolicy: "allowlist",
allowFrom: ["+15551234567"],
groupPolicy: "allowlist",
groupAllowFrom: ["120363000000000000@g.us"],
groupAllowFrom: ["group-123"],
},
});
expect(keys).toEqual(["dmPolicy", "allowFrom", "groupPolicy", "groupAllowFrom"]);
expect(getLoadedChannelPluginMock).toHaveBeenCalledTimes(1);
expect(getLoadedChannelPluginMock).toHaveBeenCalledWith("whatsapp");
expect(getLoadedChannelPluginMock).not.toHaveBeenCalled();
expect(getBundledChannelPluginMock).not.toHaveBeenCalled();
});
@@ -79,4 +75,27 @@ describe("setup promotion helpers", () => {
expect(keys).toEqual(["token"]);
expect(getBundledChannelPluginMock).not.toHaveBeenCalled();
});
it("loads bundled setup for named-account filters before registry bootstrap", () => {
getBundledChannelPluginMock.mockReturnValue({
setup: {
namedAccountPromotionKeys: ["token"],
},
});
const keys = resolveSingleAccountKeysToMove({
channelKey: "demo",
channel: {
accounts: {
work: { enabled: true },
},
token: "secret",
dmPolicy: "allowlist",
},
});
expect(keys).toEqual(["token"]);
expect(getLoadedChannelPluginMock).toHaveBeenCalledWith("demo");
expect(getBundledChannelPluginMock).toHaveBeenCalledWith("demo");
});
});

View File

@@ -41,16 +41,6 @@ const COMMON_SINGLE_ACCOUNT_KEYS_TO_MOVE = new Set([
"defaultTo",
]);
const BUNDLED_SINGLE_ACCOUNT_PROMOTION_FALLBACKS: Record<string, readonly string[]> = {
// Some setup/migration paths run before the channel setup surface has been loaded.
telegram: ["streaming"],
};
const BUNDLED_NAMED_ACCOUNT_PROMOTION_FALLBACKS: Record<string, readonly string[]> = {
// Keep top-level Telegram policy fallback intact when only auth needs seeding.
telegram: ["botToken", "tokenFile"],
};
type ChannelSetupPromotionSurface = {
singleAccountKeysToMove?: readonly string[];
namedAccountPromotionKeys?: readonly string[];
@@ -72,18 +62,15 @@ function getChannelSetupPromotionSurface(
return setup as ChannelSetupPromotionSurface;
}
function isStaticSingleAccountPromotionKey(channelKey: string, key: string): boolean {
if (COMMON_SINGLE_ACCOUNT_KEYS_TO_MOVE.has(key)) {
return true;
}
return BUNDLED_SINGLE_ACCOUNT_PROMOTION_FALLBACKS[channelKey]?.includes(key) ?? false;
function isStaticSingleAccountPromotionKey(key: string): boolean {
return COMMON_SINGLE_ACCOUNT_KEYS_TO_MOVE.has(key);
}
export function shouldMoveSingleAccountChannelKey(params: {
channelKey: string;
key: string;
}): boolean {
if (isStaticSingleAccountPromotionKey(params.channelKey, params.key)) {
if (isStaticSingleAccountPromotionKey(params.key)) {
return true;
}
const contractKeys = getChannelSetupPromotionSurface(params.channelKey, {
@@ -118,7 +105,7 @@ export function resolveSingleAccountKeysToMove(params: {
};
const keysToMove = entries.filter((key) => {
if (isStaticSingleAccountPromotionKey(params.channelKey, key)) {
if (isStaticSingleAccountPromotionKey(key)) {
return true;
}
return Boolean(resolveSetupSurface()?.singleAccountKeysToMove?.includes(key));
@@ -128,9 +115,7 @@ export function resolveSingleAccountKeysToMove(params: {
}
const namedAccountPromotionKeys =
setupSurface?.namedAccountPromotionKeys ??
getChannelSetupPromotionSurface(params.channelKey)?.namedAccountPromotionKeys ??
BUNDLED_NAMED_ACCOUNT_PROMOTION_FALLBACKS[params.channelKey];
setupSurface?.namedAccountPromotionKeys ?? resolveSetupSurface()?.namedAccountPromotionKeys;
if (!namedAccountPromotionKeys) {
return keysToMove;
}

View File

@@ -62,6 +62,10 @@ const CORE_SECRET_SURFACE_GUARDS = [
path: "src/config/sessions/group.ts",
forbiddenPatterns: [/\bwhatsapp\b/, /@g\.us/],
},
{
path: "src/channels/plugins/setup-promotion-helpers.ts",
forbiddenPatterns: [/\btelegram\b/],
},
{
path: "src/media-understanding/defaults.ts",
forbiddenPatterns: [