mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 03:31:35 +00:00
test: dedupe and optimize test suites
This commit is contained in:
@@ -13,40 +13,106 @@ import {
|
||||
} from "./argv.js";
|
||||
|
||||
describe("argv helpers", () => {
|
||||
it("detects help/version flags", () => {
|
||||
expect(hasHelpOrVersion(["node", "openclaw", "--help"])).toBe(true);
|
||||
expect(hasHelpOrVersion(["node", "openclaw", "-V"])).toBe(true);
|
||||
expect(hasHelpOrVersion(["node", "openclaw", "status"])).toBe(false);
|
||||
it.each([
|
||||
{
|
||||
name: "help flag",
|
||||
argv: ["node", "openclaw", "--help"],
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "version flag",
|
||||
argv: ["node", "openclaw", "-V"],
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "normal command",
|
||||
argv: ["node", "openclaw", "status"],
|
||||
expected: false,
|
||||
},
|
||||
])("detects help/version flags: $name", ({ argv, expected }) => {
|
||||
expect(hasHelpOrVersion(argv)).toBe(expected);
|
||||
});
|
||||
|
||||
it("extracts command path ignoring flags and terminator", () => {
|
||||
expect(getCommandPath(["node", "openclaw", "status", "--json"], 2)).toEqual(["status"]);
|
||||
expect(getCommandPath(["node", "openclaw", "agents", "list"], 2)).toEqual(["agents", "list"]);
|
||||
expect(getCommandPath(["node", "openclaw", "status", "--", "ignored"], 2)).toEqual(["status"]);
|
||||
it.each([
|
||||
{
|
||||
name: "single command with trailing flag",
|
||||
argv: ["node", "openclaw", "status", "--json"],
|
||||
expected: ["status"],
|
||||
},
|
||||
{
|
||||
name: "two-part command",
|
||||
argv: ["node", "openclaw", "agents", "list"],
|
||||
expected: ["agents", "list"],
|
||||
},
|
||||
{
|
||||
name: "terminator cuts parsing",
|
||||
argv: ["node", "openclaw", "status", "--", "ignored"],
|
||||
expected: ["status"],
|
||||
},
|
||||
])("extracts command path: $name", ({ argv, expected }) => {
|
||||
expect(getCommandPath(argv, 2)).toEqual(expected);
|
||||
});
|
||||
|
||||
it("returns primary command", () => {
|
||||
expect(getPrimaryCommand(["node", "openclaw", "agents", "list"])).toBe("agents");
|
||||
expect(getPrimaryCommand(["node", "openclaw"])).toBeNull();
|
||||
it.each([
|
||||
{
|
||||
name: "returns first command token",
|
||||
argv: ["node", "openclaw", "agents", "list"],
|
||||
expected: "agents",
|
||||
},
|
||||
{
|
||||
name: "returns null when no command exists",
|
||||
argv: ["node", "openclaw"],
|
||||
expected: null,
|
||||
},
|
||||
])("returns primary command: $name", ({ argv, expected }) => {
|
||||
expect(getPrimaryCommand(argv)).toBe(expected);
|
||||
});
|
||||
|
||||
it("parses boolean flags and ignores terminator", () => {
|
||||
expect(hasFlag(["node", "openclaw", "status", "--json"], "--json")).toBe(true);
|
||||
expect(hasFlag(["node", "openclaw", "--", "--json"], "--json")).toBe(false);
|
||||
it.each([
|
||||
{
|
||||
name: "detects flag before terminator",
|
||||
argv: ["node", "openclaw", "status", "--json"],
|
||||
flag: "--json",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "ignores flag after terminator",
|
||||
argv: ["node", "openclaw", "--", "--json"],
|
||||
flag: "--json",
|
||||
expected: false,
|
||||
},
|
||||
])("parses boolean flags: $name", ({ argv, flag, expected }) => {
|
||||
expect(hasFlag(argv, flag)).toBe(expected);
|
||||
});
|
||||
|
||||
it("extracts flag values with equals and missing values", () => {
|
||||
expect(getFlagValue(["node", "openclaw", "status", "--timeout", "5000"], "--timeout")).toBe(
|
||||
"5000",
|
||||
);
|
||||
expect(getFlagValue(["node", "openclaw", "status", "--timeout=2500"], "--timeout")).toBe(
|
||||
"2500",
|
||||
);
|
||||
expect(getFlagValue(["node", "openclaw", "status", "--timeout"], "--timeout")).toBeNull();
|
||||
expect(getFlagValue(["node", "openclaw", "status", "--timeout", "--json"], "--timeout")).toBe(
|
||||
null,
|
||||
);
|
||||
expect(getFlagValue(["node", "openclaw", "--", "--timeout=99"], "--timeout")).toBeUndefined();
|
||||
it.each([
|
||||
{
|
||||
name: "value in next token",
|
||||
argv: ["node", "openclaw", "status", "--timeout", "5000"],
|
||||
expected: "5000",
|
||||
},
|
||||
{
|
||||
name: "value in equals form",
|
||||
argv: ["node", "openclaw", "status", "--timeout=2500"],
|
||||
expected: "2500",
|
||||
},
|
||||
{
|
||||
name: "missing value",
|
||||
argv: ["node", "openclaw", "status", "--timeout"],
|
||||
expected: null,
|
||||
},
|
||||
{
|
||||
name: "next token is another flag",
|
||||
argv: ["node", "openclaw", "status", "--timeout", "--json"],
|
||||
expected: null,
|
||||
},
|
||||
{
|
||||
name: "flag appears after terminator",
|
||||
argv: ["node", "openclaw", "--", "--timeout=99"],
|
||||
expected: undefined,
|
||||
},
|
||||
])("extracts flag values: $name", ({ argv, expected }) => {
|
||||
expect(getFlagValue(argv, "--timeout")).toBe(expected);
|
||||
});
|
||||
|
||||
it("parses verbose flags", () => {
|
||||
@@ -57,79 +123,82 @@ describe("argv helpers", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("parses positive integer flag values", () => {
|
||||
expect(getPositiveIntFlagValue(["node", "openclaw", "status"], "--timeout")).toBeUndefined();
|
||||
expect(
|
||||
getPositiveIntFlagValue(["node", "openclaw", "status", "--timeout"], "--timeout"),
|
||||
).toBeNull();
|
||||
expect(
|
||||
getPositiveIntFlagValue(["node", "openclaw", "status", "--timeout", "5000"], "--timeout"),
|
||||
).toBe(5000);
|
||||
expect(
|
||||
getPositiveIntFlagValue(["node", "openclaw", "status", "--timeout", "nope"], "--timeout"),
|
||||
).toBeUndefined();
|
||||
it.each([
|
||||
{
|
||||
name: "missing flag",
|
||||
argv: ["node", "openclaw", "status"],
|
||||
expected: undefined,
|
||||
},
|
||||
{
|
||||
name: "missing value",
|
||||
argv: ["node", "openclaw", "status", "--timeout"],
|
||||
expected: null,
|
||||
},
|
||||
{
|
||||
name: "valid positive integer",
|
||||
argv: ["node", "openclaw", "status", "--timeout", "5000"],
|
||||
expected: 5000,
|
||||
},
|
||||
{
|
||||
name: "invalid integer",
|
||||
argv: ["node", "openclaw", "status", "--timeout", "nope"],
|
||||
expected: undefined,
|
||||
},
|
||||
])("parses positive integer flag values: $name", ({ argv, expected }) => {
|
||||
expect(getPositiveIntFlagValue(argv, "--timeout")).toBe(expected);
|
||||
});
|
||||
|
||||
it("builds parse argv from raw args", () => {
|
||||
const nodeArgv = buildParseArgv({
|
||||
programName: "openclaw",
|
||||
rawArgs: ["node", "openclaw", "status"],
|
||||
});
|
||||
expect(nodeArgv).toEqual(["node", "openclaw", "status"]);
|
||||
const cases = [
|
||||
{
|
||||
rawArgs: ["node", "openclaw", "status"],
|
||||
expected: ["node", "openclaw", "status"],
|
||||
},
|
||||
{
|
||||
rawArgs: ["node-22", "openclaw", "status"],
|
||||
expected: ["node-22", "openclaw", "status"],
|
||||
},
|
||||
{
|
||||
rawArgs: ["node-22.2.0.exe", "openclaw", "status"],
|
||||
expected: ["node-22.2.0.exe", "openclaw", "status"],
|
||||
},
|
||||
{
|
||||
rawArgs: ["node-22.2", "openclaw", "status"],
|
||||
expected: ["node-22.2", "openclaw", "status"],
|
||||
},
|
||||
{
|
||||
rawArgs: ["node-22.2.exe", "openclaw", "status"],
|
||||
expected: ["node-22.2.exe", "openclaw", "status"],
|
||||
},
|
||||
{
|
||||
rawArgs: ["/usr/bin/node-22.2.0", "openclaw", "status"],
|
||||
expected: ["/usr/bin/node-22.2.0", "openclaw", "status"],
|
||||
},
|
||||
{
|
||||
rawArgs: ["nodejs", "openclaw", "status"],
|
||||
expected: ["nodejs", "openclaw", "status"],
|
||||
},
|
||||
{
|
||||
rawArgs: ["node-dev", "openclaw", "status"],
|
||||
expected: ["node", "openclaw", "node-dev", "openclaw", "status"],
|
||||
},
|
||||
{
|
||||
rawArgs: ["openclaw", "status"],
|
||||
expected: ["node", "openclaw", "status"],
|
||||
},
|
||||
{
|
||||
rawArgs: ["bun", "src/entry.ts", "status"],
|
||||
expected: ["bun", "src/entry.ts", "status"],
|
||||
},
|
||||
] as const;
|
||||
|
||||
const versionedNodeArgv = buildParseArgv({
|
||||
programName: "openclaw",
|
||||
rawArgs: ["node-22", "openclaw", "status"],
|
||||
});
|
||||
expect(versionedNodeArgv).toEqual(["node-22", "openclaw", "status"]);
|
||||
|
||||
const versionedNodeWindowsArgv = buildParseArgv({
|
||||
programName: "openclaw",
|
||||
rawArgs: ["node-22.2.0.exe", "openclaw", "status"],
|
||||
});
|
||||
expect(versionedNodeWindowsArgv).toEqual(["node-22.2.0.exe", "openclaw", "status"]);
|
||||
|
||||
const versionedNodePatchlessArgv = buildParseArgv({
|
||||
programName: "openclaw",
|
||||
rawArgs: ["node-22.2", "openclaw", "status"],
|
||||
});
|
||||
expect(versionedNodePatchlessArgv).toEqual(["node-22.2", "openclaw", "status"]);
|
||||
|
||||
const versionedNodeWindowsPatchlessArgv = buildParseArgv({
|
||||
programName: "openclaw",
|
||||
rawArgs: ["node-22.2.exe", "openclaw", "status"],
|
||||
});
|
||||
expect(versionedNodeWindowsPatchlessArgv).toEqual(["node-22.2.exe", "openclaw", "status"]);
|
||||
|
||||
const versionedNodeWithPathArgv = buildParseArgv({
|
||||
programName: "openclaw",
|
||||
rawArgs: ["/usr/bin/node-22.2.0", "openclaw", "status"],
|
||||
});
|
||||
expect(versionedNodeWithPathArgv).toEqual(["/usr/bin/node-22.2.0", "openclaw", "status"]);
|
||||
|
||||
const nodejsArgv = buildParseArgv({
|
||||
programName: "openclaw",
|
||||
rawArgs: ["nodejs", "openclaw", "status"],
|
||||
});
|
||||
expect(nodejsArgv).toEqual(["nodejs", "openclaw", "status"]);
|
||||
|
||||
const nonVersionedNodeArgv = buildParseArgv({
|
||||
programName: "openclaw",
|
||||
rawArgs: ["node-dev", "openclaw", "status"],
|
||||
});
|
||||
expect(nonVersionedNodeArgv).toEqual(["node", "openclaw", "node-dev", "openclaw", "status"]);
|
||||
|
||||
const directArgv = buildParseArgv({
|
||||
programName: "openclaw",
|
||||
rawArgs: ["openclaw", "status"],
|
||||
});
|
||||
expect(directArgv).toEqual(["node", "openclaw", "status"]);
|
||||
|
||||
const bunArgv = buildParseArgv({
|
||||
programName: "openclaw",
|
||||
rawArgs: ["bun", "src/entry.ts", "status"],
|
||||
});
|
||||
expect(bunArgv).toEqual(["bun", "src/entry.ts", "status"]);
|
||||
for (const testCase of cases) {
|
||||
const parsed = buildParseArgv({
|
||||
programName: "openclaw",
|
||||
rawArgs: [...testCase.rawArgs],
|
||||
});
|
||||
expect(parsed).toEqual([...testCase.expected]);
|
||||
}
|
||||
});
|
||||
|
||||
it("builds parse argv from fallback args", () => {
|
||||
@@ -141,23 +210,36 @@ describe("argv helpers", () => {
|
||||
});
|
||||
|
||||
it("decides when to migrate state", () => {
|
||||
expect(shouldMigrateState(["node", "openclaw", "status"])).toBe(false);
|
||||
expect(shouldMigrateState(["node", "openclaw", "health"])).toBe(false);
|
||||
expect(shouldMigrateState(["node", "openclaw", "sessions"])).toBe(false);
|
||||
expect(shouldMigrateState(["node", "openclaw", "config", "get", "update"])).toBe(false);
|
||||
expect(shouldMigrateState(["node", "openclaw", "config", "unset", "update"])).toBe(false);
|
||||
expect(shouldMigrateState(["node", "openclaw", "models", "list"])).toBe(false);
|
||||
expect(shouldMigrateState(["node", "openclaw", "models", "status"])).toBe(false);
|
||||
expect(shouldMigrateState(["node", "openclaw", "memory", "status"])).toBe(false);
|
||||
expect(shouldMigrateState(["node", "openclaw", "agent", "--message", "hi"])).toBe(false);
|
||||
expect(shouldMigrateState(["node", "openclaw", "agents", "list"])).toBe(true);
|
||||
expect(shouldMigrateState(["node", "openclaw", "message", "send"])).toBe(true);
|
||||
const nonMutatingArgv = [
|
||||
["node", "openclaw", "status"],
|
||||
["node", "openclaw", "health"],
|
||||
["node", "openclaw", "sessions"],
|
||||
["node", "openclaw", "config", "get", "update"],
|
||||
["node", "openclaw", "config", "unset", "update"],
|
||||
["node", "openclaw", "models", "list"],
|
||||
["node", "openclaw", "models", "status"],
|
||||
["node", "openclaw", "memory", "status"],
|
||||
["node", "openclaw", "agent", "--message", "hi"],
|
||||
] as const;
|
||||
const mutatingArgv = [
|
||||
["node", "openclaw", "agents", "list"],
|
||||
["node", "openclaw", "message", "send"],
|
||||
] as const;
|
||||
|
||||
for (const argv of nonMutatingArgv) {
|
||||
expect(shouldMigrateState([...argv])).toBe(false);
|
||||
}
|
||||
for (const argv of mutatingArgv) {
|
||||
expect(shouldMigrateState([...argv])).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("reuses command path for migrate state decisions", () => {
|
||||
expect(shouldMigrateStateFromPath(["status"])).toBe(false);
|
||||
expect(shouldMigrateStateFromPath(["config", "get"])).toBe(false);
|
||||
expect(shouldMigrateStateFromPath(["models", "status"])).toBe(false);
|
||||
expect(shouldMigrateStateFromPath(["agents", "list"])).toBe(true);
|
||||
it.each([
|
||||
{ path: ["status"], expected: false },
|
||||
{ path: ["config", "get"], expected: false },
|
||||
{ path: ["models", "status"], expected: false },
|
||||
{ path: ["agents", "list"], expected: true },
|
||||
])("reuses command path for migrate state decisions: $path", ({ path, expected }) => {
|
||||
expect(shouldMigrateStateFromPath(path)).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user