diff --git a/extensions/feishu/src/onboarding.test.ts b/extensions/feishu/src/onboarding.test.ts index dbb71448508..97ba127e74c 100644 --- a/extensions/feishu/src/onboarding.test.ts +++ b/extensions/feishu/src/onboarding.test.ts @@ -17,6 +17,44 @@ const baseStatusContext = { accountOverrides: {}, }; +async function withEnvVars(values: Record, run: () => Promise) { + const previous = new Map(); + for (const [key, value] of Object.entries(values)) { + previous.set(key, process.env[key]); + if (value === undefined) { + delete process.env[key]; + } else { + process.env[key] = value; + } + } + + try { + await run(); + } finally { + for (const [key, prior] of previous.entries()) { + if (prior === undefined) { + delete process.env[key]; + } else { + process.env[key] = prior; + } + } + } +} + +async function getStatusWithEnvRefs(params: { appIdKey: string; appSecretKey: string }) { + return await feishuOnboardingAdapter.getStatus({ + cfg: { + channels: { + feishu: { + appId: { source: "env", id: params.appIdKey, provider: "default" }, + appSecret: { source: "env", id: params.appSecretKey, provider: "default" }, + }, + }, + } as never, + ...baseStatusContext, + }); +} + describe("feishuOnboardingAdapter.configure", () => { it("does not throw when config appId/appSecret are SecretRef objects", async () => { const text = vi @@ -76,72 +114,30 @@ describe("feishuOnboardingAdapter.getStatus", () => { it("treats env SecretRef appId as not configured when env var is missing", async () => { const appIdKey = "FEISHU_APP_ID_STATUS_MISSING_TEST"; const appSecretKey = "FEISHU_APP_SECRET_STATUS_MISSING_TEST"; - const prevAppId = process.env[appIdKey]; - const prevAppSecret = process.env[appSecretKey]; - delete process.env[appIdKey]; - process.env[appSecretKey] = "secret_env_456"; - - try { - const status = await feishuOnboardingAdapter.getStatus({ - cfg: { - channels: { - feishu: { - appId: { source: "env", id: appIdKey, provider: "default" }, - appSecret: { source: "env", id: appSecretKey, provider: "default" }, - }, - }, - } as never, - ...baseStatusContext, - }); - - expect(status.configured).toBe(false); - } finally { - if (prevAppId === undefined) { - delete process.env[appIdKey]; - } else { - process.env[appIdKey] = prevAppId; - } - if (prevAppSecret === undefined) { - delete process.env[appSecretKey]; - } else { - process.env[appSecretKey] = prevAppSecret; - } - } + await withEnvVars( + { + [appIdKey]: undefined, + [appSecretKey]: "secret_env_456", + }, + async () => { + const status = await getStatusWithEnvRefs({ appIdKey, appSecretKey }); + expect(status.configured).toBe(false); + }, + ); }); it("treats env SecretRef appId/appSecret as configured in status", async () => { const appIdKey = "FEISHU_APP_ID_STATUS_TEST"; const appSecretKey = "FEISHU_APP_SECRET_STATUS_TEST"; - const prevAppId = process.env[appIdKey]; - const prevAppSecret = process.env[appSecretKey]; - process.env[appIdKey] = "cli_env_123"; - process.env[appSecretKey] = "secret_env_456"; - - try { - const status = await feishuOnboardingAdapter.getStatus({ - cfg: { - channels: { - feishu: { - appId: { source: "env", id: appIdKey, provider: "default" }, - appSecret: { source: "env", id: appSecretKey, provider: "default" }, - }, - }, - } as never, - ...baseStatusContext, - }); - - expect(status.configured).toBe(true); - } finally { - if (prevAppId === undefined) { - delete process.env[appIdKey]; - } else { - process.env[appIdKey] = prevAppId; - } - if (prevAppSecret === undefined) { - delete process.env[appSecretKey]; - } else { - process.env[appSecretKey] = prevAppSecret; - } - } + await withEnvVars( + { + [appIdKey]: "cli_env_123", + [appSecretKey]: "secret_env_456", + }, + async () => { + const status = await getStatusWithEnvRefs({ appIdKey, appSecretKey }); + expect(status.configured).toBe(true); + }, + ); }); });