From 143eca8e8677892f0162d5e78bc6dff4754562e4 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 7 Mar 2026 18:47:37 +0000 Subject: [PATCH] refactor: dedupe runtime snapshot test fixtures --- src/config/io.runtime-snapshot-write.test.ts | 114 +++++++------------ 1 file changed, 42 insertions(+), 72 deletions(-) diff --git a/src/config/io.runtime-snapshot-write.test.ts b/src/config/io.runtime-snapshot-write.test.ts index e8820ad1d9c..b9ea7d51edb 100644 --- a/src/config/io.runtime-snapshot-write.test.ts +++ b/src/config/io.runtime-snapshot-write.test.ts @@ -12,96 +12,67 @@ import { } from "./io.js"; import type { OpenClawConfig } from "./types.js"; +function createSourceConfig(): OpenClawConfig { + return { + models: { + providers: { + openai: { + baseUrl: "https://api.openai.com/v1", + apiKey: { source: "env", provider: "default", id: "OPENAI_API_KEY" }, + models: [], + }, + }, + }, + }; +} + +function createRuntimeConfig(): OpenClawConfig { + return { + models: { + providers: { + openai: { + baseUrl: "https://api.openai.com/v1", + apiKey: "sk-runtime-resolved", // pragma: allowlist secret + models: [], + }, + }, + }, + }; +} + +function resetRuntimeConfigState(): void { + clearRuntimeConfigSnapshot(); + clearConfigCache(); +} + describe("runtime config snapshot writes", () => { it("returns the source snapshot when runtime snapshot is active", async () => { await withTempHome("openclaw-config-runtime-source-", async () => { - const sourceConfig: OpenClawConfig = { - models: { - providers: { - openai: { - baseUrl: "https://api.openai.com/v1", - apiKey: { source: "env", provider: "default", id: "OPENAI_API_KEY" }, - models: [], - }, - }, - }, - }; - const runtimeConfig: OpenClawConfig = { - models: { - providers: { - openai: { - baseUrl: "https://api.openai.com/v1", - apiKey: "sk-runtime-resolved", // pragma: allowlist secret - models: [], - }, - }, - }, - }; + const sourceConfig = createSourceConfig(); + const runtimeConfig = createRuntimeConfig(); try { setRuntimeConfigSnapshot(runtimeConfig, sourceConfig); expect(getRuntimeConfigSourceSnapshot()).toEqual(sourceConfig); } finally { - clearRuntimeConfigSnapshot(); - clearConfigCache(); + resetRuntimeConfigState(); } }); }); it("clears runtime source snapshot when runtime snapshot is cleared", async () => { - const sourceConfig: OpenClawConfig = { - models: { - providers: { - openai: { - baseUrl: "https://api.openai.com/v1", - apiKey: { source: "env", provider: "default", id: "OPENAI_API_KEY" }, - models: [], - }, - }, - }, - }; - const runtimeConfig: OpenClawConfig = { - models: { - providers: { - openai: { - baseUrl: "https://api.openai.com/v1", - apiKey: "sk-runtime-resolved", // pragma: allowlist secret - models: [], - }, - }, - }, - }; + const sourceConfig = createSourceConfig(); + const runtimeConfig = createRuntimeConfig(); setRuntimeConfigSnapshot(runtimeConfig, sourceConfig); - clearRuntimeConfigSnapshot(); - clearConfigCache(); + resetRuntimeConfigState(); expect(getRuntimeConfigSourceSnapshot()).toBeNull(); }); it("preserves source secret refs when writeConfigFile receives runtime-resolved config", async () => { await withTempHome("openclaw-config-runtime-write-", async (home) => { const configPath = path.join(home, ".openclaw", "openclaw.json"); - const sourceConfig: OpenClawConfig = { - models: { - providers: { - openai: { - baseUrl: "https://api.openai.com/v1", - apiKey: { source: "env", provider: "default", id: "OPENAI_API_KEY" }, - models: [], - }, - }, - }, - }; - const runtimeConfig: OpenClawConfig = { - models: { - providers: { - openai: { - baseUrl: "https://api.openai.com/v1", - apiKey: "sk-runtime-resolved", // pragma: allowlist secret - models: [], - }, - }, - }, - }; + const sourceConfig = createSourceConfig(); + const runtimeConfig = createRuntimeConfig(); await fs.mkdir(path.dirname(configPath), { recursive: true }); await fs.writeFile(configPath, `${JSON.stringify(sourceConfig, null, 2)}\n`, "utf8"); @@ -121,8 +92,7 @@ describe("runtime config snapshot writes", () => { id: "OPENAI_API_KEY", }); } finally { - clearRuntimeConfigSnapshot(); - clearConfigCache(); + resetRuntimeConfigState(); } }); });