

























@@ -0,0 +1,48 @@
1+import { afterEach, beforeEach, describe, expect, it } from "vitest";
2+import { loggingState } from "../logging/state.js";
3+import { hasJsonOutputFlag, withConsoleLogsRoutedToStderrForJson } from "./json-output-mode.js";
4+5+describe("json output mode", () => {
6+const originalForceStderr = loggingState.forceConsoleToStderr;
7+8+beforeEach(() => {
9+loggingState.forceConsoleToStderr = false;
10+});
11+12+afterEach(() => {
13+loggingState.forceConsoleToStderr = originalForceStderr;
14+});
15+16+it("detects json output flags before argv terminators", () => {
17+expect(hasJsonOutputFlag(["node", "openclaw", "nodes", "list", "--json"])).toBe(true);
18+expect(hasJsonOutputFlag(["node", "openclaw", "nodes", "list", "--json=true"])).toBe(true);
19+expect(hasJsonOutputFlag(["node", "openclaw", "nodes", "--", "--json"])).toBe(false);
20+});
21+22+it("temporarily routes console logs to stderr while json output is being prepared", async () => {
23+const snapshots: boolean[] = [];
24+25+await withConsoleLogsRoutedToStderrForJson(
26+["node", "openclaw", "nodes", "list", "--json"],
27+async () => {
28+snapshots.push(loggingState.forceConsoleToStderr);
29+},
30+);
31+32+expect(snapshots).toEqual([true]);
33+expect(loggingState.forceConsoleToStderr).toBe(false);
34+});
35+36+it("leaves existing stderr routing enabled after json output preparation", async () => {
37+loggingState.forceConsoleToStderr = true;
38+39+await withConsoleLogsRoutedToStderrForJson(
40+["node", "openclaw", "nodes", "list", "--json"],
41+async () => {
42+expect(loggingState.forceConsoleToStderr).toBe(true);
43+},
44+);
45+46+expect(loggingState.forceConsoleToStderr).toBe(true);
47+});
48+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。