Skip to content

Commit 1296ab3

Browse files
steipetePeter Steinberger
andauthored
fix(cli): report user profile command outcomes (#117466)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent a566ac2 commit 1296ab3

2 files changed

Lines changed: 49 additions & 8 deletions

File tree

‎src/cli/users-cli.test.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ afterEach(() => {
1111
});
1212

1313
describe("registerUsersCli", () => {
14-
it("routes link-email through the admin gateway method", async () => {
14+
it("reports the authoritative linked profile and uses the admin gateway method", async () => {
15+
const output = vi.spyOn(process.stdout, "write").mockImplementation(() => true);
16+
callGatewayFromCli.mockResolvedValue({
17+
profile: {
18+
id: "p-1\nshadow",
19+
displayName: "Ada\tadmin\u001b[2J",
20+
21+
},
22+
});
1523
const program = new Command().exitOverride();
1624
registerUsersCli(program);
1725

@@ -31,6 +39,37 @@ describe("registerUsersCli", () => {
3139
{ email: "[email protected]", targetProfileId: "p-1" },
3240
{ scopes: ["operator.admin"] },
3341
);
42+
expect(output).toHaveBeenCalledWith(
43+
"p-1\\nshadow\tAda\\tadmin\[email protected]\\[email protected]\n",
44+
);
45+
});
46+
47+
it("reports an empty user profile list", async () => {
48+
const output = vi.spyOn(process.stdout, "write").mockImplementation(() => true);
49+
callGatewayFromCli.mockResolvedValue({ profiles: [] });
50+
const program = new Command().exitOverride();
51+
registerUsersCli(program);
52+
53+
await program.parseAsync(["node", "openclaw", "users", "list"]);
54+
55+
expect(callGatewayFromCli).toHaveBeenCalledWith(
56+
"users.list",
57+
expect.any(Object),
58+
{},
59+
{ scopes: ["operator.read"] },
60+
);
61+
expect(output).toHaveBeenCalledWith("No user profiles found.\n");
62+
});
63+
64+
it("preserves the empty profile list JSON response", async () => {
65+
const output = vi.spyOn(process.stdout, "write").mockImplementation(() => true);
66+
callGatewayFromCli.mockResolvedValue({ profiles: [] });
67+
const program = new Command().exitOverride();
68+
registerUsersCli(program);
69+
70+
await program.parseAsync(["node", "openclaw", "users", "list", "--json"]);
71+
72+
expect(output).toHaveBeenCalledWith('{\n "profiles": []\n}\n');
3473
});
3574

3675
it("prints the link result as JSON when requested", async () => {

‎src/cli/users-cli.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ function addUsersGatewayOptions(command: Command) {
1616
.option("--json", "Output JSON", false);
1717
}
1818

19-
type UsersListResult = {
20-
profiles?: Array<{ id?: string; displayName?: string | null; emails?: string[] }>;
21-
};
19+
type UserProfile = { id?: string; displayName?: string | null; emails?: string[] };
20+
type UsersListResult = { profiles?: UserProfile[]; profile?: UserProfile };
2221

2322
function writeUsersList(result: unknown, json: boolean): void {
2423
if (json) {
2524
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
2625
return;
2726
}
28-
const profiles = (result as UsersListResult).profiles ?? [];
27+
const response = result as UsersListResult;
28+
const profiles = response.profiles ?? (response.profile ? [response.profile] : []);
29+
if (profiles.length === 0) {
30+
process.stdout.write("No user profiles found.\n");
31+
return;
32+
}
2933
for (const profile of profiles) {
3034
process.stdout.write(
3135
`${sanitizeTerminalText(profile.id ?? "")}\t${sanitizeTerminalText(profile.displayName ?? "")}\t${sanitizeTerminalText((profile.emails ?? []).join(","))}\n`,
@@ -65,9 +69,7 @@ export function registerUsersCli(program: Command) {
6569
{ email, targetProfileId: opts.to },
6670
{ scopes: ["operator.admin"] },
6771
);
68-
if (opts.json) {
69-
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
70-
}
72+
writeUsersList(result, opts.json === true);
7173
}),
7274
);
7375

0 commit comments

Comments
 (0)