mirror of
https://github.com/moltbot/moltbot.git
synced 2026-03-07 22:44:16 +00:00
perf(test): speed up setup and config path resolution
This commit is contained in:
@@ -9,15 +9,11 @@ import {
|
||||
import { fetchTelegramChatId } from "../channels/telegram/api.js";
|
||||
import { formatCliCommand } from "../cli/command-format.js";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import {
|
||||
OpenClawSchema,
|
||||
CONFIG_PATH,
|
||||
migrateLegacyConfig,
|
||||
readConfigFileSnapshot,
|
||||
} from "../config/config.js";
|
||||
import { CONFIG_PATH, migrateLegacyConfig, readConfigFileSnapshot } from "../config/config.js";
|
||||
import { collectProviderDangerousNameMatchingScopes } from "../config/dangerous-name-matching.js";
|
||||
import { applyPluginAutoEnable } from "../config/plugin-auto-enable.js";
|
||||
import { parseToolsBySenderTypedKey } from "../config/types.tools.js";
|
||||
import { OpenClawSchema } from "../config/zod-schema.js";
|
||||
import { resolveCommandResolutionFromArgv } from "../infra/exec-command-resolution.js";
|
||||
import {
|
||||
listInterpreterLikeSafeBins,
|
||||
|
||||
@@ -21,4 +21,3 @@ export {
|
||||
validateConfigObjectRawWithPlugins,
|
||||
validateConfigObjectWithPlugins,
|
||||
} from "./validation.js";
|
||||
export { OpenClawSchema } from "./zod-schema.js";
|
||||
|
||||
@@ -67,6 +67,9 @@ export function resolveStateDir(
|
||||
return resolveUserPath(override, env, effectiveHomedir);
|
||||
}
|
||||
const newDir = newStateDir(effectiveHomedir);
|
||||
if (env.OPENCLAW_TEST_FAST === "1") {
|
||||
return newDir;
|
||||
}
|
||||
const legacyDirs = legacyStateDirs(effectiveHomedir);
|
||||
const hasNew = fs.existsSync(newDir);
|
||||
if (hasNew) {
|
||||
@@ -131,6 +134,9 @@ export function resolveConfigPathCandidate(
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
homedir: () => string = envHomedir(env),
|
||||
): string {
|
||||
if (env.OPENCLAW_TEST_FAST === "1") {
|
||||
return resolveCanonicalConfigPath(env, resolveStateDir(env, homedir));
|
||||
}
|
||||
const candidates = resolveDefaultConfigCandidates(env, homedir);
|
||||
const existing = candidates.find((candidate) => {
|
||||
try {
|
||||
@@ -157,6 +163,9 @@ export function resolveConfigPath(
|
||||
if (override) {
|
||||
return resolveUserPath(override, env, homedir);
|
||||
}
|
||||
if (env.OPENCLAW_TEST_FAST === "1") {
|
||||
return path.join(stateDir, CONFIG_FILENAME);
|
||||
}
|
||||
const stateOverride = env.OPENCLAW_STATE_DIR?.trim();
|
||||
const candidates = [
|
||||
path.join(stateDir, CONFIG_FILENAME),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { afterAll, afterEach, beforeEach, vi } from "vitest";
|
||||
import { afterAll, afterEach, beforeAll, vi } from "vitest";
|
||||
|
||||
// Ensure Vitest environment is properly set
|
||||
process.env.VITEST = "true";
|
||||
@@ -25,12 +25,15 @@ import { withIsolatedTestHome } from "./test-env.js";
|
||||
const testEnv = withIsolatedTestHome();
|
||||
afterAll(() => testEnv.cleanup());
|
||||
|
||||
const [{ installProcessWarningFilter }, { setActivePluginRegistry }, { createTestRegistry }] =
|
||||
await Promise.all([
|
||||
import("../src/infra/warning-filter.js"),
|
||||
import("../src/plugins/runtime.js"),
|
||||
import("../src/test-utils/channel-plugins.js"),
|
||||
]);
|
||||
const [
|
||||
{ installProcessWarningFilter },
|
||||
{ getActivePluginRegistry, setActivePluginRegistry },
|
||||
{ createTestRegistry },
|
||||
] = await Promise.all([
|
||||
import("../src/infra/warning-filter.js"),
|
||||
import("../src/plugins/runtime.js"),
|
||||
import("../src/test-utils/channel-plugins.js"),
|
||||
]);
|
||||
|
||||
installProcessWarningFilter();
|
||||
|
||||
@@ -172,16 +175,18 @@ const createDefaultRegistry = () =>
|
||||
},
|
||||
]);
|
||||
|
||||
// Creating a fresh registry before every single test was measurable overhead.
|
||||
// The registry is treated as immutable by production code; tests that need a
|
||||
// custom registry set it explicitly.
|
||||
// Creating a fresh registry before every test is measurable overhead.
|
||||
// The registry is immutable by default; tests that override it are restored in afterEach.
|
||||
const DEFAULT_PLUGIN_REGISTRY = createDefaultRegistry();
|
||||
|
||||
beforeEach(() => {
|
||||
beforeAll(() => {
|
||||
setActivePluginRegistry(DEFAULT_PLUGIN_REGISTRY);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (getActivePluginRegistry() !== DEFAULT_PLUGIN_REGISTRY) {
|
||||
setActivePluginRegistry(DEFAULT_PLUGIN_REGISTRY);
|
||||
}
|
||||
// Guard against leaked fake timers across test files/workers.
|
||||
if (vi.isFakeTimers()) {
|
||||
vi.useRealTimers();
|
||||
|
||||
Reference in New Issue
Block a user