From 225c7c6f9ca57ab8989a1897535feb30ccb699d9 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 12 Apr 2026 03:58:54 +0100 Subject: [PATCH] test(secrets): share channel snapshot fixtures --- src/secrets/runtime-discord-surface.test.ts | 65 +++---------------- src/secrets/runtime-discord.test-support.ts | 24 +++++++ ...runtime-inactive-telegram-surfaces.test.ts | 53 ++------------- src/secrets/runtime-telegram.test-support.ts | 24 +++++++ 4 files changed, 60 insertions(+), 106 deletions(-) create mode 100644 src/secrets/runtime-discord.test-support.ts create mode 100644 src/secrets/runtime-telegram.test-support.ts diff --git a/src/secrets/runtime-discord-surface.test.ts b/src/secrets/runtime-discord-surface.test.ts index 68fa100f8ca..c034ab0fa08 100644 --- a/src/secrets/runtime-discord-surface.test.ts +++ b/src/secrets/runtime-discord-surface.test.ts @@ -1,63 +1,14 @@ -import { afterEach, beforeAll, describe, expect, it, vi } from "vitest"; -import type { AuthProfileStore } from "../agents/auth-profiles.js"; -import type { OpenClawConfig } from "../config/config.js"; -import { createEmptyPluginRegistry } from "../plugins/registry.js"; -import { setActivePluginRegistry } from "../plugins/runtime.js"; -import { loadBundledChannelSecretContractApi } from "./channel-contract-api.js"; +import { describe, expect, it } from "vitest"; +import "./runtime-discord.test-support.ts"; +import { + asConfig, + loadAuthStoreWithProfiles, + setupSecretsRuntimeSnapshotTestHooks, +} from "./runtime.test-support.ts"; -const discordSecrets = loadBundledChannelSecretContractApi("discord"); -if (!discordSecrets?.collectRuntimeConfigAssignments) { - throw new Error("Missing Discord secret contract api"); -} - -vi.mock("../channels/plugins/bootstrap-registry.js", () => { - return { - getBootstrapChannelPlugin: (id: string) => - id === "discord" - ? { - secrets: { - collectRuntimeConfigAssignments: discordSecrets.collectRuntimeConfigAssignments, - }, - } - : undefined, - getBootstrapChannelSecrets: (id: string) => - id === "discord" - ? { - collectRuntimeConfigAssignments: discordSecrets.collectRuntimeConfigAssignments, - } - : undefined, - }; -}); - -function asConfig(value: unknown): OpenClawConfig { - return value as OpenClawConfig; -} - -let clearConfigCache: typeof import("../config/config.js").clearConfigCache; -let clearRuntimeConfigSnapshot: typeof import("../config/config.js").clearRuntimeConfigSnapshot; -let clearSecretsRuntimeSnapshot: typeof import("./runtime.js").clearSecretsRuntimeSnapshot; -let prepareSecretsRuntimeSnapshot: typeof import("./runtime.js").prepareSecretsRuntimeSnapshot; - -function loadAuthStoreWithProfiles(profiles: AuthProfileStore["profiles"]): AuthProfileStore { - return { - version: 1, - profiles, - }; -} +const { prepareSecretsRuntimeSnapshot } = setupSecretsRuntimeSnapshotTestHooks(); describe("secrets runtime snapshot discord surface", () => { - beforeAll(async () => { - ({ clearConfigCache, clearRuntimeConfigSnapshot } = await import("../config/config.js")); - ({ clearSecretsRuntimeSnapshot, prepareSecretsRuntimeSnapshot } = await import("./runtime.js")); - }); - - afterEach(() => { - setActivePluginRegistry(createEmptyPluginRegistry()); - clearSecretsRuntimeSnapshot(); - clearRuntimeConfigSnapshot(); - clearConfigCache(); - }); - it("fails when non-default Discord account inherits an unresolved top-level token ref", async () => { await expect( prepareSecretsRuntimeSnapshot({ diff --git a/src/secrets/runtime-discord.test-support.ts b/src/secrets/runtime-discord.test-support.ts new file mode 100644 index 00000000000..9b25ad79bf2 --- /dev/null +++ b/src/secrets/runtime-discord.test-support.ts @@ -0,0 +1,24 @@ +import { vi } from "vitest"; +import { loadBundledChannelSecretContractApi } from "./channel-contract-api.js"; + +const discordSecrets = loadBundledChannelSecretContractApi("discord"); +if (!discordSecrets?.collectRuntimeConfigAssignments) { + throw new Error("Missing Discord secret contract api"); +} + +vi.mock("../channels/plugins/bootstrap-registry.js", () => ({ + getBootstrapChannelPlugin: (id: string) => + id === "discord" + ? { + secrets: { + collectRuntimeConfigAssignments: discordSecrets.collectRuntimeConfigAssignments, + }, + } + : undefined, + getBootstrapChannelSecrets: (id: string) => + id === "discord" + ? { + collectRuntimeConfigAssignments: discordSecrets.collectRuntimeConfigAssignments, + } + : undefined, +})); diff --git a/src/secrets/runtime-inactive-telegram-surfaces.test.ts b/src/secrets/runtime-inactive-telegram-surfaces.test.ts index 54ff92d2b63..97139b50c1e 100644 --- a/src/secrets/runtime-inactive-telegram-surfaces.test.ts +++ b/src/secrets/runtime-inactive-telegram-surfaces.test.ts @@ -1,55 +1,10 @@ -import { afterEach, beforeAll, describe, expect, it, vi } from "vitest"; -import type { OpenClawConfig } from "../config/config.js"; -import { createEmptyPluginRegistry } from "../plugins/registry.js"; -import { setActivePluginRegistry } from "../plugins/runtime.js"; -import { loadBundledChannelSecretContractApi } from "./channel-contract-api.js"; +import { describe, expect, it } from "vitest"; +import "./runtime-telegram.test-support.ts"; +import { asConfig, setupSecretsRuntimeSnapshotTestHooks } from "./runtime.test-support.ts"; -const telegramSecrets = loadBundledChannelSecretContractApi("telegram"); -if (!telegramSecrets?.collectRuntimeConfigAssignments) { - throw new Error("Missing Telegram secret contract api"); -} - -vi.mock("../channels/plugins/bootstrap-registry.js", () => { - return { - getBootstrapChannelPlugin: (id: string) => - id === "telegram" - ? { - secrets: { - collectRuntimeConfigAssignments: telegramSecrets.collectRuntimeConfigAssignments, - }, - } - : undefined, - getBootstrapChannelSecrets: (id: string) => - id === "telegram" - ? { - collectRuntimeConfigAssignments: telegramSecrets.collectRuntimeConfigAssignments, - } - : undefined, - }; -}); - -function asConfig(value: unknown): OpenClawConfig { - return value as OpenClawConfig; -} - -let clearConfigCache: typeof import("../config/config.js").clearConfigCache; -let clearRuntimeConfigSnapshot: typeof import("../config/config.js").clearRuntimeConfigSnapshot; -let clearSecretsRuntimeSnapshot: typeof import("./runtime.js").clearSecretsRuntimeSnapshot; -let prepareSecretsRuntimeSnapshot: typeof import("./runtime.js").prepareSecretsRuntimeSnapshot; +const { prepareSecretsRuntimeSnapshot } = setupSecretsRuntimeSnapshotTestHooks(); describe("secrets runtime snapshot inactive telegram surfaces", () => { - beforeAll(async () => { - ({ clearConfigCache, clearRuntimeConfigSnapshot } = await import("../config/config.js")); - ({ clearSecretsRuntimeSnapshot, prepareSecretsRuntimeSnapshot } = await import("./runtime.js")); - }); - - afterEach(() => { - setActivePluginRegistry(createEmptyPluginRegistry()); - clearSecretsRuntimeSnapshot(); - clearRuntimeConfigSnapshot(); - clearConfigCache(); - }); - it("skips inactive Telegram refs and emits diagnostics", async () => { const snapshot = await prepareSecretsRuntimeSnapshot({ config: asConfig({ diff --git a/src/secrets/runtime-telegram.test-support.ts b/src/secrets/runtime-telegram.test-support.ts new file mode 100644 index 00000000000..a4fbcbdae1e --- /dev/null +++ b/src/secrets/runtime-telegram.test-support.ts @@ -0,0 +1,24 @@ +import { vi } from "vitest"; +import { loadBundledChannelSecretContractApi } from "./channel-contract-api.js"; + +const telegramSecrets = loadBundledChannelSecretContractApi("telegram"); +if (!telegramSecrets?.collectRuntimeConfigAssignments) { + throw new Error("Missing Telegram secret contract api"); +} + +vi.mock("../channels/plugins/bootstrap-registry.js", () => ({ + getBootstrapChannelPlugin: (id: string) => + id === "telegram" + ? { + secrets: { + collectRuntimeConfigAssignments: telegramSecrets.collectRuntimeConfigAssignments, + }, + } + : undefined, + getBootstrapChannelSecrets: (id: string) => + id === "telegram" + ? { + collectRuntimeConfigAssignments: telegramSecrets.collectRuntimeConfigAssignments, + } + : undefined, +}));