File size: 7,133 Bytes
76289e7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | // Logging config tests cover config file loading and defaults.
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { withEnv } from "../test-utils/env.js";
import { readLoggingConfig } from "./config.js";
import { applyLoggingConfig, resetLogger } from "./logger.js";
const originalArgv = process.argv;
let tempDirs: string[] = [];
function writeConfig(source: string): string {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-logging-config-"));
tempDirs.push(dir);
const configPath = path.join(dir, "openclaw.json");
fs.writeFileSync(configPath, source);
return configPath;
}
describe("readLoggingConfig", () => {
afterEach(() => {
resetLogger();
process.argv = originalArgv;
for (const dir of tempDirs) {
fs.rmSync(dir, { force: true, recursive: true });
}
tempDirs = [];
});
it("returns the applied runtime snapshot without bootstrap filesystem work", () => {
const existsSync = vi.spyOn(fs, "existsSync");
applyLoggingConfig({ level: "debug", consoleStyle: "json" });
expect(readLoggingConfig()).toEqual({ level: "debug", consoleStyle: "json" });
expect(existsSync).not.toHaveBeenCalled();
});
it("reads logging style without a mutating config load for config schema", () => {
process.argv = ["node", "openclaw", "config", "schema"];
const configPath = writeConfig(`{ logging: { consoleStyle: "json" } }`);
withEnv({ OPENCLAW_CONFIG_PATH: configPath }, () => {
expect(readLoggingConfig()).toStrictEqual({ consoleStyle: "json" });
});
});
it("reads logging config directly from the active config path", () => {
const configPath = writeConfig(`{
logging: {
level: "debug",
file: "/tmp/openclaw-custom.log",
maxFileBytes: 1234,
},
}`);
withEnv({ OPENCLAW_CONFIG_PATH: configPath }, () => {
expect(readLoggingConfig()).toStrictEqual({
level: "debug",
file: "/tmp/openclaw-custom.log",
maxFileBytes: 1234,
});
});
});
it("supports JSON5 comments and trailing commas", () => {
const configPath = writeConfig(`{
// users commonly keep comments in openclaw.json
logging: {
consoleLevel: "warn",
},
}`);
withEnv({ OPENCLAW_CONFIG_PATH: configPath }, () => {
expect(readLoggingConfig()).toStrictEqual({
consoleLevel: "warn",
});
});
});
it("resolves nested includes and environment substitutions for logging style", () => {
const configPath = writeConfig(`{ logging: { $include: "./logging.json5" } }`);
fs.writeFileSync(
path.join(path.dirname(configPath), "logging.json5"),
`{ consoleStyle: "\${OPENCLAW_TEST_CONSOLE_STYLE}", file: "\${MISSING_LOG_FILE}" }`,
);
withEnv(
{
OPENCLAW_CONFIG_PATH: configPath,
OPENCLAW_TEST_CONSOLE_STYLE: "json",
MISSING_LOG_FILE: undefined,
},
() => {
expect(readLoggingConfig()).toStrictEqual({ consoleStyle: "json" });
},
);
});
it("preserves direct logging style when unrelated config resolution fails", () => {
const configPath = writeConfig(`{
logging: { consoleStyle: "json", file: "\${MISSING_LOG_FILE}" },
plugins: { $include: "./missing-plugins.json5" },
models: { providers: { demo: { apiKey: "\${MISSING_DEMO_KEY}" } } },
}`);
withEnv(
{
OPENCLAW_CONFIG_PATH: configPath,
MISSING_DEMO_KEY: undefined,
MISSING_LOG_FILE: undefined,
},
() => {
expect(readLoggingConfig()).toStrictEqual({ consoleStyle: "json" });
},
);
});
it("resolves root-included logging when an unrelated sibling include fails", () => {
const configPath = writeConfig(`{
$include: "./base.json5",
plugins: { $include: "./missing-plugins.json5" },
}`);
fs.writeFileSync(
path.join(path.dirname(configPath), "base.json5"),
`{ logging: { consoleStyle: "json", file: "\${MISSING_LOG_FILE}" } }`,
);
withEnv(
{
OPENCLAW_CONFIG_PATH: configPath,
MISSING_LOG_FILE: undefined,
},
() => {
expect(readLoggingConfig()).toStrictEqual({ consoleStyle: "json" });
},
);
});
it("does not cache a partial style while environment-backed fields are unresolved", () => {
const configPath = writeConfig(`{
logging: {
consoleStyle: "json",
file: "\${OPENCLAW_TEST_LOG_FILE}",
level: "debug",
},
}`);
withEnv(
{
OPENCLAW_CONFIG_PATH: configPath,
OPENCLAW_TEST_LOG_FILE: undefined,
},
() => {
expect(readLoggingConfig()).toStrictEqual({ consoleStyle: "json" });
},
);
withEnv(
{
OPENCLAW_CONFIG_PATH: configPath,
OPENCLAW_TEST_LOG_FILE: "/tmp/openclaw-env-backed.log",
},
() => {
expect(readLoggingConfig()).toStrictEqual({
consoleStyle: "json",
file: "/tmp/openclaw-env-backed.log",
level: "debug",
});
},
);
});
it("preserves custom redaction patterns while another logging field is unresolved", () => {
const configPath = writeConfig(`{
logging: {
consoleStyle: "json",
redactPatterns: ["/custom-only-secret/g"],
file: "\${MISSING_LOG_FILE}",
},
}`);
withEnv(
{
OPENCLAW_CONFIG_PATH: configPath,
MISSING_LOG_FILE: undefined,
},
() => {
expect(readLoggingConfig()).toStrictEqual({
consoleStyle: "json",
redactPatterns: ["/custom-only-secret/g"],
});
},
);
});
it("returns undefined for missing or malformed config files", () => {
withEnv(
{ OPENCLAW_CONFIG_PATH: path.join(os.tmpdir(), "openclaw-missing-config.json") },
() => {
expect(readLoggingConfig()).toBeUndefined();
},
);
const configPath = writeConfig(`{ logging: `);
withEnv({ OPENCLAW_CONFIG_PATH: configPath }, () => {
expect(readLoggingConfig()).toBeUndefined();
});
});
it("caches a missing config until the path selector changes", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-logging-config-missing-"));
tempDirs.push(dir);
const firstPath = path.join(dir, "missing-first.json");
const secondPath = path.join(dir, "missing-second.json");
const existsSync = vi.spyOn(fs, "existsSync");
try {
withEnv({ OPENCLAW_CONFIG_PATH: firstPath }, () => {
expect(readLoggingConfig()).toBeUndefined();
expect(readLoggingConfig()).toBeUndefined();
});
withEnv({ OPENCLAW_CONFIG_PATH: secondPath }, () => {
expect(readLoggingConfig()).toBeUndefined();
});
const checksFor = (configPath: string) =>
existsSync.mock.calls.filter(([candidate]) => candidate === configPath).length;
expect(checksFor(firstPath)).toBe(1);
expect(checksFor(secondPath)).toBe(1);
} finally {
existsSync.mockRestore();
}
});
});
|