mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 19:24:31 +00:00
refactor: dedupe runtime snapshot test fixtures
This commit is contained in:
@@ -12,96 +12,67 @@ import {
|
|||||||
} from "./io.js";
|
} from "./io.js";
|
||||||
import type { OpenClawConfig } from "./types.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", () => {
|
describe("runtime config snapshot writes", () => {
|
||||||
it("returns the source snapshot when runtime snapshot is active", async () => {
|
it("returns the source snapshot when runtime snapshot is active", async () => {
|
||||||
await withTempHome("openclaw-config-runtime-source-", async () => {
|
await withTempHome("openclaw-config-runtime-source-", async () => {
|
||||||
const sourceConfig: OpenClawConfig = {
|
const sourceConfig = createSourceConfig();
|
||||||
models: {
|
const runtimeConfig = createRuntimeConfig();
|
||||||
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: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
try {
|
try {
|
||||||
setRuntimeConfigSnapshot(runtimeConfig, sourceConfig);
|
setRuntimeConfigSnapshot(runtimeConfig, sourceConfig);
|
||||||
expect(getRuntimeConfigSourceSnapshot()).toEqual(sourceConfig);
|
expect(getRuntimeConfigSourceSnapshot()).toEqual(sourceConfig);
|
||||||
} finally {
|
} finally {
|
||||||
clearRuntimeConfigSnapshot();
|
resetRuntimeConfigState();
|
||||||
clearConfigCache();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("clears runtime source snapshot when runtime snapshot is cleared", async () => {
|
it("clears runtime source snapshot when runtime snapshot is cleared", async () => {
|
||||||
const sourceConfig: OpenClawConfig = {
|
const sourceConfig = createSourceConfig();
|
||||||
models: {
|
const runtimeConfig = createRuntimeConfig();
|
||||||
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: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
setRuntimeConfigSnapshot(runtimeConfig, sourceConfig);
|
setRuntimeConfigSnapshot(runtimeConfig, sourceConfig);
|
||||||
clearRuntimeConfigSnapshot();
|
resetRuntimeConfigState();
|
||||||
clearConfigCache();
|
|
||||||
expect(getRuntimeConfigSourceSnapshot()).toBeNull();
|
expect(getRuntimeConfigSourceSnapshot()).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("preserves source secret refs when writeConfigFile receives runtime-resolved config", async () => {
|
it("preserves source secret refs when writeConfigFile receives runtime-resolved config", async () => {
|
||||||
await withTempHome("openclaw-config-runtime-write-", async (home) => {
|
await withTempHome("openclaw-config-runtime-write-", async (home) => {
|
||||||
const configPath = path.join(home, ".openclaw", "openclaw.json");
|
const configPath = path.join(home, ".openclaw", "openclaw.json");
|
||||||
const sourceConfig: OpenClawConfig = {
|
const sourceConfig = createSourceConfig();
|
||||||
models: {
|
const runtimeConfig = createRuntimeConfig();
|
||||||
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: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
await fs.mkdir(path.dirname(configPath), { recursive: true });
|
await fs.mkdir(path.dirname(configPath), { recursive: true });
|
||||||
await fs.writeFile(configPath, `${JSON.stringify(sourceConfig, null, 2)}\n`, "utf8");
|
await fs.writeFile(configPath, `${JSON.stringify(sourceConfig, null, 2)}\n`, "utf8");
|
||||||
@@ -121,8 +92,7 @@ describe("runtime config snapshot writes", () => {
|
|||||||
id: "OPENAI_API_KEY",
|
id: "OPENAI_API_KEY",
|
||||||
});
|
});
|
||||||
} finally {
|
} finally {
|
||||||
clearRuntimeConfigSnapshot();
|
resetRuntimeConfigState();
|
||||||
clearConfigCache();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user