mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 14:58:26 +00:00
refactor(cli): share styled select prompt helper
This commit is contained in:
50
src/terminal/prompt-select-styled.test.ts
Normal file
50
src/terminal/prompt-select-styled.test.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { selectMock, stylePromptMessageMock, stylePromptHintMock } = vi.hoisted(() => ({
|
||||
selectMock: vi.fn(),
|
||||
stylePromptMessageMock: vi.fn((value: string) => `msg:${value}`),
|
||||
stylePromptHintMock: vi.fn((value: string) => `hint:${value}`),
|
||||
}));
|
||||
|
||||
vi.mock("@clack/prompts", () => ({
|
||||
select: selectMock,
|
||||
}));
|
||||
|
||||
vi.mock("./prompt-style.js", () => ({
|
||||
stylePromptMessage: stylePromptMessageMock,
|
||||
stylePromptHint: stylePromptHintMock,
|
||||
}));
|
||||
|
||||
import { selectStyled } from "./prompt-select-styled.js";
|
||||
|
||||
describe("selectStyled", () => {
|
||||
beforeEach(() => {
|
||||
selectMock.mockReset();
|
||||
stylePromptMessageMock.mockClear();
|
||||
stylePromptHintMock.mockClear();
|
||||
});
|
||||
|
||||
it("styles message and option hints before delegating to clack select", () => {
|
||||
const expected = Symbol("selected");
|
||||
selectMock.mockReturnValue(expected);
|
||||
|
||||
const result = selectStyled({
|
||||
message: "Pick channel",
|
||||
options: [
|
||||
{ value: "stable", label: "Stable", hint: "Tagged releases" },
|
||||
{ value: "dev", label: "Dev" },
|
||||
],
|
||||
});
|
||||
|
||||
expect(result).toBe(expected);
|
||||
expect(stylePromptMessageMock).toHaveBeenCalledWith("Pick channel");
|
||||
expect(stylePromptHintMock).toHaveBeenCalledWith("Tagged releases");
|
||||
expect(selectMock).toHaveBeenCalledWith({
|
||||
message: "msg:Pick channel",
|
||||
options: [
|
||||
{ value: "stable", label: "Stable", hint: "hint:Tagged releases" },
|
||||
{ value: "dev", label: "Dev" },
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
12
src/terminal/prompt-select-styled.ts
Normal file
12
src/terminal/prompt-select-styled.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { select } from "@clack/prompts";
|
||||
import { stylePromptHint, stylePromptMessage } from "./prompt-style.js";
|
||||
|
||||
export function selectStyled<T>(params: Parameters<typeof select<T>>[0]) {
|
||||
return select({
|
||||
...params,
|
||||
message: stylePromptMessage(params.message),
|
||||
options: params.options.map((opt) =>
|
||||
opt.hint === undefined ? opt : { ...opt, hint: stylePromptHint(opt.hint) },
|
||||
),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user