test: share cli help version assertions

This commit is contained in:
Peter Steinberger
2026-03-13 23:39:17 +00:00
parent 0a50eb0343
commit 403e35e6b0

View File

@@ -90,6 +90,23 @@ describe("configureProgramHelp", () => {
}
}
function expectVersionExit(params: { expectedVersion: string }) {
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
const exitSpy = vi.spyOn(process, "exit").mockImplementation(((code?: number) => {
throw new Error(`exit:${code ?? ""}`);
}) as typeof process.exit);
try {
const program = makeProgramWithCommands();
expect(() => configureProgramHelp(program, testProgramContext)).toThrow("exit:0");
expect(logSpy).toHaveBeenCalledWith(params.expectedVersion);
expect(exitSpy).toHaveBeenCalledWith(0);
} finally {
logSpy.mockRestore();
exitSpy.mockRestore();
}
}
it("adds root help hint and marks commands with subcommands", () => {
process.argv = ["node", "openclaw", "--help"];
const program = makeProgramWithCommands();
@@ -115,35 +132,12 @@ describe("configureProgramHelp", () => {
it("prints version and exits immediately when version flags are present", () => {
process.argv = ["node", "openclaw", "--version"];
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
const exitSpy = vi.spyOn(process, "exit").mockImplementation(((code?: number) => {
throw new Error(`exit:${code ?? ""}`);
}) as typeof process.exit);
const program = makeProgramWithCommands();
expect(() => configureProgramHelp(program, testProgramContext)).toThrow("exit:0");
expect(logSpy).toHaveBeenCalledWith("OpenClaw 9.9.9-test (abc1234)");
expect(exitSpy).toHaveBeenCalledWith(0);
logSpy.mockRestore();
exitSpy.mockRestore();
expectVersionExit({ expectedVersion: "OpenClaw 9.9.9-test (abc1234)" });
});
it("prints version and exits immediately without commit metadata", () => {
process.argv = ["node", "openclaw", "--version"];
resolveCommitHashMock.mockReturnValue(null);
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
const exitSpy = vi.spyOn(process, "exit").mockImplementation(((code?: number) => {
throw new Error(`exit:${code ?? ""}`);
}) as typeof process.exit);
const program = makeProgramWithCommands();
expect(() => configureProgramHelp(program, testProgramContext)).toThrow("exit:0");
expect(logSpy).toHaveBeenCalledWith("OpenClaw 9.9.9-test");
expect(exitSpy).toHaveBeenCalledWith(0);
logSpy.mockRestore();
exitSpy.mockRestore();
expectVersionExit({ expectedVersion: "OpenClaw 9.9.9-test" });
});
});