Skip to content

Commit 86a95b4

Browse files
authored
test: consolidate agent tool suites (#117886)
* test(agents): consolidate agent tool suites * test(agents): fix consolidated suite lint * test(plugins): remove duplicate loader mock
1 parent 37de567 commit 86a95b4

19 files changed

Lines changed: 1669 additions & 1798 deletions

‎src/agents/agent-tools.availability.test.ts‎

Lines changed: 0 additions & 55 deletions
This file was deleted.

‎src/agents/agent-tools.before-tool-call.approval.test.ts‎

Lines changed: 0 additions & 80 deletions
This file was deleted.

‎src/agents/agent-tools.before-tool-call.embedded-mode.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { HookRunner } from "../plugins/hooks.js";
1717
import { createEmptyPluginRegistry } from "../plugins/registry-empty.js";
1818
import { setActivePluginRegistry } from "../plugins/runtime.js";
1919
import { PluginApprovalResolutions } from "../plugins/types.js";
20+
import { resolveBeforeToolCallApprovalOutcome } from "./agent-tools.before-tool-call.approval.js";
2021
import { runBeforeToolCallHook } from "./agent-tools.before-tool-call.js";
2122
import { callGatewayTool } from "./tools/gateway.js";
2223

@@ -811,3 +812,81 @@ describe("runBeforeToolCallHook — embedded mode approvals", () => {
811812
}
812813
});
813814
});
815+
816+
describe("before_tool_call approval snapshots", () => {
817+
it("detaches deferred approval params from mutable hook and caller objects", async () => {
818+
const baseParams = { command: "safe", options: { cwd: "/safe" } };
819+
const overrideParams = { env: { MODE: "safe" } };
820+
821+
const outcome = await resolveBeforeToolCallApprovalOutcome({
822+
result: {
823+
requireApproval: {
824+
pluginId: "policy",
825+
title: "Needs approval",
826+
description: "Approval needed",
827+
},
828+
params: overrideParams,
829+
},
830+
approvalMode: "defer",
831+
toolName: "bash",
832+
baseParams,
833+
});
834+
835+
baseParams.options.cwd = "/unapproved";
836+
overrideParams.env.MODE = "unapproved";
837+
838+
expect(outcome).toMatchObject({
839+
blocked: false,
840+
params: { command: "safe", options: { cwd: "/safe" } },
841+
deferredApproval: {
842+
baseParams: { command: "safe", options: { cwd: "/safe" } },
843+
overrideParams: { env: { MODE: "safe" } },
844+
},
845+
});
846+
if (!outcome || outcome.blocked || !outcome.deferredApproval) {
847+
throw new Error("expected deferred approval outcome");
848+
}
849+
(outcome.params as typeof baseParams).options.cwd = "/outcome-mutated";
850+
expect(outcome.deferredApproval.baseParams).toEqual({
851+
command: "safe",
852+
options: { cwd: "/safe" },
853+
});
854+
});
855+
856+
const sharedMemoryCases: Array<
857+
[
858+
string,
859+
{
860+
baseParams: Record<string, unknown>;
861+
overrideParams?: Record<string, unknown>;
862+
},
863+
]
864+
> = [
865+
["base params", { baseParams: { shared: new Uint8Array(new SharedArrayBuffer(4)) } }],
866+
[
867+
"override params",
868+
{
869+
baseParams: { command: "safe" },
870+
overrideParams: { shared: new Uint8Array(new SharedArrayBuffer(4)) },
871+
},
872+
],
873+
];
874+
875+
it.each(sharedMemoryCases)("rejects shared memory in %s", async (_name, values) => {
876+
await expect(
877+
resolveBeforeToolCallApprovalOutcome({
878+
result: {
879+
requireApproval: {
880+
pluginId: "policy",
881+
title: "Needs approval",
882+
description: "Approval needed",
883+
},
884+
params: values.overrideParams,
885+
},
886+
approvalMode: "defer",
887+
toolName: "bash",
888+
baseParams: values.baseParams,
889+
}),
890+
).rejects.toThrow("before_tool_call mutable input isolation failed");
891+
});
892+
});

src/agents/agent-tools.cron-scope.test.ts renamed to src/agents/agent-tools.construction-routing.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Tests cron-triggered tool assembly.
2+
* Tests trigger and session routing during tool assembly.
33
* Ensures cron runs scope cron tool behavior to self-removal of the current
44
* job only.
55
*/
@@ -69,3 +69,48 @@ describe("createOpenClawCodingTools cron scope", () => {
6969
expect(firstOpenClawToolsOptions()?.cronSelfRemoveOnlyJobId).toBeUndefined();
7070
});
7171
});
72+
73+
const createLazyExecToolMock = vi.hoisted(() => vi.fn());
74+
75+
vi.mock("./lazy-exec-tool.js", async (importOriginal) => {
76+
const actual = await importOriginal<typeof import("./lazy-exec-tool.js")>();
77+
return {
78+
...actual,
79+
createLazyExecTool: (defaults: unknown) => {
80+
createLazyExecToolMock(defaults);
81+
return {
82+
name: "exec",
83+
description: "exec stub",
84+
parameters: { type: "object", properties: {} },
85+
execute: vi.fn(),
86+
};
87+
},
88+
};
89+
});
90+
91+
describe("createOpenClawCodingTools exec notification routing", () => {
92+
it("routes detached completions to the live session without changing process scope", () => {
93+
const liveSessionKey = "agent:main:channel:group:example:thread:25";
94+
const policySessionKey = "agent:main:runtime-policy";
95+
96+
createOpenClawCodingTools({
97+
sessionKey: policySessionKey,
98+
runSessionKey: liveSessionKey,
99+
toolConstructionPlan: {
100+
includeBaseCodingTools: false,
101+
includeShellTools: true,
102+
includeChannelTools: false,
103+
includeOpenClawTools: false,
104+
includePluginTools: false,
105+
},
106+
});
107+
108+
expect(createLazyExecToolMock).toHaveBeenCalledWith(
109+
expect.objectContaining({
110+
scopeKey: policySessionKey,
111+
sessionKey: policySessionKey,
112+
notifySessionKey: liveSessionKey,
113+
}),
114+
);
115+
});
116+
});

0 commit comments

Comments
 (0)