Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(console-prefix): the redirect reset puts console.log/info/debug back
garrytan253d956e927d
diff --git a/src/core/console-prefix.ts b/src/core/console-prefix.tsindex ff95cdc0c2..ef4985a282 100644--- a/src/core/console-prefix.ts+++ b/src/core/console-prefix.ts@@ -61,6 +61,7 @@ const __prefixStore = new AsyncLocalStorage<string>();// onto the JSON-RPC channel. One direction only — never unset at runtime// (the serve process is stdio-MCP for its whole lifetime).let __stdoutLoggingRedirected = false;+let __consoleBeforeRedirect: Pick<Console, 'log' | 'info' | 'debug'> | null = null;// CLI `--json` mode: stdout is reserved for the JSON envelope (and any JSON// status lines). Scoped, not process-wide: rebinding console.log here would@@ -112,6 +113,8 @@ export function redirectStdoutLoggingToStderr(): void {if (__stdoutLoggingRedirected) return;__stdoutLoggingRedirected = true;// eslint-disable-next-line no-console+ __consoleBeforeRedirect = { log: console.log, info: console.info, debug: console.debug };+ // eslint-disable-next-line no-consoleconst toStderr = (...args: unknown[]): void => console.error(...args);// eslint-disable-next-line no-consoleconsole.log = toStderr;@@ -129,11 +132,16 @@ export function isStdoutLoggingRedirected(): boolean {}/**- * Test-only reset. Does NOT restore the original console bindings (tests- * that need those should snapshot them before calling the redirect).+ * Test-only reset: clears the flag and puts back the console bindings the+ * redirect replaced. Bun runs many test files in one process, so a reset that+ * left console.log bound to stderr sent every later file's stdout logging to+ * stderr.*/export function _resetStdoutRedirectForTests(): void {__stdoutLoggingRedirected = false;+ if (!__consoleBeforeRedirect) return;+ Object.assign(console, __consoleBeforeRedirect);+ __consoleBeforeRedirect = null;}/**diff --git a/test/facts-drain-scheduler.test.ts b/test/facts-drain-scheduler.test.tsindex a99aa5aec5..fb1a986594 100644--- a/test/facts-drain-scheduler.test.ts+++ b/test/facts-drain-scheduler.test.ts@@ -7,7 +7,7 @@* written to stdout. A real PGLite + stub chat case covers the acceptance* path end to end (start serve, write after startup, restart with a backlog).*/-import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'bun:test';+import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from 'bun:test';import { EventEmitter } from 'node:events';import { mkdtempSync, rmSync } from 'node:fs';import { tmpdir } from 'node:os';@@ -27,6 +27,8 @@ import type { ChatResult } from '../src/core/ai/gateway.ts';import { configureGateway, resetGateway, __setChatTransportForTests } from '../src/core/ai/gateway.ts';const settle = () => new Promise<void>(r => setTimeout(r, 0));+// runServe's stdio path redirects console to stderr for the process; put it back after every test.+afterEach(() => _resetStdoutRedirectForTests());const fakeResult = (owner: FactsDrainRunResult['owner']) => ({ owner, outcome: 'drained' } as FactsDrainRunResult);function intervalStub() {diff --git a/test/stdio-stderr-redirect.serial.test.ts b/test/stdio-stderr-redirect.serial.test.tsindex 485105c046..b449f0014e 100644--- a/test/stdio-stderr-redirect.serial.test.ts+++ b/test/stdio-stderr-redirect.serial.test.ts@@ -109,6 +109,17 @@ describe('redirectStdoutLoggingToStderr', () => {expect(errCalls).toEqual([['bare progress line']]);});+ test('the test reset puts back the console bindings the redirect replaced', () => {+ const log = (..._args: unknown[]) => {};+ console.log = log;+ redirectStdoutLoggingToStderr();+ expect(console.log).not.toBe(log);+ _resetStdoutRedirectForTests();+ expect(console.log).toBe(log);+ expect(console.info).toBe(realInfo);+ expect(console.debug).toBe(realDebug);+ });+test('idempotent: second call does not re-wrap (no double stderr emission)', () => {const errCalls: unknown[][] = [];console.error = (...args: unknown[]) => {