mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 04:19:34 +00:00
test: dedupe and optimize test suites
This commit is contained in:
@@ -2,40 +2,40 @@ import { Command } from "commander";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { inheritOptionFromParent } from "./command-options.js";
|
||||
|
||||
function attachRunCommandAndCaptureInheritedToken(command: Command) {
|
||||
let inherited: string | undefined;
|
||||
command
|
||||
.command("run")
|
||||
.option("--token <token>", "Run token")
|
||||
.action((_opts, childCommand) => {
|
||||
inherited = inheritOptionFromParent<string>(childCommand, "token");
|
||||
});
|
||||
return () => inherited;
|
||||
}
|
||||
|
||||
describe("inheritOptionFromParent", () => {
|
||||
it("inherits from grandparent when parent does not define the option", async () => {
|
||||
it.each([
|
||||
{
|
||||
label: "inherits from grandparent when parent does not define the option",
|
||||
parentHasTokenOption: false,
|
||||
argv: ["--token", "root-token", "gateway", "run"],
|
||||
expected: "root-token",
|
||||
},
|
||||
{
|
||||
label: "prefers nearest ancestor value when multiple ancestors set the same option",
|
||||
parentHasTokenOption: true,
|
||||
argv: ["--token", "root-token", "gateway", "--token", "gateway-token", "run"],
|
||||
expected: "gateway-token",
|
||||
},
|
||||
])("$label", async ({ parentHasTokenOption, argv, expected }) => {
|
||||
const program = new Command().option("--token <token>", "Root token");
|
||||
const gateway = program.command("gateway");
|
||||
let inherited: string | undefined;
|
||||
const gateway = parentHasTokenOption
|
||||
? program.command("gateway").option("--token <token>", "Gateway token")
|
||||
: program.command("gateway");
|
||||
const getInherited = attachRunCommandAndCaptureInheritedToken(gateway);
|
||||
|
||||
gateway
|
||||
.command("run")
|
||||
.option("--token <token>", "Run token")
|
||||
.action((_opts, command) => {
|
||||
inherited = inheritOptionFromParent<string>(command, "token");
|
||||
});
|
||||
|
||||
await program.parseAsync(["--token", "root-token", "gateway", "run"], { from: "user" });
|
||||
expect(inherited).toBe("root-token");
|
||||
});
|
||||
|
||||
it("prefers nearest ancestor value when multiple ancestors set the same option", async () => {
|
||||
const program = new Command().option("--token <token>", "Root token");
|
||||
const gateway = program.command("gateway").option("--token <token>", "Gateway token");
|
||||
let inherited: string | undefined;
|
||||
|
||||
gateway
|
||||
.command("run")
|
||||
.option("--token <token>", "Run token")
|
||||
.action((_opts, command) => {
|
||||
inherited = inheritOptionFromParent<string>(command, "token");
|
||||
});
|
||||
|
||||
await program.parseAsync(
|
||||
["--token", "root-token", "gateway", "--token", "gateway-token", "run"],
|
||||
{ from: "user" },
|
||||
);
|
||||
expect(inherited).toBe("gateway-token");
|
||||
await program.parseAsync(argv, { from: "user" });
|
||||
expect(getInherited()).toBe(expected);
|
||||
});
|
||||
|
||||
it("does not inherit when the child option was set explicitly", async () => {
|
||||
@@ -54,18 +54,11 @@ describe("inheritOptionFromParent", () => {
|
||||
const program = new Command().option("--token <token>", "Root token");
|
||||
const level1 = program.command("level1");
|
||||
const level2 = level1.command("level2");
|
||||
let inherited: string | undefined;
|
||||
|
||||
level2
|
||||
.command("run")
|
||||
.option("--token <token>", "Run token")
|
||||
.action((_opts, command) => {
|
||||
inherited = inheritOptionFromParent<string>(command, "token");
|
||||
});
|
||||
const getInherited = attachRunCommandAndCaptureInheritedToken(level2);
|
||||
|
||||
await program.parseAsync(["--token", "root-token", "level1", "level2", "run"], {
|
||||
from: "user",
|
||||
});
|
||||
expect(inherited).toBeUndefined();
|
||||
expect(getInherited()).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user