Skip to content

Commit b5f6230

Browse files
authored
refactor(system-agent): remove plugin install test seam (#123888)
1 parent 0a6b95a commit b5f6230

4 files changed

Lines changed: 35 additions & 48 deletions

File tree

‎src/system-agent/operations-parse.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ export type SystemAgentCommandDeps = {
5757
runGatewayRestart?: () => Promise<void | boolean>;
5858
runGatewayStart?: () => Promise<void>;
5959
runGatewayStop?: () => Promise<void>;
60-
runPluginInstall?: (
61-
spec: string,
62-
runtime: RuntimeEnv,
63-
options: { allowInstallPolicyWarningPrompt: false },
64-
) => Promise<void>;
6560
runPluginUninstall?: (pluginId: string, runtime: RuntimeEnv) => Promise<void>;
6661
runPluginsList?: (runtime: RuntimeEnv) => Promise<void>;
6762
runPluginsSearch?: (query: string, runtime: RuntimeEnv) => Promise<void>;

‎src/system-agent/operations.test.ts‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ const mockConfig = vi.hoisted(() => {
165165
};
166166
});
167167
const mockDaemonRestart = vi.hoisted(() => vi.fn(async () => true));
168+
const runPluginInstallCommandMock = vi.hoisted(() => vi.fn(async () => undefined));
168169
const mockScheduleGatewayRestart = vi.hoisted(() =>
169170
vi.fn(() => ({
170171
ok: true,
@@ -182,6 +183,9 @@ vi.mock("../cli/daemon-cli/lifecycle.js", () => ({
182183
runDaemonStop: vi.fn(async () => {}),
183184
runDaemonRestart: mockDaemonRestart,
184185
}));
186+
vi.mock("../cli/plugins-install-command.js", () => ({
187+
runPluginInstallCommand: runPluginInstallCommandMock,
188+
}));
185189
vi.mock("../infra/restart.js", async (importOriginal) => ({
186190
...(await importOriginal<typeof import("../infra/restart.js")>()),
187191
scheduleGatewaySigusr1Restart: mockScheduleGatewayRestart,
@@ -237,6 +241,7 @@ describe("system agent operations", () => {
237241
beforeEach(() => {
238242
mockConfig.reset();
239243
mockDaemonRestart.mockClear();
244+
runPluginInstallCommandMock.mockClear();
240245
mockScheduleGatewayRestart.mockClear();
241246
stateDirSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]);
242247
vi.stubEnv("OPENCLAW_TEST_FAST", "1");
@@ -957,36 +962,38 @@ describe("system agent operations", () => {
957962
const tempDir = opTempDirs.make("openclaw-plugin-install-");
958963
setTestEnvValue("OPENCLAW_STATE_DIR", tempDir);
959964
const { runtime, lines } = createSystemAgentTestRuntime();
960-
const runPluginInstall = vi.fn(async (spec: string, pluginRuntime: RuntimeEnv) => {
961-
pluginRuntime.log(`installed ${spec}`);
962-
});
963965

964966
const plan = await executeSystemAgentOperation(
965967
{ kind: "plugin-install", spec: "clawhub:openclaw-demo" },
966968
runtime,
967-
{ deps: { runPluginInstall } },
968969
);
969970
expectRecordFields(plan as unknown as Record<string, unknown>, {
970971
applied: false,
971972
message: "Plan: install plugin clawhub:openclaw-demo. Say yes to apply.",
972973
});
973-
expect(runPluginInstall).not.toHaveBeenCalled();
974+
expect(runPluginInstallCommandMock).not.toHaveBeenCalled();
974975

975976
const result = await executeSystemAgentOperation(
976977
{ kind: "plugin-install", spec: "clawhub:openclaw-demo" },
977978
runtime,
978979
{
979980
approved: true,
980-
deps: { runPluginInstall },
981981
auditDetails: { rescue: true },
982982
},
983983
);
984984
expect(result.applied).toBe(true);
985985

986-
const installCall = requireFirstMockCall(runPluginInstall, "runPluginInstall");
987-
expect(installCall[0]).toBe("clawhub:openclaw-demo");
988-
expectRuntimeArg(installCall[1]);
989-
expect(installCall[2]).toEqual({ allowInstallPolicyWarningPrompt: false });
986+
const [installParams] = requireFirstMockCall(
987+
runPluginInstallCommandMock,
988+
"runPluginInstallCommand",
989+
);
990+
const installRequest = requireRecord(installParams, "plugin install request");
991+
expectRecordFields(installRequest, {
992+
raw: "clawhub:openclaw-demo",
993+
opts: {},
994+
allowInstallPolicyWarningPrompt: false,
995+
});
996+
expectRuntimeArg(installRequest.runtime);
990997
expect(lines.join("\n")).toContain("[openclaw] done: plugin.install");
991998
const audit = readLastAuditEntry();
992999
expectAuditRecord(
@@ -1000,7 +1007,6 @@ describe("system agent operations", () => {
10001007
});
10011008

10021009
it("rejects an invalid approved plugin spec without exiting inside the executor", async () => {
1003-
const runPluginInstall = vi.fn();
10041010
mockConfig.readConfigFileSnapshot.mockClear();
10051011
const runtime: RuntimeEnv = {
10061012
log: vi.fn(),
@@ -1012,30 +1018,25 @@ describe("system agent operations", () => {
10121018
executeSystemAgentOperation(
10131019
{ kind: "plugin-install", spec: "https://example.test/plugin.tgz" },
10141020
runtime,
1015-
{ approved: true, deps: { runPluginInstall } },
1021+
{ approved: true },
10161022
),
10171023
).rejects.toThrow("accepts npm or ClawHub package specs only");
10181024

10191025
expect(runtime.error).not.toHaveBeenCalled();
10201026
expect(runtime.exit).not.toHaveBeenCalled();
1021-
expect(runPluginInstall).not.toHaveBeenCalled();
1027+
expect(runPluginInstallCommandMock).not.toHaveBeenCalled();
10221028
expect(mockConfig.readConfigFileSnapshot).not.toHaveBeenCalled();
10231029
});
10241030

10251031
it("rejects arbitrary plugin sources before proposing or installing them", async () => {
10261032
const { runtime } = createSystemAgentTestRuntime();
1027-
const runPluginInstall = vi.fn();
10281033

10291034
// Untrusted spec must be rejected on the unapproved path too, so a
10301035
// formatted "plan" never surfaces an arbitrary source for approval.
10311036
await expect(
1032-
executeSystemAgentOperation(
1033-
{ kind: "plugin-install", spec: "npm:@example/plugin" },
1034-
runtime,
1035-
{ deps: { runPluginInstall } },
1036-
),
1037+
executeSystemAgentOperation({ kind: "plugin-install", spec: "npm:@example/plugin" }, runtime),
10371038
).rejects.toThrow("trusted shell");
1038-
expect(runPluginInstall).not.toHaveBeenCalled();
1039+
expect(runPluginInstallCommandMock).not.toHaveBeenCalled();
10391040
});
10401041

10411042
it("uninstalls a non-route plugin only after approval and audits the write", async () => {

‎src/system-agent/plugin-install.ts‎

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,12 @@ export async function executePluginInstall(
2525
runtime,
2626
opts,
2727
run: async (ctx) => {
28-
const runPluginInstall =
29-
ctx.deps?.runPluginInstall ??
30-
(async (
31-
spec: string,
32-
pluginRuntime: RuntimeEnv,
33-
installOptions: { allowInstallPolicyWarningPrompt: false },
34-
) => {
35-
const { runPluginInstallCommand } = await import("../cli/plugins-install-command.js");
36-
await runPluginInstallCommand({
37-
raw: spec,
38-
opts: {},
39-
runtime: pluginRuntime,
40-
...installOptions,
41-
});
42-
});
4328
await ctx.commit(async () => {
44-
await runPluginInstall(operation.spec, createNoExitRuntime(ctx.runtime), {
29+
const { runPluginInstallCommand } = await import("../cli/plugins-install-command.js");
30+
await runPluginInstallCommand({
31+
raw: operation.spec,
32+
opts: {},
33+
runtime: createNoExitRuntime(ctx.runtime),
4534
allowInstallPolicyWarningPrompt: false,
4635
});
4736
});

‎src/system-agent/rescue-message.test.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ function readLastAuditEntry(): Record<string, unknown> {
2323
return (listSystemAgentAuditEntriesForTests().at(-1)?.value ?? {}) as Record<string, unknown>;
2424
}
2525

26+
const runPluginInstallCommandMock = vi.hoisted(() => vi.fn(async () => undefined));
27+
2628
const mockConfig = vi.hoisted(() => {
2729
const state = {
2830
path: "/tmp/openclaw.json",
@@ -83,6 +85,10 @@ const mockConfig = vi.hoisted(() => {
8385
};
8486
});
8587

88+
vi.mock("../cli/plugins-install-command.js", () => ({
89+
runPluginInstallCommand: runPluginInstallCommandMock,
90+
}));
91+
8692
vi.mock("../config/config.js", async (importOriginal) => {
8793
const actual = await importOriginal<typeof import("../config/config.js")>();
8894
return {
@@ -203,6 +209,7 @@ describe("OpenClaw rescue message", () => {
203209

204210
beforeEach(() => {
205211
mockConfig.reset();
212+
runPluginInstallCommandMock.mockClear();
206213
});
207214

208215
afterAll(async () => {
@@ -496,16 +503,11 @@ describe("OpenClaw rescue message", () => {
496503

497504
it("refuses plugin install from remote rescue", async () => {
498505
const cfg: OpenClawConfig = {};
499-
const deps = {
500-
runPluginInstall: vi.fn(async () => {
501-
throw new Error("remote rescue must not install plugins");
502-
}),
503-
};
504506

505507
await expect(
506-
runRescue("/openclaw plugin install clawhub:openclaw-demo", cfg, commandContext(), deps),
508+
runRescue("/openclaw plugin install clawhub:openclaw-demo", cfg),
507509
).resolves.toContain("cannot install plugins from a message channel");
508-
expect(deps.runPluginInstall).not.toHaveBeenCalled();
510+
expect(runPluginInstallCommandMock).not.toHaveBeenCalled();
509511
});
510512

511513
it("allows plugin list and search from remote rescue", async () => {

0 commit comments

Comments
 (0)