Skip to content

Commit 3a09ca3

Browse files
authored
refactor(cli): share status report rendering context (#134946)
1 parent e8c3fbc commit 3a09ca3

7 files changed

Lines changed: 114 additions & 96 deletions

File tree

‎src/commands/status-all/report-lines.test.ts‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { describe, expect, it, vi } from "vitest";
33
import type { ProgressReporter } from "../../cli/progress.js";
44
import { buildStatusAllReportLines } from "./report-lines.js";
55

6-
const diagnosisSpy = vi.hoisted(() => vi.fn(async () => {}));
6+
const diagnosisSpy = vi.hoisted(() =>
7+
vi.fn(async ({ lines }: { lines: string[]; secretDiagnostics: string[] }) => {
8+
await Promise.resolve();
9+
lines.push("diagnosis body", "");
10+
}),
11+
);
712

813
vi.mock("./diagnosis.js", () => ({
914
appendStatusAllDiagnosis: diagnosisSpy,
@@ -34,7 +39,14 @@ describe("buildStatusAllReportLines", () => {
3439
detail: "connected",
3540
},
3641
],
37-
details: [],
42+
details: [
43+
{
44+
title: "Discord accounts",
45+
columns: ["Account", "Status", "Notes"],
46+
rows: [{ Account: "default", Status: "OK", Notes: "ready" }],
47+
},
48+
{ title: "Empty accounts", columns: ["Account", "Status"], rows: [] },
49+
],
3850
},
3951
channelIssues: [{ channel: "discord", message: `${"x".repeat(89)}🚀tail` }],
4052
agentStatus: {
@@ -96,10 +108,22 @@ describe("buildStatusAllReportLines", () => {
96108
output.indexOf("OpenClaw status --all"),
97109
);
98110
expect(output).not.toContain(String.fromCharCode(0xd83d));
111+
expect(
112+
lines.filter((line) =>
113+
/^(Overview|Channels|Discord accounts|Empty accounts|Agents|Diagnosis \(read-only\))$/.test(
114+
line,
115+
),
116+
),
117+
).toEqual([
118+
"Overview",
119+
"Channels",
120+
"Discord accounts",
121+
"Empty accounts",
122+
"Agents",
123+
"Diagnosis (read-only)",
124+
]);
125+
expect(lines.slice(-4)).toEqual(["", "Diagnosis (read-only)", "diagnosis body", ""]);
99126
expect(diagnosisSpy).toHaveBeenCalledOnce();
100-
const [diagnosisOptions] = diagnosisSpy.mock.calls[0] as unknown as [
101-
{ secretDiagnostics?: unknown[] },
102-
];
103-
expect(diagnosisOptions?.secretDiagnostics).toEqual([]);
127+
expect(diagnosisSpy).toHaveBeenCalledWith(expect.objectContaining({ secretDiagnostics: [] }));
104128
});
105129
});

‎src/commands/status-all/report-lines.ts‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,18 @@ export async function buildStatusAllReportLines(params: {
8585
appendStatusReportSections({
8686
lines,
8787
heading,
88+
width: tableWidth,
89+
renderTable,
8890
sections: [
8991
{
9092
kind: "table",
9193
title: "Overview",
92-
width: tableWidth,
93-
renderTable,
9494
columns: [...statusOverviewTableColumns],
9595
rows: params.overviewRows,
9696
},
9797
{
9898
kind: "table",
9999
title: "Channels",
100-
width: tableWidth,
101-
renderTable,
102100
// The status-all report has more horizontal space than compact status output.
103101
columns: statusChannelsTableColumns.map((column) =>
104102
column.key === "Detail" ? Object.assign({}, column, { minWidth: 28 }) : column,
@@ -115,16 +113,12 @@ export async function buildStatusAllReportLines(params: {
115113
},
116114
...buildStatusChannelDetailSections({
117115
details: params.channels.details,
118-
width: tableWidth,
119-
renderTable,
120116
ok,
121117
warn,
122118
}),
123119
{
124120
kind: "table",
125121
title: "Agents",
126-
width: tableWidth,
127-
renderTable,
128122
columns: [...statusAgentsTableColumns],
129123
rows: buildStatusAgentTableRows({
130124
agentStatus: params.agentStatus,

‎src/commands/status-all/report-tables.test.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ describe("status-all report tables", () => {
5252
});
5353

5454
it("builds colored detail table sections", () => {
55-
const renderTable = ({ rows }: { rows: unknown[] }) => `rows:${rows.length}`;
5655
const [section] = buildStatusChannelDetailSections({
5756
details: [
5857
{
@@ -61,17 +60,13 @@ describe("status-all report tables", () => {
6160
rows: [{ Channel: "quietchat", Status: "WARN", Notes: "setup" }],
6261
},
6362
],
64-
width: 120,
65-
renderTable,
6663
ok: (value) => `ok(${value})`,
6764
warn: (value) => `warn(${value})`,
6865
});
6966

7067
expect(section).toEqual({
7168
kind: "table",
7269
title: "Channel detail",
73-
width: 120,
74-
renderTable,
7570
columns: [
7671
{ key: "Channel", header: "Channel", flex: false, minWidth: 10 },
7772
{ key: "Status", header: "Status", flex: false, minWidth: 10 },

‎src/commands/status-all/report-tables.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Table row helpers for status report sections.
22
// These functions keep terminal styling decisions out of the scan/data layer.
33

4-
import type { RenderTableOptions } from "../../../packages/terminal-core/src/table.js";
54
import { formatTimeAgo } from "./format.js";
65
import type { StatusReportSection } from "./text-report.js";
76

@@ -58,16 +57,12 @@ export function buildStatusAgentTableRows(params: {
5857
/** Converts per-channel account detail rows into renderable table sections. */
5958
export function buildStatusChannelDetailSections(params: {
6059
details: ChannelDetailLike[];
61-
width: number;
62-
renderTable: (input: RenderTableOptions) => string;
6360
ok: (text: string) => string;
6461
warn: (text: string) => string;
6562
}): StatusReportSection[] {
6663
return params.details.map((detail) => ({
6764
kind: "table" as const,
6865
title: detail.title,
69-
width: params.width,
70-
renderTable: params.renderTable,
7166
columns: detail.columns.map((column) => ({
7267
key: column,
7368
header: column,

‎src/commands/status-all/text-report.test.ts‎

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ describe("appendStatusReportSections", () => {
99
appendStatusReportSections({
1010
lines,
1111
heading: (text) => `# ${text}`,
12+
width: 120,
13+
renderTable: ({ width, rows }) => ` table:${width}:${rows.length} \n\t`,
1214
sections: [
1315
{
1416
kind: "raw",
@@ -22,11 +24,9 @@ describe("appendStatusReportSections", () => {
2224
{
2325
kind: "table",
2426
title: "Health",
25-
width: 120,
26-
renderTable: ({ rows }) => `table:${rows.length}`,
2727
columns: [{ key: "Item", header: "Item" }],
2828
rows: [{ Item: "Gateway" }],
29-
trailer: "trailer",
29+
trailer: "trailer ",
3030
},
3131
{
3232
kind: "lines",
@@ -46,8 +46,62 @@ describe("appendStatusReportSections", () => {
4646
"overview body",
4747
"",
4848
"# Health",
49-
"table:1",
50-
"trailer",
49+
" table:120:1",
50+
"trailer ",
5151
]);
5252
});
53+
54+
it.each([{ initial: [] }, { initial: ["# Start"] }, { initial: ["# Start", ""] }])(
55+
"keeps empty sections unless explicitly skipped after $initial",
56+
({ initial }) => {
57+
const lines = [...initial];
58+
appendStatusReportSections({
59+
lines,
60+
heading: (text) => `# ${text}`,
61+
width: 80,
62+
renderTable: ({ width, rows }) => `table:${width}:${rows.length}\n`,
63+
sections: [
64+
{ kind: "raw", body: [], skipIfEmpty: true },
65+
{ kind: "lines", title: "Skipped", body: [], skipIfEmpty: true },
66+
{
67+
kind: "table",
68+
title: "Skipped table",
69+
columns: [],
70+
rows: [],
71+
skipIfEmpty: true,
72+
trailer: "skipped trailer",
73+
},
74+
{ kind: "lines", title: "Empty lines", body: [] },
75+
{ kind: "raw", body: [] },
76+
{
77+
kind: "table",
78+
title: "Empty table",
79+
columns: [],
80+
rows: [],
81+
trailer: "empty trailer",
82+
},
83+
{
84+
kind: "table",
85+
title: "More rows",
86+
columns: [{ key: "Item", header: "Item" }],
87+
rows: [{ Item: "Gateway" }],
88+
skipIfEmpty: true,
89+
trailer: "",
90+
},
91+
],
92+
});
93+
expect(lines).toEqual([
94+
...initial,
95+
...(initial.length ? [""] : []),
96+
"# Empty lines",
97+
"",
98+
"# Empty table",
99+
"table:80:0",
100+
"empty trailer",
101+
"",
102+
"# More rows",
103+
"table:80:1",
104+
]);
105+
},
106+
);
53107
});

‎src/commands/status-all/text-report.ts‎

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ export type StatusReportSection =
1616
| {
1717
kind: "table";
1818
title: string;
19-
width: number;
20-
renderTable: TableRenderer;
2119
columns: readonly TableColumn[];
2220
rows: Array<Record<string, string>>;
2321
trailer?: string | null;
@@ -41,75 +39,41 @@ export function appendStatusSectionHeading(params: {
4139
params.lines.push(params.heading(params.title));
4240
}
4341

44-
function appendStatusLinesSection(params: {
45-
lines: string[];
46-
heading: HeadingFn;
47-
title: string;
48-
body: string[];
49-
}) {
50-
appendStatusSectionHeading(params);
51-
params.lines.push(...params.body);
52-
}
53-
54-
function appendStatusTableSection<Row extends Record<string, string>>(params: {
55-
lines: string[];
56-
heading: HeadingFn;
57-
title: string;
58-
width: number;
59-
renderTable: (input: { width: number; columns: TableColumn[]; rows: Row[] }) => string;
60-
columns: readonly TableColumn[];
61-
rows: Row[];
62-
}) {
63-
appendStatusSectionHeading(params);
64-
params.lines.push(
65-
params
66-
.renderTable({
67-
width: params.width,
68-
columns: [...params.columns],
69-
rows: params.rows,
70-
})
71-
.trimEnd(),
72-
);
73-
}
74-
75-
/** Appends all non-empty report sections in display order. */
42+
/** Appends report sections in display order, honoring explicit empty-section skipping. */
7643
export function appendStatusReportSections(params: {
7744
lines: string[];
7845
heading: HeadingFn;
46+
width: number;
47+
renderTable: TableRenderer;
7948
sections: StatusReportSection[];
8049
}) {
8150
for (const section of params.sections) {
82-
if (section.kind === "raw") {
83-
if (section.skipIfEmpty && section.body.length === 0) {
84-
continue;
85-
}
86-
params.lines.push(...section.body);
87-
continue;
88-
}
89-
if (section.kind === "lines") {
90-
if (section.skipIfEmpty && section.body.length === 0) {
91-
continue;
92-
}
93-
appendStatusLinesSection({
94-
lines: params.lines,
95-
heading: params.heading,
96-
title: section.title,
97-
body: section.body,
98-
});
51+
const content = section.kind === "table" ? section.rows : section.body;
52+
if (section.skipIfEmpty && content.length === 0) {
9953
continue;
10054
}
101-
if (section.skipIfEmpty && section.rows.length === 0) {
55+
if (section.kind === "raw") {
56+
params.lines.push(...section.body);
10257
continue;
10358
}
104-
appendStatusTableSection({
59+
appendStatusSectionHeading({
10560
lines: params.lines,
10661
heading: params.heading,
10762
title: section.title,
108-
width: section.width,
109-
renderTable: section.renderTable,
110-
columns: section.columns,
111-
rows: section.rows,
11263
});
64+
if (section.kind === "lines") {
65+
params.lines.push(...section.body);
66+
continue;
67+
}
68+
params.lines.push(
69+
params
70+
.renderTable({
71+
width: params.width,
72+
columns: [...section.columns],
73+
rows: section.rows,
74+
})
75+
.trimEnd(),
76+
);
11377
if (section.trailer) {
11478
params.lines.push(section.trailer);
11579
}

‎src/commands/status.command-report.ts‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ export async function buildStatusCommandReportLines(params: {
3737
appendStatusReportSections({
3838
lines,
3939
heading: params.heading,
40+
width: params.width,
41+
renderTable: params.renderTable,
4042
sections: [
4143
{
4244
kind: "table",
4345
title: "Overview",
44-
width: params.width,
45-
renderTable: params.renderTable,
4646
columns: [...statusOverviewTableColumns],
4747
rows: params.overviewRows,
4848
},
@@ -95,8 +95,6 @@ export async function buildStatusCommandReportLines(params: {
9595
: {
9696
kind: "table",
9797
title: "Channels",
98-
width: params.width,
99-
renderTable: params.renderTable,
10098
columns: [...params.channelsColumns],
10199
rows: params.channelsRows,
102100
},
@@ -109,16 +107,12 @@ export async function buildStatusCommandReportLines(params: {
109107
: {
110108
kind: "table",
111109
title: "Sessions",
112-
width: params.width,
113-
renderTable: params.renderTable,
114110
columns: [...params.sessionsColumns],
115111
rows: params.sessionsRows,
116112
},
117113
{
118114
kind: "table",
119115
title: "System events",
120-
width: params.width,
121-
renderTable: params.renderTable,
122116
columns: [{ key: "Event", header: "Event", flex: true, minWidth: 24 }],
123117
rows: params.systemEventsRows ?? [],
124118
trailer: params.systemEventsTrailer,
@@ -127,8 +121,6 @@ export async function buildStatusCommandReportLines(params: {
127121
{
128122
kind: "table",
129123
title: "Health",
130-
width: params.width,
131-
renderTable: params.renderTable,
132124
columns: [...(params.healthColumns ?? [])],
133125
rows: params.healthRows ?? [],
134126
skipIfEmpty: true,

0 commit comments

Comments
 (0)