perf(test): eliminate resetModules via injectable seams

This commit is contained in:
Peter Steinberger
2026-02-13 16:20:31 +00:00
parent a844fb161c
commit b272158fe4
21 changed files with 223 additions and 188 deletions

View File

@@ -16,29 +16,6 @@ vi.mock("./logger.js", () => ({
}));
let loadConfigCalls = 0;
vi.mock("node:module", async () => {
const actual = await vi.importActual<typeof import("node:module")>("node:module");
return Object.assign({}, actual, {
createRequire: (url: string | URL) => {
const realRequire = actual.createRequire(url);
return (specifier: string) => {
if (specifier.endsWith("config.js")) {
return {
loadConfig: () => {
loadConfigCalls += 1;
if (loadConfigCalls > 5) {
return {};
}
console.error("config load failed");
return {};
},
};
}
return realRequire(specifier);
};
},
});
});
type ConsoleSnapshot = {
log: typeof console.log;
info: typeof console.info;
@@ -53,7 +30,6 @@ let snapshot: ConsoleSnapshot;
beforeEach(() => {
loadConfigCalls = 0;
vi.resetModules();
snapshot = {
log: console.log,
info: console.info,
@@ -66,7 +42,7 @@ beforeEach(() => {
Object.defineProperty(process.stdout, "isTTY", { value: false, configurable: true });
});
afterEach(() => {
afterEach(async () => {
console.log = snapshot.log;
console.info = snapshot.info;
console.warn = snapshot.warn;
@@ -74,6 +50,8 @@ afterEach(() => {
console.debug = snapshot.debug;
console.trace = snapshot.trace;
Object.defineProperty(process.stdout, "isTTY", { value: originalIsTty, configurable: true });
const logging = await import("../logging.js");
logging.setConsoleConfigLoaderForTests();
vi.restoreAllMocks();
});
@@ -81,6 +59,14 @@ async function loadLogging() {
const logging = await import("../logging.js");
const state = await import("./state.js");
state.loggingState.cachedConsoleSettings = null;
logging.setConsoleConfigLoaderForTests(() => {
loadConfigCalls += 1;
if (loadConfigCalls > 5) {
return {};
}
console.error("config load failed");
return {};
});
return { logging, state };
}

View File

@@ -16,6 +16,22 @@ type ConsoleSettings = {
export type ConsoleLoggerSettings = ConsoleSettings;
const requireConfig = createRequire(import.meta.url);
type ConsoleConfigLoader = () => OpenClawConfig["logging"] | undefined;
const loadConfigFallbackDefault: ConsoleConfigLoader = () => {
try {
const loaded = requireConfig("../config/config.js") as {
loadConfig?: () => OpenClawConfig;
};
return loaded.loadConfig?.().logging;
} catch {
return undefined;
}
};
let loadConfigFallback: ConsoleConfigLoader = loadConfigFallbackDefault;
export function setConsoleConfigLoaderForTests(loader?: ConsoleConfigLoader): void {
loadConfigFallback = loader ?? loadConfigFallbackDefault;
}
function normalizeConsoleLevel(level?: string): LogLevel {
if (isVerbose()) {
@@ -43,12 +59,7 @@ function resolveConsoleSettings(): ConsoleSettings {
} else {
loggingState.resolvingConsoleSettings = true;
try {
const loaded = requireConfig("../config/config.js") as {
loadConfig?: () => OpenClawConfig;
};
cfg = loaded.loadConfig?.().logging;
} catch {
cfg = undefined;
cfg = loadConfigFallback();
} finally {
loggingState.resolvingConsoleSettings = false;
}