Files
openclaw/src/cli/json-output-mode.test.ts
Gio Della-Libera ee9813f478 fix(cli): keep nodes json stdout clean (#84423)
Co-authored-by: Gio Della-Libera <giodl@microsoft.com>
2026-05-21 18:05:11 -07:00

49 lines
1.6 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { loggingState } from "../logging/state.js";
import { hasJsonOutputFlag, withConsoleLogsRoutedToStderrForJson } from "./json-output-mode.js";
describe("json output mode", () => {
const originalForceStderr = loggingState.forceConsoleToStderr;
beforeEach(() => {
loggingState.forceConsoleToStderr = false;
});
afterEach(() => {
loggingState.forceConsoleToStderr = originalForceStderr;
});
it("detects json output flags before argv terminators", () => {
expect(hasJsonOutputFlag(["node", "openclaw", "nodes", "list", "--json"])).toBe(true);
expect(hasJsonOutputFlag(["node", "openclaw", "nodes", "list", "--json=true"])).toBe(true);
expect(hasJsonOutputFlag(["node", "openclaw", "nodes", "--", "--json"])).toBe(false);
});
it("temporarily routes console logs to stderr while json output is being prepared", async () => {
const snapshots: boolean[] = [];
await withConsoleLogsRoutedToStderrForJson(
["node", "openclaw", "nodes", "list", "--json"],
async () => {
snapshots.push(loggingState.forceConsoleToStderr);
},
);
expect(snapshots).toEqual([true]);
expect(loggingState.forceConsoleToStderr).toBe(false);
});
it("leaves existing stderr routing enabled after json output preparation", async () => {
loggingState.forceConsoleToStderr = true;
await withConsoleLogsRoutedToStderrForJson(
["node", "openclaw", "nodes", "list", "--json"],
async () => {
expect(loggingState.forceConsoleToStderr).toBe(true);
},
);
expect(loggingState.forceConsoleToStderr).toBe(true);
});
});