From 82b529a6d9b0bfd166684a9b97723952aa3c6bc9 Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Fri, 17 Apr 2026 02:58:24 -0400 Subject: [PATCH] Tests: speed up channel setup promotion --- src/channels/plugins/setup-helpers.test.ts | 4 +- .../plugins/setup-promotion-helpers.ts | 62 +++++++++++-------- .../plugins/setup-wizard-helpers.test.ts | 4 +- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/channels/plugins/setup-helpers.test.ts b/src/channels/plugins/setup-helpers.test.ts index 1a339ab47fb..4e3f0f1de38 100644 --- a/src/channels/plugins/setup-helpers.test.ts +++ b/src/channels/plugins/setup-helpers.test.ts @@ -1,4 +1,4 @@ -import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { afterAll, beforeEach, describe, expect, it } from "vitest"; import type { OpenClawConfig } from "../../config/config.js"; import { resetPluginRuntimeStateForTest, setActivePluginRegistry } from "../../plugins/runtime.js"; import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../routing/session-key.js"; @@ -53,7 +53,7 @@ function resolveMatrixSingleAccountPromotionTarget(params: { return namedAccounts.length === 1 ? namedAccounts[0] : DEFAULT_ACCOUNT_ID; } -beforeAll(() => { +beforeEach(() => { setActivePluginRegistry( createTestRegistry([ { diff --git a/src/channels/plugins/setup-promotion-helpers.ts b/src/channels/plugins/setup-promotion-helpers.ts index 76dfa17cda1..3a9355fff93 100644 --- a/src/channels/plugins/setup-promotion-helpers.ts +++ b/src/channels/plugins/setup-promotion-helpers.ts @@ -67,21 +67,24 @@ function getChannelSetupPromotionSurface(channelKey: string): ChannelSetupPromot 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; +} + export function shouldMoveSingleAccountChannelKey(params: { channelKey: string; key: string; }): boolean { - if (COMMON_SINGLE_ACCOUNT_KEYS_TO_MOVE.has(params.key)) { + if (isStaticSingleAccountPromotionKey(params.channelKey, params.key)) { return true; } const contractKeys = getChannelSetupPromotionSurface(params.channelKey)?.singleAccountKeysToMove; if (contractKeys?.includes(params.key)) { return true; } - const fallbackKeys = BUNDLED_SINGLE_ACCOUNT_PROMOTION_FALLBACKS[params.channelKey]; - if (fallbackKeys?.includes(params.key)) { - return true; - } return false; } @@ -92,27 +95,36 @@ export function resolveSingleAccountKeysToMove(params: { const hasNamedAccounts = Object.keys((params.channel.accounts as Record) ?? {}).filter(Boolean).length > 0; - const namedAccountPromotionKeys = - getChannelSetupPromotionSurface(params.channelKey)?.namedAccountPromotionKeys ?? - BUNDLED_NAMED_ACCOUNT_PROMOTION_FALLBACKS[params.channelKey]; - return Object.entries(params.channel) - .filter(([key, value]) => { - if (key === "accounts" || key === "enabled" || value === undefined) { - return false; - } - if (!shouldMoveSingleAccountChannelKey({ channelKey: params.channelKey, key })) { - return false; - } - if ( - hasNamedAccounts && - namedAccountPromotionKeys && - !namedAccountPromotionKeys.includes(key) - ) { - return false; - } - return true; - }) + const entries = Object.entries(params.channel) + .filter(([key, value]) => key !== "accounts" && key !== "enabled" && value !== undefined) .map(([key]) => key); + if (entries.length === 0) { + return []; + } + + let setupSurface: ChannelSetupPromotionSurface | null | undefined; + const resolveSetupSurface = () => { + setupSurface ??= getChannelSetupPromotionSurface(params.channelKey); + return setupSurface; + }; + + const keysToMove = entries.filter((key) => { + if (isStaticSingleAccountPromotionKey(params.channelKey, key)) { + return true; + } + return Boolean(resolveSetupSurface()?.singleAccountKeysToMove?.includes(key)); + }); + if (!hasNamedAccounts || keysToMove.length === 0) { + return keysToMove; + } + + const namedAccountPromotionKeys = + resolveSetupSurface()?.namedAccountPromotionKeys ?? + BUNDLED_NAMED_ACCOUNT_PROMOTION_FALLBACKS[params.channelKey]; + if (!namedAccountPromotionKeys) { + return keysToMove; + } + return keysToMove.filter((key) => namedAccountPromotionKeys.includes(key)); } export function resolveSingleAccountPromotionTarget(params: { diff --git a/src/channels/plugins/setup-wizard-helpers.test.ts b/src/channels/plugins/setup-wizard-helpers.test.ts index 9c55da25190..54b3bcaf038 100644 --- a/src/channels/plugins/setup-wizard-helpers.test.ts +++ b/src/channels/plugins/setup-wizard-helpers.test.ts @@ -1,4 +1,4 @@ -import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { afterAll, beforeEach, describe, expect, it, vi } from "vitest"; import { resolveSetupWizardAllowFromEntries, resolveSetupWizardGroupAllowlist, @@ -102,7 +102,7 @@ function resolveMatrixSingleAccountPromotionTarget(params: { return namedAccounts.length === 1 ? namedAccounts[0] : DEFAULT_ACCOUNT_ID; } -beforeAll(() => { +beforeEach(() => { setActivePluginRegistry( createTestRegistry([ {