test(perf): dedupe setup in cli/security script suites

This commit is contained in:
Peter Steinberger
2026-03-02 10:53:21 +00:00
parent 4a8ada662e
commit 96ef6ea3cf
3 changed files with 86 additions and 86 deletions

View File

@@ -74,38 +74,38 @@ afterEach(() => {
describe("registerPreActionHooks", () => { describe("registerPreActionHooks", () => {
function buildProgram() { function buildProgram() {
const program = new Command().name("openclaw"); const program = new Command().name("openclaw");
program.command("status").action(async () => {}); program.command("status").action(() => {});
program.command("doctor").action(async () => {}); program.command("doctor").action(() => {});
program.command("completion").action(async () => {}); program.command("completion").action(() => {});
program.command("secrets").action(async () => {}); program.command("secrets").action(() => {});
program program
.command("update") .command("update")
.command("status") .command("status")
.option("--json") .option("--json")
.action(async () => {}); .action(() => {});
const config = program.command("config"); const config = program.command("config");
config config
.command("set") .command("set")
.argument("<path>") .argument("<path>")
.argument("<value>") .argument("<value>")
.option("--json") .option("--json")
.action(async () => {}); .action(() => {});
program.command("channels").action(async () => {}); program.command("agents").action(() => {});
program.command("directory").action(async () => {}); program.command("configure").action(() => {});
program.command("agents").action(async () => {}); program.command("onboard").action(() => {});
program.command("configure").action(async () => {});
program.command("onboard").action(async () => {});
program program
.command("message") .command("message")
.command("send") .command("send")
.option("--json") .option("--json")
.action(async () => {}); .action(() => {});
registerPreActionHooks(program, "9.9.9-test"); registerPreActionHooks(program, "9.9.9-test");
return program; return program;
} }
async function runCommand(params: { parseArgv: string[]; processArgv?: string[] }) { async function runCommand(
const program = buildProgram(); params: { parseArgv: string[]; processArgv?: string[] },
program = buildProgram(),
) {
process.argv = params.processArgv ?? [...params.parseArgv]; process.argv = params.processArgv ?? [...params.parseArgv];
await program.parseAsync(params.parseArgv, { from: "user" }); await program.parseAsync(params.parseArgv, { from: "user" });
} }
@@ -143,29 +143,43 @@ describe("registerPreActionHooks", () => {
it("loads plugin registry for configure/onboard/agents commands", async () => { it("loads plugin registry for configure/onboard/agents commands", async () => {
const commands = ["configure", "onboard", "agents"] as const; const commands = ["configure", "onboard", "agents"] as const;
const program = buildProgram();
for (const command of commands) { for (const command of commands) {
vi.clearAllMocks(); vi.clearAllMocks();
await runCommand({ await runCommand(
parseArgv: [command], {
processArgv: ["node", "openclaw", command], parseArgv: [command],
}); processArgv: ["node", "openclaw", command],
},
program,
);
expect(ensurePluginRegistryLoadedMock, command).toHaveBeenCalledTimes(1); expect(ensurePluginRegistryLoadedMock, command).toHaveBeenCalledTimes(1);
} }
}); });
it("skips config guard for doctor, completion, and secrets commands", async () => { it("skips config guard for doctor, completion, and secrets commands", async () => {
await runCommand({ const program = buildProgram();
parseArgv: ["doctor"], await runCommand(
processArgv: ["node", "openclaw", "doctor"], {
}); parseArgv: ["doctor"],
await runCommand({ processArgv: ["node", "openclaw", "doctor"],
parseArgv: ["completion"], },
processArgv: ["node", "openclaw", "completion"], program,
}); );
await runCommand({ await runCommand(
parseArgv: ["secrets"], {
processArgv: ["node", "openclaw", "secrets"], parseArgv: ["completion"],
}); processArgv: ["node", "openclaw", "completion"],
},
program,
);
await runCommand(
{
parseArgv: ["secrets"],
processArgv: ["node", "openclaw", "secrets"],
},
program,
);
expect(ensureConfigReadyMock).not.toHaveBeenCalled(); expect(ensureConfigReadyMock).not.toHaveBeenCalled();
}); });
@@ -193,10 +207,14 @@ describe("registerPreActionHooks", () => {
}); });
it("suppresses doctor stdout for any --json output command", async () => { it("suppresses doctor stdout for any --json output command", async () => {
await runCommand({ const program = buildProgram();
parseArgv: ["message", "send", "--json"], await runCommand(
processArgv: ["node", "openclaw", "message", "send", "--json"], {
}); parseArgv: ["message", "send", "--json"],
processArgv: ["node", "openclaw", "message", "send", "--json"],
},
program,
);
expect(ensureConfigReadyMock).toHaveBeenCalledWith({ expect(ensureConfigReadyMock).toHaveBeenCalledWith({
runtime: runtimeMock, runtime: runtimeMock,
@@ -206,10 +224,13 @@ describe("registerPreActionHooks", () => {
vi.clearAllMocks(); vi.clearAllMocks();
await runCommand({ await runCommand(
parseArgv: ["update", "status", "--json"], {
processArgv: ["node", "openclaw", "update", "status", "--json"], parseArgv: ["update", "status", "--json"],
}); processArgv: ["node", "openclaw", "update", "status", "--json"],
},
program,
);
expect(ensureConfigReadyMock).toHaveBeenCalledWith({ expect(ensureConfigReadyMock).toHaveBeenCalledWith({
runtime: runtimeMock, runtime: runtimeMock,

View File

@@ -11,6 +11,10 @@ import { runSecurityAudit } from "./audit.js";
import * as skillScanner from "./skill-scanner.js"; import * as skillScanner from "./skill-scanner.js";
const isWindows = process.platform === "win32"; const isWindows = process.platform === "win32";
const windowsAuditEnv = {
USERNAME: "Tester",
USERDOMAIN: "DESKTOP-TEST",
};
function stubChannelPlugin(params: { function stubChannelPlugin(params: {
id: "discord" | "slack" | "telegram"; id: "discord" | "slack" | "telegram";
@@ -603,7 +607,7 @@ description: test skill
stateDir, stateDir,
configPath, configPath,
platform: "win32", platform: "win32",
env: { ...process.env, USERNAME: "Tester", USERDOMAIN: "DESKTOP-TEST" }, env: windowsAuditEnv,
execIcacls, execIcacls,
}); });
@@ -649,7 +653,7 @@ description: test skill
stateDir, stateDir,
configPath, configPath,
platform: "win32", platform: "win32",
env: { ...process.env, USERNAME: "Tester", USERDOMAIN: "DESKTOP-TEST" }, env: windowsAuditEnv,
execIcacls, execIcacls,
}); });

View File

@@ -70,6 +70,24 @@ exit 0`,
await writeExecutable( await writeExecutable(
path.join(sharedBinDir, "security"), path.join(sharedBinDir, "security"),
`#!/usr/bin/env bash `#!/usr/bin/env bash
if [[ "$1" == "cms" && "$2" == "-D" ]]; then
if [[ "$4" == *"one.mobileprovision" ]]; then
cat <<'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict><key>TeamIdentifier</key><array><string>AAAAA11111</string></array></dict></plist>
PLIST
exit 0
fi
if [[ "$4" == *"two.mobileprovision" ]]; then
cat <<'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict><key>TeamIdentifier</key><array><string>BBBBB22222</string></array></dict></plist>
PLIST
exit 0
fi
fi
exit 1`, exit 1`,
); );
}); });
@@ -92,7 +110,7 @@ exit 1`,
} }
it("falls back to Xcode-managed provisioning profiles when preference teams are empty", async () => { it("falls back to Xcode-managed provisioning profiles when preference teams are empty", async () => {
const { homeDir, binDir } = await createHomeDir(); const { homeDir } = await createHomeDir();
await mkdir(path.join(homeDir, "Library", "MobileDevice", "Provisioning Profiles"), { await mkdir(path.join(homeDir, "Library", "MobileDevice", "Provisioning Profiles"), {
recursive: true, recursive: true,
}); });
@@ -101,30 +119,9 @@ exit 1`,
"stub", "stub",
); );
await writeExecutable(
path.join(binDir, "security"),
`#!/usr/bin/env bash
if [[ "$1" == "cms" && "$2" == "-D" ]]; then
cat <<'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>TeamIdentifier</key>
<array>
<string>ABCDE12345</string>
</array>
</dict>
</plist>
PLIST
exit 0
fi
exit 0`,
);
const result = runScript(homeDir); const result = runScript(homeDir);
expect(result.ok).toBe(true); expect(result.ok).toBe(true);
expect(result.stdout).toBe("ABCDE12345"); expect(result.stdout).toBe("AAAAA11111");
}); });
it("prints actionable guidance when Xcode account exists but no Team ID is resolvable", async () => { it("prints actionable guidance when Xcode account exists but no Team ID is resolvable", async () => {
@@ -152,7 +149,7 @@ exit 1`,
}); });
it("honors IOS_PREFERRED_TEAM_ID when multiple profile teams are available", async () => { it("honors IOS_PREFERRED_TEAM_ID when multiple profile teams are available", async () => {
const { homeDir, binDir } = await createHomeDir(); const { homeDir } = await createHomeDir();
await mkdir(path.join(homeDir, "Library", "MobileDevice", "Provisioning Profiles"), { await mkdir(path.join(homeDir, "Library", "MobileDevice", "Provisioning Profiles"), {
recursive: true, recursive: true,
}); });
@@ -165,28 +162,6 @@ exit 1`,
"stub2", "stub2",
); );
await writeExecutable(
path.join(binDir, "security"),
`#!/usr/bin/env bash
if [[ "$1" == "cms" && "$2" == "-D" ]]; then
if [[ "$4" == *"one.mobileprovision" ]]; then
cat <<'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict><key>TeamIdentifier</key><array><string>AAAAA11111</string></array></dict></plist>
PLIST
exit 0
fi
cat <<'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict><key>TeamIdentifier</key><array><string>BBBBB22222</string></array></dict></plist>
PLIST
exit 0
fi
exit 0`,
);
const result = runScript(homeDir, { IOS_PREFERRED_TEAM_ID: "BBBBB22222" }); const result = runScript(homeDir, { IOS_PREFERRED_TEAM_ID: "BBBBB22222" });
expect(result.ok).toBe(true); expect(result.ok).toBe(true);
expect(result.stdout).toBe("BBBBB22222"); expect(result.stdout).toBe("BBBBB22222");